From 2cf0bcb409f3a1fd298b664103d57c945c6349f5 Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Wed, 10 May 2023 14:26:40 -0300 Subject: gas: Removes Linux entrypoint logic from function declaration Before this commit, function declarations were making syscalls to interrupt the flow. This is a fair approach considering all our examples just have a main function. But won't work if the namespace has more then a single function. The return now always sets the return value on RAX and jumps to the function return label. The function return label, will restore RBP and jump back to callee's next instruction using RET instruction. Function labels are kept, which means that a function called my_fn will have the assembly label my_fn, so then, they can have INTEROP with other languages. Signed-off-by: Carlos Maniero --- src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 4f68256..780499c 100644 --- a/src/main.c +++ b/src/main.c @@ -31,7 +31,7 @@ generate_gas_x86_64_linux(ast_node_t *func) { gas_assembly_generator_t gen; gas_assembly_generator_init(&gen, stdout); - gas_assembly_generator_compile(&gen, func); + gas_assembly_generator_compile_linux_main(&gen, func); } static void -- cgit v1.2.3 From 75639fbf01bd6ae1212521b6cf822025eb8b598d Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Wed, 10 May 2023 16:07:39 -0300 Subject: namespaces: Add a namespace structure that represents a file We have been always parsing a single function. Since we want to have multiple functions in a near future, this patch introduces an namespace that represents an entire file. To ensure a function is defined inside a namespace, a helper function was created. Today our ast_node structure is highly exposed, and this is something that Johnny and I have been discussed. So then, this is a first step to try to protected the code generation from our ast tree. Signed-off-by: Carlos Maniero --- src/main.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 780499c..ca6ed9b 100644 --- a/src/main.c +++ b/src/main.c @@ -27,11 +27,11 @@ #include "string_view.h" static void -generate_gas_x86_64_linux(ast_node_t *func) +generate_gas_x86_64_linux(ast_node_t *ns) { gas_assembly_generator_t gen; gas_assembly_generator_init(&gen, stdout); - gas_assembly_generator_compile_linux_main(&gen, func); + gas_assembly_generator_compile_linux_main(&gen, ns); } static void @@ -89,20 +89,20 @@ main(int argc, char **argv) parser_t parser; parser_init(&parser, &lexer, scope); - ast_node_t *func = parser_parse_function_declaration(&parser); + ast_node_t *ns = parser_parse_ns(&parser); - if (func == NULL) { + if (ns == NULL) { parser_print_errors(&parser); return EXIT_FAILURE; } if (should_dump_ast) { - pretty_print_ast(func); + pretty_print_ast(ns); } else { - generate_gas_x86_64_linux(func); + generate_gas_x86_64_linux(ns); } scope_destroy(scope); - ast_node_destroy(func); + ast_node_destroy(ns); return EXIT_SUCCESS; } -- cgit v1.2.3