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
|
|
|
|
2009-03-30 20:15:07 -06:00
|
|
|
if(newproc == N) {
|
2009-01-30 15:39:42 -07:00
|
|
|
newproc = sysfunc("newproc");
|
|
|
|
deferproc = sysfunc("deferproc");
|
|
|
|
deferreturn = sysfunc("deferreturn");
|
|
|
|
throwindex = sysfunc("throwindex");
|
2009-08-28 13:37:39 -06:00
|
|
|
throwslice = sysfunc("throwslice");
|
2009-01-30 15:39:42 -07:00
|
|
|
throwreturn = sysfunc("throwreturn");
|
2009-03-30 20:15:07 -06:00
|
|
|
}
|
2008-09-12 17:48:35 -06:00
|
|
|
|
2009-07-17 02:00:44 -06:00
|
|
|
if(fn->nbody == nil)
|
2008-06-04 15:37:38 -06:00
|
|
|
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)
|
2009-07-17 02:00:44 -06:00
|
|
|
curfn->nbody = concat(list1(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-27 13:03:53 -07:00
|
|
|
hasdefer = 0;
|
2008-06-04 15:37:38 -06:00
|
|
|
walk(curfn);
|
2009-09-09 00:16:19 -06:00
|
|
|
if(nerrors != 0 || isblank(curfn->nname))
|
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;
|
|
|
|
|
|
|
|
nodconst(&nod1, types[TINT32], 0);
|
|
|
|
ptxt = gins(ATEXT, curfn->nname, &nod1);
|
2009-01-30 15:39:42 -07:00
|
|
|
afunclit(&ptxt->from);
|
2008-06-04 15:37:38 -06:00
|
|
|
|
|
|
|
ginit();
|
2009-07-17 02:00:44 -06:00
|
|
|
genlist(curfn->enter);
|
|
|
|
genlist(curfn->nbody);
|
2008-06-04 15:37:38 -06:00
|
|
|
gclean();
|
2008-08-29 21:30:19 -06:00
|
|
|
checklabels();
|
2009-07-30 12:03:58 -06:00
|
|
|
if(nerrors != 0)
|
|
|
|
goto ret;
|
2008-06-04 15:37:38 -06:00
|
|
|
|
2009-01-30 15:39:42 -07:00
|
|
|
if(curfn->type->outtuple != 0)
|
|
|
|
ginscall(throwreturn, 0);
|
2008-06-04 15:37:38 -06:00
|
|
|
|
2009-01-27 13:03:53 -07:00
|
|
|
if(hasdefer)
|
2009-01-30 15:39:42 -07:00
|
|
|
ginscall(deferreturn, 0);
|
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
|
|
|
}
|
|
|
|
|
2009-01-05 18:32:23 -07:00
|
|
|
/*
|
|
|
|
* generate:
|
|
|
|
* call f
|
2009-01-27 13:03:53 -07:00
|
|
|
* proc=0 normal call
|
|
|
|
* proc=1 goroutine run in new proc
|
|
|
|
* proc=2 defer call save away stack
|
2009-01-05 18:32:23 -07:00
|
|
|
*/
|
2008-06-04 15:37:38 -06:00
|
|
|
void
|
2008-07-07 18:59:32 -06:00
|
|
|
ginscall(Node *f, int proc)
|
|
|
|
{
|
2009-01-30 15:39:42 -07:00
|
|
|
Prog *p;
|
2008-07-08 18:19:17 -06:00
|
|
|
Node reg, con;
|
2008-07-07 18:59:32 -06:00
|
|
|
|
2009-01-27 13:03:53 -07:00
|
|
|
switch(proc) {
|
|
|
|
default:
|
|
|
|
fatal("ginscall: bad proc %d", proc);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0: // normal call
|
2009-01-30 15:39:42 -07:00
|
|
|
p = gins(ACALL, N, f);
|
|
|
|
afunclit(&p->to);
|
2009-01-27 13:03:53 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: // call in new proc (go)
|
|
|
|
case 2: // defered call (defer)
|
2008-07-08 18:19:17 -06:00
|
|
|
nodreg(®, types[TINT64], D_AX);
|
2009-01-30 15:39:42 -07:00
|
|
|
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);
|
2009-01-27 13:03:53 -07:00
|
|
|
if(proc == 1)
|
2009-01-30 15:39:42 -07:00
|
|
|
ginscall(newproc, 0);
|
2009-06-03 00:21:58 -06:00
|
|
|
else {
|
|
|
|
if(!hasdefer)
|
|
|
|
fatal("hasdefer=0 but has defer");
|
2009-01-30 15:39:42 -07:00
|
|
|
ginscall(deferproc, 0);
|
2009-06-03 00:21:58 -06:00
|
|
|
}
|
2008-07-11 20:16:39 -06:00
|
|
|
gins(APOPQ, N, ®);
|
|
|
|
gins(APOPQ, N, ®);
|
2009-01-27 13:03:53 -07:00
|
|
|
break;
|
2008-07-07 18:59:32 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-07-17 02:00:44 -06:00
|
|
|
genlist(n->list); // assign the 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
|
|
|
|
|
2009-06-03 00:21:58 -06:00
|
|
|
nodo.xoffset = n->left->xoffset + 3*widthptr + 8;
|
2008-06-04 15:37:38 -06:00
|
|
|
cgen(&nodo, &nodr); // REG = 32+offset(REG) -- i.m->fun[f]
|
|
|
|
|
2008-07-14 21:40:48 -06:00
|
|
|
// BOTCH nodr.type = fntype;
|
2009-02-23 23:43:04 -07:00
|
|
|
nodr.type = n->left->type;
|
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 function call;
|
2009-01-27 13:03:53 -07:00
|
|
|
* proc=0 normal call
|
|
|
|
* proc=1 goroutine run in new proc
|
|
|
|
* proc=2 defer call save away stack
|
2009-01-05 18:32:23 -07:00
|
|
|
*/
|
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]);
|
2009-01-30 15:39:42 -07:00
|
|
|
cgen(n->left, &afun);
|
2008-06-04 15:37:38 -06:00
|
|
|
}
|
|
|
|
|
2009-07-17 02:00:44 -06:00
|
|
|
genlist(n->list); // assign the args
|
2008-06-04 15:37:38 -06:00
|
|
|
t = n->left->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
|
2009-01-30 15:39:42 -07:00
|
|
|
if(n->left->op != ONAME || n->left->class != PFUNC) {
|
2008-06-04 15:37:38 -06:00
|
|
|
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
|
|
|
|
2009-01-30 15:39:42 -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
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2009-07-17 02:00:44 -06:00
|
|
|
genlist(n->list); // copy out args
|
2009-01-27 13:03:53 -07:00
|
|
|
if(hasdefer)
|
2009-01-30 15:39:42 -07:00
|
|
|
ginscall(deferreturn, 0);
|
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;
|
2009-05-03 16:17:03 -06:00
|
|
|
int a;
|
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) {
|
2009-05-03 16:17:03 -06:00
|
|
|
a = optoas(OINC, nl->type);
|
2008-12-14 19:45:00 -07:00
|
|
|
if(nl->addable) {
|
2009-05-03 16:17:03 -06:00
|
|
|
gins(a, N, nl);
|
2008-12-14 19:45:00 -07:00
|
|
|
goto ret;
|
|
|
|
}
|
2009-05-03 16:17:03 -06:00
|
|
|
if(sudoaddable(a, nl, &addr)) {
|
|
|
|
p1 = gins(a, N, N);
|
2008-12-14 19:45:00 -07:00
|
|
|
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) {
|
2009-05-03 16:17:03 -06:00
|
|
|
a = optoas(ODEC, nl->type);
|
2008-12-14 19:45:00 -07:00
|
|
|
if(nl->addable) {
|
2009-05-03 16:17:03 -06:00
|
|
|
gins(a, N, nl);
|
2008-12-14 19:45:00 -07:00
|
|
|
goto ret;
|
|
|
|
}
|
2009-05-03 16:17:03 -06:00
|
|
|
if(sudoaddable(a, nl, &addr)) {
|
|
|
|
p1 = gins(a, N, N);
|
2008-12-14 19:45:00 -07:00
|
|
|
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:
|
2009-05-03 16:17:03 -06:00
|
|
|
a = optoas(n->etype, nl->type);
|
2008-12-14 19:45:00 -07:00
|
|
|
if(nl->addable) {
|
|
|
|
if(smallintconst(nr)) {
|
2009-05-03 16:17:03 -06:00
|
|
|
gins(a, nr, nl);
|
2008-12-14 19:45:00 -07:00
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
regalloc(&n2, nr->type, N);
|
|
|
|
cgen(nr, &n2);
|
2009-05-03 16:17:03 -06:00
|
|
|
gins(a, &n2, nl);
|
2008-12-14 19:45:00 -07:00
|
|
|
regfree(&n2);
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
if(nr->ullman < UINF)
|
2009-05-03 16:17:03 -06:00
|
|
|
if(sudoaddable(a, nl, &addr)) {
|
2008-12-14 19:45:00 -07:00
|
|
|
if(smallintconst(nr)) {
|
2009-05-03 16:17:03 -06:00
|
|
|
p1 = gins(a, nr, N);
|
2008-12-14 19:45:00 -07:00
|
|
|
p1->to = addr;
|
|
|
|
sudoclean();
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
regalloc(&n2, nr->type, N);
|
|
|
|
cgen(nr, &n2);
|
2009-05-03 16:17:03 -06:00
|
|
|
p1 = gins(a, &n2, N);
|
2008-12-14 19:45:00 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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);
|
2009-05-28 00:55:14 -06:00
|
|
|
ax->type = t;
|
|
|
|
dx->type = t;
|
2008-06-06 21:43:29 -06:00
|
|
|
|
2009-05-28 00:55:14 -06:00
|
|
|
regalloc(&n3, t, 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-08-07 21:21:04 -06:00
|
|
|
static void
|
|
|
|
savex(int dr, Node *x, Node *oldx, Node *res, Type *t)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = reg[dr];
|
|
|
|
nodreg(x, types[TINT64], dr);
|
|
|
|
|
|
|
|
// save current ax and dx if they are live
|
|
|
|
// and not the destination
|
|
|
|
memset(oldx, 0, sizeof *oldx);
|
|
|
|
if(r > 0 && !samereg(x, res)) {
|
2009-08-12 14:18:27 -06:00
|
|
|
regalloc(oldx, types[TINT64], N);
|
2009-08-07 21:21:04 -06:00
|
|
|
gmove(x, oldx);
|
|
|
|
}
|
2009-08-12 14:18:27 -06:00
|
|
|
|
|
|
|
regalloc(x, t, x);
|
2009-08-07 21:21:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
restx(Node *x, Node *oldx)
|
|
|
|
{
|
|
|
|
regfree(x);
|
|
|
|
|
|
|
|
if(oldx->op != 0) {
|
2009-08-12 14:18:27 -06:00
|
|
|
x->type = types[TINT64];
|
2009-08-07 21:21:04 -06:00
|
|
|
gmove(oldx, x);
|
|
|
|
regfree(oldx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2009-08-09 16:16:06 -06:00
|
|
|
Node ax, dx, oldax, olddx;
|
2009-08-07 21:21:04 -06:00
|
|
|
Node n1, n2, n3, savl, savr;
|
2009-08-10 13:46:23 -06:00
|
|
|
int n, w, s, a;
|
2009-08-07 21:21:04 -06:00
|
|
|
Magic m;
|
2008-07-17 16:03:39 -06:00
|
|
|
|
2009-08-04 20:16:57 -06:00
|
|
|
if(nl->ullman >= UINF) {
|
2009-08-07 21:21:04 -06:00
|
|
|
tempname(&savl, nl->type);
|
|
|
|
cgen(nl, &savl);
|
|
|
|
nl = &savl;
|
2009-08-04 20:16:57 -06:00
|
|
|
}
|
|
|
|
if(nr->ullman >= UINF) {
|
2009-08-07 21:21:04 -06:00
|
|
|
tempname(&savr, nr->type);
|
|
|
|
cgen(nr, &savr);
|
|
|
|
nr = &savr;
|
2009-08-04 20:16:57 -06:00
|
|
|
}
|
2009-08-04 18:59:10 -06:00
|
|
|
|
2009-08-06 23:33:12 -06:00
|
|
|
if(nr->op != OLITERAL)
|
|
|
|
goto longdiv;
|
|
|
|
|
|
|
|
// special cases of mod/div
|
|
|
|
// by a constant
|
|
|
|
w = nl->type->width*8;
|
2009-08-07 21:21:04 -06:00
|
|
|
s = 0;
|
|
|
|
n = powtwo(nr);
|
|
|
|
if(n >= 1000) {
|
|
|
|
// negative power of 2
|
|
|
|
s = 1;
|
|
|
|
n -= 1000;
|
|
|
|
}
|
2009-08-06 23:33:12 -06:00
|
|
|
|
|
|
|
if(n+1 >= w) {
|
|
|
|
// just sign bit
|
|
|
|
goto longdiv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(n < 0)
|
|
|
|
goto divbymul;
|
|
|
|
switch(n) {
|
|
|
|
case 0:
|
|
|
|
// divide by 1
|
|
|
|
regalloc(&n1, nl->type, res);
|
|
|
|
cgen(nl, &n1);
|
2009-08-08 17:53:21 -06:00
|
|
|
if(op == OMOD) {
|
|
|
|
gins(optoas(OXOR, nl->type), &n1, &n1);
|
|
|
|
} else
|
2009-08-07 21:21:04 -06:00
|
|
|
if(s)
|
|
|
|
gins(optoas(OMINUS, nl->type), N, &n1);
|
2009-08-06 23:33:12 -06:00
|
|
|
gmove(&n1, res);
|
|
|
|
regfree(&n1);
|
|
|
|
return;
|
2009-08-07 21:21:04 -06:00
|
|
|
case 1:
|
|
|
|
// divide by 2
|
2009-08-08 17:53:21 -06:00
|
|
|
if(op == OMOD) {
|
2009-08-10 13:46:23 -06:00
|
|
|
if(issigned[nl->type->etype])
|
|
|
|
goto longmod;
|
2009-08-08 17:53:21 -06:00
|
|
|
regalloc(&n1, nl->type, res);
|
|
|
|
cgen(nl, &n1);
|
|
|
|
nodconst(&n2, nl->type, 1);
|
|
|
|
gins(optoas(OAND, nl->type), &n2, &n1);
|
|
|
|
gmove(&n1, res);
|
|
|
|
regfree(&n1);
|
|
|
|
return;
|
|
|
|
}
|
2009-08-07 21:21:04 -06:00
|
|
|
regalloc(&n1, nl->type, res);
|
|
|
|
cgen(nl, &n1);
|
|
|
|
if(!issigned[nl->type->etype])
|
|
|
|
break;
|
|
|
|
|
|
|
|
// develop -1 iff nl is negative
|
|
|
|
regalloc(&n2, nl->type, N);
|
|
|
|
gmove(&n1, &n2);
|
|
|
|
nodconst(&n3, nl->type, w-1);
|
|
|
|
gins(optoas(ORSH, nl->type), &n3, &n2);
|
|
|
|
gins(optoas(OSUB, nl->type), &n2, &n1);
|
|
|
|
regfree(&n2);
|
|
|
|
break;
|
2009-08-06 23:33:12 -06:00
|
|
|
default:
|
2009-08-08 17:53:21 -06:00
|
|
|
if(op == OMOD) {
|
2009-08-10 13:46:23 -06:00
|
|
|
if(issigned[nl->type->etype])
|
|
|
|
goto longmod;
|
2009-08-08 17:53:21 -06:00
|
|
|
regalloc(&n1, nl->type, res);
|
|
|
|
cgen(nl, &n1);
|
|
|
|
nodconst(&n2, nl->type, mpgetfix(nr->val.u.xval)-1);
|
|
|
|
if(!smallintconst(&n2)) {
|
|
|
|
regalloc(&n3, nl->type, N);
|
|
|
|
gmove(&n2, &n3);
|
|
|
|
gins(optoas(OAND, nl->type), &n3, &n1);
|
|
|
|
regfree(&n3);
|
|
|
|
} else
|
|
|
|
gins(optoas(OAND, nl->type), &n2, &n1);
|
|
|
|
gmove(&n1, res);
|
|
|
|
regfree(&n1);
|
|
|
|
return;
|
|
|
|
}
|
2009-08-06 23:33:12 -06:00
|
|
|
regalloc(&n1, nl->type, res);
|
|
|
|
cgen(nl, &n1);
|
2009-08-07 21:21:04 -06:00
|
|
|
if(!issigned[nl->type->etype])
|
|
|
|
break;
|
|
|
|
|
|
|
|
// develop (2^k)-1 iff nl is negative
|
|
|
|
regalloc(&n2, nl->type, N);
|
|
|
|
gmove(&n1, &n2);
|
|
|
|
nodconst(&n3, nl->type, w-1);
|
|
|
|
gins(optoas(ORSH, nl->type), &n3, &n2);
|
|
|
|
nodconst(&n3, nl->type, w-n);
|
|
|
|
gins(optoas(ORSH, tounsigned(nl->type)), &n3, &n2);
|
|
|
|
gins(optoas(OADD, nl->type), &n2, &n1);
|
|
|
|
regfree(&n2);
|
|
|
|
break;
|
2009-08-06 23:33:12 -06:00
|
|
|
}
|
2009-08-07 21:21:04 -06:00
|
|
|
nodconst(&n2, nl->type, n);
|
|
|
|
gins(optoas(ORSH, nl->type), &n2, &n1);
|
|
|
|
if(s)
|
|
|
|
gins(optoas(OMINUS, nl->type), N, &n1);
|
|
|
|
gmove(&n1, res);
|
|
|
|
regfree(&n1);
|
2009-08-06 23:33:12 -06:00
|
|
|
return;
|
|
|
|
|
|
|
|
divbymul:
|
2009-08-08 17:53:21 -06:00
|
|
|
// try to do division by multiply by (2^w)/d
|
|
|
|
// see hacker's delight chapter 10
|
2009-08-06 23:33:12 -06:00
|
|
|
switch(simtype[nl->type->etype]) {
|
|
|
|
default:
|
|
|
|
goto longdiv;
|
|
|
|
|
2009-08-10 13:46:23 -06:00
|
|
|
case TUINT8:
|
2009-08-07 21:21:04 -06:00
|
|
|
case TUINT16:
|
2009-08-06 23:33:12 -06:00
|
|
|
case TUINT32:
|
|
|
|
case TUINT64:
|
2009-08-07 21:21:04 -06:00
|
|
|
m.w = w;
|
|
|
|
m.ud = mpgetfix(nr->val.u.xval);
|
|
|
|
umagic(&m);
|
|
|
|
if(m.bad)
|
|
|
|
break;
|
2009-08-08 17:53:21 -06:00
|
|
|
if(op == OMOD)
|
|
|
|
goto longmod;
|
2009-08-08 16:26:09 -06:00
|
|
|
|
|
|
|
savex(D_AX, &ax, &oldax, res, nl->type);
|
|
|
|
savex(D_DX, &dx, &olddx, res, nl->type);
|
|
|
|
|
|
|
|
regalloc(&n1, nl->type, N);
|
|
|
|
cgen(nl, &n1); // num -> reg(n1)
|
|
|
|
|
|
|
|
nodconst(&n2, nl->type, m.um);
|
|
|
|
gmove(&n2, &ax); // const->ax
|
|
|
|
|
|
|
|
gins(optoas(OHMUL, nl->type), &n1, N); // imul reg
|
2009-08-10 13:46:23 -06:00
|
|
|
if(w == 8) {
|
|
|
|
// fix up 8-bit multiply
|
|
|
|
Node ah, dl;
|
|
|
|
nodreg(&ah, types[TUINT8], D_AH);
|
|
|
|
nodreg(&dl, types[TUINT8], D_DL);
|
|
|
|
gins(AMOVB, &ah, &dl);
|
|
|
|
}
|
2009-08-08 16:26:09 -06:00
|
|
|
|
2009-08-09 16:16:06 -06:00
|
|
|
if(m.ua) {
|
|
|
|
// need to add numerator accounting for overflow
|
|
|
|
gins(optoas(OADD, nl->type), &n1, &dx);
|
|
|
|
nodconst(&n2, nl->type, 1);
|
|
|
|
gins(optoas(ORRC, nl->type), &n2, &dx);
|
|
|
|
nodconst(&n2, nl->type, m.s-1);
|
|
|
|
gins(optoas(ORSH, nl->type), &n2, &dx);
|
|
|
|
} else {
|
|
|
|
nodconst(&n2, nl->type, m.s);
|
|
|
|
gins(optoas(ORSH, nl->type), &n2, &dx); // shift dx
|
|
|
|
}
|
|
|
|
|
2009-08-08 16:26:09 -06:00
|
|
|
|
|
|
|
regfree(&n1);
|
|
|
|
gmove(&dx, res);
|
|
|
|
|
|
|
|
restx(&ax, &oldax);
|
|
|
|
restx(&dx, &olddx);
|
|
|
|
return;
|
2009-08-06 23:33:12 -06:00
|
|
|
|
2009-08-10 13:46:23 -06:00
|
|
|
case TINT8:
|
2009-08-07 21:21:04 -06:00
|
|
|
case TINT16:
|
|
|
|
case TINT32:
|
|
|
|
case TINT64:
|
|
|
|
m.w = w;
|
|
|
|
m.sd = mpgetfix(nr->val.u.xval);
|
|
|
|
smagic(&m);
|
|
|
|
if(m.bad)
|
|
|
|
break;
|
2009-08-08 17:53:21 -06:00
|
|
|
if(op == OMOD)
|
|
|
|
goto longmod;
|
2009-08-04 20:16:57 -06:00
|
|
|
|
2009-08-07 21:21:04 -06:00
|
|
|
savex(D_AX, &ax, &oldax, res, nl->type);
|
|
|
|
savex(D_DX, &dx, &olddx, res, nl->type);
|
2008-07-17 16:03:39 -06:00
|
|
|
|
2009-08-07 21:21:04 -06:00
|
|
|
regalloc(&n1, nl->type, N);
|
|
|
|
cgen(nl, &n1); // num -> reg(n1)
|
2009-08-04 18:59:10 -06:00
|
|
|
|
2009-08-07 21:21:04 -06:00
|
|
|
nodconst(&n2, nl->type, m.sm);
|
|
|
|
gmove(&n2, &ax); // const->ax
|
2008-07-17 17:22:40 -06:00
|
|
|
|
2009-08-08 16:26:09 -06:00
|
|
|
gins(optoas(OHMUL, nl->type), &n1, N); // imul reg
|
2009-08-10 13:46:23 -06:00
|
|
|
if(w == 8) {
|
|
|
|
// fix up 8-bit multiply
|
|
|
|
Node ah, dl;
|
|
|
|
nodreg(&ah, types[TUINT8], D_AH);
|
|
|
|
nodreg(&dl, types[TUINT8], D_DL);
|
|
|
|
gins(AMOVB, &ah, &dl);
|
|
|
|
}
|
2009-08-04 23:59:23 -06:00
|
|
|
|
2009-08-09 16:16:06 -06:00
|
|
|
if(m.sm < 0) {
|
|
|
|
// need to add numerator
|
|
|
|
gins(optoas(OADD, nl->type), &n1, &dx);
|
|
|
|
}
|
|
|
|
|
2009-08-07 21:21:04 -06:00
|
|
|
nodconst(&n2, nl->type, m.s);
|
|
|
|
gins(optoas(ORSH, nl->type), &n2, &dx); // shift dx
|
|
|
|
|
|
|
|
nodconst(&n2, nl->type, w-1);
|
|
|
|
gins(optoas(ORSH, nl->type), &n2, &n1); // -1 iff num is neg
|
|
|
|
gins(optoas(OSUB, nl->type), &n1, &dx); // added
|
|
|
|
|
2009-08-09 16:16:06 -06:00
|
|
|
if(m.sd < 0) {
|
|
|
|
// this could probably be removed
|
|
|
|
// by factoring it into the multiplier
|
2009-08-07 21:21:04 -06:00
|
|
|
gins(optoas(OMINUS, nl->type), N, &dx);
|
2009-08-09 16:16:06 -06:00
|
|
|
}
|
2009-08-07 21:21:04 -06:00
|
|
|
|
|
|
|
regfree(&n1);
|
|
|
|
gmove(&dx, res);
|
|
|
|
|
|
|
|
restx(&ax, &oldax);
|
|
|
|
restx(&dx, &olddx);
|
|
|
|
return;
|
2009-08-04 18:59:10 -06:00
|
|
|
}
|
2009-08-07 21:21:04 -06:00
|
|
|
goto longdiv;
|
2009-08-04 23:59:23 -06:00
|
|
|
|
2009-08-07 21:21:04 -06:00
|
|
|
longdiv:
|
2009-08-08 17:53:21 -06:00
|
|
|
// division and mod using (slow) hardware instruction
|
2009-08-07 21:21:04 -06:00
|
|
|
savex(D_AX, &ax, &oldax, res, nl->type);
|
|
|
|
savex(D_DX, &dx, &olddx, res, nl->type);
|
|
|
|
dodiv(op, nl, nr, res, &ax, &dx);
|
|
|
|
restx(&ax, &oldax);
|
|
|
|
restx(&dx, &olddx);
|
2009-08-08 17:53:21 -06:00
|
|
|
return;
|
|
|
|
|
|
|
|
longmod:
|
|
|
|
// mod using formula A%B = A-(A/B*B) but
|
|
|
|
// we know that there is a fast algorithm for A/B
|
|
|
|
regalloc(&n1, nl->type, res);
|
|
|
|
cgen(nl, &n1);
|
|
|
|
regalloc(&n2, nl->type, N);
|
|
|
|
cgen_div(ODIV, &n1, nr, &n2);
|
2009-08-10 13:46:23 -06:00
|
|
|
a = optoas(OMUL, nl->type);
|
|
|
|
if(w == 8) {
|
|
|
|
// use 2-operand 16-bit multiply
|
|
|
|
// because there is no 2-operand 8-bit multiply
|
|
|
|
a = AIMULW;
|
|
|
|
}
|
2009-08-08 17:53:21 -06:00
|
|
|
if(!smallintconst(nr)) {
|
|
|
|
regalloc(&n3, nl->type, N);
|
|
|
|
cgen(nr, &n3);
|
2009-08-10 13:46:23 -06:00
|
|
|
gins(a, &n3, &n2);
|
2009-08-08 17:53:21 -06:00
|
|
|
regfree(&n3);
|
|
|
|
} else
|
2009-08-10 13:46:23 -06:00
|
|
|
gins(a, nr, &n2);
|
2009-08-08 17:53:21 -06:00
|
|
|
gins(optoas(OSUB, nl->type), &n2, &n1);
|
|
|
|
gmove(&n1, res);
|
|
|
|
regfree(&n1);
|
|
|
|
regfree(&n2);
|
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)
|
|
|
|
{
|
2009-08-04 20:16:57 -06:00
|
|
|
Node n1, n2, n3, n4, n5, cx, oldcx;
|
|
|
|
int a, rcx;
|
2008-08-28 20:59:42 -06:00
|
|
|
Prog *p1;
|
2009-05-29 15:42:24 -06:00
|
|
|
uvlong sc;
|
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);
|
2009-05-29 15:42:24 -06:00
|
|
|
sc = mpgetfix(nr->val.u.xval);
|
|
|
|
if(sc >= nl->type->width*8) {
|
2008-11-18 18:15:42 -07:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2009-08-04 20:16:57 -06:00
|
|
|
if(nl->ullman >= UINF) {
|
|
|
|
tempname(&n4, nl->type);
|
|
|
|
cgen(nl, &n4);
|
|
|
|
nl = &n4;
|
|
|
|
}
|
|
|
|
if(nr->ullman >= UINF) {
|
|
|
|
tempname(&n5, nr->type);
|
|
|
|
cgen(nr, &n5);
|
|
|
|
nr = &n5;
|
|
|
|
}
|
|
|
|
|
|
|
|
rcx = reg[D_CX];
|
2009-04-21 20:38:58 -06:00
|
|
|
nodreg(&n1, types[TUINT32], D_CX);
|
|
|
|
regalloc(&n1, nr->type, &n1); // to hold the shift type in CX
|
|
|
|
regalloc(&n3, types[TUINT64], &n1); // to clear high bits of CX
|
2008-06-07 16:21:02 -06:00
|
|
|
|
2009-08-04 20:16:57 -06:00
|
|
|
nodreg(&cx, types[TUINT64], D_CX);
|
|
|
|
memset(&oldcx, 0, sizeof oldcx);
|
|
|
|
if(rcx > 0 && !samereg(&cx, res)) {
|
|
|
|
regalloc(&oldcx, types[TUINT64], N);
|
|
|
|
gmove(&cx, &oldcx);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(samereg(&cx, res))
|
|
|
|
regalloc(&n2, nl->type, N);
|
|
|
|
else
|
|
|
|
regalloc(&n2, nl->type, res);
|
2008-06-07 16:21:02 -06:00
|
|
|
if(nl->ullman >= nr->ullman) {
|
|
|
|
cgen(nl, &n2);
|
|
|
|
cgen(nr, &n1);
|
2009-04-21 20:38:58 -06:00
|
|
|
gmove(&n1, &n3);
|
2008-06-07 16:21:02 -06:00
|
|
|
} else {
|
|
|
|
cgen(nr, &n1);
|
2009-04-21 20:38:58 -06:00
|
|
|
gmove(&n1, &n3);
|
2008-06-07 16:21:02 -06:00
|
|
|
cgen(nl, &n2);
|
|
|
|
}
|
2009-04-21 20:38:58 -06:00
|
|
|
regfree(&n3);
|
|
|
|
|
2008-08-28 20:59:42 -06:00
|
|
|
// test and fix up large shifts
|
2009-04-21 20:38:58 -06:00
|
|
|
nodconst(&n3, types[TUINT64], nl->type->width*8);
|
|
|
|
gins(optoas(OCMP, types[TUINT64]), &n1, &n3);
|
|
|
|
p1 = gbranch(optoas(OLT, types[TUINT64]), T);
|
2008-08-28 20:59:42 -06:00
|
|
|
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
|
|
|
|
2009-08-04 20:16:57 -06:00
|
|
|
if(oldcx.op != 0) {
|
|
|
|
gmove(&oldcx, &cx);
|
|
|
|
regfree(&oldcx);
|
|
|
|
}
|
|
|
|
|
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
|
2009-08-10 13:46:23 -06:00
|
|
|
* no 2-operand byte multiply instruction so have to do
|
2009-01-05 18:32:23 -07:00
|
|
|
* 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
|
2009-03-30 20:15:07 -06:00
|
|
|
clearfat(Node *nl)
|
2008-08-29 21:30:19 -06:00
|
|
|
{
|
2009-03-30 20:15:07 -06:00
|
|
|
uint32 w, c, q;
|
|
|
|
Node n1;
|
2008-08-29 21:30:19 -06:00
|
|
|
|
2009-03-30 20:15:07 -06:00
|
|
|
/* clear a fat object */
|
|
|
|
if(debug['g'])
|
|
|
|
dump("\nclearfat", nl);
|
2008-08-29 21:30:19 -06:00
|
|
|
|
2009-03-30 20:15:07 -06:00
|
|
|
w = nl->type->width;
|
|
|
|
c = w % 8; // bytes
|
|
|
|
q = w / 8; // quads
|
|
|
|
|
|
|
|
gconreg(AMOVQ, 0, D_AX);
|
|
|
|
nodreg(&n1, types[tptr], D_DI);
|
|
|
|
agen(nl, &n1);
|
|
|
|
|
|
|
|
if(q >= 4) {
|
|
|
|
gconreg(AMOVQ, q, D_CX);
|
|
|
|
gins(AREP, N, N); // repeat
|
|
|
|
gins(ASTOSQ, N, N); // STOQ AL,*(DI)+
|
|
|
|
} else
|
|
|
|
while(q > 0) {
|
|
|
|
gins(ASTOSQ, N, N); // STOQ AL,*(DI)+
|
|
|
|
q--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(c >= 4) {
|
|
|
|
gconreg(AMOVQ, 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--;
|
2008-08-29 21:30:19 -06:00
|
|
|
}
|
|
|
|
}
|
2009-05-16 14:29:08 -06:00
|
|
|
|
2009-05-17 20:16:16 -06:00
|
|
|
int
|
|
|
|
getlit(Node *lit)
|
|
|
|
{
|
|
|
|
if(smallintconst(lit))
|
|
|
|
return mpgetfix(lit->val.u.xval);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
stataddr(Node *nam, Node *n)
|
|
|
|
{
|
|
|
|
int l;
|
|
|
|
|
|
|
|
if(n == N)
|
|
|
|
goto no;
|
|
|
|
|
|
|
|
switch(n->op) {
|
2009-09-05 21:32:24 -06:00
|
|
|
|
2009-05-17 20:16:16 -06:00
|
|
|
case ONAME:
|
|
|
|
*nam = *n;
|
2009-05-23 16:34:29 -06:00
|
|
|
return n->addable;
|
2009-05-17 20:16:16 -06:00
|
|
|
|
|
|
|
case ODOT:
|
|
|
|
if(!stataddr(nam, n->left))
|
|
|
|
break;
|
|
|
|
nam->xoffset += n->xoffset;
|
|
|
|
nam->type = n->type;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case OINDEX:
|
|
|
|
if(n->left->type->bound < 0)
|
|
|
|
break;
|
|
|
|
if(!stataddr(nam, n->left))
|
|
|
|
break;
|
|
|
|
l = getlit(n->right);
|
|
|
|
if(l < 0)
|
|
|
|
break;
|
|
|
|
nam->xoffset += l*n->type->width;
|
|
|
|
nam->type = n->type;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
no:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-16 14:29:08 -06:00
|
|
|
int
|
2009-09-05 21:32:24 -06:00
|
|
|
gen_as_init(Node *n)
|
2009-05-16 14:29:08 -06:00
|
|
|
{
|
2009-09-05 21:32:24 -06:00
|
|
|
Node *nr, *nl;
|
2009-05-18 23:11:22 -06:00
|
|
|
Node nam, nod1;
|
2009-05-17 20:16:16 -06:00
|
|
|
Prog *p;
|
|
|
|
|
2009-09-05 21:32:24 -06:00
|
|
|
if(n->dodata == 0)
|
2009-05-17 20:16:16 -06:00
|
|
|
goto no;
|
|
|
|
|
2009-09-05 21:32:24 -06:00
|
|
|
nr = n->right;
|
|
|
|
nl = n->left;
|
2009-05-17 20:16:16 -06:00
|
|
|
if(nr == N) {
|
|
|
|
if(!stataddr(&nam, nl))
|
|
|
|
goto no;
|
|
|
|
if(nam.class != PEXTERN)
|
|
|
|
goto no;
|
2009-05-18 23:11:22 -06:00
|
|
|
goto yes;
|
|
|
|
}
|
|
|
|
|
2009-09-05 21:32:24 -06:00
|
|
|
if(nr->type == T || !eqtype(nl->type, nr->type))
|
2009-05-17 20:16:16 -06:00
|
|
|
goto no;
|
|
|
|
|
|
|
|
if(!stataddr(&nam, nl))
|
|
|
|
goto no;
|
2009-09-05 21:32:24 -06:00
|
|
|
|
2009-05-17 20:16:16 -06:00
|
|
|
if(nam.class != PEXTERN)
|
|
|
|
goto no;
|
|
|
|
|
|
|
|
switch(nr->op) {
|
|
|
|
default:
|
|
|
|
goto no;
|
|
|
|
|
2009-09-09 18:48:55 -06:00
|
|
|
case OCONVSLICE:
|
|
|
|
goto slice;
|
|
|
|
|
2009-05-17 20:16:16 -06:00
|
|
|
case OLITERAL:
|
2009-09-05 21:32:24 -06:00
|
|
|
break;
|
2009-05-17 20:16:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
switch(nr->type->etype) {
|
|
|
|
default:
|
|
|
|
goto no;
|
|
|
|
|
|
|
|
case TBOOL:
|
|
|
|
case TINT8:
|
|
|
|
case TUINT8:
|
|
|
|
case TINT16:
|
|
|
|
case TUINT16:
|
|
|
|
case TINT32:
|
|
|
|
case TUINT32:
|
|
|
|
case TINT64:
|
|
|
|
case TUINT64:
|
|
|
|
case TINT:
|
|
|
|
case TUINT:
|
2009-09-05 21:32:24 -06:00
|
|
|
case TUINTPTR:
|
|
|
|
case TPTR32:
|
|
|
|
case TPTR64:
|
2009-05-17 20:16:16 -06:00
|
|
|
case TFLOAT32:
|
|
|
|
case TFLOAT64:
|
|
|
|
case TFLOAT:
|
2009-09-05 21:32:24 -06:00
|
|
|
p = gins(ANOP, N, N); // in case the data is the dest of a goto
|
2009-05-17 20:16:16 -06:00
|
|
|
p = gins(ADATA, &nam, nr);
|
|
|
|
p->from.scale = nr->type->width;
|
|
|
|
break;
|
2009-05-19 14:37:36 -06:00
|
|
|
|
|
|
|
case TSTRING:
|
2009-09-05 21:32:24 -06:00
|
|
|
gins(ANOP, N, N); // in case the data is the dest of a goto
|
2009-05-19 14:37:36 -06:00
|
|
|
p = gins(ADATA, &nam, N);
|
|
|
|
datastring(nr->val.u.sval->s, nr->val.u.sval->len, &p->to);
|
|
|
|
p->from.scale = types[tptr]->width;
|
|
|
|
p->to.index = p->to.type;
|
|
|
|
p->to.type = D_ADDR;
|
|
|
|
//print("%P\n", p);
|
|
|
|
|
|
|
|
nodconst(&nod1, types[TINT32], nr->val.u.sval->len);
|
|
|
|
p = gins(ADATA, &nam, &nod1);
|
|
|
|
p->from.scale = types[TINT32]->width;
|
|
|
|
p->from.offset += types[tptr]->width;
|
|
|
|
//print("%P\n", p);
|
|
|
|
break;
|
2009-05-17 20:16:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
yes:
|
|
|
|
return 1;
|
2009-09-05 21:32:24 -06:00
|
|
|
|
2009-09-09 18:48:55 -06:00
|
|
|
slice:
|
|
|
|
p = gins(ANOP, N, N); // in case the data is the dest of a goto
|
|
|
|
nr = n->right->left;
|
|
|
|
if(nr == N || nr->op != OADDR)
|
|
|
|
goto no;
|
|
|
|
nr = nr->left;
|
|
|
|
if(nr == N || nr->op != ONAME)
|
|
|
|
goto no;
|
|
|
|
|
|
|
|
// nr is the array being converted to a slice
|
|
|
|
if(nr->type == T || nr->type->etype != TARRAY || nr->type->bound < 0)
|
|
|
|
goto no;
|
|
|
|
|
|
|
|
nam.xoffset += Array_array;
|
|
|
|
p = gins(ADATA, &nam, n->right->left);
|
|
|
|
p->from.scale = types[tptr]->width;
|
|
|
|
|
|
|
|
nam.xoffset += Array_nel-Array_array;
|
|
|
|
nodconst(&nod1, types[TINT32], nr->type->bound);
|
|
|
|
p = gins(ADATA, &nam, &nod1);
|
|
|
|
p->from.scale = types[TINT32]->width;
|
|
|
|
|
|
|
|
nam.xoffset += Array_cap-Array_nel;
|
|
|
|
p = gins(ADATA, &nam, &nod1);
|
|
|
|
p->from.scale = types[TINT32]->width;
|
|
|
|
|
|
|
|
goto yes;
|
|
|
|
|
2009-09-05 21:32:24 -06:00
|
|
|
no:
|
|
|
|
if(n->dodata == 2) {
|
|
|
|
dump("\ngen_as_init", n);
|
|
|
|
fatal("gen_as_init couldnt make data statement");
|
|
|
|
}
|
|
|
|
return 0;
|
2009-05-16 14:29:08 -06:00
|
|
|
}
|
2009-08-27 14:19:44 -06:00
|
|
|
|
2009-08-27 15:59:26 -06:00
|
|
|
static int
|
2009-08-29 21:33:21 -06:00
|
|
|
regcmp(const void *va, const void *vb)
|
2009-08-27 15:59:26 -06:00
|
|
|
{
|
2009-08-29 21:33:21 -06:00
|
|
|
Node *ra, *rb;
|
|
|
|
|
|
|
|
ra = (Node*)va;
|
|
|
|
rb = (Node*)vb;
|
2009-08-28 13:37:39 -06:00
|
|
|
return ra->local - rb->local;
|
2009-08-27 15:59:26 -06:00
|
|
|
}
|
|
|
|
|
2009-08-28 16:44:24 -06:00
|
|
|
static Prog* throwpc;
|
|
|
|
|
2009-08-27 14:19:44 -06:00
|
|
|
void
|
|
|
|
getargs(NodeList *nn, Node *reg, int n)
|
|
|
|
{
|
|
|
|
NodeList *l;
|
|
|
|
int i;
|
|
|
|
|
2009-08-28 16:44:24 -06:00
|
|
|
throwpc = nil;
|
|
|
|
|
2009-08-27 14:19:44 -06:00
|
|
|
l = nn;
|
|
|
|
for(i=0; i<n; i++) {
|
2009-08-27 15:59:26 -06:00
|
|
|
if(!smallintconst(l->n->right) && !isslice(l->n->right->type)) {
|
2009-08-27 14:19:44 -06:00
|
|
|
regalloc(reg+i, l->n->right->type, N);
|
|
|
|
cgen(l->n->right, reg+i);
|
|
|
|
} else
|
|
|
|
reg[i] = *l->n->right;
|
2009-08-28 13:37:39 -06:00
|
|
|
if(reg[i].local != 0)
|
|
|
|
yyerror("local used");
|
|
|
|
reg[i].local = l->n->left->xoffset;
|
2009-08-27 14:19:44 -06:00
|
|
|
l = l->next;
|
|
|
|
}
|
2009-08-28 13:37:39 -06:00
|
|
|
qsort((void*)reg, n, sizeof(*reg), regcmp);
|
2009-08-27 15:59:26 -06:00
|
|
|
for(i=0; i<n; i++)
|
2009-08-28 13:37:39 -06:00
|
|
|
reg[i].local = 0;
|
2009-08-27 14:19:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-08-27 15:59:26 -06:00
|
|
|
cmpandthrow(Node *nl, Node *nr)
|
2009-08-27 14:19:44 -06:00
|
|
|
{
|
|
|
|
vlong cl, cr;
|
|
|
|
Prog *p1;
|
2009-08-27 15:59:26 -06:00
|
|
|
int op;
|
|
|
|
Node *c;
|
2009-08-27 14:19:44 -06:00
|
|
|
|
|
|
|
op = OLE;
|
2009-08-27 15:59:26 -06:00
|
|
|
if(smallintconst(nl)) {
|
|
|
|
cl = mpgetfix(nl->val.u.xval);
|
2009-08-27 14:19:44 -06:00
|
|
|
if(cl == 0)
|
|
|
|
return;
|
2009-08-27 15:59:26 -06:00
|
|
|
if(smallintconst(nr)) {
|
|
|
|
cr = mpgetfix(nr->val.u.xval);
|
2009-08-28 16:44:24 -06:00
|
|
|
if(cl > cr) {
|
|
|
|
if(throwpc == nil) {
|
|
|
|
throwpc = pc;
|
|
|
|
ginscall(throwslice, 0);
|
|
|
|
} else
|
|
|
|
patch(gbranch(AJMP, T), throwpc);
|
|
|
|
}
|
2009-08-27 14:19:44 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// put the constant on the right
|
|
|
|
op = brrev(op);
|
2009-08-27 15:59:26 -06:00
|
|
|
c = nl;
|
|
|
|
nl = nr;
|
|
|
|
nr = c;
|
2009-08-27 14:19:44 -06:00
|
|
|
}
|
|
|
|
|
2009-08-27 15:59:26 -06:00
|
|
|
gins(optoas(OCMP, types[TUINT32]), nl, nr);
|
2009-08-28 16:44:24 -06:00
|
|
|
if(throwpc == nil) {
|
|
|
|
p1 = gbranch(optoas(op, types[TUINT32]), T);
|
|
|
|
throwpc = pc;
|
|
|
|
ginscall(throwslice, 0);
|
|
|
|
patch(p1, pc);
|
|
|
|
} else {
|
|
|
|
op = brcom(op);
|
|
|
|
p1 = gbranch(optoas(op, types[TUINT32]), T);
|
|
|
|
patch(p1, throwpc);
|
|
|
|
}
|
2009-08-27 14:19:44 -06:00
|
|
|
}
|
|
|
|
|
2009-08-28 14:32:09 -06:00
|
|
|
int
|
|
|
|
sleasy(Node *n)
|
|
|
|
{
|
|
|
|
if(n->op != ONAME)
|
|
|
|
return 0;
|
|
|
|
if(!n->addable)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-08-27 14:19:44 -06:00
|
|
|
// generate inline code for
|
|
|
|
// slicearray
|
|
|
|
// sliceslice
|
|
|
|
// arraytoslice
|
|
|
|
int
|
|
|
|
cgen_inline(Node *n, Node *res)
|
|
|
|
{
|
2009-08-28 16:44:24 -06:00
|
|
|
Node nodes[5];
|
2009-08-27 14:19:44 -06:00
|
|
|
Node n1, n2;
|
|
|
|
vlong v;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if(n->op != OCALLFUNC)
|
|
|
|
goto no;
|
2009-08-28 13:37:39 -06:00
|
|
|
if(!n->left->addable)
|
2009-08-27 14:19:44 -06:00
|
|
|
goto no;
|
2009-08-28 14:32:09 -06:00
|
|
|
if(!sleasy(res))
|
2009-08-27 14:19:44 -06:00
|
|
|
goto no;
|
|
|
|
if(strcmp(n->left->sym->package, "sys") != 0)
|
|
|
|
goto no;
|
|
|
|
if(strcmp(n->left->sym->name, "slicearray") == 0)
|
|
|
|
goto slicearray;
|
|
|
|
if(strcmp(n->left->sym->name, "sliceslice") == 0)
|
|
|
|
goto sliceslice;
|
|
|
|
if(strcmp(n->left->sym->name, "arraytoslice") == 0)
|
|
|
|
goto arraytoslice;
|
|
|
|
goto no;
|
|
|
|
|
|
|
|
slicearray:
|
|
|
|
getargs(n->list, nodes, 5);
|
|
|
|
|
|
|
|
// if(hb[3] > nel[1]) goto throw
|
2009-08-28 16:44:24 -06:00
|
|
|
cmpandthrow(&nodes[3], &nodes[1]);
|
2009-08-27 14:19:44 -06:00
|
|
|
|
|
|
|
// if(lb[2] > hb[3]) goto throw
|
2009-08-28 16:44:24 -06:00
|
|
|
cmpandthrow(&nodes[2], &nodes[3]);
|
2009-08-27 14:19:44 -06:00
|
|
|
|
|
|
|
// len = hb[3] - lb[2] (destroys hb)
|
|
|
|
n2 = *res;
|
|
|
|
n2.xoffset += Array_nel;
|
|
|
|
|
2009-08-28 16:44:24 -06:00
|
|
|
if(smallintconst(&nodes[3]) && smallintconst(&nodes[2])) {
|
|
|
|
v = mpgetfix(nodes[3].val.u.xval) -
|
|
|
|
mpgetfix(nodes[2].val.u.xval);
|
2009-08-27 14:19:44 -06:00
|
|
|
nodconst(&n1, types[TUINT32], v);
|
|
|
|
gins(optoas(OAS, types[TUINT32]), &n1, &n2);
|
|
|
|
} else {
|
2009-08-28 16:44:24 -06:00
|
|
|
regalloc(&n1, types[TUINT32], &nodes[3]);
|
|
|
|
gmove(&nodes[3], &n1);
|
|
|
|
if(!smallintconst(&nodes[2]) || mpgetfix(nodes[2].val.u.xval) != 0)
|
|
|
|
gins(optoas(OSUB, types[TUINT32]), &nodes[2], &n1);
|
2009-08-27 14:19:44 -06:00
|
|
|
gins(optoas(OAS, types[TUINT32]), &n1, &n2);
|
|
|
|
regfree(&n1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// cap = nel[1] - lb[2] (destroys nel)
|
|
|
|
n2 = *res;
|
|
|
|
n2.xoffset += Array_cap;
|
|
|
|
|
2009-08-28 16:44:24 -06:00
|
|
|
if(smallintconst(&nodes[1]) && smallintconst(&nodes[2])) {
|
|
|
|
v = mpgetfix(nodes[1].val.u.xval) -
|
|
|
|
mpgetfix(nodes[2].val.u.xval);
|
2009-08-27 14:19:44 -06:00
|
|
|
nodconst(&n1, types[TUINT32], v);
|
|
|
|
gins(optoas(OAS, types[TUINT32]), &n1, &n2);
|
|
|
|
} else {
|
2009-08-28 16:44:24 -06:00
|
|
|
regalloc(&n1, types[TUINT32], &nodes[1]);
|
|
|
|
gmove(&nodes[1], &n1);
|
|
|
|
if(!smallintconst(&nodes[2]) || mpgetfix(nodes[2].val.u.xval) != 0)
|
|
|
|
gins(optoas(OSUB, types[TUINT32]), &nodes[2], &n1);
|
2009-08-27 14:19:44 -06:00
|
|
|
gins(optoas(OAS, types[TUINT32]), &n1, &n2);
|
|
|
|
regfree(&n1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ary = old[0] + (lb[2] * width[4]) (destroys old)
|
|
|
|
n2 = *res;
|
|
|
|
n2.xoffset += Array_array;
|
|
|
|
|
2009-08-28 16:44:24 -06:00
|
|
|
if(smallintconst(&nodes[2]) && smallintconst(&nodes[4])) {
|
|
|
|
v = mpgetfix(nodes[2].val.u.xval) *
|
|
|
|
mpgetfix(nodes[4].val.u.xval);
|
2009-08-28 13:37:39 -06:00
|
|
|
if(v != 0) {
|
|
|
|
nodconst(&n1, types[tptr], v);
|
2009-08-28 16:44:24 -06:00
|
|
|
gins(optoas(OADD, types[tptr]), &n1, &nodes[0]);
|
2009-08-28 13:37:39 -06:00
|
|
|
}
|
2009-08-27 14:19:44 -06:00
|
|
|
} else {
|
2009-08-28 16:44:24 -06:00
|
|
|
regalloc(&n1, types[tptr], &nodes[2]);
|
|
|
|
gmove(&nodes[2], &n1);
|
|
|
|
if(!smallintconst(&nodes[4]) || mpgetfix(nodes[4].val.u.xval) != 1)
|
|
|
|
gins(optoas(OMUL, types[tptr]), &nodes[4], &n1);
|
|
|
|
gins(optoas(OADD, types[tptr]), &n1, &nodes[0]);
|
2009-08-27 14:19:44 -06:00
|
|
|
regfree(&n1);
|
|
|
|
}
|
2009-08-28 16:44:24 -06:00
|
|
|
gins(optoas(OAS, types[tptr]), &nodes[0], &n2);
|
2009-08-27 14:19:44 -06:00
|
|
|
|
|
|
|
for(i=0; i<5; i++) {
|
2009-08-28 16:44:24 -06:00
|
|
|
if(nodes[i].op == OREGISTER)
|
|
|
|
regfree(&nodes[i]);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
arraytoslice:
|
|
|
|
getargs(n->list, nodes, 2);
|
|
|
|
|
|
|
|
// ret.len = nel[1];
|
|
|
|
n2 = *res;
|
|
|
|
n2.xoffset += Array_nel;
|
|
|
|
gins(optoas(OAS, types[TUINT32]), &nodes[1], &n2);
|
|
|
|
|
|
|
|
// ret.cap = nel[1];
|
|
|
|
n2 = *res;
|
|
|
|
n2.xoffset += Array_cap;
|
|
|
|
gins(optoas(OAS, types[TUINT32]), &nodes[1], &n2);
|
|
|
|
|
|
|
|
// ret.array = old[0];
|
|
|
|
n2 = *res;
|
|
|
|
n2.xoffset += Array_array;
|
|
|
|
gins(optoas(OAS, types[tptr]), &nodes[0], &n2);
|
|
|
|
|
|
|
|
for(i=0; i<2; i++) {
|
|
|
|
if(nodes[i].op == OREGISTER)
|
|
|
|
regfree(&nodes[i]);
|
2009-08-27 14:19:44 -06:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
sliceslice:
|
2009-08-27 15:59:26 -06:00
|
|
|
getargs(n->list, nodes, 4);
|
2009-08-28 16:44:24 -06:00
|
|
|
if(!sleasy(&nodes[0])) {
|
2009-08-28 13:37:39 -06:00
|
|
|
for(i=0; i<4; i++) {
|
2009-08-28 16:44:24 -06:00
|
|
|
if(nodes[i].op == OREGISTER)
|
|
|
|
regfree(&nodes[i]);
|
2009-08-28 13:37:39 -06:00
|
|
|
}
|
|
|
|
goto no;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if(hb[2] > old.cap[0]) goto throw;
|
2009-08-28 16:44:24 -06:00
|
|
|
n2 = nodes[0];
|
2009-08-28 13:37:39 -06:00
|
|
|
n2.xoffset += Array_cap;
|
2009-08-28 16:44:24 -06:00
|
|
|
cmpandthrow(&nodes[2], &n2);
|
2009-08-27 15:59:26 -06:00
|
|
|
|
2009-08-28 13:37:39 -06:00
|
|
|
// if(lb[1] > hb[2]) goto throw;
|
2009-08-28 16:44:24 -06:00
|
|
|
cmpandthrow(&nodes[1], &nodes[2]);
|
2009-08-28 13:37:39 -06:00
|
|
|
|
|
|
|
// ret.len = hb[2]-lb[1]; (destroys hb[2])
|
|
|
|
n2 = *res;
|
|
|
|
n2.xoffset += Array_nel;
|
|
|
|
|
2009-08-28 16:44:24 -06:00
|
|
|
if(smallintconst(&nodes[2]) && smallintconst(&nodes[1])) {
|
|
|
|
v = mpgetfix(nodes[2].val.u.xval) -
|
|
|
|
mpgetfix(nodes[1].val.u.xval);
|
2009-08-28 13:37:39 -06:00
|
|
|
nodconst(&n1, types[TUINT32], v);
|
|
|
|
gins(optoas(OAS, types[TUINT32]), &n1, &n2);
|
|
|
|
} else {
|
2009-08-28 16:44:24 -06:00
|
|
|
regalloc(&n1, types[TUINT32], &nodes[2]);
|
|
|
|
gmove(&nodes[2], &n1);
|
|
|
|
if(!smallintconst(&nodes[1]) || mpgetfix(nodes[1].val.u.xval) != 0)
|
|
|
|
gins(optoas(OSUB, types[TUINT32]), &nodes[1], &n1);
|
2009-08-28 13:37:39 -06:00
|
|
|
gins(optoas(OAS, types[TUINT32]), &n1, &n2);
|
|
|
|
regfree(&n1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ret.cap = old.cap[0]-lb[1]; (uses hb[2])
|
2009-08-28 16:44:24 -06:00
|
|
|
n2 = nodes[0];
|
2009-08-28 13:37:39 -06:00
|
|
|
n2.xoffset += Array_cap;
|
|
|
|
|
2009-08-28 16:44:24 -06:00
|
|
|
regalloc(&n1, types[TUINT32], &nodes[2]);
|
2009-08-28 13:37:39 -06:00
|
|
|
gins(optoas(OAS, types[TUINT32]), &n2, &n1);
|
2009-08-28 16:44:24 -06:00
|
|
|
if(!smallintconst(&nodes[1]) || mpgetfix(nodes[1].val.u.xval) != 0)
|
|
|
|
gins(optoas(OSUB, types[TUINT32]), &nodes[1], &n1);
|
2009-08-28 13:37:39 -06:00
|
|
|
|
|
|
|
n2 = *res;
|
|
|
|
n2.xoffset += Array_cap;
|
|
|
|
gins(optoas(OAS, types[TUINT32]), &n1, &n2);
|
|
|
|
regfree(&n1);
|
|
|
|
|
2009-08-28 14:45:48 -06:00
|
|
|
// ret.array = old.array[0]+lb[1]*width[3]; (uses lb[1])
|
2009-08-28 16:44:24 -06:00
|
|
|
n2 = nodes[0];
|
2009-08-28 13:37:39 -06:00
|
|
|
n2.xoffset += Array_array;
|
|
|
|
|
2009-08-28 16:44:24 -06:00
|
|
|
regalloc(&n1, types[tptr], &nodes[1]);
|
|
|
|
if(smallintconst(&nodes[1]) && smallintconst(&nodes[3])) {
|
2009-08-28 14:32:09 -06:00
|
|
|
gins(optoas(OAS, types[tptr]), &n2, &n1);
|
2009-08-28 16:44:24 -06:00
|
|
|
v = mpgetfix(nodes[1].val.u.xval) *
|
|
|
|
mpgetfix(nodes[3].val.u.xval);
|
2009-08-28 13:37:39 -06:00
|
|
|
if(v != 0) {
|
|
|
|
nodconst(&n2, types[tptr], v);
|
|
|
|
gins(optoas(OADD, types[tptr]), &n2, &n1);
|
|
|
|
}
|
|
|
|
} else {
|
2009-08-28 16:44:24 -06:00
|
|
|
gmove(&nodes[1], &n1);
|
|
|
|
if(!smallintconst(&nodes[3]) || mpgetfix(nodes[3].val.u.xval) != 1)
|
|
|
|
gins(optoas(OMUL, types[tptr]), &nodes[3], &n1);
|
2009-08-28 13:37:39 -06:00
|
|
|
gins(optoas(OADD, types[tptr]), &n2, &n1);
|
|
|
|
}
|
|
|
|
|
|
|
|
n2 = *res;
|
|
|
|
n2.xoffset += Array_array;
|
|
|
|
gins(optoas(OAS, types[tptr]), &n1, &n2);
|
|
|
|
regfree(&n1);
|
2009-08-27 15:59:26 -06:00
|
|
|
|
|
|
|
for(i=0; i<4; i++) {
|
2009-08-28 16:44:24 -06:00
|
|
|
if(nodes[i].op == OREGISTER)
|
|
|
|
regfree(&nodes[i]);
|
2009-08-27 15:59:26 -06:00
|
|
|
}
|
|
|
|
return 1;
|
2009-08-27 14:19:44 -06:00
|
|
|
|
|
|
|
no:
|
|
|
|
return 0;
|
|
|
|
}
|