void
cgen(Node *n, Node *res)
{
- Node *nl, *nr, *r, n1, n2, nt, f0, f1;
+ Node *nl, *nr, *r, n1, n2, nt;
Prog *p1, *p2, *p3;
int a;
}
}
- if(nl != N && isfloat[n->type->etype] && isfloat[nl->type->etype])
- goto flt;
+ if(nl != N && isfloat[n->type->etype] && isfloat[nl->type->etype]) {
+ cgen_float(n, res);
+ return;
+ }
switch(n->op) {
default:
gins(a, N, &n1);
gmove(&n1, res);
return;
-
-flt: // floating-point. 387 (not SSE2) to interoperate with 8c
- nodreg(&f0, nl->type, D_F0);
- nodreg(&f1, n->type, D_F0+1);
- if(nr != N)
- goto flt2;
-
- // unary
- cgen(nl, &f0);
- if(n->op != OCONV && n->op != OPLUS)
- gins(foptoas(n->op, n->type, 0), N, N);
- gmove(&f0, res);
- return;
-
-flt2: // binary
- if(nl->ullman >= nr->ullman) {
- cgen(nl, &f0);
- if(nr->addable)
- gins(foptoas(n->op, n->type, 0), nr, &f0);
- else {
- cgen(nr, &f0);
- gins(foptoas(n->op, n->type, Fpop), &f0, &f1);
- }
- } else {
- cgen(nr, &f0);
- if(nl->addable)
- gins(foptoas(n->op, n->type, Frev), nl, &f0);
- else {
- cgen(nl, &f0);
- gins(foptoas(n->op, n->type, Frev|Fpop), &f0, &f1);
- }
- }
- gmove(&f0, res);
- return;
}
/*
{
int et, a;
Node *nl, *nr, *r;
- Node n1, n2, tmp, t1, t2, ax;
- NodeList *ll;
+ Node n1, n2, tmp;
Prog *p1, *p2;
if(debug['g']) {
patch(gins(AEND, N, N), to);
return;
}
+ nl = n->left;
nr = N;
+ if(nl != N && isfloat[nl->type->etype]) {
+ bgen_float(n, true, likely, to);
+ return;
+ }
+
switch(n->op) {
default:
def:
case OGE:
a = n->op;
if(!true) {
- if(isfloat[nl->type->etype]) {
- // brcom is not valid on floats when NaN is involved.
- p1 = gbranch(AJMP, T, 0);
- p2 = gbranch(AJMP, T, 0);
- patch(p1, pc);
- ll = n->ninit; // avoid re-genning ninit
- n->ninit = nil;
- bgen(n, 1, -likely, p2);
- n->ninit = ll;
- patch(gbranch(AJMP, T, 0), to);
- patch(p2, pc);
- break;
- }
a = brcom(a);
true = !true;
}
break;
}
- if(isfloat[nr->type->etype]) {
- a = brrev(a); // because the args are stacked
- if(a == OGE || a == OGT) {
- // only < and <= work right with NaN; reverse if needed
- r = nr;
- nr = nl;
- nl = r;
- a = brrev(a);
- }
- nodreg(&tmp, nr->type, D_F0);
- nodreg(&n2, nr->type, D_F0 + 1);
- nodreg(&ax, types[TUINT16], D_AX);
- et = simsimtype(nr->type);
- if(et == TFLOAT64) {
- if(nl->ullman > nr->ullman) {
- cgen(nl, &tmp);
- cgen(nr, &tmp);
- gins(AFXCHD, &tmp, &n2);
- } else {
- cgen(nr, &tmp);
- cgen(nl, &tmp);
- }
- gins(AFUCOMIP, &tmp, &n2);
- gins(AFMOVDP, &tmp, &tmp); // annoying pop but still better than STSW+SAHF
- } else {
- // TODO(rsc): The moves back and forth to memory
- // here are for truncating the value to 32 bits.
- // This handles 32-bit comparison but presumably
- // all the other ops have the same problem.
- // We need to figure out what the right general
- // solution is, besides telling people to use float64.
- tempname(&t1, types[TFLOAT32]);
- tempname(&t2, types[TFLOAT32]);
- cgen(nr, &t1);
- cgen(nl, &t2);
- gmove(&t2, &tmp);
- gins(AFCOMFP, &t1, &tmp);
- gins(AFSTSW, N, &ax);
- gins(ASAHF, N, N);
- }
- if(a == OEQ) {
- // neither NE nor P
- p1 = gbranch(AJNE, T, -likely);
- p2 = gbranch(AJPS, T, -likely);
- patch(gbranch(AJMP, T, 0), to);
- patch(p1, pc);
- patch(p2, pc);
- } else if(a == ONE) {
- // either NE or P
- patch(gbranch(AJNE, T, likely), to);
- patch(gbranch(AJPS, T, likely), to);
- } else
- patch(gbranch(optoas(a, nr->type), T, likely), to);
- break;
- }
if(iscomplex[nl->type->etype]) {
complexbool(a, nl, nr, true, likely, to);
break;
break;
}
- a = optoas(a, nr->type);
-
if(nr->ullman >= UINF) {
if(!nl->addable) {
tempname(&n1, nl->type);
}
regalloc(&n2, nr->type, N);
cgen(nr, &n2);
+ nr = &n2;
goto cmp;
}
if(smallintconst(nr)) {
gins(optoas(OCMP, nr->type), nl, nr);
- patch(gbranch(a, nr->type, likely), to);
+ patch(gbranch(optoas(a, nr->type), nr->type, likely), to);
break;
}
}
regalloc(&n2, nr->type, N);
gmove(nr, &n2);
+ nr = &n2;
cmp:
- gins(optoas(OCMP, nr->type), nl, &n2);
- patch(gbranch(a, nr->type, likely), to);
- regfree(&n2);
+ gins(optoas(OCMP, nr->type), nl, nr);
+ patch(gbranch(optoas(a, nr->type), nr->type, likely), to);
+
+ if(nl->op == OREGISTER)
+ regfree(nl);
+ regfree(nr);
break;
}
}
gmove(&dx, res);
}
+static void cgen_float387(Node *n, Node *res);
+static void cgen_floatsse(Node *n, Node *res);
+
+/*
+ * generate floating-point operation.
+ */
+void
+cgen_float(Node *n, Node *res)
+{
+ Node *nl;
+ Node n1, n2;
+ Prog *p1, *p2, *p3;
+
+ nl = n->left;
+ switch(n->op) {
+ case OEQ:
+ case ONE:
+ case OLT:
+ case OLE:
+ case OGE:
+ p1 = gbranch(AJMP, T, 0);
+ p2 = pc;
+ gmove(nodbool(1), res);
+ p3 = gbranch(AJMP, T, 0);
+ patch(p1, pc);
+ bgen(n, 1, 0, p2);
+ gmove(nodbool(0), res);
+ patch(p3, pc);
+ return;
+
+ case OPLUS:
+ cgen(nl, res);
+ return;
+
+ case OCONV:
+ if(eqtype(n->type, nl->type) || noconv(n->type, nl->type)) {
+ cgen(nl, res);
+ return;
+ }
+
+ tempname(&n2, n->type);
+ mgen(nl, &n1, res);
+ gmove(&n1, &n2);
+ gmove(&n2, res);
+ mfree(&n1);
+ return;
+ }
+
+ if(use_sse)
+ cgen_floatsse(n, res);
+ else
+ cgen_float387(n, res);
+}
+
+// floating-point. 387 (not SSE2)
+static void
+cgen_float387(Node *n, Node *res)
+{
+ Node f0, f1;
+ Node *nl, *nr;
+
+ nl = n->left;
+ nr = n->right;
+ nodreg(&f0, nl->type, D_F0);
+ nodreg(&f1, n->type, D_F0+1);
+ if(nr != N)
+ goto flt2;
+
+ // unary
+ cgen(nl, &f0);
+ if(n->op != OCONV && n->op != OPLUS)
+ gins(foptoas(n->op, n->type, 0), N, N);
+ gmove(&f0, res);
+ return;
+
+flt2: // binary
+ if(nl->ullman >= nr->ullman) {
+ cgen(nl, &f0);
+ if(nr->addable)
+ gins(foptoas(n->op, n->type, 0), nr, &f0);
+ else {
+ cgen(nr, &f0);
+ gins(foptoas(n->op, n->type, Fpop), &f0, &f1);
+ }
+ } else {
+ cgen(nr, &f0);
+ if(nl->addable)
+ gins(foptoas(n->op, n->type, Frev), nl, &f0);
+ else {
+ cgen(nl, &f0);
+ gins(foptoas(n->op, n->type, Frev|Fpop), &f0, &f1);
+ }
+ }
+ gmove(&f0, res);
+ return;
+
+}
+
+static void
+cgen_floatsse(Node *n, Node *res)
+{
+ Node *nl, *nr, *r;
+ Node n1, n2, nt;
+ int a;
+
+ nl = n->left;
+ nr = n->right;
+ switch(n->op) {
+ default:
+ dump("cgen_floatsse", n);
+ fatal("cgen_floatsse %O", n->op);
+ return;
+
+ case OMINUS:
+ case OCOM:
+ nr = nodintconst(-1);
+ convlit(&nr, n->type);
+ a = foptoas(OMUL, nl->type, 0);
+ goto sbop;
+
+ // symmetric binary
+ case OADD:
+ case OMUL:
+ a = foptoas(n->op, nl->type, 0);
+ goto sbop;
+
+ // asymmetric binary
+ case OSUB:
+ case OMOD:
+ case ODIV:
+ a = foptoas(n->op, nl->type, 0);
+ goto abop;
+ }
+
+sbop: // symmetric binary
+ if(nl->ullman < nr->ullman || nl->op == OLITERAL) {
+ r = nl;
+ nl = nr;
+ nr = r;
+ }
+
+abop: // asymmetric binary
+ if(nl->ullman >= nr->ullman) {
+ tempname(&nt, nl->type);
+ cgen(nl, &nt);
+ mgen(nr, &n2, N);
+ regalloc(&n1, nl->type, res);
+ gmove(&nt, &n1);
+ gins(a, &n2, &n1);
+ gmove(&n1, res);
+ regfree(&n1);
+ mfree(&n2);
+ } else {
+ regalloc(&n2, nr->type, res);
+ cgen(nr, &n2);
+ regalloc(&n1, nl->type, N);
+ cgen(nl, &n1);
+ gins(a, &n2, &n1);
+ regfree(&n2);
+ gmove(&n1, res);
+ regfree(&n1);
+ }
+ return;
+}
+
+void
+bgen_float(Node *n, int true, int likely, Prog *to)
+{
+ int et, a;
+ Node *nl, *nr, *r;
+ Node n1, n2, n3, tmp, t1, t2, ax;
+ Prog *p1, *p2;
+
+ nl = n->left;
+ nr = n->right;
+ a = n->op;
+ if(!true) {
+ // brcom is not valid on floats when NaN is involved.
+ p1 = gbranch(AJMP, T, 0);
+ p2 = gbranch(AJMP, T, 0);
+ patch(p1, pc);
+ // No need to avoid re-genning ninit.
+ bgen_float(n, 1, -likely, p2);
+ patch(gbranch(AJMP, T, 0), to);
+ patch(p2, pc);
+ return;
+ }
+
+ if(use_sse)
+ goto sse;
+ else
+ goto x87;
+
+x87:
+ a = brrev(a); // because the args are stacked
+ if(a == OGE || a == OGT) {
+ // only < and <= work right with NaN; reverse if needed
+ r = nr;
+ nr = nl;
+ nl = r;
+ a = brrev(a);
+ }
+
+ nodreg(&tmp, nr->type, D_F0);
+ nodreg(&n2, nr->type, D_F0 + 1);
+ nodreg(&ax, types[TUINT16], D_AX);
+ et = simsimtype(nr->type);
+ if(et == TFLOAT64) {
+ if(nl->ullman > nr->ullman) {
+ cgen(nl, &tmp);
+ cgen(nr, &tmp);
+ gins(AFXCHD, &tmp, &n2);
+ } else {
+ cgen(nr, &tmp);
+ cgen(nl, &tmp);
+ }
+ gins(AFUCOMIP, &tmp, &n2);
+ gins(AFMOVDP, &tmp, &tmp); // annoying pop but still better than STSW+SAHF
+ } else {
+ // TODO(rsc): The moves back and forth to memory
+ // here are for truncating the value to 32 bits.
+ // This handles 32-bit comparison but presumably
+ // all the other ops have the same problem.
+ // We need to figure out what the right general
+ // solution is, besides telling people to use float64.
+ tempname(&t1, types[TFLOAT32]);
+ tempname(&t2, types[TFLOAT32]);
+ cgen(nr, &t1);
+ cgen(nl, &t2);
+ gmove(&t2, &tmp);
+ gins(AFCOMFP, &t1, &tmp);
+ gins(AFSTSW, N, &ax);
+ gins(ASAHF, N, N);
+ }
+
+ goto ret;
+
+sse:
+ if(nr->ullman >= UINF) {
+ if(!nl->addable) {
+ tempname(&n1, nl->type);
+ cgen(nl, &n1);
+ nl = &n1;
+ }
+ if(!nr->addable) {
+ tempname(&tmp, nr->type);
+ cgen(nr, &tmp);
+ nr = &tmp;
+ }
+ regalloc(&n2, nr->type, N);
+ cgen(nr, &n2);
+ nr = &n2;
+ goto ssecmp;
+ }
+
+ if(!nl->addable) {
+ tempname(&n1, nl->type);
+ cgen(nl, &n1);
+ nl = &n1;
+ }
+
+ if(!nr->addable) {
+ tempname(&tmp, nr->type);
+ cgen(nr, &tmp);
+ nr = &tmp;
+ }
+
+ regalloc(&n2, nr->type, N);
+ gmove(nr, &n2);
+ nr = &n2;
+
+ if(nl->op != OREGISTER) {
+ regalloc(&n3, nl->type, N);
+ gmove(nl, &n3);
+ nl = &n3;
+ }
+
+ssecmp:
+ if(a == OGE || a == OGT) {
+ // only < and <= work right with NaN; reverse if needed
+ r = nr;
+ nr = nl;
+ nl = r;
+ a = brrev(a);
+ }
+
+ gins(foptoas(OCMP, nr->type, 0), nl, nr);
+ if(nl->op == OREGISTER)
+ regfree(nl);
+ regfree(nr);
+
+ret:
+ if(a == OEQ) {
+ // neither NE nor P
+ p1 = gbranch(AJNE, T, -likely);
+ p2 = gbranch(AJPS, T, -likely);
+ patch(gbranch(AJMP, T, 0), to);
+ patch(p1, pc);
+ patch(p2, pc);
+ } else if(a == ONE) {
+ // either NE or P
+ patch(gbranch(AJNE, T, likely), to);
+ patch(gbranch(AJPS, T, likely), to);
+ } else
+ patch(gbranch(optoas(a, nr->type), T, likely), to);
+
+}
int
foptoas(int op, Type *t, int flg)
{
- int et;
+ int et, a;
et = simtype[t->etype];
+ if(use_sse)
+ goto sse;
+
// If we need Fpop, it means we're working on
// two different floating-point registers, not memory.
// There the instruction only has a float64 form.
fatal("foptoas %O %T %#x", op, t, flg);
return 0;
+
+sse:
+ switch(CASE(op, et)) {
+ default:
+ fatal("foptoas-sse: no entry %O-%T", op, t);
+ break;
+
+ case CASE(OCMP, TFLOAT32):
+ a = AUCOMISS;
+ break;
+
+ case CASE(OCMP, TFLOAT64):
+ a = AUCOMISD;
+ break;
+
+ case CASE(OAS, TFLOAT32):
+ a = AMOVSS;
+ break;
+
+ case CASE(OAS, TFLOAT64):
+ a = AMOVSD;
+ break;
+
+ case CASE(OADD, TFLOAT32):
+ a = AADDSS;
+ break;
+
+ case CASE(OADD, TFLOAT64):
+ a = AADDSD;
+ break;
+
+ case CASE(OSUB, TFLOAT32):
+ a = ASUBSS;
+ break;
+
+ case CASE(OSUB, TFLOAT64):
+ a = ASUBSD;
+ break;
+
+ case CASE(OMUL, TFLOAT32):
+ a = AMULSS;
+ break;
+
+ case CASE(OMUL, TFLOAT64):
+ a = AMULSD;
+ break;
+
+ case CASE(ODIV, TFLOAT32):
+ a = ADIVSS;
+ break;
+
+ case CASE(ODIV, TFLOAT64):
+ a = ADIVSD;
+ break;
+ }
+ return a;
}
+
static int resvd[] =
{
// D_DI, // for movstring
reg[i] = 1;
for(i=D_AX; i<=D_DI; i++)
reg[i] = 0;
+ for(i=D_X0; i<=D_X7; i++)
+ reg[i] = 0;
for(i=0; i<nelem(resvd); i++)
reg[resvd[i]]++;
}
for(i=D_AX; i<=D_DI; i++)
if(reg[i])
yyerror("reg %R left allocated at %ux", i, regpc[i]);
+ for(i=D_X0; i<=D_X7; i++)
+ if(reg[i])
+ yyerror("reg %R left allocated\n", i);
}
int32
return 1;
ok:;
}
+ for(i=D_X0; i<=D_X7; i++)
+ if(reg[i])
+ return 1;
return 0;
}
et = simtype[t->etype];
switch(et) {
+ case TINT64:
+ case TUINT64:
+ fatal("regalloc64");
+
case TINT8:
case TUINT8:
case TINT16:
case TUINT16:
case TINT32:
case TUINT32:
- case TINT64:
- case TUINT64:
case TPTR32:
case TPTR64:
case TBOOL:
case TFLOAT32:
case TFLOAT64:
- i = D_F0;
- goto out;
+ if(!use_sse) {
+ i = D_F0;
+ goto out;
+ }
+ if(o != N && o->op == OREGISTER) {
+ i = o->val.u.reg;
+ if(i >= D_X0 && i <= D_X7)
+ goto out;
+ }
+ for(i=D_X0; i<=D_X7; i++)
+ if(reg[i] == 0)
+ goto out;
+ fprint(2, "registers allocated at\n");
+ for(i=D_X0; i<=D_X7; i++)
+ fprint(2, "\t%R\t%#lux\n", i, regpc[i]);
+ fatal("out of floating registers");
}
yyerror("regalloc: unknown type %T", t);
n->orig->sym = n->sym;
}
+static void floatmove(Node *f, Node *t);
+static void floatmove_387(Node *f, Node *t);
+static void floatmove_sse(Node *f, Node *t);
+
void
gmove(Node *f, Node *t)
{
int a, ft, tt;
Type *cvt;
- Node r1, r2, t1, t2, flo, fhi, tlo, thi, con, f0, f1, ax, dx, cx;
- Prog *p1, *p2, *p3;
+ Node r1, r2, flo, fhi, tlo, thi, con;
if(debug['M'])
print("gmove %N -> %N\n", f, t);
ft = simsimtype(f->type);
tt = simsimtype(t->type);
cvt = t->type;
-
+
if(iscomplex[ft] || iscomplex[tt]) {
complexmove(f, t);
return;
}
+ if(isfloat[ft] || isfloat[tt]) {
+ floatmove(f, t);
+ return;
+ }
// cannot have two integer memory operands;
// except 64-bit, which always copies via registers anyway.
// convert constant to desired type
if(f->op == OLITERAL) {
- if(tt == TFLOAT32)
- convconst(&con, types[TFLOAT64], &f->val);
- else
- convconst(&con, t->type, &f->val);
+ convconst(&con, t->type, &f->val);
f = &con;
ft = simsimtype(con.type);
-
- // some constants can't move directly to memory.
- if(ismem(t)) {
- // float constants come from memory.
- if(isfloat[tt])
- goto hard;
- }
}
// value -> value copy, only one memory operand.
gins(AMOVL, ncon(0), &thi);
splitclean();
return;
+ }
+
+ gins(a, f, t);
+ return;
+
+rsrc:
+ // requires register source
+ regalloc(&r1, f->type, t);
+ gmove(f, &r1);
+ gins(a, &r1, t);
+ regfree(&r1);
+ return;
+
+rdst:
+ // requires register destination
+ regalloc(&r1, t->type, t);
+ gins(a, f, &r1);
+ gmove(&r1, t);
+ regfree(&r1);
+ return;
+
+hard:
+ // requires register intermediate
+ regalloc(&r1, cvt, t);
+ gmove(f, &r1);
+ gmove(&r1, t);
+ regfree(&r1);
+ return;
+
+fatal:
+ // should not happen
+ fatal("gmove %N -> %N", f, t);
+}
+
+static void
+floatmove(Node *f, Node *t)
+{
+ Node r1, r2, t1, t2, tlo, thi, con, f0, f1, ax, dx, cx;
+ Type *cvt;
+ int a, ft, tt;
+ Prog *p1, *p2, *p3;
+
+ ft = simsimtype(f->type);
+ tt = simsimtype(t->type);
+ cvt = t->type;
+
+ // cannot have two floating point memory operands.
+ if(isfloat[ft] && isfloat[tt] && ismem(f) && ismem(t))
+ goto hard;
+
+ // convert constant to desired type
+ if(f->op == OLITERAL) {
+ convconst(&con, t->type, &f->val);
+ f = &con;
+ ft = simsimtype(con.type);
+
+ // some constants can't move directly to memory.
+ if(ismem(t)) {
+ // float constants come from memory.
+ if(isfloat[tt])
+ goto hard;
+ }
+ }
+
+ // value -> value copy, only one memory operand.
+ // figure out the instruction to use.
+ // break out of switch for one-instruction gins.
+ // goto rdst for "destination must be register".
+ // goto hard for "convert to cvt type first".
+ // otherwise handle and return.
+
+ switch(CASE(ft, tt)) {
+ default:
+ if(use_sse)
+ floatmove_sse(f, t);
+ else
+ floatmove_387(f, t);
+ return;
+
+ // float to very long integer.
+ case CASE(TFLOAT32, TINT64):
+ case CASE(TFLOAT64, TINT64):
+ if(f->op == OREGISTER) {
+ cvt = f->type;
+ goto hardmem;
+ }
+ nodreg(&r1, types[ft], D_F0);
+ if(ft == TFLOAT32)
+ gins(AFMOVF, f, &r1);
+ else
+ gins(AFMOVD, f, &r1);
+
+ // set round to zero mode during conversion
+ memname(&t1, types[TUINT16]);
+ memname(&t2, types[TUINT16]);
+ gins(AFSTCW, N, &t1);
+ gins(AMOVW, ncon(0xf7f), &t2);
+ gins(AFLDCW, &t2, N);
+ if(tt == TINT16)
+ gins(AFMOVWP, &r1, t);
+ else if(tt == TINT32)
+ gins(AFMOVLP, &r1, t);
+ else
+ gins(AFMOVVP, &r1, t);
+ gins(AFLDCW, &t1, N);
+ return;
+
+ case CASE(TFLOAT32, TUINT64):
+ case CASE(TFLOAT64, TUINT64):
+ if(!ismem(f)) {
+ cvt = f->type;
+ goto hardmem;
+ }
+ bignodes();
+ nodreg(&f0, types[ft], D_F0);
+ nodreg(&f1, types[ft], D_F0 + 1);
+ nodreg(&ax, types[TUINT16], D_AX);
+
+ if(ft == TFLOAT32)
+ gins(AFMOVF, f, &f0);
+ else
+ gins(AFMOVD, f, &f0);
+
+ // if 0 > v { answer = 0 }
+ gins(AFMOVD, &zerof, &f0);
+ gins(AFUCOMIP, &f0, &f1);
+ p1 = gbranch(optoas(OGT, types[tt]), T, 0);
+ // if 1<<64 <= v { answer = 0 too }
+ gins(AFMOVD, &two64f, &f0);
+ gins(AFUCOMIP, &f0, &f1);
+ p2 = gbranch(optoas(OGT, types[tt]), T, 0);
+ patch(p1, pc);
+ gins(AFMOVVP, &f0, t); // don't care about t, but will pop the stack
+ split64(t, &tlo, &thi);
+ gins(AMOVL, ncon(0), &tlo);
+ gins(AMOVL, ncon(0), &thi);
+ splitclean();
+ p1 = gbranch(AJMP, T, 0);
+ patch(p2, pc);
+
+ // in range; algorithm is:
+ // if small enough, use native float64 -> int64 conversion.
+ // otherwise, subtract 2^63, convert, and add it back.
+
+ // set round to zero mode during conversion
+ memname(&t1, types[TUINT16]);
+ memname(&t2, types[TUINT16]);
+ gins(AFSTCW, N, &t1);
+ gins(AMOVW, ncon(0xf7f), &t2);
+ gins(AFLDCW, &t2, N);
+
+ // actual work
+ gins(AFMOVD, &two63f, &f0);
+ gins(AFUCOMIP, &f0, &f1);
+ p2 = gbranch(optoas(OLE, types[tt]), T, 0);
+ gins(AFMOVVP, &f0, t);
+ p3 = gbranch(AJMP, T, 0);
+ patch(p2, pc);
+ gins(AFMOVD, &two63f, &f0);
+ gins(AFSUBDP, &f0, &f1);
+ gins(AFMOVVP, &f0, t);
+ split64(t, &tlo, &thi);
+ gins(AXORL, ncon(0x80000000), &thi); // + 2^63
+ patch(p3, pc);
+ splitclean();
+ // restore rounding mode
+ gins(AFLDCW, &t1, N);
+
+ patch(p1, pc);
+ return;
+
+ /*
+ * integer to float
+ */
+ case CASE(TINT64, TFLOAT32):
+ case CASE(TINT64, TFLOAT64):
+ if(t->op == OREGISTER)
+ goto hardmem;
+ nodreg(&f0, t->type, D_F0);
+ gins(AFMOVV, f, &f0);
+ if(tt == TFLOAT32)
+ gins(AFMOVFP, &f0, t);
+ else
+ gins(AFMOVDP, &f0, t);
+ return;
+
+ case CASE(TUINT64, TFLOAT32):
+ case CASE(TUINT64, TFLOAT64):
+ // algorithm is:
+ // if small enough, use native int64 -> float64 conversion.
+ // otherwise, halve (rounding to odd?), convert, and double.
+ nodreg(&ax, types[TUINT32], D_AX);
+ nodreg(&dx, types[TUINT32], D_DX);
+ nodreg(&cx, types[TUINT32], D_CX);
+ tempname(&t1, f->type);
+ split64(&t1, &tlo, &thi);
+ gmove(f, &t1);
+ gins(ACMPL, &thi, ncon(0));
+ p1 = gbranch(AJLT, T, 0);
+ // native
+ t1.type = types[TINT64];
+ nodreg(&r1, types[tt], D_F0);
+ gins(AFMOVV, &t1, &r1);
+ if(tt == TFLOAT32)
+ gins(AFMOVFP, &r1, t);
+ else
+ gins(AFMOVDP, &r1, t);
+ p2 = gbranch(AJMP, T, 0);
+ // simulated
+ patch(p1, pc);
+ gmove(&tlo, &ax);
+ gmove(&thi, &dx);
+ p1 = gins(ASHRL, ncon(1), &ax);
+ p1->from.index = D_DX; // double-width shift DX -> AX
+ p1->from.scale = 0;
+ gins(AMOVL, ncon(0), &cx);
+ gins(ASETCC, N, &cx);
+ gins(AORL, &cx, &ax);
+ gins(ASHRL, ncon(1), &dx);
+ gmove(&dx, &thi);
+ gmove(&ax, &tlo);
+ nodreg(&r1, types[tt], D_F0);
+ nodreg(&r2, types[tt], D_F0 + 1);
+ gins(AFMOVV, &t1, &r1);
+ gins(AFMOVD, &r1, &r1);
+ gins(AFADDDP, &r1, &r2);
+ if(tt == TFLOAT32)
+ gins(AFMOVFP, &r1, t);
+ else
+ gins(AFMOVDP, &r1, t);
+ patch(p2, pc);
+ splitclean();
+ return;
+ }
+
+ gins(a, f, t);
+ return;
+
+hard:
+ // requires register intermediate
+ regalloc(&r1, cvt, t);
+ gmove(f, &r1);
+ gmove(&r1, t);
+ regfree(&r1);
+ return;
+
+hardmem:
+ // requires memory intermediate
+ tempname(&r1, cvt);
+ gmove(f, &r1);
+ gmove(&r1, t);
+ return;
+}
+
+static void
+floatmove_387(Node *f, Node *t)
+{
+ Node r1, t1, t2;
+ Type *cvt;
+ Prog *p1, *p2, *p3;
+ int a, ft, tt;
+
+ ft = simsimtype(f->type);
+ tt = simsimtype(t->type);
+ cvt = t->type;
+
+ switch(CASE(ft, tt)) {
+ default:
+ goto fatal;
/*
* float to integer
case CASE(TFLOAT32, TUINT32):
case CASE(TFLOAT64, TUINT32):
// convert via int64.
- tempname(&t1, types[TINT64]);
- gmove(f, &t1);
- split64(&t1, &tlo, &thi);
- gins(ACMPL, &thi, ncon(0));
- p1 = gbranch(AJEQ, T, +1);
- gins(AMOVL, ncon(0), &tlo);
- patch(p1, pc);
- gmove(&tlo, t);
- splitclean();
- return;
-
- case CASE(TFLOAT32, TUINT64):
- case CASE(TFLOAT64, TUINT64):
- bignodes();
- nodreg(&f0, types[ft], D_F0);
- nodreg(&f1, types[ft], D_F0 + 1);
- nodreg(&ax, types[TUINT16], D_AX);
-
- gmove(f, &f0);
-
- // if 0 > v { answer = 0 }
- gmove(&zerof, &f0);
- gins(AFUCOMIP, &f0, &f1);
- p1 = gbranch(optoas(OGT, types[tt]), T, 0);
- // if 1<<64 <= v { answer = 0 too }
- gmove(&two64f, &f0);
- gins(AFUCOMIP, &f0, &f1);
- p2 = gbranch(optoas(OGT, types[tt]), T, 0);
- patch(p1, pc);
- gins(AFMOVVP, &f0, t); // don't care about t, but will pop the stack
- split64(t, &tlo, &thi);
- gins(AMOVL, ncon(0), &tlo);
- gins(AMOVL, ncon(0), &thi);
- splitclean();
- p1 = gbranch(AJMP, T, 0);
- patch(p2, pc);
-
- // in range; algorithm is:
- // if small enough, use native float64 -> int64 conversion.
- // otherwise, subtract 2^63, convert, and add it back.
-
- // set round to zero mode during conversion
- memname(&t1, types[TUINT16]);
- memname(&t2, types[TUINT16]);
- gins(AFSTCW, N, &t1);
- gins(AMOVW, ncon(0xf7f), &t2);
- gins(AFLDCW, &t2, N);
-
- // actual work
- gmove(&two63f, &f0);
- gins(AFUCOMIP, &f0, &f1);
- p2 = gbranch(optoas(OLE, types[tt]), T, 0);
- gins(AFMOVVP, &f0, t);
- p3 = gbranch(AJMP, T, 0);
- patch(p2, pc);
- gmove(&two63f, &f0);
- gins(AFSUBDP, &f0, &f1);
- gins(AFMOVVP, &f0, t);
- split64(t, &tlo, &thi);
- gins(AXORL, ncon(0x80000000), &thi); // + 2^63
- patch(p3, pc);
- splitclean();
- // restore rounding mode
- gins(AFLDCW, &t1, N);
-
- patch(p1, pc);
- return;
+ cvt = types[TINT64];
+ goto hardmem;
/*
* integer to float
cvt = types[TINT64];
goto hardmem;
- case CASE(TUINT64, TFLOAT32):
- case CASE(TUINT64, TFLOAT64):
- // algorithm is:
- // if small enough, use native int64 -> uint64 conversion.
- // otherwise, halve (rounding to odd?), convert, and double.
- nodreg(&ax, types[TUINT32], D_AX);
- nodreg(&dx, types[TUINT32], D_DX);
- nodreg(&cx, types[TUINT32], D_CX);
- tempname(&t1, f->type);
- split64(&t1, &tlo, &thi);
- gmove(f, &t1);
- gins(ACMPL, &thi, ncon(0));
- p1 = gbranch(AJLT, T, 0);
- // native
- t1.type = types[TINT64];
- gmove(&t1, t);
- p2 = gbranch(AJMP, T, 0);
- // simulated
- patch(p1, pc);
- gmove(&tlo, &ax);
- gmove(&thi, &dx);
- p1 = gins(ASHRL, ncon(1), &ax);
- p1->from.index = D_DX; // double-width shift DX -> AX
- p1->from.scale = 0;
- gins(AMOVL, ncon(0), &cx);
- gins(ASETCC, N, &cx);
- gins(AORL, &cx, &ax);
- gins(ASHRL, ncon(1), &dx);
- gmove(&dx, &thi);
- gmove(&ax, &tlo);
- nodreg(&r1, types[tt], D_F0);
- nodreg(&r2, types[tt], D_F0 + 1);
- gmove(&t1, &r1); // t1.type is TINT64 now, set above
- gins(AFMOVD, &r1, &r1);
- gins(AFADDDP, &r1, &r2);
- gmove(&r1, t);
- patch(p2, pc);
- splitclean();
- return;
-
/*
* float to float
*/
gins(a, f, t);
return;
-rsrc:
- // requires register source
- regalloc(&r1, f->type, t);
+hard:
+ // requires register intermediate
+ regalloc(&r1, cvt, t);
gmove(f, &r1);
- gins(a, &r1, t);
+ gmove(&r1, t);
regfree(&r1);
return;
-rdst:
- // requires register destination
- regalloc(&r1, t->type, t);
- gins(a, f, &r1);
+hardmem:
+ // requires memory intermediate
+ tempname(&r1, cvt);
+ gmove(f, &r1);
gmove(&r1, t);
- regfree(&r1);
+ return;
+
+fatal:
+ // should not happen
+ fatal("gmove %lN -> %lN", f, t);
+ return;
+}
+
+static void
+floatmove_sse(Node *f, Node *t)
+{
+ Node r1;
+ Type *cvt;
+ int a, ft, tt;
+
+ ft = simsimtype(f->type);
+ tt = simsimtype(t->type);
+
+ switch(CASE(ft, tt)) {
+ default:
+ // should not happen
+ fatal("gmove %N -> %N", f, t);
+ return;
+ /*
+ * float to integer
+ */
+ case CASE(TFLOAT32, TINT16):
+ case CASE(TFLOAT32, TINT8):
+ case CASE(TFLOAT32, TUINT16):
+ case CASE(TFLOAT32, TUINT8):
+ case CASE(TFLOAT64, TINT16):
+ case CASE(TFLOAT64, TINT8):
+ case CASE(TFLOAT64, TUINT16):
+ case CASE(TFLOAT64, TUINT8):
+ // convert via int32.
+ cvt = types[TINT32];
+ goto hard;
+
+ case CASE(TFLOAT32, TUINT32):
+ case CASE(TFLOAT64, TUINT32):
+ // convert via int64.
+ cvt = types[TINT64];
+ goto hardmem;
+
+ case CASE(TFLOAT32, TINT32):
+ a = ACVTTSS2SL;
+ goto rdst;
+
+ case CASE(TFLOAT64, TINT32):
+ a = ACVTTSD2SL;
+ goto rdst;
+
+ /*
+ * integer to float
+ */
+ case CASE(TINT8, TFLOAT32):
+ case CASE(TINT8, TFLOAT64):
+ case CASE(TINT16, TFLOAT32):
+ case CASE(TINT16, TFLOAT64):
+ case CASE(TUINT16, TFLOAT32):
+ case CASE(TUINT16, TFLOAT64):
+ case CASE(TUINT8, TFLOAT32):
+ case CASE(TUINT8, TFLOAT64):
+ // convert via int32 memory
+ cvt = types[TINT32];
+ goto hard;
+
+ case CASE(TUINT32, TFLOAT32):
+ case CASE(TUINT32, TFLOAT64):
+ // convert via int64 memory
+ cvt = types[TINT64];
+ goto hardmem;
+
+ case CASE(TINT32, TFLOAT32):
+ a = ACVTSL2SS;
+ goto rdst;
+
+ case CASE(TINT32, TFLOAT64):
+ a = ACVTSL2SD;
+ goto rdst;
+
+ /*
+ * float to float
+ */
+ case CASE(TFLOAT32, TFLOAT32):
+ a = AMOVSS;
+ break;
+
+ case CASE(TFLOAT64, TFLOAT64):
+ a = AMOVSD;
+ break;
+
+ case CASE(TFLOAT32, TFLOAT64):
+ a = ACVTSS2SD;
+ goto rdst;
+
+ case CASE(TFLOAT64, TFLOAT32):
+ a = ACVTSD2SS;
+ goto rdst;
+ }
+
+ gins(a, f, t);
return;
hard:
gmove(&r1, t);
return;
-fatal:
- // should not happen
- fatal("gmove %N -> %N", f, t);
+rdst:
+ // requires register destination
+ regalloc(&r1, t->type, t);
+ gins(a, f, &r1);
+ gmove(&r1, t);
+ regfree(&r1);
+ return;
}
int
if(as == AFMOVF && f && f->op == OREGISTER && t && t->op == OREGISTER)
fatal("gins MOVF reg, reg");
+ if(as == ACVTSD2SS && f && f->op == OLITERAL)
+ fatal("gins CVTSD2SS const");
+ if(as == AMOVSD && t && t->op == OREGISTER && t->val.u.reg == D_F0)
+ fatal("gins MOVSD into F0");
switch(as) {
case AMOVB: