Skip to content

Commit

Permalink
cmd/7g: fix comparisons
Browse files Browse the repository at this point in the history
Arm64 CPU uses op from, reg form, not op from, to.

Fixes golang#5

Change-Id: I5203ee8f1c4fef52da2ec3da2afae07bf7ff0273
  • Loading branch information
4ad committed Dec 15, 2014
1 parent b417b32 commit b5365e7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
18 changes: 9 additions & 9 deletions src/cmd/7g/cgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ cgen(Node *n, Node *res)
cgen(nl, &n1);

nodconst(&n2, types[tptr], 0);
gins(optoas(OCMP, types[tptr]), &n1, &n2);
gcmp(optoas(OCMP, types[tptr]), &n1, &n2);
p1 = gbranch(optoas(OEQ, types[tptr]), T, 0);

n2 = n1;
Expand Down Expand Up @@ -383,7 +383,7 @@ cgen(Node *n, Node *res)
cgen(nl, &n1);

nodconst(&n2, types[tptr], 0);
gins(optoas(OCMP, types[tptr]), &n1, &n2);
gcmp(optoas(OCMP, types[tptr]), &n1, &n2);
p1 = gbranch(optoas(OEQ, types[tptr]), T, 0);

n2 = n1;
Expand Down Expand Up @@ -735,7 +735,7 @@ agenr(Node *n, Node *a, Node *res)
p1->from.offset = nl->type->bound;
}
}
gins(optoas(OCMP, types[TUINT64]), &n2, &n4);
gcmp(optoas(OCMP, types[TUINT64]), &n2, &n4);
if(n4.op == OREGISTER)
regfree(&n4);
p1 = gbranch(optoas(OLT, types[TUINT64]), T, +1);
Expand Down Expand Up @@ -1094,7 +1094,7 @@ bgen(Node *n, int true, int likely, Prog *to)
regalloc(&n1, n->type, N);
cgen(n, &n1);
nodconst(&n2, n->type, 0);
gins(optoas(OCMP, n->type), &n1, &n2);
gcmp(optoas(OCMP, n->type), &n1, &n2);
a = ABNE;
if(!true)
a = ABEQ;
Expand Down Expand Up @@ -1201,7 +1201,7 @@ bgen(Node *n, int true, int likely, Prog *to)
nodconst(&tmp, types[tptr], 0);
regalloc(&n2, types[tptr], &n1);
gmove(&n1, &n2);
gins(optoas(OCMP, types[tptr]), &n2, &tmp);
gcmp(optoas(OCMP, types[tptr]), &n2, &tmp);
regfree(&n2);
patch(gbranch(a, types[tptr], likely), to);
regfree(&n1);
Expand All @@ -1220,7 +1220,7 @@ bgen(Node *n, int true, int likely, Prog *to)
nodconst(&tmp, types[tptr], 0);
regalloc(&n2, types[tptr], &n1);
gmove(&n1, &n2);
gins(optoas(OCMP, types[tptr]), &n2, &tmp);
gcmp(optoas(OCMP, types[tptr]), &n2, &tmp);
regfree(&n2);
patch(gbranch(a, types[tptr], likely), to);
regfree(&n1);
Expand Down Expand Up @@ -1254,7 +1254,7 @@ bgen(Node *n, int true, int likely, Prog *to)
// TODO(minux): cmpi does accept 16-bit signed immediate as p->to.
// and cmpli accepts 16-bit unsigned immediate.
//if(smallintconst(nr)) {
// gins(optoas(OCMP, nr->type), &n1, nr);
// gcmp(optoas(OCMP, nr->type), &n1, nr);
// patch(gbranch(optoas(a, nr->type), nr->type, likely), to);
// regfree(&n1);
// break;
Expand All @@ -1265,7 +1265,7 @@ bgen(Node *n, int true, int likely, Prog *to)
cmp:
l = &n1;
r = &n2;
gins(optoas(OCMP, nr->type), l, r);
gcmp(optoas(OCMP, nr->type), l, r);
if(isfloat[nr->type->etype] && (a == OLE || a == OGE)) {
// To get NaN right, must rewrite x <= y into separate x < y or x = y.
switch(a) {
Expand Down Expand Up @@ -1501,7 +1501,7 @@ sgen(Node *n, Node *ns, int64 w)
p->to.type = D_OREG;
p->to.offset = dir;

p = gins(ACMP, &src, &nend);
p = gcmp(ACMP, &src, &nend);

patch(gbranch(ABNE, T, 0), ploop);
regfree(&nend);
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/7g/gg.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ void afunclit(Addr*, Node*);
void nodfconst(Node*, Type*, Mpflt*);
void gtrack(Sym*);
void fixlargeoffset(Node *n);
void raddr(Node *n, Prog *p);
Prog* gcmp(int, Node*, Node*);

/*
* cplx.c
Expand Down
10 changes: 5 additions & 5 deletions src/cmd/7g/ggen.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ ginscall(Node *f, int proc)

if(proc == 2) {
nodreg(&reg, types[TINT64], D_R0+3);
p = gins(ACMP, &reg, N);
p = gcmp(ACMP, &reg, N);
p->to.type = D_REG;
p->to.reg = D_R0;
p = gbranch(ABEQ, T, +1);
Expand Down Expand Up @@ -562,7 +562,7 @@ dodiv(int op, Node *nl, Node *nr, Node *res)
}

// Handle divide-by-zero panic.
p1 = gins(optoas(OCMP, t), &tr, N);
p1 = gcmp(optoas(OCMP, t), &tr, N);
p1->to.type = D_REG;
p1->to.reg = REGZERO;
p1 = gbranch(optoas(ONE, t), T, +1);
Expand All @@ -573,7 +573,7 @@ dodiv(int op, Node *nl, Node *nr, Node *res)

if(check) {
nodconst(&nm1, t, -1);
gins(optoas(OCMP, t), &tr, &nm1);
gcmp(optoas(OCMP, t), &tr, &nm1);
p1 = gbranch(optoas(ONE, t), T, +1);
if(op == ODIV) {
// a / (-1) is -a.
Expand Down Expand Up @@ -857,7 +857,7 @@ cgen_shift(int op, int bounded, Node *nl, Node *nr, Node *res)
// test and fix up large shifts
if(!bounded) {
nodconst(&n3, tcount, nl->type->width*8);
gins(optoas(OCMP, tcount), &n1, &n3);
gcmp(optoas(OCMP, tcount), &n1, &n3);
p1 = gbranch(optoas(OLT, tcount), T, +1);
if(op == ORSH && issigned[nl->type->etype]) {
nodconst(&n3, types[TUINT32], nl->type->width*8-1);
Expand Down Expand Up @@ -923,7 +923,7 @@ clearfat(Node *nl)
p->to.offset = 8;
pl = p;

p = gins(ACMP, &dst, &end);
p = gcmp(ACMP, &dst, &end);
patch(gbranch(ABNE, T, 0), pl);

regfree(&end);
Expand Down
39 changes: 37 additions & 2 deletions src/cmd/7g/gsubr.c
Original file line number Diff line number Diff line change
Expand Up @@ -573,15 +573,15 @@ ginscon2(int as, Node *n2, vlong c)
fatal("ginscon2");
case ACMP:
if(-BIG <= c && c <= BIG) {
gins(as, n2, &n1);
gcmp(as, n2, &n1);
return;
}
break;
}
// MOV n1 into register first
regalloc(&ntmp, types[TINT64], N);
gins(AMOV, &n1, &ntmp);
gins(as, n2, &ntmp);
gcmp(as, n2, &ntmp);
regfree(&ntmp);
}

Expand Down Expand Up @@ -1083,6 +1083,41 @@ fixlargeoffset(Node *n)
}
}

/*
* insert n into reg slot of p
*/
void
raddr(Node *n, Prog *p)
{
Addr a;

naddr(n, &a, 1);
if(a.type != D_REG && a.type != D_FREG) {
if(n)
fatal("bad in raddr: %O", n->op);
else
fatal("bad in raddr: <null>");
p->reg = NREG;
} else
p->reg = a.reg;
}

/* generate a comparison
TODO(kaib): one of the args can actually be a small constant. relax the constraint and fix call sites.
*/
Prog*
gcmp(int as, Node *lhs, Node *rhs)
{
Prog *p;

if(lhs->op != OREGISTER)
fatal("bad operands to gcmp: %O %O", lhs->op, rhs->op);

p = gins(as, rhs, N);
raddr(lhs, p);
return p;
}

/*
* generate code to compute n;
* make a refer to result.
Expand Down

0 comments on commit b5365e7

Please sign in to comment.