From 3b2503978998ee1e8d4aeebbdbfb743f125e1b5e Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Fri, 29 May 2020 14:55:24 -0700 Subject: switch types to be integers into the main database --- sys/cmd/cc/ast.c | 124 ++++++++++++++++++++++++++++++------------------------- sys/cmd/cc/cc.c | 6 +-- sys/cmd/cc/cc.h | 20 ++++----- 3 files changed, 79 insertions(+), 71 deletions(-) diff --git a/sys/cmd/cc/ast.c b/sys/cmd/cc/ast.c index e471a32..f8ab328 100644 --- a/sys/cmd/cc/ast.c +++ b/sys/cmd/cc/ast.c @@ -162,7 +162,7 @@ declareenum(Parser *p, int n, string *elts, Expr *vals) static void -declaretag(Parser *p, Type *t, string name) +declaretag(Parser *p, uint32 t, string name) { Sym *sym; sym = lookup(&p->sp->tags, name); @@ -172,7 +172,7 @@ declaretag(Parser *p, Type *t, string name) } sym = define(&p->sp->tags, name, Stype); - sym->typ = t; + sym->type = t; } static @@ -221,7 +221,7 @@ nomatch(Token t, vlong kind) static error spec(Parser *, Lexer *, uint64 *); static error dtor(Parser *p, Lexer *lx, Dtor *d, int ab); -static Type *typeofdtor(Dtor *, Type *); +static uint32 typeofdtor(Dtor *, uint32); static Decl *decl(Parser *, Lexer *); static Expr *expr(Parser *, Lexer *); @@ -723,7 +723,7 @@ stmt(Parser *p, Lexer *lx) if (sym->kind == Stype) goto Tdecl; - if (sym->kind == Sobj) + if (sym->kind == Svar) goto Texpr; errorat(lx->pos, "bad symbol type used as type identifier"); @@ -1030,12 +1030,14 @@ Bad: // ----------------------------------------------------------------------- // types -Type * -ptrtype(Type *base, uint32 qual) +uint32 +ptrtype(uint32 base, uint32 qual) { + uint32 i; Type *t; - t = type(); + i = type(); + t = C.type.info + i; t->kind = Tptr; t->ptr.base = base; t->ptr.qual = qual; @@ -1043,34 +1045,36 @@ ptrtype(Type *base, uint32 qual) t->align = pointer.align; t->sign = pointer.sign; - return t; + return i; } -Type* -arraytype(Type *base, uint32 qual, Expr *ix) +uint32 +arraytype(uint32 base, uint32 qual, Expr *ix) { - int n; + int i, n; Type *t; /* TODO: evaluate the length */ n = 10; - t = type(); + i = type(); + t = C.type.info + i; t->kind = Tarray; t->ptr.base = base; - t->size = n * base->size; - t->align = base->align; + t->size = n * C.type.info[base].size; + t->align = C.type.info[base].align; t->sign = 0; - return t; + return i; } -Type* -functype(Type *ret, int n, Field *args, int dots) +uint32 +functype(uint32 ret, int n, Field *args, int dots) { - int i; - Type *t; + uint32 i; + Type *t; - t = type(); + i = type(); + t = C.type.info + i; t->kind = Tfunc; t->size = pointer.size; t->align = pointer.align; @@ -1081,60 +1085,66 @@ functype(Type *ret, int n, Field *args, int dots) t->func.arg = args; t->func.dots = dots; - return t; + return i; } #define ALIGN_DOWN(n, a) ((n) & ~((a)-1)) #define ALIGN_UP(n, a) ALIGN_DOWN((n) + (a)-1, (a)) -Type * +uint32 structtype(int n, Field *field, Expr *bits) { + uint32 i; Type *t; Field *f, *e; - t = type(); + i = type(); + t = C.type.info + i; t->kind = Tstruct; t->size = 0; t->align = 0; for (f = field, e = field+n; f != e; ++f) { - t->size += f->type->size + ALIGN_UP(t->size, f->type->align); - t->align = MAX(t->align, f->type->align); + t->size += C.type.info[f->type].size + ALIGN_UP(t->size, C.type.info[f->type].align); + t->align = MAX(t->align, C.type.info[f->type].align); } t->aggr.len = n; t->aggr.f = field; t->aggr.x = bits; - return t; + return i; } -Type * +uint32 uniontype(int n, Field *field, Expr *bits) { + uint32 i; Type *t; Field *f, *e; - t = type(); + i = type(); + t = C.type.info + i; t->kind = Tstruct; t->size = 0; t->align = 0; for (f = field, e = field+n; f != e; ++f) { - t->size = MAX(t->size, f->type->size); - t->align = MAX(t->align, f->type->align); + t->size = MAX(t->size, C.type.info[f->type].size); + t->align = MAX(t->align, C.type.info[f->type].align); } t->aggr.len = n; t->aggr.f = field; t->aggr.x = bits; - return t; + return i; } -Type * +uint32 enumtype(int n, string *elts, Expr *vals) { + uint32 i; Type *t; Field *f, *e; - t = type(); + i = type(); + t = C.type.info + i; t->kind = Tenum; /* TODO: dont hardcode int32 */ t->size = 4; @@ -1143,15 +1153,15 @@ enumtype(int n, string *elts, Expr *vals) t->enm.elt = elts; t->enm.val = vals; - return t; + return i; } #undef ALIGN_UP #undef ALIGN_DOWN /* unpacking C declarations into sensible types */ static -Type * -typeofname(Name *name, Type *base) +uint32 +typeofname(Name *name, uint32 base) { switch (name->kind) { case Nident: @@ -1165,12 +1175,12 @@ typeofname(Name *name, Type *base) default: panicf("unreachable"); } - return nil; + return 0; } static -Type * -typeofdtor(Dtor *decl, Type* base) +uint32 +typeofdtor(Dtor *decl, uint32 base) { int n; Ptr *p; @@ -1193,7 +1203,7 @@ typeofdtor(Dtor *decl, Type* base) } static -Type* +uint32 basetype(Parser *p, Lexer *lx, uint64 *s) { int n; @@ -1201,7 +1211,7 @@ basetype(Parser *p, Lexer *lx, uint64 *s) if (spec(p, lx, s)) { errorat(lx->pos, "failed to parse type specifier"); - return nil; + return 0; } m = (((*s<<32)>>32) & ~(MaskQul | MaskMem)); @@ -1211,20 +1221,20 @@ basetype(Parser *p, Lexer *lx, uint64 *s) m = *s >> 32; if (!m) { errorat(lx->pos, "not a valid type identifier"); - return nil; + return 0; } - return C.type.info + m; + return m; } - return C.type.info + indextypespec[n]; + return indextypespec[n]; } errorat(lx->pos, "invalid type specifier"); - return nil; + return 0; } static string -namedecl(Parser *p, Lexer *lx, Type **base) +namedecl(Parser *p, Lexer *lx, uint32 *base) { Dtor *dt; string name; @@ -1247,13 +1257,13 @@ End: // declarations static -Type * +uint32 enumerate(Parser *p, Lexer *lx, string name) { int i, n; uint64 s; + uint32 t; Token tk; - Type *t; /* TODO: think of a better soln */ string nm[1024], *elts; Expr *cx[1024], *vals; @@ -1287,11 +1297,11 @@ enumerate(Parser *p, Lexer *lx, string name) return t; Bad: errorat(tk.pos, "failed to parse enum declaration"); - return nil; + return 0; } static -Type * +uint32 aggregate(Parser *p, Lexer *lx, string name, int kind) { int n; @@ -1340,7 +1350,7 @@ aggregate(Parser *p, Lexer *lx, string name, int kind) return structtype(n, f, x); Bad: errorat(tk.pos, "failed to parse aggregate declaration"); - return nil; + return 0; } static @@ -1351,7 +1361,7 @@ spec(Parser *p, Lexer *lx, uint64 *spec) int n; Sym *typ; string name; - Type *tag; + uint32 tag; uint64 s, sm; s = 0; @@ -1363,7 +1373,7 @@ spec(Parser *p, Lexer *lx, uint64 *spec) errorat(t.pos, "invalid type name '%s' in specifier", t.val.s); goto Bad; } - sm = typ->typ - C.type.info; + sm = typ->type; s |= sm << 32; continue; } @@ -1429,7 +1439,7 @@ spec(Parser *p, Lexer *lx, uint64 *spec) } name = nil; - tag = nil; + tag = 0; if (t.kind == Aident) { name = t.val.s; t = advance(p, lx); @@ -1460,7 +1470,7 @@ spec(Parser *p, Lexer *lx, uint64 *spec) } name = nil; - tag = nil; + tag = 0; if (t.kind == Aident) { name = t.val.s; t = advance(p, lx); @@ -1633,7 +1643,7 @@ name(Parser *p, Lexer *lx, Name **nmp, int ab) panicf("ident stack overflow"); args[nm->sfx.call.n++] = (struct Field) { .qual = 0, - .type = nil, + .type = 0, .name = t.val.s, }; t = advance(p, lx); @@ -1772,7 +1782,7 @@ decl(Parser *p, Lexer *lx) Token t; Decl *d; Expr *x; - Type *base, *type; + uint32 base, type; string name; struct Decls *ds; diff --git a/sys/cmd/cc/cc.c b/sys/cmd/cc/cc.c index a25fd69..2dcf897 100644 --- a/sys/cmd/cc/cc.c +++ b/sys/cmd/cc/cc.c @@ -94,17 +94,15 @@ END: // ----------------------------------------------------------------------- // type interning -Type * +int type() { - Type *t; - if (C.type.len >= C.type.cap) { C.type.cap += 100; C.type.info = realloc(C.type.info, C.type.cap * sizeof(*C.type.info)); } - return C.type.info + C.type.len++; + return C.type.len++; } // ----------------------------------------------------------------------- diff --git a/sys/cmd/cc/cc.h b/sys/cmd/cc/cc.h index e711770..44b0037 100644 --- a/sys/cmd/cc/cc.h +++ b/sys/cmd/cc/cc.h @@ -245,7 +245,7 @@ struct Sym union { string macro; Decl *obj; - Type *typ; + int32 type; Stmt *blk; Expr *val; }; @@ -400,7 +400,7 @@ struct Key struct Expr { struct Node; - Type *type; + int32 type; union { struct { uint64 kind; @@ -438,7 +438,7 @@ struct Expr Expr *post; } unary; struct { - Type *to; + int32 to; Expr *x; } cast; struct { @@ -597,14 +597,14 @@ struct Dtor struct Field { uint32 qual; - Type *type; + uint32 type; string name; }; struct Decls { string name; - Type *type; + uint32 type; Expr *init; struct Decls *link; }; @@ -617,7 +617,7 @@ struct Decl union { struct { string name; - Type *type; + uint32 type; union { Stmt *body; Expr *init; @@ -649,12 +649,12 @@ struct Type union { struct { uint32 qual; - Type *base; + uint32 base; } ptr; struct { int len; uint32 qual; - Type *elt; + uint32 *elt; } arr; struct { int len; @@ -667,7 +667,7 @@ struct Type Expr *val; } enm; struct { - Type *ret; + uint32 ret; int n; int dots : 1; Field *arg; @@ -768,6 +768,6 @@ extern Compiler C; /* cc.c functions */ void init(); int32 intern(byte **str); -Type *type(); +int32 type(); #undef iota -- cgit v1.2.1