From 0f26e1d204d3a3026ca3edfc4c6bd9638b2632e7 Mon Sep 17 00:00:00 2001
From: dec05eba <dec05eba@protonmail.com>
Date: Wed, 31 Jul 2019 22:29:24 +0200
Subject: Add nullable, add bytecode documentation

---
 src/ssa/ssa.c | 27 ++++++++++++++-------------
 1 file changed, 14 insertions(+), 13 deletions(-)

(limited to 'src/ssa')

diff --git a/src/ssa/ssa.c b/src/ssa/ssa.c
index b95cd10..405d8e4 100644
--- a/src/ssa/ssa.c
+++ b/src/ssa/ssa.c
@@ -342,22 +342,23 @@ static CHECK_RESULT SsaRegister lhsexpr_generate_ssa(Ast *self, SsaCompilerConte
     if(lhs_expr->is_extern)
         return lhsexpr_extern_generate_ssa(lhs_expr, context);
 
-    if(lhs_expr->rhs_expr) {
+    if(is_not_null(lhs_expr->rhs_expr)) {
+        Ast *rhs_expr = nullable_unwrap(lhs_expr->rhs_expr);
         SsaRegister rhs_reg;
-        rhs_reg = ast_generate_ssa(lhs_expr->rhs_expr, context);
+        rhs_reg = ast_generate_ssa(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) {
+        if(self->resolve_data.type == lhs_expr || rhs_expr->type == AST_IMPORT) {
             /*assert(bool_false);*/
             return 0;
         }
         throw_if_error(ssa_get_unique_reg(context->ssa, &reg));
         if(reg == rhs_reg) {
-            amal_log_error("rhs_expr is same as reg.. rhs type: %d", lhs_expr->rhs_expr->type);
+            amal_log_error("rhs_expr is same as reg.. rhs type: %d", rhs_expr->type);
         }
         assert(reg != rhs_reg);
         throw_if_error(ssa_ins_assign_reg(context->ssa, reg, rhs_reg));
@@ -432,7 +433,7 @@ static CHECK_RESULT SsaRegister funccall_generate_ssa(Ast *self, SsaCompilerCont
         throw_if_error(ssa_ins_push(context->ssa, arg_reg));
     }
 
-    assert((self->resolve_data.type->rhs_expr && self->resolve_data.type->rhs_expr->type == AST_FUNCTION_DECL) ||
+    assert((is_not_null(self->resolve_data.type->rhs_expr) && nullable_unwrap(self->resolve_data.type->rhs_expr)->type == AST_FUNCTION_DECL) ||
             self->resolve_data.type->type.type == VARIABLE_TYPE_SIGNATURE);
     if(self->resolve_data.type->is_extern) {
         amal_log_error("TODO: Implement extern function call (extern function %.*s was called)", func_call->func.name.size, func_call->func.name.data);
@@ -440,7 +441,8 @@ static CHECK_RESULT SsaRegister funccall_generate_ssa(Ast *self, SsaCompilerCont
         assert(bool_false && "TODO: Implement extern function call!");
     } else {
         FunctionDecl *func_to_call;
-        func_to_call = self->resolve_data.type->rhs_expr->value.func_decl;
+        /* rhs wont be null here because only extern variable can't have rhs */
+        func_to_call = nullable_unwrap(self->resolve_data.type->rhs_expr)->value.func_decl;
         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, &reg));
     }
@@ -497,19 +499,18 @@ static SsaInstruction binop_type_to_ssa_type(BinopType binop_type, amal_default_
 }
 
 static CHECK_RESULT SsaRegister binop_generate_ssa(Binop *self, SsaCompilerContext *context) {
-    SsaRegister lhs_reg;
-    SsaRegister rhs_reg;
     SsaRegister reg;
 
     /*
-    const std = @import("std.amal");
-    std.printf
+        Syntax example for binop dot + func_decl expr
+        const std = @import("std.amal");
+        std.printf
     */
-    if(self->type == BINOP_DOT && self->rhs->resolve_data.type->rhs_expr->type == AST_FUNCTION_DECL) {
+    if(self->type == BINOP_DOT && resolved_type_is_func_decl(self->rhs)) {
         reg = ast_generate_ssa(self->rhs, context);
     } else {
-        lhs_reg = ast_generate_ssa(self->lhs, context);
-        rhs_reg = ast_generate_ssa(self->rhs, context);
+        const SsaRegister lhs_reg = ast_generate_ssa(self->lhs, context);
+        const SsaRegister rhs_reg = ast_generate_ssa(self->rhs, context);
         throw_if_error(ssa_ins_binop(context->ssa, binop_type_to_ssa_type(self->type, (amal_default_type*)self->lhs->resolve_data.type), lhs_reg, rhs_reg, &reg));
     }
     return reg;
-- 
cgit v1.2.3-70-g09d2