2
0
Эх сурвалжийг харах

Integrate numerous debug fixes from #1877

gingerBill 3 жил өмнө
parent
commit
6c7e5748a8

+ 1 - 1
src/llvm_backend_debug.cpp

@@ -969,7 +969,7 @@ void lb_add_debug_local_variable(lbProcedure *p, LLVMValueRef ptr, Type *type, T
 	);
 
 	LLVMValueRef storage = ptr;
-	LLVMBasicBlockRef block = p->decl_block->block;
+	LLVMBasicBlockRef block = p->curr_block->block;
 	LLVMMetadataRef llvm_debug_loc = lb_debug_location_from_token_pos(p, token.pos);
 	LLVMMetadataRef llvm_expr = LLVMDIBuilderCreateExpression(m->debug_builder, nullptr, 0);
 	lb_set_llvm_metadata(m, ptr, llvm_expr);

+ 0 - 11
src/llvm_backend_expr.cpp

@@ -3068,17 +3068,6 @@ lbValue lb_build_expr_internal(lbProcedure *p, Ast *expr) {
 		return lb_const_value(p->module, type, tv.value);
 	}
 
-	#if 0
-	LLVMMetadataRef prev_debug_location = nullptr;
-	if (p->debug_info != nullptr) {
-		prev_debug_location = LLVMGetCurrentDebugLocation2(p->builder);
-		LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_ast(p, expr));
-	}
-	defer (if (prev_debug_location != nullptr) {
-		LLVMSetCurrentDebugLocation2(p->builder, prev_debug_location);
-	});
-	#endif
-
 	switch (expr->kind) {
 	case_ast_node(bl, BasicLit, expr);
 		TokenPos pos = bl->token.pos;

+ 4 - 4
src/llvm_backend_general.cpp

@@ -2746,10 +2746,6 @@ lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e, bool zero_init, i32 p
 		}
 	}
 
-	if (zero_init) {
-		lb_mem_zero_ptr(p, ptr, type, alignment);
-	}
-
 	lbValue val = {};
 	val.value = ptr;
 	val.type = alloc_type_pointer(type);
@@ -2759,6 +2755,10 @@ lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e, bool zero_init, i32 p
 		lb_add_debug_local_variable(p, ptr, type, e->token);
 	}
 
+	if (zero_init) {
+		lb_mem_zero_ptr(p, ptr, type, alignment);
+	}
+
 	return lb_addr(val);
 }
 

+ 41 - 14
src/llvm_backend_proc.cpp

@@ -434,6 +434,40 @@ void lb_start_block(lbProcedure *p, lbBlock *b) {
 	p->curr_block = b;
 }
 
+void lb_set_debug_position_to_procedure_begin(lbProcedure *p) {
+	if (p->debug_info == nullptr) {
+		return;
+	}
+	TokenPos pos = {};
+	if (p->body != nullptr) {
+		pos = ast_token(p->body).pos;
+	} else if (p->type_expr != nullptr) {
+		pos = ast_token(p->type_expr).pos;
+	} else if (p->entity != nullptr) {
+		pos = p->entity->token.pos;
+	}
+	if (pos.file_id != 0) {
+		LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_token_pos(p, pos));
+	}
+}
+
+void lb_set_debug_position_to_procedure_end(lbProcedure *p) {
+	if (p->debug_info == nullptr) {
+		return;
+	}
+	TokenPos pos = {};
+	if (p->body != nullptr) {
+		pos = ast_end_token(p->body).pos;
+	} else if (p->type_expr != nullptr) {
+		pos = ast_end_token(p->type_expr).pos;
+	} else if (p->entity != nullptr) {
+		pos = p->entity->token.pos;
+	}
+	if (pos.file_id != 0) {
+		LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_token_pos(p, pos));
+	}
+}
+
 void lb_begin_procedure_body(lbProcedure *p) {
 	DeclInfo *decl = decl_info_of_entity(p->entity);
 	if (decl != nullptr) {
@@ -565,29 +599,21 @@ void lb_begin_procedure_body(lbProcedure *p) {
 		lb_push_context_onto_stack_from_implicit_parameter(p);
 	}
 
-	lb_start_block(p, p->entry_block);
-
+	lb_set_debug_position_to_procedure_begin(p);
 	if (p->debug_info != nullptr) {
-		TokenPos pos = {};
-		if (p->body != nullptr) {
-			pos = ast_token(p->body).pos;
-		} else if (p->type_expr != nullptr) {
-			pos = ast_token(p->type_expr).pos;
-		} else if (p->entity != nullptr) {
-			pos = p->entity->token.pos;
-		}
-		if (pos.file_id != 0) {
-			LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_token_pos(p, pos));
-		}
-
 		if (p->context_stack.count != 0) {
+			p->curr_block = p->decl_block;
 			lb_add_debug_context_variable(p, lb_find_or_generate_context_ptr(p));
 		}
 
 	}
+
+	lb_start_block(p, p->entry_block);
 }
 
 void lb_end_procedure_body(lbProcedure *p) {
+	lb_set_debug_position_to_procedure_begin(p);
+
 	LLVMPositionBuilderAtEnd(p->builder, p->decl_block->block);
 	LLVMBuildBr(p->builder, p->entry_block->block);
 	LLVMPositionBuilderAtEnd(p->builder, p->curr_block->block);
@@ -599,6 +625,7 @@ void lb_end_procedure_body(lbProcedure *p) {
 		instr = LLVMGetLastInstruction(p->curr_block->block);
 		if (!lb_is_instr_terminating(instr)) {
 			lb_emit_defer_stmts(p, lbDeferExit_Return, nullptr);
+			lb_set_debug_position_to_procedure_end(p);
 			LLVMBuildRetVoid(p->builder);
 		}
 	}

+ 3 - 5
src/llvm_backend_stmt.cpp

@@ -1721,6 +1721,9 @@ void lb_build_for_stmt(lbProcedure *p, Ast *node) {
 	ast_node(fs, ForStmt, node);
 
 	lb_open_scope(p, fs->scope); // Open Scope here
+	if (p->debug_info != nullptr) {
+		LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_ast(p, node));
+	}
 
 	if (fs->init != nullptr) {
 	#if 1
@@ -1971,14 +1974,9 @@ void lb_build_stmt(lbProcedure *p, Ast *node) {
 		}
 	}
 
-	LLVMMetadataRef prev_debug_location = nullptr;
 	if (p->debug_info != nullptr) {
-		prev_debug_location = LLVMGetCurrentDebugLocation2(p->builder);
 		LLVMSetCurrentDebugLocation2(p->builder, lb_debug_location_from_ast(p, node));
 	}
-	defer (if (prev_debug_location != nullptr) {
-		LLVMSetCurrentDebugLocation2(p->builder, prev_debug_location);
-	});
 
 	u16 prev_state_flags = p->state_flags;
 	defer (p->state_flags = prev_state_flags);