Commit a1214105 authored by Russ Cox's avatar Russ Cox

make Syms smaller.

collapse a lot of duplication in dcl.c
switch to NodeList* from Dcl*

R=ken
OCL=32770
CL=32770
parent 3a23e533
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
#undef EXTERN #undef EXTERN
#define EXTERN #define EXTERN
#include "gg.h" #include "gg.h"
...@@ -59,7 +58,6 @@ compile(Node *fn) ...@@ -59,7 +58,6 @@ compile(Node *fn)
pl = newplist(); pl = newplist();
pl->name = curfn->nname; pl->name = curfn->nname;
pl->locals = autodcl;
nodconst(&nod1, types[TINT32], 0); nodconst(&nod1, types[TINT32], 0);
ptxt = gins(ATEXT, curfn->nname, &nod1); ptxt = gins(ATEXT, curfn->nname, &nod1);
......
...@@ -59,7 +59,6 @@ compile(Node *fn) ...@@ -59,7 +59,6 @@ compile(Node *fn)
pl = newplist(); pl = newplist();
pl->name = curfn->nname; pl->name = curfn->nname;
pl->locals = autodcl;
nodconst(&nod1, types[TINT32], 0); nodconst(&nod1, types[TINT32], 0);
ptxt = gins(ATEXT, curfn->nname, &nod1); ptxt = gins(ATEXT, curfn->nname, &nod1);
...@@ -573,7 +572,7 @@ cgen_div(int op, Node *nl, Node *nr, Node *res) ...@@ -573,7 +572,7 @@ cgen_div(int op, Node *nl, Node *nr, Node *res)
regfree(&ax); regfree(&ax);
regfree(&dx); regfree(&dx);
if(oldax.op != 0) { if(oldax.op != 0) {
gmove(&oldax, &ax); gmove(&oldax, &ax);
regfree(&oldax); regfree(&oldax);
...@@ -582,7 +581,7 @@ cgen_div(int op, Node *nl, Node *nr, Node *res) ...@@ -582,7 +581,7 @@ cgen_div(int op, Node *nl, Node *nr, Node *res)
gmove(&olddx, &dx); gmove(&olddx, &dx);
regfree(&olddx); regfree(&olddx);
} }
} }
/* /*
......
...@@ -57,7 +57,6 @@ compile(Node *fn) ...@@ -57,7 +57,6 @@ compile(Node *fn)
pl = newplist(); pl = newplist();
pl->name = curfn->nname; pl->name = curfn->nname;
pl->locals = autodcl;
nodconst(&nod1, types[TINT32], 0); nodconst(&nod1, types[TINT32], 0);
ptxt = gins(ATEXT, curfn->nname, &nod1); ptxt = gins(ATEXT, curfn->nname, &nod1);
......
...@@ -358,9 +358,9 @@ datastring(char *s, int len, Addr *a) ...@@ -358,9 +358,9 @@ datastring(char *s, int len, Addr *a)
*a = ao; *a = ao;
// only generate data the first time. // only generate data the first time.
if(ao.sym->uniq) if(ao.sym->flags & SymUniq)
return; return;
ao.sym->uniq = 1; ao.sym->flags |= SymUniq;
data(); data();
for(w=0; w<len; w+=8) { for(w=0; w<len; w+=8) {
...@@ -438,9 +438,9 @@ datagostring(Strlit *sval, Addr *a) ...@@ -438,9 +438,9 @@ datagostring(Strlit *sval, Addr *a)
} }
*a = ao; *a = ao;
if(ao.sym->uniq) if(ao.sym->flags & SymUniq)
return; return;
ao.sym->uniq = 1; ao.sym->flags |= SymUniq;
data(); data();
// DATA gostring, wp, $cstring // DATA gostring, wp, $cstring
......
...@@ -25,14 +25,11 @@ static Sym* dclstack; ...@@ -25,14 +25,11 @@ static Sym* dclstack;
void void
dcopy(Sym *a, Sym *b) dcopy(Sym *a, Sym *b)
{ {
a->package = b->package;
a->name = b->name; a->name = b->name;
a->def = b->def; a->def = b->def;
a->package = b->package;
a->undef = b->undef;
a->vargen = b->vargen;
a->block = b->block; a->block = b->block;
a->lastlineno = b->lastlineno; a->lastlineno = b->lastlineno;
a->offset = b->offset;
} }
Sym* Sym*
...@@ -148,155 +145,79 @@ testdclstack(void) ...@@ -148,155 +145,79 @@ testdclstack(void)
/* /*
* declare individual names - var, typ, const * declare individual names - var, typ, const
*/ */
static void
redeclare(char *str, Sym *s)
{
if(s->block == block) {
yyerror("%s %S redeclared in this block", str, s);
print(" previous declaration at %L\n", s->lastlineno);
}
s->block = block;
s->lastlineno = lineno;
}
void void
addvar(Node *n, Type *t, int ctxt) declare(Node *n, int ctxt)
{ {
Dcl *r, *d;
Sym *s; Sym *s;
char *what;
int gen; int gen;
static int typegen, vargen;
if(n==N || n->sym == S || (n->op != ONAME && n->op != ONONAME) || t == T)
fatal("addvar: n=%N t=%T nil", n, t);
s = n->sym; s = n->sym;
gen = 0;
if(ctxt == PEXTERN || ctxt == PFUNC) { if(ctxt == PEXTERN) {
r = externdcl; externdcl = list(externdcl, n);
gen = 0;
} else { } else {
r = autodcl; if(autodcl != nil)
vargen++; autodcl = list(autodcl, n);
gen = vargen; if(n->op == OTYPE)
gen = ++typegen;
else if(n->op == ONAME)
gen = ++vargen;
pushdcl(s); pushdcl(s);
} }
redeclare("variable", s); if(s->block == block) {
n->op = ONAME; what = "???";
s->vargen = gen; switch(n->op) {
s->def = n; case ONAME:
s->offset = 0; what = "variable";
break;
case OLITERAL:
what = "constant";
break;
case OTYPE:
what = "type";
break;
}
n->funcdepth = funcdepth; yyerror("%s %S redeclared in this block", what, s);
n->type = t; print("\tprevious declaration at %L\n", s->lastlineno);
}
s->block = block;
s->lastlineno = lineno;
s->def = n;
n->vargen = gen; n->vargen = gen;
n->funcdepth = funcdepth;
n->class = ctxt; n->class = ctxt;
d = dcl(); autoexport(n, ctxt);
d->dsym = s;
d->dnode = n;
d->op = ONAME;
r->back->forw = d;
r->back = d;
if(dflag()) {
if(ctxt == PEXTERN)
print("extern var-dcl %S G%ld %T\n", s, s->vargen, t);
else if(ctxt == PFUNC)
print("extern func-dcl %S G%ld %T\n", s, s->vargen, t);
else
print("auto var-dcl %S G%ld %T\n", s, s->vargen, t);
}
} }
void void
addtyp(Type *n, int ctxt) addvar(Node *n, Type *t, int ctxt)
{ {
Dcl *r, *d; if(n==N || n->sym == S || (n->op != ONAME && n->op != ONONAME) || t == T)
Sym *s; fatal("addvar: n=%N t=%T nil", n, t);
static int typgen;
if(n==T || n->sym == S)
fatal("addtyp: n=%T t=%T nil", n);
s = n->sym;
if(ctxt == PEXTERN)
r = externdcl;
else {
r = autodcl;
pushdcl(s);
n->vargen = ++typgen;
}
redeclare("type", s);
s->def = typenod(n);
d = dcl();
d->dsym = s;
d->dtype = n;
d->op = OTYPE;
d->back = r->back;
r->back->forw = d;
r->back = d;
d = dcl();
d->dtype = n;
d->op = OTYPE;
r = typelist;
d->back = r->back;
r->back->forw = d;
r->back = d;
if(dflag()) { n->op = ONAME;
if(ctxt == PEXTERN) declare(n, ctxt);
print("extern typ-dcl %S G%ld %T\n", s, s->vargen, n); n->type = t;
else
print("auto typ-dcl %S G%ld %T\n", s, s->vargen, n);
}
} }
// TODO(rsc): cut
void void
addconst(Node *n, Node *e, int ctxt) addtyp(Type *n, int ctxt)
{ {
Sym *s; Node *def;
Dcl *r, *d;
if(n->op != ONAME && n->op != ONONAME)
fatal("addconst: not a name");
if(e->op != OLITERAL) {
yyerror("expression must be a constant");
return;
}
s = n->sym;
if(ctxt == PEXTERN) if(n==T || n->sym == S)
r = externdcl; fatal("addtyp: n=%T t=%T nil", n);
else {
r = autodcl;
pushdcl(s);
}
redeclare("constant", s);
s->def = e;
e->sym = s;
d = dcl(); def = typenod(n);
d->dsym = s; declare(def, ctxt);
d->dnode = e; n->vargen = def->vargen;
d->op = OLITERAL;
d->back = r->back;
r->back->forw = d;
r->back = d;
if(dflag()) typelist = list(typelist, def);
print("const-dcl %S %N\n", n->sym, n->sym->def);
} }
/* /*
...@@ -320,21 +241,10 @@ dodclvar(Node *n, Type *t, NodeList **init) ...@@ -320,21 +241,10 @@ dodclvar(Node *n, Type *t, NodeList **init)
t = typ(TFORW); t = typ(TFORW);
addvar(n, t, dclcontext); addvar(n, t, dclcontext);
autoexport(n->sym);
if(funcdepth > 0) if(funcdepth > 0)
*init = list(*init, nod(ODCL, n, N)); *init = list(*init, nod(ODCL, n, N));
} }
// TODO(rsc): cut
void
dodclconst(Node *n, Node *e)
{
if(n == N)
return;
addconst(n, e, dclcontext);
autoexport(n->sym);
}
/* /*
* introduce a type named n * introduce a type named n
* but it is an unknown type for now * but it is an unknown type for now
...@@ -354,15 +264,7 @@ dodcltype(Type *n) ...@@ -354,15 +264,7 @@ dodcltype(Type *n)
n = s->def->type; n = s->def->type;
if(s->block != block) { if(s->block != block) {
// completing forward struct from other file // completing forward struct from other file
Dcl *d, *r; externdcl = list(externdcl, typenod(n));
d = dcl();
d->dsym = s;
d->dtype = n;
d->op = OTYPE;
r = externdcl;
d->back = r->back;
r->back->forw = d;
r->back = d;
} }
goto found; goto found;
} }
...@@ -373,7 +275,7 @@ dodcltype(Type *n) ...@@ -373,7 +275,7 @@ dodcltype(Type *n)
found: found:
n->local = 1; n->local = 1;
autoexport(n->sym); autoexport(typenod(n), dclcontext);
return n; return n;
} }
...@@ -458,11 +360,10 @@ updatetype(Type *n, Type *t) ...@@ -458,11 +360,10 @@ updatetype(Type *n, Type *t)
NodeList* NodeList*
variter(NodeList *vl, Node *t, NodeList *el) variter(NodeList *vl, Node *t, NodeList *el)
{ {
int doexpr, gen; int doexpr;
Node *v, *e; Node *v, *e;
NodeList *init; NodeList *init;
Sym *s; Sym *s;
Dcl *r, *d;
init = nil; init = nil;
doexpr = el != nil; doexpr = el != nil;
...@@ -477,28 +378,13 @@ variter(NodeList *vl, Node *t, NodeList *el) ...@@ -477,28 +378,13 @@ variter(NodeList *vl, Node *t, NodeList *el)
} else } else
e = N; e = N;
v = vl->n; v = vl->n;
s = v->sym; s = v->sym;
if(dclcontext == PEXTERN || dclcontext == PFUNC) {
r = externdcl;
gen = 0;
} else {
r = autodcl;
gen = ++vargen;
pushdcl(s);
}
redeclare("variable", s);
s->def = v;
// TODO: vargen
s->offset = 0;
s->block = block;
v->op = ONAME; v->op = ONAME;
v->class = dclcontext; declare(v, dclcontext);
v->ntype = t; v->ntype = t;
v->funcdepth = funcdepth;
v->vargen = gen;
if(e != N || funcdepth > 0) { if(e != N || funcdepth > 0) {
if(funcdepth > 0) if(funcdepth > 0)
init = list(init, nod(ODCL, v, N)); init = list(init, nod(ODCL, v, N));
...@@ -507,15 +393,6 @@ variter(NodeList *vl, Node *t, NodeList *el) ...@@ -507,15 +393,6 @@ variter(NodeList *vl, Node *t, NodeList *el)
if(e->right != N) if(e->right != N)
v->defn = e; v->defn = e;
} }
d = dcl();
d->dsym = s;
d->dnode = v;
d->op = ONAME;
r->back->forw = d;
r->back = d;
autoexport(s);
} }
if(el != nil) if(el != nil)
yyerror("extra expr in var dcl"); yyerror("extra expr in var dcl");
...@@ -531,7 +408,6 @@ constiter(NodeList *vl, Node *t, NodeList *cl) ...@@ -531,7 +408,6 @@ constiter(NodeList *vl, Node *t, NodeList *cl)
{ {
Node *v, *c; Node *v, *c;
NodeList *vv; NodeList *vv;
Sym *s;
vv = vl; vv = vl;
if(cl == nil) { if(cl == nil) {
...@@ -554,16 +430,11 @@ constiter(NodeList *vl, Node *t, NodeList *cl) ...@@ -554,16 +430,11 @@ constiter(NodeList *vl, Node *t, NodeList *cl)
cl = cl->next; cl = cl->next;
v = vl->n; v = vl->n;
s = v->sym;
if(dclcontext != PEXTERN)
pushdcl(s);
redeclare("constant", s);
s->def = v;
v->op = OLITERAL; v->op = OLITERAL;
declare(v, dclcontext);
v->ntype = t; v->ntype = t;
v->defn = c; v->defn = c;
autoexport(s);
} }
if(cl != nil) if(cl != nil)
yyerror("extra expr in const dcl"); yyerror("extra expr in const dcl");
...@@ -572,10 +443,8 @@ constiter(NodeList *vl, Node *t, NodeList *cl) ...@@ -572,10 +443,8 @@ constiter(NodeList *vl, Node *t, NodeList *cl)
} }
/* /*
* this generates a new name that is * this generates a new name node,
* pushed down on the declaration list. * typically for labels or other one-off names.
* no diagnostics are produced as this
* name will soon be declared.
*/ */
Node* Node*
newname(Sym *s) newname(Sym *s)
...@@ -591,6 +460,11 @@ newname(Sym *s) ...@@ -591,6 +460,11 @@ newname(Sym *s)
return n; return n;
} }
/*
* this generates a new name node for a name
* being declared. if at the top level, it might return
* an ONONAME node created by an earlier reference.
*/
Node* Node*
dclname(Sym *s) dclname(Sym *s)
{ {
...@@ -685,42 +559,18 @@ newtype(Sym *s) ...@@ -685,42 +559,18 @@ newtype(Sym *s)
return t; return t;
} }
Type*
oldtype(Sym *s)
{
Type *t;
if(s == S)
return T;
if(s->def == N || s->def->op != OTYPE) {
if(!s->undef)
yyerror("%S is not a type", s);
return T;
}
t = s->def->type;
/*
* If t is lowercase and not in our package
* and this isn't a reference during the parsing
* of import data, complain.
*/
if(pkgimportname == S && !exportname(s->name) && strcmp(s->package, package) != 0)
yyerror("cannot use type %T", t);
return t;
}
/* /*
* type check top level declarations * type check top level declarations
*/ */
void void
dclchecks(void) dclchecks(void)
{ {
Dcl *d; NodeList *l;
for(d=externdcl; d!=D; d=d->forw) { for(l=externdcl; l; l=l->next) {
if(d->op != ONAME) if(l->n->op != ONAME)
continue; continue;
typecheck(&d->dnode, Erv); typecheck(&l->n, Erv);
} }
} }
...@@ -1228,8 +1078,7 @@ funchdr(Node *n) ...@@ -1228,8 +1078,7 @@ funchdr(Node *n)
} }
// change the declaration context from extern to auto // change the declaration context from extern to auto
autodcl = dcl(); autodcl = list1(nod(OXXX, N, N));
autodcl->back = autodcl;
if(funcdepth == 0 && dclcontext != PEXTERN) if(funcdepth == 0 && dclcontext != PEXTERN)
fatal("funchdr: dclcontext"); fatal("funchdr: dclcontext");
...@@ -1312,8 +1161,7 @@ funclit0(Node *t) ...@@ -1312,8 +1161,7 @@ funclit0(Node *t)
funclit = n; funclit = n;
// new declaration context // new declaration context
autodcl = dcl(); autodcl = list1(nod(OEMPTY, N, N));
autodcl->back = autodcl;
typecheck(&t, Etype); typecheck(&t, Etype);
funcargs(t->type); funcargs(t->type);
...@@ -1330,6 +1178,7 @@ funclit1(Node *ntype, NodeList *body) ...@@ -1330,6 +1178,7 @@ funclit1(Node *ntype, NodeList *body)
Iter save; Iter save;
int narg, shift; int narg, shift;
NodeList *args, *l, *in, *out; NodeList *args, *l, *in, *out;
static int closgen;
type = ntype->type; type = ntype->type;
popdcl(); popdcl();
...@@ -1408,8 +1257,7 @@ funclit1(Node *ntype, NodeList *body) ...@@ -1408,8 +1257,7 @@ funclit1(Node *ntype, NodeList *body)
ft->outnamed = type->outnamed; ft->outnamed = type->outnamed;
// declare function. // declare function.
vargen++; snprint(namebuf, sizeof(namebuf), "_f%.3ld·%s", ++closgen, filename);
snprint(namebuf, sizeof(namebuf), "_f%.3ld·%s", vargen, filename);
f = newname(lookup(namebuf)); f = newname(lookup(namebuf));
addvar(f, ft, PFUNC); addvar(f, ft, PFUNC);
f->funcdepth = 0; f->funcdepth = 0;
......
...@@ -8,49 +8,39 @@ ...@@ -8,49 +8,39 @@
void dumpsym(Sym*); void dumpsym(Sym*);
void void
addexportsym(Sym *s) addexportsym(Node *n)
{ {
Dcl *d, *r; exportlist = list(exportlist, n);
d = mal(sizeof(*d));
d->dsym = s;
d->dnode = N;
d->lineno = lineno;
r = exportlist;
d->back = r->back;
r->back->forw = d;
r->back = d;
} }
void void
exportsym(Sym *s) exportsym(Node *n)
{ {
if(s == S) if(n == N || n->sym == S)
return; return;
if(s->export != 0) { if(n->sym->flags & (SymExport|SymPackage)) {
if(s->export != 1) if(n->sym->flags & SymPackage)
yyerror("export/package mismatch: %S", s); yyerror("export/package mismatch: %S", n->sym);
return; return;
} }
s->export = 1; n->sym->flags |= SymExport;
addexportsym(s); addexportsym(n);
} }
void void
packagesym(Sym *s) packagesym(Node *n)
{ {
if(s == S) if(n == N || n->sym == S)
return; return;
if(s->export != 0) { if(n->sym->flags & (SymExport|SymPackage)) {
if(s->export != 2) if(n->sym->flags & SymExport)
yyerror("export/package mismatch: %S", s); yyerror("export/package mismatch: %S", n->sym);
return; return;
} }
s->export = 2; n->sym->flags |= SymPackage;
addexportsym(s); addexportsym(n);
} }
int int
...@@ -65,17 +55,16 @@ exportname(char *s) ...@@ -65,17 +55,16 @@ exportname(char *s)
} }
void void
autoexport(Sym *s) autoexport(Node *n, int ctxt)
{ {
if(s == S) if(n == N || n->sym == S)
return; return;
if(dclcontext != PEXTERN) if((ctxt != PEXTERN && ctxt != PFUNC) || dclcontext != PEXTERN)
return; return;
if(exportname(s->name)) { if(exportname(n->sym->name) || strcmp(n->sym->name, "init") == 0)
exportsym(s); exportsym(n);
} else { else
packagesym(s); packagesym(n);
}
} }
void void
...@@ -187,9 +176,9 @@ dumpsym(Sym *s) ...@@ -187,9 +176,9 @@ dumpsym(Sym *s)
{ {
Type *f, *t; Type *f, *t;
if(s->exported != 0) if(s->flags & SymExported)
return; return;
s->exported = 1; s->flags |= SymExported;
if(s->def == N) { if(s->def == N) {
yyerror("unknown export symbol: %S", s); yyerror("unknown export symbol: %S", s);
...@@ -236,7 +225,7 @@ dumptype(Type *t) ...@@ -236,7 +225,7 @@ dumptype(Type *t)
void void
dumpexport(void) dumpexport(void)
{ {
Dcl *d; NodeList *l;
int32 lno; int32 lno;
lno = lineno; lno = lineno;
...@@ -246,16 +235,16 @@ dumpexport(void) ...@@ -246,16 +235,16 @@ dumpexport(void)
Bprint(bout, " package %s\n", package); Bprint(bout, " package %s\n", package);
for(d=exportlist->forw; d!=D; d=d->forw) { for(l=exportlist; l; l=l->next) {
lineno = d->lineno; lineno = l->n->lineno;
dumpsym(d->dsym); dumpsym(l->n->sym);
} }
Bprint(bout, "\n$$ // local types\n"); Bprint(bout, "\n$$ // local types\n");
for(d=typelist->forw; d!=D; d=d->forw) { for(l=typelist; l; l=l->next) {
lineno = d->lineno; lineno = l->n->lineno;
dumptype(d->dtype); dumptype(l->n->type);
} }
Bprint(bout, "\n$$\n"); Bprint(bout, "\n$$\n");
...@@ -286,10 +275,10 @@ importsym(Sym *s, int op) ...@@ -286,10 +275,10 @@ importsym(Sym *s, int op)
// mark the symbol so it is not reexported // mark the symbol so it is not reexported
if(s->def == N) { if(s->def == N) {
if(exportname(s->name)) if(exportname(s->name))
s->export = 1; s->flags |= SymExport;
else else
s->export = 2; // package scope s->flags |= SymPackage; // package scope
s->imported = 1; s->flags |= SymImported;
} }
return s; return s;
} }
...@@ -332,7 +321,12 @@ importconst(Sym *s, Type *t, Node *n) ...@@ -332,7 +321,12 @@ importconst(Sym *s, Type *t, Node *n)
return; return;
} }
dodclconst(newname(s), n); if(n->op != OLITERAL) {
yyerror("expression must be a constant");
return;
}
n->sym = s;
declare(n, PEXTERN);
if(debug['E']) if(debug['E'])
print("import const %S\n", s); print("import const %S\n", s);
...@@ -341,6 +335,8 @@ importconst(Sym *s, Type *t, Node *n) ...@@ -341,6 +335,8 @@ importconst(Sym *s, Type *t, Node *n)
void void
importvar(Sym *s, Type *t, int ctxt) importvar(Sym *s, Type *t, int ctxt)
{ {
Node *n;
if(!exportname(s->name) && !mypackage(s)) if(!exportname(s->name) && !mypackage(s))
return; return;
...@@ -352,7 +348,9 @@ importvar(Sym *s, Type *t, int ctxt) ...@@ -352,7 +348,9 @@ importvar(Sym *s, Type *t, int ctxt)
s, s->def->type, t); s, s->def->type, t);
} }
checkwidth(t); checkwidth(t);
addvar(newname(s), t, ctxt); n = newname(s);
n->type = t;
declare(n, ctxt);
if(debug['E']) if(debug['E'])
print("import var %S %lT\n", s, t); print("import var %S %lT\n", s, t);
...@@ -393,8 +391,8 @@ importtype(Sym *s, Type *t) ...@@ -393,8 +391,8 @@ importtype(Sym *s, Type *t)
case TFORWINTER: case TFORWINTER:
case TFORWSTRUCT: case TFORWSTRUCT:
// allow re-export in case it gets defined // allow re-export in case it gets defined
s->export = 0; s->flags &= ~(SymExport|SymPackage);
s->imported = 0; s->flags &= ~SymImported;
break; break;
default: default:
checkwidth(n->type); checkwidth(n->type);
......
...@@ -22,7 +22,7 @@ sysfunc(char *name) ...@@ -22,7 +22,7 @@ sysfunc(char *name)
void void
allocparams(void) allocparams(void)
{ {
Dcl *d; NodeList *l;
Node *n; Node *n;
uint32 w; uint32 w;
...@@ -31,14 +31,10 @@ allocparams(void) ...@@ -31,14 +31,10 @@ allocparams(void)
* slots for all automatics. * slots for all automatics.
* allocated starting at -w down. * allocated starting at -w down.
*/ */
for(d=autodcl; d!=D; d=d->forw) { for(l=autodcl; l; l=l->next) {
if(d->op != ONAME) n = l->n;
if(n->op != ONAME || n->class != PAUTO)
continue; continue;
n = d->dnode;
if(n->class != PAUTO)
continue;
typecheck(&n, Erv); typecheck(&n, Erv);
dowidth(n->type); dowidth(n->type);
w = n->type->width; w = n->type->width;
......
...@@ -131,7 +131,6 @@ typedef struct Sym Sym; ...@@ -131,7 +131,6 @@ typedef struct Sym Sym;
typedef struct Node Node; typedef struct Node Node;
typedef struct NodeList NodeList; typedef struct NodeList NodeList;
typedef struct Type Type; typedef struct Type Type;
typedef struct Dcl Dcl;
struct Type struct Type
{ {
...@@ -221,7 +220,7 @@ struct Node ...@@ -221,7 +220,7 @@ struct Node
NodeList* enter; NodeList* enter;
NodeList* exit; NodeList* exit;
NodeList* cvars; // closure params NodeList* cvars; // closure params
Dcl* dcl; // outer autodcl NodeList* dcl; // outer autodcl
// OLITERAL/OREGISTER // OLITERAL/OREGISTER
Val val; Val val;
...@@ -258,44 +257,32 @@ struct NodeList ...@@ -258,44 +257,32 @@ struct NodeList
NodeList* end; NodeList* end;
}; };
struct Sym enum
{ {
ushort block; // blocknumber to catch redeclaration SymExport = 1<<0,
SymPackage = 1<<1,
SymExported = 1<<2,
SymImported = 1<<3,
SymUniq = 1<<4,
SymSiggen = 1<<5,
};
uchar undef; // a diagnostic has been generated struct Sym
uchar export; // marked as export {
uchar exported; // exported ushort lexical;
uchar imported; // imported uchar flags;
uchar sym; // huffman encoding in object file uchar sym; // huffman encoding in object file
uchar uniq; // imbedded field name first found Sym* link;
uchar siggen; // signature generated
// saved and restored by dcopy
char* package; // package name char* package; // package name
char* name; // variable name char* name; // variable name
Node* def; // definition: ONAME OTYPE OPACK or OLITERAL Node* def; // definition: ONAME OTYPE OPACK or OLITERAL
vlong offset; // stack location if automatic int32 block; // blocknumber to catch redeclaration
int32 lexical;
int32 vargen; // unique variable number
int32 lastlineno; // last declaration for diagnostic int32 lastlineno; // last declaration for diagnostic
Sym* link;
}; };
#define S ((Sym*)0) #define S ((Sym*)0)
struct Dcl
{
uchar op;
ushort block;
int32 lineno;
Sym* dsym; // for printing only
Node* dnode; // oname
Type* dtype; // otype
Dcl* forw;
Dcl* back; // sentinel has pointer to last
};
#define D ((Dcl*)0)
typedef struct Iter Iter; typedef struct Iter Iter;
struct Iter struct Iter
{ {
...@@ -636,14 +623,11 @@ EXTERN Mpint* maxintval[NTYPE]; ...@@ -636,14 +623,11 @@ EXTERN Mpint* maxintval[NTYPE];
EXTERN Mpflt* minfltval[NTYPE]; EXTERN Mpflt* minfltval[NTYPE];
EXTERN Mpflt* maxfltval[NTYPE]; EXTERN Mpflt* maxfltval[NTYPE];
EXTERN Dcl* autodcl; EXTERN NodeList* autodcl;
EXTERN Dcl* paramdcl; EXTERN NodeList* externdcl;
EXTERN Dcl* externdcl; EXTERN NodeList* exportlist;
EXTERN Dcl* exportlist; EXTERN NodeList* typelist;
EXTERN Dcl* signatlist;
EXTERN Dcl* typelist;
EXTERN int dclcontext; // PEXTERN/PAUTO EXTERN int dclcontext; // PEXTERN/PAUTO
EXTERN int importflag;
EXTERN int inimportsys; EXTERN int inimportsys;
EXTERN int initflag; // compiling the init fn EXTERN int initflag; // compiling the init fn
EXTERN int statuniqgen; // name generator for static temps EXTERN int statuniqgen; // name generator for static temps
...@@ -652,13 +636,11 @@ EXTERN int loophack; ...@@ -652,13 +636,11 @@ EXTERN int loophack;
EXTERN uint32 iota; EXTERN uint32 iota;
EXTERN NodeList* lastconst; EXTERN NodeList* lastconst;
EXTERN Node* lasttype; EXTERN Node* lasttype;
EXTERN int32 vargen;
EXTERN int32 exportgen;
EXTERN int32 maxarg; EXTERN int32 maxarg;
EXTERN int32 stksize; // stack size for current frame EXTERN int32 stksize; // stack size for current frame
EXTERN int32 initstksize; // stack size for init function EXTERN int32 initstksize; // stack size for init function
EXTERN ushort blockgen; // max block number EXTERN int32 blockgen; // max block number
EXTERN ushort block; // current block number EXTERN int32 block; // current block number
EXTERN int hasdefer; // flag that curfn has defer statetment EXTERN int hasdefer; // flag that curfn has defer statetment
EXTERN Node* curfn; EXTERN Node* curfn;
...@@ -666,8 +648,6 @@ EXTERN Node* curfn; ...@@ -666,8 +648,6 @@ EXTERN Node* curfn;
EXTERN int maxround; EXTERN int maxround;
EXTERN int widthptr; EXTERN int widthptr;
EXTERN Node* retnil;
EXTERN Node* typeswvar; EXTERN Node* typeswvar;
EXTERN char* structpkg; EXTERN char* structpkg;
...@@ -788,7 +768,6 @@ int32 setlineno(Node*); ...@@ -788,7 +768,6 @@ int32 setlineno(Node*);
Node* nod(int, Node*, Node*); Node* nod(int, Node*, Node*);
Node* nodlit(Val); Node* nodlit(Val);
Type* typ(int); Type* typ(int);
Dcl* dcl(void);
int algtype(Type*); int algtype(Type*);
void dodump(Node*, int); void dodump(Node*, int);
void dump(char*, Node*); void dump(char*, Node*);
...@@ -878,10 +857,10 @@ int simsimtype(Type*); ...@@ -878,10 +857,10 @@ int simsimtype(Type*);
/* /*
* dcl.c * dcl.c
*/ */
void declare(Node*, int);
void dodclvar(Node*, Type*, NodeList**); void dodclvar(Node*, Type*, NodeList**);
Type* dodcltype(Type*); Type* dodcltype(Type*);
void updatetype(Type*, Type*); void updatetype(Type*, Type*);
void dodclconst(Node*, Node*);
void defaultlit(Node**, Type*); void defaultlit(Node**, Type*);
void defaultlit2(Node**, Node**, int); void defaultlit2(Node**, Node**, int);
int structcount(Type*); int structcount(Type*);
...@@ -941,10 +920,10 @@ NodeList* initfix(NodeList*); ...@@ -941,10 +920,10 @@ NodeList* initfix(NodeList*);
* export.c * export.c
*/ */
void renameimports(void); void renameimports(void);
void autoexport(Sym*); void autoexport(Node*, int);
int exportname(char*); int exportname(char*);
void exportsym(Sym*); void exportsym(Node*);
void packagesym(Sym*); void packagesym(Node*);
void dumpe(Sym*); void dumpe(Sym*);
void dumpexport(void); void dumpexport(void);
void dumpexporttype(Sym*); void dumpexporttype(Sym*);
...@@ -1065,7 +1044,6 @@ typedef struct Plist Plist; ...@@ -1065,7 +1044,6 @@ typedef struct Plist Plist;
struct Plist struct Plist
{ {
Node* name; Node* name;
Dcl* locals;
Prog* firstpc; Prog* firstpc;
int recur; int recur;
Plist* link; Plist* link;
......
...@@ -274,7 +274,7 @@ xdcl: ...@@ -274,7 +274,7 @@ xdcl:
| xfndcl | xfndcl
{ {
if($1 != N && $1->nname != N && $1->type->thistuple == 0) if($1 != N && $1->nname != N && $1->type->thistuple == 0)
autoexport($1->nname->sym); autoexport($1->nname, dclcontext);
$$ = nil; $$ = nil;
} }
| ';' | ';'
...@@ -1643,7 +1643,12 @@ hidden_type1: ...@@ -1643,7 +1643,12 @@ hidden_type1:
} }
| LNAME | LNAME
{ {
$$ = oldtype($1); // predefined name like uint8
if($1->def == N || $1->def->op != OTYPE) {
yyerror("%S is not a type", $1);
$$ = T;
} else
$$ = $1->def->type;
} }
| '[' ']' hidden_type | '[' ']' hidden_type
{ {
......
...@@ -178,7 +178,7 @@ fninit(NodeList *n) ...@@ -178,7 +178,7 @@ fninit(NodeList *n)
a = nod(ORETURN, N, N); a = nod(ORETURN, N, N);
r = list(r, a); r = list(r, a);
exportsym(fn->nname->sym); exportsym(fn->nname);
fn->nbody = r; fn->nbody = r;
//dump("b", fn); //dump("b", fn);
......
...@@ -84,16 +84,8 @@ main(int argc, char *argv[]) ...@@ -84,16 +84,8 @@ main(int argc, char *argv[])
curio.peekc = 0; curio.peekc = 0;
curio.peekc1 = 0; curio.peekc1 = 0;
externdcl = mal(sizeof(*externdcl));
externdcl->back = externdcl;
dclcontext = PEXTERN; dclcontext = PEXTERN;
exportlist = mal(sizeof(*exportlist));
exportlist->back = exportlist;
typelist = mal(sizeof(*typelist));
typelist->back = typelist;
nerrors = 0; nerrors = 0;
yyparse(); yyparse();
runifacechecks(); runifacechecks();
......
...@@ -38,27 +38,23 @@ dumpobj(void) ...@@ -38,27 +38,23 @@ dumpobj(void)
void void
dumpglobls(void) dumpglobls(void)
{ {
Dcl *d;
Sym *s;
Node *n; Node *n;
NodeList *l;
// add globals // add globals
for(d=externdcl; d!=D; d=d->forw) { for(l=externdcl; l; l=l->next) {
if(d->op != ONAME) n = l->n;
if(n->op != ONAME)
continue; continue;
s = d->dsym; if(n->type == T)
if(s == S) fatal("external %#N nil type\n", n);
fatal("external nil");
n = d->dnode;
if(n == N || n->type == T)
fatal("external %S nil\n", s);
if(n->class == PFUNC) if(n->class == PFUNC)
continue; continue;
dowidth(n->type); dowidth(n->type);
ggloblnod(s->def, n->type->width);
// TODO(rsc): why is this not s/n->sym->def/n/ ?
ggloblnod(n->sym->def, n->type->width);
} }
} }
......
...@@ -8,7 +8,8 @@ ...@@ -8,7 +8,8 @@
* runtime interface and reflection data structures * runtime interface and reflection data structures
*/ */
static Sym* dtypesym(Type*); static NodeList* signatlist;
static Sym* dtypesym(Type*);
static int static int
sigcmp(Sig *a, Sig *b) sigcmp(Sig *a, Sig *b)
...@@ -181,8 +182,8 @@ methods(Type *t) ...@@ -181,8 +182,8 @@ methods(Type *t)
a->tsym = methodsym(method, t); a->tsym = methodsym(method, t);
a->type = methodfunc(f); a->type = methodfunc(f);
if(!a->isym->siggen) { if(!(a->isym->flags & SymSiggen)) {
a->isym->siggen = 1; a->isym->flags |= SymSiggen;
if(!eqtype(this, it)) { if(!eqtype(this, it)) {
if(oldlist == nil) if(oldlist == nil)
oldlist = pc; oldlist = pc;
...@@ -198,8 +199,8 @@ methods(Type *t) ...@@ -198,8 +199,8 @@ methods(Type *t)
} }
} }
if(!a->tsym->siggen) { if(!(a->tsym->flags & SymSiggen)) {
a->tsym->siggen = 1; a->tsym->flags |= SymSiggen;
if(!eqtype(this, t)) { if(!eqtype(this, t)) {
if(oldlist == nil) if(oldlist == nil)
oldlist = pc; oldlist = pc;
...@@ -447,7 +448,6 @@ typename(Type *t) ...@@ -447,7 +448,6 @@ typename(Type *t)
{ {
Sym *s; Sym *s;
Node *n; Node *n;
Dcl *d;
s = typesym(t); s = typesym(t);
if(s->def == N) { if(s->def == N) {
...@@ -460,13 +460,7 @@ typename(Type *t) ...@@ -460,13 +460,7 @@ typename(Type *t)
n->xoffset = 0; n->xoffset = 0;
s->def = n; s->def = n;
// copy to signatlist signatlist = list(signatlist, typenod(t));
d = dcl();
d->dsym = s;
d->dtype = t;
d->op = OTYPE;
d->forw = signatlist;
signatlist = d;
} }
n = nod(OADDR, s->def, N); n = nod(OADDR, s->def, N);
...@@ -485,9 +479,9 @@ dtypesym(Type *t) ...@@ -485,9 +479,9 @@ dtypesym(Type *t)
Type *t1; Type *t1;
s = typesym(t); s = typesym(t);
if(s->siggen) if(s->flags & SymSiggen)
return s; return s;
s->siggen = 1; s->flags |= SymSiggen;
// special case (look for runtime below): // special case (look for runtime below):
// when compiling package runtime, // when compiling package runtime,
...@@ -652,27 +646,24 @@ void ...@@ -652,27 +646,24 @@ void
dumptypestructs(void) dumptypestructs(void)
{ {
int i; int i;
Dcl *d, *x; NodeList *l;
Node *n;
Type *t; Type *t;
// copy types from externdcl list to signatlist // copy types from externdcl list to signatlist
for(d=externdcl; d!=D; d=d->forw) { for(l=externdcl; l; l=l->next) {
if(d->op != OTYPE) n = l->n;
if(n->op != OTYPE)
continue; continue;
t = d->dtype; signatlist = list(signatlist, n);
x = mal(sizeof(*x));
x->op = OTYPE;
x->dtype = t;
x->forw = signatlist;
x->block = 0;
signatlist = x;
} }
// process signatlist // process signatlist
for(d=signatlist; d!=D; d=d->forw) { for(l=signatlist; l; l=l->next) {
if(d->op != OTYPE) n = l->n;
if(n->op != OTYPE)
continue; continue;
t = d->dtype; t = n->type;
dtypesym(t); dtypesym(t);
if(t->sym && !isptr[t->etype]) if(t->sym && !isptr[t->etype])
dtypesym(ptrto(t)); dtypesym(ptrto(t));
......
...@@ -307,16 +307,6 @@ remal(void *p, int32 on, int32 n) ...@@ -307,16 +307,6 @@ remal(void *p, int32 on, int32 n)
return p; return p;
} }
Dcl*
dcl(void)
{
Dcl *d;
d = mal(sizeof(*d));
d->lineno = lineno;
return d;
}
extern int yychar; extern int yychar;
Node* Node*
nod(int op, Node *nleft, Node *nright) nod(int op, Node *nleft, Node *nright)
...@@ -965,9 +955,9 @@ Tpretty(Fmt *fp, Type *t) ...@@ -965,9 +955,9 @@ Tpretty(Fmt *fp, Type *t)
fmtprint(fp, "%lS", s); fmtprint(fp, "%lS", s);
if(strcmp(s->package, package) != 0) if(strcmp(s->package, package) != 0)
return 0; return 0;
if(s->imported) if(s->flags & SymImported)
return 0; return 0;
if(t->vargen || !s->export) { if(t->vargen || !(s->flags & (SymExport|SymPackage))) {
fmtprint(fp, "·%s", filename); fmtprint(fp, "·%s", filename);
if(t->vargen) if(t->vargen)
fmtprint(fp, "·%d", t->vargen); fmtprint(fp, "·%d", t->vargen);
...@@ -1244,7 +1234,7 @@ Nconv(Fmt *fp) ...@@ -1244,7 +1234,7 @@ Nconv(Fmt *fp)
break; break;
} }
fmtprint(fp, "%O-%S G%ld%J", n->op, fmtprint(fp, "%O-%S G%ld%J", n->op,
n->sym, n->sym->vargen, n); n->sym, n->vargen, n);
goto ptyp; goto ptyp;
case OREGISTER: case OREGISTER:
...@@ -1284,7 +1274,7 @@ Nconv(Fmt *fp) ...@@ -1284,7 +1274,7 @@ Nconv(Fmt *fp)
break; break;
} }
if(n->sym != S) if(n->sym != S)
fmtprint(fp, " %S G%ld", n->sym, n->sym->vargen); fmtprint(fp, " %S G%ld", n->sym, n->vargen);
ptyp: ptyp:
if(n->type != T) if(n->type != T)
...@@ -1935,30 +1925,32 @@ void ...@@ -1935,30 +1925,32 @@ void
frame(int context) frame(int context)
{ {
char *p; char *p;
Dcl *d; NodeList *l;
Node *n;
int flag; int flag;
p = "stack"; p = "stack";
d = autodcl; l = autodcl;
if(context) { if(context) {
p = "external"; p = "external";
d = externdcl; l = externdcl;
} }
flag = 1; flag = 1;
for(; d!=D; d=d->forw) { for(; l; l=l->next) {
switch(d->op) { n = l->n;
switch(n->op) {
case ONAME: case ONAME:
if(flag) if(flag)
print("--- %s frame ---\n", p); print("--- %s frame ---\n", p);
print("%O %S G%ld T\n", d->op, d->dsym, d->dnode->vargen, d->dnode->type); print("%O %S G%ld T\n", n->op, n->sym, n->vargen, n->type);
flag = 0; flag = 0;
break; break;
case OTYPE: case OTYPE:
if(flag) if(flag)
print("--- %s frame ---\n", p); print("--- %s frame ---\n", p);
print("%O %T\n", d->op, d->dnode); print("%O %T\n", n->op, n->type);
flag = 0; flag = 0;
break; break;
} }
...@@ -2520,9 +2512,9 @@ expand0(Type *t, int followptr) ...@@ -2520,9 +2512,9 @@ expand0(Type *t, int followptr)
for(f=u->type; f!=T; f=f->down) { for(f=u->type; f!=T; f=f->down) {
if(!exportname(f->sym->name) && strcmp(f->sym->package, package) != 0) if(!exportname(f->sym->name) && strcmp(f->sym->package, package) != 0)
continue; continue;
if(f->sym->uniq) if(f->sym->flags & SymUniq)
continue; continue;
f->sym->uniq = 1; f->sym->flags |= SymUniq;
sl = mal(sizeof(*sl)); sl = mal(sizeof(*sl));
sl->field = f; sl->field = f;
sl->link = slist; sl->link = slist;
...@@ -2537,9 +2529,9 @@ expand0(Type *t, int followptr) ...@@ -2537,9 +2529,9 @@ expand0(Type *t, int followptr)
for(f=u->method; f!=T; f=f->down) { for(f=u->method; f!=T; f=f->down) {
if(!exportname(f->sym->name) && strcmp(f->sym->package, package) != 0) if(!exportname(f->sym->name) && strcmp(f->sym->package, package) != 0)
continue; continue;
if(f->sym->uniq) if(f->sym->flags & SymUniq)
continue; continue;
f->sym->uniq = 1; f->sym->flags |= SymUniq;
sl = mal(sizeof(*sl)); sl = mal(sizeof(*sl));
sl->field = f; sl->field = f;
sl->link = slist; sl->link = slist;
...@@ -2601,7 +2593,7 @@ expandmeth(Sym *s, Type *t) ...@@ -2601,7 +2593,7 @@ expandmeth(Sym *s, Type *t)
// check each method to be uniquely reachable // check each method to be uniquely reachable
for(sl=slist; sl!=nil; sl=sl->link) { for(sl=slist; sl!=nil; sl=sl->link) {
sl->field->sym->uniq = 0; sl->field->sym->flags &= ~SymUniq;
for(d=0; d<nelem(dotlist); d++) { for(d=0; d<nelem(dotlist); d++) {
c = adddot1(sl->field->sym, t, d, &f); c = adddot1(sl->field->sym, t, d, &f);
if(c == 0) if(c == 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment