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...@@ -4535,6 +4535,22 @@ void codegen_add_root_code(CodeGen *g, Buf *src_dir, Buf *src_basename, Buf *sou
4535 do_code_gen(g);4535 do_code_gen(g);
4536}4536}
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
4538static const char *c_int_type_names[] = {4554static const char *c_int_type_names[] = {
4539 [CIntTypeShort] = "short",4555 [CIntTypeShort] = "short",
4540 [CIntTypeUShort] = "unsigned short",4556 [CIntTypeUShort] = "unsigned short",
src/codegen.hpp+1
...@@ -47,6 +47,7 @@ void codegen_set_omit_zigrt(CodeGen *g, bool omit_zigrt);...@@ -47,6 +47,7 @@ void codegen_set_omit_zigrt(CodeGen *g, bool omit_zigrt);
4747
48PackageTableEntry *new_package(const char *root_src_dir, const char *root_src_path);48PackageTableEntry *new_package(const char *root_src_dir, const char *root_src_path);
49void codegen_add_root_code(CodeGen *g, Buf *source_dir, Buf *source_basename, Buf *source_code);49void 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
51void codegen_parseh(CodeGen *g, Buf *src_dirname, Buf *src_basename, Buf *source_code);52void codegen_parseh(CodeGen *g, Buf *src_dirname, Buf *src_basename, Buf *source_code);
52void codegen_render_ast(CodeGen *g, FILE *f, int indent_size);53void codegen_render_ast(CodeGen *g, FILE *f, int indent_size);
src/main.cpp+16-3
...@@ -19,6 +19,7 @@...@@ -19,6 +19,7 @@
19static int usage(const char *arg0) {19static int usage(const char *arg0) {
20 fprintf(stderr, "Usage: %s [command] [options]\n"20 fprintf(stderr, "Usage: %s [command] [options]\n"
21 "Commands:\n"21 "Commands:\n"
22 " asm [source] create object from assembly\n"
22 " build build project from build.zig\n"23 " build build project from build.zig\n"
23 " build_exe [source] create executable from source\n"24 " build_exe [source] create executable from source\n"
24 " build_lib [source] create library from source\n"25 " build_lib [source] create library from source\n"
...@@ -106,6 +107,7 @@ enum Cmd {...@@ -106,6 +107,7 @@ enum Cmd {
106 CmdVersion,107 CmdVersion,
107 CmdParseH,108 CmdParseH,
108 CmdTargets,109 CmdTargets,
110 CmdAsm,
109};111};
110112
111int main(int argc, char **argv) {113int main(int argc, char **argv) {
...@@ -310,6 +312,8 @@ int main(int argc, char **argv) {...@@ -310,6 +312,8 @@ int main(int argc, char **argv) {
310 cmd = CmdTest;312 cmd = CmdTest;
311 } else if (strcmp(arg, "targets") == 0) {313 } else if (strcmp(arg, "targets") == 0) {
312 cmd = CmdTargets;314 cmd = CmdTargets;
315 } else if (strcmp(arg, "asm") == 0) {
316 cmd = CmdAsm;
313 } else {317 } else {
314 fprintf(stderr, "Unrecognized command: %s\n", arg);318 fprintf(stderr, "Unrecognized command: %s\n", arg);
315 return usage(arg0);319 return usage(arg0);
...@@ -319,6 +323,7 @@ int main(int argc, char **argv) {...@@ -319,6 +323,7 @@ int main(int argc, char **argv) {
319 case CmdBuild:323 case CmdBuild:
320 case CmdParseH:324 case CmdParseH:
321 case CmdTest:325 case CmdTest:
326 case CmdAsm:
322 if (!in_file) {327 if (!in_file) {
323 in_file = arg;328 in_file = arg;
324 } else {329 } else {
...@@ -338,6 +343,7 @@ int main(int argc, char **argv) {...@@ -338,6 +343,7 @@ int main(int argc, char **argv) {
338 case CmdBuild:343 case CmdBuild:
339 case CmdParseH:344 case CmdParseH:
340 case CmdTest:345 case CmdTest:
346 case CmdAsm:
341 {347 {
342 if (!in_file)348 if (!in_file)
343 return usage(arg0);349 return usage(arg0);
...@@ -373,6 +379,7 @@ int main(int argc, char **argv) {...@@ -373,6 +379,7 @@ int main(int argc, char **argv) {
373 }379 }
374 }380 }
375381
382 bool need_name = (cmd == CmdBuild || cmd == CmdAsm);
376383
377 Buf in_file_buf = BUF_INIT;384 Buf in_file_buf = BUF_INIT;
378 buf_init_from_str(&in_file_buf, in_file);385 buf_init_from_str(&in_file_buf, in_file);
...@@ -398,14 +405,14 @@ int main(int argc, char **argv) {...@@ -398,14 +405,14 @@ int main(int argc, char **argv) {
398 return 1;405 return 1;
399 }406 }
400407
401 if (cmd == CmdBuild && buf_out_name == nullptr) {408 if (need_name && buf_out_name == nullptr) {
402 buf_out_name = buf_alloc();409 buf_out_name = buf_alloc();
403 Buf ext_name = BUF_INIT;410 Buf ext_name = BUF_INIT;
404 os_path_extname(&root_source_name, buf_out_name, &ext_name);411 os_path_extname(&root_source_name, buf_out_name, &ext_name);
405 }412 }
406 }413 }
407414
408 if (cmd == CmdBuild && buf_out_name == nullptr) {415 if (need_name && buf_out_name == nullptr) {
409 fprintf(stderr, "--name [name] not provided and unable to infer\n\n");416 fprintf(stderr, "--name [name] not provided and unable to infer\n\n");
410 return usage(arg0);417 return usage(arg0);
411 }418 }
...@@ -420,7 +427,9 @@ int main(int argc, char **argv) {...@@ -420,7 +427,9 @@ int main(int argc, char **argv) {
420 codegen_set_clang_argv(g, clang_argv.items, clang_argv.length);427 codegen_set_clang_argv(g, clang_argv.items, clang_argv.length);
421 codegen_set_strip(g, strip);428 codegen_set_strip(g, strip);
422 codegen_set_is_static(g, is_static);429 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) {
424 codegen_set_out_type(g, out_type);433 codegen_set_out_type(g, out_type);
425 } else if (cmd == CmdTest) {434 } else if (cmd == CmdTest) {
426 codegen_set_out_type(g, OutTypeExe);435 codegen_set_out_type(g, OutTypeExe);
...@@ -475,6 +484,10 @@ int main(int argc, char **argv) {...@@ -475,6 +484,10 @@ int main(int argc, char **argv) {
475 codegen_add_root_code(g, &root_source_dir, &root_source_name, &root_source_code);484 codegen_add_root_code(g, &root_source_dir, &root_source_name, &root_source_code);
476 codegen_link(g, out_file);485 codegen_link(g, out_file);
477 return EXIT_SUCCESS;486 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;
478 } else if (cmd == CmdParseH) {491 } else if (cmd == CmdParseH) {
479 codegen_parseh(g, &root_source_dir, &root_source_name, &root_source_code);492 codegen_parseh(g, &root_source_dir, &root_source_name, &root_source_code);
480 ast_render_decls(stdout, 4, g->root_import);493 ast_render_decls(stdout, 4, g->root_import);