diff options
author | Carlos Maniero <carlos@maniero.me> | 2023-05-10 22:24:14 -0300 |
---|---|---|
committer | Carlos Maniero <carlos@maniero.me> | 2023-05-10 22:45:31 -0300 |
commit | 5042a4ffc1363d6f0f99a3afd79f76cf2da738d6 (patch) | |
tree | 90c31d77ddf6b9051669fafdc6dfe0fc3b1f35eb /src/ast.h | |
parent | 6f187a71cbe3aa4ebb32ba287c75562d96c7a3f4 (diff) |
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 <carlos@maniero.me>
Diffstat (limited to 'src/ast.h')
-rw-r--r-- | src/ast.h | 33 |
1 files changed, 30 insertions, 3 deletions
@@ -57,12 +57,25 @@ typedef struct ast_variable_t typedef struct ast_function_call_t { ast_identifier_t *identifier; + vector_t *arguments; } ast_function_call_t; -typedef struct ast_function_declaration_t +typedef struct ast_function_parameter_t +{ + ast_identifier_t identifier; + type_t type; +} ast_function_parameter_t; + +typedef struct ast_function_prototype_t { ast_identifier_t identifier; type_t return_type; + vector_t *parameters; +} ast_function_prototype_t; + +typedef struct ast_function_declaration_t +{ + ast_function_prototype_t prototype; ast_node_t *body; } ast_function_declaration_t; @@ -133,6 +146,7 @@ typedef enum AST_BINARY_OPERATION, AST_BLOCK, AST_FUNCTION_DECLARATION, + AST_FUNCTION_PARAMETER, AST_FUNCTION_CALL, AST_LITERAL, AST_RETURN_STMT, @@ -148,6 +162,7 @@ typedef union ast_namespace_t ns; ast_binary_operation_t binary_operation; ast_function_declaration_t function; + ast_function_parameter_t function_parameter; ast_function_call_t function_call; ast_literal_t literal; ast_block_t block; @@ -187,10 +202,16 @@ ast_node_new_binary_operation(ast_binary_operation_kind_t kind, type_t result_type); ast_node_t * -ast_node_new_function_declaration(string_view_t function_name, type_t return_type, ast_node_t *body); +ast_node_new_function_declaration(string_view_t function_name, + type_t return_type, + vector_t *parameters, + ast_node_t *body); + +ast_node_t * +ast_node_new_function_parameter(string_view_t name, type_t type); ast_node_t * -ast_node_new_function_call(ast_identifier_t *identifier, type_t result_type); +ast_node_new_function_call(ast_identifier_t *identifier, type_t result_type, vector_t *arguments); ast_node_t * ast_node_new_return_stmt(ast_node_t *argument); @@ -228,4 +249,10 @@ ast_node_is_variable_declaration(ast_node_t *node); bool ast_node_is_function_declaration(ast_node_t *node); +ast_identifier_t * +ast_node_function_declaration_identifier(ast_node_t *node); + +string_view_t +ast_node_function_declaration_name(ast_node_t *node); + #endif /* AST_H */ |