| author | |
| committer | |
| log | 505317a12fd89b144097e4b99e84b619f7ca5ac2 |
| tree | dde38d6810e2f0497599b61e5ea0282e83db2052 |
| parent | cda10f0577fcf5794f1f583ff159311a1b186082 |
4 files changed, 55 insertions(+), 10 deletions(-)
README.md-1| ... | ... | @@ -40,7 +40,6 @@ readable, safe, optimal, and concise code to solve any computing problem. |
| 40 | 40 | |
| 41 | 41 | ## Roadmap |
| 42 | 42 | |
| 43 | * Debug/Release mode. | |
| 44 | 43 | * don't hardcode the link against libc |
| 45 | 44 | * C style comments. |
| 46 | 45 | * Unit tests. |
src/codegen.cpp+26-5| ... | ... | @@ -60,6 +60,8 @@ struct CodeGen { |
| 60 | 60 | LLVMTargetDataRef target_data_ref; |
| 61 | 61 | unsigned pointer_size_bytes; |
| 62 | 62 | bool is_static; |
| 63 | bool strip_debug_symbols; | |
| 64 | CodeGenBuildType build_type; | |
| 63 | 65 | LLVMTargetMachineRef target_machine; |
| 64 | 66 | Buf in_file; |
| 65 | 67 | Buf in_dir; |
| ... | ... | @@ -77,19 +79,33 @@ struct CodeGenNode { |
| 77 | 79 | } data; |
| 78 | 80 | }; |
| 79 | 81 | |
| 80 | CodeGen *create_codegen(AstNode *root, bool is_static, Buf *in_full_path) { | |
| 82 | CodeGen *create_codegen(AstNode *root, Buf *in_full_path) { | |
| 81 | 83 | CodeGen *g = allocate<CodeGen>(1); |
| 82 | 84 | g->root = root; |
| 83 | 85 | g->fn_defs.init(32); |
| 84 | 86 | g->fn_table.init(32); |
| 85 | 87 | g->str_table.init(32); |
| 86 | 88 | g->type_table.init(32); |
| 87 | g->is_static = is_static; | |
| 89 | g->is_static = false; | |
| 90 | g->build_type = CodeGenBuildTypeDebug; | |
| 91 | g->strip_debug_symbols = false; | |
| 88 | 92 | |
| 89 | 93 | os_path_split(in_full_path, &g->in_dir, &g->in_file); |
| 90 | 94 | return g; |
| 91 | 95 | } |
| 92 | 96 | |
| 97 | void codegen_set_build_type(CodeGen *g, CodeGenBuildType build_type) { | |
| 98 | g->build_type = build_type; | |
| 99 | } | |
| 100 | ||
| 101 | void codegen_set_is_static(CodeGen *g, bool is_static) { | |
| 102 | g->is_static = is_static; | |
| 103 | } | |
| 104 | ||
| 105 | void codegen_set_strip(CodeGen *g, bool strip) { | |
| 106 | g->strip_debug_symbols = strip; | |
| 107 | } | |
| 108 | ||
| 93 | 109 | static void add_node_error(CodeGen *g, AstNode *node, Buf *msg) { |
| 94 | 110 | g->errors.add_one(); |
| 95 | 111 | ErrorMsg *last_msg = &g->errors.last(); |
| ... | ... | @@ -352,7 +368,8 @@ void semantic_analyze(CodeGen *g) { |
| 352 | 368 | char *native_cpu = LLVMZigGetHostCPUName(); |
| 353 | 369 | char *native_features = LLVMZigGetNativeFeatures(); |
| 354 | 370 | |
| 355 | LLVMCodeGenOptLevel opt_level = LLVMCodeGenLevelNone; | |
| 371 | LLVMCodeGenOptLevel opt_level = (g->build_type == CodeGenBuildTypeDebug) ? | |
| 372 | LLVMCodeGenLevelNone : LLVMCodeGenLevelAggressive; | |
| 356 | 373 | |
| 357 | 374 | LLVMRelocMode reloc_mode = g->is_static ? LLVMRelocStatic : LLVMRelocPIC; |
| 358 | 375 | |
| ... | ... | @@ -522,12 +539,13 @@ static llvm::DISubroutineType *create_di_function_type(CodeGen *g, AstNodeFnProt |
| 522 | 539 | |
| 523 | 540 | void code_gen(CodeGen *g) { |
| 524 | 541 | Buf *producer = buf_sprintf("zig %s", ZIG_VERSION_STRING); |
| 525 | bool is_optimized = false; | |
| 542 | bool is_optimized = g->build_type == CodeGenBuildTypeRelease; | |
| 526 | 543 | const char *flags = ""; |
| 527 | 544 | unsigned runtime_version = 0; |
| 528 | 545 | g->compile_unit = g->dbuilder->createCompileUnit(llvm::dwarf::DW_LANG_C99, |
| 529 | 546 | buf_ptr(&g->in_file), buf_ptr(&g->in_dir), |
| 530 | buf_ptr(producer), is_optimized, flags, runtime_version); | |
| 547 | buf_ptr(producer), is_optimized, flags, runtime_version, | |
| 548 | "", llvm::DIBuilder::FullDebug, 0, !g->strip_debug_symbols); | |
| 531 | 549 | |
| 532 | 550 | g->block_scopes.append(g->compile_unit); |
| 533 | 551 | |
| ... | ... | @@ -615,6 +633,9 @@ void code_gen_link(CodeGen *g, const char *out_file) { |
| 615 | 633 | } |
| 616 | 634 | |
| 617 | 635 | ZigList<const char *> args = {0}; |
| 636 | if (g->is_static) { | |
| 637 | args.append("-static"); | |
| 638 | } | |
| 618 | 639 | args.append("-o"); |
| 619 | 640 | args.append(out_file); |
| 620 | 641 | args.append((const char *)buf_ptr(&out_file_o)); |
src/codegen.hpp+9-1| ... | ... | @@ -21,7 +21,15 @@ struct ErrorMsg { |
| 21 | 21 | }; |
| 22 | 22 | |
| 23 | 23 | |
| 24 | CodeGen *create_codegen(AstNode *root, bool is_static, Buf *in_file); | |
| 24 | CodeGen *create_codegen(AstNode *root, Buf *in_file); | |
| 25 | ||
| 26 | enum CodeGenBuildType { | |
| 27 | CodeGenBuildTypeDebug, | |
| 28 | CodeGenBuildTypeRelease, | |
| 29 | }; | |
| 30 | void codegen_set_build_type(CodeGen *codegen, CodeGenBuildType build_type); | |
| 31 | void codegen_set_is_static(CodeGen *codegen, bool is_static); | |
| 32 | void codegen_set_strip(CodeGen *codegen, bool strip); | |
| 25 | 33 | |
| 26 | 34 | void semantic_analyze(CodeGen *g); |
| 27 | 35 |
src/main.cpp+20-3| ... | ... | @@ -33,6 +33,9 @@ static int usage(const char *arg0) { |
| 33 | 33 | " --output output file\n" |
| 34 | 34 | " --version print version number and exit\n" |
| 35 | 35 | " -Ipath add path to header include path\n" |
| 36 | " --release build with optimizations on\n" | |
| 37 | " --strip exclude debug symbols\n" | |
| 38 | " --static build a static executable\n" | |
| 36 | 39 | , arg0); |
| 37 | 40 | return EXIT_FAILURE; |
| 38 | 41 | } |
| ... | ... | @@ -55,7 +58,9 @@ static Buf *fetch_file(FILE *f) { |
| 55 | 58 | return buf; |
| 56 | 59 | } |
| 57 | 60 | |
| 58 | static int build(const char *arg0, const char *in_file, const char *out_file, ZigList<char *> *include_paths) { | |
| 61 | static int build(const char *arg0, const char *in_file, const char *out_file, | |
| 62 | ZigList<char *> *include_paths, bool release, bool strip, bool is_static) | |
| 63 | { | |
| 59 | 64 | static char cur_dir[1024]; |
| 60 | 65 | |
| 61 | 66 | if (!in_file || !out_file) |
| ... | ... | @@ -91,7 +96,10 @@ static int build(const char *arg0, const char *in_file, const char *out_file, Zi |
| 91 | 96 | |
| 92 | 97 | fprintf(stderr, "\nSemantic Analysis:\n"); |
| 93 | 98 | fprintf(stderr, "--------------------\n"); |
| 94 | CodeGen *codegen = create_codegen(root, false, buf_create_from_str(in_file)); | |
| 99 | CodeGen *codegen = create_codegen(root, buf_create_from_str(in_file)); | |
| 100 | codegen_set_build_type(codegen, release ? CodeGenBuildTypeRelease : CodeGenBuildTypeDebug); | |
| 101 | codegen_set_strip(codegen, strip); | |
| 102 | codegen_set_is_static(codegen, is_static); | |
| 95 | 103 | semantic_analyze(codegen); |
| 96 | 104 | ZigList<ErrorMsg> *errors = codegen_error_messages(codegen); |
| 97 | 105 | if (errors->length == 0) { |
| ... | ... | @@ -128,6 +136,9 @@ int main(int argc, char **argv) { |
| 128 | 136 | char *in_file = NULL; |
| 129 | 137 | char *out_file = NULL; |
| 130 | 138 | ZigList<char *> include_paths = {0}; |
| 139 | bool release = false; | |
| 140 | bool strip = false; | |
| 141 | bool is_static = false; | |
| 131 | 142 | |
| 132 | 143 | Cmd cmd = CmdNone; |
| 133 | 144 | for (int i = 1; i < argc; i += 1) { |
| ... | ... | @@ -136,6 +147,12 @@ int main(int argc, char **argv) { |
| 136 | 147 | if (strcmp(arg, "--version") == 0) { |
| 137 | 148 | printf("%s\n", ZIG_VERSION_STRING); |
| 138 | 149 | return EXIT_SUCCESS; |
| 150 | } else if (strcmp(arg, "--release") == 0) { | |
| 151 | release = true; | |
| 152 | } else if (strcmp(arg, "--strip") == 0) { | |
| 153 | strip = true; | |
| 154 | } else if (strcmp(arg, "--static") == 0) { | |
| 155 | is_static = true; | |
| 139 | 156 | } else if (i + 1 >= argc) { |
| 140 | 157 | return usage(arg0); |
| 141 | 158 | } else { |
| ... | ... | @@ -174,7 +191,7 @@ int main(int argc, char **argv) { |
| 174 | 191 | case CmdNone: |
| 175 | 192 | return usage(arg0); |
| 176 | 193 | case CmdBuild: |
| 177 | return build(arg0, in_file, out_file, &include_paths); | |
| 194 | return build(arg0, in_file, out_file, &include_paths, release, strip, is_static); | |
| 178 | 195 | } |
| 179 | 196 | |
| 180 | 197 | zig_unreachable(); |