From 505317a12fd89b144097e4b99e84b619f7ca5ac2 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 24 Nov 2015 22:32:26 -0700 Subject: [PATCH] debug/release mode --- README.md | 1 - src/codegen.cpp | 31 ++++++++++++++++++++++++++----- src/codegen.hpp | 10 +++++++++- src/main.cpp | 23 ++++++++++++++++++++--- 4 files changed, 55 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 4672054d0c..389b5ada0d 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,6 @@ readable, safe, optimal, and concise code to solve any computing problem. ## Roadmap - * Debug/Release mode. * don't hardcode the link against libc * C style comments. * Unit tests. diff --git a/src/codegen.cpp b/src/codegen.cpp index 5a47f68de3..5cd8236260 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -60,6 +60,8 @@ struct CodeGen { LLVMTargetDataRef target_data_ref; unsigned pointer_size_bytes; bool is_static; + bool strip_debug_symbols; + CodeGenBuildType build_type; LLVMTargetMachineRef target_machine; Buf in_file; Buf in_dir; @@ -77,19 +79,33 @@ struct CodeGenNode { } data; }; -CodeGen *create_codegen(AstNode *root, bool is_static, Buf *in_full_path) { +CodeGen *create_codegen(AstNode *root, Buf *in_full_path) { CodeGen *g = allocate(1); g->root = root; g->fn_defs.init(32); g->fn_table.init(32); g->str_table.init(32); g->type_table.init(32); - g->is_static = is_static; + g->is_static = false; + g->build_type = CodeGenBuildTypeDebug; + g->strip_debug_symbols = false; os_path_split(in_full_path, &g->in_dir, &g->in_file); return g; } +void codegen_set_build_type(CodeGen *g, CodeGenBuildType build_type) { + g->build_type = build_type; +} + +void codegen_set_is_static(CodeGen *g, bool is_static) { + g->is_static = is_static; +} + +void codegen_set_strip(CodeGen *g, bool strip) { + g->strip_debug_symbols = strip; +} + static void add_node_error(CodeGen *g, AstNode *node, Buf *msg) { g->errors.add_one(); ErrorMsg *last_msg = &g->errors.last(); @@ -352,7 +368,8 @@ void semantic_analyze(CodeGen *g) { char *native_cpu = LLVMZigGetHostCPUName(); char *native_features = LLVMZigGetNativeFeatures(); - LLVMCodeGenOptLevel opt_level = LLVMCodeGenLevelNone; + LLVMCodeGenOptLevel opt_level = (g->build_type == CodeGenBuildTypeDebug) ? + LLVMCodeGenLevelNone : LLVMCodeGenLevelAggressive; LLVMRelocMode reloc_mode = g->is_static ? LLVMRelocStatic : LLVMRelocPIC; @@ -522,12 +539,13 @@ static llvm::DISubroutineType *create_di_function_type(CodeGen *g, AstNodeFnProt void code_gen(CodeGen *g) { Buf *producer = buf_sprintf("zig %s", ZIG_VERSION_STRING); - bool is_optimized = false; + bool is_optimized = g->build_type == CodeGenBuildTypeRelease; const char *flags = ""; unsigned runtime_version = 0; g->compile_unit = g->dbuilder->createCompileUnit(llvm::dwarf::DW_LANG_C99, buf_ptr(&g->in_file), buf_ptr(&g->in_dir), - buf_ptr(producer), is_optimized, flags, runtime_version); + buf_ptr(producer), is_optimized, flags, runtime_version, + "", llvm::DIBuilder::FullDebug, 0, !g->strip_debug_symbols); g->block_scopes.append(g->compile_unit); @@ -615,6 +633,9 @@ void code_gen_link(CodeGen *g, const char *out_file) { } ZigList args = {0}; + if (g->is_static) { + args.append("-static"); + } args.append("-o"); args.append(out_file); args.append((const char *)buf_ptr(&out_file_o)); diff --git a/src/codegen.hpp b/src/codegen.hpp index 98b906767f..49ee599c96 100644 --- a/src/codegen.hpp +++ b/src/codegen.hpp @@ -21,7 +21,15 @@ struct ErrorMsg { }; -CodeGen *create_codegen(AstNode *root, bool is_static, Buf *in_file); +CodeGen *create_codegen(AstNode *root, Buf *in_file); + +enum CodeGenBuildType { + CodeGenBuildTypeDebug, + CodeGenBuildTypeRelease, +}; +void codegen_set_build_type(CodeGen *codegen, CodeGenBuildType build_type); +void codegen_set_is_static(CodeGen *codegen, bool is_static); +void codegen_set_strip(CodeGen *codegen, bool strip); void semantic_analyze(CodeGen *g); diff --git a/src/main.cpp b/src/main.cpp index 846ded6340..5ca7ffe968 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,6 +33,9 @@ static int usage(const char *arg0) { " --output output file\n" " --version print version number and exit\n" " -Ipath add path to header include path\n" + " --release build with optimizations on\n" + " --strip exclude debug symbols\n" + " --static build a static executable\n" , arg0); return EXIT_FAILURE; } @@ -55,7 +58,9 @@ static Buf *fetch_file(FILE *f) { return buf; } -static int build(const char *arg0, const char *in_file, const char *out_file, ZigList *include_paths) { +static int build(const char *arg0, const char *in_file, const char *out_file, + ZigList *include_paths, bool release, bool strip, bool is_static) +{ static char cur_dir[1024]; if (!in_file || !out_file) @@ -91,7 +96,10 @@ static int build(const char *arg0, const char *in_file, const char *out_file, Zi fprintf(stderr, "\nSemantic Analysis:\n"); fprintf(stderr, "--------------------\n"); - CodeGen *codegen = create_codegen(root, false, buf_create_from_str(in_file)); + CodeGen *codegen = create_codegen(root, buf_create_from_str(in_file)); + codegen_set_build_type(codegen, release ? CodeGenBuildTypeRelease : CodeGenBuildTypeDebug); + codegen_set_strip(codegen, strip); + codegen_set_is_static(codegen, is_static); semantic_analyze(codegen); ZigList *errors = codegen_error_messages(codegen); if (errors->length == 0) { @@ -128,6 +136,9 @@ int main(int argc, char **argv) { char *in_file = NULL; char *out_file = NULL; ZigList include_paths = {0}; + bool release = false; + bool strip = false; + bool is_static = false; Cmd cmd = CmdNone; for (int i = 1; i < argc; i += 1) { @@ -136,6 +147,12 @@ int main(int argc, char **argv) { if (strcmp(arg, "--version") == 0) { printf("%s\n", ZIG_VERSION_STRING); return EXIT_SUCCESS; + } else if (strcmp(arg, "--release") == 0) { + release = true; + } else if (strcmp(arg, "--strip") == 0) { + strip = true; + } else if (strcmp(arg, "--static") == 0) { + is_static = true; } else if (i + 1 >= argc) { return usage(arg0); } else { @@ -174,7 +191,7 @@ int main(int argc, char **argv) { case CmdNone: return usage(arg0); case CmdBuild: - return build(arg0, in_file, out_file, &include_paths); + return build(arg0, in_file, out_file, &include_paths, release, strip, is_static); } zig_unreachable();