From 5042a4ffc1363d6f0f99a3afd79f76cf2da738d6 Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Wed, 10 May 2023 22:24:14 -0300 Subject: gas: implement function calls For now function calls are following the C's calling convention, which means they are using the following registers to pass functions' arguments: rdi, rsi, rdx, rcx, r8, r9 If a function has more then 6 parameters, the compilation will fail. To enable function with more than 6 parameters we will need to save the extra arguments on stack. Naming: parameters: function parameters are the variables a function receives. arguments: Arguments are the values passed to a function when calling it. Calling mechanism: When a function is called, all the expressions passed as argument are evaluated, after the evaluation, the result is stored on the register that represents its argument position, the first argument will be stored on rdi, the second on rsi and so on. Receiving mechanism: When a function starts, the first thing it does is store all the registers onto the stack. So rdi will be stored on -8(rbp), rsi on -16(rbp) and so on. And, a ref_entry is created making the relationship parameter-stack_offset. Signed-off-by: Carlos Maniero --- test/integration_test.c | 1 + test/parser_test.c | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/integration_test.c b/test/integration_test.c index 985e574..608764a 100644 --- a/test/integration_test.c +++ b/test/integration_test.c @@ -44,6 +44,7 @@ test_examples(const MunitParameter params[], void *user_data_or_fixture) assert_exit_status("../examples/arithmetics.pipa", 13); assert_exit_status("../examples/variables.pipa", 28); assert_exit_status("../examples/if.pipa", 42); + assert_exit_status("../examples/function_call.pipa", 42); return MUNIT_OK; } diff --git a/test/parser_test.c b/test/parser_test.c index ebb917c..44ffd57 100644 --- a/test/parser_test.c +++ b/test/parser_test.c @@ -104,7 +104,7 @@ test_parse_variable_definition(const MunitParameter params[], void *user_data_or char actual[5]; - string_view_to_str(&ast_function->data.function.identifier.name, actual); + string_view_to_str(&ast_function->data.function.prototype.identifier.name, actual); assert_string_equal("main", actual); assert_int(AST_FUNCTION_DECLARATION, ==, ast_function->kind); @@ -141,7 +141,7 @@ test_parse_boolean(const MunitParameter params[], void *user_data_or_fixture) assert_true(ast_function != NULL); - assert_string_view_equal("my_bool_fn", ast_function->data.function.identifier.name); + assert_string_view_equal("my_bool_fn", ast_node_function_declaration_name(ast_function)); assert_int(AST_FUNCTION_DECLARATION, ==, ast_function->kind); ast_node_t *ast_variable = vector_at(ast_function->data.function.body->data.block.body, 0); @@ -397,7 +397,7 @@ test_parse_basic_syntax_errors(const MunitParameter params[], void *user_data_or assert_parser_error("main(): i32 { return 42; }", "Unexpected token 'main'"); assert_parser_error("fn (): i32 { return 42; }", "expected 'TOKEN_NAME' but got '('"); assert_parser_error("fn main): i32 { return 42; }", "expected '(' but got ')'"); - assert_parser_error("fn main(: i32 { return 42; }", "expected ')' but got ':'"); + assert_parser_error("fn main(: i32 { return 42; }", "expected 'TOKEN_NAME' but got ':'"); assert_parser_error("fn main() i32 { return 42; }", "expected ':' but got 'TOKEN_NAME'"); assert_parser_error("fn main(): { return 42; }", "expected 'TOKEN_NAME' but got '{'"); assert_parser_error("fn main(): i32 return 42; }", "expected '{' but got 'return'"); -- cgit v1.2.3