From 2b0d4ead061a39da9dbd705c18c9955549c4d405 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Thu, 1 Oct 2026 01:04:30 +0200 Subject: WIP sym --- src/prc1/td.c | 178 ++++++++++++++++++++++++++-------------------------------- 1 file changed, 81 insertions(+), 97 deletions(-) (limited to 'src/prc1/td.c') diff --git a/src/prc1/td.c b/src/prc1/td.c index 06572e7..af88af8 100644 --- a/src/prc1/td.c +++ b/src/prc1/td.c @@ -18,7 +18,6 @@ static int cid(const struct lex *, struct prv *); static int cnum(const struct lex *, struct prv *); static int end(const struct lex *, struct prv *); -static int endp(const struct lex *, struct prv *); static int u(const struct lex *, struct prv *); static int s(const struct lex *, struct prv *); static int c(const struct lex *, struct prv *); @@ -34,19 +33,10 @@ static const struct seq seqs[] = {(struct step[]){{ID}, {ID, "union"}, {0}}, .fn = u}, {(struct step[]){{ID}, {ID, "struct"}, {0}}, .fn = s}, {(struct step[]){{ID}, {ID, "constant"}, {0}}, .fn = c}, - {(struct step[]){{ID}, {ID, "prototype"}, {0}}, .fn = p, .end = endp}, + {(struct step[]){{ID}, {ID, "prototype"}, {0}}, .fn = p}, {0} }; -static void reset(struct prv *p) -{ - struct pos *pos = &p->pos[p->i]; - - pos->seq = seqs; - pos->step = seqs->steps; - p->stk = p->tk; -} - static int bump(const struct tk *tk, const struct type *t, struct prv *p) { struct tdconstant *tdc = p->tdc; @@ -90,6 +80,21 @@ static int bump(const struct tk *tk, const struct type *t, struct prv *p) return 0; } +static void insert(struct td *td, struct type *t) +{ + struct type *tt = td->types; + + if (!tt) + td->types = t; + else + for (; tt; tt = tt->next) + if (!tt->next) + { + tt->next = t; + break; + } +} + static int cid(const struct lex *l, struct prv *p) { const struct fn *fn = fn_cur(p); @@ -98,29 +103,26 @@ static int cid(const struct lex *l, struct prv *p) const struct type *t = type_find(fn, id->s); struct tdconstant *tdc = p->tdc; struct td *td = fn->td; - size_t n = td->ntypes + 1; - struct type *types, *nt; + struct type *nt; + const struct loc *loc; if (t) { - const struct loc *loc = &t->tk->loc; - + loc = &t->tk->loc; errloc(id, "type \"%s\" already defined at %s:%d:%d", id->s, loc->f, loc->line, loc->col); return -1; } else if ((e = fn_var(fn, id))) { - const struct loc *loc = &e->tk->loc; - + loc = &e->tk->loc; errloc(id, "\"%s\" already defined as a variable at %s:%d:%d", id->s, loc->f, loc->line, loc->col); return -1; } else if ((e = fn_var(fn, v))) { - const struct loc *loc = &e->tk->loc; - + loc = &e->tk->loc; errloc(id, "value \"%s\" (defined at %s:%d:%d) is not a constant", v->s, loc->f, loc->line, loc->col); return -1; @@ -132,20 +134,17 @@ static int cid(const struct lex *l, struct prv *p) } else if (t->type != C) { - const struct loc *loc = &t->tk->loc; - + loc = &t->tk->loc; errloc(id, "value \"%s\" (defined at %s:%d:%d) is not a constant", v->s, loc->f, loc->line, loc->col); return -1; } - else if (!(types = realloc(td->types, n * sizeof *types))) + else if (!(nt = malloc(sizeof *nt))) { - perror("realloc(3)"); + perror("malloc(3)"); return -1; } - td->types = types; - nt = &td->types[td->ntypes++]; *nt = (struct type) { .type = C, @@ -154,6 +153,7 @@ static int cid(const struct lex *l, struct prv *p) .u.c = t->u.c }; + insert(td, nt); tdc->neg = t->sign; if (bump(id, t, p)) @@ -162,9 +162,11 @@ static int cid(const struct lex *l, struct prv *p) for (size_t i = 0; i < p->block; i++) dbg("\t"); - dbg("\tadding constant %s to %s (%lld)\n", id->s, v->s, t->u.c.v); + loc = &id->loc; + dbg("\tadd constant %s (%d:%d) to %s (%lld)\n", id->s, loc->line, + loc->col, v->s, t->u.c.v); reset(p); - return 1; + return 0; } static int cnum(const struct lex *l, struct prv *p) @@ -174,12 +176,12 @@ static int cnum(const struct lex *l, struct prv *p) const char *name = id->s; const struct stentry *e; struct td *td = fn->td; - size_t n = td->ntypes + 1; - struct type *types, *nt; + struct type *nt; struct tdconstant *tdc = p->tdc; const struct type *t = type_find(fn, name); int sign = *num->s == '-'; unsigned long long uv = 0; + const struct loc *loc; long long v; char *end; @@ -194,28 +196,24 @@ static int cnum(const struct lex *l, struct prv *p) if (t) { - const struct loc *loc = &t->tk->loc; - + loc = &t->tk->loc; errloc(id, "type \"%s\" already defined at %s:%d:%d", name, loc->f, loc->line, loc->col); return -1; } else if ((e = fn_var(fn, id))) { - const struct loc *loc = &e->tk->loc; - + loc = &e->tk->loc; errloc(id, "\"%s\" already defined as a variable at %s:%d:%d", id->s, loc->f, loc->line, loc->col); return -1; } - else if (!(types = realloc(td->types, n * sizeof *types))) + else if (!(nt = malloc(sizeof *nt))) { - perror("realloc(3)"); + perror("malloc(3)"); return -1; } - td->types = types; - nt = &td->types[td->ntypes++]; *nt = (struct type) { .type = C, @@ -228,18 +226,21 @@ static int cnum(const struct lex *l, struct prv *p) else tdc->u.uv = nt->u.c.uv = uv; + insert(td, nt); tdc->neg = sign; for (size_t i = 0; i < p->block; i++) dbg("\t"); - dbg("\tadding constant %s to %s\n", id->s, num->s); + loc = &id->loc; + dbg("\tadd constant %s (%d:%d) to %s\n", id->s, loc->line, loc->col, + num->s); if (bump(id, nt, p)) return -1; reset(p); - return 1; + return 0; } static int c(const struct lex *l, struct prv *p) @@ -249,34 +250,30 @@ static int c(const struct lex *l, struct prv *p) const struct stentry *e; const struct type *t = type_find(fn, id->s); struct td *td = fn->td; - size_t n = td->ntypes + 1; - struct type *types, *nt; + struct type *nt; const struct tdconstant *tdc = p->tdc; + const struct loc *loc; if (t) { - const struct loc *loc = &t->tk->loc; - + loc = &t->tk->loc; errloc(id, "type \"%s\" already defined at %s:%d:%d", id->s, loc->f, loc->line, loc->col); return -1; } else if ((e = fn_var(fn, id))) { - const struct loc *loc = &e->tk->loc; - + loc = &e->tk->loc; errloc(id, "\"%s\" already defined as a variable at %s:%d:%d", id->s, loc->f, loc->line, loc->col); return -1; } - else if (!(types = realloc(td->types, n * sizeof *types))) + else if (!(nt = malloc(sizeof *nt))) { - perror("realloc(3)"); + perror("malloc(3)"); return -1; } - td->types = types; - nt = &td->types[td->ntypes]; *nt = (struct type) { .type = C, @@ -289,31 +286,33 @@ static int c(const struct lex *l, struct prv *p) else nt->u.c.uv = tdc->u.uv; + insert(td, nt); + for (size_t i = 0; i < p->block; i++) dbg("\t"); - dbg("\tadding constant %s to ", id->s); + loc = &id->loc; + dbg("\tadd constant %s (%d:%d) to ", id->s, loc->line, loc->col); if (tdc->neg) dbg("%lld", tdc->u.v); else dbg("%llu", tdc->u.uv); - dbg("\n", stderr); + dbg("\n"); - if (bump(id, &td->types[td->ntypes++], p)) + if (bump(id, nt, p)) return -1; reset(p); - return 1; + return 0; } static int p(const struct lex *l, struct prv *p) { struct fn *fn = fn_cur(p), *pt = NULL; struct td *td = fn->td; - size_t n = td->ntypes + 1; - struct type *types; + struct type *nt = NULL; if (!(pt = malloc(sizeof *pt))) { @@ -323,28 +322,30 @@ static int p(const struct lex *l, struct prv *p) *pt = (struct fn){.tk = p->stk, .parent = fn}; - if (!(types = realloc(td->types, n * sizeof *types))) + if (!(nt = malloc(sizeof *nt))) { - perror("realloc(3)"); + perror("malloc(3)"); goto failure; } - types[td->ntypes++] = (struct type) + *nt = (struct type) { .type = P, .tk = p->stk, + /* TODO: use frontend-specific pointer size */ .sz = sizeof (void *), .align = sizeof (void *), .u.p.fn = pt }; - td->types = types; + insert(td, nt); p->proto = pt; - dbg("\tadding prototype %s\n", p->stk->s); + dbg("\tadd prototype %s\n", p->stk->s); return div_p(l, p); failure: free(pt); + type_free(nt); return -1; } @@ -364,7 +365,7 @@ static int ends(struct type *t) t->align = st->entries->t->align; t->sz = e->offset + e->t->sz; t->sz += t->align - (t->sz % t->align); - dbg("\tadding struct %s, size %zu, align %zu\n", t->tk->s, t->sz, t->align); + dbg("\tadd struct %s, size %zu, align %zu\n", t->tk->s, t->sz, t->align); return 0; } @@ -419,15 +420,6 @@ static int end(const struct lex *l, struct prv *p) p->ag = NULL; p->block--; reset(p); - return 1; -} - -static int endp(const struct lex *l, struct prv *p) -{ - if (fn_end(l, p)) - return -1; - - p->proto = NULL; return 0; } @@ -436,8 +428,7 @@ static int s(const struct lex *l, struct prv *p) const struct tk *id = p->stk; struct fn *fn = fn_cur(p); struct td *td = fn->td; - size_t nt = td->ntypes + 1; - struct type *types, *newt; + struct type *nt; const struct type *t = type_find(fn, id->s); if (t) @@ -448,23 +439,22 @@ static int s(const struct lex *l, struct prv *p) loc->line, loc->col); return -1; } - else if (!(types = realloc(td->types, nt * sizeof *types))) + else if (!(nt = malloc(sizeof *nt))) { perror("realloc(3)"); return -1; } - td->types = types; - newt = &td->types[td->ntypes++]; - *newt = (struct type) + *nt = (struct type) { .type = S, .tk = id }; - p->ag = newt; + p->ag = nt; p->block++; - return storage(l, p, id->s, 0, &newt->u.s.storage); + insert(td, nt); + return storage(l, p, id->s, 0, &nt->u.s.storage); } static int u(const struct lex *l, struct prv *p) @@ -481,8 +471,7 @@ static int tt(const struct lex *l, struct prv *p, const struct type *t) const struct type *tp = type_ufind(fn, id->s); const struct loc *loc = &id->loc; struct td *td = fn->td; - size_t n = td->ntypes + 1; - struct type *types, *nt; + struct type *nt; char *s = type_name(t); if (!s) @@ -495,16 +484,12 @@ static int tt(const struct lex *l, struct prv *p, const struct type *t) loc->line, loc->col); goto end; } - else if (pop(l, p)) - return -1; - else if (!(types = realloc(td->types, n * sizeof *types))) + else if (!(nt = malloc(sizeof *nt))) { perror("realloc(3)"); goto end; } - td->types = types; - nt = &td->types[td->ntypes++]; *nt = (struct type) { .type = T, @@ -512,10 +497,11 @@ static int tt(const struct lex *l, struct prv *p, const struct type *t) .u.t.alias = t }; - dbg("\tadding %s (%d:%d) alias %s\n", id->s, loc->line, loc->col, s); + dbg("\tadd %s (%d:%d) alias %s\n", id->s, loc->line, loc->col, s); + insert(td, nt); reset(p); p->id = NULL; - ret = 1; + ret = 0; end: free(s); return ret; @@ -530,12 +516,11 @@ static int a(const struct lex *l, struct prv *p) void td_free(struct td *td) { if (td) - { - for (size_t i = 0; i < td->ntypes; i++) - type_free(&td->types[i]); - - free(td->types); - } + for (struct type *t = td->types, *next; t; t = next) + { + next = t->next; + type_lfree(t); + } free(td); } @@ -548,9 +533,8 @@ int td(const struct lex *l, struct prv *p) struct tdconstant *tdc = NULL; struct pos init = { - .seq = seqs, - .stseq = seqs, - .step = seqs->steps + .name = __func__, + .seq = seqs }; if (prev) @@ -575,7 +559,7 @@ int td(const struct lex *l, struct prv *p) fn->td = td; p->tdc = tdc; p->stk = p->tk; - return 1; + return 0; failure: free(tdc); -- cgit v1.2.3