2008-06-04 15:37:38 -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"
|
2008-11-19 10:49:06 -07:00
|
|
|
#include "opt.h"
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
void
|
|
|
|
compile(Node *fn)
|
|
|
|
{
|
|
|
|
Plist *pl;
|
|
|
|
Node nod1;
|
|
|
|
Prog *ptxt;
|
2008-08-03 18:25:15 -06:00
|
|
|
int32 lno;
|
2008-10-01 21:00:58 -06:00
|
|
|
Type *t;
|
2008-10-02 16:37:15 -06:00
|
|
|
Iter save;
|
2008-06-16 23:34:50 -06:00
|
|
|
|
2008-07-07 18:59:32 -06:00
|
|
|
if(newproc == N) {
|
|
|
|
newproc = nod(ONAME, N, N);
|
2008-07-11 20:16:39 -06:00
|
|
|
newproc->sym = pkglookup("newproc", "sys");
|
2008-07-07 18:59:32 -06:00
|
|
|
newproc->class = PEXTERN;
|
|
|
|
newproc->addable = 1;
|
2008-11-06 15:50:58 -07:00
|
|
|
newproc->ullman = 1;
|
2008-07-07 18:59:32 -06:00
|
|
|
}
|
|
|
|
|
2008-09-12 17:48:35 -06:00
|
|
|
if(throwindex == N) {
|
|
|
|
throwindex = nod(ONAME, N, N);
|
|
|
|
throwindex->sym = pkglookup("throwindex", "sys");
|
|
|
|
throwindex->class = PEXTERN;
|
|
|
|
throwindex->addable = 1;
|
2008-11-06 15:50:58 -07:00
|
|
|
throwindex->ullman = 1;
|
2008-09-12 17:48:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if(throwreturn == N) {
|
|
|
|
throwreturn = nod(ONAME, N, N);
|
|
|
|
throwreturn->sym = pkglookup("throwreturn", "sys");
|
|
|
|
throwreturn->class = PEXTERN;
|
|
|
|
throwreturn->addable = 1;
|
2008-11-06 15:50:58 -07:00
|
|
|
throwreturn->ullman = 1;
|
2008-09-12 17:48:35 -06:00
|
|
|
}
|
|
|
|
|
2008-06-04 15:37:38 -06:00
|
|
|
if(fn->nbody == N)
|
|
|
|
return;
|
2008-08-29 21:30:19 -06:00
|
|
|
|
|
|
|
// set up domain for labels
|
|
|
|
labellist = L;
|
|
|
|
|
2008-06-26 18:54:44 -06:00
|
|
|
lno = setlineno(fn);
|
2008-06-16 23:34:50 -06:00
|
|
|
|
2008-06-04 15:37:38 -06:00
|
|
|
curfn = fn;
|
|
|
|
dowidth(curfn->type);
|
|
|
|
|
2008-10-01 21:00:58 -06:00
|
|
|
if(curfn->type->outnamed) {
|
|
|
|
// add clearing of the output parameters
|
2008-10-02 16:37:15 -06:00
|
|
|
t = structfirst(&save, getoutarg(curfn->type));
|
2008-10-01 21:00:58 -06:00
|
|
|
while(t != T) {
|
2009-01-21 15:51:57 -07:00
|
|
|
if(t->nname != N)
|
2008-10-01 21:00:58 -06:00
|
|
|
curfn->nbody = list(nod(OAS, t->nname, N), curfn->nbody);
|
2008-10-02 16:37:15 -06:00
|
|
|
t = structnext(&save);
|
2008-10-01 21:00:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-04 15:37:38 -06:00
|
|
|
walk(curfn);
|
|
|
|
if(nerrors != 0)
|
2008-06-26 18:54:44 -06:00
|
|
|
goto ret;
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
// inarggen();
|
|
|
|
|
|
|
|
ginit();
|
2008-08-29 21:30:19 -06:00
|
|
|
gen(curfn->nbody, L);
|
2008-06-04 15:37:38 -06:00
|
|
|
gclean();
|
2008-08-29 21:30:19 -06:00
|
|
|
checklabels();
|
2008-06-04 15:37:38 -06:00
|
|
|
|
2008-09-11 16:23:01 -06:00
|
|
|
if(curfn->type->outtuple != 0) {
|
2008-09-12 17:48:35 -06:00
|
|
|
gins(ACALL, N, throwreturn);
|
2008-09-11 16:23:01 -06:00
|
|
|
}
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
pc->as = ARET; // overwrite AEND
|
2008-06-28 18:27:39 -06:00
|
|
|
pc->lineno = lineno;
|
2008-06-04 15:37:38 -06:00
|
|
|
|
2008-11-22 18:58:53 -07:00
|
|
|
if(!debug['N'] || debug['R'] || debug['P'])
|
2008-11-18 20:24:37 -07:00
|
|
|
regopt(ptxt);
|
2008-11-18 18:15:42 -07:00
|
|
|
|
2008-07-12 14:08:53 -06:00
|
|
|
// fill in argument size
|
2008-07-12 18:16:22 -06:00
|
|
|
ptxt->to.offset = rnd(curfn->type->argwid, maxround);
|
2008-07-12 14:08:53 -06:00
|
|
|
|
2008-06-04 15:37:38 -06:00
|
|
|
// fill in final stack size
|
2008-07-12 14:08:53 -06:00
|
|
|
ptxt->to.offset <<= 32;
|
|
|
|
ptxt->to.offset |= rnd(stksize+maxarg, maxround);
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
if(debug['f'])
|
|
|
|
frame(0);
|
2008-06-16 23:34:50 -06:00
|
|
|
|
2008-06-26 18:54:44 -06:00
|
|
|
ret:
|
|
|
|
lineno = lno;
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
allocparams(void)
|
|
|
|
{
|
|
|
|
Dcl *d;
|
|
|
|
Node *n;
|
2008-08-03 18:25:15 -06:00
|
|
|
uint32 w;
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
* allocate (set xoffset) the stack
|
|
|
|
* slots for all automatics.
|
|
|
|
* allocated starting at -w down.
|
|
|
|
*/
|
|
|
|
for(d=autodcl; d!=D; d=d->forw) {
|
|
|
|
if(d->op != ONAME)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
n = d->dnode;
|
|
|
|
if(n->class != PAUTO)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dowidth(n->type);
|
|
|
|
w = n->type->width;
|
|
|
|
stksize += w;
|
|
|
|
stksize = rnd(stksize, w);
|
|
|
|
|
|
|
|
n->xoffset = -stksize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* compile statements
|
|
|
|
*/
|
|
|
|
void
|
2008-08-29 21:30:19 -06:00
|
|
|
gen(Node *n, Label *labloop)
|
2008-06-04 15:37:38 -06:00
|
|
|
{
|
2008-08-03 18:25:15 -06:00
|
|
|
int32 lno;
|
2008-06-04 15:37:38 -06:00
|
|
|
Prog *scontin, *sbreak;
|
|
|
|
Prog *p1, *p2, *p3;
|
2008-08-29 21:30:19 -06:00
|
|
|
Node *l;
|
|
|
|
Label *lab;
|
2008-06-04 15:37:38 -06:00
|
|
|
|
2008-06-26 18:54:44 -06:00
|
|
|
lno = setlineno(n);
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
loop:
|
|
|
|
if(n == N)
|
|
|
|
goto ret;
|
2008-09-02 20:11:25 -06:00
|
|
|
if(n->ninit)
|
|
|
|
gen(n->ninit, L);
|
|
|
|
|
2008-06-26 18:54:44 -06:00
|
|
|
setlineno(n);
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
switch(n->op) {
|
|
|
|
default:
|
|
|
|
fatal("gen: unknown op %N", n);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OLIST:
|
2008-08-29 21:30:19 -06:00
|
|
|
l = n->left;
|
|
|
|
gen(l, L);
|
|
|
|
if(l != N && l->op == OLABEL) {
|
|
|
|
// call the next statement with a label
|
|
|
|
l = n->right;
|
|
|
|
if(l != N) {
|
|
|
|
if(l->op != OLIST) {
|
|
|
|
gen(l, labellist);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
gen(l->left, labellist);
|
|
|
|
n = l->right;
|
|
|
|
labloop = L;
|
|
|
|
goto loop;
|
|
|
|
}
|
|
|
|
}
|
2008-06-04 15:37:38 -06:00
|
|
|
n = n->right;
|
2008-08-29 21:30:19 -06:00
|
|
|
labloop = L;
|
2008-06-04 15:37:38 -06:00
|
|
|
goto loop;
|
|
|
|
|
|
|
|
case OPANIC:
|
|
|
|
genpanic();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OCASE:
|
|
|
|
case OFALL:
|
|
|
|
case OXCASE:
|
|
|
|
case OXFALL:
|
|
|
|
case OEMPTY:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OLABEL:
|
2008-08-29 21:30:19 -06:00
|
|
|
lab = mal(sizeof(*lab));
|
|
|
|
lab->link = labellist;
|
|
|
|
labellist = lab;
|
|
|
|
lab->sym = n->left->sym;
|
2008-06-04 15:37:38 -06:00
|
|
|
|
2008-08-29 21:30:19 -06:00
|
|
|
lab->op = OLABEL;
|
|
|
|
lab->label = pc;
|
2008-06-04 15:37:38 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OGOTO:
|
2008-08-29 21:30:19 -06:00
|
|
|
lab = mal(sizeof(*lab));
|
|
|
|
lab->link = labellist;
|
|
|
|
labellist = lab;
|
|
|
|
lab->sym = n->left->sym;
|
|
|
|
|
|
|
|
lab->op = OGOTO;
|
|
|
|
lab->label = pc;
|
|
|
|
gbranch(AJMP, T);
|
2008-06-04 15:37:38 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OBREAK:
|
2008-08-29 21:30:19 -06:00
|
|
|
if(n->left != N) {
|
|
|
|
lab = findlab(n->left->sym);
|
|
|
|
if(lab == L || lab->breakpc == P) {
|
|
|
|
yyerror("break label is not defined: %S", n->left->sym);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
patch(gbranch(AJMP, T), lab->breakpc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-06-04 15:37:38 -06:00
|
|
|
if(breakpc == P) {
|
2008-08-29 21:30:19 -06:00
|
|
|
yyerror("break is not in a loop");
|
2008-06-04 15:37:38 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
patch(gbranch(AJMP, T), breakpc);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OCONTINUE:
|
2008-08-29 21:30:19 -06:00
|
|
|
if(n->left != N) {
|
|
|
|
lab = findlab(n->left->sym);
|
|
|
|
if(lab == L || lab->continpc == P) {
|
|
|
|
yyerror("continue label is not defined: %S", n->left->sym);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
patch(gbranch(AJMP, T), lab->continpc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-06-04 15:37:38 -06:00
|
|
|
if(continpc == P) {
|
|
|
|
yyerror("gen: continue is not in a loop");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
patch(gbranch(AJMP, T), continpc);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OFOR:
|
|
|
|
p1 = gbranch(AJMP, T); // goto test
|
|
|
|
sbreak = breakpc;
|
|
|
|
breakpc = gbranch(AJMP, T); // break: goto done
|
|
|
|
scontin = continpc;
|
|
|
|
continpc = pc;
|
2008-08-29 21:30:19 -06:00
|
|
|
gen(n->nincr, L); // contin: incr
|
2008-06-04 15:37:38 -06:00
|
|
|
patch(p1, pc); // test:
|
2008-09-09 16:47:31 -06:00
|
|
|
if(n->ntest != N)
|
|
|
|
if(n->ntest->ninit != N)
|
|
|
|
gen(n->ntest->ninit, L);
|
2008-06-04 15:37:38 -06:00
|
|
|
bgen(n->ntest, 0, breakpc); // if(!test) goto break
|
2008-08-29 21:30:19 -06:00
|
|
|
if(labloop != L) {
|
|
|
|
labloop->op = OFOR;
|
|
|
|
labloop->continpc = continpc;
|
|
|
|
labloop->breakpc = breakpc;
|
|
|
|
}
|
|
|
|
gen(n->nbody, L); // body
|
2008-06-04 15:37:38 -06:00
|
|
|
patch(gbranch(AJMP, T), continpc); // goto contin
|
|
|
|
patch(breakpc, pc); // done:
|
|
|
|
continpc = scontin;
|
|
|
|
breakpc = sbreak;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OIF:
|
|
|
|
p1 = gbranch(AJMP, T); // goto test
|
|
|
|
p2 = gbranch(AJMP, T); // p2: goto else
|
|
|
|
patch(p1, pc); // test:
|
2008-09-09 16:47:31 -06:00
|
|
|
if(n->ntest != N)
|
|
|
|
if(n->ntest->ninit != N)
|
|
|
|
gen(n->ntest->ninit, L);
|
2008-06-04 15:37:38 -06:00
|
|
|
bgen(n->ntest, 0, p2); // if(!test) goto p2
|
2008-08-29 21:30:19 -06:00
|
|
|
gen(n->nbody, L); // then
|
2008-06-04 15:37:38 -06:00
|
|
|
p3 = gbranch(AJMP, T); // goto done
|
|
|
|
patch(p2, pc); // else:
|
2008-08-29 21:30:19 -06:00
|
|
|
gen(n->nelse, L); // else
|
2008-06-04 15:37:38 -06:00
|
|
|
patch(p3, pc); // done:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OSWITCH:
|
|
|
|
p1 = gbranch(AJMP, T); // goto test
|
|
|
|
sbreak = breakpc;
|
|
|
|
breakpc = gbranch(AJMP, T); // break: goto done
|
|
|
|
patch(p1, pc); // test:
|
2008-08-29 21:30:19 -06:00
|
|
|
if(labloop != L) {
|
|
|
|
labloop->op = OFOR;
|
|
|
|
labloop->breakpc = breakpc;
|
|
|
|
}
|
2008-06-04 15:37:38 -06:00
|
|
|
swgen(n); // switch(test) body
|
|
|
|
patch(breakpc, pc); // done:
|
|
|
|
breakpc = sbreak;
|
|
|
|
break;
|
|
|
|
|
2008-07-24 16:57:30 -06:00
|
|
|
case OSELECT:
|
|
|
|
sbreak = breakpc;
|
|
|
|
p1 = gbranch(AJMP, T); // goto test
|
|
|
|
breakpc = gbranch(AJMP, T); // break: goto done
|
|
|
|
patch(p1, pc); // test:
|
2008-08-29 21:30:19 -06:00
|
|
|
if(labloop != L) {
|
|
|
|
labloop->op = OFOR;
|
|
|
|
labloop->breakpc = breakpc;
|
|
|
|
}
|
|
|
|
gen(n->nbody, L); // select() body
|
2008-07-24 16:57:30 -06:00
|
|
|
patch(breakpc, pc); // done:
|
|
|
|
breakpc = sbreak;
|
|
|
|
break;
|
|
|
|
|
2008-06-04 15:37:38 -06:00
|
|
|
case OASOP:
|
2008-06-12 15:21:09 -06:00
|
|
|
cgen_asop(n);
|
2008-06-04 15:37:38 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OAS:
|
2009-01-05 18:32:23 -07:00
|
|
|
cgen_as(n->left, n->right);
|
2008-06-04 15:37:38 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OCALLMETH:
|
2008-07-07 18:59:32 -06:00
|
|
|
cgen_callmeth(n, 0);
|
2008-06-04 15:37:38 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OCALLINTER:
|
2008-07-07 18:59:32 -06:00
|
|
|
cgen_callinter(n, N, 0);
|
2008-06-04 15:37:38 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OCALL:
|
2008-07-07 18:59:32 -06:00
|
|
|
cgen_call(n, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPROC:
|
|
|
|
cgen_proc(n);
|
2008-06-04 15:37:38 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ORETURN:
|
|
|
|
cgen_ret(n);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret:
|
2008-06-26 18:54:44 -06:00
|
|
|
lineno = lno;
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
|
|
|
|
2008-11-14 17:24:27 -07:00
|
|
|
Case*
|
|
|
|
csort(Case *l, int(*f)(Case*, Case*))
|
|
|
|
{
|
|
|
|
Case *l1, *l2, *le;
|
|
|
|
|
|
|
|
if(l == 0 || l->slink == 0)
|
|
|
|
return l;
|
|
|
|
|
|
|
|
l1 = l;
|
|
|
|
l2 = l;
|
|
|
|
for(;;) {
|
|
|
|
l2 = l2->slink;
|
|
|
|
if(l2 == 0)
|
|
|
|
break;
|
|
|
|
l2 = l2->slink;
|
|
|
|
if(l2 == 0)
|
|
|
|
break;
|
|
|
|
l1 = l1->slink;
|
|
|
|
}
|
|
|
|
|
|
|
|
l2 = l1->slink;
|
|
|
|
l1->slink = 0;
|
|
|
|
l1 = csort(l, f);
|
|
|
|
l2 = csort(l2, f);
|
|
|
|
|
|
|
|
/* set up lead element */
|
|
|
|
if((*f)(l1, l2) < 0) {
|
|
|
|
l = l1;
|
|
|
|
l1 = l1->slink;
|
|
|
|
} else {
|
|
|
|
l = l2;
|
|
|
|
l2 = l2->slink;
|
|
|
|
}
|
|
|
|
le = l;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
if(l1 == 0) {
|
|
|
|
while(l2) {
|
|
|
|
le->slink = l2;
|
|
|
|
le = l2;
|
|
|
|
l2 = l2->slink;
|
|
|
|
}
|
|
|
|
le->slink = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(l2 == 0) {
|
|
|
|
while(l1) {
|
|
|
|
le->slink = l1;
|
|
|
|
le = l1;
|
|
|
|
l1 = l1->slink;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if((*f)(l1, l2) < 0) {
|
|
|
|
le->slink = l1;
|
|
|
|
le = l1;
|
|
|
|
l1 = l1->slink;
|
|
|
|
} else {
|
|
|
|
le->slink = l2;
|
|
|
|
le = l2;
|
|
|
|
l2 = l2->slink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
le->slink = 0;
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
casecmp(Case *c1, Case *c2)
|
|
|
|
{
|
|
|
|
int w;
|
|
|
|
|
|
|
|
w = whatis(c1->scase);
|
|
|
|
if(w != whatis(c2->scase))
|
2008-11-17 13:55:22 -07:00
|
|
|
fatal("casecmp1");
|
2008-11-14 17:24:27 -07:00
|
|
|
|
|
|
|
switch(w) {
|
|
|
|
case Wlitfloat:
|
|
|
|
return mpcmpfltflt(c1->scase->val.u.fval, c2->scase->val.u.fval);
|
|
|
|
case Wlitint:
|
|
|
|
return mpcmpfixfix(c1->scase->val.u.xval, c2->scase->val.u.xval);
|
|
|
|
case Wlitstr:
|
|
|
|
return cmpslit(c1->scase, c2->scase);
|
|
|
|
// case Wlitbool:
|
|
|
|
// case Wlitnil:
|
|
|
|
}
|
|
|
|
|
2008-11-17 13:55:22 -07:00
|
|
|
fatal("casecmp2");
|
|
|
|
return 0;
|
2008-11-14 17:24:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
swconst(Case *sa, int nc, Node *n1, Node *tmp)
|
|
|
|
{
|
|
|
|
Case *s, *sb;
|
|
|
|
Prog *p1, *p2, *p3;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
// small number of cases --
|
|
|
|
// test them sequentially
|
|
|
|
if(nc < 4) {
|
|
|
|
for(s=sa; s!=C; s=s->slink) {
|
|
|
|
setlineno(s->scase);
|
|
|
|
memset(n1, 0, sizeof(*n1));
|
|
|
|
n1->op = OEQ;
|
|
|
|
n1->left = tmp;
|
|
|
|
n1->right = s->scase;
|
|
|
|
walktype(n1, Erv);
|
|
|
|
bgen(n1, 1, s->sprog);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// large number of cases --
|
|
|
|
// find the middle and recur on each half
|
|
|
|
|
|
|
|
n = nc/2;
|
|
|
|
for(s=sa; s!=C; s=s->slink) {
|
|
|
|
n--;
|
|
|
|
if(n == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
n = nc/2;
|
|
|
|
sb = s->slink;
|
|
|
|
s->slink = C;
|
|
|
|
|
|
|
|
p1 = gbranch(AJMP, T); // goto midcmp
|
|
|
|
p2 = pc; // low half of switch
|
|
|
|
swconst(sa, n, n1, tmp);
|
|
|
|
|
|
|
|
p3 = gbranch(AJMP, T); // goto end
|
|
|
|
patch(p1, pc);
|
|
|
|
|
|
|
|
setlineno(s->scase);
|
|
|
|
memset(n1, 0, sizeof(*n1));
|
|
|
|
n1->op = OLE;
|
|
|
|
n1->left = tmp;
|
|
|
|
n1->right = s->scase;
|
|
|
|
walktype(n1, Erv);
|
|
|
|
bgen(n1, 1, p2);
|
|
|
|
|
|
|
|
swconst(sb, nc-n, n1, tmp); // high half of switch
|
|
|
|
patch(p3, pc);
|
|
|
|
}
|
|
|
|
|
2008-06-04 15:37:38 -06:00
|
|
|
void
|
|
|
|
swgen(Node *n)
|
|
|
|
{
|
|
|
|
Node *c1, *c2;
|
|
|
|
Node n1, tmp;
|
2008-11-14 17:24:27 -07:00
|
|
|
Case *s0, *se, *s, *sa;
|
2008-06-04 15:37:38 -06:00
|
|
|
Prog *p1, *dflt;
|
2008-08-03 18:25:15 -06:00
|
|
|
int32 lno;
|
2008-11-19 10:49:06 -07:00
|
|
|
int any, nc;
|
2008-06-04 15:37:38 -06:00
|
|
|
Iter save1, save2;
|
|
|
|
|
|
|
|
// botch - put most of this code in
|
|
|
|
// walk. gen binary search for
|
|
|
|
// sequence of constant cases
|
|
|
|
|
2008-06-26 18:54:44 -06:00
|
|
|
lno = setlineno(n);
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
p1 = gbranch(AJMP, T);
|
|
|
|
s0 = C;
|
|
|
|
se = C;
|
|
|
|
|
|
|
|
// walk thru the body placing breaks
|
|
|
|
// and labels into the case statements
|
|
|
|
|
|
|
|
any = 0;
|
|
|
|
dflt = P;
|
|
|
|
c1 = listfirst(&save1, &n->nbody);
|
|
|
|
while(c1 != N) {
|
2008-06-28 18:27:39 -06:00
|
|
|
setlineno(c1);
|
2008-12-04 17:05:40 -07:00
|
|
|
if(c1->op == OEMPTY)
|
|
|
|
break;
|
2008-06-04 15:37:38 -06:00
|
|
|
if(c1->op != OCASE) {
|
2008-06-18 12:43:50 -06:00
|
|
|
if(s0 == C && dflt == P)
|
2008-06-04 15:37:38 -06:00
|
|
|
yyerror("unreachable statements in a switch");
|
2008-08-29 21:30:19 -06:00
|
|
|
gen(c1, L);
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
any = 1;
|
|
|
|
if(c1->op == OFALL)
|
|
|
|
any = 0;
|
|
|
|
c1 = listnext(&save1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// put in the break between cases
|
2008-08-12 15:04:03 -06:00
|
|
|
if(any)
|
2008-06-04 15:37:38 -06:00
|
|
|
patch(gbranch(AJMP, T), breakpc);
|
2008-08-12 15:04:03 -06:00
|
|
|
any = 1;
|
2008-06-04 15:37:38 -06:00
|
|
|
|
2008-11-14 17:24:27 -07:00
|
|
|
// loop over case expressions
|
2008-06-04 15:37:38 -06:00
|
|
|
c2 = listfirst(&save2, &c1->left);
|
|
|
|
if(c2 == N)
|
|
|
|
dflt = pc;
|
|
|
|
|
|
|
|
while(c2 != N) {
|
|
|
|
s = mal(sizeof(*s));
|
|
|
|
if(s0 == C)
|
|
|
|
s0 = s;
|
|
|
|
else
|
|
|
|
se->slink = s;
|
|
|
|
se = s;
|
|
|
|
|
|
|
|
s->scase = c2; // case expression
|
|
|
|
s->sprog = pc; // where to go
|
|
|
|
|
|
|
|
c2 = listnext(&save2);
|
|
|
|
}
|
|
|
|
|
|
|
|
c1 = listnext(&save1);
|
|
|
|
}
|
|
|
|
|
2008-06-28 18:27:39 -06:00
|
|
|
lineno = lno;
|
|
|
|
|
2008-06-04 15:37:38 -06:00
|
|
|
if(any)
|
|
|
|
patch(gbranch(AJMP, T), breakpc);
|
|
|
|
|
|
|
|
patch(p1, pc);
|
|
|
|
|
2008-09-09 16:47:31 -06:00
|
|
|
if(n->ntest != N)
|
|
|
|
if(n->ntest->ninit != N)
|
|
|
|
gen(n->ntest->ninit, L);
|
2008-06-04 15:37:38 -06:00
|
|
|
tempname(&tmp, n->ntest->type);
|
|
|
|
cgen(n->ntest, &tmp);
|
|
|
|
|
2008-11-14 17:24:27 -07:00
|
|
|
sa = C; // base of constant cases
|
|
|
|
nc = 0;
|
2008-06-04 15:37:38 -06:00
|
|
|
for(s=s0; s!=C; s=s->slink) {
|
2008-11-14 17:24:27 -07:00
|
|
|
switch(whatis(s->scase)) {
|
|
|
|
case Wlitfloat:
|
|
|
|
case Wlitint:
|
|
|
|
case Wlitstr:
|
|
|
|
// case Wlitbool:
|
|
|
|
// case Wlitnil:
|
|
|
|
nc++;
|
|
|
|
if(sa == C)
|
|
|
|
sa = s;
|
|
|
|
se = s;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(sa != C) {
|
|
|
|
se->slink = C;
|
|
|
|
sa = csort(sa, casecmp);
|
|
|
|
swconst(sa, nc, &n1, &tmp);
|
|
|
|
nc = 0;
|
|
|
|
sa = C;
|
|
|
|
}
|
2008-06-28 18:27:39 -06:00
|
|
|
setlineno(s->scase);
|
2008-06-04 15:37:38 -06:00
|
|
|
memset(&n1, 0, sizeof(n1));
|
|
|
|
n1.op = OEQ;
|
|
|
|
n1.left = &tmp;
|
|
|
|
n1.right = s->scase;
|
2008-06-15 21:24:30 -06:00
|
|
|
walktype(&n1, Erv);
|
2008-06-04 15:37:38 -06:00
|
|
|
bgen(&n1, 1, s->sprog);
|
|
|
|
}
|
2008-11-14 17:24:27 -07:00
|
|
|
if(sa != C) {
|
|
|
|
se->slink = C;
|
|
|
|
sa = csort(sa, casecmp);
|
|
|
|
swconst(sa, nc, &n1, &tmp);
|
|
|
|
}
|
2008-06-04 15:37:38 -06:00
|
|
|
if(dflt != P) {
|
|
|
|
patch(gbranch(AJMP, T), dflt);
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
patch(gbranch(AJMP, T), breakpc);
|
|
|
|
|
|
|
|
ret:
|
2008-06-26 18:54:44 -06:00
|
|
|
lineno = lno;
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
inarggen(void)
|
|
|
|
{
|
|
|
|
fatal("inarggen");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
genpanic(void)
|
|
|
|
{
|
|
|
|
Node n1, n2;
|
|
|
|
Prog *p;
|
|
|
|
|
|
|
|
nodconst(&n1, types[TINT64], 0xf0);
|
|
|
|
nodreg(&n2, types[TINT64], D_AX);
|
|
|
|
gins(AMOVL, &n1, &n2);
|
|
|
|
p = pc;
|
|
|
|
gins(AMOVQ, &n2, N);
|
|
|
|
p->to.type = D_INDIR+D_AX;
|
|
|
|
}
|
|
|
|
|
2009-01-05 18:32:23 -07:00
|
|
|
/*
|
|
|
|
* compute total size of f's in/out arguments.
|
|
|
|
*/
|
2008-07-08 18:19:17 -06:00
|
|
|
int
|
|
|
|
argsize(Type *t)
|
|
|
|
{
|
|
|
|
Iter save;
|
|
|
|
Type *fp;
|
|
|
|
int w, x;
|
|
|
|
|
|
|
|
w = 0;
|
|
|
|
|
|
|
|
fp = structfirst(&save, getoutarg(t));
|
|
|
|
while(fp != T) {
|
|
|
|
x = fp->width + fp->type->width;
|
|
|
|
if(x > w)
|
|
|
|
w = x;
|
|
|
|
fp = structnext(&save);
|
|
|
|
}
|
|
|
|
|
|
|
|
fp = funcfirst(&save, t);
|
|
|
|
while(fp != T) {
|
|
|
|
x = fp->width + fp->type->width;
|
|
|
|
if(x > w)
|
|
|
|
w = x;
|
2008-08-02 16:31:21 -06:00
|
|
|
fp = funcnext(&save);
|
2008-07-08 18:19:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
w = (w+7) & ~7;
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
2009-01-05 18:32:23 -07:00
|
|
|
/*
|
|
|
|
* generate:
|
|
|
|
* call f
|
|
|
|
* if proc, generate:
|
|
|
|
* push f
|
|
|
|
* push argsize
|
|
|
|
* call newproc
|
|
|
|
* pop
|
|
|
|
* pop
|
|
|
|
*/
|
2008-06-04 15:37:38 -06:00
|
|
|
void
|
2008-07-07 18:59:32 -06:00
|
|
|
ginscall(Node *f, int proc)
|
|
|
|
{
|
2008-07-08 18:19:17 -06:00
|
|
|
Node reg, con;
|
2008-07-07 18:59:32 -06:00
|
|
|
|
|
|
|
if(proc) {
|
2008-07-08 18:19:17 -06:00
|
|
|
nodreg(®, types[TINT64], D_AX);
|
2008-07-14 21:40:48 -06:00
|
|
|
if(f->op != OREGISTER) {
|
|
|
|
gins(ALEAQ, f, ®);
|
|
|
|
gins(APUSHQ, ®, N);
|
|
|
|
} else
|
|
|
|
gins(APUSHQ, f, N);
|
2008-07-08 18:19:17 -06:00
|
|
|
nodconst(&con, types[TINT32], argsize(f->type));
|
2008-07-11 20:16:39 -06:00
|
|
|
gins(APUSHQ, &con, N);
|
2008-07-07 18:59:32 -06:00
|
|
|
gins(ACALL, N, newproc);
|
2008-07-11 20:16:39 -06:00
|
|
|
gins(APOPQ, N, ®);
|
|
|
|
gins(APOPQ, N, ®);
|
2008-07-07 18:59:32 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
gins(ACALL, N, f);
|
|
|
|
}
|
|
|
|
|
2009-01-05 18:32:23 -07:00
|
|
|
/*
|
|
|
|
* n is call to interface method.
|
|
|
|
* generate res = n.
|
|
|
|
*/
|
2008-07-07 18:59:32 -06:00
|
|
|
void
|
|
|
|
cgen_callinter(Node *n, Node *res, int proc)
|
2008-06-04 15:37:38 -06:00
|
|
|
{
|
|
|
|
Node *i, *f;
|
|
|
|
Node tmpi, nodo, nodr, nodsp;
|
|
|
|
|
|
|
|
i = n->left;
|
|
|
|
if(i->op != ODOTINTER)
|
|
|
|
fatal("cgen_callinter: not ODOTINTER %O", i->op);
|
|
|
|
|
|
|
|
f = i->right; // field
|
|
|
|
if(f->op != ONAME)
|
|
|
|
fatal("cgen_callinter: not ONAME %O", f->op);
|
|
|
|
|
|
|
|
i = i->left; // interface
|
|
|
|
|
|
|
|
if(!i->addable) {
|
|
|
|
tempname(&tmpi, i->type);
|
|
|
|
cgen(i, &tmpi);
|
|
|
|
i = &tmpi;
|
|
|
|
}
|
|
|
|
|
2008-08-29 21:30:19 -06:00
|
|
|
gen(n->right, L); // args
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
regalloc(&nodr, types[tptr], res);
|
|
|
|
regalloc(&nodo, types[tptr], &nodr);
|
|
|
|
nodo.op = OINDREG;
|
|
|
|
|
|
|
|
agen(i, &nodr); // REG = &inter
|
|
|
|
|
|
|
|
nodindreg(&nodsp, types[tptr], D_SP);
|
|
|
|
nodo.xoffset += widthptr;
|
|
|
|
cgen(&nodo, &nodsp); // 0(SP) = 8(REG) -- i.s
|
|
|
|
|
|
|
|
nodo.xoffset -= widthptr;
|
|
|
|
cgen(&nodo, &nodr); // REG = 0(REG) -- i.m
|
|
|
|
|
|
|
|
nodo.xoffset = n->left->xoffset + 4*widthptr;
|
|
|
|
cgen(&nodo, &nodr); // REG = 32+offset(REG) -- i.m->fun[f]
|
|
|
|
|
2008-07-14 21:40:48 -06:00
|
|
|
// BOTCH nodr.type = fntype;
|
2008-07-07 18:59:32 -06:00
|
|
|
ginscall(&nodr, proc);
|
|
|
|
|
2008-06-04 15:37:38 -06:00
|
|
|
regfree(&nodr);
|
2008-07-07 18:59:32 -06:00
|
|
|
regfree(&nodo);
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
setmaxarg(n->left->type);
|
|
|
|
}
|
|
|
|
|
2009-01-05 18:32:23 -07:00
|
|
|
/*
|
|
|
|
* generate call to non-interface method
|
|
|
|
*/
|
2008-06-04 15:37:38 -06:00
|
|
|
void
|
2008-07-07 18:59:32 -06:00
|
|
|
cgen_callmeth(Node *n, int proc)
|
2008-06-04 15:37:38 -06:00
|
|
|
{
|
|
|
|
Node *l;
|
|
|
|
|
|
|
|
// generate a rewrite for method call
|
|
|
|
// (p.f)(...) goes to (f)(p,...)
|
|
|
|
|
|
|
|
l = n->left;
|
2008-06-11 22:06:26 -06:00
|
|
|
if(l->op != ODOTMETH)
|
|
|
|
fatal("cgen_callmeth: not dotmethod: %N");
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
n->op = OCALL;
|
|
|
|
n->left = n->left->right;
|
|
|
|
n->left->type = l->type;
|
|
|
|
|
|
|
|
if(n->left->op == ONAME)
|
|
|
|
n->left->class = PEXTERN;
|
2008-07-07 18:59:32 -06:00
|
|
|
cgen_call(n, proc);
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
|
|
|
|
2009-01-05 18:32:23 -07:00
|
|
|
/*
|
|
|
|
* generate function call;
|
|
|
|
* if proc, run call in new proc.
|
|
|
|
*/
|
2008-06-04 15:37:38 -06:00
|
|
|
void
|
2008-07-07 18:59:32 -06:00
|
|
|
cgen_call(Node *n, int proc)
|
2008-06-04 15:37:38 -06:00
|
|
|
{
|
|
|
|
Type *t;
|
2008-10-03 17:23:02 -06:00
|
|
|
Node nod, afun;
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
if(n == N)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(n->left->ullman >= UINF) {
|
|
|
|
// if name involves a fn call
|
|
|
|
// precompute the address of the fn
|
|
|
|
tempname(&afun, types[tptr]);
|
|
|
|
if(isptr[n->left->type->etype])
|
|
|
|
cgen(n->left, &afun);
|
|
|
|
else
|
|
|
|
agen(n->left, &afun);
|
|
|
|
}
|
|
|
|
|
2008-08-29 21:30:19 -06:00
|
|
|
gen(n->right, L); // assign the args
|
2008-06-04 15:37:38 -06:00
|
|
|
t = n->left->type;
|
|
|
|
if(isptr[t->etype])
|
|
|
|
t = t->type;
|
|
|
|
|
|
|
|
setmaxarg(t);
|
|
|
|
|
|
|
|
// call tempname pointer
|
|
|
|
if(n->left->ullman >= UINF) {
|
|
|
|
regalloc(&nod, types[tptr], N);
|
2009-01-05 18:32:23 -07:00
|
|
|
cgen_as(&nod, &afun);
|
2008-07-14 21:40:48 -06:00
|
|
|
nod.type = t;
|
2008-07-07 18:59:32 -06:00
|
|
|
ginscall(&nod, proc);
|
2008-06-04 15:37:38 -06:00
|
|
|
regfree(&nod);
|
2008-06-26 18:54:44 -06:00
|
|
|
goto ret;
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// call pointer
|
|
|
|
if(isptr[n->left->type->etype]) {
|
|
|
|
regalloc(&nod, types[tptr], N);
|
2009-01-05 18:32:23 -07:00
|
|
|
cgen_as(&nod, n->left);
|
2008-07-14 21:40:48 -06:00
|
|
|
nod.type = t;
|
2008-07-07 18:59:32 -06:00
|
|
|
ginscall(&nod, proc);
|
2008-06-04 15:37:38 -06:00
|
|
|
regfree(&nod);
|
2008-06-26 18:54:44 -06:00
|
|
|
goto ret;
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// call direct
|
2008-06-11 22:06:26 -06:00
|
|
|
n->left->method = 1;
|
2008-07-07 18:59:32 -06:00
|
|
|
ginscall(n->left, proc);
|
2008-06-26 18:54:44 -06:00
|
|
|
|
|
|
|
ret:
|
2008-06-28 18:27:39 -06:00
|
|
|
;
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
|
|
|
|
2009-01-05 18:32:23 -07:00
|
|
|
/*
|
|
|
|
* generate code to start new proc running call n.
|
|
|
|
*/
|
2008-07-07 18:59:32 -06:00
|
|
|
void
|
|
|
|
cgen_proc(Node *n)
|
|
|
|
{
|
|
|
|
switch(n->left->op) {
|
|
|
|
default:
|
|
|
|
fatal("cgen_proc: unknown call %O", n->left->op);
|
|
|
|
|
|
|
|
case OCALLMETH:
|
|
|
|
cgen_callmeth(n->left, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OCALLINTER:
|
|
|
|
cgen_callinter(n->left, N, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OCALL:
|
|
|
|
cgen_call(n->left, 1);
|
|
|
|
break;
|
|
|
|
}
|
2008-10-06 17:44:17 -06:00
|
|
|
|
2008-07-07 18:59:32 -06:00
|
|
|
}
|
|
|
|
|
2009-01-05 18:32:23 -07:00
|
|
|
/*
|
|
|
|
* call to n has already been generated.
|
|
|
|
* generate:
|
|
|
|
* res = return value from call.
|
|
|
|
*/
|
2008-06-04 15:37:38 -06:00
|
|
|
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)
|
2008-06-24 11:30:33 -06:00
|
|
|
fatal("cgen_callret: nil");
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
memset(&nod, 0, sizeof(nod));
|
|
|
|
nod.op = OINDREG;
|
2008-08-08 18:13:31 -06:00
|
|
|
nod.val.u.reg = D_SP;
|
2008-06-04 15:37:38 -06:00
|
|
|
nod.addable = 1;
|
|
|
|
|
|
|
|
nod.xoffset = fp->width;
|
|
|
|
nod.type = fp->type;
|
2009-01-05 18:32:23 -07:00
|
|
|
cgen_as(res, &nod);
|
2008-06-21 16:11:29 -06:00
|
|
|
}
|
|
|
|
|
2009-01-05 18:32:23 -07:00
|
|
|
/*
|
|
|
|
* call to n has already been generated.
|
|
|
|
* generate:
|
|
|
|
* res = &return value from call.
|
|
|
|
*/
|
2008-06-21 16:11:29 -06:00
|
|
|
void
|
|
|
|
cgen_aret(Node *n, Node *res)
|
|
|
|
{
|
2008-12-20 17:05:12 -07:00
|
|
|
Node nod1, nod2;
|
2008-06-21 16:11:29 -06:00
|
|
|
Type *fp, *t;
|
|
|
|
Iter flist;
|
|
|
|
|
|
|
|
t = n->left->type;
|
2008-06-24 11:30:33 -06:00
|
|
|
if(isptr[t->etype])
|
2008-06-21 16:11:29 -06:00
|
|
|
t = t->type;
|
|
|
|
|
|
|
|
fp = structfirst(&flist, getoutarg(t));
|
|
|
|
if(fp == T)
|
|
|
|
fatal("cgen_aret: nil");
|
2008-06-24 11:30:33 -06:00
|
|
|
|
2008-06-21 16:11:29 -06:00
|
|
|
memset(&nod1, 0, sizeof(nod1));
|
|
|
|
nod1.op = OINDREG;
|
2008-08-08 18:13:31 -06:00
|
|
|
nod1.val.u.reg = D_SP;
|
2008-06-21 16:11:29 -06:00
|
|
|
nod1.addable = 1;
|
|
|
|
|
|
|
|
nod1.xoffset = fp->width;
|
|
|
|
nod1.type = fp->type;
|
|
|
|
|
2008-12-20 17:05:12 -07:00
|
|
|
if(res->op != OREGISTER) {
|
|
|
|
regalloc(&nod2, types[tptr], res);
|
|
|
|
gins(ALEAQ, &nod1, &nod2);
|
|
|
|
gins(AMOVQ, &nod2, res);
|
2008-12-20 17:07:56 -07:00
|
|
|
regfree(&nod2);
|
2008-12-20 17:05:12 -07:00
|
|
|
} else
|
|
|
|
gins(ALEAQ, &nod1, res);
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
|
|
|
|
2009-01-05 18:32:23 -07:00
|
|
|
/*
|
|
|
|
* generate return.
|
|
|
|
* n->left is assignments to return values.
|
|
|
|
*/
|
2008-06-04 15:37:38 -06:00
|
|
|
void
|
|
|
|
cgen_ret(Node *n)
|
|
|
|
{
|
2008-08-29 21:30:19 -06:00
|
|
|
gen(n->left, L); // copy out args
|
2008-06-04 15:37:38 -06:00
|
|
|
gins(ARET, N, N);
|
|
|
|
}
|
|
|
|
|
2009-01-05 18:32:23 -07:00
|
|
|
/*
|
|
|
|
* generate += *= etc.
|
|
|
|
*/
|
2008-06-04 15:37:38 -06:00
|
|
|
void
|
2008-06-12 15:21:09 -06:00
|
|
|
cgen_asop(Node *n)
|
2008-06-04 15:37:38 -06:00
|
|
|
{
|
2008-06-12 15:21:09 -06:00
|
|
|
Node n1, n2, n3, n4;
|
|
|
|
Node *nl, *nr;
|
2008-12-14 19:45:00 -07:00
|
|
|
Prog *p1;
|
|
|
|
Addr addr;
|
2008-06-26 18:54:44 -06:00
|
|
|
|
2008-06-12 15:21:09 -06:00
|
|
|
nl = n->left;
|
|
|
|
nr = n->right;
|
2008-06-04 15:37:38 -06:00
|
|
|
|
2008-12-14 19:45:00 -07:00
|
|
|
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;
|
|
|
|
|
2008-11-22 18:58:53 -07:00
|
|
|
switch(n->etype) {
|
|
|
|
case OADD:
|
2008-12-14 19:45:00 -07:00
|
|
|
if(smallintconst(nr))
|
|
|
|
if(mpgetfix(nr->val.u.xval) == 1) {
|
|
|
|
if(nl->addable) {
|
|
|
|
gins(optoas(OINC, nl->type), N, nl);
|
|
|
|
goto ret;
|
|
|
|
}
|
2009-01-06 12:23:58 -07:00
|
|
|
if(sudoaddable(nl, &addr)) {
|
2008-12-14 19:45:00 -07:00
|
|
|
p1 = gins(optoas(OINC, nl->type), N, N);
|
|
|
|
p1->to = addr;
|
|
|
|
sudoclean();
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-11-22 18:58:53 -07:00
|
|
|
case OSUB:
|
2008-12-14 19:45:00 -07:00
|
|
|
if(smallintconst(nr))
|
|
|
|
if(mpgetfix(nr->val.u.xval) == 1) {
|
|
|
|
if(nl->addable) {
|
|
|
|
gins(optoas(ODEC, nl->type), N, nl);
|
|
|
|
goto ret;
|
|
|
|
}
|
2009-01-06 12:23:58 -07:00
|
|
|
if(sudoaddable(nl, &addr)) {
|
2008-12-14 19:45:00 -07:00
|
|
|
p1 = gins(optoas(ODEC, nl->type), N, N);
|
|
|
|
p1->to = addr;
|
|
|
|
sudoclean();
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2008-11-24 18:51:26 -07:00
|
|
|
}
|
2008-11-22 18:58:53 -07:00
|
|
|
|
2008-11-24 18:51:26 -07:00
|
|
|
switch(n->etype) {
|
2008-12-14 19:45:00 -07:00
|
|
|
case OADD:
|
|
|
|
case OSUB:
|
2008-11-22 18:58:53 -07:00
|
|
|
case OXOR:
|
|
|
|
case OAND:
|
|
|
|
case OOR:
|
2008-12-14 19:45:00 -07:00
|
|
|
if(nl->addable) {
|
|
|
|
if(smallintconst(nr)) {
|
|
|
|
gins(optoas(n->etype, nl->type), nr, nl);
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
regalloc(&n2, nr->type, N);
|
|
|
|
cgen(nr, &n2);
|
|
|
|
gins(optoas(n->etype, nl->type), &n2, nl);
|
|
|
|
regfree(&n2);
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
if(nr->ullman < UINF)
|
2009-01-06 12:23:58 -07:00
|
|
|
if(sudoaddable(nl, &addr)) {
|
2008-12-14 19:45:00 -07:00
|
|
|
if(smallintconst(nr)) {
|
|
|
|
p1 = gins(optoas(n->etype, nl->type), nr, N);
|
|
|
|
p1->to = addr;
|
|
|
|
sudoclean();
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
regalloc(&n2, nr->type, N);
|
|
|
|
cgen(nr, &n2);
|
|
|
|
p1 = gins(optoas(n->etype, nl->type), &n2, N);
|
|
|
|
p1->to = addr;
|
|
|
|
regfree(&n2);
|
|
|
|
sudoclean();
|
|
|
|
goto ret;
|
|
|
|
}
|
2008-06-06 17:49:35 -06:00
|
|
|
}
|
2008-06-04 15:37:38 -06:00
|
|
|
|
2008-12-14 19:45:00 -07:00
|
|
|
hard:
|
2008-06-12 15:21:09 -06:00
|
|
|
if(nr->ullman > nl->ullman) {
|
2008-11-01 18:53:12 -06:00
|
|
|
regalloc(&n2, nr->type, N);
|
2008-06-12 15:21:09 -06:00
|
|
|
cgen(nr, &n2);
|
|
|
|
igen(nl, &n1, N);
|
|
|
|
} else {
|
|
|
|
igen(nl, &n1, N);
|
2008-11-01 18:53:12 -06:00
|
|
|
regalloc(&n2, nr->type, N);
|
2008-06-10 22:29:57 -06:00
|
|
|
cgen(nr, &n2);
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
|
|
|
|
2008-06-12 15:21:09 -06:00
|
|
|
n3 = *n;
|
|
|
|
n3.left = &n1;
|
|
|
|
n3.right = &n2;
|
|
|
|
n3.op = n->etype;
|
|
|
|
|
2008-11-01 18:53:12 -06:00
|
|
|
regalloc(&n4, nl->type, N);
|
2008-06-12 15:21:09 -06:00
|
|
|
cgen(&n3, &n4);
|
|
|
|
gmove(&n4, &n1);
|
2008-06-10 22:29:57 -06:00
|
|
|
|
2008-06-04 15:37:38 -06:00
|
|
|
regfree(&n1);
|
|
|
|
regfree(&n2);
|
2008-06-12 15:21:09 -06:00
|
|
|
regfree(&n4);
|
2008-06-28 18:27:39 -06:00
|
|
|
|
|
|
|
ret:
|
|
|
|
;
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
|
|
|
|
2009-01-05 18:32:23 -07:00
|
|
|
/*
|
|
|
|
* generate assignment:
|
|
|
|
* nl = nr
|
|
|
|
* nr == N means zero nl.
|
|
|
|
*/
|
2008-06-04 15:37:38 -06:00
|
|
|
void
|
2009-01-05 18:32:23 -07:00
|
|
|
cgen_as(Node *nl, Node *nr)
|
2008-06-04 15:37:38 -06:00
|
|
|
{
|
|
|
|
Node nc, n1;
|
|
|
|
Type *tl;
|
2008-12-15 16:07:35 -07:00
|
|
|
uint32 w, c, q;
|
2008-11-22 18:58:53 -07:00
|
|
|
int iszer;
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
if(nl == N)
|
|
|
|
return;
|
|
|
|
|
2008-11-22 18:58:53 -07:00
|
|
|
iszer = 0;
|
2008-06-17 19:07:40 -06:00
|
|
|
if(nr == N || isnil(nr)) {
|
2008-10-01 15:16:57 -06:00
|
|
|
if(nl->op == OLIST) {
|
2009-01-05 18:32:23 -07:00
|
|
|
cgen_as(nl->left, nr);
|
|
|
|
cgen_as(nl->right, nr);
|
2008-10-01 15:16:57 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
tl = nl->type;
|
|
|
|
if(tl == T)
|
|
|
|
return;
|
2008-06-04 15:37:38 -06:00
|
|
|
if(isfat(tl)) {
|
|
|
|
/* clear a fat object */
|
|
|
|
if(debug['g'])
|
|
|
|
dump("\nclearfat", nl);
|
|
|
|
|
|
|
|
w = nl->type->width;
|
2008-12-15 16:07:35 -07:00
|
|
|
c = w % 8; // bytes
|
|
|
|
q = w / 8; // quads
|
2008-10-06 17:44:17 -06:00
|
|
|
|
2008-12-15 16:07:35 -07:00
|
|
|
gconreg(AMOVQ, 0, D_AX);
|
|
|
|
nodreg(&n1, types[tptr], D_DI);
|
|
|
|
agen(nl, &n1);
|
2008-06-04 15:37:38 -06:00
|
|
|
|
2008-12-15 16:07:35 -07:00
|
|
|
if(q >= 4) {
|
|
|
|
gconreg(AMOVQ, q, D_CX);
|
2008-06-04 15:37:38 -06:00
|
|
|
gins(AREP, N, N); // repeat
|
|
|
|
gins(ASTOSQ, N, N); // STOQ AL,*(DI)+
|
2008-12-15 16:07:35 -07:00
|
|
|
} else
|
|
|
|
while(q > 0) {
|
|
|
|
gins(ASTOSQ, N, N); // STOQ AL,*(DI)+
|
|
|
|
q--;
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
|
|
|
|
2008-12-15 16:07:35 -07:00
|
|
|
if(c >= 4) {
|
2008-06-04 15:37:38 -06:00
|
|
|
gconreg(AMOVQ, c, D_CX);
|
|
|
|
gins(AREP, N, N); // repeat
|
|
|
|
gins(ASTOSB, N, N); // STOB AL,*(DI)+
|
2008-12-15 16:07:35 -07:00
|
|
|
} else
|
|
|
|
while(c > 0) {
|
|
|
|
gins(ASTOSB, N, N); // STOB AL,*(DI)+
|
|
|
|
c--;
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
2008-06-26 18:54:44 -06:00
|
|
|
goto ret;
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* invent a "zero" for the rhs */
|
2008-11-22 18:58:53 -07:00
|
|
|
iszer = 1;
|
2008-06-04 15:37:38 -06:00
|
|
|
nr = &nc;
|
|
|
|
memset(nr, 0, sizeof(*nr));
|
2009-01-09 12:13:39 -07:00
|
|
|
switch(simtype[tl->etype]) {
|
2008-06-04 15:37:38 -06:00
|
|
|
default:
|
|
|
|
fatal("cgen_as: tl %T", tl);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TINT8:
|
|
|
|
case TUINT8:
|
|
|
|
case TINT16:
|
|
|
|
case TUINT16:
|
|
|
|
case TINT32:
|
|
|
|
case TUINT32:
|
|
|
|
case TINT64:
|
|
|
|
case TUINT64:
|
2008-08-08 18:13:31 -06:00
|
|
|
nr->val.u.xval = mal(sizeof(*nr->val.u.xval));
|
|
|
|
mpmovecfix(nr->val.u.xval, 0);
|
2008-06-04 15:37:38 -06:00
|
|
|
nr->val.ctype = CTINT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TFLOAT32:
|
|
|
|
case TFLOAT64:
|
|
|
|
case TFLOAT80:
|
2008-08-08 18:13:31 -06:00
|
|
|
nr->val.u.fval = mal(sizeof(*nr->val.u.fval));
|
|
|
|
mpmovecflt(nr->val.u.fval, 0.0);
|
2008-06-04 15:37:38 -06:00
|
|
|
nr->val.ctype = CTFLT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TBOOL:
|
2008-08-08 18:13:31 -06:00
|
|
|
nr->val.u.bval = 0;
|
2008-06-04 15:37:38 -06:00
|
|
|
nr->val.ctype = CTBOOL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TPTR32:
|
|
|
|
case TPTR64:
|
|
|
|
nr->val.ctype = CTNIL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
nr->op = OLITERAL;
|
|
|
|
nr->type = tl;
|
|
|
|
nr->addable = 1;
|
|
|
|
ullmancalc(nr);
|
|
|
|
}
|
2008-06-06 17:49:35 -06:00
|
|
|
|
2008-10-01 15:16:57 -06:00
|
|
|
tl = nl->type;
|
|
|
|
if(tl == T)
|
|
|
|
return;
|
|
|
|
|
2008-06-04 15:37:38 -06:00
|
|
|
cgen(nr, nl);
|
2008-11-22 18:58:53 -07:00
|
|
|
if(iszer && nl->addable)
|
|
|
|
gins(ANOP, nl, N); // used
|
2009-01-09 12:13:39 -07:00
|
|
|
|
2008-06-26 18:54:44 -06:00
|
|
|
|
|
|
|
ret:
|
2008-06-28 18:27:39 -06:00
|
|
|
;
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
2008-06-06 21:43:29 -06:00
|
|
|
|
2008-06-06 22:21:54 -06:00
|
|
|
int
|
|
|
|
samereg(Node *a, Node *b)
|
|
|
|
{
|
|
|
|
if(a->op != OREGISTER)
|
|
|
|
return 0;
|
|
|
|
if(b->op != OREGISTER)
|
|
|
|
return 0;
|
2008-08-08 18:13:31 -06:00
|
|
|
if(a->val.u.reg != b->val.u.reg)
|
2008-06-06 22:21:54 -06:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-01-05 18:32:23 -07:00
|
|
|
/*
|
|
|
|
* generate division.
|
|
|
|
* caller must set:
|
|
|
|
* ax = allocated AX register
|
|
|
|
* dx = allocated DX register
|
|
|
|
* generates one of:
|
|
|
|
* res = nl / nr
|
|
|
|
* res = nl % nr
|
|
|
|
* according to op.
|
|
|
|
*/
|
2008-06-06 21:43:29 -06:00
|
|
|
void
|
2008-07-17 17:22:40 -06:00
|
|
|
dodiv(int op, Node *nl, Node *nr, Node *res, Node *ax, Node *dx)
|
2008-06-06 21:43:29 -06:00
|
|
|
{
|
2008-07-17 16:03:39 -06:00
|
|
|
int a;
|
2008-11-03 14:27:16 -07:00
|
|
|
Node n3, n4;
|
2008-09-15 16:03:10 -06:00
|
|
|
Type *t;
|
2008-06-06 22:21:54 -06:00
|
|
|
|
2008-09-15 16:03:10 -06:00
|
|
|
t = nl->type;
|
|
|
|
if(t->width == 1) {
|
|
|
|
if(issigned[t->etype])
|
|
|
|
t = types[TINT32];
|
|
|
|
else
|
|
|
|
t = types[TUINT32];
|
|
|
|
}
|
|
|
|
a = optoas(op, t);
|
2008-06-06 21:43:29 -06:00
|
|
|
|
2008-06-07 16:21:02 -06:00
|
|
|
regalloc(&n3, nr->type, N);
|
2008-06-06 21:43:29 -06:00
|
|
|
if(nl->ullman >= nr->ullman) {
|
2008-07-17 17:22:40 -06:00
|
|
|
cgen(nl, ax);
|
2008-11-03 14:27:16 -07:00
|
|
|
if(!issigned[t->etype]) {
|
|
|
|
nodconst(&n4, t, 0);
|
|
|
|
gmove(&n4, dx);
|
|
|
|
} else
|
2009-01-05 18:32:23 -07:00
|
|
|
gins(optoas(OEXTEND, t), N, N);
|
2008-06-06 22:21:54 -06:00
|
|
|
cgen(nr, &n3);
|
2008-06-06 21:43:29 -06:00
|
|
|
} else {
|
|
|
|
cgen(nr, &n3);
|
2008-07-17 17:22:40 -06:00
|
|
|
cgen(nl, ax);
|
2008-11-03 14:27:16 -07:00
|
|
|
if(!issigned[t->etype]) {
|
|
|
|
nodconst(&n4, t, 0);
|
|
|
|
gmove(&n4, dx);
|
|
|
|
} else
|
2009-01-05 18:32:23 -07:00
|
|
|
gins(optoas(OEXTEND, t), N, N);
|
2008-06-06 21:43:29 -06:00
|
|
|
}
|
2008-11-03 14:27:16 -07:00
|
|
|
gins(a, &n3, N);
|
2008-06-06 22:21:54 -06:00
|
|
|
regfree(&n3);
|
2008-06-06 21:43:29 -06:00
|
|
|
|
|
|
|
if(op == ODIV)
|
2008-07-17 17:22:40 -06:00
|
|
|
gmove(ax, res);
|
2008-06-06 21:43:29 -06:00
|
|
|
else
|
2008-07-17 17:22:40 -06:00
|
|
|
gmove(dx, res);
|
2008-07-17 16:03:39 -06:00
|
|
|
}
|
2008-06-26 18:54:44 -06:00
|
|
|
|
2009-01-05 18:32:23 -07:00
|
|
|
/*
|
|
|
|
* generate division according to op, one of:
|
|
|
|
* res = nl / nr
|
|
|
|
* res = nl % nr
|
|
|
|
*/
|
2008-07-17 16:03:39 -06:00
|
|
|
void
|
|
|
|
cgen_div(int op, Node *nl, Node *nr, Node *res)
|
|
|
|
{
|
2008-12-13 17:41:47 -07:00
|
|
|
Node ax, dx;
|
2008-10-03 17:23:02 -06:00
|
|
|
int rax, rdx;
|
2008-07-17 16:03:39 -06:00
|
|
|
|
|
|
|
rax = reg[D_AX];
|
|
|
|
rdx = reg[D_DX];
|
|
|
|
|
2008-07-17 17:22:40 -06:00
|
|
|
nodreg(&ax, types[TINT64], D_AX);
|
|
|
|
nodreg(&dx, types[TINT64], D_DX);
|
|
|
|
regalloc(&ax, nl->type, &ax);
|
|
|
|
regalloc(&dx, nl->type, &dx);
|
2008-07-17 16:03:39 -06:00
|
|
|
|
2008-07-17 17:22:40 -06:00
|
|
|
dodiv(op, nl, nr, res, &ax, &dx);
|
|
|
|
|
|
|
|
regfree(&ax);
|
|
|
|
regfree(&dx);
|
2008-06-06 21:43:29 -06:00
|
|
|
}
|
2008-06-07 16:21:02 -06:00
|
|
|
|
2009-01-05 18:32:23 -07:00
|
|
|
/*
|
|
|
|
* generate shift according to op, one of:
|
|
|
|
* res = nl << nr
|
|
|
|
* res = nl >> nr
|
|
|
|
*/
|
2008-06-07 16:21:02 -06:00
|
|
|
void
|
|
|
|
cgen_shift(int op, Node *nl, Node *nr, Node *res)
|
|
|
|
{
|
2008-08-28 20:59:42 -06:00
|
|
|
Node n1, n2, n3;
|
2008-06-07 16:21:02 -06:00
|
|
|
int a, rcl;
|
2008-08-28 20:59:42 -06:00
|
|
|
Prog *p1;
|
2008-06-07 16:21:02 -06:00
|
|
|
|
|
|
|
a = optoas(op, nl->type);
|
|
|
|
|
|
|
|
if(nr->op == OLITERAL) {
|
2008-10-29 13:46:44 -06:00
|
|
|
regalloc(&n1, nl->type, res);
|
2008-06-07 16:21:02 -06:00
|
|
|
cgen(nl, &n1);
|
2008-11-18 18:15:42 -07:00
|
|
|
if(mpgetfix(nr->val.u.xval) >= nl->type->width*8) {
|
|
|
|
// large shift gets 2 shifts by width
|
|
|
|
nodconst(&n3, types[TUINT32], nl->type->width*8-1);
|
|
|
|
gins(a, &n3, &n1);
|
|
|
|
gins(a, &n3, &n1);
|
|
|
|
} else
|
|
|
|
gins(a, nr, &n1);
|
2008-06-07 16:21:02 -06:00
|
|
|
gmove(&n1, res);
|
|
|
|
regfree(&n1);
|
2008-06-26 18:54:44 -06:00
|
|
|
goto ret;
|
2008-06-07 16:21:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
rcl = reg[D_CX];
|
|
|
|
|
|
|
|
nodreg(&n1, types[TINT64], D_CX);
|
|
|
|
regalloc(&n1, nr->type, &n1);
|
|
|
|
|
2008-12-13 17:41:47 -07:00
|
|
|
regalloc(&n2, nl->type, res);
|
2008-06-07 16:21:02 -06:00
|
|
|
if(nl->ullman >= nr->ullman) {
|
|
|
|
cgen(nl, &n2);
|
|
|
|
cgen(nr, &n1);
|
|
|
|
} else {
|
|
|
|
cgen(nr, &n1);
|
|
|
|
cgen(nl, &n2);
|
|
|
|
}
|
2008-08-28 20:59:42 -06:00
|
|
|
// test and fix up large shifts
|
|
|
|
nodconst(&n3, types[TUINT32], nl->type->width*8);
|
|
|
|
gins(optoas(OCMP, types[TUINT32]), &n1, &n3);
|
|
|
|
p1 = gbranch(optoas(OLT, types[TUINT32]), T);
|
|
|
|
if(op == ORSH && issigned[nl->type->etype]) {
|
|
|
|
nodconst(&n3, types[TUINT32], nl->type->width*8-1);
|
|
|
|
gins(a, &n3, &n2);
|
|
|
|
} else {
|
|
|
|
nodconst(&n3, nl->type, 0);
|
|
|
|
gmove(&n3, &n2);
|
|
|
|
}
|
|
|
|
patch(p1, pc);
|
2008-06-07 16:21:02 -06:00
|
|
|
gins(a, &n1, &n2);
|
2008-08-28 20:59:42 -06:00
|
|
|
|
2008-06-07 16:21:02 -06:00
|
|
|
gmove(&n2, res);
|
|
|
|
|
|
|
|
regfree(&n1);
|
|
|
|
regfree(&n2);
|
2008-06-26 18:54:44 -06:00
|
|
|
|
|
|
|
ret:
|
2008-06-28 18:27:39 -06:00
|
|
|
;
|
2008-06-07 16:21:02 -06:00
|
|
|
}
|
2008-08-29 21:30:19 -06:00
|
|
|
|
2009-01-05 18:32:23 -07:00
|
|
|
/*
|
|
|
|
* generate byte multiply:
|
|
|
|
* res = nl * nr
|
|
|
|
* no byte multiply instruction so have to do
|
|
|
|
* 16-bit multiply and take bottom half.
|
|
|
|
*/
|
2008-11-07 15:20:32 -07:00
|
|
|
void
|
|
|
|
cgen_bmul(int op, Node *nl, Node *nr, Node *res)
|
|
|
|
{
|
2009-01-05 18:32:23 -07:00
|
|
|
Node n1b, n2b, n1w, n2w;
|
2008-11-07 15:20:32 -07:00
|
|
|
Type *t;
|
|
|
|
int a;
|
|
|
|
|
|
|
|
if(nl->ullman >= nr->ullman) {
|
2009-01-05 18:32:23 -07:00
|
|
|
regalloc(&n1b, nl->type, res);
|
|
|
|
cgen(nl, &n1b);
|
|
|
|
regalloc(&n2b, nr->type, N);
|
|
|
|
cgen(nr, &n2b);
|
2008-11-07 15:20:32 -07:00
|
|
|
} else {
|
2009-01-05 18:32:23 -07:00
|
|
|
regalloc(&n2b, nr->type, N);
|
|
|
|
cgen(nr, &n2b);
|
|
|
|
regalloc(&n1b, nl->type, res);
|
|
|
|
cgen(nl, &n1b);
|
2008-11-07 15:20:32 -07:00
|
|
|
}
|
2008-11-07 17:05:17 -07:00
|
|
|
|
2009-01-05 18:32:23 -07:00
|
|
|
// copy from byte to short registers
|
2008-11-07 17:05:17 -07:00
|
|
|
t = types[TUINT16];
|
|
|
|
if(issigned[nl->type->etype])
|
|
|
|
t = types[TINT16];
|
|
|
|
|
2009-01-05 18:32:23 -07:00
|
|
|
regalloc(&n2w, t, &n2b);
|
|
|
|
cgen(&n2b, &n2w);
|
2008-11-07 17:05:17 -07:00
|
|
|
|
2009-01-05 18:32:23 -07:00
|
|
|
regalloc(&n1w, t, &n1b);
|
|
|
|
cgen(&n1b, &n1w);
|
2008-11-07 17:05:17 -07:00
|
|
|
|
2008-11-07 15:20:32 -07:00
|
|
|
a = optoas(op, t);
|
2009-01-05 18:32:23 -07:00
|
|
|
gins(a, &n2w, &n1w);
|
|
|
|
cgen(&n1w, &n1b);
|
|
|
|
cgen(&n1b, res);
|
|
|
|
|
|
|
|
regfree(&n1w);
|
|
|
|
regfree(&n2w);
|
|
|
|
regfree(&n1b);
|
|
|
|
regfree(&n2b);
|
2008-11-07 15:20:32 -07:00
|
|
|
}
|
|
|
|
|
2008-08-29 21:30:19 -06:00
|
|
|
void
|
|
|
|
checklabels(void)
|
|
|
|
{
|
|
|
|
Label *l, *m;
|
|
|
|
Sym *s;
|
|
|
|
|
|
|
|
// // print the label list
|
|
|
|
// for(l=labellist; l!=L; l=l->link) {
|
|
|
|
// print("lab %O %S\n", l->op, l->sym);
|
|
|
|
// }
|
|
|
|
|
|
|
|
for(l=labellist; l!=L; l=l->link) {
|
|
|
|
switch(l->op) {
|
|
|
|
case OFOR:
|
|
|
|
case OLABEL:
|
|
|
|
// these are definitions -
|
|
|
|
s = l->sym;
|
|
|
|
for(m=labellist; m!=L; m=m->link) {
|
|
|
|
if(m->sym != s)
|
|
|
|
continue;
|
|
|
|
switch(m->op) {
|
|
|
|
case OFOR:
|
|
|
|
case OLABEL:
|
|
|
|
// these are definitions -
|
|
|
|
// look for redefinitions
|
|
|
|
if(l != m)
|
|
|
|
yyerror("label %S redefined", s);
|
|
|
|
break;
|
|
|
|
case OGOTO:
|
|
|
|
// these are references -
|
|
|
|
// patch to definition
|
|
|
|
patch(m->label, l->label);
|
|
|
|
m->sym = S; // mark done
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// diagnostic for all undefined references
|
|
|
|
for(l=labellist; l!=L; l=l->link)
|
|
|
|
if(l->op == OGOTO && l->sym != S)
|
|
|
|
yyerror("label %S not defined", l->sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
Label*
|
|
|
|
findlab(Sym *s)
|
|
|
|
{
|
|
|
|
Label *l;
|
|
|
|
|
|
|
|
for(l=labellist; l!=L; l=l->link) {
|
|
|
|
if(l->sym != s)
|
|
|
|
continue;
|
|
|
|
if(l->op != OFOR)
|
|
|
|
continue;
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
return L;
|
|
|
|
}
|