From dcd26bcfcc691f24508a7b6bf04c15042a4668ea Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 17 Sep 2019 22:05:40 +0200 Subject: Fix function call resolved type --- src/ast.c | 8 ++++++++ src/ssa/ssa.c | 20 +++++++++++++------- tests/glfw.amal | 6 +++++- tests/main.c | 2 +- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/ast.c b/src/ast.c index 8e102ee..0008785 100644 --- a/src/ast.c +++ b/src/ast.c @@ -743,6 +743,14 @@ static void funccall_resolve(Ast *self, AstCompilerContext *context) { throw(AST_ERR); } + { + FunctionSignature *func_sig = self->resolve_data.type.value.func_sig; + FunctionReturnType *return_type = buffer_begin(&func_sig->return_types); + assert(buffer_get_size(&func_sig->return_types, FunctionReturnType) == 1); + self->resolve_data.type.type = RESOLVED_TYPE_LHS_EXPR; + self->resolve_data.type = return_type->resolved_type; + } + ast = buffer_begin(&func_call->args); ast_end = buffer_end(&func_call->args); for(; ast != ast_end; ++ast) { diff --git a/src/ssa/ssa.c b/src/ssa/ssa.c index 13f19a9..d76ad28 100644 --- a/src/ssa/ssa.c +++ b/src/ssa/ssa.c @@ -640,7 +640,7 @@ static CHECK_RESULT SsaRegister funcdecl_generate_ssa(FunctionDecl *self, SsaCom return 0; } -static CHECK_RESULT SsaRegister funccall_generate_ssa(FunctionCall *self, AstResolveData *resolve_data, SsaCompilerContext *context) { +static CHECK_RESULT SsaRegister funccall_generate_ssa(FunctionCall *self, SsaCompilerContext *context) { SsaRegister reg; FunctionSignature *func_sig; FunctionDecl *func_decl; @@ -649,12 +649,18 @@ static CHECK_RESULT SsaRegister funccall_generate_ssa(FunctionCall *self, AstRes context->import_index = 0; throw_if_error(ssa_get_unique_reg(context->ssa, ®)); - assert(resolve_data->type.type == RESOLVED_TYPE_FUNC_SIG); - func_sig = resolve_data->type.value.func_sig; + assert(self->func.resolved_var.type == NAMED_OBJECT_LHS_EXPR); + func_lhs_expr = self->func.resolved_var.value.lhs_expr; + if(func_lhs_expr->type.type == VARIABLE_TYPE_SIGNATURE) { + func_sig = func_lhs_expr->type.value.signature; + } else if(func_lhs_expr->type.type == VARIABLE_TYPE_VARIABLE) { + AstResolveData *resolve_data = func_lhs_expr->type.value.variable->resolved_var.resolve_data; + assert(resolve_data->type.type == RESOLVED_TYPE_FUNC_SIG); + func_sig = resolve_data->type.value.func_sig; + } else { + assert(bool_false); + } func_decl = func_sig->func_decl; - func_lhs_expr = NULL; - if(self->func.resolved_var.type == NAMED_OBJECT_LHS_EXPR) - func_lhs_expr = self->func.resolved_var.value.lhs_expr; /* Push return arguments */ { @@ -869,7 +875,7 @@ static CHECK_RESULT SsaRegister ast_generate_ssa_resolve_data(void *ast_data, As resolve_data->ssa_reg = funcdecl_generate_ssa(ast_data, context); break; case AST_FUNCTION_CALL: - resolve_data->ssa_reg = funccall_generate_ssa(ast_data, resolve_data, context); + resolve_data->ssa_reg = funccall_generate_ssa(ast_data, context); break; case AST_STRUCT_DECL: resolve_data->ssa_reg = structdecl_generate_ssa(ast_data, context); diff --git a/tests/glfw.amal b/tests/glfw.amal index b888dac..d0f02d4 100644 --- a/tests/glfw.amal +++ b/tests/glfw.amal @@ -1,10 +1,14 @@ extern const glfwInit: fn() i32; extern const glfwTerminate: fn() i32; // should return void.... extern const glfwCreateWindow: fn(x: i32, y: i32, title: str, monitor: usize, share: usize) usize; +extern const glfwWindowShouldClose: fn(window: usize) i64; const main = fn { glfwInit(); - glfwCreateWindow(50, 50, "hello, world", 0, 0); + const window = glfwCreateWindow(50, 50, "hello, world", 0, 0); + while glfwWindowShouldClose(window) == 0 { + + } glfwTerminate(); //const a = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1; } \ No newline at end of file diff --git a/tests/main.c b/tests/main.c index 1a86769..265a0ea 100644 --- a/tests/main.c +++ b/tests/main.c @@ -204,7 +204,7 @@ static void test_load_gl(void) { fprintf(stderr, "Unexpected error (alloc failure)\n"); FAIL_TEST_CLEANUP(full_path); } - if(amal_program_register_extern_func(&program, create_buffer_view_auto("print_extern_num"), print_extern_num, sizeof(i64)) != 0) { + if(amal_program_register_extern_func(&program, create_buffer_view_auto("glfwWindowShouldClose"), glfwWindowShouldClose, sizeof(void*)) != 0) { fprintf(stderr, "Unexpected error (alloc failure)\n"); FAIL_TEST_CLEANUP(full_path); } -- cgit v1.2.3