| ... | ... | @@ -9,27 +9,63 @@ |
| 9 | 9 | #include "hash_map.hpp" |
| 10 | 10 | #include "zig_llvm.hpp" |
| 11 | 11 | #include "os.hpp" |
| 12 | #include "config.h" |
| 12 | 13 | |
| 13 | 14 | #include <stdio.h> |
| 14 | 15 | |
| 16 | #include <llvm/IR/DIBuilder.h> |
| 17 | #include <llvm/IR/DiagnosticInfo.h> |
| 18 | #include <llvm/IR/DiagnosticPrinter.h> |
| 19 | |
| 15 | 20 | struct FnTableEntry { |
| 16 | 21 | LLVMValueRef fn_value; |
| 17 | 22 | AstNode *proto_node; |
| 18 | 23 | }; |
| 19 | 24 | |
| 25 | enum TypeId { |
| 26 | TypeIdUserDefined, |
| 27 | TypeIdPointer, |
| 28 | TypeIdU8, |
| 29 | TypeIdI32, |
| 30 | TypeIdVoid, |
| 31 | TypeIdUnreachable, |
| 32 | }; |
| 33 | |
| 34 | struct TypeTableEntry { |
| 35 | TypeId id; |
| 36 | LLVMTypeRef type_ref; |
| 37 | llvm::DIType *di_type; |
| 38 | |
| 39 | TypeTableEntry *pointer_child; |
| 40 | bool pointer_is_const; |
| 41 | int user_defined_id; |
| 42 | Buf name; |
| 43 | TypeTableEntry *pointer_const_parent; |
| 44 | TypeTableEntry *pointer_mut_parent; |
| 45 | }; |
| 46 | |
| 20 | 47 | struct CodeGen { |
| 21 | 48 | LLVMModuleRef mod; |
| 22 | 49 | AstNode *root; |
| 23 | 50 | HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> fn_defs; |
| 24 | 51 | ZigList<ErrorMsg> errors; |
| 25 | 52 | LLVMBuilderRef builder; |
| 53 | llvm::DIBuilder *dbuilder; |
| 54 | llvm::DICompileUnit *compile_unit; |
| 26 | 55 | HashMap<Buf *, FnTableEntry *, buf_hash, buf_eql_buf> fn_table; |
| 27 | 56 | HashMap<Buf *, LLVMValueRef, buf_hash, buf_eql_buf> str_table; |
| 57 | HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> type_table; |
| 58 | TypeTableEntry *invalid_type_entry; |
| 59 | LLVMTargetDataRef target_data_ref; |
| 60 | unsigned pointer_size_bytes; |
| 61 | bool is_static; |
| 62 | LLVMTargetMachineRef target_machine; |
| 63 | Buf in_file; |
| 64 | Buf in_dir; |
| 28 | 65 | }; |
| 29 | 66 | |
| 30 | 67 | struct TypeNode { |
| 31 | | LLVMTypeRef type_ref; |
| 32 | | bool is_unreachable; |
| 68 | TypeTableEntry *entry; |
| 33 | 69 | }; |
| 34 | 70 | |
| 35 | 71 | struct CodeGenNode { |
| ... | ... | @@ -38,12 +74,16 @@ struct CodeGenNode { |
| 38 | 74 | } data; |
| 39 | 75 | }; |
| 40 | 76 | |
| 41 | | CodeGen *create_codegen(AstNode *root) { |
| 77 | CodeGen *create_codegen(AstNode *root, bool is_static, Buf *in_full_path) { |
| 42 | 78 | CodeGen *g = allocate<CodeGen>(1); |
| 43 | 79 | g->root = root; |
| 44 | 80 | g->fn_defs.init(32); |
| 45 | 81 | g->fn_table.init(32); |
| 46 | 82 | g->str_table.init(32); |
| 83 | g->type_table.init(32); |
| 84 | g->is_static = is_static; |
| 85 | |
| 86 | os_path_split(in_full_path, &g->in_dir, &g->in_file); |
| 47 | 87 | return g; |
| 48 | 88 | } |
| 49 | 89 | |
| ... | ... | @@ -60,9 +100,17 @@ static void add_node_error(CodeGen *g, AstNode *node, Buf *msg) { |
| 60 | 100 | static LLVMTypeRef to_llvm_type(AstNode *type_node) { |
| 61 | 101 | assert(type_node->type == NodeTypeType); |
| 62 | 102 | assert(type_node->codegen_node); |
| 63 | | assert(type_node->codegen_node->data.type_node.type_ref); |
| 103 | assert(type_node->codegen_node->data.type_node.entry); |
| 104 | |
| 105 | return type_node->codegen_node->data.type_node.entry->type_ref; |
| 106 | } |
| 107 | |
| 108 | static llvm::DIType *to_llvm_debug_type(AstNode *type_node) { |
| 109 | assert(type_node->type == NodeTypeType); |
| 110 | assert(type_node->codegen_node); |
| 111 | assert(type_node->codegen_node->data.type_node.entry); |
| 64 | 112 | |
| 65 | | return type_node->codegen_node->data.type_node.type_ref; |
| 113 | return type_node->codegen_node->data.type_node.entry->di_type; |
| 66 | 114 | } |
| 67 | 115 | |
| 68 | 116 | |
| ... | ... | @@ -72,6 +120,56 @@ static bool type_is_unreachable(AstNode *type_node) { |
| 72 | 120 | buf_eql_str(&type_node->data.type.primitive_name, "unreachable"); |
| 73 | 121 | } |
| 74 | 122 | |
| 123 | static void analyze_node(CodeGen *g, AstNode *node); |
| 124 | |
| 125 | static void resolve_type_and_recurse(CodeGen *g, AstNode *node) { |
| 126 | assert(!node->codegen_node); |
| 127 | node->codegen_node = allocate<CodeGenNode>(1); |
| 128 | TypeNode *type_node = &node->codegen_node->data.type_node; |
| 129 | switch (node->data.type.type) { |
| 130 | case AstNodeTypeTypePrimitive: |
| 131 | { |
| 132 | Buf *name = &node->data.type.primitive_name; |
| 133 | auto table_entry = g->type_table.maybe_get(name); |
| 134 | if (table_entry) { |
| 135 | type_node->entry = table_entry->value; |
| 136 | } else { |
| 137 | add_node_error(g, node, |
| 138 | buf_sprintf("invalid type name: '%s'", buf_ptr(name))); |
| 139 | type_node->entry = g->invalid_type_entry; |
| 140 | } |
| 141 | break; |
| 142 | } |
| 143 | case AstNodeTypeTypePointer: |
| 144 | { |
| 145 | analyze_node(g, node->data.type.child_type); |
| 146 | TypeNode *child_type_node = &node->data.type.child_type->codegen_node->data.type_node; |
| 147 | if (child_type_node->entry->id == TypeIdUnreachable) { |
| 148 | add_node_error(g, node, |
| 149 | buf_create_from_str("pointer to unreachable not allowed")); |
| 150 | } |
| 151 | TypeTableEntry **parent_pointer = node->data.type.is_const ? |
| 152 | &child_type_node->entry->pointer_const_parent : |
| 153 | &child_type_node->entry->pointer_mut_parent; |
| 154 | const char *const_or_mut_str = node->data.type.is_const ? "const" : "mut"; |
| 155 | if (*parent_pointer) { |
| 156 | type_node->entry = *parent_pointer; |
| 157 | } else { |
| 158 | TypeTableEntry *entry = allocate<TypeTableEntry>(1); |
| 159 | entry->id = TypeIdPointer; |
| 160 | entry->type_ref = LLVMPointerType(child_type_node->entry->type_ref, 0); |
| 161 | buf_appendf(&entry->name, "*%s %s", const_or_mut_str, buf_ptr(&child_type_node->entry->name)); |
| 162 | entry->di_type = g->dbuilder->createPointerType(child_type_node->entry->di_type, |
| 163 | g->pointer_size_bytes * 8, g->pointer_size_bytes * 8, buf_ptr(&entry->name)); |
| 164 | g->type_table.put(&entry->name, entry); |
| 165 | type_node->entry = entry; |
| 166 | *parent_pointer = entry; |
| 167 | } |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 75 | 173 | static void analyze_node(CodeGen *g, AstNode *node) { |
| 76 | 174 | switch (node->type) { |
| 77 | 175 | case NodeTypeRoot: |
| ... | ... | @@ -148,42 +246,10 @@ static void analyze_node(CodeGen *g, AstNode *node) { |
| 148 | 246 | case NodeTypeParamDecl: |
| 149 | 247 | analyze_node(g, node->data.param_decl.type); |
| 150 | 248 | break; |
| 249 | |
| 151 | 250 | case NodeTypeType: |
| 152 | 251 | { |
| 153 | | node->codegen_node = allocate<CodeGenNode>(1); |
| 154 | | TypeNode *type_node = &node->codegen_node->data.type_node; |
| 155 | | switch (node->data.type.type) { |
| 156 | | case AstNodeTypeTypePrimitive: |
| 157 | | { |
| 158 | | Buf *name = &node->data.type.primitive_name; |
| 159 | | if (buf_eql_str(name, "u8")) { |
| 160 | | type_node->type_ref = LLVMInt8Type(); |
| 161 | | } else if (buf_eql_str(name, "i32")) { |
| 162 | | type_node->type_ref = LLVMInt32Type(); |
| 163 | | } else if (buf_eql_str(name, "void")) { |
| 164 | | type_node->type_ref = LLVMVoidType(); |
| 165 | | } else if (buf_eql_str(name, "unreachable")) { |
| 166 | | type_node->type_ref = LLVMVoidType(); |
| 167 | | type_node->is_unreachable = true; |
| 168 | | } else { |
| 169 | | add_node_error(g, node, |
| 170 | | buf_sprintf("invalid type name: '%s'", buf_ptr(name))); |
| 171 | | type_node->type_ref = LLVMVoidType(); |
| 172 | | } |
| 173 | | break; |
| 174 | | } |
| 175 | | case AstNodeTypeTypePointer: |
| 176 | | { |
| 177 | | analyze_node(g, node->data.type.child_type); |
| 178 | | TypeNode *child_type_node = &node->data.type.child_type->codegen_node->data.type_node; |
| 179 | | if (child_type_node->is_unreachable) { |
| 180 | | add_node_error(g, node, |
| 181 | | buf_create_from_str("pointer to unreachable not allowed")); |
| 182 | | } |
| 183 | | type_node->type_ref = LLVMPointerType(child_type_node->type_ref, 0); |
| 184 | | break; |
| 185 | | } |
| 186 | | } |
| 252 | resolve_type_and_recurse(g, node); |
| 187 | 253 | break; |
| 188 | 254 | } |
| 189 | 255 | case NodeTypeBlock: |
| ... | ... | @@ -224,10 +290,85 @@ static void analyze_node(CodeGen *g, AstNode *node) { |
| 224 | 290 | } |
| 225 | 291 | } |
| 226 | 292 | |
| 293 | static void add_types(CodeGen *g) { |
| 294 | { |
| 295 | TypeTableEntry *entry = allocate<TypeTableEntry>(1); |
| 296 | entry->id = TypeIdU8; |
| 297 | entry->type_ref = LLVMInt8Type(); |
| 298 | buf_init_from_str(&entry->name, "u8"); |
| 299 | entry->di_type = g->dbuilder->createBasicType(buf_ptr(&entry->name), 8, 8, llvm::dwarf::DW_ATE_unsigned); |
| 300 | g->type_table.put(&entry->name, entry); |
| 301 | } |
| 302 | { |
| 303 | TypeTableEntry *entry = allocate<TypeTableEntry>(1); |
| 304 | entry->id = TypeIdI32; |
| 305 | entry->type_ref = LLVMInt32Type(); |
| 306 | buf_init_from_str(&entry->name, "i32"); |
| 307 | entry->di_type = g->dbuilder->createBasicType(buf_ptr(&entry->name), 32, 32, |
| 308 | llvm::dwarf::DW_ATE_signed); |
| 309 | g->type_table.put(&entry->name, entry); |
| 310 | } |
| 311 | { |
| 312 | TypeTableEntry *entry = allocate<TypeTableEntry>(1); |
| 313 | entry->id = TypeIdVoid; |
| 314 | entry->type_ref = LLVMVoidType(); |
| 315 | buf_init_from_str(&entry->name, "void"); |
| 316 | entry->di_type = g->dbuilder->createBasicType(buf_ptr(&entry->name), 0, 0, |
| 317 | llvm::dwarf::DW_ATE_unsigned); |
| 318 | g->type_table.put(&entry->name, entry); |
| 319 | |
| 320 | // invalid types are void |
| 321 | g->invalid_type_entry = entry; |
| 322 | } |
| 323 | { |
| 324 | TypeTableEntry *entry = allocate<TypeTableEntry>(1); |
| 325 | entry->id = TypeIdUnreachable; |
| 326 | entry->type_ref = LLVMVoidType(); |
| 327 | buf_init_from_str(&entry->name, "unreachable"); |
| 328 | entry->di_type = g->invalid_type_entry->di_type; |
| 329 | g->type_table.put(&entry->name, entry); |
| 330 | } |
| 331 | } |
| 332 | |
| 227 | 333 | |
| 228 | 334 | void semantic_analyze(CodeGen *g) { |
| 335 | LLVMInitializeAllTargets(); |
| 336 | LLVMInitializeAllTargetMCs(); |
| 337 | LLVMInitializeAllAsmPrinters(); |
| 338 | LLVMInitializeAllAsmParsers(); |
| 339 | LLVMInitializeNativeTarget(); |
| 340 | |
| 341 | char *native_triple = LLVMGetDefaultTargetTriple(); |
| 342 | |
| 343 | LLVMTargetRef target_ref; |
| 344 | char *err_msg = nullptr; |
| 345 | if (LLVMGetTargetFromTriple(native_triple, &target_ref, &err_msg)) { |
| 346 | zig_panic("unable to get target from triple: %s", err_msg); |
| 347 | } |
| 348 | |
| 349 | char *native_cpu = LLVMZigGetHostCPUName(); |
| 350 | char *native_features = LLVMZigGetNativeFeatures(); |
| 351 | |
| 352 | LLVMCodeGenOptLevel opt_level = LLVMCodeGenLevelNone; |
| 353 | |
| 354 | LLVMRelocMode reloc_mode = g->is_static ? LLVMRelocStatic : LLVMRelocPIC; |
| 355 | |
| 356 | g->target_machine = LLVMCreateTargetMachine(target_ref, native_triple, |
| 357 | native_cpu, native_features, opt_level, reloc_mode, LLVMCodeModelDefault); |
| 358 | |
| 359 | g->target_data_ref = LLVMGetTargetMachineData(g->target_machine); |
| 360 | |
| 361 | |
| 229 | 362 | g->mod = LLVMModuleCreateWithName("ZigModule"); |
| 230 | 363 | |
| 364 | g->pointer_size_bytes = LLVMPointerSize(g->target_data_ref); |
| 365 | |
| 366 | g->builder = LLVMCreateBuilder(); |
| 367 | g->dbuilder = new llvm::DIBuilder(*llvm::unwrap(g->mod), true); |
| 368 | |
| 369 | |
| 370 | add_types(g); |
| 371 | |
| 231 | 372 | // Pass 1. |
| 232 | 373 | analyze_node(g, g->root); |
| 233 | 374 | } |
| ... | ... | @@ -344,8 +485,29 @@ static void gen_block(CodeGen *g, AstNode *block_node) { |
| 344 | 485 | } |
| 345 | 486 | } |
| 346 | 487 | |
| 488 | static llvm::DISubroutineType *create_di_function_type(CodeGen *g, AstNodeFnProto *fn_proto, llvm::DIFile *unit) { |
| 489 | llvm::SmallVector<llvm::Metadata *, 8> types; |
| 490 | |
| 491 | llvm::DIType *return_type = to_llvm_debug_type(fn_proto->return_type); |
| 492 | types.push_back(return_type); |
| 493 | |
| 494 | for (int i = 0; i < fn_proto->params.length; i += 1) { |
| 495 | AstNode *param_node = fn_proto->params.at(i); |
| 496 | llvm::DIType *param_type = to_llvm_debug_type(param_node); |
| 497 | types.push_back(param_type); |
| 498 | } |
| 499 | |
| 500 | return g->dbuilder->createSubroutineType(unit, g->dbuilder->getOrCreateTypeArray(types)); |
| 501 | } |
| 502 | |
| 347 | 503 | void code_gen(CodeGen *g) { |
| 348 | | g->builder = LLVMCreateBuilder(); |
| 504 | Buf *producer = buf_sprintf("zig %s", ZIG_VERSION_STRING); |
| 505 | bool is_optimized = false; |
| 506 | const char *flags = ""; |
| 507 | unsigned runtime_version = 0; |
| 508 | g->compile_unit = g->dbuilder->createCompileUnit(llvm::dwarf::DW_LANG_C99, |
| 509 | buf_ptr(&g->in_file), buf_ptr(&g->in_dir), |
| 510 | buf_ptr(producer), is_optimized, flags, runtime_version); |
| 349 | 511 | |
| 350 | 512 | auto it = g->fn_defs.entry_iterator(); |
| 351 | 513 | for (;;) { |
| ... | ... | @@ -369,9 +531,29 @@ void code_gen(CodeGen *g) { |
| 369 | 531 | LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, fn_proto->params.length, 0); |
| 370 | 532 | LLVMValueRef fn = LLVMAddFunction(g->mod, buf_ptr(&fn_proto->name), function_type); |
| 371 | 533 | |
| 534 | bool internal_linkage = false; |
| 535 | LLVMSetLinkage(fn, internal_linkage ? LLVMPrivateLinkage : LLVMExternalLinkage); |
| 536 | |
| 372 | 537 | if (type_is_unreachable(fn_proto->return_type)) { |
| 373 | 538 | LLVMAddFunctionAttr(fn, LLVMNoReturnAttribute); |
| 374 | 539 | } |
| 540 | LLVMAddFunctionAttr(fn, LLVMNoUnwindAttribute); |
| 541 | |
| 542 | // Add debug info. |
| 543 | llvm::DIFile *unit = g->dbuilder->createFile(g->compile_unit->getFilename(), |
| 544 | g->compile_unit->getDirectory()); |
| 545 | llvm::DIScope *fn_scope = unit; |
| 546 | unsigned line_number = fn_def_node->line + 1; |
| 547 | unsigned scope_line = line_number; |
| 548 | bool is_definition = true; |
| 549 | unsigned flags = 0; |
| 550 | llvm::Function *unwrapped_function = reinterpret_cast<llvm::Function*>(llvm::unwrap(fn)); |
| 551 | g->dbuilder->createFunction( |
| 552 | fn_scope, buf_ptr(&fn_proto->name), "", unit, line_number, |
| 553 | create_di_function_type(g, fn_proto, unit), internal_linkage, |
| 554 | is_definition, scope_line, flags, is_optimized, unwrapped_function); |
| 555 | |
| 556 | |
| 375 | 557 | |
| 376 | 558 | LLVMBasicBlockRef entry_block = LLVMAppendBasicBlock(fn, "entry"); |
| 377 | 559 | LLVMPositionBuilderAtEnd(g->builder, entry_block); |
| ... | ... | @@ -379,6 +561,8 @@ void code_gen(CodeGen *g) { |
| 379 | 561 | gen_block(g, fn_def->body); |
| 380 | 562 | } |
| 381 | 563 | |
| 564 | g->dbuilder->finalize(); |
| 565 | |
| 382 | 566 | LLVMDumpModule(g->mod); |
| 383 | 567 | |
| 384 | 568 | char *error = nullptr; |
| ... | ... | @@ -390,14 +574,7 @@ ZigList<ErrorMsg> *codegen_error_messages(CodeGen *g) { |
| 390 | 574 | } |
| 391 | 575 | |
| 392 | 576 | |
| 393 | | void code_gen_link(CodeGen *g, bool is_static, const char *out_file) { |
| 394 | | LLVMInitializeAllTargets(); |
| 395 | | LLVMInitializeAllTargetMCs(); |
| 396 | | LLVMInitializeAllAsmPrinters(); |
| 397 | | LLVMInitializeAllAsmParsers(); |
| 398 | | LLVMInitializeNativeTarget(); |
| 399 | | |
| 400 | | |
| 577 | void code_gen_link(CodeGen *g, const char *out_file) { |
| 401 | 578 | LLVMPassRegistryRef registry = LLVMGetGlobalPassRegistry(); |
| 402 | 579 | LLVMInitializeCore(registry); |
| 403 | 580 | LLVMInitializeCodeGen(registry); |
| ... | ... | @@ -405,29 +582,12 @@ void code_gen_link(CodeGen *g, bool is_static, const char *out_file) { |
| 405 | 582 | LLVMZigInitializeLowerIntrinsicsPass(registry); |
| 406 | 583 | LLVMZigInitializeUnreachableBlockElimPass(registry); |
| 407 | 584 | |
| 408 | | char *native_triple = LLVMGetDefaultTargetTriple(); |
| 409 | | |
| 410 | | LLVMTargetRef target_ref; |
| 411 | | char *err_msg = nullptr; |
| 412 | | if (LLVMGetTargetFromTriple(native_triple, &target_ref, &err_msg)) { |
| 413 | | zig_panic("unable to get target from triple: %s", err_msg); |
| 414 | | } |
| 415 | | |
| 416 | | char *native_cpu = LLVMZigGetHostCPUName(); |
| 417 | | char *native_features = LLVMZigGetNativeFeatures(); |
| 418 | | |
| 419 | | LLVMCodeGenOptLevel opt_level = LLVMCodeGenLevelNone; |
| 420 | | |
| 421 | | LLVMRelocMode reloc_mode = is_static ? LLVMRelocStatic : LLVMRelocPIC; |
| 422 | | |
| 423 | | LLVMTargetMachineRef target_machine = LLVMCreateTargetMachine(target_ref, native_triple, |
| 424 | | native_cpu, native_features, opt_level, reloc_mode, LLVMCodeModelDefault); |
| 425 | | |
| 426 | 585 | Buf out_file_o = BUF_INIT; |
| 427 | 586 | buf_init_from_str(&out_file_o, out_file); |
| 428 | 587 | buf_append_str(&out_file_o, ".o"); |
| 429 | 588 | |
| 430 | | if (LLVMTargetMachineEmitToFile(target_machine, g->mod, buf_ptr(&out_file_o), LLVMObjectFile, &err_msg)) { |
| 589 | char *err_msg = nullptr; |
| 590 | if (LLVMTargetMachineEmitToFile(g->target_machine, g->mod, buf_ptr(&out_file_o), LLVMObjectFile, &err_msg)) { |
| 431 | 591 | zig_panic("unable to write object file: %s", err_msg); |
| 432 | 592 | } |
| 433 | 593 | |