This source file includes following definitions.
- makefuncdatasym
- hasdotdotdot
- argsize
- codgen
- supgen
- gen
- usedset
- bcomplex
- walktype1
- dumpgcargs
#include "gc.h"
#include "../../pkg/runtime/funcdata.h"
enum { BitsPerPointer = 2 };
static void dumpgcargs(Type *fn, Sym *sym);
static Sym*
makefuncdatasym(char *namefmt, int64 funcdatakind)
{
Node nod;
Sym *sym;
static int32 nsym;
static char namebuf[40];
snprint(namebuf, sizeof(namebuf), namefmt, nsym++);
sym = slookup(namebuf);
sym->class = CSTATIC;
memset(&nod, 0, sizeof nod);
nod.op = ONAME;
nod.sym = sym;
nod.class = CSTATIC;
gins(AFUNCDATA, nodconst(funcdatakind), &nod);
linksym(sym)->type = SRODATA;
return sym;
}
int
hasdotdotdot(void)
{
Type *t;
for(t=thisfn->down; t!=T; t=t->down)
if(t->etype == TDOT)
return 1;
return 0;
}
vlong
argsize(void)
{
Type *t;
int32 s;
s = align(0, thisfn->link, Aarg0, nil);
for(t=thisfn->down; t!=T; t=t->down) {
switch(t->etype) {
case TVOID:
break;
case TDOT:
if((textflag & NOSPLIT) == 0)
yyerror("function takes ... without textflag NOSPLIT");
return ArgsSizeUnknown;
default:
s = align(s, t, Aarg1, nil);
s = align(s, t, Aarg2, nil);
break;
}
}
if(thechar == '6')
s = (s+7) & ~7;
else
s = (s+3) & ~3;
return s;
}
void
codgen(Node *n, Node *nn)
{
Prog *sp;
Node *n1, nod, nod1;
Sym *gcargs;
Sym *gclocals;
int isvarargs;
cursafe = 0;
curarg = 0;
maxargsafe = 0;
for(n1 = nn;; n1 = n1->left) {
if(n1 == Z) {
diag(nn, "can't find function name");
return;
}
if(n1->op == ONAME)
break;
}
nearln = nn->lineno;
p = gtext(n1->sym, stkoff);
sp = p;
isvarargs = hasdotdotdot();
gcargs = nil;
if(!isvarargs)
gcargs = makefuncdatasym("gcargs·%d", FUNCDATA_ArgsPointerMaps);
gclocals = makefuncdatasym("gclocals·%d", FUNCDATA_LocalsPointerMaps);
if(REGARG >= 0) {
if(typesuv[thisfn->link->etype]) {
nod1 = *nodret->left;
nodreg(&nod, &nod1, REGARG);
gmove(&nod, &nod1);
} else
if(firstarg && typechlp[firstargtype->etype]) {
nod1 = *nodret->left;
nod1.sym = firstarg;
nod1.type = firstargtype;
nod1.xoffset = align(0, firstargtype, Aarg1, nil);
nod1.etype = firstargtype->etype;
nodreg(&nod, &nod1, REGARG);
gmove(&nod, &nod1);
}
}
retok = 0;
canreach = 1;
warnreach = 1;
gen(n);
if(canreach && thisfn->link->etype != TVOID)
diag(Z, "no return at end of function: %s", n1->sym->name);
noretval(3);
gbranch(ORETURN);
if(!debug['N'] || debug['R'] || debug['P'])
regopt(sp);
if(thechar=='6' || thechar=='7')
maxargsafe = xround(maxargsafe, 8);
sp->to.offset += maxargsafe;
if(!isvarargs)
dumpgcargs(thisfn, gcargs);
gextern(gclocals, nodconst(-stkoff), 0, 4);
gclocals->type = typ(0, T);
gclocals->type->width = 4;
}
void
supgen(Node *n)
{
int owarn;
long spc;
Prog *sp;
if(n == Z)
return;
suppress++;
owarn = warnreach;
warnreach = 0;
spc = pc;
sp = lastp;
gen(n);
lastp = sp;
pc = spc;
sp->link = nil;
suppress--;
warnreach = owarn;
}
void
gen(Node *n)
{
Node *l, nod;
Prog *sp, *spc, *spb;
Case *cn;
long sbc, scc;
int snbreak, sncontin;
int f, o, oldreach;
loop:
if(n == Z)
return;
nearln = n->lineno;
o = n->op;
if(debug['G'])
if(o != OLIST)
print("%L %O\n", nearln, o);
if(!canreach) {
switch(o) {
case OLABEL:
case OCASE:
case OLIST:
case OBREAK:
case OFOR:
case OWHILE:
case ODWHILE:
break;
default:
if(warnreach) {
warn(n, "unreachable code %O", o);
warnreach = 0;
}
}
}
switch(o) {
default:
complex(n);
cgen(n, Z);
break;
case OLIST:
gen(n->left);
rloop:
n = n->right;
goto loop;
case ORETURN:
canreach = 0;
warnreach = !suppress;
complex(n);
if(n->type == T)
break;
l = n->left;
if(l == Z) {
noretval(3);
gbranch(ORETURN);
break;
}
if(typecmplx[n->type->etype]) {
sugen(l, nodret, n->type->width);
noretval(3);
gbranch(ORETURN);
break;
}
regret(&nod, n);
cgen(l, &nod);
regfree(&nod);
if(typefd[n->type->etype])
noretval(1);
else
noretval(2);
gbranch(ORETURN);
break;
case OLABEL:
canreach = 1;
l = n->left;
if(l) {
l->pc = pc;
if(l->label)
patch(l->label, pc);
}
gbranch(OGOTO);
patch(p, pc);
goto rloop;
case OGOTO:
canreach = 0;
warnreach = !suppress;
n = n->left;
if(n == Z)
return;
if(n->complex == 0) {
diag(Z, "label undefined: %s", n->sym->name);
return;
}
if(suppress)
return;
gbranch(OGOTO);
if(n->pc) {
patch(p, n->pc);
return;
}
if(n->label)
patch(n->label, pc-1);
n->label = p;
return;
case OCASE:
canreach = 1;
l = n->left;
if(cases == C)
diag(n, "case/default outside a switch");
if(l == Z) {
newcase();
cases->val = 0;
cases->def = 1;
cases->label = pc;
cases->isv = 0;
goto rloop;
}
complex(l);
if(l->type == T)
goto rloop;
if(l->op == OCONST)
if(typeword[l->type->etype] && l->type->etype != TIND) {
newcase();
cases->val = l->vconst;
cases->def = 0;
cases->label = pc;
cases->isv = typev[l->type->etype];
goto rloop;
}
diag(n, "case expression must be integer constant");
goto rloop;
case OSWITCH:
l = n->left;
complex(l);
if(l->type == T)
break;
if(!typechlvp[l->type->etype] || l->type->etype == TIND) {
diag(n, "switch expression must be integer");
break;
}
gbranch(OGOTO);
sp = p;
cn = cases;
cases = C;
newcase();
sbc = breakpc;
breakpc = pc;
snbreak = nbreak;
nbreak = 0;
gbranch(OGOTO);
spb = p;
gen(n->right);
if(canreach){
gbranch(OGOTO);
patch(p, breakpc);
nbreak++;
}
patch(sp, pc);
doswit(l);
patch(spb, pc);
cases = cn;
breakpc = sbc;
canreach = nbreak!=0;
if(canreach == 0)
warnreach = !suppress;
nbreak = snbreak;
break;
case OWHILE:
case ODWHILE:
l = n->left;
gbranch(OGOTO);
sp = p;
scc = continpc;
continpc = pc;
gbranch(OGOTO);
spc = p;
sbc = breakpc;
breakpc = pc;
snbreak = nbreak;
nbreak = 0;
gbranch(OGOTO);
spb = p;
patch(spc, pc);
if(n->op == OWHILE)
patch(sp, pc);
bcomplex(l, Z);
patch(p, breakpc);
if(l->op != OCONST || vconst(l) == 0)
nbreak++;
if(n->op == ODWHILE)
patch(sp, pc);
gen(n->right);
gbranch(OGOTO);
patch(p, continpc);
patch(spb, pc);
continpc = scc;
breakpc = sbc;
canreach = nbreak!=0;
if(canreach == 0)
warnreach = !suppress;
nbreak = snbreak;
break;
case OFOR:
l = n->left;
if(!canreach && l->right->left && warnreach) {
warn(n, "unreachable code FOR");
warnreach = 0;
}
gen(l->right->left);
gbranch(OGOTO);
sp = p;
if(!canreach && warnreach && deadheads(n)) {
warn(n, "unreachable code %O", o);
warnreach = 0;
}
scc = continpc;
continpc = pc;
gbranch(OGOTO);
spc = p;
sbc = breakpc;
breakpc = pc;
snbreak = nbreak;
nbreak = 0;
sncontin = ncontin;
ncontin = 0;
gbranch(OGOTO);
spb = p;
patch(spc, pc);
gen(l->right->right);
patch(sp, pc);
if(l->left != Z) {
bcomplex(l->left, Z);
patch(p, breakpc);
if(l->left->op != OCONST || vconst(l->left) == 0)
nbreak++;
}
canreach = 1;
gen(n->right);
if(canreach){
gbranch(OGOTO);
patch(p, continpc);
ncontin++;
}
if(!ncontin && l->right->right && warnreach) {
warn(l->right->right, "unreachable FOR inc");
warnreach = 0;
}
patch(spb, pc);
continpc = scc;
breakpc = sbc;
canreach = nbreak!=0;
if(canreach == 0)
warnreach = !suppress;
nbreak = snbreak;
ncontin = sncontin;
break;
case OCONTINUE:
if(continpc < 0) {
diag(n, "continue not in a loop");
break;
}
gbranch(OGOTO);
patch(p, continpc);
ncontin++;
canreach = 0;
warnreach = !suppress;
break;
case OBREAK:
if(breakpc < 0) {
diag(n, "break not in a loop");
break;
}
if(!canreach)
break;
gbranch(OGOTO);
patch(p, breakpc);
nbreak++;
canreach = 0;
warnreach = !suppress;
break;
case OIF:
l = n->left;
if(bcomplex(l, n->right)) {
if(typefd[l->type->etype])
f = !l->fconst;
else
f = !l->vconst;
if(debug['c'])
print("%L const if %s\n", nearln, f ? "false" : "true");
if(f) {
canreach = 1;
supgen(n->right->left);
oldreach = canreach;
canreach = 1;
gen(n->right->right);
if(!canreach && oldreach && debug['w'] < 2)
warnreach = 0;
}
else {
canreach = 1;
gen(n->right->left);
oldreach = canreach;
canreach = 1;
supgen(n->right->right);
if(!oldreach && canreach && debug['w'] < 2)
warnreach = 0;
canreach = oldreach;
}
}
else {
sp = p;
canreach = 1;
if(n->right->left != Z)
gen(n->right->left);
oldreach = canreach;
canreach = 1;
if(n->right->right != Z) {
gbranch(OGOTO);
patch(sp, pc);
sp = p;
gen(n->right->right);
}
patch(sp, pc);
canreach = canreach || oldreach;
if(canreach == 0)
warnreach = !suppress;
}
break;
case OSET:
case OUSED:
case OPREFETCH:
usedset(n->left, o);
break;
}
}
void
usedset(Node *n, int o)
{
if(n->op == OLIST) {
usedset(n->left, o);
usedset(n->right, o);
return;
}
complex(n);
if(o == OPREFETCH) {
gprefetch(n);
return;
}
switch(n->op) {
case OADDR:
gins(ANOP, n, Z);
break;
case ONAME:
if(o == OSET)
gins(ANOP, Z, n);
else
gins(ANOP, n, Z);
break;
}
}
int
bcomplex(Node *n, Node *c)
{
Node *b, nod;
complex(n);
if(n->type != T)
if(tcompat(n, T, n->type, tnot))
n->type = T;
if(n->type == T) {
gbranch(OGOTO);
return 0;
}
if(c != Z && n->op == OCONST && deadheads(c))
return 1;
if(typev[n->type->etype] && machcap(Z)) {
b = &nod;
b->op = ONE;
b->left = n;
b->right = new(0, Z, Z);
*b->right = *nodconst(0);
b->right->type = n->type;
b->type = types[TLONG];
n = b;
}
bool64(n);
boolgen(n, 1, Z);
return 0;
}
static void
walktype1(Type *t, int32 offset, Bvec *bv, int param)
{
Type *t1;
int32 o;
int32 widthptr;
widthptr = ewidth[TIND];
switch(t->etype) {
case TCHAR:
case TUCHAR:
case TSHORT:
case TUSHORT:
case TINT:
case TUINT:
case TLONG:
case TULONG:
case TVLONG:
case TUVLONG:
case TFLOAT:
case TDOUBLE:
for(o = 0; o < t->width; o++)
bvset(bv, ((offset + t->offset + o) / widthptr) * BitsPerPointer);
break;
case TIND:
pointer:
if((offset + t->offset) % widthptr != 0)
yyerror("unaligned pointer");
bvset(bv, ((offset + t->offset) / widthptr)*BitsPerPointer + 1);
break;
case TARRAY:
if(param)
goto pointer;
for(o = 0; o < t->width; o += t->link->width)
walktype1(t->link, offset+o, bv, 0);
break;
case TSTRUCT:
for(t1 = t->link; t1 != T; t1 = t1->down)
walktype1(t1, offset, bv, 0);
break;
case TUNION:
walktype1(t->link, offset, bv, 0);
break;
default:
yyerror("can't handle arg type %s\n", tnames[t->etype]);
}
}
static void
dumpgcargs(Type *fn, Sym *sym)
{
Bvec *bv;
Type *t;
int32 i;
int32 argbytes;
int32 symoffset, argoffset;
symoffset = 0;
gextern(sym, nodconst(1), symoffset, 4);
symoffset += 4;
argbytes = (argsize() + ewidth[TIND] - 1);
bv = bvalloc((argbytes / ewidth[TIND]) * BitsPerPointer);
argoffset = align(0, fn->link, Aarg0, nil);
if(argoffset > 0) {
if(argoffset != ewidth[TIND])
yyerror("passbyptr arg not the right size");
bvset(bv, 1);
}
for(t = fn->down; t != T; t = t->down) {
if(t->etype == TVOID)
continue;
argoffset = align(argoffset, t, Aarg1, nil);
walktype1(t, argoffset, bv, 1);
argoffset = align(argoffset, t, Aarg2, nil);
}
gextern(sym, nodconst(bv->n), symoffset, 4);
symoffset += 4;
for(i = 0; i < bv->n; i += 32) {
gextern(sym, nodconst(bv->b[i/32]), symoffset, 4);
symoffset += 4;
}
free(bv);
sym->type = typ(0, T);
sym->type->width = symoffset;
}