From 8c10b6dcbddc58b488e399690e8ddefb4bafb816 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 5 Apr 2017 00:19:06 -0400 Subject: [PATCH] ability to use zig as an assembler see #243 --- src/codegen.cpp | 16 ++++++++++++++++ src/codegen.hpp | 1 + src/main.cpp | 19 ++++++++++++++++--- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/codegen.cpp b/src/codegen.cpp index 83b7e01614..28a8243aca 100644 --- a/src/codegen.cpp +++ b/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", diff --git a/src/codegen.hpp b/src/codegen.hpp index 6b493e4464..bf65a19dd3 100644 --- a/src/codegen.hpp +++ b/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); diff --git a/src/main.cpp b/src/main.cpp index 30441f51f8..afe05b5d6f 100644 --- a/src/main.cpp +++ b/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);