authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-09-04 22:32:23-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-09-04 22:32:23-07:00
log20eb749ad6983d74ce8285a725d84248d41fca00
tree9444a3a8983dad943ca9f07c998f170fe98a06b8
parent4e7effd3d38894f4d00bb0bb51ed34e237a4167e

generate debug info for global constants

See #41

5 files changed, 66 insertions(+), 4 deletions(-)

src/all_types.hpp+1
......@@ -1336,6 +1336,7 @@ struct VariableTableEntry {
13361336 BlockContext *block_context;
13371337 LLVMValueRef param_value_ref;
13381338 bool force_depends_on_compile_var;
1339 ImportTableEntry *import;
13391340};
13401341
13411342struct ErrorTableEntry {
src/analyze.cpp+1
......@@ -3757,6 +3757,7 @@ static VariableTableEntry *add_local_var(CodeGen *g, AstNode *source_node, Impor
37573757 VariableTableEntry *variable_entry = allocate<VariableTableEntry>(1);
37583758 variable_entry->type = type_entry;
37593759 variable_entry->block_context = context;
3760 variable_entry->import = import;
37603761
37613762 if (name) {
37623763 buf_init_from_buf(&variable_entry->name, name);
src/codegen.cpp+43-4
......@@ -3954,6 +3954,19 @@ static void build_label_blocks(CodeGen *g, FnTableEntry *fn) {
39543954 LLVMPositionBuilderAtEnd(g->builder, entry_block);
39553955}
39563956
3957static void gen_global_var(CodeGen *g, VariableTableEntry *var, LLVMValueRef init_val,
3958 TypeTableEntry *type_entry)
3959{
3960 assert(var->is_const);
3961 assert(var->import);
3962 assert(type_entry);
3963 bool is_local_to_unit = true;
3964 LLVMZigCreateGlobalVariable(g->dbuilder,
3965 var->block_context->di_scope, buf_ptr(&var->name),
3966 buf_ptr(&var->name), var->import->di_file, var->decl_node->line + 1,
3967 type_entry->di_type, is_local_to_unit, init_val);
3968}
3969
39573970static void do_code_gen(CodeGen *g) {
39583971 assert(!g->errors.length);
39593972
......@@ -3967,10 +3980,29 @@ static void do_code_gen(CodeGen *g) {
39673980 for (int i = 0; i < g->global_vars.length; i += 1) {
39683981 VariableTableEntry *var = g->global_vars.at(i);
39693982
3970 if (var->type->id == TypeTableEntryIdNumLitFloat ||
3971 var->type->id == TypeTableEntryIdNumLitInt ||
3972 !type_has_bits(var->type))
3973 {
3983 if (var->type->id == TypeTableEntryIdNumLitFloat) {
3984 // Generate debug info for it but that's it.
3985 ConstExprValue *const_val = &get_resolved_expr(var->val_node)->const_val;
3986 assert(const_val->ok);
3987 TypeTableEntry *var_type = g->builtin_types.entry_f64;
3988 LLVMValueRef init_val = LLVMConstReal(var_type->type_ref, const_val->data.x_bignum.data.x_float);
3989 gen_global_var(g, var, init_val, var_type);
3990 continue;
3991 }
3992
3993 if (var->type->id == TypeTableEntryIdNumLitInt) {
3994 // Generate debug info for it but that's it.
3995 ConstExprValue *const_val = &get_resolved_expr(var->val_node)->const_val;
3996 assert(const_val->ok);
3997 TypeTableEntry *var_type = const_val->data.x_bignum.is_negative ?
3998 g->builtin_types.entry_isize : g->builtin_types.entry_usize;
3999 LLVMValueRef init_val = LLVMConstInt(var_type->type_ref,
4000 bignum_to_twos_complement(&const_val->data.x_bignum), false);
4001 gen_global_var(g, var, init_val, var_type);
4002 continue;
4003 }
4004
4005 if (!type_has_bits(var->type)) {
39744006 continue;
39754007 }
39764008
......@@ -3981,6 +4013,8 @@ static void do_code_gen(CodeGen *g) {
39814013 if (var->decl_node->data.variable_declaration.is_extern) {
39824014 global_value = LLVMAddGlobal(g->module, var->type->type_ref, buf_ptr(&var->name));
39834015
4016 // TODO debug info for the extern variable
4017
39844018 LLVMSetLinkage(global_value, LLVMExternalLinkage);
39854019 } else {
39864020 AstNode *expr_node = var->decl_node->data.variable_declaration.expr;
......@@ -3999,6 +4033,11 @@ static void do_code_gen(CodeGen *g) {
39994033 LLVMSetInitializer(global_value, init_val);
40004034 LLVMSetLinkage(global_value, LLVMInternalLinkage);
40014035 LLVMSetUnnamedAddr(global_value, true);
4036
4037 // TODO debug info for function pointers
4038 if (var->is_const && var->type->id != TypeTableEntryIdFn) {
4039 gen_global_var(g, var, init_val, var->type);
4040 }
40024041 }
40034042
40044043 LLVMSetGlobalConstant(global_value, var->is_const);
src/zig_llvm.cpp+16
......@@ -401,6 +401,22 @@ LLVMZigDILocalVariable *LLVMZigCreateAutoVariable(LLVMZigDIBuilder *dbuilder,
401401 return reinterpret_cast<LLVMZigDILocalVariable*>(result);
402402}
403403
404LLVMZigDIGlobalVariable *LLVMZigCreateGlobalVariable(LLVMZigDIBuilder *dbuilder,
405 LLVMZigDIScope *scope, const char *name, const char *linkage_name, LLVMZigDIFile *file,
406 unsigned line_no, LLVMZigDIType *di_type, bool is_local_to_unit, LLVMValueRef constant_val)
407{
408 DIGlobalVariable *result = reinterpret_cast<DIBuilder*>(dbuilder)->createGlobalVariable(
409 reinterpret_cast<DIScope*>(scope),
410 name,
411 linkage_name,
412 reinterpret_cast<DIFile*>(file),
413 line_no,
414 reinterpret_cast<DIType*>(di_type),
415 is_local_to_unit,
416 reinterpret_cast<llvm::Constant *>(constant_val));
417 return reinterpret_cast<LLVMZigDIGlobalVariable*>(result);
418}
419
404420LLVMZigDILocalVariable *LLVMZigCreateParameterVariable(LLVMZigDIBuilder *dbuilder,
405421 LLVMZigDIScope *scope, const char *name, LLVMZigDIFile *file, unsigned line_no,
406422 LLVMZigDIType *type, bool always_preserve, unsigned flags, unsigned arg_no)
src/zig_llvm.hpp+5
......@@ -23,6 +23,7 @@ struct LLVMZigDILexicalBlock;
2323struct LLVMZigDISubprogram;
2424struct LLVMZigDISubroutineType;
2525struct LLVMZigDILocalVariable;
26struct LLVMZigDIGlobalVariable;
2627struct LLVMZigDILocation;
2728struct LLVMZigDIEnumerator;
2829struct LLVMZigInsertionPoint;
......@@ -125,6 +126,10 @@ LLVMZigDILocalVariable *LLVMZigCreateAutoVariable(LLVMZigDIBuilder *dbuilder,
125126 LLVMZigDIScope *scope, const char *name, LLVMZigDIFile *file, unsigned line_no,
126127 LLVMZigDIType *type, bool always_preserve, unsigned flags);
127128
129LLVMZigDIGlobalVariable *LLVMZigCreateGlobalVariable(LLVMZigDIBuilder *dbuilder,
130 LLVMZigDIScope *scope, const char *name, const char *linkage_name, LLVMZigDIFile *file,
131 unsigned line_no, LLVMZigDIType *di_type, bool is_local_to_unit, LLVMValueRef constant_val);
132
128133LLVMZigDILocalVariable *LLVMZigCreateParameterVariable(LLVMZigDIBuilder *dbuilder,
129134 LLVMZigDIScope *scope, const char *name, LLVMZigDIFile *file, unsigned line_no,
130135 LLVMZigDIType *type, bool always_preserve, unsigned flags, unsigned arg_no);