1
0
mirror of https://github.com/golang/go synced 2024-11-24 21:10:04 -07:00

gc: unify stack frame layout

allocparams + tempname + compactframe
all knew about how to place stack variables.

Now only compactframe, renamed to allocauto,
does the work.  Until the last minute, each PAUTO
variable is in its own space and has xoffset == 0.

This might break 5g.  I get failures in concurrent
code running under qemu and I can't tell whether
it's 5g's fault or qemu's.  We'll see what the real
ARM builders say.

R=ken2
CC=golang-dev
https://golang.org/cl/4973057
This commit is contained in:
Russ Cox 2011-09-06 10:24:21 -04:00
parent 37f390aa20
commit 5ddf6255a1
12 changed files with 62 additions and 137 deletions

View File

@ -1273,7 +1273,6 @@ naddr(Node *n, Addr *a, int canemitcode)
a->etype = simtype[n->type->etype]; a->etype = simtype[n->type->etype];
a->width = n->type->width; a->width = n->type->width;
} }
a->pun = n->pun;
a->offset = n->xoffset; a->offset = n->xoffset;
a->sym = n->sym; a->sym = n->sym;
if(a->sym == S) if(a->sym == S)

View File

@ -114,19 +114,19 @@ setaddrs(Bits bit)
{ {
int i, n; int i, n;
Var *v; Var *v;
Sym *s; Node *node;
while(bany(&bit)) { while(bany(&bit)) {
// convert each bit to a variable // convert each bit to a variable
i = bnum(bit); i = bnum(bit);
s = var[i].sym; node = var[i].node;
n = var[i].name; n = var[i].name;
bit.b[i/32] &= ~(1L<<(i%32)); bit.b[i/32] &= ~(1L<<(i%32));
// disable all pieces of that variable // disable all pieces of that variable
for(i=0; i<nvar; i++) { for(i=0; i<nvar; i++) {
v = var+i; v = var+i;
if(v->sym == s && v->name == n) if(v->node == node && v->name == n)
v->addr = 2; v->addr = 2;
} }
} }
@ -204,7 +204,7 @@ regopt(Prog *firstp)
nvar = NREGVAR; nvar = NREGVAR;
memset(var, 0, NREGVAR*sizeof var[0]); memset(var, 0, NREGVAR*sizeof var[0]);
for(i=0; i<NREGVAR; i++) for(i=0; i<NREGVAR; i++)
var[i].sym = lookup(regname[i]); var[i].node = newname(lookup(regname[i]));
regbits = RtoB(REGSP)|RtoB(REGLINK)|RtoB(REGPC); regbits = RtoB(REGSP)|RtoB(REGLINK)|RtoB(REGPC);
for(z=0; z<BITS; z++) { for(z=0; z<BITS; z++) {
@ -752,9 +752,9 @@ addmove(Reg *r, int bn, int rn, int f)
v = var + bn; v = var + bn;
a = &p1->to; a = &p1->to;
a->sym = v->sym;
a->name = v->name; a->name = v->name;
a->node = v->node; a->node = v->node;
a->sym = v->node->sym;
a->offset = v->offset; a->offset = v->offset;
a->etype = v->etype; a->etype = v->etype;
a->type = D_OREG; a->type = D_OREG;
@ -840,7 +840,7 @@ mkvar(Reg *r, Adr *a)
int i, t, n, et, z, w, flag; int i, t, n, et, z, w, flag;
int32 o; int32 o;
Bits bit; Bits bit;
Sym *s; Node *node;
// mark registers used // mark registers used
t = a->type; t = a->type;
@ -910,10 +910,11 @@ mkvar(Reg *r, Adr *a)
break; break;
} }
s = a->sym; node = a->node;
if(s == S) if(node == N || node->op != ONAME || node->orig != N)
goto none; goto none;
if(s->name[0] == '.') node = node->orig;
if(node->sym->name[0] == '.')
goto none; goto none;
et = a->etype; et = a->etype;
o = a->offset; o = a->offset;
@ -921,7 +922,7 @@ mkvar(Reg *r, Adr *a)
for(i=0; i<nvar; i++) { for(i=0; i<nvar; i++) {
v = var+i; v = var+i;
if(v->sym == s && v->name == n) { if(v->node == node && v->name == n) {
if(v->offset == o) if(v->offset == o)
if(v->etype == et) if(v->etype == et)
if(v->width == w) if(v->width == w)
@ -945,7 +946,7 @@ mkvar(Reg *r, Adr *a)
} }
if(nvar >= NVAR) { if(nvar >= NVAR) {
if(debug['w'] > 1 && s) if(debug['w'] > 1 && node)
fatal("variable not optimized: %D", a); fatal("variable not optimized: %D", a);
goto none; goto none;
} }
@ -954,17 +955,16 @@ mkvar(Reg *r, Adr *a)
nvar++; nvar++;
//print("var %d %E %D %S\n", i, et, a, s); //print("var %d %E %D %S\n", i, et, a, s);
v = var+i; v = var+i;
v->sym = s;
v->offset = o; v->offset = o;
v->name = n; v->name = n;
// v->gotype = a->gotype; // v->gotype = a->gotype;
v->etype = et; v->etype = et;
v->width = w; v->width = w;
v->addr = flag; // funny punning v->addr = flag; // funny punning
v->node = a->node; v->node = node;
if(debug['R']) if(debug['R'])
print("bit=%2d et=%2d w=%d+%d %S %D flag=%d\n", i, et, o, w, s, a, v->addr); print("bit=%2d et=%2d w=%d+%d %#N %D flag=%d\n", i, et, o, w, node, a, v->addr);
bit = blsh(i); bit = blsh(i);
if(n == D_EXTERN || n == D_STATIC) if(n == D_EXTERN || n == D_STATIC)

View File

@ -1129,7 +1129,6 @@ naddr(Node *n, Addr *a, int canemitcode)
a->width = n->type->width; a->width = n->type->width;
a->gotype = ngotype(n); a->gotype = ngotype(n);
} }
a->pun = n->pun;
a->offset = n->xoffset; a->offset = n->xoffset;
a->sym = n->sym; a->sym = n->sym;
if(a->sym == S) if(a->sym == S)

View File

@ -98,19 +98,19 @@ setaddrs(Bits bit)
{ {
int i, n; int i, n;
Var *v; Var *v;
Sym *s; Node *node;
while(bany(&bit)) { while(bany(&bit)) {
// convert each bit to a variable // convert each bit to a variable
i = bnum(bit); i = bnum(bit);
s = var[i].sym; node = var[i].node;
n = var[i].name; n = var[i].name;
bit.b[i/32] &= ~(1L<<(i%32)); bit.b[i/32] &= ~(1L<<(i%32));
// disable all pieces of that variable // disable all pieces of that variable
for(i=0; i<nvar; i++) { for(i=0; i<nvar; i++) {
v = var+i; v = var+i;
if(v->sym == s && v->name == n) if(v->node == node && v->name == n)
v->addr = 2; v->addr = 2;
} }
} }
@ -188,7 +188,7 @@ regopt(Prog *firstp)
nvar = NREGVAR; nvar = NREGVAR;
memset(var, 0, NREGVAR*sizeof var[0]); memset(var, 0, NREGVAR*sizeof var[0]);
for(i=0; i<NREGVAR; i++) for(i=0; i<NREGVAR; i++)
var[i].sym = lookup(regname[i]); var[i].node = newname(lookup(regname[i]));
regbits = RtoB(D_SP); regbits = RtoB(D_SP);
for(z=0; z<BITS; z++) { for(z=0; z<BITS; z++) {
@ -831,12 +831,12 @@ addmove(Reg *r, int bn, int rn, int f)
v = var + bn; v = var + bn;
a = &p1->to; a = &p1->to;
a->sym = v->sym;
a->offset = v->offset; a->offset = v->offset;
a->etype = v->etype; a->etype = v->etype;
a->type = v->name; a->type = v->name;
a->gotype = v->gotype; a->gotype = v->gotype;
a->node = v->node; a->node = v->node;
a->sym = v->node->sym;
// need to clean this up with wptr and // need to clean this up with wptr and
// some of the defaults // some of the defaults
@ -932,7 +932,7 @@ mkvar(Reg *r, Adr *a)
uint32 regu; uint32 regu;
int32 o; int32 o;
Bits bit; Bits bit;
Sym *s; Node *node;
/* /*
* mark registers used * mark registers used
@ -968,10 +968,11 @@ mkvar(Reg *r, Adr *a)
n = t; n = t;
break; break;
} }
s = a->sym; node = a->node;
if(s == S) if(node == N || node->op != ONAME || node->orig != N)
goto none; goto none;
if(s->name[0] == '.') node = node->orig;
if(node->sym->name[0] == '.')
goto none; goto none;
et = a->etype; et = a->etype;
o = a->offset; o = a->offset;
@ -980,7 +981,7 @@ mkvar(Reg *r, Adr *a)
flag = 0; flag = 0;
for(i=0; i<nvar; i++) { for(i=0; i<nvar; i++) {
v = var+i; v = var+i;
if(v->sym == s && v->name == n) { if(v->node == node && v->name == n) {
if(v->offset == o) if(v->offset == o)
if(v->etype == et) if(v->etype == et)
if(v->width == w) if(v->width == w)
@ -994,11 +995,6 @@ mkvar(Reg *r, Adr *a)
} }
} }
} }
if(a->pun) {
// print("disable pun %s\n", s->name);
flag = 1;
}
switch(et) { switch(et) {
case 0: case 0:
case TFUNC: case TFUNC:
@ -1006,25 +1002,24 @@ mkvar(Reg *r, Adr *a)
} }
if(nvar >= NVAR) { if(nvar >= NVAR) {
if(debug['w'] > 1 && s) if(debug['w'] > 1 && node != N)
fatal("variable not optimized: %D", a); fatal("variable not optimized: %#N", node);
goto none; goto none;
} }
i = nvar; i = nvar;
nvar++; nvar++;
v = var+i; v = var+i;
v->sym = s;
v->offset = o; v->offset = o;
v->name = n; v->name = n;
v->gotype = a->gotype; v->gotype = a->gotype;
v->etype = et; v->etype = et;
v->width = w; v->width = w;
v->addr = flag; // funny punning v->addr = flag; // funny punning
v->node = a->node; v->node = node;
if(debug['R']) if(debug['R'])
print("bit=%2d et=%2d w=%d %S %D\n", i, et, w, s, a); print("bit=%2d et=%2d w=%d %#N %D\n", i, et, w, node, a);
ostats.nvar++; ostats.nvar++;
bit = blsh(i); bit = blsh(i);

View File

@ -1838,7 +1838,6 @@ naddr(Node *n, Addr *a, int canemitcode)
a->width = n->type->width; a->width = n->type->width;
a->gotype = ngotype(n); a->gotype = ngotype(n);
} }
a->pun = n->pun;
a->offset = n->xoffset; a->offset = n->xoffset;
a->sym = n->sym; a->sym = n->sym;
if(a->sym == S) if(a->sym == S)

View File

@ -98,19 +98,19 @@ setaddrs(Bits bit)
{ {
int i, n; int i, n;
Var *v; Var *v;
Sym *s; Node *node;
while(bany(&bit)) { while(bany(&bit)) {
// convert each bit to a variable // convert each bit to a variable
i = bnum(bit); i = bnum(bit);
s = var[i].sym; node = var[i].node;
n = var[i].name; n = var[i].name;
bit.b[i/32] &= ~(1L<<(i%32)); bit.b[i/32] &= ~(1L<<(i%32));
// disable all pieces of that variable // disable all pieces of that variable
for(i=0; i<nvar; i++) { for(i=0; i<nvar; i++) {
v = var+i; v = var+i;
if(v->sym == s && v->name == n) if(v->node == node && v->name == n)
v->addr = 2; v->addr = 2;
} }
} }
@ -155,7 +155,7 @@ regopt(Prog *firstp)
nvar = NREGVAR; nvar = NREGVAR;
memset(var, 0, NREGVAR*sizeof var[0]); memset(var, 0, NREGVAR*sizeof var[0]);
for(i=0; i<NREGVAR; i++) for(i=0; i<NREGVAR; i++)
var[i].sym = lookup(regname[i]); var[i].node = newname(lookup(regname[i]));
regbits = RtoB(D_SP); regbits = RtoB(D_SP);
for(z=0; z<BITS; z++) { for(z=0; z<BITS; z++) {
@ -725,12 +725,12 @@ addmove(Reg *r, int bn, int rn, int f)
v = var + bn; v = var + bn;
a = &p1->to; a = &p1->to;
a->sym = v->sym;
a->offset = v->offset; a->offset = v->offset;
a->etype = v->etype; a->etype = v->etype;
a->type = v->name; a->type = v->name;
a->gotype = v->gotype; a->gotype = v->gotype;
a->node = v->node; a->node = v->node;
a->sym = v->node->sym;
// need to clean this up with wptr and // need to clean this up with wptr and
// some of the defaults // some of the defaults
@ -810,7 +810,7 @@ mkvar(Reg *r, Adr *a)
int i, t, n, et, z, w, flag, regu; int i, t, n, et, z, w, flag, regu;
int32 o; int32 o;
Bits bit; Bits bit;
Sym *s; Node *node;
/* /*
* mark registers used * mark registers used
@ -847,10 +847,11 @@ mkvar(Reg *r, Adr *a)
break; break;
} }
s = a->sym; node = a->node;
if(s == S) if(node == N || node->op != ONAME || node->orig != N)
goto none; goto none;
if(s->name[0] == '.') node = node->orig;
if(node->sym->name[0] == '.')
goto none; goto none;
et = a->etype; et = a->etype;
o = a->offset; o = a->offset;
@ -859,7 +860,7 @@ mkvar(Reg *r, Adr *a)
flag = 0; flag = 0;
for(i=0; i<nvar; i++) { for(i=0; i<nvar; i++) {
v = var+i; v = var+i;
if(v->sym == s && v->name == n) { if(v->node == node && v->name == n) {
if(v->offset == o) if(v->offset == o)
if(v->etype == et) if(v->etype == et)
if(v->width == w) if(v->width == w)
@ -868,7 +869,7 @@ mkvar(Reg *r, Adr *a)
// if they overlap, disable both // if they overlap, disable both
if(overlap(v->offset, v->width, o, w)) { if(overlap(v->offset, v->width, o, w)) {
if(debug['R']) if(debug['R'])
print("disable %s\n", v->sym->name); print("disable %s\n", node->sym->name);
v->addr = 1; v->addr = 1;
flag = 1; flag = 1;
} }
@ -882,7 +883,7 @@ mkvar(Reg *r, Adr *a)
} }
if(nvar >= NVAR) { if(nvar >= NVAR) {
if(debug['w'] > 1 && s) if(debug['w'] > 1 && node != N)
fatal("variable not optimized: %D", a); fatal("variable not optimized: %D", a);
goto none; goto none;
} }
@ -890,17 +891,16 @@ mkvar(Reg *r, Adr *a)
i = nvar; i = nvar;
nvar++; nvar++;
v = var+i; v = var+i;
v->sym = s;
v->offset = o; v->offset = o;
v->name = n; v->name = n;
v->gotype = a->gotype; v->gotype = a->gotype;
v->etype = et; v->etype = et;
v->width = w; v->width = w;
v->addr = flag; // funny punning v->addr = flag; // funny punning
v->node = a->node; v->node = node;
if(debug['R']) if(debug['R'])
print("bit=%2d et=%2d w=%d+%d %S %D flag=%d\n", i, et, o, w, s, a, v->addr); print("bit=%2d et=%2d w=%d+%d %#N %D flag=%d\n", i, et, o, w, node, a, v->addr);
ostats.nvar++; ostats.nvar++;
bit = blsh(i); bit = blsh(i);

View File

@ -150,10 +150,10 @@ Qconv(Fmt *fp)
first = 0; first = 0;
else else
fmtprint(fp, " "); fmtprint(fp, " ");
if(var[i].sym == S) if(var[i].node == N || var[i].node->sym == S)
fmtprint(fp, "$%lld", var[i].offset); fmtprint(fp, "$%lld", var[i].offset);
else { else {
fmtprint(fp, var[i].sym->name); fmtprint(fp, var[i].node->sym->name);
if(var[i].offset != 0) if(var[i].offset != 0)
fmtprint(fp, "%+lld", (vlong)var[i].offset); fmtprint(fp, "%+lld", (vlong)var[i].offset);
} }

View File

@ -192,7 +192,7 @@ declare(Node *n, int ctxt)
n->curfn = curfn; n->curfn = curfn;
} }
if(ctxt == PAUTO) if(ctxt == PAUTO)
n->xoffset = BADWIDTH; n->xoffset = 0;
if(s->block == block) if(s->block == block)
redeclare(s, "in this block"); redeclare(s, "in this block");

View File

@ -28,52 +28,6 @@ sysfunc(char *name)
return n; return n;
} }
void
allocparams(void)
{
NodeList *l;
Node *n;
uint32 w;
Sym *s;
int lno;
if(stksize < 0)
fatal("allocparams not during code generation");
/*
* allocate (set xoffset) the stack
* slots for all automatics.
* allocated starting at -w down.
*/
lno = lineno;
for(l=curfn->dcl; l; l=l->next) {
n = l->n;
if(n->op == ONAME && n->class == PHEAP-1) {
// heap address variable; finish the job
// started in addrescapes.
s = n->sym;
tempname(n, n->type);
n->sym = s;
}
if(n->op != ONAME || n->class != PAUTO)
continue;
if(n->xoffset != BADWIDTH)
continue;
if(n->type == T)
continue;
dowidth(n->type);
w = n->type->width;
if(w >= MAXWIDTH)
fatal("bad width");
stksize += w;
stksize = rnd(stksize, n->type->align);
if(thechar == '5')
stksize = rnd(stksize, widthptr);
n->xoffset = -stksize;
}
lineno = lno;
}
/* /*
* the address of n has been taken and might be used after * the address of n has been taken and might be used after
* the current function returns. mark any local vars * the current function returns. mark any local vars
@ -83,6 +37,8 @@ void
addrescapes(Node *n) addrescapes(Node *n)
{ {
char buf[100]; char buf[100];
Node *oldfn;
switch(n->op) { switch(n->op) {
default: default:
// probably a type error already. // probably a type error already.
@ -129,18 +85,17 @@ addrescapes(Node *n)
n->xoffset = 0; n->xoffset = 0;
// create stack variable to hold pointer to heap // create stack variable to hold pointer to heap
n->heapaddr = nod(ONAME, N, N); oldfn = curfn;
n->heapaddr->type = ptrto(n->type); curfn = n->curfn;
n->heapaddr = temp(ptrto(n->type));
snprint(buf, sizeof buf, "&%S", n->sym); snprint(buf, sizeof buf, "&%S", n->sym);
n->heapaddr->sym = lookup(buf); n->heapaddr->sym = lookup(buf);
n->heapaddr->class = PHEAP-1; // defer tempname to allocparams n->heapaddr->orig->sym = n->heapaddr->sym;
n->heapaddr->ullman = 1;
n->curfn->dcl = list(n->curfn->dcl, n->heapaddr);
if(!debug['s']) if(!debug['s'])
n->esc = EscHeap; n->esc = EscHeap;
if(debug['m']) if(debug['m'])
print("%L: moved to heap: %hN\n", n->lineno, n); print("%L: moved to heap: %hN\n", n->lineno, n);
curfn = oldfn;
break; break;
} }
break; break;
@ -687,15 +642,12 @@ cgen_as(Node *nl, Node *nr)
Type *tl; Type *tl;
int iszer; int iszer;
if(nl == N)
return;
if(debug['g']) { if(debug['g']) {
dump("cgen_as", nl); dump("cgen_as", nl);
dump("cgen_as = ", nr); dump("cgen_as = ", nr);
} }
if(isblank(nl)) { if(nl == N || isblank(nl)) {
cgen_discard(nr); cgen_discard(nr);
return; return;
} }
@ -837,10 +789,6 @@ tempname(Node *nn, Type *t)
{ {
Node *n; Node *n;
Sym *s; Sym *s;
uint32 w;
if(stksize < 0)
fatal("tempname not during code generation");
if(curfn == N) if(curfn == N)
fatal("no curfn for tempname"); fatal("no curfn for tempname");
@ -866,15 +814,7 @@ tempname(Node *nn, Type *t)
curfn->dcl = list(curfn->dcl, n); curfn->dcl = list(curfn->dcl, n);
dowidth(t); dowidth(t);
w = t->width; n->xoffset = 0;
stksize += w;
stksize = rnd(stksize, t->align);
if(thechar == '5')
stksize = rnd(stksize, widthptr);
n->xoffset = -stksize;
// print("\ttmpname (%d): %N\n", stksize, n);
*nn = *n; *nn = *n;
} }

View File

@ -264,7 +264,6 @@ struct Node
uchar initorder; uchar initorder;
uchar used; uchar used;
uchar isddd; uchar isddd;
uchar pun; // don't registerize variable ONAME
uchar readonly; uchar readonly;
uchar implicit; // don't show in printout uchar implicit; // don't show in printout
@ -608,7 +607,6 @@ typedef struct Var Var;
struct Var struct Var
{ {
vlong offset; vlong offset;
Sym* sym;
Sym* gotype; Sym* gotype;
Node* node; Node* node;
int width; int width;
@ -981,7 +979,6 @@ Type* pkgtype(Sym *s);
* gen.c * gen.c
*/ */
void addrescapes(Node *n); void addrescapes(Node *n);
void allocparams(void);
void cgen_as(Node *nl, Node *nr); void cgen_as(Node *nl, Node *nr);
void cgen_callmeth(Node *n, int proc); void cgen_callmeth(Node *n, int proc);
void clearlabels(void); void clearlabels(void);

View File

@ -7,7 +7,7 @@
#include "gg.h" #include "gg.h"
#include "opt.h" #include "opt.h"
static void compactframe(Prog* p); static void allocauto(Prog* p);
void void
compile(Node *fn) compile(Node *fn)
@ -60,8 +60,6 @@ compile(Node *fn)
if(nerrors != 0) if(nerrors != 0)
goto ret; goto ret;
allocparams();
continpc = P; continpc = P;
breakpc = P; breakpc = P;
@ -115,9 +113,9 @@ compile(Node *fn)
} }
oldstksize = stksize; oldstksize = stksize;
compactframe(ptxt); allocauto(ptxt);
if(0) if(0)
print("compactframe: %lld to %lld\n", oldstksize, (vlong)stksize); print("allocauto: %lld to %lld\n", oldstksize, (vlong)stksize);
defframe(ptxt); defframe(ptxt);
@ -147,13 +145,13 @@ cmpstackvar(Node *a, Node *b)
// TODO(lvd) find out where the PAUTO/OLITERAL nodes come from. // TODO(lvd) find out where the PAUTO/OLITERAL nodes come from.
static void static void
compactframe(Prog* ptxt) allocauto(Prog* ptxt)
{ {
NodeList *ll; NodeList *ll;
Node* n; Node* n;
vlong w; vlong w;
if (stksize == 0) if(curfn->dcl == nil)
return; return;
// Mark the PAUTO's unused. // Mark the PAUTO's unused.
@ -190,6 +188,7 @@ compactframe(Prog* ptxt)
if (n->class != PAUTO || n->op != ONAME) if (n->class != PAUTO || n->op != ONAME)
continue; continue;
dowidth(n->type);
w = n->type->width; w = n->type->width;
if(w >= MAXWIDTH || w < 0) if(w >= MAXWIDTH || w < 0)
fatal("bad width"); fatal("bad width");

View File

@ -1156,9 +1156,6 @@ Jconv(Fmt *fp)
if(n->implicit != 0) if(n->implicit != 0)
fmtprint(fp, " implicit(%d)", n->implicit); fmtprint(fp, " implicit(%d)", n->implicit);
if(!c && n->pun != 0)
fmtprint(fp, " pun(%d)", n->pun);
if(!c && n->used != 0) if(!c && n->used != 0)
fmtprint(fp, " used(%d)", n->used); fmtprint(fp, " used(%d)", n->used);
return 0; return 0;