authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-07 22:11:04-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-07 22:11:04-07:00
log9c9ea935191190471c1b2fd30c8cffde74421456
treefe709fbb92cb58cc6027883e37fe55a7cb916798
parent1279fe0caaa83dd9f573dfe3c359098143224abc

integrate debug scopes with block context


5 files changed, 80 insertions(+), 42 deletions(-)

src/analyze.cpp+4-8
......@@ -443,10 +443,13 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
443443 TypeTableEntry *expected_type, AstNode *node)
444444{
445445 TypeTableEntry *return_type = nullptr;
446 assert(!node->codegen_node);
447 node->codegen_node = allocate<CodeGenNode>(1);
446448 switch (node->type) {
447449 case NodeTypeBlock:
448450 {
449451 BlockContext *child_context = new_block_context(node, context);
452 node->codegen_node->data.block_node.block_context = child_context;
450453 return_type = g->builtin_types.entry_void;
451454 for (int i = 0; i < node->data.block.statements.length; i += 1) {
452455 AstNode *child = node->data.block.statements.at(i);
......@@ -538,8 +541,6 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
538541 FnTableEntry *fn_table_entry = get_context_fn_entry(context);
539542 auto table_entry = fn_table_entry->label_table.maybe_get(&node->data.go_to.name);
540543 if (table_entry) {
541 assert(!node->codegen_node);
542 node->codegen_node = allocate<CodeGenNode>(1);
543544 node->codegen_node->data.label_entry = table_entry->value;
544545 table_entry->value->used = true;
545546 } else {
......@@ -792,12 +793,6 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
792793 assert(return_type);
793794 check_type_compatibility(g, node, expected_type, return_type);
794795
795 if (node->codegen_node) {
796 assert(node->type == NodeTypeGoto);
797 } else {
798 assert(node->type != NodeTypeGoto);
799 node->codegen_node = allocate<CodeGenNode>(1);
800 }
801796 node->codegen_node->expr_node.type_entry = return_type;
802797 node->codegen_node->expr_node.block_context = context;
803798
......@@ -837,6 +832,7 @@ static void analyze_top_level_declaration(CodeGen *g, ImportTableEntry *import,
837832 variable_entry->type = type;
838833 variable_entry->is_const = true;
839834 variable_entry->decl_node = param_decl_node;
835 variable_entry->arg_index = i;
840836
841837 LocalVariableTableEntry *existing_entry = find_local_variable(context, &variable_entry->name);
842838 if (!existing_entry) {
src/codegen.cpp+33-23
......@@ -102,8 +102,8 @@ static int count_non_void_params(CodeGen *g, ZigList<AstNode *> *params) {
102102}
103103
104104static void add_debug_source_node(CodeGen *g, AstNode *node) {
105 // TODO g->block_scopes.last() is not always correct and should probably integrate with BlockContext
106 LLVMZigSetCurrentDebugLocation(g->builder, node->line + 1, node->column + 1, g->block_scopes.last());
105 LLVMZigSetCurrentDebugLocation(g->builder, node->line + 1, node->column + 1,
106 g->cur_block_context->di_scope);
107107}
108108
109109static LLVMValueRef find_or_create_string(CodeGen *g, Buf *str) {
......@@ -484,13 +484,7 @@ static LLVMValueRef gen_if_expr(CodeGen *g, AstNode *node) {
484484static LLVMValueRef gen_block(CodeGen *g, AstNode *block_node, TypeTableEntry *implicit_return_type) {
485485 assert(block_node->type == NodeTypeBlock);
486486
487 ImportTableEntry *import = g->cur_fn->import_entry;
488
489 LLVMZigDILexicalBlock *di_block = LLVMZigCreateLexicalBlock(g->dbuilder, g->block_scopes.last(),
490 import->di_file, block_node->line + 1, block_node->column + 1);
491 g->block_scopes.append(LLVMZigLexicalBlockToScope(di_block));
492
493 add_debug_source_node(g, block_node);
487 g->cur_block_context = block_node->codegen_node->data.block_node.block_context;
494488
495489 LLVMValueRef return_value;
496490 for (int i = 0; i < block_node->data.block.statements.length; i += 1) {
......@@ -506,8 +500,6 @@ static LLVMValueRef gen_block(CodeGen *g, AstNode *block_node, TypeTableEntry *i
506500 }
507501 }
508502
509 g->block_scopes.pop();
510
511503 return return_value;
512504}
513505
......@@ -654,9 +646,6 @@ static LLVMZigDISubroutineType *create_di_function_type(CodeGen *g, AstNodeFnPro
654646static void do_code_gen(CodeGen *g) {
655647 assert(!g->errors.length);
656648
657 g->block_scopes.append(LLVMZigCompileUnitToScope(g->compile_unit));
658
659
660649 // Generate function prototypes
661650 for (int i = 0; i < g->fn_protos.length; i += 1) {
662651 FnTableEntry *fn_table_entry = g->fn_protos.at(i);
......@@ -718,8 +707,6 @@ static void do_code_gen(CodeGen *g) {
718707 create_di_function_type(g, fn_proto, import->di_file), fn_table_entry->internal_linkage,
719708 is_definition, scope_line, flags, is_optimized, fn);
720709
721 g->block_scopes.append(LLVMZigSubprogramToScope(subprogram));
722
723710 LLVMBasicBlockRef entry_block = LLVMAppendBasicBlock(fn, "entry");
724711 LLVMPositionBuilderAtEnd(g->builder, entry_block);
725712
......@@ -728,6 +715,9 @@ static void do_code_gen(CodeGen *g) {
728715
729716 FnDefNode *codegen_fn_def = &codegen_node->data.fn_def_node;
730717 assert(codegen_fn_def);
718
719 codegen_fn_def->block_context->di_scope = LLVMZigSubprogramToScope(subprogram);
720
731721 int non_void_param_count = count_non_void_params(g, &fn_proto->params);
732722 assert(non_void_param_count == (int)LLVMCountParams(fn));
733723 LLVMValueRef *params = allocate<LLVMValueRef>(non_void_param_count);
......@@ -746,15 +736,22 @@ static void do_code_gen(CodeGen *g) {
746736
747737 build_label_blocks(g, fn_def_node->data.fn_def.body);
748738
739 // Set up debug info for blocks and variables and
749740 // allocate all local variables
750741 for (int i = 0; i < codegen_fn_def->all_block_contexts.length; i += 1) {
751742 BlockContext *block_context = codegen_fn_def->all_block_contexts.at(i);
752743
753 // skip the block context for function parameters
754 if (block_context->node->type == NodeTypeFnDef) {
755 continue;
744 if (block_context->parent) {
745 LLVMZigDILexicalBlock *di_block = LLVMZigCreateLexicalBlock(g->dbuilder,
746 block_context->parent->di_scope,
747 import->di_file,
748 block_context->node->line + 1,
749 block_context->node->column + 1);
750 block_context->di_scope = LLVMZigLexicalBlockToScope(di_block);
756751 }
757752
753 g->cur_block_context = block_context;
754
758755 auto it = block_context->variable_table.entry_iterator();
759756 for (;;) {
760757 auto *entry = it.next();
......@@ -765,16 +762,29 @@ static void do_code_gen(CodeGen *g) {
765762 if (var->type == g->builtin_types.entry_void)
766763 continue;
767764
768 add_debug_source_node(g, var->decl_node);
769 var->value_ref = LLVMBuildAlloca(g->builder, var->type->type_ref, buf_ptr(&var->name));
765 unsigned tag;
766 unsigned arg_no;
767 if (block_context->node->type == NodeTypeFnDef) {
768 tag = LLVMZigTag_DW_arg_variable();
769 arg_no = var->arg_index + 1;
770 } else {
771 tag = LLVMZigTag_DW_auto_variable();
772 arg_no = 0;
773
774 add_debug_source_node(g, var->decl_node);
775 var->value_ref = LLVMBuildAlloca(g->builder, var->type->type_ref, buf_ptr(&var->name));
776 }
777
778 var->di_loc_var = LLVMZigCreateLocalVariable(g->dbuilder, tag,
779 block_context->di_scope, buf_ptr(&var->name),
780 import->di_file, var->decl_node->line + 1,
781 var->type->di_type, !g->strip_debug_symbols, 0, arg_no);
770782 }
771783 }
772784
773785 TypeTableEntry *implicit_return_type = codegen_fn_def->implicit_return_type;
774786 gen_block(g, fn_def_node->data.fn_def.body, implicit_return_type);
775787
776 g->block_scopes.pop();
777
778788 }
779789 assert(!g->errors.length);
780790
src/semantic_info.hpp+11-1
......@@ -14,6 +14,7 @@
1414#include "errmsg.hpp"
1515
1616struct FnTableEntry;
17struct BlockContext;
1718
1819struct TypeTableEntry {
1920 LLVMTypeRef type_ref;
......@@ -96,7 +97,6 @@ struct CodeGen {
9697 bool is_native_target;
9798 Buf *root_source_dir;
9899 Buf *root_out_name;
99 ZigList<LLVMZigDIScope *> block_scopes;
100100
101101 // The function definitions this module includes. There must be a corresponding
102102 // fn_protos entry.
......@@ -108,6 +108,7 @@ struct CodeGen {
108108 OutType out_type;
109109 FnTableEntry *cur_fn;
110110 LLVMBasicBlockRef cur_basic_block;
111 BlockContext *cur_block_context;
111112 bool c_stdint_used;
112113 AstNode *root_export_decl;
113114 int version_major;
......@@ -125,6 +126,8 @@ struct LocalVariableTableEntry {
125126 bool is_const;
126127 bool is_ptr; // if true, value_ref is a pointer
127128 AstNode *decl_node;
129 LLVMZigDILocalVariable *di_loc_var;
130 int arg_index;
128131};
129132
130133struct BlockContext {
......@@ -132,6 +135,7 @@ struct BlockContext {
132135 BlockContext *root; // always points to the BlockContext with the NodeTypeFnDef
133136 BlockContext *parent; // nullptr when this is the root
134137 HashMap<Buf *, LocalVariableTableEntry *, buf_hash, buf_eql_buf> variable_table;
138 LLVMZigDIScope *di_scope;
135139};
136140
137141struct TypeNode {
......@@ -146,6 +150,7 @@ struct FnDefNode {
146150 TypeTableEntry *implicit_return_type;
147151 BlockContext *block_context;
148152 bool skip;
153 // Required to be a pre-order traversal of the AST. (parents must come before children)
149154 ZigList<BlockContext *> all_block_contexts;
150155};
151156
......@@ -160,6 +165,10 @@ struct AssignNode {
160165 LocalVariableTableEntry *var_entry;
161166};
162167
168struct BlockNode {
169 BlockContext *block_context;
170};
171
163172struct CodeGenNode {
164173 union {
165174 TypeNode type_node; // for NodeTypeType
......@@ -167,6 +176,7 @@ struct CodeGenNode {
167176 FnProtoNode fn_proto_node; // for NodeTypeFnProto
168177 LabelTableEntry *label_entry; // for NodeTypeGoto and NodeTypeLabel
169178 AssignNode assign_node; // for NodeTypeBinOpExpr where op is BinOpTypeAssign
179 BlockNode block_node; // for NodeTypeBlock
170180 } data;
171181 ExprNode expr_node; // for all the expression nodes
172182};
src/zig_llvm.cpp+25-10
......@@ -189,6 +189,14 @@ unsigned LLVMZigLang_DW_LANG_C99(void) {
189189 return dwarf::DW_LANG_C99;
190190}
191191
192unsigned LLVMZigTag_DW_auto_variable(void) {
193 return dwarf::DW_TAG_auto_variable;
194}
195
196unsigned LLVMZigTag_DW_arg_variable(void) {
197 return dwarf::DW_TAG_arg_variable;
198}
199
192200LLVMZigDIBuilder *LLVMZigCreateDIBuilder(LLVMModuleRef module, bool allow_unresolved) {
193201 DIBuilder *di_builder = new DIBuilder(*unwrap(module), allow_unresolved);
194202 return reinterpret_cast<LLVMZigDIBuilder *>(di_builder);
......@@ -211,16 +219,23 @@ LLVMZigDILexicalBlock *LLVMZigCreateLexicalBlock(LLVMZigDIBuilder *dbuilder, LLV
211219 return reinterpret_cast<LLVMZigDILexicalBlock*>(result);
212220}
213221
214/*
215LLVMZigDILocalVariable *
216
217 DILocalVariable *createLocalVariable(unsigned Tag, DIScope *Scope,
218 StringRef Name, DIFile *File,
219 unsigned LineNo, DIType *Ty,
220 bool AlwaysPreserve = false,
221 unsigned Flags = 0,
222 unsigned ArgNo = 0);
223 */
222
223LLVMZigDILocalVariable *LLVMZigCreateLocalVariable(LLVMZigDIBuilder *dbuilder, unsigned tag,
224 LLVMZigDIScope *scope, const char *name, LLVMZigDIFile *file, unsigned line_no,
225 LLVMZigDIType *type, bool always_preserve, unsigned flags, unsigned arg_no)
226{
227 DILocalVariable *result = reinterpret_cast<DIBuilder*>(dbuilder)->createLocalVariable(
228 tag,
229 reinterpret_cast<DIScope*>(scope),
230 name,
231 reinterpret_cast<DIFile*>(file),
232 line_no,
233 reinterpret_cast<DIType*>(type),
234 always_preserve,
235 flags,
236 arg_no);
237 return reinterpret_cast<LLVMZigDILocalVariable*>(result);
238}
224239
225240LLVMZigDIScope *LLVMZigLexicalBlockToScope(LLVMZigDILexicalBlock *lexical_block) {
226241 DIScope *scope = reinterpret_cast<DILexicalBlock*>(lexical_block);
src/zig_llvm.hpp+7
......@@ -22,6 +22,7 @@ struct LLVMZigDIFile;
2222struct LLVMZigDILexicalBlock;
2323struct LLVMZigDISubprogram;
2424struct LLVMZigDISubroutineType;
25struct LLVMZigDILocalVariable;
2526struct LLVMZigInsertionPoint;
2627
2728void LLVMZigInitializeLoopStrengthReducePass(LLVMPassRegistryRef R);
......@@ -54,6 +55,8 @@ LLVMZigDISubroutineType *LLVMZigCreateSubroutineType(LLVMZigDIBuilder *dibuilder
5455unsigned LLVMZigEncoding_DW_ATE_unsigned(void);
5556unsigned LLVMZigEncoding_DW_ATE_signed(void);
5657unsigned LLVMZigLang_DW_LANG_C99(void);
58unsigned LLVMZigTag_DW_auto_variable(void);
59unsigned LLVMZigTag_DW_arg_variable(void);
5760
5861LLVMZigDIBuilder *LLVMZigCreateDIBuilder(LLVMModuleRef module, bool allow_unresolved);
5962
......@@ -64,6 +67,10 @@ LLVMZigDIScope *LLVMZigCompileUnitToScope(LLVMZigDICompileUnit *compile_unit);
6467LLVMZigDIScope *LLVMZigFileToScope(LLVMZigDIFile *difile);
6568LLVMZigDIScope *LLVMZigSubprogramToScope(LLVMZigDISubprogram *subprogram);
6669
70LLVMZigDILocalVariable *LLVMZigCreateLocalVariable(LLVMZigDIBuilder *dbuilder, unsigned tag,
71 LLVMZigDIScope *scope, const char *name, LLVMZigDIFile *file, unsigned line_no,
72 LLVMZigDIType *type, bool always_preserve, unsigned flags, unsigned arg_no);
73
6774LLVMZigDILexicalBlock *LLVMZigCreateLexicalBlock(LLVMZigDIBuilder *dbuilder, LLVMZigDIScope *scope,
6875 LLVMZigDIFile *file, unsigned line, unsigned col);
6976