aboutsummaryrefslogtreecommitdiff
path: root/src/ssa
diff options
context:
space:
mode:
Diffstat (limited to 'src/ssa')
-rw-r--r--src/ssa/ssa.c67
1 files changed, 43 insertions, 24 deletions
diff --git a/src/ssa/ssa.c b/src/ssa/ssa.c
index e55f687..3fffb63 100644
--- a/src/ssa/ssa.c
+++ b/src/ssa/ssa.c
@@ -2,6 +2,7 @@
#include "../../include/std/mem.h"
#include "../../include/std/log.h"
#include "../../include/std/hash.h"
+#include "../../include/std/thread.h"
#include "../../include/ast.h"
#include <assert.h>
@@ -246,6 +247,27 @@ static CHECK_RESULT SsaRegister number_generate_ssa(Number *self, SsaCompilerCon
return reg;
}
+
+static CHECK_RESULT SsaRegister lhs_generate_ssa(LhsExpr *self, SsaCompilerContext *context) {
+ /* TODO: Implement */
+ SsaRegister reg;
+ throw_if_error(amal_mutex_lock(self->mutex, "lhs_generate_ssa"));
+ throw_if_error(ssa_get_unique_reg(&context->ssa, &reg));
+ if(self->rhs_expr) {
+ SsaRegister rhs_reg;
+ rhs_reg = ast_generate_ssa(self->rhs_expr, context);
+ 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;
+}
+
+/*
+TODO: Each function declaration should be in separate SSA instances so ast can be converted into ssa
+in any order.
+*/
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));
@@ -254,21 +276,27 @@ static CHECK_RESULT SsaRegister funcdecl_generate_ssa(FunctionDecl *self, SsaCom
return 0;
}
-static CHECK_RESULT SsaRegister funccall_generate_ssa(FunctionCall *self, SsaCompilerContext *context) {
+static CHECK_RESULT SsaRegister funccall_generate_ssa(Ast *self, SsaCompilerContext *context) {
/* TODO: Implement */
+ FunctionCall *func_call;
Ast **ast;
Ast **ast_end;
SsaRegister reg;
- ast = buffer_start(&self->args);
- ast_end = buffer_end(&self->args);
+ assert(self->type == AST_FUNCTION_CALL);
+ func_call = self->value.func_call;
+ ast = buffer_start(&func_call->args);
+ ast_end = buffer_end(&func_call->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, &reg));
+
+ 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, &reg));
return reg;
}
@@ -285,20 +313,6 @@ static CHECK_RESULT SsaRegister structfield_generate_ssa(StructField *self, SsaC
return 0;
}
-static CHECK_RESULT SsaRegister lhs_generate_ssa(LhsExpr *self, SsaCompilerContext *context) {
- /* TODO: Implement */
- SsaRegister reg;
- throw_if_error(ssa_get_unique_reg(&context->ssa, &reg));
- if(self->rhs_expr) {
- SsaRegister rhs_reg;
- rhs_reg = ast_generate_ssa(self->rhs_expr, context);
- throw_if_error(ssa_ins_assign_reg(&context->ssa, reg, rhs_reg));
- } else {
- /* TODO: assign default value to reg depending on LhsExpr type */
- }
- return reg;
-}
-
static CHECK_RESULT SsaRegister string_generate_ssa(String *self, SsaCompilerContext *context) {
SsaRegister reg;
throw_if_error(ssa_get_unique_reg(&context->ssa, &reg));
@@ -324,7 +338,7 @@ static SsaInstructionType binop_type_to_ssa_type(BinopType binop_type) {
case BINOP_DIV:
return SSA_DIV;
case BINOP_DOT:
- assert(bool_false && "TODO: Implement dot access");
+ assert(bool_false && "Binop dot not valid for arithmetic operation and requires special functionality");
return 0;
}
return 0;
@@ -334,9 +348,14 @@ static CHECK_RESULT SsaRegister binop_generate_ssa(Binop *self, SsaCompilerConte
SsaRegister lhs_reg;
SsaRegister rhs_reg;
SsaRegister reg;
- lhs_reg = ast_generate_ssa(self->lhs, context);
- rhs_reg = ast_generate_ssa(self->rhs, context);
- throw_if_error(ssa_ins_binop(&context->ssa, binop_type_to_ssa_type(self->type), lhs_reg, rhs_reg, &reg));
+
+ if(self->type == BINOP_DOT && self->rhs->resolve_data.type->rhs_expr->type == AST_FUNCTION_DECL) {
+ reg = ast_generate_ssa(self->rhs, context);
+ } else {
+ lhs_reg = ast_generate_ssa(self->lhs, context);
+ rhs_reg = ast_generate_ssa(self->rhs, context);
+ throw_if_error(ssa_ins_binop(&context->ssa, binop_type_to_ssa_type(self->type), lhs_reg, rhs_reg, &reg));
+ }
return reg;
}
@@ -354,7 +373,7 @@ CHECK_RESULT SsaRegister ast_generate_ssa(Ast *self, SsaCompilerContext *context
case AST_FUNCTION_DECL:
return funcdecl_generate_ssa(self->value.func_decl, context);
case AST_FUNCTION_CALL:
- return funccall_generate_ssa(self->value.func_call, context);
+ return funccall_generate_ssa(self, context);
case AST_STRUCT_DECL:
return structdecl_generate_ssa(self->value.struct_decl, context);
case AST_STRUCT_FIELD: