From fb1e3a5be976c76f821abf387bdd69446f2ab07b Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 2 Jan 2016 20:42:32 -0700 Subject: [PATCH] codegen: emit debug metadata for parameters --- src/analyze.cpp | 3 +++ src/analyze.hpp | 5 +++++ src/codegen.cpp | 19 ++++++++++++++++++- src/zig_llvm.cpp | 11 +++++++++++ src/zig_llvm.hpp | 2 ++ 5 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index 4ebf10a5b6..7e3f80439b 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -1898,6 +1898,9 @@ static void analyze_top_level_declaration(CodeGen *g, ImportTableEntry *import, variable_entry->decl_node = param_decl_node; variable_entry->arg_index = i; + alloc_codegen_node(param_decl_node); + param_decl_node->codegen_node->data.param_decl_node.variable = variable_entry; + VariableTableEntry *existing_entry = find_local_variable(context, &variable_entry->name); if (!existing_entry) { // unique definition diff --git a/src/analyze.hpp b/src/analyze.hpp index 8152e16686..2cde26e8c7 100644 --- a/src/analyze.hpp +++ b/src/analyze.hpp @@ -322,6 +322,10 @@ struct IfVarNode { BlockContext *block_context; }; +struct ParamDeclNode { + VariableTableEntry *variable; +}; + struct CodeGenNode { union { TypeNode type_node; // for NodeTypeType @@ -338,6 +342,7 @@ struct CodeGenNode { StructValFieldNode struct_val_field_node; // for NodeTypeStructValueField StructValExprNode struct_val_expr_node; // for NodeTypeStructValueExpr IfVarNode if_var_node; // for NodeTypeStructValueExpr + ParamDeclNode param_decl_node; // for NodeTypeParamDecl } data; ExprNode expr_node; // for all the expression nodes }; diff --git a/src/codegen.cpp b/src/codegen.cpp index 699487a1b7..f2660f58a1 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -1530,7 +1530,8 @@ static void do_code_gen(CodeGen *g) { non_void_index += 1; } - build_label_blocks(g, fn_def_node->data.fn_def.body); + AstNode *body_node = fn_def_node->data.fn_def.body; + build_label_blocks(g, body_node); // Set up debug info for blocks and variables and // allocate all local variables @@ -1593,6 +1594,22 @@ static void do_code_gen(CodeGen *g) { } } + // create debug variable declarations for parameters + for (int param_i = 0; param_i < fn_proto->params.length; param_i += 1) { + AstNode *param_decl = fn_proto->params.at(param_i); + assert(param_decl->type == NodeTypeParamDecl); + + if (is_param_decl_type_void(g, param_decl)) + continue; + + VariableTableEntry *variable = param_decl->codegen_node->data.param_decl_node.variable; + + LLVMZigDILocation *debug_loc = LLVMZigGetDebugLoc(param_decl->line + 1, param_decl->column + 1, + codegen_fn_def->block_context->di_scope); + LLVMZigInsertDeclareAtEnd(g->dbuilder, variable->value_ref, variable->di_loc_var, debug_loc, + entry_block); + } + TypeTableEntry *implicit_return_type = codegen_fn_def->implicit_return_type; gen_block(g, fn_def_node->data.fn_def.body, implicit_return_type); diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index 13e9dc956e..122ddf4843 100644 --- a/src/zig_llvm.cpp +++ b/src/zig_llvm.cpp @@ -387,6 +387,17 @@ void LLVMZigRestoreInsertPoint(LLVMBuilderRef builder, LLVMZigInsertionPoint *ip unwrap(builder)->restoreIP(*ip); } +LLVMValueRef LLVMZigInsertDeclareAtEnd(LLVMZigDIBuilder *dibuilder, LLVMValueRef storage, + LLVMZigDILocalVariable *var_info, LLVMZigDILocation *debug_loc, LLVMBasicBlockRef basic_block_ref) +{ + Instruction *result = reinterpret_cast(dibuilder)->insertDeclare( + unwrap(storage), + reinterpret_cast(var_info), + reinterpret_cast(dibuilder)->createExpression(), + reinterpret_cast(debug_loc), + static_cast(unwrap(basic_block_ref))); + return wrap(result); +} LLVMValueRef LLVMZigInsertDeclare(LLVMZigDIBuilder *dibuilder, LLVMValueRef storage, LLVMZigDILocalVariable *var_info, LLVMZigDILocation *debug_loc, LLVMValueRef insert_before_instr) diff --git a/src/zig_llvm.hpp b/src/zig_llvm.hpp index 2e7747c12d..7f14d4aa17 100644 --- a/src/zig_llvm.hpp +++ b/src/zig_llvm.hpp @@ -113,6 +113,8 @@ void LLVMZigDIBuilderFinalize(LLVMZigDIBuilder *dibuilder); LLVMZigInsertionPoint *LLVMZigSaveInsertPoint(LLVMBuilderRef builder); void LLVMZigRestoreInsertPoint(LLVMBuilderRef builder, LLVMZigInsertionPoint *point); +LLVMValueRef LLVMZigInsertDeclareAtEnd(LLVMZigDIBuilder *dibuilder, LLVMValueRef storage, + LLVMZigDILocalVariable *var_info, LLVMZigDILocation *debug_loc, LLVMBasicBlockRef basic_block_ref); LLVMValueRef LLVMZigInsertDeclare(LLVMZigDIBuilder *dibuilder, LLVMValueRef storage, LLVMZigDILocalVariable *var_info, LLVMZigDILocation *debug_loc, LLVMValueRef insert_before_instr); LLVMZigDILocation *LLVMZigGetDebugLoc(unsigned line, unsigned col, LLVMZigDIScope *scope);