| 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,7 +40,6 @@ readable, safe, optimal, and concise code to solve any computing problem. |
| 40 | 40 | ||
| 41 | ## Roadmap | 41 | ## Roadmap |
| 42 | 42 | ||
| 43 | * Debug/Release mode. | ||
| 44 | * don't hardcode the link against libc | 43 | * don't hardcode the link against libc |
| 45 | * C style comments. | 44 | * C style comments. |
| 46 | * Unit tests. | 45 | * Unit tests. |
src/codegen.cpp+26-5| ... | @@ -60,6 +60,8 @@ struct CodeGen { | ... | @@ -60,6 +60,8 @@ struct CodeGen { |
| 60 | LLVMTargetDataRef target_data_ref; | 60 | LLVMTargetDataRef target_data_ref; |
| 61 | unsigned pointer_size_bytes; | 61 | unsigned pointer_size_bytes; |
| 62 | bool is_static; | 62 | bool is_static; |
| 63 | bool strip_debug_symbols; | ||
| 64 | CodeGenBuildType build_type; | ||
| 63 | LLVMTargetMachineRef target_machine; | 65 | LLVMTargetMachineRef target_machine; |
| 64 | Buf in_file; | 66 | Buf in_file; |
| 65 | Buf in_dir; | 67 | Buf in_dir; |
| ... | @@ -77,19 +79,33 @@ struct CodeGenNode { | ... | @@ -77,19 +79,33 @@ struct CodeGenNode { |
| 77 | } data; | 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 | CodeGen *g = allocate<CodeGen>(1); | 83 | CodeGen *g = allocate<CodeGen>(1); |
| 82 | g->root = root; | 84 | g->root = root; |
| 83 | g->fn_defs.init(32); | 85 | g->fn_defs.init(32); |
| 84 | g->fn_table.init(32); | 86 | g->fn_table.init(32); |
| 85 | g->str_table.init(32); | 87 | g->str_table.init(32); |
| 86 | g->type_table.init(32); | 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 | os_path_split(in_full_path, &g->in_dir, &g->in_file); | 93 | os_path_split(in_full_path, &g->in_dir, &g->in_file); |
| 90 | return g; | 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 | static void add_node_error(CodeGen *g, AstNode *node, Buf *msg) { | 109 | static void add_node_error(CodeGen *g, AstNode *node, Buf *msg) { |
| 94 | g->errors.add_one(); | 110 | g->errors.add_one(); |
| 95 | ErrorMsg *last_msg = &g->errors.last(); | 111 | ErrorMsg *last_msg = &g->errors.last(); |
| ... | @@ -352,7 +368,8 @@ void semantic_analyze(CodeGen *g) { | ... | @@ -352,7 +368,8 @@ void semantic_analyze(CodeGen *g) { |
| 352 | char *native_cpu = LLVMZigGetHostCPUName(); | 368 | char *native_cpu = LLVMZigGetHostCPUName(); |
| 353 | char *native_features = LLVMZigGetNativeFeatures(); | 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 | LLVMRelocMode reloc_mode = g->is_static ? LLVMRelocStatic : LLVMRelocPIC; | 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,12 +539,13 @@ static llvm::DISubroutineType *create_di_function_type(CodeGen *g, AstNodeFnProt |
| 522 | 539 | ||
| 523 | void code_gen(CodeGen *g) { | 540 | void code_gen(CodeGen *g) { |
| 524 | Buf *producer = buf_sprintf("zig %s", ZIG_VERSION_STRING); | 541 | Buf *producer = buf_sprintf("zig %s", ZIG_VERSION_STRING); |
| 525 | bool is_optimized = false; | 542 | bool is_optimized = g->build_type == CodeGenBuildTypeRelease; |
| 526 | const char *flags = ""; | 543 | const char *flags = ""; |
| 527 | unsigned runtime_version = 0; | 544 | unsigned runtime_version = 0; |
| 528 | g->compile_unit = g->dbuilder->createCompileUnit(llvm::dwarf::DW_LANG_C99, | 545 | g->compile_unit = g->dbuilder->createCompileUnit(llvm::dwarf::DW_LANG_C99, |
| 529 | buf_ptr(&g->in_file), buf_ptr(&g->in_dir), | 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 | g->block_scopes.append(g->compile_unit); | 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,6 +633,9 @@ void code_gen_link(CodeGen *g, const char *out_file) { |
| 615 | } | 633 | } |
| 616 | 634 | ||
| 617 | ZigList<const char *> args = {0}; | 635 | ZigList<const char *> args = {0}; |
| 636 | if (g->is_static) { | ||
| 637 | args.append("-static"); | ||
| 638 | } | ||
| 618 | args.append("-o"); | 639 | args.append("-o"); |
| 619 | args.append(out_file); | 640 | args.append(out_file); |
| 620 | args.append((const char *)buf_ptr(&out_file_o)); | 641 | args.append((const char *)buf_ptr(&out_file_o)); |
src/codegen.hpp+9-1| ... | @@ -21,7 +21,15 @@ struct ErrorMsg { | ... | @@ -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 | void semantic_analyze(CodeGen *g); | 34 | void semantic_analyze(CodeGen *g); |
| 27 | 35 |
src/main.cpp+20-3| ... | @@ -33,6 +33,9 @@ static int usage(const char *arg0) { | ... | @@ -33,6 +33,9 @@ static int usage(const char *arg0) { |
| 33 | " --output output file\n" | 33 | " --output output file\n" |
| 34 | " --version print version number and exit\n" | 34 | " --version print version number and exit\n" |
| 35 | " -Ipath add path to header include path\n" | 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 | , arg0); | 39 | , arg0); |
| 37 | return EXIT_FAILURE; | 40 | return EXIT_FAILURE; |
| 38 | } | 41 | } |
| ... | @@ -55,7 +58,9 @@ static Buf *fetch_file(FILE *f) { | ... | @@ -55,7 +58,9 @@ static Buf *fetch_file(FILE *f) { |
| 55 | return buf; | 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 | static char cur_dir[1024]; | 64 | static char cur_dir[1024]; |
| 60 | 65 | ||
| 61 | if (!in_file || !out_file) | 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,7 +96,10 @@ static int build(const char *arg0, const char *in_file, const char *out_file, Zi |
| 91 | 96 | ||
| 92 | fprintf(stderr, "\nSemantic Analysis:\n"); | 97 | fprintf(stderr, "\nSemantic Analysis:\n"); |
| 93 | fprintf(stderr, "--------------------\n"); | 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 | semantic_analyze(codegen); | 103 | semantic_analyze(codegen); |
| 96 | ZigList<ErrorMsg> *errors = codegen_error_messages(codegen); | 104 | ZigList<ErrorMsg> *errors = codegen_error_messages(codegen); |
| 97 | if (errors->length == 0) { | 105 | if (errors->length == 0) { |
| ... | @@ -128,6 +136,9 @@ int main(int argc, char **argv) { | ... | @@ -128,6 +136,9 @@ int main(int argc, char **argv) { |
| 128 | char *in_file = NULL; | 136 | char *in_file = NULL; |
| 129 | char *out_file = NULL; | 137 | char *out_file = NULL; |
| 130 | ZigList<char *> include_paths = {0}; | 138 | ZigList<char *> include_paths = {0}; |
| 139 | bool release = false; | ||
| 140 | bool strip = false; | ||
| 141 | bool is_static = false; | ||
| 131 | 142 | ||
| 132 | Cmd cmd = CmdNone; | 143 | Cmd cmd = CmdNone; |
| 133 | for (int i = 1; i < argc; i += 1) { | 144 | for (int i = 1; i < argc; i += 1) { |
| ... | @@ -136,6 +147,12 @@ int main(int argc, char **argv) { | ... | @@ -136,6 +147,12 @@ int main(int argc, char **argv) { |
| 136 | if (strcmp(arg, "--version") == 0) { | 147 | if (strcmp(arg, "--version") == 0) { |
| 137 | printf("%s\n", ZIG_VERSION_STRING); | 148 | printf("%s\n", ZIG_VERSION_STRING); |
| 138 | return EXIT_SUCCESS; | 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 | } else if (i + 1 >= argc) { | 156 | } else if (i + 1 >= argc) { |
| 140 | return usage(arg0); | 157 | return usage(arg0); |
| 141 | } else { | 158 | } else { |
| ... | @@ -174,7 +191,7 @@ int main(int argc, char **argv) { | ... | @@ -174,7 +191,7 @@ int main(int argc, char **argv) { |
| 174 | case CmdNone: | 191 | case CmdNone: |
| 175 | return usage(arg0); | 192 | return usage(arg0); |
| 176 | case CmdBuild: | 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 | zig_unreachable(); | 197 | zig_unreachable(); |