From: mike lowis Date: Thu, 6 May 2021 03:52:24 +0000 (-0400) Subject: function calls are almost working.... X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=65294246a80e2a2b8fc490e8a712ec9914a0d473;p=proto%2Fobnc.git function calls are almost working.... --- diff --git a/cerise/backend/c99/codegen.c b/cerise/backend/c99/codegen.c index 08e6f44..93e1807 100644 --- a/cerise/backend/c99/codegen.c +++ b/cerise/backend/c99/codegen.c @@ -162,7 +162,16 @@ void codegen_startproc(Parser* p, Symbol* proc) else { char* export = (proc->export ? "" : "static"); - printf("\n%svoid %s_%s(void) {\n", export, p->name, proc->name); + printf("\n%s%s %s_%s(", + export, + TypeNames[proc->type->form], + p->name, + proc->name); + for (Symbol* arg = proc->desc; arg; arg = arg->next) + { + printf("%s %s", TypeNames[arg->type->form], arg->name); + } + printf(") {\n"); } } @@ -240,20 +249,21 @@ void codegen_endif(Parser* p, long elsifs, Item* item) void codegen_prepcall(Parser* p, Item* item) { -// printf(" const %s _T%d = %lld;\n", -// TypeNames[item->type->form], p->curr_reg, item->imm.i); -// item->reg = p->curr_reg; -// p->curr_reg++; - printf(" const long _T%d = %s(", p->curr_reg, item->imm.s); - item->reg = p->curr_reg; - p->curr_reg++; + (void)p, (void)item; } -void codegen_call(Parser* p, Item* item) +void codegen_call(Parser* p, Item* item, Item* args) { - (void)p, (void)item; - printf(" const long _T%d = %s(", p->curr_reg, item->imm.s); + while (args) + { + printf("_T%d", args->reg); + if (args->next) + { + printf(", "); + } + args = args->next; + } item->reg = p->curr_reg; p->curr_reg++; printf(");\n"); @@ -262,13 +272,4 @@ void codegen_call(Parser* p, Item* item) void codegen_setarg(Parser* p, Item* item, bool firstarg) { load_var(p, item); - (void)p, (void)item; - if (item->mode == ITEM_CONST) - { - printf("%s%lld", (!firstarg ? ", " : ""), item->imm.i); - } - else - { - printf("%s_T%d", (!firstarg ? ", " : ""), item->reg); - } } diff --git a/cerise/backend/test/codegen.c b/cerise/backend/test/codegen.c index 47a2a62..23caad8 100644 --- a/cerise/backend/test/codegen.c +++ b/cerise/backend/test/codegen.c @@ -106,9 +106,9 @@ void codegen_prepcall(Parser* p, Item* item) (void)p, (void)item; } -void codegen_call(Parser* p, Item* item) +void codegen_call(Parser* p, Item* item, Item* args) { - (void)p, (void)item; + (void)p, (void)item, (void)args; } void codegen_setarg(Parser* p, Item* item, bool firstarg) diff --git a/cerise/backend/x86_64/codegen.c b/cerise/backend/x86_64/codegen.c index 4a4f0ee..7f1dc3a 100644 --- a/cerise/backend/x86_64/codegen.c +++ b/cerise/backend/x86_64/codegen.c @@ -334,9 +334,9 @@ void codegen_prepcall(Parser* p, Item* item) (void)p, (void)item; } -void codegen_call(Parser* p, Item* item) +void codegen_call(Parser* p, Item* item, Item* args) { - (void)p, (void)item; + (void)p, (void)item, (void)args; } void codegen_setarg(Parser* p, Item* item, bool firstarg) diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index b8db0ac..e38d424 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -112,10 +112,12 @@ typedef struct Module { char* alias; } Module; -typedef struct { +typedef struct Item { + struct Item* next; enum { ITEM_CONST, ITEM_VAR, ITEM_MVAR, ITEM_REG } mode; + Symbol* sym; Type* type; int reg; ImmValue imm; @@ -217,9 +219,8 @@ void codegen_store(Parser* p, Item* a, Item* b); void codegen_if(Parser* p, Item* item); void codegen_else(Parser* p, Item* item); void codegen_endif(Parser* p, long elsifs, Item* item); - void codegen_prepcall(Parser* p, Item* item); -void codegen_call(Parser* p, Item* item); +void codegen_call(Parser* p, Item* item, Item* args); void codegen_setarg(Parser* p, Item* item, bool firstarg); /* diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index 37f724b..a46e0fe 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -61,39 +61,42 @@ static void init_item(Item* item, Symbol* sym) *****************************************************************************/ static void expression(Parser* p, Item* item); -RULE(param_list) -{ - (void)item; - int nargs = 0; - Item argdef = {0}; - (void)argdef; - if (!matches(p, ')')) - { - Item arg = {0}; - expression(p, &arg); - // if check_types() - codegen_setarg(p, &arg, true); - // else check_array_type() - nargs++; - - while (!matches(p, ')')) - { - expect(p, ','); - expression(p, &arg); - // if check_types() - codegen_setarg(p, &arg, false); - // else check_array_type() - nargs++; - } - } -} +//RULE(param_list) +//{ +//// (void)item; +//// int nargs = 0; +//// Item argdef = {0}; +//// (void)argdef; +//// if (!matches(p, ')')) +//// { +//// Item arg = {0}; +//// expression(p, &arg); +//// // if check_types() +//// codegen_setarg(p, &arg, true); +//// // else check_array_type() +//// nargs++; +//// +//// while (!matches(p, ')')) +//// { +//// expect(p, ','); +//// expression(p, &arg); +//// // if check_types() +//// codegen_setarg(p, &arg, false); +//// // else check_array_type() +//// nargs++; +//// } +//// } +// +// Symbol* args = item->sym; +// +//} RULE(qualident) { char* name = expect_text(p, IDENT); Symbol* sym = symbol_get(p, -1, name); init_item(item, sym); - if (sym->class == SYM_VAR) + if (sym->class == SYM_VAR || sym->class == SYM_PROC) { item->imm.s = sym->name; } @@ -187,10 +190,32 @@ RULE(factor) designator(p, item); if (accept(p, '(')) { - // TODO: check item is a procedure - codegen_prepcall(p, item); - param_list(p, item); - codegen_call(p, item); +// codegen_prepcall(p, item); +// param_list(p, item); + Symbol* proc = symbol_get(p, SYM_PROC, item->imm.s); + Symbol* args = proc->desc; + Item arglist = {0}; + Item** currarg = &(arglist.next); + while (args && !matches(p, ')')) + { + Item argdef = { .type = args->type }; + Item* argval = calloc(1, sizeof(Item)); + expression(p, argval); + check_types(p, &argdef, argval); + codegen_setarg(p, argval, (arglist.next == 0)); + *currarg = argval; + currarg = &(argval->next); + args = args->next; + } + if (args) + { + error(p, "too few arguments to function '%s'", proc->name); + } + else if (!matches(p, ')')) + { + error(p, "too many arguments to function '%s'", proc->name); + } + codegen_call(p, item, arglist.next); expect(p, ')'); } break; @@ -462,6 +487,10 @@ RULE(proc_decl) proc->nargs++; Symbol* arg = symbol_addfield(p, proc, name, SYM_VAR, false); arg->type = item->type; + if (!matches(p, ')')) + { + expect(p, ','); + } } expect(p, ')'); if (accept(p, ':')) diff --git a/cerise/src/sym.c b/cerise/src/sym.c index c127493..6f1e15c 100644 --- a/cerise/src/sym.c +++ b/cerise/src/sym.c @@ -4,7 +4,7 @@ static int sym_matches(Parser* p, int class, char* name, Symbol* sym) { // printf(" %s == %s\n", name, sym->name); - if (sym->name && !strcmp(sym->name, name)) + if (name && sym->name && !strcmp(sym->name, name)) { if (class >= 0 && (int)sym->class != class) { @@ -22,6 +22,10 @@ static int sym_matches(Parser* p, int class, char* name, Symbol* sym) error(p, "'%s' is not a type", name); break; + case SYM_PROC: + error(p, "'%s' is not a procedure", name); + break; + default: assert(!"unknown identifier type"); break; @@ -96,7 +100,6 @@ Symbol* symbol_addfield(Parser* p, Symbol* parent, char* name, int class, bool e return sym; } - Symbol* symbol_get(Parser* p, int class, char* name) { for (Symbol* scope = p->scope; scope; scope = scope->next) @@ -140,7 +143,4 @@ void symbol_closescope(Parser* p) *****************************************************************************/ #ifdef CERISE_TESTS #include "atf.h" - - - #endif \ No newline at end of file diff --git a/cerise/src/type_checks.c b/cerise/src/type_checks.c index 7301781..a50130e 100644 --- a/cerise/src/type_checks.c +++ b/cerise/src/type_checks.c @@ -68,6 +68,7 @@ void check_bools(Parser* p, Item* a, Item* b) void check_types(Parser* p, Item* a, Item* b) { +// printf("%d %d\n", a->type->form, b->type->form); if (a->type->form == FORM_REAL) { check_reals(p, a, b); diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index 77d2f1a..115f3c3 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -21,7 +21,7 @@ var b : Int c : Int -procedure Foo*(e : Int) : Int +procedure Foo*(e : Int, z : Int) : Int const FOO = 2 type foo = Int var z : Int @@ -93,5 +93,5 @@ begin # c = 3; # end - c = c(1,a,3); + c = Foo(1,2); end