From d4ca9de33906972fa06bd2b7e38cbc2b4d3574c2 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 14 Mar 2019 21:24:44 +0100 Subject: Add ssa string --- src/ssa/ssa.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 8 deletions(-) (limited to 'src/ssa') diff --git a/src/ssa/ssa.c b/src/ssa/ssa.c index 1b5ed3d..b8aa8b0 100644 --- a/src/ssa/ssa.c +++ b/src/ssa/ssa.c @@ -1,6 +1,7 @@ #include "../../include/ssa/ssa.h" #include "../../include/std/mem.h" #include "../../include/std/log.h" +#include "../../include/std/hash.h" #include "../../include/ast.h" #include @@ -47,7 +48,9 @@ SsaNumber create_ssa_float(f64 value) { int ssa_init(Ssa *self, ScopedAllocator *allocator) { return_if_error(buffer_init(&self->instructions, allocator)); return_if_error(hash_map_init(&self->intermediates, allocator, sizeof(SsaIntermediateIndex), compare_number, hash_number)); + return_if_error(hash_map_init(&self->strings, allocator, sizeof(SsaStringIndex), hash_compare_string, amal_hash_string)); self->intermediate_counter = 0; + self->string_counter = 0; self->reg_counter = 0; self->func_counter = 0; return 0; @@ -91,6 +94,24 @@ static CHECK_RESULT int ssa_try_add_intermediate(Ssa *self, SsaNumber number, Ss return hash_map_insert(&self->intermediates, key, result_index); } +static CHECK_RESULT int ssa_try_add_string(Ssa *self, BufferView str, SsaStringIndex *result_index) { + bool exists; + assert(result_index); + + exists = hash_map_get(&self->strings, str, result_index); + if(exists) + return 0; + + /* Overflow */ + if(self->string_counter + 1 < self->string_counter) + return -1; + + *result_index = self->string_counter; + ++self->string_counter; + amal_log_debug("s%u = \"%.*s\"", *result_index, str.size, str.data); + return hash_map_insert(&self->strings, str, result_index); +} + static CHECK_RESULT int ssa_add_ins_form1(Ssa *self, SsaInstructionType ins_type, SsaRegister lhs, u16 rhs) { usize index; index = self->instructions.size; @@ -128,6 +149,13 @@ int ssa_ins_assign_inter(Ssa *self, SsaRegister dest, SsaNumber number) { return ssa_add_ins_form1(self, SSA_ASSIGN_INTER, dest, index); } +int ssa_ins_assign_string(Ssa *self, SsaRegister dest, BufferView str) { + SsaStringIndex index; + return_if_error(ssa_try_add_string(self, str, &index)); + amal_log_debug("r%u = s%u", dest, index); + return ssa_add_ins_form1(self, SSA_ASSIGN_STRING, dest, index); +} + 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); @@ -208,30 +236,43 @@ static CHECK_RESULT SsaRegister number_generate_ssa(Number *self, SsaCompilerCon static CHECK_RESULT SsaRegister funcdecl_generate_ssa(FunctionDecl *self, SsaCompilerContext *context) { /* TODO: Implement */ + 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)); return 0; } static CHECK_RESULT SsaRegister funccall_generate_ssa(FunctionCall *self, SsaCompilerContext *context) { /* TODO: Implement */ - (void)self; - (void)context; - return 0; + Ast *ast; + Ast *ast_end; + SsaRegister reg; + + ast = buffer_start(&self->args); + ast_end = buffer_end(&self->args); + for(; ast != ast_end; ++ast) { + SsaRegister arg_reg; + arg_reg = ast_generate_ssa(ast, context); + throw_if_error(ssa_ins_push(&context->ssa, arg_reg)); + } + /* TODO: Use real func index */ + throw_if_error(ssa_ins_call(&context->ssa, 0, ®)); + return reg; } static CHECK_RESULT SsaRegister lhs_generate_ssa(LhsExpr *self, SsaCompilerContext *context) { /* TODO: Implement */ SsaRegister rhs_reg; - amal_log_debug("lhs_generate_ssa"); rhs_reg = ast_generate_ssa(&self->rhs_expr, context); + /* TODO: Is this correct? */ return rhs_reg; } static CHECK_RESULT SsaRegister string_generate_ssa(String *self, SsaCompilerContext *context) { - /* TODO: Implement */ - (void)self; - (void)context; - return 0; + SsaRegister reg; + throw_if_error(ssa_get_unique_reg(&context->ssa, ®)); + throw_if_error(ssa_ins_assign_string(&context->ssa, reg, self->str)); + return reg; } static CHECK_RESULT SsaRegister variable_generate_ssa(Variable *self, SsaCompilerContext *context) { @@ -269,6 +310,7 @@ static CHECK_RESULT SsaRegister binop_generate_ssa(Binop *self, SsaCompilerConte } CHECK_RESULT SsaRegister ast_generate_ssa(Ast *self, SsaCompilerContext *context) { + assert(self->resolve_status == AST_RESOLVED); switch(self->type) { case AST_NONE: return 0; -- cgit v1.2.3