From 4ce39e4fc1de9536de7dedfe91b6dee38dec76ae Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Thu, 1 Oct 2026 01:04:30 +0200 Subject: WIP sym + cgen --- src/prc1/fn.c | 115 +++++++++------------------------------------------------- 1 file changed, 17 insertions(+), 98 deletions(-) (limited to 'src/prc1/fn.c') diff --git a/src/prc1/fn.c b/src/prc1/fn.c index 03aa11e..affa4be 100644 --- a/src/prc1/fn.c +++ b/src/prc1/fn.c @@ -9,6 +9,7 @@ #include "pr.h" #include "stmt.h" #include "storage.h" +#include "sym.h" #include "td.h" #include "ws.h" #include @@ -20,11 +21,8 @@ static int prvfn(const struct lex *l, struct prv *p); static const struct seq fnseq[] = { - {(struct step[]) - {{ID, "public"}, {ID, "function"}, {ID}, {0}}, .fn = pubfn, - .end = fn_end}, - {(struct step[]) - {{ID, "function"}, {ID}, {0}}, .fn = prvfn, .end = fn_end}, + {(struct step[]){{ID, "public"}, {ID, "function"}, {ID}, {0}}, .fn = pubfn}, + {(struct step[]){{ID, "function"}, {ID}, {0}}, .fn = prvfn}, {0} }; @@ -92,8 +90,8 @@ static int pushfn(const struct tk *tk, const struct lex *lex, struct prv *p, if (div_fn(lex, p)) return -1; - dbg("adding %s function %s\n", l == STATIC ? "private" : "public", tk->s); - return 1; + dbg("add %s function %s\n", l == STATIC ? "private" : "public", tk->s); + return 0; } static int pubfn(const struct lex *l, struct prv *p) @@ -123,90 +121,6 @@ const struct stentry *fn_var(const struct fn *fn, const struct tk *tk) return NULL; } -int fn_cgen(const struct fn *fn, struct cgen *c) -{ - const struct pr *pr = fn->pr; - const struct param *ret = pr->ret; - const struct type *rt = NULL; - const char *s = NULL; - - if (fn->gl && gl_cgen(fn->gl, c)) - return -1; - - switch (fn->linkage) - { - case STATIC: - printf("function "); - break; - case EXTERNAL: - printf("export function "); - break; - } - - if (ret) - { - rt = ret->entry->t; - s = ret->entry->tk->s; - printf("%s ", cgen_abity(ret->entry->t)); - } - - printf("$%s(", fn->tk->s); - - for (size_t i = 0; i < pr->nparams; i++) - { - const struct param *p = &pr->params[i]; - - printf(" %s", cgen_abity(p->entry->t)); - printf(" %%__param_%s ", p->tk->s); - - if (i + 1 < pr->nparams) - fputs(", ", stdout); - } - - puts(") {\n@start"); - - for (size_t i = 0; i < pr->nparams; i++) - { - const struct param *p = &pr->params[i]; - const struct type *t = p->entry->t; - const char *s = p->tk->s; - - printf("%%%s_ =l alloc%zu %zu\n", s, t->align, t->sz); - printf("store%s %%__param_%s, %%%s_\n", cgen_sz(t->sz), s, s); - } - - if (ret) - printf("%%%s_ =l alloc%zu %zu\n", s, rt->align, rt->sz); - - if (fn->ws && ws_cgen(fn->ws, c)) - return -1; - - for (const struct tmp *tmp = fn->tmps; tmp; tmp = tmp->next) - { - const struct stentry *e = &tmp->e; - const struct type *t = tmp->e.t; - - printf("%%%s_ =l alloc%zu %zu\n", e->tk->s, t->align, t->sz); - } - - for (size_t i = 0; i < fn->nstmts; i++) - if (stmt_cgen(&fn->stmts[i], c)) - return -1; - - if (ret) - { - const char *t = cgen_tmp(rt->sz); - - printf("%%%s =%s load%s %%%s_\n", t, cgen_sz(rt->sz), cgen_load(rt), s); - printf("ret %%%s", t); - } - else - fputs("ret", stdout); - - puts("\n}"); - return 0; -} - void fn_cgen_free(struct fn_cgen *c) { free(c); @@ -229,6 +143,14 @@ void fn_free(struct fn *fn) t = next; } + for (struct sym *s = fn->syms; s;) + { + struct sym *next = s->next; + + sym_free(s); + s = next; + } + storage_free(fn->lk); storage_free(fn->ws); storage_free(fn->gl); @@ -253,14 +175,11 @@ int fn(const struct lex *l, struct prv *p) { struct pos init = { - .seq = fnseq, - .stseq = fnseq, - .step = fnseq->steps + .name = __func__, + .seq = fnseq }; - if (lex_eof(l, p->stk)) - return 0; - else if (lex_eof(l, p->stk + 1)) + if (lex_eof(l, p->stk + 1)) { errloc(p->stk, "incomplete function statement"); return -1; @@ -269,5 +188,5 @@ int fn(const struct lex *l, struct prv *p) return -1; p->stk = p->tk; - return 1; + return 0; } -- cgit v1.2.3