diff --git a/src/codegen.cpp b/src/codegen.cpp index b72303a040..0d53a12c97 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -528,7 +528,15 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) { return nullptr; } else { add_debug_source_node(g, node); - return LLVMBuildStore(g->builder, value, variable->value_ref); + variable->value_ref = LLVMBuildAlloca(g->builder, + variable->type->type_ref, buf_ptr(&variable->name)); + LLVMValueRef store_instr = LLVMBuildStore(g->builder, value, variable->value_ref); + + LLVMZigDILocation *debug_loc = LLVMZigGetDebugLoc(node->line + 1, node->column + 1, + g->cur_block_context->di_scope); + LLVMZigInsertDeclare(g->dbuilder, variable->value_ref, variable->di_loc_var, + debug_loc, store_instr); + return nullptr; } } case NodeTypeCastExpr: @@ -577,6 +585,7 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) { if (variable->type == g->builtin_types.entry_void) { return nullptr; } else if (variable->is_ptr) { + add_debug_source_node(g, node); return LLVMBuildLoad(g->builder, variable->value_ref, ""); } else { return variable->value_ref; @@ -770,9 +779,6 @@ static void do_code_gen(CodeGen *g) { } else { tag = LLVMZigTag_DW_auto_variable(); arg_no = 0; - - add_debug_source_node(g, var->decl_node); - var->value_ref = LLVMBuildAlloca(g->builder, var->type->type_ref, buf_ptr(&var->name)); } var->di_loc_var = LLVMZigCreateLocalVariable(g->dbuilder, tag, diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index bef47b07fe..43dd68b6b0 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -304,6 +304,24 @@ void LLVMZigRestoreInsertPoint(LLVMBuilderRef builder, LLVMZigInsertionPoint *ip unwrap(builder)->restoreIP(*ip); } + +LLVMValueRef LLVMZigInsertDeclare(LLVMZigDIBuilder *dibuilder, LLVMValueRef storage, + LLVMZigDILocalVariable *var_info, LLVMZigDILocation *debug_loc, LLVMValueRef insert_before_instr) +{ + Instruction *result = reinterpret_cast(dibuilder)->insertDeclare( + unwrap(storage), + reinterpret_cast(var_info), + reinterpret_cast(dibuilder)->createExpression(), + reinterpret_cast(debug_loc), + static_cast(unwrap(insert_before_instr))); + return wrap(result); +} + +LLVMZigDILocation *LLVMZigGetDebugLoc(unsigned line, unsigned col, LLVMZigDIScope *scope) { + DebugLoc debug_loc = DebugLoc::get(line, col, reinterpret_cast(scope), nullptr); + return reinterpret_cast(debug_loc.get()); +} + //------------------------------------ enum FloatAbi { diff --git a/src/zig_llvm.hpp b/src/zig_llvm.hpp index 8ad63c77c1..7b5b14ba10 100644 --- a/src/zig_llvm.hpp +++ b/src/zig_llvm.hpp @@ -23,6 +23,7 @@ struct LLVMZigDILexicalBlock; struct LLVMZigDISubprogram; struct LLVMZigDISubroutineType; struct LLVMZigDILocalVariable; +struct LLVMZigDILocation; struct LLVMZigInsertionPoint; void LLVMZigInitializeLoopStrengthReducePass(LLVMPassRegistryRef R); @@ -91,6 +92,10 @@ void LLVMZigDIBuilderFinalize(LLVMZigDIBuilder *dibuilder); LLVMZigInsertionPoint *LLVMZigSaveInsertPoint(LLVMBuilderRef builder); void LLVMZigRestoreInsertPoint(LLVMBuilderRef builder, LLVMZigInsertionPoint *point); +LLVMValueRef LLVMZigInsertDeclare(LLVMZigDIBuilder *dibuilder, LLVMValueRef storage, + LLVMZigDILocalVariable *var_info, LLVMZigDILocation *debug_loc, LLVMValueRef insert_before_instr); +LLVMZigDILocation *LLVMZigGetDebugLoc(unsigned line, unsigned col, LLVMZigDIScope *scope); + /* * This stuff is not LLVM API but it depends on the LLVM C++ API so we put it here.