From 7d6efdd8626fd3a654a8f231fc1ddc481352af6a Mon Sep 17 00:00:00 2001 From: Michael Forney Date: Fri, 12 Apr 2024 00:46:22 -0700 Subject: decl: Add name field to decl struct --- cc.h | 5 +++-- decl.c | 15 ++++++++------- eval.c | 2 +- qbe.c | 12 ++++++------ scope.c | 43 ++++++++++++++++++++----------------------- 5 files changed, 38 insertions(+), 39 deletions(-) diff --git a/cc.h b/cc.h index 4eb2c63..e69bad0 100644 --- a/cc.h +++ b/cc.h @@ -271,6 +271,7 @@ enum builtinkind { }; struct decl { + char *name; enum declkind kind; enum linkage linkage; struct type *type; @@ -487,7 +488,7 @@ bool gnuattr(struct attr *, enum attrkind); /* decl */ -struct decl *mkdecl(enum declkind, struct type *, enum typequal, enum linkage); +struct decl *mkdecl(char *name, enum declkind, struct type *, enum typequal, enum linkage); bool decl(struct scope *, struct func *); struct type *typename(struct scope *, enum typequal *); @@ -501,7 +502,7 @@ void scopeinit(void); struct scope *mkscope(struct scope *); struct scope *delscope(struct scope *); -void scopeputdecl(struct scope *, const char *, struct decl *); +void scopeputdecl(struct scope *, struct decl *); struct decl *scopegetdecl(struct scope *, const char *, bool); void scopeputtag(struct scope *, const char *, struct type *); diff --git a/decl.c b/decl.c index eb9217b..a2b689e 100644 --- a/decl.c +++ b/decl.c @@ -60,12 +60,13 @@ struct structbuilder { }; struct decl * -mkdecl(enum declkind k, struct type *t, enum typequal tq, enum linkage linkage) +mkdecl(char *name, enum declkind k, struct type *t, enum typequal tq, enum linkage linkage) { struct decl *d; d = xmalloc(sizeof(*d)); memset(d, 0, sizeof(*d)); + d->name = name; d->kind = k; d->linkage = linkage; d->type = t; @@ -271,7 +272,7 @@ tagspec(struct scope *s) } assert(i < LEN(inttypes)); } - d = mkdecl(DECLCONST, et, QUALNONE, LINKNONE); + d = mkdecl(name, DECLCONST, et, QUALNONE, LINKNONE); d->value = mkintconst(value); d->next = enumconsts; enumconsts = d; @@ -281,7 +282,7 @@ tagspec(struct scope *s) } else if (value > max) { max = value; } - scopeputdecl(s, name, d); + scopeputdecl(s, d); if (!consume(TCOMMA)) break; } @@ -979,8 +980,8 @@ declcommon(struct scope *s, enum declkind kind, char *name, char *asmname, struc t = typecomposite(t, prior->type); } } - d = mkdecl(kind, t, tq, linkage); - scopeputdecl(s, name, d); + d = mkdecl(name, kind, t, tq, linkage); + scopeputdecl(s, d); if (kind == DECLFUNC || linkage != LINKNONE || sc & SCSTATIC) { if (asmname) name = asmname; @@ -1053,7 +1054,7 @@ decl(struct scope *s, struct func *f) if (asmname) error(&tok.loc, "typedef '%s' declared with assembler label", name); if (!prior) - scopeputdecl(s, name, mkdecl(DECLTYPE, t, tq, LINKNONE)); + scopeputdecl(s, mkdecl(name, DECLTYPE, t, tq, LINKNONE)); else if (!typesame(prior->type, t) || prior->qual != tq) error(&tok.loc, "typedef '%s' redefined with different type", name); break; @@ -1147,7 +1148,7 @@ stringdecl(struct expr *expr) entry = mapput(&strings, &key); d = *entry; if (!d) { - d = mkdecl(DECLOBJECT, expr->type, QUALNONE, LINKNONE); + d = mkdecl(NULL, DECLOBJECT, expr->type, QUALNONE, LINKNONE); d->value = mkglobal("string", true); emitdata(d, mkinit(0, expr->type->size, (struct bitfield){0}, expr)); *entry = d; diff --git a/eval.c b/eval.c index 3e83085..d6c70b1 100644 --- a/eval.c +++ b/eval.c @@ -118,7 +118,7 @@ eval(struct expr *expr) case EXPRCOMPOUND: if (expr->u.compound.storage != SDSTATIC) break; - d = mkdecl(DECLOBJECT, t, expr->qual, LINKNONE); + d = mkdecl(NULL, DECLOBJECT, t, expr->qual, LINKNONE); d->value = mkglobal(NULL, true); emitdata(d, expr->u.compound.init); expr->kind = EXPRIDENT; diff --git a/qbe.c b/qbe.c index 8a7010a..f4c97db 100644 --- a/qbe.c +++ b/qbe.c @@ -493,7 +493,7 @@ mkfunc(struct decl *decl, char *name, struct type *t, struct scope *s) functemp(f, p->value); if(!p->name) continue; - d = mkdecl(DECLOBJECT, p->type, p->qual, LINKNONE); + d = mkdecl(p->name, DECLOBJECT, p->type, p->qual, LINKNONE); if (p->type->value) { d->value = p->value; } else { @@ -501,13 +501,13 @@ mkfunc(struct decl *decl, char *name, struct type *t, struct scope *s) funcalloc(f, d); funcstore(f, p->type, QUALNONE, (struct lvalue){d->value}, v); } - scopeputdecl(s, p->name, d); + scopeputdecl(s, d); } t = mkarraytype(&typechar, QUALCONST, strlen(name) + 1); - d = mkdecl(DECLOBJECT, t, QUALNONE, LINKNONE); - d->value = mkglobal("__func__", true); - scopeputdecl(s, "__func__", d); + d = mkdecl("__func__", DECLOBJECT, t, QUALNONE, LINKNONE); + d->value = mkglobal(d->name, true); + scopeputdecl(s, d); f->namedecl = d; funclabel(f, mkblock("body")); @@ -639,7 +639,7 @@ funclval(struct func *f, struct expr *e) lval.addr = d->value; break; case EXPRCOMPOUND: - d = mkdecl(DECLOBJECT, e->type, e->qual, LINKNONE); + d = mkdecl(NULL, DECLOBJECT, e->type, e->qual, LINKNONE); funcinit(f, d, e->u.compound.init, true); lval.addr = d->value; break; diff --git a/scope.c b/scope.c index c8de958..213acba 100644 --- a/scope.c +++ b/scope.c @@ -9,32 +9,29 @@ struct scope filescope; void scopeinit(void) { - static struct builtin { - char *name; - struct decl decl; - } builtins[] = { - {"__builtin_alloca", {.kind = DECLBUILTIN, .u.builtin = BUILTINALLOCA}}, - {"__builtin_constant_p", {.kind = DECLBUILTIN, .u.builtin = BUILTINCONSTANTP}}, - {"__builtin_expect", {.kind = DECLBUILTIN, .u.builtin = BUILTINEXPECT}}, - {"__builtin_inff", {.kind = DECLBUILTIN, .u.builtin = BUILTININFF}}, - {"__builtin_nanf", {.kind = DECLBUILTIN, .u.builtin = BUILTINNANF}}, - {"__builtin_offsetof", {.kind = DECLBUILTIN, .u.builtin = BUILTINOFFSETOF}}, - {"__builtin_types_compatible_p", - {.kind = DECLBUILTIN, .u.builtin = BUILTINTYPESCOMPATIBLEP}}, - {"__builtin_unreachable", {.kind = DECLBUILTIN, .u.builtin = BUILTINUNREACHABLE}}, - {"__builtin_va_arg", {.kind = DECLBUILTIN, .u.builtin = BUILTINVAARG}}, - {"__builtin_va_copy", {.kind = DECLBUILTIN, .u.builtin = BUILTINVACOPY}}, - {"__builtin_va_end", {.kind = DECLBUILTIN, .u.builtin = BUILTINVAEND}}, - {"__builtin_va_start", {.kind = DECLBUILTIN, .u.builtin = BUILTINVASTART}}, + static struct decl builtins[] = { + {.name = "__builtin_alloca", .kind = DECLBUILTIN, .u.builtin = BUILTINALLOCA}, + {.name = "__builtin_constant_p", .kind = DECLBUILTIN, .u.builtin = BUILTINCONSTANTP}, + {.name = "__builtin_expect", .kind = DECLBUILTIN, .u.builtin = BUILTINEXPECT}, + {.name = "__builtin_inff", .kind = DECLBUILTIN, .u.builtin = BUILTININFF}, + {.name = "__builtin_nanf", .kind = DECLBUILTIN, .u.builtin = BUILTINNANF}, + {.name = "__builtin_offsetof", .kind = DECLBUILTIN, .u.builtin = BUILTINOFFSETOF}, + {.name = "__builtin_types_compatible_p", .kind = DECLBUILTIN, .u.builtin = BUILTINTYPESCOMPATIBLEP}, + {.name = "__builtin_unreachable", .kind = DECLBUILTIN, .u.builtin = BUILTINUNREACHABLE}, + {.name = "__builtin_va_arg", .kind = DECLBUILTIN, .u.builtin = BUILTINVAARG}, + {.name = "__builtin_va_copy", .kind = DECLBUILTIN, .u.builtin = BUILTINVACOPY}, + {.name = "__builtin_va_end", .kind = DECLBUILTIN, .u.builtin = BUILTINVAEND}, + {.name = "__builtin_va_start", .kind = DECLBUILTIN, .u.builtin = BUILTINVASTART}, }; static struct decl valist; - struct builtin *b; + struct decl *d; - for (b = builtins; b < builtins + LEN(builtins); ++b) - scopeputdecl(&filescope, b->name, &b->decl); + for (d = builtins; d < builtins + LEN(builtins); ++d) + scopeputdecl(&filescope, d); + valist.name = "__builtin_va_list"; valist.kind = DECLTYPE; valist.type = targ->typevalist; - scopeputdecl(&filescope, "__builtin_va_list", &valist); + scopeputdecl(&filescope, &valist); } struct scope * @@ -98,13 +95,13 @@ scopegettag(struct scope *s, const char *name, bool recurse) } void -scopeputdecl(struct scope *s, const char *name, struct decl *d) +scopeputdecl(struct scope *s, struct decl *d) { struct mapkey k; if (!s->decls.len) mapinit(&s->decls, 32); - mapkey(&k, name, strlen(name)); + mapkey(&k, d->name, strlen(d->name)); *mapput(&s->decls, &k) = d; } -- cgit v1.2.3