gingerBill 1 year ago
parent
commit
e05315831f
1 changed files with 21 additions and 20 deletions
  1. 21 20
      src/llvm_backend_stmt.cpp

+ 21 - 20
src/llvm_backend_stmt.cpp

@@ -2157,6 +2157,16 @@ gb_internal void lb_build_if_stmt(lbProcedure *p, Ast *node) {
 	lb_open_scope(p, is->scope); // Scope #1
 	lb_open_scope(p, is->scope); // Scope #1
 	defer (lb_close_scope(p, lbDeferExit_Default, nullptr));
 	defer (lb_close_scope(p, lbDeferExit_Default, nullptr));
 
 
+	lbBlock *then = lb_create_block(p, "if.then");
+	lbBlock *done = lb_create_block(p, "if.done");
+	lbBlock *else_ = done;
+	if (is->else_stmt != nullptr) {
+		else_ = lb_create_block(p, "if.else");
+	}
+	if (is->label != nullptr) {
+		lbTargetList *tl = lb_push_target_list(p, is->label, done, nullptr, nullptr);
+		tl->is_block = true;
+	}
 	if (is->init != nullptr) {
 	if (is->init != nullptr) {
 		lbBlock *init = lb_create_block(p, "if.init");
 		lbBlock *init = lb_create_block(p, "if.init");
 		lb_emit_jump(p, init);
 		lb_emit_jump(p, init);
@@ -2164,22 +2174,12 @@ gb_internal void lb_build_if_stmt(lbProcedure *p, Ast *node) {
 
 
 		lb_build_stmt(p, is->init);
 		lb_build_stmt(p, is->init);
 	}
 	}
-	lbBlock *then = lb_create_block(p, "if.then");
-	lbBlock *done = lb_create_block(p, "if.done");
-	lbBlock *else_ = done;
-	if (is->else_stmt != nullptr) {
-		else_ = lb_create_block(p, "if.else");
-	}
 
 
 	lbValue cond = lb_build_cond(p, is->cond, then, else_);
 	lbValue cond = lb_build_cond(p, is->cond, then, else_);
 	// Note `cond.value` only set for non-and/or conditions and const negs so that the `LLVMIsConstant()`
 	// Note `cond.value` only set for non-and/or conditions and const negs so that the `LLVMIsConstant()`
 	// and `LLVMConstIntGetZExtValue()` calls below will be valid and `LLVMInstructionEraseFromParent()`
 	// and `LLVMConstIntGetZExtValue()` calls below will be valid and `LLVMInstructionEraseFromParent()`
 	// will target the correct (& only) branch statement
 	// will target the correct (& only) branch statement
 
 
-	if (is->label != nullptr) {
-		lbTargetList *tl = lb_push_target_list(p, is->label, done, nullptr, nullptr);
-		tl->is_block = true;
-	}
 
 
 	if (cond.value && LLVMIsConstant(cond.value)) {
 	if (cond.value && LLVMIsConstant(cond.value)) {
 		// NOTE(bill): Do a compile time short circuit for when the condition is constantly known.
 		// NOTE(bill): Do a compile time short circuit for when the condition is constantly known.
@@ -2244,15 +2244,6 @@ gb_internal void lb_build_for_stmt(lbProcedure *p, Ast *node) {
 	if (p->debug_info != nullptr) {
 	if (p->debug_info != nullptr) {
 		LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_ast(p, node));
 		LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_ast(p, node));
 	}
 	}
-
-	if (fs->init != nullptr) {
-	#if 1
-		lbBlock *init = lb_create_block(p, "for.init");
-		lb_emit_jump(p, init);
-		lb_start_block(p, init);
-	#endif
-		lb_build_stmt(p, fs->init);
-	}
 	lbBlock *body = lb_create_block(p, "for.body");
 	lbBlock *body = lb_create_block(p, "for.body");
 	lbBlock *done = lb_create_block(p, "for.done"); // NOTE(bill): Append later
 	lbBlock *done = lb_create_block(p, "for.done"); // NOTE(bill): Append later
 	lbBlock *loop = body;
 	lbBlock *loop = body;
@@ -2264,6 +2255,17 @@ gb_internal void lb_build_for_stmt(lbProcedure *p, Ast *node) {
 		post = lb_create_block(p, "for.post");
 		post = lb_create_block(p, "for.post");
 	}
 	}
 
 
+	lb_push_target_list(p, fs->label, done, post, nullptr);
+
+	if (fs->init != nullptr) {
+	#if 1
+		lbBlock *init = lb_create_block(p, "for.init");
+		lb_emit_jump(p, init);
+		lb_start_block(p, init);
+	#endif
+		lb_build_stmt(p, fs->init);
+	}
+
 	lb_emit_jump(p, loop);
 	lb_emit_jump(p, loop);
 	lb_start_block(p, loop);
 	lb_start_block(p, loop);
 
 
@@ -2276,7 +2278,6 @@ gb_internal void lb_build_for_stmt(lbProcedure *p, Ast *node) {
 		lb_start_block(p, body);
 		lb_start_block(p, body);
 	}
 	}
 
 
-	lb_push_target_list(p, fs->label, done, post, nullptr);
 
 
 	lb_build_stmt(p, fs->body);
 	lb_build_stmt(p, fs->body);