authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-11-30 14:13:04-07:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2015-11-30 14:13:04-07:00
logf36255b670f2bcec368a9ce4622af194b5b71d70
tree2cd89abbb70fa803d7adf1c1de7923a0dbb4409d
parent757ebefd6075ba2fb3c33af717a2b188347600e9
parent4ccb98bdcef86b2dac3bd1c8f01b9b823de6e9a4

Merge branch 'master' of github.com:andrewrk/zig


5 files changed, 198 insertions(+), 38 deletions(-)

src/analyze.cpp+8-7
......@@ -81,7 +81,7 @@ static void resolve_type(CodeGen *g, AstNode *node) {
8181 entry->type_ref = LLVMPointerType(child_type_node->entry->type_ref, 0);
8282 buf_resize(&entry->name, 0);
8383 buf_appendf(&entry->name, "*%s %s", const_or_mut_str, buf_ptr(&child_type_node->entry->name));
84 entry->di_type = g->dbuilder->createPointerType(child_type_node->entry->di_type,
84 entry->di_type = LLVMZigCreateDebugPointerType(g->dbuilder, child_type_node->entry->di_type,
8585 g->pointer_size_bytes * 8, g->pointer_size_bytes * 8, buf_ptr(&entry->name));
8686 g->type_table.put(&entry->name, entry);
8787 type_node->entry = entry;
......@@ -431,7 +431,8 @@ static void define_primitive_types(CodeGen *g) {
431431 entry->id = TypeIdU8;
432432 entry->type_ref = LLVMInt8Type();
433433 buf_init_from_str(&entry->name, "u8");
434 entry->di_type = g->dbuilder->createBasicType(buf_ptr(&entry->name), 8, 8, llvm::dwarf::DW_ATE_unsigned);
434 entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name), 8, 8,
435 LLVMZigEncoding_DW_ATE_unsigned());
435436 g->type_table.put(&entry->name, entry);
436437 }
437438 {
......@@ -439,8 +440,8 @@ static void define_primitive_types(CodeGen *g) {
439440 entry->id = TypeIdI32;
440441 entry->type_ref = LLVMInt32Type();
441442 buf_init_from_str(&entry->name, "i32");
442 entry->di_type = g->dbuilder->createBasicType(buf_ptr(&entry->name), 32, 32,
443 llvm::dwarf::DW_ATE_signed);
443 entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name), 32, 32,
444 LLVMZigEncoding_DW_ATE_signed());
444445 g->type_table.put(&entry->name, entry);
445446 }
446447 {
......@@ -448,8 +449,8 @@ static void define_primitive_types(CodeGen *g) {
448449 entry->id = TypeIdVoid;
449450 entry->type_ref = LLVMVoidType();
450451 buf_init_from_str(&entry->name, "void");
451 entry->di_type = g->dbuilder->createBasicType(buf_ptr(&entry->name), 0, 0,
452 llvm::dwarf::DW_ATE_unsigned);
452 entry->di_type = LLVMZigCreateDebugBasicType(g->dbuilder, buf_ptr(&entry->name), 0, 0,
453 LLVMZigEncoding_DW_ATE_unsigned());
453454 g->type_table.put(&entry->name, entry);
454455
455456 // invalid types are void
......@@ -501,7 +502,7 @@ void semantic_analyze(CodeGen *g) {
501502 g->pointer_size_bytes = LLVMPointerSize(g->target_data_ref);
502503
503504 g->builder = LLVMCreateBuilder();
504 g->dbuilder = new llvm::DIBuilder(*llvm::unwrap(g->module), true);
505 g->dbuilder = LLVMZigCreateDIBuilder(g->module, true);
505506
506507
507508 define_primitive_types(g);
src/codegen.cpp+23-23
......@@ -70,7 +70,7 @@ static LLVMTypeRef to_llvm_type(AstNode *type_node) {
7070 return type_node->codegen_node->data.type_node.entry->type_ref;
7171}
7272
73static llvm::DIType *to_llvm_debug_type(AstNode *type_node) {
73static LLVMZigDIType *to_llvm_debug_type(AstNode *type_node) {
7474 assert(type_node->type == NodeTypeType);
7575 assert(type_node->codegen_node);
7676 assert(type_node->codegen_node->data.type_node.entry);
......@@ -86,9 +86,7 @@ static bool type_is_unreachable(AstNode *type_node) {
8686}
8787
8888static void add_debug_source_node(CodeGen *g, AstNode *node) {
89 llvm::unwrap(g->builder)->SetCurrentDebugLocation(llvm::DebugLoc::get(
90 node->line + 1, node->column + 1,
91 g->block_scopes.last()));
89 LLVMZigSetCurrentDebugLocation(g->builder, node->line + 1, node->column + 1, g->block_scopes.last());
9290}
9391
9492static LLVMValueRef find_or_create_string(CodeGen *g, Buf *str) {
......@@ -441,9 +439,9 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) {
441439static void gen_block(CodeGen *g, AstNode *block_node, bool add_implicit_return) {
442440 assert(block_node->type == NodeTypeBlock);
443441
444 llvm::DILexicalBlock *di_block = g->dbuilder->createLexicalBlock(g->block_scopes.last(),
442 LLVMZigDILexicalBlock *di_block = LLVMZigCreateLexicalBlock(g->dbuilder, g->block_scopes.last(),
445443 g->di_file, block_node->line + 1, block_node->column + 1);
446 g->block_scopes.append(di_block);
444 g->block_scopes.append(LLVMZigLexicalBlockToScope(di_block));
447445
448446 add_debug_source_node(g, block_node);
449447
......@@ -459,22 +457,27 @@ static void gen_block(CodeGen *g, AstNode *block_node, bool add_implicit_return)
459457 g->block_scopes.pop();
460458}
461459
462static llvm::DISubroutineType *create_di_function_type(CodeGen *g, AstNodeFnProto *fn_proto,
463 llvm::DIFile *di_file)
460static LLVMZigDISubroutineType *create_di_function_type(CodeGen *g, AstNodeFnProto *fn_proto,
461 LLVMZigDIFile *di_file)
464462{
465463 llvm::SmallVector<llvm::Metadata *, 8> types;
466464
467 llvm::DIType *return_type = to_llvm_debug_type(fn_proto->return_type);
465 llvm::DIType *return_type = reinterpret_cast<llvm::DIType*>(to_llvm_debug_type(fn_proto->return_type));
468466 types.push_back(return_type);
469467
470468 for (int i = 0; i < fn_proto->params.length; i += 1) {
471469 AstNode *param_node = fn_proto->params.at(i);
472470 assert(param_node->type == NodeTypeParamDecl);
473 llvm::DIType *param_type = to_llvm_debug_type(param_node->data.param_decl.type);
471 llvm::DIType *param_type = reinterpret_cast<llvm::DIType*>(to_llvm_debug_type(param_node->data.param_decl.type));
474472 types.push_back(param_type);
475473 }
476474
477 return g->dbuilder->createSubroutineType(di_file, g->dbuilder->getOrCreateTypeArray(types));
475 llvm::DIBuilder *dibuilder = reinterpret_cast<llvm::DIBuilder*>(g->dbuilder);
476
477 llvm::DISubroutineType *result = dibuilder->createSubroutineType(
478 reinterpret_cast<llvm::DIFile*>(di_file),
479 dibuilder->getOrCreateTypeArray(types));
480 return reinterpret_cast<LLVMZigDISubroutineType*>(result);
478481}
479482
480483void code_gen(CodeGen *g) {
......@@ -484,14 +487,14 @@ void code_gen(CodeGen *g) {
484487 bool is_optimized = g->build_type == CodeGenBuildTypeRelease;
485488 const char *flags = "";
486489 unsigned runtime_version = 0;
487 g->compile_unit = g->dbuilder->createCompileUnit(llvm::dwarf::DW_LANG_C99,
490 g->compile_unit = LLVMZigCreateCompileUnit(g->dbuilder, LLVMZigLang_DW_LANG_C99(),
488491 buf_ptr(&g->in_file), buf_ptr(&g->in_dir),
489492 buf_ptr(producer), is_optimized, flags, runtime_version,
490 "", llvm::DIBuilder::FullDebug, 0, !g->strip_debug_symbols);
493 "", 0, !g->strip_debug_symbols);
491494
492 g->block_scopes.append(g->compile_unit);
495 g->block_scopes.append(LLVMZigCompileUnitToScope(g->compile_unit));
493496
494 g->di_file = g->dbuilder->createFile(g->compile_unit->getFilename(), g->compile_unit->getDirectory());
497 g->di_file = LLVMZigCreateFile(g->dbuilder, buf_ptr(&g->in_file), buf_ptr(&g->in_dir));
495498
496499
497500 // Generate function prototypes
......@@ -543,18 +546,17 @@ void code_gen(CodeGen *g) {
543546 AstNodeFnProto *fn_proto = &proto_node->data.fn_proto;
544547
545548 // Add debug info.
546 llvm::DIScope *fn_scope = g->di_file;
549 LLVMZigDIScope *fn_scope = LLVMZigFileToScope(g->di_file);
547550 unsigned line_number = fn_def_node->line + 1;
548551 unsigned scope_line = line_number;
549552 bool is_definition = true;
550553 unsigned flags = 0;
551 llvm::Function *unwrapped_function = reinterpret_cast<llvm::Function*>(llvm::unwrap(fn));
552 llvm::DISubprogram *subprogram = g->dbuilder->createFunction(
554 LLVMZigDISubprogram *subprogram = LLVMZigCreateFunction(g->dbuilder,
553555 fn_scope, buf_ptr(&fn_proto->name), "", g->di_file, line_number,
554556 create_di_function_type(g, fn_proto, g->di_file), fn_table_entry->internal_linkage,
555 is_definition, scope_line, flags, is_optimized, unwrapped_function);
557 is_definition, scope_line, flags, is_optimized, fn);
556558
557 g->block_scopes.append(subprogram);
559 g->block_scopes.append(LLVMZigSubprogramToScope(subprogram));
558560
559561 LLVMBasicBlockRef entry_block = LLVMAppendBasicBlock(fn, "entry");
560562 LLVMPositionBuilderAtEnd(g->builder, entry_block);
......@@ -573,7 +575,7 @@ void code_gen(CodeGen *g) {
573575 }
574576 assert(!g->errors.length);
575577
576 g->dbuilder->finalize();
578 LLVMZigDIBuilderFinalize(g->dbuilder);
577579
578580 LLVMDumpModule(g->module);
579581
......@@ -897,5 +899,3 @@ void code_gen_link(CodeGen *g, const char *out_file) {
897899 generate_h_file(g);
898900 }
899901}
900
901
src/semantic_info.hpp+6-8
......@@ -10,9 +10,7 @@
1010
1111#include "codegen.hpp"
1212#include "hash_map.hpp"
13
14#include <llvm/IR/DIBuilder.h>
15#include <llvm/IR/DiagnosticInfo.h>
13#include "zig_llvm.hpp"
1614
1715struct FnTableEntry {
1816 LLVMValueRef fn_value;
......@@ -35,7 +33,7 @@ enum TypeId {
3533struct TypeTableEntry {
3634 TypeId id;
3735 LLVMTypeRef type_ref;
38 llvm::DIType *di_type;
36 LLVMZigDIType *di_type;
3937
4038 TypeTableEntry *pointer_child;
4139 bool pointer_is_const;
......@@ -50,8 +48,8 @@ struct CodeGen {
5048 AstNode *root;
5149 ZigList<ErrorMsg> errors;
5250 LLVMBuilderRef builder;
53 llvm::DIBuilder *dbuilder;
54 llvm::DICompileUnit *compile_unit;
51 LLVMZigDIBuilder *dbuilder;
52 LLVMZigDICompileUnit *compile_unit;
5553 HashMap<Buf *, FnTableEntry *, buf_hash, buf_eql_buf> fn_table;
5654 HashMap<Buf *, LLVMValueRef, buf_hash, buf_eql_buf> str_table;
5755 HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> type_table;
......@@ -66,8 +64,8 @@ struct CodeGen {
6664 bool is_native_target;
6765 Buf in_file;
6866 Buf in_dir;
69 ZigList<llvm::DIScope *> block_scopes;
70 llvm::DIFile *di_file;
67 ZigList<LLVMZigDIScope *> block_scopes;
68 LLVMZigDIFile *di_file;
7169 ZigList<FnTableEntry *> fn_defs;
7270 Buf *out_name;
7371 OutType out_type;
src/zig_llvm.cpp+115
......@@ -7,6 +7,13 @@
77
88#include "zig_llvm.hpp"
99
10/*
11 * The point of this file is to contain all the LLVM C++ API interaction so that:
12 * 1. The compile time of other files is kept under control.
13 * 2. Provide a C interface to the LLVM functions we need for self-hosting purposes.
14 * 3. Prevent C++ from infecting the rest of the project.
15 */
16
1017#include <llvm/InitializePasses.h>
1118#include <llvm/PassRegistry.h>
1219#include <llvm/MC/SubtargetFeature.h>
......@@ -18,6 +25,8 @@
1825#include <llvm/IR/Verifier.h>
1926#include <llvm/IR/Instructions.h>
2027#include <llvm/IR/IRBuilder.h>
28#include <llvm/IR/DIBuilder.h>
29#include <llvm/IR/DiagnosticInfo.h>
2130#include <llvm/Analysis/TargetLibraryInfo.h>
2231#include <llvm/Analysis/TargetTransformInfo.h>
2332#include <llvm/Transforms/IPO.h>
......@@ -122,3 +131,109 @@ LLVMValueRef LLVMZigBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *A
122131 call_inst->setCallingConv(CC);
123132 return wrap(unwrap(B)->Insert(call_inst));
124133}
134
135LLVMZigDIType *LLVMZigCreateDebugPointerType(LLVMZigDIBuilder *dibuilder, LLVMZigDIType *pointee_type,
136 uint64_t size_in_bits, uint64_t align_in_bits, const char *name)
137{
138 DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createPointerType(
139 reinterpret_cast<DIType*>(pointee_type), size_in_bits, align_in_bits, name);
140 return reinterpret_cast<LLVMZigDIType*>(di_type);
141}
142
143LLVMZigDIType *LLVMZigCreateDebugBasicType(LLVMZigDIBuilder *dibuilder, const char *name,
144 uint64_t size_in_bits, uint64_t align_in_bits, unsigned encoding)
145{
146 DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createBasicType(
147 name, size_in_bits, align_in_bits, encoding);
148 return reinterpret_cast<LLVMZigDIType*>(di_type);
149}
150
151unsigned LLVMZigEncoding_DW_ATE_unsigned(void) {
152 return dwarf::DW_ATE_unsigned;
153}
154
155unsigned LLVMZigEncoding_DW_ATE_signed(void) {
156 return dwarf::DW_ATE_signed;
157}
158
159unsigned LLVMZigLang_DW_LANG_C99(void) {
160 return dwarf::DW_LANG_C99;
161}
162
163LLVMZigDIBuilder *LLVMZigCreateDIBuilder(LLVMModuleRef module, bool allow_unresolved) {
164 DIBuilder *di_builder = new DIBuilder(*llvm::unwrap(module), allow_unresolved);
165 return reinterpret_cast<LLVMZigDIBuilder *>(di_builder);
166}
167
168void LLVMZigSetCurrentDebugLocation(LLVMBuilderRef builder, int line, int column, LLVMZigDIScope *scope) {
169 unwrap(builder)->SetCurrentDebugLocation(llvm::DebugLoc::get(
170 line, column, reinterpret_cast<DIScope*>(scope)));
171}
172
173
174LLVMZigDILexicalBlock *LLVMZigCreateLexicalBlock(LLVMZigDIBuilder *dbuilder, LLVMZigDIScope *scope,
175 LLVMZigDIFile *file, unsigned line, unsigned col)
176{
177 DILexicalBlock *result = reinterpret_cast<DIBuilder*>(dbuilder)->createLexicalBlock(
178 reinterpret_cast<DIScope*>(scope),
179 reinterpret_cast<DIFile*>(file),
180 line,
181 col);
182 return reinterpret_cast<LLVMZigDILexicalBlock*>(result);
183}
184
185LLVMZigDIScope *LLVMZigLexicalBlockToScope(LLVMZigDILexicalBlock *lexical_block) {
186 DIScope *scope = reinterpret_cast<DILexicalBlock*>(lexical_block);
187 return reinterpret_cast<LLVMZigDIScope*>(scope);
188}
189
190LLVMZigDIScope *LLVMZigCompileUnitToScope(LLVMZigDICompileUnit *compile_unit) {
191 DIScope *scope = reinterpret_cast<DICompileUnit*>(compile_unit);
192 return reinterpret_cast<LLVMZigDIScope*>(scope);
193}
194
195LLVMZigDIScope *LLVMZigFileToScope(LLVMZigDIFile *difile) {
196 DIScope *scope = reinterpret_cast<DIFile*>(difile);
197 return reinterpret_cast<LLVMZigDIScope*>(scope);
198}
199
200LLVMZigDIScope *LLVMZigSubprogramToScope(LLVMZigDISubprogram *subprogram) {
201 DIScope *scope = reinterpret_cast<DISubprogram*>(subprogram);
202 return reinterpret_cast<LLVMZigDIScope*>(scope);
203}
204
205LLVMZigDICompileUnit *LLVMZigCreateCompileUnit(LLVMZigDIBuilder *dibuilder,
206 unsigned lang, const char *file, const char *dir, const char *producer,
207 bool is_optimized, const char *flags, unsigned runtime_version, const char *split_name,
208 uint64_t dwo_id, bool emit_debug_info)
209{
210 DICompileUnit *result = reinterpret_cast<DIBuilder*>(dibuilder)->createCompileUnit(
211 lang, file, dir, producer, is_optimized, flags, runtime_version, split_name,
212 DIBuilder::FullDebug, dwo_id, emit_debug_info);
213 return reinterpret_cast<LLVMZigDICompileUnit*>(result);
214}
215
216LLVMZigDIFile *LLVMZigCreateFile(LLVMZigDIBuilder *dibuilder, const char *filename, const char *directory) {
217 DIFile *result = reinterpret_cast<DIBuilder*>(dibuilder)->createFile(filename, directory);
218 return reinterpret_cast<LLVMZigDIFile*>(result);
219}
220
221LLVMZigDISubprogram *LLVMZigCreateFunction(LLVMZigDIBuilder *dibuilder, LLVMZigDIScope *scope,
222 const char *name, const char *linkage_name, LLVMZigDIFile *file, unsigned lineno,
223 LLVMZigDISubroutineType *ty, bool is_local_to_unit, bool is_definition, unsigned scope_line,
224 unsigned flags, bool is_optimized, LLVMValueRef function)
225{
226 llvm::Function *unwrapped_function = reinterpret_cast<llvm::Function*>(unwrap(function));
227 DISubprogram *result = reinterpret_cast<DIBuilder*>(dibuilder)->createFunction(
228 reinterpret_cast<DIScope*>(scope),
229 name, linkage_name,
230 reinterpret_cast<DIFile*>(file),
231 lineno,
232 reinterpret_cast<DISubroutineType*>(ty),
233 is_local_to_unit, is_definition, scope_line, flags, is_optimized, unwrapped_function);
234 return reinterpret_cast<LLVMZigDISubprogram*>(result);
235}
236
237void LLVMZigDIBuilderFinalize(LLVMZigDIBuilder *dibuilder) {
238 reinterpret_cast<DIBuilder*>(dibuilder)->finalize();
239}
src/zig_llvm.hpp+46
......@@ -14,6 +14,15 @@
1414#include <llvm-c/Initialization.h>
1515#include <llvm-c/TargetMachine.h>
1616
17struct LLVMZigDIType;
18struct LLVMZigDIBuilder;
19struct LLVMZigDICompileUnit;
20struct LLVMZigDIScope;
21struct LLVMZigDIFile;
22struct LLVMZigDILexicalBlock;
23struct LLVMZigDISubprogram;
24struct LLVMZigDISubroutineType;
25
1726void LLVMZigInitializeLoopStrengthReducePass(LLVMPassRegistryRef R);
1827void LLVMZigInitializeLowerIntrinsicsPass(LLVMPassRegistryRef R);
1928void LLVMZigInitializeUnreachableBlockElimPass(LLVMPassRegistryRef R);
......@@ -26,4 +35,41 @@ void LLVMZigOptimizeModule(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef
2635LLVMValueRef LLVMZigBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,
2736 unsigned NumArgs, unsigned CC, const char *Name);
2837
38
39LLVMZigDIType *LLVMZigCreateDebugPointerType(LLVMZigDIBuilder *dibuilder, LLVMZigDIType *pointee_type,
40 uint64_t size_in_bits, uint64_t align_in_bits, const char *name);
41
42LLVMZigDIType *LLVMZigCreateDebugBasicType(LLVMZigDIBuilder *dibuilder, const char *name,
43 uint64_t size_in_bits, uint64_t align_in_bits, unsigned encoding);
44
45unsigned LLVMZigEncoding_DW_ATE_unsigned(void);
46unsigned LLVMZigEncoding_DW_ATE_signed(void);
47unsigned LLVMZigLang_DW_LANG_C99(void);
48
49LLVMZigDIBuilder *LLVMZigCreateDIBuilder(LLVMModuleRef module, bool allow_unresolved);
50
51void LLVMZigSetCurrentDebugLocation(LLVMBuilderRef builder, int line, int column, LLVMZigDIScope *scope);
52
53LLVMZigDIScope *LLVMZigLexicalBlockToScope(LLVMZigDILexicalBlock *lexical_block);
54LLVMZigDIScope *LLVMZigCompileUnitToScope(LLVMZigDICompileUnit *compile_unit);
55LLVMZigDIScope *LLVMZigFileToScope(LLVMZigDIFile *difile);
56LLVMZigDIScope *LLVMZigSubprogramToScope(LLVMZigDISubprogram *subprogram);
57
58LLVMZigDILexicalBlock *LLVMZigCreateLexicalBlock(LLVMZigDIBuilder *dbuilder, LLVMZigDIScope *scope,
59 LLVMZigDIFile *file, unsigned line, unsigned col);
60
61LLVMZigDICompileUnit *LLVMZigCreateCompileUnit(LLVMZigDIBuilder *dibuilder,
62 unsigned lang, const char *file, const char *dir, const char *producer,
63 bool is_optimized, const char *flags, unsigned runtime_version, const char *split_name,
64 uint64_t dwo_id, bool emit_debug_info);
65
66LLVMZigDIFile *LLVMZigCreateFile(LLVMZigDIBuilder *dibuilder, const char *filename, const char *directory);
67
68LLVMZigDISubprogram *LLVMZigCreateFunction(LLVMZigDIBuilder *dibuilder, LLVMZigDIScope *scope,
69 const char *name, const char *linkage_name, LLVMZigDIFile *file, unsigned lineno,
70 LLVMZigDISubroutineType *ty, bool is_local_to_unit, bool is_definition, unsigned scope_line,
71 unsigned flags, bool is_optimized, LLVMValueRef function);
72
73void LLVMZigDIBuilderFinalize(LLVMZigDIBuilder *dibuilder);
74
2975#endif