From 6927b6338b9655974db79c429e6ffc73037ab5e0 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 29 Mar 2019 20:49:29 +0100 Subject: Fix ssa call, use correct tokenizer in error --- src/ssa/ssa.c | 66 ++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 23 deletions(-) (limited to 'src/ssa/ssa.c') diff --git a/src/ssa/ssa.c b/src/ssa/ssa.c index 3fffb63..2391778 100644 --- a/src/ssa/ssa.c +++ b/src/ssa/ssa.c @@ -119,8 +119,8 @@ static CHECK_RESULT int ssa_add_ins_form1(Ssa *self, SsaInstructionType ins_type return_if_error(buffer_append(&self->instructions, NULL, sizeof(u8) + sizeof(SsaRegister) + sizeof(u16))); self->instructions.data[index + 0] = ins_type; - *(SsaRegister*)&self->instructions.data[index + 1] = lhs; - *(u16*)&self->instructions.data[index + 3] = rhs; + am_memcpy(self->instructions.data + index + 1, &lhs, sizeof(lhs)); + am_memcpy(self->instructions.data + index + 3, &rhs, sizeof(rhs)); return 0; } @@ -147,9 +147,9 @@ static CHECK_RESULT int ssa_add_ins_form2(Ssa *self, SsaInstructionType ins_type return_if_error(buffer_append(&self->instructions, NULL, sizeof(u8) + sizeof(SsaRegister) + sizeof(SsaRegister) + sizeof(SsaRegister))); *result = self->reg_counter++; self->instructions.data[index + 0] = ins_type; - *(SsaRegister*)&self->instructions.data[index + 1] = *result; - *(SsaRegister*)&self->instructions.data[index + 3] = lhs; - *(SsaRegister*)&self->instructions.data[index + 5] = rhs; + am_memcpy(self->instructions.data + index + 1, &result, sizeof(*result)); + am_memcpy(self->instructions.data + index + 3, &lhs, sizeof(lhs)); + am_memcpy(self->instructions.data + index + 5, &rhs, sizeof(rhs)); amal_log_debug("r%u = r%u %s r%u", *result, lhs, binop_type_to_string(ins_type), rhs); return 0; } @@ -170,7 +170,7 @@ int ssa_ins_assign_string(Ssa *self, SsaRegister dest, BufferView str) { int ssa_ins_assign_reg(Ssa *self, SsaRegister dest, SsaRegister src) { amal_log_debug("r%u = r%u", dest, src); - return ssa_add_ins_form1(self, SSA_ASSIGN_INTER, dest, src); + return ssa_add_ins_form1(self, SSA_ASSIGN_REG, dest, src); } int ssa_ins_binop(Ssa *self, SsaInstructionType binop_type, SsaRegister lhs, SsaRegister rhs, SsaRegister *result) { @@ -189,7 +189,7 @@ int ssa_ins_func_start(Ssa *self, u8 num_args, SsaFuncIndex *result) { return_if_error(buffer_append(&self->instructions, NULL, sizeof(u8) + sizeof(SsaFuncIndex) + sizeof(u8))); *result = self->func_counter++; self->instructions.data[index + 0] = SSA_FUNC_START; - *(SsaFuncIndex*)&self->instructions.data[index + 1] = *result; + am_memcpy(self->instructions.data + index + 1, result, sizeof(*result)); self->instructions.data[index + 3] = num_args; amal_log_debug("FUNC_START f%u(%u)", *result, num_args); return 0; @@ -208,12 +208,12 @@ int ssa_ins_push(Ssa *self, SsaRegister reg) { return_if_error(buffer_append(&self->instructions, NULL, sizeof(u8) + sizeof(SsaRegister))); self->instructions.data[index + 0] = SSA_PUSH; - *(SsaRegister*)&self->instructions.data[index + 1] = reg; + am_memcpy(self->instructions.data + index + 1, ®, sizeof(reg)); amal_log_debug("PUSH r%u", reg); return 0; } -int ssa_ins_call(Ssa *self, SsaFuncIndex func, SsaRegister *result) { +int ssa_ins_call(Ssa *self, void *func, SsaRegister *result) { usize index; index = self->instructions.size; @@ -224,9 +224,9 @@ int ssa_ins_call(Ssa *self, SsaFuncIndex func, SsaRegister *result) { return_if_error(buffer_append(&self->instructions, NULL, sizeof(u8) + sizeof(SsaFuncIndex) + sizeof(SsaRegister))); *result = self->reg_counter++; self->instructions.data[index + 0] = SSA_CALL; - *(SsaFuncIndex*)&self->instructions.data[index + 1] = func; - *(SsaRegister*)&self->instructions.data[index + 3] = *result; - amal_log_debug("r%u = CALL f%u", *result, func); + am_memcpy(self->instructions.data + index + 1, &func, sizeof(func)); + am_memcpy(self->instructions.data + index + 3, result, sizeof(*result)); + amal_log_debug("r%u = CALL %p", *result, func); return 0; } @@ -247,20 +247,34 @@ static CHECK_RESULT SsaRegister number_generate_ssa(Number *self, SsaCompilerCon return reg; } - -static CHECK_RESULT SsaRegister lhs_generate_ssa(LhsExpr *self, SsaCompilerContext *context) { +static CHECK_RESULT SsaRegister lhsexpr_generate_ssa(Ast *self, SsaCompilerContext *context) { /* TODO: Implement */ + LhsExpr *lhs_expr; SsaRegister reg; - throw_if_error(amal_mutex_lock(self->mutex, "lhs_generate_ssa")); - throw_if_error(ssa_get_unique_reg(&context->ssa, ®)); - if(self->rhs_expr) { + + assert(self->type == AST_LHS); + lhs_expr = self->value.lhs_expr; + + if(lhs_expr->rhs_expr) { SsaRegister rhs_reg; - rhs_reg = ast_generate_ssa(self->rhs_expr, context); + rhs_reg = ast_generate_ssa(lhs_expr->rhs_expr, context); + /* + Declarations (struct and function declaration) resolves to itself and in that case this expression + is just a compile-time name for the declaration. + Import expression also has no meaning in SSA until it's used. + TODO: Shouldn't lhsexpr that have struct/function declaration as rhs be different ast expression types? + */ + if(self->resolve_data.type == lhs_expr || lhs_expr->rhs_expr->type == AST_IMPORT) + return 0; + throw_if_error(ssa_get_unique_reg(&context->ssa, ®)); + if(reg == rhs_reg) { + amal_log_error("rhs_expr is same as reg.. rhs type: %d", lhs_expr->rhs_expr->type); + } + assert(reg != rhs_reg); throw_if_error(ssa_ins_assign_reg(&context->ssa, reg, rhs_reg)); } else { /* TODO: assign default value to reg depending on LhsExpr type */ } - throw_if_error(amal_mutex_unlock(self->mutex)); return reg; } @@ -270,6 +284,7 @@ in any order. */ static CHECK_RESULT SsaRegister funcdecl_generate_ssa(FunctionDecl *self, SsaCompilerContext *context) { /* TODO: Implement */ + amal_log_debug("SSA funcdecl %p", self); throw_if_error(ssa_ins_func_start(&context->ssa, 0, &self->ssa_func_index)); scope_generate_ssa(&self->body, context); throw_if_error(ssa_ins_func_end(&context->ssa)); @@ -279,6 +294,7 @@ static CHECK_RESULT SsaRegister funcdecl_generate_ssa(FunctionDecl *self, SsaCom static CHECK_RESULT SsaRegister funccall_generate_ssa(Ast *self, SsaCompilerContext *context) { /* TODO: Implement */ FunctionCall *func_call; + FunctionDecl *func_to_call; Ast **ast; Ast **ast_end; SsaRegister reg; @@ -294,9 +310,13 @@ static CHECK_RESULT SsaRegister funccall_generate_ssa(Ast *self, SsaCompilerCont } assert(self->resolve_data.type->rhs_expr->type == AST_FUNCTION_DECL); - ignore_result_int(lhs_generate_ssa(self->resolve_data.type, context)); - amal_log_debug("SSA funccall %.*s, func index: %d", func_call->func.name.size, func_call->func.name.data, self->resolve_data.type->rhs_expr->value.func_decl->ssa_func_index); - throw_if_error(ssa_ins_call(&context->ssa, self->resolve_data.type->rhs_expr->value.func_decl->ssa_func_index, ®)); + func_to_call = self->resolve_data.type->rhs_expr->value.func_decl; + /* + TODO: Implement func reference instead of using 0. Perhaps the best way is to use function declaration pointer value? + then there is no need for mutex locks. + */ + amal_log_debug("SSA funccall %.*s, func index ptr: %p", func_call->func.name.size, func_call->func.name.data, func_to_call); + throw_if_error(ssa_ins_call(&context->ssa, func_to_call, ®)); return reg; } @@ -379,7 +399,7 @@ CHECK_RESULT SsaRegister ast_generate_ssa(Ast *self, SsaCompilerContext *context case AST_STRUCT_FIELD: return structfield_generate_ssa(self->value.struct_field, context); case AST_LHS: - return lhs_generate_ssa(self->value.lhs_expr, context); + return lhsexpr_generate_ssa(self, context); case AST_IMPORT: /* TODO: When @import(...).data syntax is added, implement the generate ssa for it */ return 0; -- cgit v1.2.3