2009-03-31 01:22:59 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#undef EXTERN
|
|
|
|
#define EXTERN
|
|
|
|
#include "gg.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
compile(Node *fn)
|
|
|
|
{
|
|
|
|
Plist *pl;
|
|
|
|
Node nod1;
|
|
|
|
Prog *ptxt;
|
|
|
|
int32 lno;
|
|
|
|
Type *t;
|
|
|
|
Iter save;
|
|
|
|
|
|
|
|
if(newproc == N) {
|
|
|
|
newproc = sysfunc("newproc");
|
|
|
|
deferproc = sysfunc("deferproc");
|
|
|
|
deferreturn = sysfunc("deferreturn");
|
|
|
|
throwindex = sysfunc("throwindex");
|
|
|
|
throwreturn = sysfunc("throwreturn");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fn->nbody == N)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// set up domain for labels
|
|
|
|
labellist = L;
|
|
|
|
|
|
|
|
lno = setlineno(fn);
|
|
|
|
|
|
|
|
curfn = fn;
|
|
|
|
dowidth(curfn->type);
|
|
|
|
|
|
|
|
if(curfn->type->outnamed) {
|
|
|
|
// add clearing of the output parameters
|
|
|
|
t = structfirst(&save, getoutarg(curfn->type));
|
|
|
|
while(t != T) {
|
|
|
|
if(t->nname != N)
|
|
|
|
curfn->nbody = list(nod(OAS, t->nname, N), curfn->nbody);
|
|
|
|
t = structnext(&save);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hasdefer = 0;
|
|
|
|
walk(curfn);
|
|
|
|
if(nerrors != 0)
|
|
|
|
goto ret;
|
|
|
|
|
|
|
|
allocparams();
|
|
|
|
|
|
|
|
continpc = P;
|
|
|
|
breakpc = P;
|
|
|
|
|
|
|
|
pl = newplist();
|
|
|
|
pl->name = curfn->nname;
|
|
|
|
pl->locals = autodcl;
|
|
|
|
|
|
|
|
nodconst(&nod1, types[TINT32], 0);
|
|
|
|
ptxt = gins(ATEXT, curfn->nname, &nod1);
|
|
|
|
afunclit(&ptxt->from);
|
|
|
|
|
|
|
|
// ginit();
|
|
|
|
gen(curfn->enter);
|
|
|
|
gen(curfn->nbody);
|
|
|
|
// gclean();
|
|
|
|
checklabels();
|
|
|
|
|
|
|
|
// if(curfn->type->outtuple != 0)
|
|
|
|
// ginscall(throwreturn, 0);
|
|
|
|
|
|
|
|
// if(hasdefer)
|
|
|
|
// ginscall(deferreturn, 0);
|
|
|
|
pc->as = ARET; // overwrite AEND
|
|
|
|
pc->lineno = lineno;
|
|
|
|
|
|
|
|
// if(!debug['N'] || debug['R'] || debug['P'])
|
|
|
|
// regopt(ptxt);
|
|
|
|
|
|
|
|
// fill in argument size
|
|
|
|
ptxt->to.offset2 = rnd(curfn->type->argwid, maxround);
|
|
|
|
|
|
|
|
// fill in final stack size
|
2009-04-02 17:48:06 -06:00
|
|
|
if(stksize > maxstksize)
|
|
|
|
maxstksize = stksize;
|
|
|
|
ptxt->to.offset = rnd(maxstksize+maxarg, maxround);
|
|
|
|
maxstksize = 0;
|
2009-03-31 01:22:59 -06:00
|
|
|
|
|
|
|
if(debug['f'])
|
|
|
|
frame(0);
|
|
|
|
|
|
|
|
ret:
|
|
|
|
lineno = lno;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
clearfat(Node *nl)
|
|
|
|
{
|
2009-05-26 17:23:54 -06:00
|
|
|
uint32 w, c, q;
|
|
|
|
Node n1;
|
|
|
|
|
|
|
|
/* clear a fat object */
|
|
|
|
if(debug['g'])
|
|
|
|
dump("\nclearfat", nl);
|
|
|
|
|
|
|
|
w = nl->type->width;
|
|
|
|
c = w % 4; // bytes
|
|
|
|
q = w / 4; // quads
|
|
|
|
|
|
|
|
gconreg(AMOVL, 0, D_AX);
|
|
|
|
nodreg(&n1, types[tptr], D_DI);
|
|
|
|
agen(nl, &n1);
|
|
|
|
|
|
|
|
if(q >= 4) {
|
|
|
|
gconreg(AMOVL, q, D_CX);
|
|
|
|
gins(AREP, N, N); // repeat
|
|
|
|
gins(ASTOSL, N, N); // STOL AL,*(DI)+
|
|
|
|
} else
|
|
|
|
while(q > 0) {
|
|
|
|
gins(ASTOSL, N, N); // STOL AL,*(DI)+
|
|
|
|
q--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(c >= 4) {
|
|
|
|
gconreg(AMOVL, c, D_CX);
|
|
|
|
gins(AREP, N, N); // repeat
|
|
|
|
gins(ASTOSB, N, N); // STOB AL,*(DI)+
|
|
|
|
} else
|
|
|
|
while(c > 0) {
|
|
|
|
gins(ASTOSB, N, N); // STOB AL,*(DI)+
|
|
|
|
c--;
|
|
|
|
}
|
2009-03-31 01:22:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* generate:
|
|
|
|
* call f
|
|
|
|
* proc=0 normal call
|
|
|
|
* proc=1 goroutine run in new proc
|
|
|
|
* proc=2 defer call save away stack
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ginscall(Node *f, int proc)
|
|
|
|
{
|
|
|
|
Prog *p;
|
|
|
|
Node reg, con;
|
|
|
|
|
|
|
|
switch(proc) {
|
|
|
|
default:
|
|
|
|
fatal("ginscall: bad proc %d", proc);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0: // normal call
|
|
|
|
p = gins(ACALL, N, f);
|
|
|
|
afunclit(&p->to);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: // call in new proc (go)
|
|
|
|
case 2: // defered call (defer)
|
|
|
|
nodreg(®, types[TINT32], D_AX);
|
|
|
|
gins(APUSHL, f, N);
|
|
|
|
nodconst(&con, types[TINT32], argsize(f->type));
|
|
|
|
gins(APUSHL, &con, N);
|
|
|
|
if(proc == 1)
|
|
|
|
ginscall(newproc, 0);
|
|
|
|
else
|
|
|
|
ginscall(deferproc, 0);
|
|
|
|
gins(APOPL, N, ®);
|
|
|
|
gins(APOPL, N, ®);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* n is call to interface method.
|
|
|
|
* generate res = n.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cgen_callinter(Node *n, Node *res, int proc)
|
|
|
|
{
|
|
|
|
fatal("cgen_call");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* generate function call;
|
|
|
|
* proc=0 normal call
|
|
|
|
* proc=1 goroutine run in new proc
|
|
|
|
* proc=2 defer call save away stack
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cgen_call(Node *n, int proc)
|
|
|
|
{
|
2009-04-02 17:48:06 -06:00
|
|
|
Type *t;
|
|
|
|
Node nod, afun;
|
|
|
|
|
|
|
|
if(n == N)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(n->left->ullman >= UINF) {
|
|
|
|
// if name involves a fn call
|
|
|
|
// precompute the address of the fn
|
|
|
|
tempalloc(&afun, types[tptr]);
|
|
|
|
cgen(n->left, &afun);
|
|
|
|
}
|
|
|
|
|
|
|
|
gen(n->right); // assign the args
|
|
|
|
t = n->left->type;
|
|
|
|
|
|
|
|
setmaxarg(t);
|
|
|
|
|
|
|
|
// call tempname pointer
|
|
|
|
if(n->left->ullman >= UINF) {
|
|
|
|
regalloc(&nod, types[tptr], N);
|
|
|
|
cgen_as(&nod, &afun);
|
|
|
|
tempfree(&afun);
|
|
|
|
nod.type = t;
|
|
|
|
ginscall(&nod, proc);
|
|
|
|
regfree(&nod);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// call pointer
|
|
|
|
if(n->left->op != ONAME || n->left->class != PFUNC) {
|
|
|
|
regalloc(&nod, types[tptr], N);
|
|
|
|
cgen_as(&nod, n->left);
|
|
|
|
nod.type = t;
|
|
|
|
ginscall(&nod, proc);
|
|
|
|
regfree(&nod);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// call direct
|
|
|
|
n->left->method = 1;
|
|
|
|
ginscall(n->left, proc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call to n has already been generated.
|
|
|
|
* generate:
|
|
|
|
* res = return value from call.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cgen_callret(Node *n, Node *res)
|
|
|
|
{
|
|
|
|
Node nod;
|
|
|
|
Type *fp, *t;
|
|
|
|
Iter flist;
|
|
|
|
|
|
|
|
t = n->left->type;
|
|
|
|
if(t->etype == TPTR32 || t->etype == TPTR64)
|
|
|
|
t = t->type;
|
|
|
|
|
|
|
|
fp = structfirst(&flist, getoutarg(t));
|
|
|
|
if(fp == T)
|
|
|
|
fatal("cgen_callret: nil");
|
|
|
|
|
|
|
|
memset(&nod, 0, sizeof(nod));
|
|
|
|
nod.op = OINDREG;
|
|
|
|
nod.val.u.reg = D_SP;
|
|
|
|
nod.addable = 1;
|
|
|
|
|
|
|
|
nod.xoffset = fp->width;
|
|
|
|
nod.type = fp->type;
|
|
|
|
cgen_as(res, &nod);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call to n has already been generated.
|
|
|
|
* generate:
|
|
|
|
* res = &return value from call.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cgen_aret(Node *n, Node *res)
|
|
|
|
{
|
|
|
|
Node nod1, nod2;
|
|
|
|
Type *fp, *t;
|
|
|
|
Iter flist;
|
|
|
|
|
|
|
|
t = n->left->type;
|
|
|
|
if(isptr[t->etype])
|
|
|
|
t = t->type;
|
|
|
|
|
|
|
|
fp = structfirst(&flist, getoutarg(t));
|
|
|
|
if(fp == T)
|
|
|
|
fatal("cgen_aret: nil");
|
|
|
|
|
|
|
|
memset(&nod1, 0, sizeof(nod1));
|
|
|
|
nod1.op = OINDREG;
|
|
|
|
nod1.val.u.reg = D_SP;
|
|
|
|
nod1.addable = 1;
|
|
|
|
|
|
|
|
nod1.xoffset = fp->width;
|
|
|
|
nod1.type = fp->type;
|
|
|
|
|
|
|
|
if(res->op != OREGISTER) {
|
|
|
|
regalloc(&nod2, types[tptr], res);
|
|
|
|
gins(ALEAL, &nod1, &nod2);
|
|
|
|
gins(AMOVL, &nod2, res);
|
|
|
|
regfree(&nod2);
|
|
|
|
} else
|
|
|
|
gins(ALEAL, &nod1, res);
|
2009-03-31 01:22:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* generate return.
|
|
|
|
* n->left is assignments to return values.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cgen_ret(Node *n)
|
|
|
|
{
|
|
|
|
gen(n->left); // copy out args
|
|
|
|
if(hasdefer)
|
|
|
|
ginscall(deferreturn, 0);
|
|
|
|
gins(ARET, N, N);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* generate += *= etc.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cgen_asop(Node *n)
|
|
|
|
{
|
2009-05-26 17:23:54 -06:00
|
|
|
Node n1, n2, n3, n4;
|
|
|
|
Node *nl, *nr;
|
|
|
|
Prog *p1;
|
|
|
|
Addr addr;
|
|
|
|
int a;
|
|
|
|
|
|
|
|
nl = n->left;
|
|
|
|
nr = n->right;
|
|
|
|
|
|
|
|
if(nr->ullman >= UINF && nl->ullman >= UINF) {
|
|
|
|
tempname(&n1, nr->type);
|
|
|
|
cgen(nr, &n1);
|
|
|
|
n2 = *n;
|
|
|
|
n2.right = &n1;
|
|
|
|
cgen_asop(&n2);
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!isint[nl->type->etype])
|
|
|
|
goto hard;
|
|
|
|
if(!isint[nr->type->etype])
|
|
|
|
goto hard;
|
|
|
|
|
|
|
|
switch(n->etype) {
|
|
|
|
case OADD:
|
|
|
|
if(smallintconst(nr))
|
|
|
|
if(mpgetfix(nr->val.u.xval) == 1) {
|
|
|
|
a = optoas(OINC, nl->type);
|
|
|
|
if(nl->addable) {
|
|
|
|
gins(a, N, nl);
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
if(sudoaddable(a, nl, &addr)) {
|
|
|
|
p1 = gins(a, N, N);
|
|
|
|
p1->to = addr;
|
|
|
|
sudoclean();
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OSUB:
|
|
|
|
if(smallintconst(nr))
|
|
|
|
if(mpgetfix(nr->val.u.xval) == 1) {
|
|
|
|
a = optoas(ODEC, nl->type);
|
|
|
|
if(nl->addable) {
|
|
|
|
gins(a, N, nl);
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
if(sudoaddable(a, nl, &addr)) {
|
|
|
|
p1 = gins(a, N, N);
|
|
|
|
p1->to = addr;
|
|
|
|
sudoclean();
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(n->etype) {
|
|
|
|
case OADD:
|
|
|
|
case OSUB:
|
|
|
|
case OXOR:
|
|
|
|
case OAND:
|
|
|
|
case OOR:
|
|
|
|
a = optoas(n->etype, nl->type);
|
|
|
|
if(nl->addable) {
|
|
|
|
if(smallintconst(nr)) {
|
|
|
|
gins(a, nr, nl);
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
regalloc(&n2, nr->type, N);
|
|
|
|
cgen(nr, &n2);
|
|
|
|
gins(a, &n2, nl);
|
|
|
|
regfree(&n2);
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
if(nr->ullman < UINF)
|
|
|
|
if(sudoaddable(a, nl, &addr)) {
|
|
|
|
if(smallintconst(nr)) {
|
|
|
|
p1 = gins(a, nr, N);
|
|
|
|
p1->to = addr;
|
|
|
|
sudoclean();
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
regalloc(&n2, nr->type, N);
|
|
|
|
cgen(nr, &n2);
|
|
|
|
p1 = gins(a, &n2, N);
|
|
|
|
p1->to = addr;
|
|
|
|
regfree(&n2);
|
|
|
|
sudoclean();
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hard:
|
|
|
|
if(nr->ullman > nl->ullman) {
|
|
|
|
regalloc(&n2, nr->type, N);
|
|
|
|
cgen(nr, &n2);
|
|
|
|
igen(nl, &n1, N);
|
|
|
|
} else {
|
|
|
|
igen(nl, &n1, N);
|
|
|
|
tempalloc(&n2, nr->type);
|
|
|
|
cgen(nr, &n2);
|
|
|
|
}
|
|
|
|
|
|
|
|
n3 = *n;
|
|
|
|
n3.left = &n1;
|
|
|
|
n3.right = &n2;
|
|
|
|
n3.op = n->etype;
|
|
|
|
|
|
|
|
tempalloc(&n4, nl->type);
|
|
|
|
cgen(&n3, &n4);
|
|
|
|
gmove(&n4, &n1);
|
|
|
|
|
|
|
|
regfree(&n1);
|
|
|
|
tempfree(&n4);
|
|
|
|
tempfree(&n2);
|
|
|
|
|
|
|
|
ret:
|
|
|
|
;
|
2009-03-31 01:22:59 -06:00
|
|
|
}
|
|
|
|
|
2009-04-02 17:48:06 -06:00
|
|
|
/*
|
|
|
|
* generate division according to op, one of:
|
|
|
|
* res = nl / nr
|
|
|
|
* res = nl % nr
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cgen_div(int op, Node *nl, Node *nr, Node *res)
|
|
|
|
{
|
|
|
|
fatal("cgen_div");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* generate shift according to op, one of:
|
|
|
|
* res = nl << nr
|
|
|
|
* res = nl >> nr
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cgen_shift(int op, Node *nl, Node *nr, Node *res)
|
|
|
|
{
|
|
|
|
fatal("cgen_shift");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* generate byte multiply:
|
|
|
|
* res = nl * nr
|
|
|
|
* no byte multiply instruction so have to do
|
|
|
|
* 16-bit multiply and take bottom half.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
cgen_bmul(int op, Node *nl, Node *nr, Node *res)
|
|
|
|
{
|
|
|
|
fatal("cgen_bmul");
|
|
|
|
}
|
|
|
|
|
2009-05-26 15:46:06 -06:00
|
|
|
int
|
|
|
|
gen_as_init(Node *nr, Node *nl)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|