]> git.mdlowis.com Git - proto/obnc.git/commitdiff
added function call syntax and ssa generation. Backend is segfaulting currently
authorMichael D. Lowis <mike@mdlowis.com>
Tue, 11 Oct 2022 02:25:33 +0000 (22:25 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Tue, 11 Oct 2022 02:25:33 +0000 (22:25 -0400)
cerise/backend/ssa/codegen.c
cerise/inc/cerise.h
cerise/src/grammar.c
cerise/src/ssa.c
cerise/tests/Module.m

index 6fa249175ecb290b8ab50f2d6c29b88b3c8e2d34..75d227cfe1bb31f8930567d92b0407e191af09f7 100644 (file)
@@ -390,6 +390,32 @@ void print_op(Parser* p, SsaNode* expr)
             {
                 printf("    br label %%L%ld\n", SSA_LBLK(expr)->id);
             }
+            else if (expr->code == CALL)
+            {
+                print_ident(p, &(SSA_VAR(expr)));
+
+//                printf("    ret ");
+//                emit_type(SSA_TYPE(expr));
+//                if (SSA_TYPE(expr)->form != FORM_VOID)
+//                {
+//                    printf(" ");
+//                    if (expr->mode == MODE_CTRL)
+//                    {
+//                        print_ident(p, &(SSA_VAR(expr)));
+//                    }
+//                    else
+//                    {
+//                        print_const(SSA_TYPE(expr), &(expr->value));
+//                    }
+//                }
+//                puts("");
+
+
+            }
+            else
+            {
+                assert(!"not implemented");
+            }
             break;
 
         case MODE_UNOP:
index 62ac170d95e495c6975dc494611b47cbd57bb932..914597881fcf9d8920bb8d3cf54c3b487fc51652 100644 (file)
@@ -56,6 +56,7 @@ typedef enum {
     WHILE,
     COUNT,
     CALL,
+    ARGS,
     LOAD,
     STORE,
 
@@ -120,12 +121,17 @@ typedef union {
 } SsaConst;
 
 struct SsaBlock;
+struct SsaNode;
 
 typedef union {
     SsaVar var;
     SsaConst val;
     struct SsaBlock* block;
     char* str;
+    struct {
+        struct SsaNode** v;
+        size_t nv;
+    } args;
 } SsaValue;
 
 //typedef struct SsaNode {
@@ -324,8 +330,8 @@ SsaNode* ssa_if(Parser* p, SsaNode* cond, SsaBlock* br1, SsaBlock* br2);
 SsaNode* ssa_return(Parser* p, SsaNode* expr);
 SsaNode* ssa_branch(Parser* p, SsaBlock* br);
 
-SsaNode* ssa_call(SsaNode* func);
-void ssa_call_add(SsaBlock* call, SsaNode* arg);
+SsaNode* ssa_call(Parser* p, SsaNode* func);
+void ssa_call_add(Parser* p, SsaNode* call, SsaNode* arg);
 
 /* Backend Code Generation and Base Type Definitions
  *****************************************************************************/
index cf680eb12412901fd43766f5c715f699fed098fa..8948a4274cf917c1804eae7d6c9ec927fb8b80bf 100644 (file)
@@ -171,44 +171,45 @@ static SsaNode* factor(Parser* p)
 
         case IDENT:
             expr = designator(p);
-//            if (accept(p, '('))
-//            {
-//                expr = ssa_call(expr);
-//                if (expr->type->form != FORM_PROC)
-//                {
-//                    error(p, "attempting to call a non-procedural value");
-//                }
-//
-//                Field* args = expr->type->fields;
-//                while (args && !matches(p, ')'))
-//                {
-//                    SsaNode* val = expression(p);
-//                    check_type(p, args->type, val);
-//                    ssa_call_add(expr, val);
-//                    args = args->next;
-//                    if (args)
-//                    {
-//                        expect(p, ',');
-//                    }
-//                }
-//                if (args)
-//                {
-//                    error(p, "too few arguments to function");
-//                }
-//                else if (!matches(p, ')'))
-//                {
-//                    bool comma = accept(p, ',');
-//                    if (comma && matches(p, ')'))
-//                    {
-//                        error(p, "trailing comma in argument list");
-//                    }
-//                    else
-//                    {
-//                        error(p, "too many arguments to function");
-//                    }
-//                }
-//                expect(p, ')');
-//            }
+            if (accept(p, '('))
+            {
+                SsaNode* call = ssa_call(p, expr);
+                if (expr->type->form != FORM_PROC)
+                {
+                    error(p, "attempting to call a non-procedural value");
+                }
+
+                Field* args = expr->type->fields;
+                while (args && !matches(p, ')'))
+                {
+                    SsaNode* val = expression(p);
+                    check_type(p, args->type, val);
+                    ssa_call_add(p, call, val);
+                    args = args->next;
+                    if (args)
+                    {
+                        expect(p, ',');
+                    }
+                }
+                if (args)
+                {
+                    error(p, "too few arguments to function");
+                }
+                else if (!matches(p, ')'))
+                {
+                    bool comma = accept(p, ',');
+                    if (comma && matches(p, ')'))
+                    {
+                        error(p, "trailing comma in argument list");
+                    }
+                    else
+                    {
+                        error(p, "too many arguments to function");
+                    }
+                }
+                expect(p, ')');
+                expr = call;
+            }
             break;
 
         default:
index 2dde0c0e6f14cb3506e4c873220adef72c05be44..d07e84e353328c351778d3a4675238f534f8048e 100644 (file)
@@ -335,8 +335,22 @@ SsaNode* ssa_branch(Parser* p, SsaBlock* br)
     return node;
 }
 
-SsaNode* ssa_call(SsaNode* func);
-void ssa_call_add(SsaBlock* call, SsaNode* arg);
+SsaNode* ssa_call(Parser* p, SsaNode* func)
+{
+    (void)p;
+    SsaNode* call = ssa_node(CALL, MODE_CTRL);
+    call->type = func->type->base;
+    call->left = func;
+    return call;
+}
+
+void ssa_call_add(Parser* p, SsaNode* call, SsaNode* arg)
+{
+    loadmem(p, arg);
+    call->value.args.nv++;
+    call->value.args.v = realloc(call->value.args.v, call->value.args.nv);
+    call->value.args.v[call->value.args.nv - 1] = arg;
+}
 
 static SsaNode* binop(Parser* p, int op, SsaNode* left, SsaNode* right)
 {
index 9dd680f3fac77269010eea6be7189f7c57dc3c93..7db0f9827314d80a6688d20b6ac179d3d7d4030a 100644 (file)
@@ -182,9 +182,17 @@ begin
     vRec1.b.c = vRec1.b.c + vRec1.b.c;
 end
 
-procedure TestFunctionCall()
+procedure Sum(a: Int, b : Int) : Int
 begin
-#    TestFunctionCall();
+    return a + b;
+end
+
+
+procedure TestFunctionCalls()
+begin
+    TestReturnVoid();
+    vInt = TestReturnIntLiteral();
+    vInt = Sum(1,2);
 end
 
 #begin