authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-05 00:19:06-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-05 03:36:55-04:00
log8c10b6dcbddc58b488e399690e8ddefb4bafb816
tree220269dd294d89c08364ba2a6ee32fadcb8e7be5
parenta461ae6c1fd1f8a75126327f104c7c077d35f0a5

ability to use zig as an assembler

see #243

3 files changed, 33 insertions(+), 3 deletions(-)

src/codegen.cpp+16
......@@ -4535,6 +4535,22 @@ void codegen_add_root_code(CodeGen *g, Buf *src_dir, Buf *src_basename, Buf *sou
45354535 do_code_gen(g);
45364536}
45374537
4538void codegen_add_root_assembly(CodeGen *g, Buf *src_dir, Buf *src_basename, Buf *source_code) {
4539 Buf source_path = BUF_INIT;
4540 os_path_join(src_dir, src_basename, &source_path);
4541
4542 init(g, &source_path);
4543
4544 assert(g->root_out_name);
4545 assert(g->out_type != OutTypeUnknown);
4546
4547 buf_init_from_str(&g->global_asm, ".intel_syntax noprefix\n");
4548 buf_append_buf(&g->global_asm, source_code);
4549
4550 do_code_gen(g);
4551}
4552
4553
45384554static const char *c_int_type_names[] = {
45394555 [CIntTypeShort] = "short",
45404556 [CIntTypeUShort] = "unsigned short",
src/codegen.hpp+1
......@@ -47,6 +47,7 @@ void codegen_set_omit_zigrt(CodeGen *g, bool omit_zigrt);
4747
4848PackageTableEntry *new_package(const char *root_src_dir, const char *root_src_path);
4949void codegen_add_root_code(CodeGen *g, Buf *source_dir, Buf *source_basename, Buf *source_code);
50void codegen_add_root_assembly(CodeGen *g, Buf *source_dir, Buf *source_basename, Buf *source_code);
5051
5152void codegen_parseh(CodeGen *g, Buf *src_dirname, Buf *src_basename, Buf *source_code);
5253void codegen_render_ast(CodeGen *g, FILE *f, int indent_size);
src/main.cpp+16-3
......@@ -19,6 +19,7 @@
1919static int usage(const char *arg0) {
2020 fprintf(stderr, "Usage: %s [command] [options]\n"
2121 "Commands:\n"
22 " asm [source] create object from assembly\n"
2223 " build build project from build.zig\n"
2324 " build_exe [source] create executable from source\n"
2425 " build_lib [source] create library from source\n"
......@@ -106,6 +107,7 @@ enum Cmd {
106107 CmdVersion,
107108 CmdParseH,
108109 CmdTargets,
110 CmdAsm,
109111};
110112
111113int main(int argc, char **argv) {
......@@ -310,6 +312,8 @@ int main(int argc, char **argv) {
310312 cmd = CmdTest;
311313 } else if (strcmp(arg, "targets") == 0) {
312314 cmd = CmdTargets;
315 } else if (strcmp(arg, "asm") == 0) {
316 cmd = CmdAsm;
313317 } else {
314318 fprintf(stderr, "Unrecognized command: %s\n", arg);
315319 return usage(arg0);
......@@ -319,6 +323,7 @@ int main(int argc, char **argv) {
319323 case CmdBuild:
320324 case CmdParseH:
321325 case CmdTest:
326 case CmdAsm:
322327 if (!in_file) {
323328 in_file = arg;
324329 } else {
......@@ -338,6 +343,7 @@ int main(int argc, char **argv) {
338343 case CmdBuild:
339344 case CmdParseH:
340345 case CmdTest:
346 case CmdAsm:
341347 {
342348 if (!in_file)
343349 return usage(arg0);
......@@ -373,6 +379,7 @@ int main(int argc, char **argv) {
373379 }
374380 }
375381
382 bool need_name = (cmd == CmdBuild || cmd == CmdAsm);
376383
377384 Buf in_file_buf = BUF_INIT;
378385 buf_init_from_str(&in_file_buf, in_file);
......@@ -398,14 +405,14 @@ int main(int argc, char **argv) {
398405 return 1;
399406 }
400407
401 if (cmd == CmdBuild && buf_out_name == nullptr) {
408 if (need_name && buf_out_name == nullptr) {
402409 buf_out_name = buf_alloc();
403410 Buf ext_name = BUF_INIT;
404411 os_path_extname(&root_source_name, buf_out_name, &ext_name);
405412 }
406413 }
407414
408 if (cmd == CmdBuild && buf_out_name == nullptr) {
415 if (need_name && buf_out_name == nullptr) {
409416 fprintf(stderr, "--name [name] not provided and unable to infer\n\n");
410417 return usage(arg0);
411418 }
......@@ -420,7 +427,9 @@ int main(int argc, char **argv) {
420427 codegen_set_clang_argv(g, clang_argv.items, clang_argv.length);
421428 codegen_set_strip(g, strip);
422429 codegen_set_is_static(g, is_static);
423 if (out_type != OutTypeUnknown) {
430 if (cmd == CmdAsm) {
431 codegen_set_out_type(g, OutTypeObj);
432 } else if (out_type != OutTypeUnknown) {
424433 codegen_set_out_type(g, out_type);
425434 } else if (cmd == CmdTest) {
426435 codegen_set_out_type(g, OutTypeExe);
......@@ -475,6 +484,10 @@ int main(int argc, char **argv) {
475484 codegen_add_root_code(g, &root_source_dir, &root_source_name, &root_source_code);
476485 codegen_link(g, out_file);
477486 return EXIT_SUCCESS;
487 } else if (cmd == CmdAsm) {
488 codegen_add_root_assembly(g, &root_source_dir, &root_source_name, &root_source_code);
489 codegen_link(g, out_file);
490 return EXIT_SUCCESS;
478491 } else if (cmd == CmdParseH) {
479492 codegen_parseh(g, &root_source_dir, &root_source_name, &root_source_code);
480493 ast_render_decls(stdout, 4, g->root_import);