Commit 22864716 authored by Russ Cox's avatar Russ Cox

5g: alignment fixes

Makes all.bash work after echo 4 >/proc/cpu/alignment,
which means kill the process on an unaligned access.

The default behavior on DreamPlug/GuruPlug/SheevaPlug
is to simulate an ARMv3 and just let the unaligned accesses
stop at the word boundary, resulting in all kinds of surprises.

Fixes #1240.

R=ken2
CC=golang-dev
https://golang.org/cl/4551064
parent 831c6844
...@@ -1190,7 +1190,8 @@ void ...@@ -1190,7 +1190,8 @@ void
sgen(Node *n, Node *res, int32 w) sgen(Node *n, Node *res, int32 w)
{ {
Node dst, src, tmp, nend; Node dst, src, tmp, nend;
int32 c, q, odst, osrc; int32 c, odst, osrc;
int dir, align, op;
Prog *p, *ploop; Prog *p, *ploop;
if(debug['g']) { if(debug['g']) {
...@@ -1200,17 +1201,39 @@ sgen(Node *n, Node *res, int32 w) ...@@ -1200,17 +1201,39 @@ sgen(Node *n, Node *res, int32 w)
} }
if(w == 0) if(w == 0)
return; return;
if(n->ullman >= UINF && res->ullman >= UINF) {
fatal("sgen UINF");
}
if(w < 0) if(w < 0)
fatal("sgen copy %d", w); fatal("sgen copy %d", w);
if(n->ullman >= UINF && res->ullman >= UINF)
fatal("sgen UINF");
if(n->type == T)
fatal("sgen: missing type");
// determine alignment.
// want to avoid unaligned access, so have to use
// smaller operations for less aligned types.
// for example moving [4]byte must use 4 MOVB not 1 MOVW.
align = n->type->align;
op = 0;
switch(align) {
default:
fatal("sgen: invalid alignment %d for %T", align, n->type);
case 1:
op = AMOVB;
break;
case 2:
op = AMOVH;
break;
case 4:
op = AMOVW;
break;
}
if(w%align)
fatal("sgen: unaligned size %d (align=%d) for %T", w, align, n->type);
c = w / align;
// offset on the stack // offset on the stack
osrc = stkof(n); osrc = stkof(n);
odst = stkof(res); odst = stkof(res);
if(osrc != -1000 && odst != -1000 && (osrc == 1000 || odst == 1000)) { if(osrc != -1000 && odst != -1000 && (osrc == 1000 || odst == 1000)) {
// osrc and odst both on stack, and at least one is in // osrc and odst both on stack, and at least one is in
// an unknown position. Could generate code to test // an unknown position. Could generate code to test
...@@ -1221,12 +1244,15 @@ sgen(Node *n, Node *res, int32 w) ...@@ -1221,12 +1244,15 @@ sgen(Node *n, Node *res, int32 w)
sgen(&tmp, res, w); sgen(&tmp, res, w);
return; return;
} }
if(osrc%align != 0 || odst%align != 0)
if(osrc % 4 != 0 || odst %4 != 0) fatal("sgen: unaligned offset src %d or dst %d (align %d)", osrc, odst, align);
fatal("sgen: non word(4) aligned offset src %d or dst %d", osrc, odst); // if we are copying forward on the stack and
// the src and dst overlap, then reverse direction
dir = align;
if(osrc < odst && odst < osrc+w)
dir = -dir;
regalloc(&dst, types[tptr], res); regalloc(&dst, types[tptr], res);
if(n->ullman >= res->ullman) { if(n->ullman >= res->ullman) {
agen(n, &dst); // temporarily use dst agen(n, &dst); // temporarily use dst
regalloc(&src, types[tptr], N); regalloc(&src, types[tptr], N);
...@@ -1240,141 +1266,64 @@ sgen(Node *n, Node *res, int32 w) ...@@ -1240,141 +1266,64 @@ sgen(Node *n, Node *res, int32 w)
regalloc(&tmp, types[TUINT32], N); regalloc(&tmp, types[TUINT32], N);
c = w % 4; // bytes // set up end marker
q = w / 4; // quads memset(&nend, 0, sizeof nend);
if(c >= 4) {
// if we are copying forward on the stack and regalloc(&nend, types[TUINT32], N);
// the src and dst overlap, then reverse direction
if(osrc < odst && odst < osrc+w) {
if(c != 0)
fatal("sgen: reverse character copy not implemented");
if(q >= 4) {
regalloc(&nend, types[TUINT32], N);
// set up end marker to 4 bytes before source
p = gins(AMOVW, &src, &nend);
p->from.type = D_CONST;
p->from.offset = -4;
// move src and dest to the end of block
p = gins(AMOVW, &src, &src);
p->from.type = D_CONST;
p->from.offset = (q-1)*4;
p = gins(AMOVW, &dst, &dst);
p->from.type = D_CONST;
p->from.offset = (q-1)*4;
p = gins(AMOVW, &src, &tmp);
p->from.type = D_OREG;
p->from.offset = -4;
p->scond |= C_PBIT;
ploop = p;
p = gins(AMOVW, &tmp, &dst); p = gins(AMOVW, &src, &nend);
p->to.type = D_OREG; p->from.type = D_CONST;
p->to.offset = -4; if(dir < 0)
p->scond |= C_PBIT; p->from.offset = dir;
else
p = gins(ACMP, &src, N); p->from.offset = w;
raddr(&nend, p); }
patch(gbranch(ABNE, T), ploop); // move src and dest to the end of block if necessary
if(dir < 0) {
p = gins(AMOVW, &src, &src);
p->from.type = D_CONST;
p->from.offset = w + dir;
regfree(&nend); p = gins(AMOVW, &dst, &dst);
} else { p->from.type = D_CONST;
// move src and dest to the end of block p->from.offset = w + dir;
p = gins(AMOVW, &src, &src); }
p->from.type = D_CONST;
p->from.offset = (q-1)*4; // move
if(c >= 4) {
p = gins(AMOVW, &dst, &dst); p = gins(op, &src, &tmp);
p->from.type = D_CONST; p->from.type = D_OREG;
p->from.offset = (q-1)*4; p->from.offset = dir;
p->scond |= C_PBIT;
while(q > 0) { ploop = p;
p = gins(AMOVW, &src, &tmp);
p->from.type = D_OREG; p = gins(op, &tmp, &dst);
p->from.offset = -4; p->to.type = D_OREG;
p->scond |= C_PBIT; p->to.offset = dir;
p->scond |= C_PBIT;
p = gins(AMOVW, &tmp, &dst);
p->to.type = D_OREG; p = gins(ACMP, &src, N);
p->to.offset = -4; raddr(&nend, p);
p->scond |= C_PBIT;
patch(gbranch(ABNE, T), ploop);
q--; regfree(&nend);
}
}
} else { } else {
// normal direction while(c-- > 0) {
if(q >= 4) { p = gins(op, &src, &tmp);
regalloc(&nend, types[TUINT32], N);
p = gins(AMOVW, &src, &nend);
p->from.type = D_CONST;
p->from.offset = q*4;
p = gins(AMOVW, &src, &tmp);
p->from.type = D_OREG; p->from.type = D_OREG;
p->from.offset = 4; p->from.offset = dir;
p->scond |= C_PBIT; p->scond |= C_PBIT;
ploop = p; ploop = p;
p = gins(AMOVW, &tmp, &dst); p = gins(op, &tmp, &dst);
p->to.type = D_OREG; p->to.type = D_OREG;
p->to.offset = 4; p->to.offset = dir;
p->scond |= C_PBIT; p->scond |= C_PBIT;
p = gins(ACMP, &src, N);
raddr(&nend, p);
patch(gbranch(ABNE, T), ploop);
regfree(&nend);
} else
while(q > 0) {
p = gins(AMOVW, &src, &tmp);
p->from.type = D_OREG;
p->from.offset = 4;
p->scond |= C_PBIT;
p = gins(AMOVW, &tmp, &dst);
p->to.type = D_OREG;
p->to.offset = 4;
p->scond |= C_PBIT;
q--;
}
if (c != 0) {
// MOVW (src), tmp
p = gins(AMOVW, &src, &tmp);
p->from.type = D_OREG;
// MOVW tmp<<((4-c)*8),src
gshift(AMOVW, &tmp, SHIFT_LL, ((4-c)*8), &src);
// MOVW src>>((4-c)*8),src
gshift(AMOVW, &src, SHIFT_LR, ((4-c)*8), &src);
// MOVW (dst), tmp
p = gins(AMOVW, &dst, &tmp);
p->from.type = D_OREG;
// MOVW tmp>>(c*8),tmp
gshift(AMOVW, &tmp, SHIFT_LR, (c*8), &tmp);
// MOVW tmp<<(c*8),tmp
gshift(AMOVW, &tmp, SHIFT_LL, c*8, &tmp);
// ORR src, tmp
gins(AORR, &src, &tmp);
// MOVW tmp, (dst)
p = gins(AMOVW, &tmp, &dst);
p->to.type = D_OREG;
} }
} }
regfree(&dst);
regfree(&dst);
regfree(&src); regfree(&src);
regfree(&tmp); regfree(&tmp);
} }
...@@ -234,9 +234,11 @@ dowidth(Type *t) ...@@ -234,9 +234,11 @@ dowidth(Type *t)
if(t->bound > cap) if(t->bound > cap)
yyerror("type %lT larger than address space", t); yyerror("type %lT larger than address space", t);
w = t->bound * t->type->width; w = t->bound * t->type->width;
if(w == 0)
w = 1;
t->align = t->type->align; t->align = t->type->align;
if(w == 0) {
w = 1;
t->align = 1;
}
} }
else if(t->bound == -1) { else if(t->bound == -1) {
w = sizeof_Array; w = sizeof_Array;
...@@ -253,10 +255,10 @@ dowidth(Type *t) ...@@ -253,10 +255,10 @@ dowidth(Type *t)
if(t->funarg) if(t->funarg)
fatal("dowidth fn struct %T", t); fatal("dowidth fn struct %T", t);
w = widstruct(t, 0, 1); w = widstruct(t, 0, 1);
if(w == 0) if(w == 0) {
w = 1; w = 1;
//if(t->align < widthptr) t->align = 1;
// warn("align %d: %T\n", t->align, t); }
break; break;
case TFUNC: case TFUNC:
......
...@@ -59,6 +59,8 @@ allocparams(void) ...@@ -59,6 +59,8 @@ allocparams(void)
fatal("bad width"); fatal("bad width");
stksize += w; stksize += w;
stksize = rnd(stksize, n->type->align); stksize = rnd(stksize, n->type->align);
if(thechar == '5')
stksize = rnd(stksize, widthptr);
n->xoffset = -stksize; n->xoffset = -stksize;
} }
lineno = lno; lineno = lno;
...@@ -698,6 +700,8 @@ tempname(Node *n, Type *t) ...@@ -698,6 +700,8 @@ tempname(Node *n, Type *t)
w = t->width; w = t->width;
stksize += w; stksize += w;
stksize = rnd(stksize, t->align); stksize = rnd(stksize, t->align);
if(thechar == '5')
stksize = rnd(stksize, widthptr);
n->xoffset = -stksize; n->xoffset = -stksize;
n->pun = anyregalloc(); n->pun = anyregalloc();
} }
...@@ -284,6 +284,7 @@ stringsym(char *s, int len) ...@@ -284,6 +284,7 @@ stringsym(char *s, int len)
off = dsname(sym, off, s+n, m); off = dsname(sym, off, s+n, m);
} }
off = duint8(sym, off, 0); // terminating NUL for runtime off = duint8(sym, off, 0); // terminating NUL for runtime
off = (off+widthptr-1)&~(widthptr-1); // round to pointer alignment
ggloblsym(sym, off, 1); ggloblsym(sym, off, 1);
text(); text();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment