]> git.mdlowis.com Git - proto/obnc.git/commitdiff
fixed function call code generation
authorMichael D. Lowis <mike@mdlowis.com>
Wed, 12 Oct 2022 01:09:38 +0000 (21:09 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Wed, 12 Oct 2022 01:09:38 +0000 (21:09 -0400)
cerise/backend/ssa/codegen.c
cerise/build.sh
cerise/inc/cerise.h
cerise/src/ssa.c
cerise/src/sym.c
cerise/tests/Module.m

index 75d227cfe1bb31f8930567d92b0407e191af09f7..a6de7c548deccb74c73f133748758620490a46af 100644 (file)
@@ -181,6 +181,8 @@ static void topsort(Bitset* set, SsaBlock** sorted, SsaBlock* block)
 
 static void print_ident(Parser* p, SsaVar* var)
 {
+//    printf("%ld\n", var->symid);
+//    fflush(stdout);
     Symbol* s = symbol_getbyid(p, var->symid);
     assert(s);
     assert(s->name);
@@ -392,7 +394,33 @@ void print_op(Parser* p, SsaNode* expr)
             }
             else if (expr->code == CALL)
             {
-                print_ident(p, &(SSA_VAR(expr)));
+                printf("    ");
+                if (SSA_TYPE(expr) != &VoidType)
+                {
+                    print_ident(p, &(SSA_VAR(expr)));
+                    printf(" = ");
+                }
+                printf("call ");
+                emit_type(SSA_TYPE(expr));
+                printf(" ");
+                print_ident(p, &(SSA_LVAR(expr)));
+                printf("(");
+                size_t nargs = expr->right->value.args.nv;
+                SsaNode** args = expr->right->value.args.v;
+                for (size_t i = 0; args && i < nargs; i++)
+                {
+                    assert(args[i]);
+                    emit_type(SSA_TYPE(args[i]));
+//                    printf(" ");
+                    print_oparg(p, args[i]);
+                    if (i+1 < nargs)
+                    {
+                        printf(", ");
+                    }
+                }
+                printf(")");
+                puts("");
+fflush(stdout);
 
 //                printf("    ret ");
 //                emit_type(SSA_TYPE(expr));
index 8a849ef4f98119108754f25e8e276d3b7e7e1e19..250a46751897524c3a36fb2456dbe5f2a6f888bc 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/sh
 
-CCCMD="gcc -g -Wall -Wextra -Werror --std=c99 -Iinc/ -fsanitize=undefined"
+CCCMD="clang -g -Wall -Wextra -Werror --std=c99 -Iinc/ -fsanitize=undefined,memory"
 TEST_BACKEND=backend/test
 
 # Update tag database and print todos
@@ -8,11 +8,12 @@ ctags -R &
 grep -rh TODO src/*.* backend/*/*.* | sed -E -e 's/ *\/\/ *//' -e 's/ *\/\* *(.*) *\*\//\1/'
 
 # Now build and test it
-   $CCCMD -D CERISE_TESTS -o cerisec-test src/*.c backend/test/*.c \
-&& $CCCMD -o cerisec src/*.c "backend/ssa"/*.c \
-&& ./cerisec-test \
-&& (./cerisec tests/Module.m | tee test.l) \
-&& llc test.l
+   $CCCMD -o cerisec src/*.c "backend/ssa"/*.c \
+&& ./cerisec tests/Module.m
+#&& $CCCMD -D CERISE_TESTS -o cerisec-test src/*.c backend/test/*.c \
+#&& ./cerisec-test \
+#&& (./cerisec tests/Module.m | tee test.l) \
+#&& llc test.l
 
 rm -f Module
 
index 914597881fcf9d8920bb8d3cf54c3b487fc51652..952740fd50ea669dccdd868339f3c07be3cc8537 100644 (file)
@@ -134,21 +134,6 @@ typedef union {
     } args;
 } SsaValue;
 
-//typedef struct SsaNode {
-//    struct SsaNode* next;
-//    unsigned int code : 25;
-//    unsigned int mode : 4;
-//    unsigned int loaded : 1;
-//    unsigned int lconst : 1;
-//    unsigned int rconst : 1;
-//    Type* ret_type;
-//    Type* left_type;
-//    Type* right_type;
-//    SsaVar dest;
-//    SsaValue left;
-//    SsaValue right;
-//} SsaNode;
-
 typedef struct SsaNode {
     struct SsaNode* next;
     unsigned int code : 25;
index d07e84e353328c351778d3a4675238f534f8048e..ba588a1811ab655321c54cb0b87bbe79b25864f3 100644 (file)
@@ -341,15 +341,18 @@ SsaNode* ssa_call(Parser* p, SsaNode* func)
     SsaNode* call = ssa_node(CALL, MODE_CTRL);
     call->type = func->type->base;
     call->left = func;
+    call->right = ssa_node(ARGS, MODE_VAR);
     return call;
 }
 
 void ssa_call_add(Parser* p, SsaNode* call, SsaNode* arg)
 {
+    SsaNode* arglist = call->right;
     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;
+    arglist->value.args.nv++;
+    arglist->value.args.v = realloc(arglist->value.args.v, arglist->value.args.nv * sizeof(SsaNode*));
+    assert(arglist->value.args.v);
+    arglist->value.args.v[arglist->value.args.nv - 1] = arg;
 }
 
 static SsaNode* binop(Parser* p, int op, SsaNode* left, SsaNode* right)
index 75c1cc7d3ae9535c9fa7d657c77f13582d1b9491..ca7764ea2c84438820262352f91f9cd9a040b2a5 100644 (file)
@@ -130,6 +130,7 @@ size_t symbol_getid(Parser* p, size_t module, char* name, int class)
 
 Symbol* symbol_getbyid(Parser* p, size_t id)
 {
+    assert(id < p->nsyms);
     return &(p->syms[id]);
 }
 
index 7db0f9827314d80a6688d20b6ac179d3d7d4030a..91333c985a0e68901be05cc2b6c5fac2e1de76d1 100644 (file)
@@ -187,7 +187,6 @@ begin
     return a + b;
 end
 
-
 procedure TestFunctionCalls()
 begin
     TestReturnVoid();