summaryrefslogtreecommitdiff
path: root/src/cmd/5g/ggen.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/5g/ggen.c')
-rw-r--r--src/cmd/5g/ggen.c552
1 files changed, 146 insertions, 406 deletions
diff --git a/src/cmd/5g/ggen.c b/src/cmd/5g/ggen.c
index de100620b..de1671bb6 100644
--- a/src/cmd/5g/ggen.c
+++ b/src/cmd/5g/ggen.c
@@ -28,6 +28,9 @@ void
markautoused(Prog* p)
{
for (; p; p = p->link) {
+ if (p->as == ATYPE)
+ continue;
+
if (p->from.name == D_AUTO && p->from.node)
p->from.node->used = 1;
@@ -40,27 +43,38 @@ markautoused(Prog* p)
void
fixautoused(Prog* p)
{
- for (; p; p = p->link) {
+ Prog **lp;
+
+ for (lp=&p; (p=*lp) != P; ) {
+ if (p->as == ATYPE && p->from.node && p->from.name == D_AUTO && !p->from.node->used) {
+ *lp = p->link;
+ continue;
+ }
+
if (p->from.name == D_AUTO && p->from.node)
p->from.offset += p->from.node->stkdelta;
if (p->to.name == D_AUTO && p->to.node)
p->to.offset += p->to.node->stkdelta;
+
+ lp = &p->link;
}
}
/*
* generate:
* call f
+ * proc=-1 normal call but no return
* proc=0 normal call
* proc=1 goroutine run in new proc
* proc=2 defer call save away stack
+ * proc=3 normal call to C pointer (not Go func value)
*/
void
ginscall(Node *f, int proc)
{
Prog *p;
- Node n1, r, con;
+ Node n1, r, r1, con;
switch(proc) {
default:
@@ -68,8 +82,26 @@ ginscall(Node *f, int proc)
break;
case 0: // normal call
- p = gins(ABL, N, f);
- afunclit(&p->to);
+ case -1: // normal call but no return
+ if(f->op == ONAME && f->class == PFUNC) {
+ p = gins(ABL, N, f);
+ afunclit(&p->to, f);
+ if(proc == -1 || noreturn(p))
+ gins(AUNDEF, N, N);
+ break;
+ }
+ nodreg(&r, types[tptr], 7);
+ nodreg(&r1, types[tptr], 1);
+ gmove(f, &r);
+ r.op = OINDREG;
+ gmove(&r, &r1);
+ r.op = OREGISTER;
+ r1.op = OINDREG;
+ gins(ABL, &r, &r1);
+ break;
+
+ case 3: // normal call of c function pointer
+ gins(ABL, N, f);
break;
case 1: // call in new proc (go)
@@ -120,7 +152,7 @@ ginscall(Node *f, int proc)
nodconst(&con, types[TINT32], 0);
p = gins(ACMP, &con, N);
p->reg = 0;
- patch(gbranch(ABNE, T), retpc);
+ patch(gbranch(ABNE, T, -1), retpc);
}
break;
}
@@ -136,6 +168,7 @@ cgen_callinter(Node *n, Node *res, int proc)
int r;
Node *i, *f;
Node tmpi, nodo, nodr, nodsp;
+ Prog *p;
i = n->left;
if(i->op != ODOTINTER)
@@ -180,7 +213,17 @@ cgen_callinter(Node *n, Node *res, int proc)
cgen(&nodo, &nodr); // REG = 0(REG) -- i.tab
nodo.xoffset = n->left->xoffset + 3*widthptr + 8;
- cgen(&nodo, &nodr); // REG = 20+offset(REG) -- i.tab->fun[f]
+
+ if(proc == 0) {
+ // plain call: use direct c function pointer - more efficient
+ cgen(&nodo, &nodr); // REG = 20+offset(REG) -- i.tab->fun[f]
+ nodr.op = OINDREG;
+ proc = 3;
+ } else {
+ // go/defer. generate go func value.
+ p = gins(AMOVW, &nodo, &nodr);
+ p->from.type = D_CONST; // REG = &(20+offset(REG)) -- i.tab->fun[f]
+ }
// BOTCH nodr.type = fntype;
nodr.type = n->left->type;
@@ -368,14 +411,19 @@ cgen_asop(Node *n)
case OOR:
a = optoas(n->etype, nl->type);
if(nl->addable) {
- regalloc(&n3, nr->type, N);
- cgen(nr, &n3);
+ if(smallintconst(nr))
+ n3 = *nr;
+ else {
+ regalloc(&n3, nr->type, N);
+ cgen(nr, &n3);
+ }
regalloc(&n2, nl->type, N);
cgen(nl, &n2);
gins(a, &n3, &n2);
cgen(&n2, nl);
regfree(&n2);
- regfree(&n3);
+ if(n3.op != OLITERAL)
+ regfree(&n3);
goto ret;
}
if(nr->ullman < UINF)
@@ -399,7 +447,9 @@ cgen_asop(Node *n)
hard:
n2.op = 0;
n1.op = 0;
- if(nr->ullman >= nl->ullman || nl->addable) {
+ if(nr->op == OLITERAL) {
+ // don't allocate a register for literals.
+ } else if(nr->ullman >= nl->ullman || nl->addable) {
regalloc(&n2, nr->type, N);
cgen(nr, &n2);
nr = &n2;
@@ -464,24 +514,99 @@ samereg(Node *a, Node *b)
}
/*
+ * generate high multiply
+ * res = (nl * nr) >> wordsize
+ */
+void
+cgen_hmul(Node *nl, Node *nr, Node *res)
+{
+ int w;
+ Node n1, n2, *tmp;
+ Type *t;
+ Prog *p;
+
+ if(nl->ullman < nr->ullman) {
+ tmp = nl;
+ nl = nr;
+ nr = tmp;
+ }
+ t = nl->type;
+ w = t->width * 8;
+ regalloc(&n1, t, res);
+ cgen(nl, &n1);
+ regalloc(&n2, t, N);
+ cgen(nr, &n2);
+ switch(simtype[t->etype]) {
+ case TINT8:
+ case TINT16:
+ gins(optoas(OMUL, t), &n2, &n1);
+ gshift(AMOVW, &n1, SHIFT_AR, w, &n1);
+ break;
+ case TUINT8:
+ case TUINT16:
+ gins(optoas(OMUL, t), &n2, &n1);
+ gshift(AMOVW, &n1, SHIFT_LR, w, &n1);
+ break;
+ case TINT32:
+ case TUINT32:
+ // perform a long multiplication.
+ if(issigned[t->etype])
+ p = gins(AMULL, &n2, N);
+ else
+ p = gins(AMULLU, &n2, N);
+ // n2 * n1 -> (n1 n2)
+ p->reg = n1.val.u.reg;
+ p->to.type = D_REGREG;
+ p->to.reg = n1.val.u.reg;
+ p->to.offset = n2.val.u.reg;
+ break;
+ default:
+ fatal("cgen_hmul %T", t);
+ break;
+ }
+ cgen(&n1, res);
+ regfree(&n1);
+ regfree(&n2);
+}
+
+/*
* generate shift according to op, one of:
* res = nl << nr
* res = nl >> nr
*/
void
-cgen_shift(int op, Node *nl, Node *nr, Node *res)
+cgen_shift(int op, int bounded, Node *nl, Node *nr, Node *res)
{
Node n1, n2, n3, nt, t, lo, hi;
- int w;
+ int w, v;
Prog *p1, *p2, *p3;
Type *tr;
uvlong sc;
+ USED(bounded);
if(nl->type->width > 4)
fatal("cgen_shift %T", nl->type);
w = nl->type->width * 8;
+ if(op == OLROT) {
+ v = mpgetfix(nr->val.u.xval);
+ regalloc(&n1, nl->type, res);
+ if(w == 32) {
+ cgen(nl, &n1);
+ gshift(AMOVW, &n1, SHIFT_RR, w-v, &n1);
+ } else {
+ regalloc(&n2, nl->type, N);
+ cgen(nl, &n2);
+ gshift(AMOVW, &n2, SHIFT_LL, v, &n1);
+ gshift(AORR, &n2, SHIFT_LR, w-v, &n1);
+ regfree(&n2);
+ }
+ gmove(&n1, res);
+ regfree(&n1);
+ return;
+ }
+
if(nr->op == OLITERAL) {
regalloc(&n1, nl->type, res);
cgen(nl, &n1);
@@ -524,6 +649,7 @@ cgen_shift(int op, Node *nl, Node *nr, Node *res)
regalloc(&n3, types[TUINT32], N);
gmove(&lo, &n1);
gmove(&hi, &n3);
+ splitclean();
gins(ATST, &n3, N);
nodconst(&t, types[TUINT32], w);
p1 = gins(AMOVW, &t, &n1);
@@ -546,9 +672,10 @@ cgen_shift(int op, Node *nl, Node *nr, Node *res)
// test for shift being 0
gins(ATST, &n1, N);
- p3 = gbranch(ABEQ, T);
+ p3 = gbranch(ABEQ, T, -1);
// test and fix up large shifts
+ // TODO: if(!bounded), don't emit some of this.
regalloc(&n3, tr, N);
nodconst(&t, types[TUINT32], w);
gmove(&t, &n3);
@@ -589,7 +716,12 @@ clearfat(Node *nl)
if(debug['g'])
dump("\nclearfat", nl);
+
w = nl->type->width;
+ // Avoid taking the address for simple enough types.
+ if(componentgen(N, nl))
+ return;
+
c = w % 4; // bytes
q = w / 4; // quads
@@ -613,7 +745,7 @@ clearfat(Node *nl)
p = gins(ACMP, &dst, N);
raddr(&end, p);
- patch(gbranch(ABNE, T), pl);
+ patch(gbranch(ABNE, T, 0), pl);
regfree(&end);
} else
@@ -637,395 +769,3 @@ clearfat(Node *nl)
regfree(&dst);
regfree(&nz);
}
-
-static int
-regcmp(const void *va, const void *vb)
-{
- Node *ra, *rb;
-
- ra = (Node*)va;
- rb = (Node*)vb;
- return ra->local - rb->local;
-}
-
-static Prog* throwpc;
-
-// We're only going to bother inlining if we can
-// convert all the arguments to 32 bits safely. Can we?
-static int
-fix64(NodeList *nn, int n)
-{
- NodeList *l;
- Node *r;
- int i;
-
- l = nn;
- for(i=0; i<n; i++) {
- r = l->n->right;
- if(is64(r->type) && !smallintconst(r)) {
- if(r->op == OCONV)
- r = r->left;
- if(is64(r->type))
- return 0;
- }
- l = l->next;
- }
- return 1;
-}
-
-void
-getargs(NodeList *nn, Node *reg, int n)
-{
- NodeList *l;
- int i;
-
- throwpc = nil;
-
- l = nn;
- for(i=0; i<n; i++) {
- if(!smallintconst(l->n->right) && !isslice(l->n->right->type)) {
- regalloc(reg+i, l->n->right->type, N);
- cgen(l->n->right, reg+i);
- } else
- reg[i] = *l->n->right;
- if(reg[i].local != 0)
- yyerror("local used");
- reg[i].local = l->n->left->xoffset;
- l = l->next;
- }
- qsort((void*)reg, n, sizeof(*reg), regcmp);
- for(i=0; i<n; i++)
- reg[i].local = 0;
-}
-
-void
-cmpandthrow(Node *nl, Node *nr)
-{
- vlong cl;
- Prog *p1;
- int op;
- Node *c, n1, n2;
-
- op = OLE;
- if(smallintconst(nl)) {
- cl = mpgetfix(nl->val.u.xval);
- if(cl == 0)
- return;
- if(smallintconst(nr))
- return;
-
- // put the constant on the right
- op = brrev(op);
- c = nl;
- nl = nr;
- nr = c;
- }
-
- n1.op = OXXX;
- if(nr->op != OREGISTER) {
- regalloc(&n1, types[TUINT32], N);
- gmove(nr, &n1);
- nr = &n1;
- }
- n2.op = OXXX;
- if(nl->op != OREGISTER) {
- regalloc(&n2, types[TUINT32], N);
- gmove(nl, &n2);
- nl = &n2;
- }
- gcmp(optoas(OCMP, types[TUINT32]), nl, nr);
- if(nr == &n1)
- regfree(&n1);
- if(nl == &n2)
- regfree(&n2);
- if(throwpc == nil) {
- p1 = gbranch(optoas(op, types[TUINT32]), T);
- throwpc = pc;
- ginscall(panicslice, 0);
- patch(p1, pc);
- } else {
- op = brcom(op);
- p1 = gbranch(optoas(op, types[TUINT32]), T);
- patch(p1, throwpc);
- }
-}
-
-int
-sleasy(Node *n)
-{
- if(n->op != ONAME)
- return 0;
- if(!n->addable)
- return 0;
- return 1;
-}
-
-// generate inline code for
-// slicearray
-// sliceslice
-// arraytoslice
-int
-cgen_inline(Node *n, Node *res)
-{
- Node nodes[5];
- Node n1, n2, n3, nres, ntemp;
- vlong v;
- int i, narg;
-
- if(n->op != OCALLFUNC)
- goto no;
- if(!n->left->addable)
- goto no;
- if(n->left->sym == S)
- goto no;
- if(n->left->sym->pkg != runtimepkg)
- goto no;
- if(strcmp(n->left->sym->name, "slicearray") == 0)
- goto slicearray;
- if(strcmp(n->left->sym->name, "sliceslice") == 0) {
- narg = 4;
- goto sliceslice;
- }
- if(strcmp(n->left->sym->name, "sliceslice1") == 0) {
- narg = 3;
- goto sliceslice;
- }
- goto no;
-
-slicearray:
- if(!sleasy(res))
- goto no;
- if(!fix64(n->list, 5))
- goto no;
- getargs(n->list, nodes, 5);
-
- // if(hb[3] > nel[1]) goto throw
- cmpandthrow(&nodes[3], &nodes[1]);
-
- // if(lb[2] > hb[3]) goto throw
- cmpandthrow(&nodes[2], &nodes[3]);
-
- // len = hb[3] - lb[2] (destroys hb)
- n2 = *res;
- n2.type = types[TUINT32];
- n2.xoffset += Array_nel;
-
- if(smallintconst(&nodes[3]) && smallintconst(&nodes[2])) {
- v = mpgetfix(nodes[3].val.u.xval) -
- mpgetfix(nodes[2].val.u.xval);
- nodconst(&n1, types[TUINT32], v);
- gmove(&n1, &n2);
- } else {
- 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);
- gmove(&n1, &n2);
- regfree(&n1);
- }
-
- // cap = nel[1] - lb[2] (destroys nel)
- n2 = *res;
- n2.type = types[TUINT32];
- n2.xoffset += Array_cap;
-
- if(smallintconst(&nodes[1]) && smallintconst(&nodes[2])) {
- v = mpgetfix(nodes[1].val.u.xval) -
- mpgetfix(nodes[2].val.u.xval);
- nodconst(&n1, types[TUINT32], v);
- gmove(&n1, &n2);
- } else {
- 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);
- gmove(&n1, &n2);
- regfree(&n1);
- }
-
- // if slice could be too big, dereference to
- // catch nil array pointer.
- if(nodes[0].op == OREGISTER && nodes[0].type->type->width >= unmappedzero) {
- n2 = nodes[0];
- n2.xoffset = 0;
- n2.op = OINDREG;
- n2.type = types[TUINT8];
- regalloc(&n1, types[TUINT32], N);
- gins(AMOVB, &n2, &n1);
- regfree(&n1);
- }
-
- // ary = old[0] + (lb[2] * width[4]) (destroys old)
- n2 = *res;
- n2.type = types[tptr];
- n2.xoffset += Array_array;
-
- if(smallintconst(&nodes[2]) && smallintconst(&nodes[4])) {
- v = mpgetfix(nodes[2].val.u.xval) *
- mpgetfix(nodes[4].val.u.xval);
- if(v != 0) {
- nodconst(&n1, types[tptr], v);
- gins(optoas(OADD, types[tptr]), &n1, &nodes[0]);
- }
- } else {
- regalloc(&n1, types[tptr], &nodes[2]);
- gmove(&nodes[2], &n1);
- if(!smallintconst(&nodes[4]) || mpgetfix(nodes[4].val.u.xval) != 1) {
- regalloc(&n3, types[tptr], N);
- gmove(&nodes[4], &n3);
- gins(optoas(OMUL, types[tptr]), &n3, &n1);
- regfree(&n3);
- }
- gins(optoas(OADD, types[tptr]), &n1, &nodes[0]);
- regfree(&n1);
- }
- gmove(&nodes[0], &n2);
-
- for(i=0; i<5; i++) {
- if(nodes[i].op == OREGISTER)
- regfree(&nodes[i]);
- }
- return 1;
-
-sliceslice:
- if(!fix64(n->list, narg))
- goto no;
- ntemp.op = OXXX;
- if(!sleasy(n->list->n->right)) {
- Node *n0;
-
- n0 = n->list->n->right;
- tempname(&ntemp, res->type);
- cgen(n0, &ntemp);
- n->list->n->right = &ntemp;
- getargs(n->list, nodes, narg);
- n->list->n->right = n0;
- } else
- getargs(n->list, nodes, narg);
-
- nres = *res; // result
- if(!sleasy(res)) {
- if(ntemp.op == OXXX)
- tempname(&ntemp, res->type);
- nres = ntemp;
- }
-
- if(narg == 3) { // old[lb:]
- // move width to where it would be for old[lb:hb]
- nodes[3] = nodes[2];
- nodes[2].op = OXXX;
-
- // if(lb[1] > old.nel[0]) goto throw;
- n2 = nodes[0];
- n2.xoffset += Array_nel;
- n2.type = types[TUINT32];
- cmpandthrow(&nodes[1], &n2);
-
- // ret.nel = old.nel[0]-lb[1];
- n2 = nodes[0];
- n2.type = types[TUINT32];
- n2.xoffset += Array_nel;
-
- regalloc(&n1, types[TUINT32], N);
- gmove(&n2, &n1);
- if(!smallintconst(&nodes[1]) || mpgetfix(nodes[1].val.u.xval) != 0)
- gins(optoas(OSUB, types[TUINT32]), &nodes[1], &n1);
-
- n2 = nres;
- n2.type = types[TUINT32];
- n2.xoffset += Array_nel;
- gmove(&n1, &n2);
- regfree(&n1);
- } else { // old[lb:hb]
- // if(hb[2] > old.cap[0]) goto throw;
- n2 = nodes[0];
- n2.xoffset += Array_cap;
- n2.type = types[TUINT32];
- cmpandthrow(&nodes[2], &n2);
-
- // if(lb[1] > hb[2]) goto throw;
- cmpandthrow(&nodes[1], &nodes[2]);
-
- // ret.len = hb[2]-lb[1]; (destroys hb[2])
- n2 = nres;
- n2.type = types[TUINT32];
- n2.xoffset += Array_nel;
-
- if(smallintconst(&nodes[2]) && smallintconst(&nodes[1])) {
- v = mpgetfix(nodes[2].val.u.xval) -
- mpgetfix(nodes[1].val.u.xval);
- nodconst(&n1, types[TUINT32], v);
- gmove(&n1, &n2);
- } else {
- 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);
- gmove(&n1, &n2);
- regfree(&n1);
- }
- }
-
- // ret.cap = old.cap[0]-lb[1]; (uses hb[2])
- n2 = nodes[0];
- n2.type = types[TUINT32];
- n2.xoffset += Array_cap;
-
- regalloc(&n1, types[TUINT32], &nodes[2]);
- gmove(&n2, &n1);
- if(!smallintconst(&nodes[1]) || mpgetfix(nodes[1].val.u.xval) != 0)
- gins(optoas(OSUB, types[TUINT32]), &nodes[1], &n1);
-
- n2 = nres;
- n2.type = types[TUINT32];
- n2.xoffset += Array_cap;
- gmove(&n1, &n2);
- regfree(&n1);
-
- // ret.array = old.array[0]+lb[1]*width[3]; (uses lb[1])
- n2 = nodes[0];
- n2.type = types[tptr];
- n2.xoffset += Array_array;
- regalloc(&n3, types[tptr], N);
- gmove(&n2, &n3);
-
- regalloc(&n1, types[tptr], &nodes[1]);
- if(smallintconst(&nodes[1]) && smallintconst(&nodes[3])) {
- gmove(&n2, &n1);
- v = mpgetfix(nodes[1].val.u.xval) *
- mpgetfix(nodes[3].val.u.xval);
- if(v != 0) {
- nodconst(&n2, types[tptr], v);
- gins(optoas(OADD, types[tptr]), &n3, &n1);
- }
- } else {
- gmove(&nodes[1], &n1);
- if(!smallintconst(&nodes[3]) || mpgetfix(nodes[3].val.u.xval) != 1) {
- regalloc(&n2, types[tptr], N);
- gmove(&nodes[3], &n2);
- gins(optoas(OMUL, types[tptr]), &n2, &n1);
- regfree(&n2);
- }
- gins(optoas(OADD, types[tptr]), &n3, &n1);
- }
- regfree(&n3);
-
- n2 = nres;
- n2.type = types[tptr];
- n2.xoffset += Array_array;
- gmove(&n1, &n2);
- regfree(&n1);
-
- for(i=0; i<4; i++) {
- if(nodes[i].op == OREGISTER)
- regfree(&nodes[i]);
- }
-
- if(!sleasy(res)) {
- cgen(&nres, res);
- }
- return 1;
-
-no:
- return 0;
-}