ref: 15c3f45e5f7bccd121726de9b3adac001ffac887
parent: b44440bd165bdaa8c5764eead3fc54cadab7334f
author: cinap_lenrek <[email protected]>
date: Wed Apr 17 19:38:00 EDT 2019
7c: fix long to vlong/pointer conversion, avoid negative immediate offsets we have to explicitely convert to vlong by sign or zero extending as not every operation leaves a proper zero/sign extended result in the register. for example NEGW will zero extend, breaking negative int offsets on pointers. we explicitely insert SXTW or MOVWU instructions which the peephole optimizer takes out again when it is safe todo so. when promoting constant offsets to immediate offsets, make sure the offset will be in range. otherwise the linker will produce not so optimal pointer arithmetic instructions to calculate the offset.
--- a/sys/src/cmd/7c/cgen.c
+++ b/sys/src/cmd/7c/cgen.c
@@ -390,7 +390,7 @@
r = l;
while(r->op == OADD)
r = r->right;
- if(sconst(r) && (v = r->vconst+nod.xoffset) >= -4096 && v < 4096) {
+ if(usableoffset(n, nod.xoffset, r)){
v = r->vconst;
r->vconst = 0;
cgen(l, &nod);
@@ -613,7 +613,7 @@
r = n->left;
while(r->op == OADD)
r = r->right;
- if(sconst(r) && (v = r->vconst+t->xoffset) >= -4096 && v < 4096) {
+ if(usableoffset(n, t->xoffset, r)) {
v = r->vconst;
r->vconst = 0;
lcgen(n, t);
@@ -623,11 +623,17 @@
return;
}
} else if(n->op == OINDREG) {
- if((v = n->xoffset) >= -4096 && v < 4096) {
+ if(usableoffset(n, t->xoffset+n->xoffset, nil)) {
+ Type *tt = n->type;
+ n->type = types[TIND];
n->op = OREGISTER;
+ v = n->xoffset;
+ n->xoffset = 0;
cgen(n, t);
t->xoffset += v;
+ n->xoffset = v;
n->op = OINDREG;
+ n->type = tt;
regind(t, n);
return;
}
--- a/sys/src/cmd/7c/peep.c
+++ b/sys/src/cmd/7c/peep.c
@@ -2,6 +2,9 @@
int xtramodes(Reg*, Adr*);
+Reg* findpre(Reg *r, Adr *v);
+Reg* findinc(Reg *r, Reg *r2, Adr *v);
+
void
peep(void)
{
@@ -55,6 +58,21 @@
t++;
}
}
+ if(p->as == ASXTW){
+ r1 = findpre(r, &p->from);
+ if(r1 != R){
+ p1 = r1->prog;
+ switch(p1->as){
+ case AMOVB:
+ case AMOVBU:
+ case AMOVH:
+ case AMOVHU:
+ case AMOVW:
+ p->as = AMOVW;
+ break;
+ }
+ }
+ }
if(p->as == AMOV || p->as == AMOVW || p->as == AFMOVS || p->as == AFMOVD)
if(regtyp(&p->to)) {
if(p->from.type == D_CONST)
@@ -425,6 +443,7 @@
case AFMOVD:
case AMOVW:
case AMOV:
+ case ASXTW:
if(p->to.type == v1->type)
if(p->to.reg == v1->reg)
goto gotit;
@@ -954,8 +973,8 @@
case AMOVBU:
case AMOVW:
case AMOVWU:
+ case ASXTW:
case AMOV:
-
case AMVN:
case AMVNW:
case ANEG:
--- a/sys/src/cmd/7c/reg.c
+++ b/sys/src/cmd/7c/reg.c
@@ -150,6 +150,7 @@
case AMOVHU:
case AMOVW:
case AMOVWU:
+ case ASXTW:
case AFMOVS:
case AFCVTSD:
case AFMOVD:
--- a/sys/src/cmd/7c/txt.c
+++ b/sys/src/cmd/7c/txt.c
@@ -761,7 +761,10 @@
case TVLONG:
case TUVLONG:
case TIND:
- a = AMOV;
+ if(typeu[ft])
+ a = AMOVWU;
+ else
+ a = ASXTW;
break;
}
break;
@@ -785,15 +788,17 @@
case TUINT:
case TLONG:
case TULONG:
- case TVLONG:
- case TUVLONG:
- case TIND:
case TSHORT:
case TUSHORT:
case TCHAR:
case TUCHAR:
- a = AMOV; /* TO DO: conversion done? */
+ a = AMOVWU;
break;
+ case TVLONG:
+ case TUVLONG:
+ case TIND:
+ a = AMOV;
+ break;
}
break;
case TSHORT:
@@ -1323,16 +1328,19 @@
int
usableoffset(Node *n, vlong o, Node *v)
{
- int s, et;
+ int s;
- et = v->type->etype;
- if(v->op != OCONST || typefd[et])
- return 0;
+ if(v != nil){
+ if(v->op != OCONST || typefd[v->type->etype])
+ return 0;
+ o += v->vconst;
+ }
s = n->type->width;
if(s > 16)
s = 16;
- o += v->vconst;
- return o >= -256 || o < 4095*s;
+ if((o % s) != 0)
+ return 0;
+ return o >= -256 && o < 4096*s;
}
long