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 --- src/lexer.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/lexer.c') diff --git a/src/lexer.c b/src/lexer.c index a6c8ae2..7c668d3 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -165,6 +165,12 @@ lexer_next_token(lexer_t *lexer, token_t *token) return; } + if (lexer_current_char(lexer) == ',') { + lexer_define_literal_token_props(lexer, token, TOKEN_COMMA); + lexer_drop_char(lexer); + return; + } + if (lexer_current_char(lexer) == ';') { lexer_define_literal_token_props(lexer, token, TOKEN_SEMICOLON); lexer_drop_char(lexer); @@ -412,6 +418,8 @@ token_kind_to_str(token_kind_t kind) return ")"; case TOKEN_COLON: return ":"; + case TOKEN_COMMA: + return ","; case TOKEN_SEMICOLON: return ";"; case TOKEN_OCURLY: -- cgit v1.2.3 From ea7f65fe1250be8f49edcaaedd3410aed1401648 Mon Sep 17 00:00:00 2001 From: Carlos Maniero Date: Thu, 11 May 2023 15:00:10 -0300 Subject: gas: implement recursion and late evaluation Until now the below code was not valid for pipac. fn main(): i32 { return fib(13); } fn fib(n: i32): i32 { if n <= 1 { return n; } return fib(n - 1) + fib(n - 2); } Pipa's parser was adding a function to scope after they were fully parsed which means that fib's self-reference would not work. Also, functions were required to follow the be called in the order they are declared for the same scope reason so, the main function was required to be defined after fib. And how it is working now? When a TOKEN_NAME is not found in the scope, instead of returning an error, an unknown token is created as placeholder. The parser stores the node reference and the token it was trying to parse. During type checks, if the parser detects an unknown node, instead of returning an error, it stores in that node what was the expected type. After the NS is fully parsed a reevaluation is made on those unknown nodes by setting the lexer back on the node's token position and parsing the TOKEN_NAME again. Ps: There is a typo on the unknown token. It will be addressed in another commit since this issue was not introduced by this change. Signed-off-by: Carlos Maniero --- src/lexer.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/lexer.c') diff --git a/src/lexer.c b/src/lexer.c index 7c668d3..c00c944 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -348,6 +348,14 @@ lexer_load_file_contents(lexer_t *lexer) } } +void +lexer_step_back_to(lexer_t *lexer, token_t *token) +{ + lexer->cur = token->bol + token->col; + lexer->row = token->row; + lexer->bol = token->bol; +} + void lexer_lookahead(lexer_t *lexer, token_t *token, size_t level) { -- cgit v1.2.3