From: Michael D. Lowis Date: Wed, 12 Oct 2022 01:09:38 +0000 (-0400) Subject: fixed function call code generation X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=a11417dd09a82c94671d0f2646e51f2fd82bf41f;p=proto%2Fobnc.git fixed function call code generation --- diff --git a/cerise/backend/ssa/codegen.c b/cerise/backend/ssa/codegen.c index 75d227c..a6de7c5 100644 --- a/cerise/backend/ssa/codegen.c +++ b/cerise/backend/ssa/codegen.c @@ -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)); diff --git a/cerise/build.sh b/cerise/build.sh index 8a849ef..250a467 100755 --- a/cerise/build.sh +++ b/cerise/build.sh @@ -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 diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index 9145978..952740f 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -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; diff --git a/cerise/src/ssa.c b/cerise/src/ssa.c index d07e84e..ba588a1 100644 --- a/cerise/src/ssa.c +++ b/cerise/src/ssa.c @@ -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) diff --git a/cerise/src/sym.c b/cerise/src/sym.c index 75c1cc7..ca7764e 100644 --- a/cerise/src/sym.c +++ b/cerise/src/sym.c @@ -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]); } diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index 7db0f98..91333c9 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -187,7 +187,6 @@ begin return a + b; end - procedure TestFunctionCalls() begin TestReturnVoid();