authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-24 22:32:26-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-24 22:32:26-07:00
log505317a12fd89b144097e4b99e84b619f7ca5ac2
treedde38d6810e2f0497599b61e5ea0282e83db2052
parentcda10f0577fcf5794f1f583ff159311a1b186082

debug/release mode


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.
4040
4141## Roadmap
4242
43 * Debug/Release mode.
4443 * don't hardcode the link against libc
4544 * C style comments.
4645 * Unit tests.
src/codegen.cpp+26-5
......@@ -60,6 +60,8 @@ struct CodeGen {
6060 LLVMTargetDataRef target_data_ref;
6161 unsigned pointer_size_bytes;
6262 bool is_static;
63 bool strip_debug_symbols;
64 CodeGenBuildType build_type;
6365 LLVMTargetMachineRef target_machine;
6466 Buf in_file;
6567 Buf in_dir;
......@@ -77,19 +79,33 @@ struct CodeGenNode {
7779 } data;
7880};
7981
80CodeGen *create_codegen(AstNode *root, bool is_static, Buf *in_full_path) {
82CodeGen *create_codegen(AstNode *root, Buf *in_full_path) {
8183 CodeGen *g = allocate<CodeGen>(1);
8284 g->root = root;
8385 g->fn_defs.init(32);
8486 g->fn_table.init(32);
8587 g->str_table.init(32);
8688 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;
8892
8993 os_path_split(in_full_path, &g->in_dir, &g->in_file);
9094 return g;
9195}
9296
97void codegen_set_build_type(CodeGen *g, CodeGenBuildType build_type) {
98 g->build_type = build_type;
99}
100
101void codegen_set_is_static(CodeGen *g, bool is_static) {
102 g->is_static = is_static;
103}
104
105void codegen_set_strip(CodeGen *g, bool strip) {
106 g->strip_debug_symbols = strip;
107}
108
93109static void add_node_error(CodeGen *g, AstNode *node, Buf *msg) {
94110 g->errors.add_one();
95111 ErrorMsg *last_msg = &g->errors.last();
......@@ -352,7 +368,8 @@ void semantic_analyze(CodeGen *g) {
352368 char *native_cpu = LLVMZigGetHostCPUName();
353369 char *native_features = LLVMZigGetNativeFeatures();
354370
355 LLVMCodeGenOptLevel opt_level = LLVMCodeGenLevelNone;
371 LLVMCodeGenOptLevel opt_level = (g->build_type == CodeGenBuildTypeDebug) ?
372 LLVMCodeGenLevelNone : LLVMCodeGenLevelAggressive;
356373
357374 LLVMRelocMode reloc_mode = g->is_static ? LLVMRelocStatic : LLVMRelocPIC;
358375
......@@ -522,12 +539,13 @@ static llvm::DISubroutineType *create_di_function_type(CodeGen *g, AstNodeFnProt
522539
523540void code_gen(CodeGen *g) {
524541 Buf *producer = buf_sprintf("zig %s", ZIG_VERSION_STRING);
525 bool is_optimized = false;
542 bool is_optimized = g->build_type == CodeGenBuildTypeRelease;
526543 const char *flags = "";
527544 unsigned runtime_version = 0;
528545 g->compile_unit = g->dbuilder->createCompileUnit(llvm::dwarf::DW_LANG_C99,
529546 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);
531549
532550 g->block_scopes.append(g->compile_unit);
533551
......@@ -615,6 +633,9 @@ void code_gen_link(CodeGen *g, const char *out_file) {
615633 }
616634
617635 ZigList<const char *> args = {0};
636 if (g->is_static) {
637 args.append("-static");
638 }
618639 args.append("-o");
619640 args.append(out_file);
620641 args.append((const char *)buf_ptr(&out_file_o));
src/codegen.hpp+9-1
......@@ -21,7 +21,15 @@ struct ErrorMsg {
2121};
2222
2323
24CodeGen *create_codegen(AstNode *root, bool is_static, Buf *in_file);
24CodeGen *create_codegen(AstNode *root, Buf *in_file);
25
26enum CodeGenBuildType {
27 CodeGenBuildTypeDebug,
28 CodeGenBuildTypeRelease,
29};
30void codegen_set_build_type(CodeGen *codegen, CodeGenBuildType build_type);
31void codegen_set_is_static(CodeGen *codegen, bool is_static);
32void codegen_set_strip(CodeGen *codegen, bool strip);
2533
2634void semantic_analyze(CodeGen *g);
2735
src/main.cpp+20-3
......@@ -33,6 +33,9 @@ static int usage(const char *arg0) {
3333 " --output output file\n"
3434 " --version print version number and exit\n"
3535 " -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"
3639 , arg0);
3740 return EXIT_FAILURE;
3841}
......@@ -55,7 +58,9 @@ static Buf *fetch_file(FILE *f) {
5558 return buf;
5659}
5760
58static int build(const char *arg0, const char *in_file, const char *out_file, ZigList<char *> *include_paths) {
61static 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{
5964 static char cur_dir[1024];
6065
6166 if (!in_file || !out_file)
......@@ -91,7 +96,10 @@ static int build(const char *arg0, const char *in_file, const char *out_file, Zi
9196
9297 fprintf(stderr, "\nSemantic Analysis:\n");
9398 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);
95103 semantic_analyze(codegen);
96104 ZigList<ErrorMsg> *errors = codegen_error_messages(codegen);
97105 if (errors->length == 0) {
......@@ -128,6 +136,9 @@ int main(int argc, char **argv) {
128136 char *in_file = NULL;
129137 char *out_file = NULL;
130138 ZigList<char *> include_paths = {0};
139 bool release = false;
140 bool strip = false;
141 bool is_static = false;
131142
132143 Cmd cmd = CmdNone;
133144 for (int i = 1; i < argc; i += 1) {
......@@ -136,6 +147,12 @@ int main(int argc, char **argv) {
136147 if (strcmp(arg, "--version") == 0) {
137148 printf("%s\n", ZIG_VERSION_STRING);
138149 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;
139156 } else if (i + 1 >= argc) {
140157 return usage(arg0);
141158 } else {
......@@ -174,7 +191,7 @@ int main(int argc, char **argv) {
174191 case CmdNone:
175192 return usage(arg0);
176193 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);
178195 }
179196
180197 zig_unreachable();