Commit 8c10b6dcbd

Andrew Kelley <superjoe30@gmail.com>
2017-04-05 06:19:06
ability to use zig as an assembler
see #243
1 parent a461ae6
Changed files (3)
src/codegen.cpp
@@ -4535,6 +4535,22 @@ void codegen_add_root_code(CodeGen *g, Buf *src_dir, Buf *src_basename, Buf *sou
     do_code_gen(g);
 }
 
+void codegen_add_root_assembly(CodeGen *g, Buf *src_dir, Buf *src_basename, Buf *source_code) {
+    Buf source_path = BUF_INIT;
+    os_path_join(src_dir, src_basename, &source_path);
+
+    init(g, &source_path);
+
+    assert(g->root_out_name);
+    assert(g->out_type != OutTypeUnknown);
+
+    buf_init_from_str(&g->global_asm, ".intel_syntax noprefix\n");
+    buf_append_buf(&g->global_asm, source_code);
+
+    do_code_gen(g);
+}
+
+
 static const char *c_int_type_names[] = {
     [CIntTypeShort] = "short",
     [CIntTypeUShort] = "unsigned short",
src/codegen.hpp
@@ -47,6 +47,7 @@ void codegen_set_omit_zigrt(CodeGen *g, bool omit_zigrt);
 
 PackageTableEntry *new_package(const char *root_src_dir, const char *root_src_path);
 void codegen_add_root_code(CodeGen *g, Buf *source_dir, Buf *source_basename, Buf *source_code);
+void codegen_add_root_assembly(CodeGen *g, Buf *source_dir, Buf *source_basename, Buf *source_code);
 
 void codegen_parseh(CodeGen *g, Buf *src_dirname, Buf *src_basename, Buf *source_code);
 void codegen_render_ast(CodeGen *g, FILE *f, int indent_size);
src/main.cpp
@@ -19,6 +19,7 @@
 static int usage(const char *arg0) {
     fprintf(stderr, "Usage: %s [command] [options]\n"
         "Commands:\n"
+        "  asm [source]                 create object from assembly\n"
         "  build                        build project from build.zig\n"
         "  build_exe [source]           create executable from source\n"
         "  build_lib [source]           create library from source\n"
@@ -106,6 +107,7 @@ enum Cmd {
     CmdVersion,
     CmdParseH,
     CmdTargets,
+    CmdAsm,
 };
 
 int main(int argc, char **argv) {
@@ -310,6 +312,8 @@ int main(int argc, char **argv) {
                 cmd = CmdTest;
             } else if (strcmp(arg, "targets") == 0) {
                 cmd = CmdTargets;
+            } else if (strcmp(arg, "asm") == 0) {
+                cmd = CmdAsm;
             } else {
                 fprintf(stderr, "Unrecognized command: %s\n", arg);
                 return usage(arg0);
@@ -319,6 +323,7 @@ int main(int argc, char **argv) {
                 case CmdBuild:
                 case CmdParseH:
                 case CmdTest:
+                case CmdAsm:
                     if (!in_file) {
                         in_file = arg;
                     } else {
@@ -338,6 +343,7 @@ int main(int argc, char **argv) {
     case CmdBuild:
     case CmdParseH:
     case CmdTest:
+    case CmdAsm:
         {
             if (!in_file)
                 return usage(arg0);
@@ -373,6 +379,7 @@ int main(int argc, char **argv) {
                 }
             }
 
+            bool need_name = (cmd == CmdBuild || cmd == CmdAsm);
 
             Buf in_file_buf = BUF_INIT;
             buf_init_from_str(&in_file_buf, in_file);
@@ -398,14 +405,14 @@ int main(int argc, char **argv) {
                     return 1;
                 }
 
-                if (cmd == CmdBuild && buf_out_name == nullptr) {
+                if (need_name && buf_out_name == nullptr) {
                     buf_out_name = buf_alloc();
                     Buf ext_name = BUF_INIT;
                     os_path_extname(&root_source_name, buf_out_name, &ext_name);
                 }
             }
 
-            if (cmd == CmdBuild && buf_out_name == nullptr) {
+            if (need_name && buf_out_name == nullptr) {
                 fprintf(stderr, "--name [name] not provided and unable to infer\n\n");
                 return usage(arg0);
             }
@@ -420,7 +427,9 @@ int main(int argc, char **argv) {
             codegen_set_clang_argv(g, clang_argv.items, clang_argv.length);
             codegen_set_strip(g, strip);
             codegen_set_is_static(g, is_static);
-            if (out_type != OutTypeUnknown) {
+            if (cmd == CmdAsm) {
+                codegen_set_out_type(g, OutTypeObj);
+            } else if (out_type != OutTypeUnknown) {
                 codegen_set_out_type(g, out_type);
             } else if (cmd == CmdTest) {
                 codegen_set_out_type(g, OutTypeExe);
@@ -475,6 +484,10 @@ int main(int argc, char **argv) {
                 codegen_add_root_code(g, &root_source_dir, &root_source_name, &root_source_code);
                 codegen_link(g, out_file);
                 return EXIT_SUCCESS;
+            } else if (cmd == CmdAsm) {
+                codegen_add_root_assembly(g, &root_source_dir, &root_source_name, &root_source_code);
+                codegen_link(g, out_file);
+                return EXIT_SUCCESS;
             } else if (cmd == CmdParseH) {
                 codegen_parseh(g, &root_source_dir, &root_source_name, &root_source_code);
                 ast_render_decls(stdout, 4, g->root_import);