Commit d4290311 authored by Russ Cox's avatar Russ Cox

5l, 6l, 8l: indent, outdent

This is entirely adding and removing tabs.
It looks weird but will make the diffs for the
next change easier to read.

R=ken2
CC=golang-dev
https://golang.org/cl/2490041
parent dd8afb80
...@@ -421,25 +421,25 @@ asmb(void) ...@@ -421,25 +421,25 @@ asmb(void)
seek(cout, OFFSET, 0); seek(cout, OFFSET, 0);
pc = INITTEXT; pc = INITTEXT;
for(p = firstp; p != P; p = p->link) { for(p = firstp; p != P; p = p->link) {
setarch(p); setarch(p);
if(p->as == ATEXT) { if(p->as == ATEXT) {
curtext = p; curtext = p;
autosize = p->to.offset + 4; autosize = p->to.offset + 4;
} }
if(p->pc != pc) { if(p->pc != pc) {
diag("phase error %lux sb %lux", diag("phase error %lux sb %lux",
p->pc, pc); p->pc, pc);
if(!debug['a']) if(!debug['a'])
prasm(curp); prasm(curp);
pc = p->pc; pc = p->pc;
} }
curp = p; curp = p;
o = oplook(p); /* could probably avoid this call */ o = oplook(p); /* could probably avoid this call */
if(thumb) if(thumb)
thumbasmout(p, o); thumbasmout(p, o);
else else
asmout(p, o); asmout(p, o);
pc += o->size; pc += o->size;
} }
while(pc-INITTEXT < textsize) { while(pc-INITTEXT < textsize) {
cput(0); cput(0);
...@@ -1148,29 +1148,29 @@ asmthumbmap(void) ...@@ -1148,29 +1148,29 @@ asmthumbmap(void)
for(p = firstp; p != P; p = p->link){ for(p = firstp; p != P; p = p->link){
pc = p->pc - INITTEXT; pc = p->pc - INITTEXT;
if(p->as == ATEXT){ if(p->as == ATEXT){
setarch(p); setarch(p);
if(thumb){ if(thumb){
if(p->from.sym->foreign){ // 8 bytes of ARM first if(p->from.sym->foreign){ // 8 bytes of ARM first
if(lastt >= 0){
outt(lastt, pc-1);
lastt = -1;
}
pc += 8;
}
if(lastt < 0)
lastt = pc;
}
else{
if(p->from.sym->foreign){ // 4 bytes of THUMB first
if(lastt < 0)
lastt = pc;
pc += 4;
}
if(lastt >= 0){ if(lastt >= 0){
outt(lastt, pc-1); outt(lastt, pc-1);
lastt = -1; lastt = -1;
} }
pc += 8;
} }
if(lastt < 0)
lastt = pc;
}
else{
if(p->from.sym->foreign){ // 4 bytes of THUMB first
if(lastt < 0)
lastt = pc;
pc += 4;
}
if(lastt >= 0){
outt(lastt, pc-1);
lastt = -1;
}
}
} }
} }
if(lastt >= 0) if(lastt >= 0)
......
...@@ -151,88 +151,88 @@ noops(void) ...@@ -151,88 +151,88 @@ noops(void)
q = P; q = P;
for(p = firstp; p != P; p = p->link) { for(p = firstp; p != P; p = p->link) {
setarch(p); setarch(p);
/* find out how much arg space is used in this TEXT */ /* find out how much arg space is used in this TEXT */
if(p->to.type == D_OREG && p->to.reg == REGSP) if(p->to.type == D_OREG && p->to.reg == REGSP)
if(p->to.offset > curframe) if(p->to.offset > curframe)
curframe = p->to.offset; curframe = p->to.offset;
switch(p->as) { switch(p->as) {
case ATEXT: case ATEXT:
if(curtext && curtext->from.sym) { if(curtext && curtext->from.sym) {
curtext->from.sym->frame = curframe; curtext->from.sym->frame = curframe;
curtext->from.sym->become = curbecome; curtext->from.sym->become = curbecome;
if(curbecome > maxbecome) if(curbecome > maxbecome)
maxbecome = curbecome; maxbecome = curbecome;
}
curframe = 0;
curbecome = 0;
p->mark |= LEAF;
curtext = p;
break;
case ARET:
/* special form of RET is BECOME */
if(p->from.type == D_CONST)
if(p->from.offset > curbecome)
curbecome = p->from.offset;
break;
case ADIV:
case ADIVU:
case AMOD:
case AMODU:
q = p;
if(prog_div == P)
initdiv();
if(curtext != P)
curtext->mark &= ~LEAF;
setdiv(p->as);
continue;
case ANOP:
q1 = p->link;
q->link = q1; /* q is non-nop */
q1->mark |= p->mark;
continue;
case ABL:
case ABX:
if(curtext != P)
curtext->mark &= ~LEAF;
case ABCASE:
case AB:
case ABEQ:
case ABNE:
case ABCS:
case ABHS:
case ABCC:
case ABLO:
case ABMI:
case ABPL:
case ABVS:
case ABVC:
case ABHI:
case ABLS:
case ABGE:
case ABLT:
case ABGT:
case ABLE:
q1 = p->cond;
if(q1 != P) {
while(q1->as == ANOP) {
q1 = q1->link;
p->cond = q1;
} }
curframe = 0;
curbecome = 0;
p->mark |= LEAF;
curtext = p;
break;
case ARET:
/* special form of RET is BECOME */
if(p->from.type == D_CONST)
if(p->from.offset > curbecome)
curbecome = p->from.offset;
break;
case ADIV:
case ADIVU:
case AMOD:
case AMODU:
q = p;
if(prog_div == P)
initdiv();
if(curtext != P)
curtext->mark &= ~LEAF;
setdiv(p->as);
continue;
case ANOP:
q1 = p->link;
q->link = q1; /* q is non-nop */
q1->mark |= p->mark;
continue;
case ABL:
case ABX:
if(curtext != P)
curtext->mark &= ~LEAF;
case ABCASE:
case AB:
case ABEQ:
case ABNE:
case ABCS:
case ABHS:
case ABCC:
case ABLO:
case ABMI:
case ABPL:
case ABVS:
case ABVC:
case ABHI:
case ABLS:
case ABGE:
case ABLT:
case ABGT:
case ABLE:
q1 = p->cond;
if(q1 != P) {
while(q1->as == ANOP) {
q1 = q1->link;
p->cond = q1;
}
}
break;
} }
break; q = p;
}
q = p;
} }
if(curtext && curtext->from.sym) { if(curtext && curtext->from.sym) {
...@@ -248,660 +248,660 @@ noops(void) ...@@ -248,660 +248,660 @@ noops(void)
curtext = 0; curtext = 0;
for(p = firstp; p != P; p = p->link) { for(p = firstp; p != P; p = p->link) {
setarch(p); setarch(p);
switch(p->as) { switch(p->as) {
case ATEXT: case ATEXT:
curtext = p; curtext = p;
break; break;
case ABL: case ABL:
// case ABX: // case ABX:
if(curtext != P && curtext->from.sym != S && curtext->to.offset >= 0) { if(curtext != P && curtext->from.sym != S && curtext->to.offset >= 0) {
o = maxbecome - curtext->from.sym->frame; o = maxbecome - curtext->from.sym->frame;
if(o <= 0) if(o <= 0)
break; break;
/* calling a become or calling a variable */ /* calling a become or calling a variable */
if(p->to.sym == S || p->to.sym->become) { if(p->to.sym == S || p->to.sym->become) {
curtext->to.offset += o; curtext->to.offset += o;
if(debug['b']) { if(debug['b']) {
curp = p; curp = p;
print("%D calling %D increase %d\n", print("%D calling %D increase %d\n",
&curtext->from, &p->to, o); &curtext->from, &p->to, o);
}
} }
} }
break;
} }
break;
}
} }
for(p = firstp; p != P; p = p->link) { for(p = firstp; p != P; p = p->link) {
setarch(p); setarch(p);
o = p->as; o = p->as;
switch(o) { switch(o) {
case ATEXT: case ATEXT:
curtext = p; curtext = p;
autosize = p->to.offset + 4; autosize = p->to.offset + 4;
if(autosize <= 4) if(autosize <= 4)
if(curtext->mark & LEAF) { if(curtext->mark & LEAF) {
p->to.offset = -4; p->to.offset = -4;
autosize = 0; autosize = 0;
} }
if(!autosize && !(curtext->mark & LEAF)) { if(!autosize && !(curtext->mark & LEAF)) {
if(debug['v']) if(debug['v'])
Bprint(&bso, "save suppressed in: %s\n", Bprint(&bso, "save suppressed in: %s\n",
curtext->from.sym->name); curtext->from.sym->name);
Bflush(&bso); Bflush(&bso);
curtext->mark |= LEAF; curtext->mark |= LEAF;
} }
#ifdef CALLEEBX #ifdef CALLEEBX
if(p->from.sym->foreign){ if(p->from.sym->foreign){
if(thumb) if(thumb)
// don't allow literal pool to seperate these // don't allow literal pool to seperate these
p = adword(0xe28f7001, 0xe12fff17, p); // arm add 1, pc, r7 and bx r7 p = adword(0xe28f7001, 0xe12fff17, p); // arm add 1, pc, r7 and bx r7
// p = aword(0xe12fff17, aword(0xe28f7001, p)); // arm add 1, pc, r7 and bx r7 // p = aword(0xe12fff17, aword(0xe28f7001, p)); // arm add 1, pc, r7 and bx r7
else else
p = aword(0x4778, p); // thumb bx pc and 2 bytes padding p = aword(0x4778, p); // thumb bx pc and 2 bytes padding
} }
#endif #endif
if(curtext->mark & LEAF) { if(curtext->mark & LEAF) {
if(curtext->from.sym) if(curtext->from.sym)
curtext->from.sym->type = SLEAF; curtext->from.sym->type = SLEAF;
if(!autosize) if(!autosize)
break;
}
if(thumb){
if(!(p->reg & NOSPLIT))
diag("stack splitting not supported in thumb");
if(!(curtext->mark & LEAF)){
q = movrr(nil, REGLINK, REGTMPT-1, p);
p->link = q;
q1 = prg();
q1->as = AMOVW;
q1->line = p->line;
q1->from.type = D_REG;
q1->from.reg = REGTMPT-1;
q1->to.type = D_OREG;
q1->to.name = D_NONE;
q1->to.reg = REGSP;
q1->to.offset = 0;
q1->link = q->link;
q->link = q1;
}
if(autosize){
q2 = prg();
q2->as = ASUB;
q2->line = p->line;
q2->from.type = D_CONST;
q2->from.offset = autosize;
q2->to.type = D_REG;
q2->to.reg = REGSP;
q2->link = p->link;
p->link = q2;
}
break; break;
} }
if(thumb){ if(p->reg & NOSPLIT) {
if(!(p->reg & NOSPLIT))
diag("stack splitting not supported in thumb");
if(!(curtext->mark & LEAF)){
q = movrr(nil, REGLINK, REGTMPT-1, p);
p->link = q;
q1 = prg(); q1 = prg();
q1->as = AMOVW; q1->as = AMOVW;
q1->scond |= C_WBIT;
q1->line = p->line; q1->line = p->line;
q1->from.type = D_REG; q1->from.type = D_REG;
q1->from.reg = REGTMPT-1; q1->from.reg = REGLINK;
q1->to.type = D_OREG; q1->to.type = D_OREG;
q1->to.name = D_NONE; q1->to.offset = -autosize;
q1->to.reg = REGSP; q1->to.reg = REGSP;
q1->to.offset = 0; q1->link = p->link;
q1->link = q->link; p->link = q1;
q->link = q1; } else if (autosize < StackBig) {
} // split stack check for small functions
if(autosize){ // MOVW g_stackguard(g), R1
q2 = prg(); // CMP R1, $-autosize(SP)
q2->as = ASUB; // MOVW.LO $autosize, R1
q2->line = p->line; // MOVW.LO $args, R2
q2->from.type = D_CONST; // MOVW.LO R14, R3
q2->from.offset = autosize; // BL.LO runtime.morestack(SB) // modifies LR
q2->to.type = D_REG; // MOVW.W R14,$-autosize(SP)
q2->to.reg = REGSP;
q2->link = p->link; // TODO(kaib): add more trampolines
p->link = q2; // TODO(kaib): put stackguard in register
// TODO(kaib): add support for -K and underflow detection
// MOVW g_stackguard(g), R1
p = appendp(p);
p->as = AMOVW;
p->from.type = D_OREG;
p->from.reg = REGG;
p->to.type = D_REG;
p->to.reg = 1;
// CMP R1, $-autosize(SP)
p = appendp(p);
p->as = ACMP;
p->from.type = D_REG;
p->from.reg = 1;
p->from.offset = -autosize;
p->reg = REGSP;
// MOVW.LO $autosize, R1
p = appendp(p);
p->as = AMOVW;
p->scond = C_SCOND_LO;
p->from.type = D_CONST;
p->from.offset = 0;
p->to.type = D_REG;
p->to.reg = 1;
// MOVW.LO $args +4, R2
// also need to store the extra 4 bytes.
p = appendp(p);
p->as = AMOVW;
p->scond = C_SCOND_LO;
p->from.type = D_CONST;
p->from.offset = ((curtext->to.offset2 + 3) & ~3) + 4;
p->to.type = D_REG;
p->to.reg = 2;
// MOVW.LO R14, R3
p = appendp(p);
p->as = AMOVW;
p->scond = C_SCOND_LO;
p->from.type = D_REG;
p->from.reg = REGLINK;
p->to.type = D_REG;
p->to.reg = 3;
// BL.LO runtime.morestack(SB) // modifies LR
p = appendp(p);
p->as = ABL;
p->scond = C_SCOND_LO;
p->to.type = D_BRANCH;
p->to.sym = symmorestack;
p->cond = pmorestack;
// MOVW.W R14,$-autosize(SP)
p = appendp(p);
p->as = AMOVW;
p->scond |= C_WBIT;
p->from.type = D_REG;
p->from.reg = REGLINK;
p->to.type = D_OREG;
p->to.offset = -autosize;
p->to.reg = REGSP;
} else { // > StackBig
// MOVW $autosize, R1
// MOVW $args, R2
// MOVW R14, R3
// BL runtime.morestack(SB) // modifies LR
// MOVW.W R14,$-autosize(SP)
// MOVW $autosize, R1
p = appendp(p);
p->as = AMOVW;
p->from.type = D_CONST;
p->from.offset = autosize;
p->to.type = D_REG;
p->to.reg = 1;
// MOVW $args +4, R2
// also need to store the extra 4 bytes.
p = appendp(p);
p->as = AMOVW;
p->from.type = D_CONST;
p->from.offset = ((curtext->to.offset2 + 3) & ~3) + 4;
p->to.type = D_REG;
p->to.reg = 2;
// MOVW R14, R3
p = appendp(p);
p->as = AMOVW;
p->from.type = D_REG;
p->from.reg = REGLINK;
p->to.type = D_REG;
p->to.reg = 3;
// BL runtime.morestack(SB) // modifies LR
p = appendp(p);
p->as = ABL;
p->to.type = D_BRANCH;
p->to.sym = symmorestack;
p->cond = pmorestack;
// MOVW.W R14,$-autosize(SP)
p = appendp(p);
p->as = AMOVW;
p->scond |= C_WBIT;
p->from.type = D_REG;
p->from.reg = REGLINK;
p->to.type = D_OREG;
p->to.offset = -autosize;
p->to.reg = REGSP;
} }
break; break;
}
case ARET:
if(p->reg & NOSPLIT) { nocache(p);
q1 = prg(); foreign = seenthumb && curtext->from.sym != S && (curtext->from.sym->foreign || curtext->from.sym->fnptr);
q1->as = AMOVW;
q1->scond |= C_WBIT;
q1->line = p->line;
q1->from.type = D_REG;
q1->from.reg = REGLINK;
q1->to.type = D_OREG;
q1->to.offset = -autosize;
q1->to.reg = REGSP;
q1->link = p->link;
p->link = q1;
} else if (autosize < StackBig) {
// split stack check for small functions
// MOVW g_stackguard(g), R1
// CMP R1, $-autosize(SP)
// MOVW.LO $autosize, R1
// MOVW.LO $args, R2
// MOVW.LO R14, R3
// BL.LO runtime.morestack(SB) // modifies LR
// MOVW.W R14,$-autosize(SP)
// TODO(kaib): add more trampolines
// TODO(kaib): put stackguard in register
// TODO(kaib): add support for -K and underflow detection
// MOVW g_stackguard(g), R1
p = appendp(p);
p->as = AMOVW;
p->from.type = D_OREG;
p->from.reg = REGG;
p->to.type = D_REG;
p->to.reg = 1;
// CMP R1, $-autosize(SP)
p = appendp(p);
p->as = ACMP;
p->from.type = D_REG;
p->from.reg = 1;
p->from.offset = -autosize;
p->reg = REGSP;
// MOVW.LO $autosize, R1
p = appendp(p);
p->as = AMOVW;
p->scond = C_SCOND_LO;
p->from.type = D_CONST;
p->from.offset = 0;
p->to.type = D_REG;
p->to.reg = 1;
// MOVW.LO $args +4, R2
// also need to store the extra 4 bytes.
p = appendp(p);
p->as = AMOVW;
p->scond = C_SCOND_LO;
p->from.type = D_CONST;
p->from.offset = ((curtext->to.offset2 + 3) & ~3) + 4;
p->to.type = D_REG;
p->to.reg = 2;
// MOVW.LO R14, R3
p = appendp(p);
p->as = AMOVW;
p->scond = C_SCOND_LO;
p->from.type = D_REG;
p->from.reg = REGLINK;
p->to.type = D_REG;
p->to.reg = 3;
// BL.LO runtime.morestack(SB) // modifies LR
p = appendp(p);
p->as = ABL;
p->scond = C_SCOND_LO;
p->to.type = D_BRANCH;
p->to.sym = symmorestack;
p->cond = pmorestack;
// MOVW.W R14,$-autosize(SP)
p = appendp(p);
p->as = AMOVW;
p->scond |= C_WBIT;
p->from.type = D_REG;
p->from.reg = REGLINK;
p->to.type = D_OREG;
p->to.offset = -autosize;
p->to.reg = REGSP;
} else { // > StackBig
// MOVW $autosize, R1
// MOVW $args, R2
// MOVW R14, R3
// BL runtime.morestack(SB) // modifies LR
// MOVW.W R14,$-autosize(SP)
// MOVW $autosize, R1
p = appendp(p);
p->as = AMOVW;
p->from.type = D_CONST;
p->from.offset = autosize;
p->to.type = D_REG;
p->to.reg = 1;
// MOVW $args +4, R2
// also need to store the extra 4 bytes.
p = appendp(p);
p->as = AMOVW;
p->from.type = D_CONST;
p->from.offset = ((curtext->to.offset2 + 3) & ~3) + 4;
p->to.type = D_REG;
p->to.reg = 2;
// MOVW R14, R3
p = appendp(p);
p->as = AMOVW;
p->from.type = D_REG;
p->from.reg = REGLINK;
p->to.type = D_REG;
p->to.reg = 3;
// BL runtime.morestack(SB) // modifies LR
p = appendp(p);
p->as = ABL;
p->to.type = D_BRANCH;
p->to.sym = symmorestack;
p->cond = pmorestack;
// MOVW.W R14,$-autosize(SP)
p = appendp(p);
p->as = AMOVW;
p->scond |= C_WBIT;
p->from.type = D_REG;
p->from.reg = REGLINK;
p->to.type = D_OREG;
p->to.offset = -autosize;
p->to.reg = REGSP;
}
break;
case ARET:
nocache(p);
foreign = seenthumb && curtext->from.sym != S && (curtext->from.sym->foreign || curtext->from.sym->fnptr);
// print("%s %d %d\n", curtext->from.sym->name, curtext->from.sym->foreign, curtext->from.sym->fnptr); // print("%s %d %d\n", curtext->from.sym->name, curtext->from.sym->foreign, curtext->from.sym->fnptr);
if(p->from.type == D_CONST) if(p->from.type == D_CONST)
goto become; goto become;
if(curtext->mark & LEAF) { if(curtext->mark & LEAF) {
if(!autosize) { if(!autosize) {
if(thumb){ if(thumb){
p = fnret(p, REGLINK, foreign, p); p = fnret(p, REGLINK, foreign, p);
break;
}
// if(foreign) print("ABXRET 1 %s\n", curtext->from.sym->name);
p->as = foreign ? ABXRET : AB;
p->from = zprg.from;
p->to.type = D_OREG;
p->to.offset = 0;
p->to.reg = REGLINK;
break; break;
} }
// if(foreign) print("ABXRET 1 %s\n", curtext->from.sym->name);
p->as = foreign ? ABXRET : AB;
p->from = zprg.from;
p->to.type = D_OREG;
p->to.offset = 0;
p->to.reg = REGLINK;
break;
} }
} if(thumb){
if(thumb){ if(curtext->mark & LEAF){
if(curtext->mark & LEAF){ if(autosize){
if(autosize){ p->as = AADD;
p->as = AADD; p->from.type = D_CONST;
p->from.type = D_CONST; p->from.offset = autosize;
p->from.offset = autosize; p->to.type = D_REG;
p->to.reg = REGSP;
q = nil;
}
else
q = p;
q = fnret(q, REGLINK, foreign, p);
if(q != p)
p->link = q;
}
else{
p->as = AMOVW;
p->from.type = D_OREG;
p->from.name = D_NONE;
p->from.reg = REGSP;
p->from.offset = 0;
p->to.type = D_REG; p->to.type = D_REG;
p->to.reg = REGSP; p->to.reg = REGTMPT-1;
q = nil; if(autosize){
q = prg();
q->as = AADD;
q->from.type = D_CONST;
q->from.offset = autosize;
q->to.type = D_REG;
q->to.reg = REGSP;
q->link = p->link;
p->link = q;
}
else
q = p;
q1 = fnret(nil, REGTMPT-1, foreign, p);
q1->link = q->link;
q->link = q1;
} }
else break;
q = p;
q = fnret(q, REGLINK, foreign, p);
if(q != p)
p->link = q;
} }
else{ if(foreign) {
// if(foreign) print("ABXRET 3 %s\n", curtext->from.sym->name);
#define R 1
p->as = AMOVW; p->as = AMOVW;
p->from.type = D_OREG; p->from.type = D_OREG;
p->from.name = D_NONE; p->from.name = D_NONE;
p->from.reg = REGSP; p->from.reg = REGSP;
p->from.offset = 0; p->from.offset = 0;
p->to.type = D_REG; p->to.type = D_REG;
p->to.reg = REGTMPT-1; p->to.reg = R;
if(autosize){ q = prg();
q = prg(); q->as = AADD;
q->as = AADD; q->scond = p->scond;
q->from.type = D_CONST; q->line = p->line;
q->from.offset = autosize; q->from.type = D_CONST;
q->to.type = D_REG; q->from.offset = autosize;
q->to.reg = REGSP; q->to.type = D_REG;
q->link = p->link; q->to.reg = REGSP;
p->link = q; q->link = p->link;
} p->link = q;
else q1 = prg();
q = p; q1->as = ABXRET;
q1 = fnret(nil, REGTMPT-1, foreign, p); q1->scond = p->scond;
q1->line = p->line;
q1->to.type = D_OREG;
q1->to.offset = 0;
q1->to.reg = R;
q1->link = q->link; q1->link = q->link;
q->link = q1; q->link = q1;
#undef R
}
else {
p->as = AMOVW;
p->scond |= C_PBIT;
p->from.type = D_OREG;
p->from.offset = autosize;
p->from.reg = REGSP;
p->to.type = D_REG;
p->to.reg = REGPC;
} }
break; break;
}
if(foreign) { become:
// if(foreign) print("ABXRET 3 %s\n", curtext->from.sym->name); if(foreign){
#define R 1 diag("foreign become - help");
p->as = AMOVW; break;
p->from.type = D_OREG; }
p->from.name = D_NONE; if(thumb){
p->from.reg = REGSP; diag("thumb become - help");
p->from.offset = 0; break;
p->to.type = D_REG; }
p->to.reg = R; print("arm become\n");
if(curtext->mark & LEAF) {
if(!autosize) {
p->as = AB;
p->from = zprg.from;
break;
}
}
q = prg(); q = prg();
q->as = AADD;
q->scond = p->scond; q->scond = p->scond;
q->line = p->line; q->line = p->line;
q->from.type = D_CONST; q->as = AB;
q->from.offset = autosize; q->from = zprg.from;
q->to.type = D_REG; q->to = p->to;
q->to.reg = REGSP; q->cond = p->cond;
q->link = p->link; q->link = p->link;
p->link = q; p->link = q;
q1 = prg(); if(thumb){
q1->as = ABXRET; q1 = prg();
q1->scond = p->scond; q1->line = p->line;
q1->line = p->line; q1->as = AADD;
q1->to.type = D_OREG; q1->from.type = D_CONST;
q1->to.offset = 0; q1->from.offset = autosize;
q1->to.reg = R; q1->to.type = D_REG;
q1->link = q->link; q1->to.reg = REGSP;
q->link = q1; p->as = AMOVW;
#undef R p->line = p->line;
} p->from.type = D_OREG;
else { p->from.name = D_NONE;
p->from.reg = REGSP;
p->from.offset = 0;
p->to.type = D_REG;
p->to.reg = REGTMPT-1;
q1->link = q;
p->link = q1;
q2 = movrr(nil, REGTMPT-1, REGLINK, p);
q2->link = q;
q1->link = q2;
break;
}
p->as = AMOVW; p->as = AMOVW;
p->scond |= C_PBIT; p->scond |= C_PBIT;
p->from = zprg.from;
p->from.type = D_OREG; p->from.type = D_OREG;
p->from.offset = autosize; p->from.offset = autosize;
p->from.reg = REGSP; p->from.reg = REGSP;
p->to = zprg.to;
p->to.type = D_REG; p->to.type = D_REG;
p->to.reg = REGPC; p->to.reg = REGLINK;
}
break;
become:
if(foreign){
diag("foreign become - help");
break;
}
if(thumb){
diag("thumb become - help");
break; break;
}
print("arm become\n"); case ADIV:
if(curtext->mark & LEAF) { case ADIVU:
case AMOD:
if(!autosize) { case AMODU:
p->as = AB; if(debug['M'])
p->from = zprg.from;
break; break;
} if(p->from.type != D_REG)
} break;
q = prg(); if(p->to.type != D_REG)
q->scond = p->scond; break;
q->line = p->line; q1 = p;
q->as = AB;
q->from = zprg.from; /* MOV a,4(SP) */
q->to = p->to; q = prg();
q->cond = p->cond; q->link = p->link;
q->link = p->link; p->link = q;
p->link = q; p = q;
if(thumb){
q1 = prg();
q1->line = p->line;
q1->as = AADD;
q1->from.type = D_CONST;
q1->from.offset = autosize;
q1->to.type = D_REG;
q1->to.reg = REGSP;
p->as = AMOVW; p->as = AMOVW;
p->line = p->line; p->line = q1->line;
p->from.type = D_OREG; p->from.type = D_REG;
p->from.name = D_NONE; p->from.reg = q1->from.reg;
p->from.reg = REGSP; p->to.type = D_OREG;
p->from.offset = 0; p->to.reg = REGSP;
p->to.offset = 4;
/* MOV b,REGTMP */
q = prg();
q->link = p->link;
p->link = q;
p = q;
p->as = AMOVW;
p->line = q1->line;
p->from.type = D_REG;
p->from.reg = q1->reg;
if(q1->reg == NREG)
p->from.reg = q1->to.reg;
p->to.type = D_REG; p->to.type = D_REG;
p->to.reg = REGTMPT-1; p->to.reg = prog_div != UP && prog_div->from.sym->thumb ? REGTMPT : REGTMP;
q1->link = q; p->to.offset = 0;
p->link = q1;
q2 = movrr(nil, REGTMPT-1, REGLINK, p); /* CALL appropriate */
q2->link = q; q = prg();
q1->link = q2; q->link = p->link;
break; p->link = q;
} p = q;
p->as = AMOVW;
p->scond |= C_PBIT;
p->from = zprg.from;
p->from.type = D_OREG;
p->from.offset = autosize;
p->from.reg = REGSP;
p->to = zprg.to;
p->to.type = D_REG;
p->to.reg = REGLINK;
break;
case ADIV:
case ADIVU:
case AMOD:
case AMODU:
if(debug['M'])
break;
if(p->from.type != D_REG)
break;
if(p->to.type != D_REG)
break;
q1 = p;
/* MOV a,4(SP) */
q = prg();
q->link = p->link;
p->link = q;
p = q;
p->as = AMOVW;
p->line = q1->line;
p->from.type = D_REG;
p->from.reg = q1->from.reg;
p->to.type = D_OREG;
p->to.reg = REGSP;
p->to.offset = 4;
/* MOV b,REGTMP */
q = prg();
q->link = p->link;
p->link = q;
p = q;
p->as = AMOVW;
p->line = q1->line;
p->from.type = D_REG;
p->from.reg = q1->reg;
if(q1->reg == NREG)
p->from.reg = q1->to.reg;
p->to.type = D_REG;
p->to.reg = prog_div != UP && prog_div->from.sym->thumb ? REGTMPT : REGTMP;
p->to.offset = 0;
/* CALL appropriate */
q = prg();
q->link = p->link;
p->link = q;
p = q;
#ifdef CALLEEBX #ifdef CALLEEBX
p->as = ABL; p->as = ABL;
#else #else
if(prog_div != UP && prog_div->from.sym->thumb) if(prog_div != UP && prog_div->from.sym->thumb)
p->as = thumb ? ABL : ABX; p->as = thumb ? ABL : ABX;
else else
p->as = thumb ? ABX : ABL; p->as = thumb ? ABX : ABL;
#endif #endif
p->line = q1->line; p->line = q1->line;
p->to.type = D_BRANCH; p->to.type = D_BRANCH;
p->cond = p; p->cond = p;
switch(o) { switch(o) {
case ADIV: case ADIV:
p->cond = prog_div; p->cond = prog_div;
p->to.sym = sym_div; p->to.sym = sym_div;
break; break;
case ADIVU: case ADIVU:
p->cond = prog_divu; p->cond = prog_divu;
p->to.sym = sym_divu; p->to.sym = sym_divu;
break; break;
case AMOD: case AMOD:
p->cond = prog_mod; p->cond = prog_mod;
p->to.sym = sym_mod; p->to.sym = sym_mod;
break;
case AMODU:
p->cond = prog_modu;
p->to.sym = sym_modu;
break;
}
/* MOV REGTMP, b */
q = prg();
q->link = p->link;
p->link = q;
p = q;
p->as = AMOVW;
p->line = q1->line;
p->from.type = D_REG;
p->from.reg = prog_div != UP && prog_div->from.sym->thumb ? REGTMPT : REGTMP;
p->from.offset = 0;
p->to.type = D_REG;
p->to.reg = q1->to.reg;
/* ADD $8,SP */
q = prg();
q->link = p->link;
p->link = q;
p = q;
p->as = AADD;
p->from.type = D_CONST;
p->from.reg = NREG;
p->from.offset = 8;
p->reg = NREG;
p->to.type = D_REG;
p->to.reg = REGSP;
/* SUB $8,SP */
q1->as = ASUB;
q1->from.type = D_CONST;
q1->from.offset = 8;
q1->from.reg = NREG;
q1->reg = NREG;
q1->to.type = D_REG;
q1->to.reg = REGSP;
break; break;
case AMODU: case AMOVW:
p->cond = prog_modu; if(thumb){
p->to.sym = sym_modu; Adr *a = &p->from;
if(a->type == D_CONST && ((a->name == D_NONE && a->reg == REGSP) || a->name == D_AUTO || a->name == D_PARAM) && (a->offset & 3))
diag("SP offset not multiple of 4");
}
break; break;
} case AMOVB:
case AMOVBU:
/* MOV REGTMP, b */ case AMOVH:
q = prg(); case AMOVHU:
q->link = p->link; if(thumb){
p->link = q; if(p->from.type == D_OREG && (p->from.name == D_AUTO || p->from.name == D_PARAM || (p->from.name == D_CONST && p->from.reg == REGSP))){
p = q; q = prg();
*q = *p;
p->as = AMOVW; if(p->from.name == D_AUTO)
p->line = q1->line; q->from.offset += autosize;
p->from.type = D_REG; else if(p->from.name == D_PARAM)
p->from.reg = prog_div != UP && prog_div->from.sym->thumb ? REGTMPT : REGTMP; q->from.offset += autosize+4;
p->from.offset = 0; q->from.name = D_NONE;
p->to.type = D_REG; q->from.reg = REGTMPT;
p->to.reg = q1->to.reg; p = movrr(p, REGSP, REGTMPT, p);
q->link = p->link;
/* ADD $8,SP */ p->link = q;
q = prg(); }
q->link = p->link; if(p->to.type == D_OREG && (p->to.name == D_AUTO || p->to.name == D_PARAM || (p->to.name == D_CONST && p->to.reg == REGSP))){
p->link = q; q = prg();
p = q; *q = *p;
if(p->to.name == D_AUTO)
p->as = AADD; q->to.offset += autosize;
p->from.type = D_CONST; else if(p->to.name == D_PARAM)
p->from.reg = NREG; q->to.offset += autosize+4;
p->from.offset = 8; q->to.name = D_NONE;
p->reg = NREG; q->to.reg = REGTMPT;
p->to.type = D_REG; p = movrr(p, REGSP, REGTMPT, p);
p->to.reg = REGSP; q->link = p->link;
p->link = q;
/* SUB $8,SP */ if(q->to.offset < 0 || q->to.offset > 255){ // complicated
q1->as = ASUB; p->to.reg = REGTMPT+1; // mov sp, r8
q1->from.type = D_CONST; q1 = prg();
q1->from.offset = 8; q1->line = p->line;
q1->from.reg = NREG; q1->as = AMOVW;
q1->reg = NREG; q1->from.type = D_CONST;
q1->to.type = D_REG; q1->from.offset = q->to.offset;
q1->to.reg = REGSP; q1->to.type = D_REG;
q1->to.reg = REGTMPT; // mov $o, r7
break; p->link = q1;
case AMOVW: q1->link = q;
if(thumb){ q1 = prg();
Adr *a = &p->from; q1->line = p->line;
q1->as = AADD;
if(a->type == D_CONST && ((a->name == D_NONE && a->reg == REGSP) || a->name == D_AUTO || a->name == D_PARAM) && (a->offset & 3)) q1->from.type = D_REG;
diag("SP offset not multiple of 4"); q1->from.reg = REGTMPT+1;
} q1->to.type = D_REG;
break; q1->to.reg = REGTMPT; // add r8, r7
case AMOVB: p->link->link = q1;
case AMOVBU: q1->link = q;
case AMOVH: q->to.offset = 0; // mov* r, 0(r7)
case AMOVHU: /* phew */
if(thumb){ }
if(p->from.type == D_OREG && (p->from.name == D_AUTO || p->from.name == D_PARAM || (p->from.name == D_CONST && p->from.reg == REGSP))){ }
q = prg();
*q = *p;
if(p->from.name == D_AUTO)
q->from.offset += autosize;
else if(p->from.name == D_PARAM)
q->from.offset += autosize+4;
q->from.name = D_NONE;
q->from.reg = REGTMPT;
p = movrr(p, REGSP, REGTMPT, p);
q->link = p->link;
p->link = q;
} }
if(p->to.type == D_OREG && (p->to.name == D_AUTO || p->to.name == D_PARAM || (p->to.name == D_CONST && p->to.reg == REGSP))){ break;
q = prg(); case AMOVM:
*q = *p; if(thumb){
if(p->to.name == D_AUTO) if(p->from.type == D_OREG){
q->to.offset += autosize; if(p->from.offset == 0)
else if(p->to.name == D_PARAM) p->from.type = D_REG;
q->to.offset += autosize+4; else
q->to.name = D_NONE; diag("non-zero AMOVM offset");
q->to.reg = REGTMPT; }
p = movrr(p, REGSP, REGTMPT, p); else if(p->to.type == D_OREG){
q->link = p->link; if(p->to.offset == 0)
p->link = q; p->to.type = D_REG;
if(q->to.offset < 0 || q->to.offset > 255){ // complicated else
p->to.reg = REGTMPT+1; // mov sp, r8 diag("non-zero AMOVM offset");
q1 = prg();
q1->line = p->line;
q1->as = AMOVW;
q1->from.type = D_CONST;
q1->from.offset = q->to.offset;
q1->to.type = D_REG;
q1->to.reg = REGTMPT; // mov $o, r7
p->link = q1;
q1->link = q;
q1 = prg();
q1->line = p->line;
q1->as = AADD;
q1->from.type = D_REG;
q1->from.reg = REGTMPT+1;
q1->to.type = D_REG;
q1->to.reg = REGTMPT; // add r8, r7
p->link->link = q1;
q1->link = q;
q->to.offset = 0; // mov* r, 0(r7)
/* phew */
} }
} }
} break;
break; case AB:
case AMOVM: if(thumb && p->to.type == D_OREG){
if(thumb){ if(p->to.offset == 0){
if(p->from.type == D_OREG){ p->as = AMOVW;
if(p->from.offset == 0)
p->from.type = D_REG; p->from.type = D_REG;
else p->from.reg = p->to.reg;
diag("non-zero AMOVM offset");
}
else if(p->to.type == D_OREG){
if(p->to.offset == 0)
p->to.type = D_REG; p->to.type = D_REG;
else p->to.reg = REGPC;
diag("non-zero AMOVM offset"); }
} else{
} p->as = AADD;
break; p->from.type = D_CONST;
case AB: p->from.offset = p->to.offset;
if(thumb && p->to.type == D_OREG){ p->reg = p->to.reg;
if(p->to.offset == 0){ p->to.type = D_REG;
p->as = AMOVW; p->to.reg = REGTMPT-1;
p->from.type = D_REG; q = prg();
p->from.reg = p->to.reg; q->as = AMOVW;
p->to.type = D_REG; q->line = p->line;
p->to.reg = REGPC; q->from.type = D_REG;
q->from.reg = REGTMPT-1;
q->to.type = D_REG;
q->to.reg = REGPC;
q->link = p->link;
p->link = q;
}
} }
else{ if(seenthumb && !thumb && p->to.type == D_OREG && p->to.reg == REGLINK){
p->as = AADD; // print("warn %s: b (R%d) assuming a return\n", curtext->from.sym->name, p->to.reg);
p->from.type = D_CONST; p->as = ABXRET;
p->from.offset = p->to.offset;
p->reg = p->to.reg;
p->to.type = D_REG;
p->to.reg = REGTMPT-1;
q = prg();
q->as = AMOVW;
q->line = p->line;
q->from.type = D_REG;
q->from.reg = REGTMPT-1;
q->to.type = D_REG;
q->to.reg = REGPC;
q->link = p->link;
p->link = q;
} }
} break;
if(seenthumb && !thumb && p->to.type == D_OREG && p->to.reg == REGLINK){ case ABL:
// print("warn %s: b (R%d) assuming a return\n", curtext->from.sym->name, p->to.reg); case ABX:
p->as = ABXRET; if(thumb && p->to.type == D_OREG){
} if(p->to.offset == 0){
break; p->as = o;
case ABL: p->from.type = D_NONE;
case ABX: p->to.type = D_REG;
if(thumb && p->to.type == D_OREG){ }
if(p->to.offset == 0){ else{
p->as = o; p->as = AADD;
p->from.type = D_NONE; p->from.type = D_CONST;
p->to.type = D_REG; p->from.offset = p->to.offset;
} p->reg = p->to.reg;
else{ p->to.type = D_REG;
p->as = AADD; p->to.reg = REGTMPT-1;
p->from.type = D_CONST; q = prg();
p->from.offset = p->to.offset; q->as = o;
p->reg = p->to.reg; q->line = p->line;
p->to.type = D_REG; q->from.type = D_NONE;
p->to.reg = REGTMPT-1; q->to.type = D_REG;
q = prg(); q->to.reg = REGTMPT-1;
q->as = o; q->link = p->link;
q->line = p->line; p->link = q;
q->from.type = D_NONE; }
q->to.type = D_REG;
q->to.reg = REGTMPT-1;
q->link = p->link;
p->link = q;
} }
break;
} }
break;
}
} }
} }
......
...@@ -359,80 +359,80 @@ patch(void) ...@@ -359,80 +359,80 @@ patch(void)
s = lookup("exit", 0); s = lookup("exit", 0);
vexit = s->value; vexit = s->value;
for(p = firstp; p != P; p = p->link) { for(p = firstp; p != P; p = p->link) {
setarch(p); setarch(p);
a = p->as; a = p->as;
if(a == ATEXT) if(a == ATEXT)
curtext = p; curtext = p;
if(seenthumb && a == ABL){ if(seenthumb && a == ABL){
// if((s = p->to.sym) != S && (s1 = curtext->from.sym) != S) // if((s = p->to.sym) != S && (s1 = curtext->from.sym) != S)
// print("%s calls %s\n", s1->name, s->name); // print("%s calls %s\n", s1->name, s->name);
if((s = p->to.sym) != S && (s1 = curtext->from.sym) != S && s->thumb != s1->thumb) if((s = p->to.sym) != S && (s1 = curtext->from.sym) != S && s->thumb != s1->thumb)
s->foreign = 1; s->foreign = 1;
}
if((a == ABL || a == ABX || a == AB || a == ARET) &&
p->to.type != D_BRANCH && p->to.sym != S) {
s = p->to.sym;
switch(s->type) {
default:
diag("undefined: %s", s->name);
s->type = STEXT;
s->value = vexit;
continue; // avoid more error messages
case STEXT:
p->to.offset = s->value;
p->to.type = D_BRANCH;
break;
case SUNDEF:
if(p->as != ABL)
diag("help: SUNDEF in AB || ARET");
p->to.offset = 0;
p->to.type = D_BRANCH;
p->cond = UP;
break;
} }
} if((a == ABL || a == ABX || a == AB || a == ARET) &&
if(p->to.type != D_BRANCH || p->cond == UP) p->to.type != D_BRANCH && p->to.sym != S) {
continue; s = p->to.sym;
c = p->to.offset; switch(s->type) {
for(q = firstp; q != P;) { default:
if(q->forwd != P) diag("undefined: %s", s->name);
if(c >= q->forwd->pc) { s->type = STEXT;
q = q->forwd; s->value = vexit;
continue; // avoid more error messages
case STEXT:
p->to.offset = s->value;
p->to.type = D_BRANCH;
break;
case SUNDEF:
if(p->as != ABL)
diag("help: SUNDEF in AB || ARET");
p->to.offset = 0;
p->to.type = D_BRANCH;
p->cond = UP;
break;
}
}
if(p->to.type != D_BRANCH || p->cond == UP)
continue; continue;
c = p->to.offset;
for(q = firstp; q != P;) {
if(q->forwd != P)
if(c >= q->forwd->pc) {
q = q->forwd;
continue;
}
if(c == q->pc)
break;
q = q->link;
} }
if(c == q->pc) if(q == P) {
break; diag("branch out of range %ld\n%P", c, p);
q = q->link; p->to.type = D_NONE;
} }
if(q == P) { p->cond = q;
diag("branch out of range %ld\n%P", c, p);
p->to.type = D_NONE;
}
p->cond = q;
} }
for(p = firstp; p != P; p = p->link) { for(p = firstp; p != P; p = p->link) {
setarch(p); setarch(p);
a = p->as; a = p->as;
if(p->as == ATEXT) if(p->as == ATEXT)
curtext = p; curtext = p;
if(seenthumb && a == ABL) { if(seenthumb && a == ABL) {
#ifdef CALLEEBX #ifdef CALLEEBX
if(0) if(0)
{} {}
#else #else
if((s = p->to.sym) != S && (s->foreign || s->fnptr)) if((s = p->to.sym) != S && (s->foreign || s->fnptr))
p->as = ABX; p->as = ABX;
#endif #endif
else if(p->to.type == D_OREG) else if(p->to.type == D_OREG)
p->as = ABX; p->as = ABX;
} }
if(p->cond != P && p->cond != UP) { if(p->cond != P && p->cond != UP) {
p->cond = brloop(p->cond); p->cond = brloop(p->cond);
if(p->cond != P) if(p->cond != P)
if(p->to.type == D_BRANCH) if(p->to.type == D_BRANCH)
p->to.offset = p->cond->pc; p->to.offset = p->cond->pc;
} }
} }
} }
......
...@@ -27,45 +27,45 @@ softfloat() ...@@ -27,45 +27,45 @@ softfloat()
wasfloat = 0; wasfloat = 0;
p = firstp; p = firstp;
for(p = firstp; p != P; p = p->link) { for(p = firstp; p != P; p = p->link) {
switch(p->as) { switch(p->as) {
case AMOVWD: case AMOVWD:
case AMOVWF: case AMOVWF:
case AMOVDW: case AMOVDW:
case AMOVFW: case AMOVFW:
case AMOVFD: case AMOVFD:
case AMOVDF: case AMOVDF:
case AMOVF: case AMOVF:
case AMOVD: case AMOVD:
case ACMPF: case ACMPF:
case ACMPD: case ACMPD:
case AADDF: case AADDF:
case AADDD: case AADDD:
case ASUBF: case ASUBF:
case ASUBD: case ASUBD:
case AMULF: case AMULF:
case AMULD: case AMULD:
case ADIVF: case ADIVF:
case ADIVD: case ADIVD:
if (psfloat == P) if (psfloat == P)
diag("floats used with _sfloat not defined"); diag("floats used with _sfloat not defined");
if (!wasfloat) { if (!wasfloat) {
next = prg(); next = prg();
*next = *p; *next = *p;
// BL _sfloat(SB) // BL _sfloat(SB)
*p = zprg; *p = zprg;
p->link = next; p->link = next;
p->as = ABL; p->as = ABL;
p->to.type = D_BRANCH; p->to.type = D_BRANCH;
p->to.sym = symsfloat; p->to.sym = symsfloat;
p->cond = psfloat; p->cond = psfloat;
p = next; p = next;
wasfloat = 1; wasfloat = 1;
}
break;
default:
wasfloat = 0;
} }
break;
default:
wasfloat = 0;
}
} }
} }
...@@ -178,59 +178,59 @@ span(void) ...@@ -178,59 +178,59 @@ span(void)
op = nil; op = nil;
otxt = c; otxt = c;
for(p = firstp; p != P; op = p, p = p->link) { for(p = firstp; p != P; op = p, p = p->link) {
setarch(p); setarch(p);
p->pc = c; p->pc = c;
o = oplook(p); o = oplook(p);
m = o->size; m = o->size;
// must check literal pool here in case p generates many instructions // must check literal pool here in case p generates many instructions
if(blitrl){ if(blitrl){
if(thumb && isbranch(p)) if(thumb && isbranch(p))
pool.extra += brextra(p);
if(checkpool(op, p->as == ACASE ? casesz(p) : m))
c = p->pc = scan(op, p, c);
}
if(m == 0) {
if(p->as == ATEXT) {
if(blitrl && lastthumb != -1 && lastthumb != thumb){ // flush literal pool
if(flushpool(op, 0, 1))
c = p->pc = scan(op, p, c);
}
lastthumb = thumb;
curtext = p;
autosize = p->to.offset + 4;
if(p->from.sym != S)
p->from.sym->value = c;
/* need passes to resolve branches */
if(c-otxt >= 1L<<17)
bflag = 1;
otxt = c;
if(thumb && blitrl)
pool.extra += brextra(p); pool.extra += brextra(p);
if(checkpool(op, p->as == ACASE ? casesz(p) : m))
c = p->pc = scan(op, p, c);
}
if(m == 0) {
if(p->as == ATEXT) {
if(blitrl && lastthumb != -1 && lastthumb != thumb){ // flush literal pool
if(flushpool(op, 0, 1))
c = p->pc = scan(op, p, c);
}
lastthumb = thumb;
curtext = p;
autosize = p->to.offset + 4;
if(p->from.sym != S)
p->from.sym->value = c;
/* need passes to resolve branches */
if(c-otxt >= 1L<<17)
bflag = 1;
otxt = c;
if(thumb && blitrl)
pool.extra += brextra(p);
continue;
}
diag("zero-width instruction\n%P", p);
continue; continue;
} }
diag("zero-width instruction\n%P", p); switch(o->flag & (LFROM|LTO|LPOOL)) {
continue; case LFROM:
} addpool(p, &p->from);
switch(o->flag & (LFROM|LTO|LPOOL)) { break;
case LFROM: case LTO:
addpool(p, &p->from); addpool(p, &p->to);
break; break;
case LTO: case LPOOL:
addpool(p, &p->to); if ((p->scond&C_SCOND) == 14)
break; flushpool(p, 0, 0);
case LPOOL: break;
if ((p->scond&C_SCOND) == 14) }
if(p->as==AMOVW && p->to.type==D_REG && p->to.reg==REGPC && (p->scond&C_SCOND) == 14)
flushpool(p, 0, 0); flushpool(p, 0, 0);
break; c += m;
} if(blitrl && p->link == P){
if(p->as==AMOVW && p->to.type==D_REG && p->to.reg==REGPC && (p->scond&C_SCOND) == 14) if(thumb && isbranch(p))
flushpool(p, 0, 0); pool.extra += brextra(p);
c += m; checkpool(p, 0);
if(blitrl && p->link == P){ }
if(thumb && isbranch(p))
pool.extra += brextra(p);
checkpool(p, 0);
}
} }
/* /*
...@@ -245,47 +245,47 @@ span(void) ...@@ -245,47 +245,47 @@ span(void)
bflag = 0; bflag = 0;
c = INITTEXT; c = INITTEXT;
for(p = firstp; p != P; p = p->link) { for(p = firstp; p != P; p = p->link) {
setarch(p); setarch(p);
p->pc = c; p->pc = c;
if(thumb && isbranch(p)) if(thumb && isbranch(p))
nocache(p); nocache(p);
o = oplook(p); o = oplook(p);
/* very larg branches /* very larg branches
if(o->type == 6 && p->cond) { if(o->type == 6 && p->cond) {
otxt = p->cond->pc - c; otxt = p->cond->pc - c;
if(otxt < 0) if(otxt < 0)
otxt = -otxt; otxt = -otxt;
if(otxt >= (1L<<17) - 10) { if(otxt >= (1L<<17) - 10) {
q = prg(); q = prg();
q->link = p->link; q->link = p->link;
p->link = q; p->link = q;
q->as = AB; q->as = AB;
q->to.type = D_BRANCH; q->to.type = D_BRANCH;
q->cond = p->cond; q->cond = p->cond;
p->cond = q; p->cond = q;
q = prg(); q = prg();
q->link = p->link; q->link = p->link;
p->link = q; p->link = q;
q->as = AB; q->as = AB;
q->to.type = D_BRANCH; q->to.type = D_BRANCH;
q->cond = q->link->link; q->cond = q->link->link;
bflag = 1; bflag = 1;
}
} }
}
*/ */
m = o->size; m = o->size;
if(m == 0) { if(m == 0) {
if(p->as == ATEXT) { if(p->as == ATEXT) {
curtext = p; curtext = p;
autosize = p->to.offset + 4; autosize = p->to.offset + 4;
if(p->from.sym != S) if(p->from.sym != S)
p->from.sym->value = c; p->from.sym->value = c;
continue;
}
diag("zero-width instruction\n%P", p);
continue; continue;
} }
diag("zero-width instruction\n%P", p); c += m;
continue;
}
c += m;
} }
} }
...@@ -305,48 +305,48 @@ span(void) ...@@ -305,48 +305,48 @@ span(void)
oop = op = nil; oop = op = nil;
again = 0; again = 0;
for(p = firstp; p != P; oop = op, op = p, p = p->link){ for(p = firstp; p != P; oop = op, op = p, p = p->link){
setarch(p); setarch(p);
if(p->pc != c) if(p->pc != c)
again = 1;
p->pc = c;
if(thumb && isbranch(p))
nocache(p);
o = oplook(p);
m = o->size;
if(passes == 1 && thumb && isbranch(p)){ // start conservative so unneeded alignment is not added
if(p->as == ABL)
m = 4;
else
m = 2;
p->align = 0;
}
if(p->align){
if((p->align == 4 && (c&3)) || (p->align == 2 && !(c&3))){
if(ispad(op)){
oop->link = p;
op = oop;
c -= 2;
p->pc = c;
}
else{
op->link = pad(op, c);
op = op->link;
c += 2;
p->pc = c;
}
again = 1; again = 1;
p->pc = c;
if(thumb && isbranch(p))
nocache(p);
o = oplook(p);
m = o->size;
if(passes == 1 && thumb && isbranch(p)){ // start conservative so unneeded alignment is not added
if(p->as == ABL)
m = 4;
else
m = 2;
p->align = 0;
} }
} if(p->align){
if(m == 0) { if((p->align == 4 && (c&3)) || (p->align == 2 && !(c&3))){
if(p->as == ATEXT) { if(ispad(op)){
curtext = p; oop->link = p;
autosize = p->to.offset + 4; op = oop;
if(p->from.sym != S) c -= 2;
p->from.sym->value = c; p->pc = c;
continue; }
else{
op->link = pad(op, c);
op = op->link;
c += 2;
p->pc = c;
}
again = 1;
}
} }
} if(m == 0) {
c += m; if(p->as == ATEXT) {
curtext = p;
autosize = p->to.offset + 4;
if(p->from.sym != S)
p->from.sym->value = c;
continue;
}
}
c += m;
} }
if(c != lastc || again){ if(c != lastc || again){
lastc = c; lastc = c;
......
...@@ -490,37 +490,37 @@ asmb(void) ...@@ -490,37 +490,37 @@ asmb(void)
pc = INITTEXT; pc = INITTEXT;
curp = firstp; curp = firstp;
for(p = firstp; p != P; p = p->link) { for(p = firstp; p != P; p = p->link) {
if(p->as == ATEXT)
curtext = p;
if(p->pc != pc) {
if(!debug['a'])
print("%P\n", curp);
diag("phase error %llux sb %llux in %s", p->pc, pc, TNAME);
pc = p->pc;
}
curp = p;
asmins(p);
a = (andptr - and);
if(cbc < a)
cflush();
if(debug['a']) {
Bprint(&bso, pcstr, pc);
for(op1 = and; op1 < andptr; op1++)
Bprint(&bso, "%.2ux", *op1);
for(; op1 < and+Maxand; op1++)
Bprint(&bso, " ");
Bprint(&bso, "%P\n", curp);
}
if(dlm) {
if(p->as == ATEXT) if(p->as == ATEXT)
reloca = nil; curtext = p;
else if(reloca != nil) if(p->pc != pc) {
diag("reloc failure: %P", curp); if(!debug['a'])
} print("%P\n", curp);
memmove(cbp, and, a); diag("phase error %llux sb %llux in %s", p->pc, pc, TNAME);
cbp += a; pc = p->pc;
pc += a; }
cbc -= a; curp = p;
asmins(p);
a = (andptr - and);
if(cbc < a)
cflush();
if(debug['a']) {
Bprint(&bso, pcstr, pc);
for(op1 = and; op1 < andptr; op1++)
Bprint(&bso, "%.2ux", *op1);
for(; op1 < and+Maxand; op1++)
Bprint(&bso, " ");
Bprint(&bso, "%P\n", curp);
}
if(dlm) {
if(p->as == ATEXT)
reloca = nil;
else if(reloca != nil)
diag("reloc failure: %P", curp);
}
memmove(cbp, and, a);
cbp += a;
pc += a;
cbc -= a;
} }
cflush(); cflush();
......
...@@ -672,260 +672,260 @@ dostkoff(void) ...@@ -672,260 +672,260 @@ dostkoff(void)
deltasp = 0; deltasp = 0;
for(p = firstp; p != P; p = p->link) { for(p = firstp; p != P; p = p->link) {
if(p->as == ATEXT) { if(p->as == ATEXT) {
curtext = p; curtext = p;
parsetextconst(p->to.offset); parsetextconst(p->to.offset);
autoffset = textstksiz; autoffset = textstksiz;
if(autoffset < 0) if(autoffset < 0)
autoffset = 0; autoffset = 0;
q = P; q = P;
q1 = P; q1 = P;
if((p->from.scale & NOSPLIT) && autoffset >= StackSmall) if((p->from.scale & NOSPLIT) && autoffset >= StackSmall)
diag("nosplit func likely to overflow stack"); diag("nosplit func likely to overflow stack");
if(!(p->from.scale & NOSPLIT)) { if(!(p->from.scale & NOSPLIT)) {
p = appendp(p); // load g into CX p = appendp(p); // load g into CX
p->as = AMOVQ; p->as = AMOVQ;
if(HEADTYPE == 7 || HEADTYPE == 9) // ELF uses FS if(HEADTYPE == 7 || HEADTYPE == 9) // ELF uses FS
p->from.type = D_INDIR+D_FS; p->from.type = D_INDIR+D_FS;
else else
p->from.type = D_INDIR+D_GS; p->from.type = D_INDIR+D_GS;
p->from.offset = tlsoffset+0; p->from.offset = tlsoffset+0;
p->to.type = D_CX; p->to.type = D_CX;
if(debug['K']) { if(debug['K']) {
// 6l -K means check not only for stack // 6l -K means check not only for stack
// overflow but stack underflow. // overflow but stack underflow.
// On underflow, INT 3 (breakpoint). // On underflow, INT 3 (breakpoint).
// Underflow itself is rare but this also // Underflow itself is rare but this also
// catches out-of-sync stack guard info // catches out-of-sync stack guard info
p = appendp(p);
p->as = ACMPQ;
p->from.type = D_INDIR+D_CX;
p->from.offset = 8;
p->to.type = D_SP;
p = appendp(p);
p->as = AJHI;
p->to.type = D_BRANCH;
p->to.offset = 4;
q1 = p;
p = appendp(p);
p->as = AINT;
p->from.type = D_CONST;
p->from.offset = 3;
p = appendp(p);
p->as = ANOP;
q1->pcond = p;
q1 = P;
}
if(autoffset < StackBig) { // do we need to call morestack?
if(autoffset <= StackSmall) {
// small stack
p = appendp(p);
p->as = ACMPQ;
p->from.type = D_SP;
p->to.type = D_INDIR+D_CX;
} else {
// large stack
p = appendp(p);
p->as = ALEAQ;
p->from.type = D_INDIR+D_SP;
p->from.offset = -(autoffset-StackSmall);
p->to.type = D_AX;
p = appendp(p);
p->as = ACMPQ;
p->from.type = D_AX;
p->to.type = D_INDIR+D_CX;
}
// common p = appendp(p);
p = appendp(p); p->as = ACMPQ;
p->as = AJHI; p->from.type = D_INDIR+D_CX;
p->to.type = D_BRANCH; p->from.offset = 8;
p->to.offset = 4; p->to.type = D_SP;
q = p;
}
/* 160 comes from 3 calls (3*8) 4 safes (4*8) and 104 guard */ p = appendp(p);
moreconst1 = 0; p->as = AJHI;
if(autoffset+160 > 4096) p->to.type = D_BRANCH;
moreconst1 = (autoffset+160) & ~7LL; p->to.offset = 4;
moreconst2 = textarg; q1 = p;
// 4 varieties varieties (const1==0 cross const2==0)
// and 6 subvarieties of (const1==0 and const2!=0)
p = appendp(p); p = appendp(p);
if(moreconst1 == 0 && moreconst2 == 0) { p->as = AINT;
p->as = ACALL; p->from.type = D_CONST;
p->to.type = D_BRANCH; p->from.offset = 3;
p->pcond = pmorestack[0];
p->to.sym = symmorestack[0];
} else
if(moreconst1 != 0 && moreconst2 == 0) {
p->as = AMOVL;
p->from.type = D_CONST;
p->from.offset = moreconst1;
p->to.type = D_AX;
p = appendp(p); p = appendp(p);
p->as = ACALL; p->as = ANOP;
p->to.type = D_BRANCH; q1->pcond = p;
p->pcond = pmorestack[1]; q1 = P;
p->to.sym = symmorestack[1]; }
} else
if(moreconst1 == 0 && moreconst2 <= 48 && moreconst2%8 == 0) {
i = moreconst2/8 + 3;
p->as = ACALL;
p->to.type = D_BRANCH;
p->pcond = pmorestack[i];
p->to.sym = symmorestack[i];
} else
if(moreconst1 == 0 && moreconst2 != 0) {
p->as = AMOVL;
p->from.type = D_CONST;
p->from.offset = moreconst2;
p->to.type = D_AX;
if(autoffset < StackBig) { // do we need to call morestack?
if(autoffset <= StackSmall) {
// small stack
p = appendp(p); p = appendp(p);
p->as = ACALL; p->as = ACMPQ;
p->to.type = D_BRANCH; p->from.type = D_SP;
p->pcond = pmorestack[2]; p->to.type = D_INDIR+D_CX;
p->to.sym = symmorestack[2];
} else { } else {
p->as = AMOVQ; // large stack
p->from.type = D_CONST; p = appendp(p);
p->from.offset = (uint64)moreconst2 << 32; p->as = ALEAQ;
p->from.offset |= moreconst1; p->from.type = D_INDIR+D_SP;
p->from.offset = -(autoffset-StackSmall);
p->to.type = D_AX; p->to.type = D_AX;
p = appendp(p); p = appendp(p);
p->as = ACALL; p->as = ACMPQ;
p->to.type = D_BRANCH; p->from.type = D_AX;
p->pcond = pmorestack[3]; p->to.type = D_INDIR+D_CX;
p->to.sym = symmorestack[3];
} }
}
if(q != P)
q->pcond = p->link;
if(autoffset) { // common
p = appendp(p); p = appendp(p);
p->as = AADJSP; p->as = AJHI;
p->from.type = D_CONST; p->to.type = D_BRANCH;
p->from.offset = autoffset; p->to.offset = 4;
p->spadj = autoffset; q = p;
if(q != P)
q->pcond = p;
} }
deltasp = autoffset;
if(debug['K'] > 1 && autoffset) { /* 160 comes from 3 calls (3*8) 4 safes (4*8) and 104 guard */
// 6l -KK means double-check for stack overflow moreconst1 = 0;
// even after calling morestack and even if the if(autoffset+160 > 4096)
// function is marked as nosplit. moreconst1 = (autoffset+160) & ~7LL;
p = appendp(p); moreconst2 = textarg;
p->as = AMOVQ;
p->from.type = D_INDIR+D_CX;
p->from.offset = 0;
p->to.type = D_BX;
p = appendp(p); // 4 varieties varieties (const1==0 cross const2==0)
p->as = ASUBQ; // and 6 subvarieties of (const1==0 and const2!=0)
p = appendp(p);
if(moreconst1 == 0 && moreconst2 == 0) {
p->as = ACALL;
p->to.type = D_BRANCH;
p->pcond = pmorestack[0];
p->to.sym = symmorestack[0];
} else
if(moreconst1 != 0 && moreconst2 == 0) {
p->as = AMOVL;
p->from.type = D_CONST; p->from.type = D_CONST;
p->from.offset = StackSmall+32; p->from.offset = moreconst1;
p->to.type = D_BX; p->to.type = D_AX;
p = appendp(p); p = appendp(p);
p->as = ACMPQ; p->as = ACALL;
p->from.type = D_SP;
p->to.type = D_BX;
p = appendp(p);
p->as = AJHI;
p->to.type = D_BRANCH; p->to.type = D_BRANCH;
q1 = p; p->pcond = pmorestack[1];
p->to.sym = symmorestack[1];
} else
if(moreconst1 == 0 && moreconst2 <= 48 && moreconst2%8 == 0) {
i = moreconst2/8 + 3;
p->as = ACALL;
p->to.type = D_BRANCH;
p->pcond = pmorestack[i];
p->to.sym = symmorestack[i];
} else
if(moreconst1 == 0 && moreconst2 != 0) {
p->as = AMOVL;
p->from.type = D_CONST;
p->from.offset = moreconst2;
p->to.type = D_AX;
p = appendp(p); p = appendp(p);
p->as = AINT; p->as = ACALL;
p->to.type = D_BRANCH;
p->pcond = pmorestack[2];
p->to.sym = symmorestack[2];
} else {
p->as = AMOVQ;
p->from.type = D_CONST; p->from.type = D_CONST;
p->from.offset = 3; p->from.offset = (uint64)moreconst2 << 32;
p->from.offset |= moreconst1;
p->to.type = D_AX;
p = appendp(p); p = appendp(p);
p->as = ANOP; p->as = ACALL;
q1->pcond = p; p->to.type = D_BRANCH;
q1 = P; p->pcond = pmorestack[3];
p->to.sym = symmorestack[3];
} }
} }
pcsize = p->mode/8;
a = p->from.type;
if(a == D_AUTO)
p->from.offset += deltasp;
if(a == D_PARAM)
p->from.offset += deltasp + pcsize;
a = p->to.type;
if(a == D_AUTO)
p->to.offset += deltasp;
if(a == D_PARAM)
p->to.offset += deltasp + pcsize;
switch(p->as) {
default:
continue;
case APUSHL:
case APUSHFL:
deltasp += 4;
p->spadj = 4;
continue;
case APUSHQ:
case APUSHFQ:
deltasp += 8;
p->spadj = 8;
continue;
case APUSHW:
case APUSHFW:
deltasp += 2;
p->spadj = 2;
continue;
case APOPL:
case APOPFL:
deltasp -= 4;
p->spadj = -4;
continue;
case APOPQ:
case APOPFQ:
deltasp -= 8;
p->spadj = -8;
continue;
case APOPW:
case APOPFW:
deltasp -= 2;
p->spadj = -2;
continue;
case ARET:
break;
}
if(autoffset != deltasp) if(q != P)
diag("unbalanced PUSH/POP"); q->pcond = p->link;
if(p->from.type == D_CONST)
goto become;
if(autoffset) { if(autoffset) {
p = appendp(p);
p->as = AADJSP; p->as = AADJSP;
p->from.type = D_CONST; p->from.type = D_CONST;
p->from.offset = -autoffset; p->from.offset = autoffset;
p->spadj = -autoffset; p->spadj = autoffset;
if(q != P)
q->pcond = p;
}
deltasp = autoffset;
if(debug['K'] > 1 && autoffset) {
// 6l -KK means double-check for stack overflow
// even after calling morestack and even if the
// function is marked as nosplit.
p = appendp(p);
p->as = AMOVQ;
p->from.type = D_INDIR+D_CX;
p->from.offset = 0;
p->to.type = D_BX;
p = appendp(p);
p->as = ASUBQ;
p->from.type = D_CONST;
p->from.offset = StackSmall+32;
p->to.type = D_BX;
p = appendp(p); p = appendp(p);
p->as = ARET; p->as = ACMPQ;
p->from.type = D_SP;
p->to.type = D_BX;
p = appendp(p);
p->as = AJHI;
p->to.type = D_BRANCH;
q1 = p;
p = appendp(p);
p->as = AINT;
p->from.type = D_CONST;
p->from.offset = 3;
p = appendp(p);
p->as = ANOP;
q1->pcond = p;
q1 = P;
} }
continue; }
pcsize = p->mode/8;
a = p->from.type;
if(a == D_AUTO)
p->from.offset += deltasp;
if(a == D_PARAM)
p->from.offset += deltasp + pcsize;
a = p->to.type;
if(a == D_AUTO)
p->to.offset += deltasp;
if(a == D_PARAM)
p->to.offset += deltasp + pcsize;
switch(p->as) {
default:
continue;
case APUSHL:
case APUSHFL:
deltasp += 4;
p->spadj = 4;
continue;
case APUSHQ:
case APUSHFQ:
deltasp += 8;
p->spadj = 8;
continue;
case APUSHW:
case APUSHFW:
deltasp += 2;
p->spadj = 2;
continue;
case APOPL:
case APOPFL:
deltasp -= 4;
p->spadj = -4;
continue;
case APOPQ:
case APOPFQ:
deltasp -= 8;
p->spadj = -8;
continue;
case APOPW:
case APOPFW:
deltasp -= 2;
p->spadj = -2;
continue;
case ARET:
break;
}
if(autoffset != deltasp)
diag("unbalanced PUSH/POP");
if(p->from.type == D_CONST)
goto become;
if(autoffset) {
p->as = AADJSP;
p->from.type = D_CONST;
p->from.offset = -autoffset;
p->spadj = -autoffset;
p = appendp(p);
p->as = ARET;
}
continue;
become: become:
q = p; q = p;
......
...@@ -51,29 +51,29 @@ span(void) ...@@ -51,29 +51,29 @@ span(void)
idat = INITDAT; idat = INITDAT;
for(p = firstp; p != P; p = p->link) { for(p = firstp; p != P; p = p->link) {
if(p->as == ATEXT) if(p->as == ATEXT)
curtext = p; curtext = p;
n = 0; n = 0;
if(p->to.type == D_BRANCH) if(p->to.type == D_BRANCH)
if(p->pcond == P) if(p->pcond == P)
p->pcond = p; p->pcond = p;
if((q = p->pcond) != P) if((q = p->pcond) != P)
if(q->back != 2) if(q->back != 2)
n = 1; n = 1;
p->back = n; p->back = n;
if(p->as == AADJSP) { if(p->as == AADJSP) {
p->to.type = D_SP; p->to.type = D_SP;
v = -p->from.offset; v = -p->from.offset;
p->from.offset = v;
p->as = p->mode != 64? AADDL: AADDQ;
if(v < 0) {
p->as = p->mode != 64? ASUBL: ASUBQ;
v = -v;
p->from.offset = v; p->from.offset = v;
p->as = p->mode != 64? AADDL: AADDQ;
if(v < 0) {
p->as = p->mode != 64? ASUBL: ASUBQ;
v = -v;
p->from.offset = v;
}
if(v == 0)
p->as = ANOP;
} }
if(v == 0)
p->as = ANOP;
}
} }
n = 0; n = 0;
...@@ -83,16 +83,16 @@ start: ...@@ -83,16 +83,16 @@ start:
Bflush(&bso); Bflush(&bso);
c = INITTEXT; c = INITTEXT;
for(p = firstp; p != P; p = p->link) { for(p = firstp; p != P; p = p->link) {
if(p->as == ATEXT) if(p->as == ATEXT)
curtext = p; curtext = p;
if(p->to.type == D_BRANCH) if(p->to.type == D_BRANCH)
if(p->back) if(p->back)
p->pc = c; p->pc = c;
asmins(p); asmins(p);
p->pc = c; p->pc = c;
m = andptr-and; m = andptr-and;
p->mark = m; p->mark = m;
c += m; c += m;
} }
loop: loop:
...@@ -107,20 +107,20 @@ loop: ...@@ -107,20 +107,20 @@ loop:
again = 0; again = 0;
c = INITTEXT; c = INITTEXT;
for(p = firstp; p != P; p = p->link) { for(p = firstp; p != P; p = p->link) {
if(p->as == ATEXT) if(p->as == ATEXT)
curtext = p; curtext = p;
if(p->to.type == D_BRANCH || p->back & 0100) { if(p->to.type == D_BRANCH || p->back & 0100) {
if(p->back) if(p->back)
p->pc = c; p->pc = c;
asmins(p); asmins(p);
m = andptr-and; m = andptr-and;
if(m != p->mark) { if(m != p->mark) {
p->mark = m; p->mark = m;
again++; again++;
}
} }
} p->pc = c;
p->pc = c; c += p->mark;
c += p->mark;
} }
if(again) { if(again) {
textsize = c; textsize = c;
......
...@@ -473,58 +473,58 @@ asmb(void) ...@@ -473,58 +473,58 @@ asmb(void)
seek(cout, HEADR, 0); seek(cout, HEADR, 0);
pc = INITTEXT; pc = INITTEXT;
curp = firstp; curp = firstp;
for(p = firstp; p != P; p = p->link) { for(p = firstp; p != P; p = p->link) {
if(p->as == ATEXT) if(p->as == ATEXT)
curtext = p; curtext = p;
curp = p; curp = p;
if(HEADTYPE == 8) { if(HEADTYPE == 8) {
// native client // native client
expectpc = p->pc; expectpc = p->pc;
p->pc = pc; p->pc = pc;
asmins(p); asmins(p);
if(p->pc != expectpc) { if(p->pc != expectpc) {
Bflush(&bso);
diag("phase error %lux sb %lux in %s", p->pc, expectpc, TNAME);
}
while(pc < p->pc) {
cput(0x90); // nop
pc++;
}
}
if(p->pc != pc) {
Bflush(&bso); Bflush(&bso);
diag("phase error %lux sb %lux in %s", p->pc, expectpc, TNAME); if(!debug['a'])
print("%P\n", curp);
diag("phase error %lux sb %lux in %s", p->pc, pc, TNAME);
pc = p->pc;
} }
while(pc < p->pc) { if(HEADTYPE != 8) {
cput(0x90); // nop asmins(p);
pc++; if(pc != p->pc) {
Bflush(&bso);
diag("asmins changed pc %lux sb %lux in %s", p->pc, pc, TNAME);
}
} }
} if(cbc < sizeof(and))
if(p->pc != pc) { cflush();
Bflush(&bso); a = (andptr - and);
if(!debug['a'])
print("%P\n", curp); if(debug['a']) {
diag("phase error %lux sb %lux in %s", p->pc, pc, TNAME); Bprint(&bso, pcstr, pc);
pc = p->pc; for(op1 = and; op1 < andptr; op1++)
} Bprint(&bso, "%.2ux", *op1 & 0xff);
if(HEADTYPE != 8) { Bprint(&bso, "\t%P\n", curp);
asmins(p);
if(pc != p->pc) {
Bflush(&bso);
diag("asmins changed pc %lux sb %lux in %s", p->pc, pc, TNAME);
} }
} if(dlm) {
if(cbc < sizeof(and)) if(p->as == ATEXT)
cflush(); reloca = nil;
a = (andptr - and); else if(reloca != nil)
diag("reloc failure: %P", curp);
if(debug['a']) { }
Bprint(&bso, pcstr, pc); memmove(cbp, and, a);
for(op1 = and; op1 < andptr; op1++) cbp += a;
Bprint(&bso, "%.2ux", *op1 & 0xff); pc += a;
Bprint(&bso, "\t%P\n", curp); cbc -= a;
}
if(dlm) {
if(p->as == ATEXT)
reloca = nil;
else if(reloca != nil)
diag("reloc failure: %P", curp);
}
memmove(cbp, and, a);
cbp += a;
pc += a;
cbc -= a;
} }
if(HEADTYPE == 8) { if(HEADTYPE == 8) {
int32 etext; int32 etext;
......
...@@ -390,99 +390,99 @@ patch(void) ...@@ -390,99 +390,99 @@ patch(void)
s = lookup("exit", 0); s = lookup("exit", 0);
vexit = s->value; vexit = s->value;
for(p = firstp; p != P; p = p->link) { for(p = firstp; p != P; p = p->link) {
if(HEADTYPE == 10) { // Windows if(HEADTYPE == 10) { // Windows
// Convert // Convert
// op n(GS), reg // op n(GS), reg
// to // to
// MOVL 0x2C(FS), reg // MOVL 0x2C(FS), reg
// op n(reg), reg // op n(reg), reg
// The purpose of this patch is to fix some accesses // The purpose of this patch is to fix some accesses
// to extern register variables (TLS) on Windows, as // to extern register variables (TLS) on Windows, as
// a different method is used to access them. // a different method is used to access them.
if(p->from.type == D_INDIR+D_GS if(p->from.type == D_INDIR+D_GS
&& p->to.type >= D_AX && p->to.type <= D_DI) { && p->to.type >= D_AX && p->to.type <= D_DI) {
q = appendp(p); q = appendp(p);
q->from = p->from; q->from = p->from;
q->from.type = D_INDIR + p->to.type; q->from.type = D_INDIR + p->to.type;
q->to = p->to; q->to = p->to;
q->as = p->as; q->as = p->as;
p->as = AMOVL; p->as = AMOVL;
p->from.type = D_INDIR+D_FS; p->from.type = D_INDIR+D_FS;
p->from.offset = 0x2C; p->from.offset = 0x2C;
}
} }
} if(HEADTYPE == 7) { // Linux
if(HEADTYPE == 7) { // Linux // Running binaries under Xen requires using
// Running binaries under Xen requires using // MOVL 0(GS), reg
// MOVL 0(GS), reg // and then off(reg) instead of saying off(GS) directly
// and then off(reg) instead of saying off(GS) directly // when the offset is negative.
// when the offset is negative. if(p->from.type == D_INDIR+D_GS && p->from.offset < 0
if(p->from.type == D_INDIR+D_GS && p->from.offset < 0 && p->to.type >= D_AX && p->to.type <= D_DI) {
&& p->to.type >= D_AX && p->to.type <= D_DI) { q = appendp(p);
q = appendp(p); q->from = p->from;
q->from = p->from; q->from.type = D_INDIR + p->to.type;
q->from.type = D_INDIR + p->to.type; q->to = p->to;
q->to = p->to; q->as = p->as;
q->as = p->as; p->as = AMOVL;
p->as = AMOVL; p->from.type = D_INDIR+D_GS;
p->from.type = D_INDIR+D_GS; p->from.offset = 0;
p->from.offset = 0; }
} }
} if(p->as == ATEXT)
if(p->as == ATEXT) curtext = p;
curtext = p; if(p->as == ACALL || (p->as == AJMP && p->to.type != D_BRANCH)) {
if(p->as == ACALL || (p->as == AJMP && p->to.type != D_BRANCH)) { s = p->to.sym;
s = p->to.sym; if(s) {
if(s) { if(debug['c'])
if(debug['c']) Bprint(&bso, "%s calls %s\n", TNAME, s->name);
Bprint(&bso, "%s calls %s\n", TNAME, s->name); switch(s->type) {
switch(s->type) { default:
default: /* diag prints TNAME first */
/* diag prints TNAME first */ diag("undefined: %s", s->name);
diag("undefined: %s", s->name); s->type = STEXT;
s->type = STEXT; s->value = vexit;
s->value = vexit; continue; // avoid more error messages
continue; // avoid more error messages case STEXT:
case STEXT: p->to.offset = s->value;
p->to.offset = s->value; break;
break; case SUNDEF:
case SUNDEF: p->pcond = UP;
p->pcond = UP; p->to.offset = 0;
p->to.offset = 0; break;
break; }
p->to.type = D_BRANCH;
} }
p->to.type = D_BRANCH;
} }
} if(p->to.type != D_BRANCH || p->pcond == UP)
if(p->to.type != D_BRANCH || p->pcond == UP)
continue;
c = p->to.offset;
for(q = firstp; q != P;) {
if(q->forwd != P)
if(c >= q->forwd->pc) {
q = q->forwd;
continue; continue;
c = p->to.offset;
for(q = firstp; q != P;) {
if(q->forwd != P)
if(c >= q->forwd->pc) {
q = q->forwd;
continue;
}
if(c == q->pc)
break;
q = q->link;
} }
if(c == q->pc) if(q == P) {
break; diag("branch out of range in %s\n%P", TNAME, p);
q = q->link; p->to.type = D_NONE;
} }
if(q == P) { p->pcond = q;
diag("branch out of range in %s\n%P", TNAME, p);
p->to.type = D_NONE;
}
p->pcond = q;
} }
for(p = firstp; p != P; p = p->link) { for(p = firstp; p != P; p = p->link) {
if(p->as == ATEXT) if(p->as == ATEXT)
curtext = p; curtext = p;
p->mark = 0; /* initialization for follow */ p->mark = 0; /* initialization for follow */
if(p->pcond != P && p->pcond != UP) { if(p->pcond != P && p->pcond != UP) {
p->pcond = brloop(p->pcond); p->pcond = brloop(p->pcond);
if(p->pcond != P) if(p->pcond != P)
if(p->to.type == D_BRANCH) if(p->to.type == D_BRANCH)
p->to.offset = p->pcond->pc; p->to.offset = p->pcond->pc;
} }
} }
} }
...@@ -635,212 +635,212 @@ dostkoff(void) ...@@ -635,212 +635,212 @@ dostkoff(void)
deltasp = 0; deltasp = 0;
for(p = firstp; p != P; p = p->link) { for(p = firstp; p != P; p = p->link) {
if(p->as == ATEXT) { if(p->as == ATEXT) {
curtext = p; curtext = p;
autoffset = p->to.offset; autoffset = p->to.offset;
if(autoffset < 0) if(autoffset < 0)
autoffset = 0; autoffset = 0;
q = P; q = P;
q1 = P; q1 = P;
if(pmorestack != P) if(pmorestack != P)
if(!(p->from.scale & NOSPLIT)) { if(!(p->from.scale & NOSPLIT)) {
p = appendp(p); // load g into CX p = appendp(p); // load g into CX
switch(HEADTYPE) { switch(HEADTYPE) {
case 10: // Windows case 10: // Windows
p->as = AMOVL; p->as = AMOVL;
p->from.type = D_INDIR+D_FS; p->from.type = D_INDIR+D_FS;
p->from.offset = 0x2c; p->from.offset = 0x2c;
p->to.type = D_CX; p->to.type = D_CX;
p = appendp(p); p = appendp(p);
p->as = AMOVL; p->as = AMOVL;
p->from.type = D_INDIR+D_CX; p->from.type = D_INDIR+D_CX;
p->from.offset = 0; p->from.offset = 0;
p->to.type = D_CX; p->to.type = D_CX;
break; break;
case 7: // Linux case 7: // Linux
p->as = AMOVL; p->as = AMOVL;
p->from.type = D_INDIR+D_GS; p->from.type = D_INDIR+D_GS;
p->from.offset = 0; p->from.offset = 0;
p->to.type = D_CX; p->to.type = D_CX;
p = appendp(p); p = appendp(p);
p->as = AMOVL; p->as = AMOVL;
p->from.type = D_INDIR+D_CX; p->from.type = D_INDIR+D_CX;
p->from.offset = tlsoffset + 0; p->from.offset = tlsoffset + 0;
p->to.type = D_CX; p->to.type = D_CX;
break; break;
default: default:
p->as = AMOVL; p->as = AMOVL;
p->from.type = D_INDIR+D_GS; p->from.type = D_INDIR+D_GS;
p->from.offset = tlsoffset + 0; p->from.offset = tlsoffset + 0;
p->to.type = D_CX; p->to.type = D_CX;
} }
if(debug['K']) { if(debug['K']) {
// 8l -K means check not only for stack // 8l -K means check not only for stack
// overflow but stack underflow. // overflow but stack underflow.
// On underflow, INT 3 (breakpoint). // On underflow, INT 3 (breakpoint).
// Underflow itself is rare but this also // Underflow itself is rare but this also
// catches out-of-sync stack guard info. // catches out-of-sync stack guard info.
p = appendp(p); p = appendp(p);
p->as = ACMPL; p->as = ACMPL;
p->from.type = D_INDIR+D_CX; p->from.type = D_INDIR+D_CX;
p->from.offset = 4; p->from.offset = 4;
p->to.type = D_SP; p->to.type = D_SP;
p = appendp(p); p = appendp(p);
p->as = AJCC; p->as = AJCC;
p->to.type = D_BRANCH; p->to.type = D_BRANCH;
p->to.offset = 4; p->to.offset = 4;
q1 = p; q1 = p;
p = appendp(p);
p->as = AINT;
p->from.type = D_CONST;
p->from.offset = 3;
p = appendp(p);
p->as = ANOP;
q1->pcond = p;
}
if(autoffset < StackBig) { // do we need to call morestack
if(autoffset <= StackSmall) {
// small stack
p = appendp(p); p = appendp(p);
p->as = AINT; p->as = ACMPL;
p->from.type = D_CONST; p->from.type = D_SP;
p->from.offset = 3; p->to.type = D_INDIR+D_CX;
} else {
// large stack
p = appendp(p); p = appendp(p);
p->as = ANOP; p->as = ALEAL;
q1->pcond = p; p->from.type = D_INDIR+D_SP;
} p->from.offset = -(autoffset-StackSmall);
p->to.type = D_AX;
if(autoffset < StackBig) { // do we need to call morestack
if(autoffset <= StackSmall) {
// small stack
p = appendp(p);
p->as = ACMPL;
p->from.type = D_SP;
p->to.type = D_INDIR+D_CX;
} else {
// large stack
p = appendp(p);
p->as = ALEAL;
p->from.type = D_INDIR+D_SP;
p->from.offset = -(autoffset-StackSmall);
p->to.type = D_AX;
p = appendp(p);
p->as = ACMPL;
p->from.type = D_AX;
p->to.type = D_INDIR+D_CX;
}
// common
p = appendp(p); p = appendp(p);
p->as = AJHI; p->as = ACMPL;
p->to.type = D_BRANCH; p->from.type = D_AX;
p->to.offset = 4; p->to.type = D_INDIR+D_CX;
q = p;
} }
p = appendp(p); // save frame size in DX // common
p->as = AMOVL;
p->to.type = D_DX;
/* 160 comes from 3 calls (3*8) 4 safes (4*8) and 104 guard */
p->from.type = D_CONST;
if(autoffset+160 > 4096)
p->from.offset = (autoffset+160) & ~7LL;
p = appendp(p); // save arg size in AX
p->as = AMOVL;
p->to.type = D_AX;
p->from.type = D_CONST;
p->from.offset = curtext->to.offset2;
p = appendp(p); p = appendp(p);
p->as = ACALL; p->as = AJHI;
p->to.type = D_BRANCH; p->to.type = D_BRANCH;
p->pcond = pmorestack; p->to.offset = 4;
p->to.sym = symmorestack; q = p;
} }
if(q != P) p = appendp(p); // save frame size in DX
q->pcond = p->link; p->as = AMOVL;
p->to.type = D_DX;
/* 160 comes from 3 calls (3*8) 4 safes (4*8) and 104 guard */
p->from.type = D_CONST;
if(autoffset+160 > 4096)
p->from.offset = (autoffset+160) & ~7LL;
if(autoffset) { p = appendp(p); // save arg size in AX
p = appendp(p); p->as = AMOVL;
p->as = AADJSP; p->to.type = D_AX;
p->from.type = D_CONST; p->from.type = D_CONST;
p->from.offset = autoffset; p->from.offset = curtext->to.offset2;
p->spadj = autoffset;
if(q != P) p = appendp(p);
q->pcond = p; p->as = ACALL;
} p->to.type = D_BRANCH;
deltasp = autoffset; p->pcond = pmorestack;
} p->to.sym = symmorestack;
a = p->from.type;
if(a == D_AUTO)
p->from.offset += deltasp;
if(a == D_PARAM)
p->from.offset += deltasp + 4;
a = p->to.type;
if(a == D_AUTO)
p->to.offset += deltasp;
if(a == D_PARAM)
p->to.offset += deltasp + 4;
switch(p->as) {
default:
continue;
case APUSHL:
case APUSHFL:
deltasp += 4;
p->spadj = 4;
continue;
case APUSHW:
case APUSHFW:
deltasp += 2;
p->spadj = 2;
continue;
case APOPL:
case APOPFL:
deltasp -= 4;
p->spadj = -4;
continue;
case APOPW:
case APOPFW:
deltasp -= 2;
p->spadj = -2;
continue;
case ARET:
break;
} }
if(autoffset != deltasp) if(q != P)
diag("unbalanced PUSH/POP"); q->pcond = p->link;
if(p->from.type == D_CONST)
goto become;
if(autoffset) { if(autoffset) {
p = appendp(p);
p->as = AADJSP;
p->from.type = D_CONST;
p->from.offset = autoffset;
p->spadj = autoffset;
if(q != P)
q->pcond = p;
}
deltasp = autoffset;
}
a = p->from.type;
if(a == D_AUTO)
p->from.offset += deltasp;
if(a == D_PARAM)
p->from.offset += deltasp + 4;
a = p->to.type;
if(a == D_AUTO)
p->to.offset += deltasp;
if(a == D_PARAM)
p->to.offset += deltasp + 4;
switch(p->as) {
default:
continue;
case APUSHL:
case APUSHFL:
deltasp += 4;
p->spadj = 4;
continue;
case APUSHW:
case APUSHFW:
deltasp += 2;
p->spadj = 2;
continue;
case APOPL:
case APOPFL:
deltasp -= 4;
p->spadj = -4;
continue;
case APOPW:
case APOPFW:
deltasp -= 2;
p->spadj = -2;
continue;
case ARET:
break;
}
if(autoffset != deltasp)
diag("unbalanced PUSH/POP");
if(p->from.type == D_CONST)
goto become;
if(autoffset) {
q = p;
p = appendp(p);
p->as = ARET;
q->as = AADJSP;
q->from.type = D_CONST;
q->from.offset = -autoffset;
p->spadj = -autoffset;
}
continue;
become:
q = p; q = p;
p = appendp(p); p = appendp(p);
p->as = ARET; p->as = AJMP;
p->to = q->to;
p->pcond = q->pcond;
q->as = AADJSP; q->as = AADJSP;
q->from = zprg.from;
q->from.type = D_CONST; q->from.type = D_CONST;
q->from.offset = -autoffset; q->from.offset = -autoffset;
p->spadj = -autoffset; p->spadj = -autoffset;
} q->to = zprg.to;
continue; continue;
become:
q = p;
p = appendp(p);
p->as = AJMP;
p->to = q->to;
p->pcond = q->pcond;
q->as = AADJSP;
q->from = zprg.from;
q->from.type = D_CONST;
q->from.offset = -autoffset;
p->spadj = -autoffset;
q->to = zprg.to;
continue;
} }
} }
......
...@@ -46,29 +46,29 @@ span(void) ...@@ -46,29 +46,29 @@ span(void)
idat = INITDAT; idat = INITDAT;
for(p = firstp; p != P; p = p->link) { for(p = firstp; p != P; p = p->link) {
if(p->as == ATEXT) if(p->as == ATEXT)
curtext = p; curtext = p;
n = 0; n = 0;
if(p->to.type == D_BRANCH) if(p->to.type == D_BRANCH)
if(p->pcond == P) if(p->pcond == P)
p->pcond = p; p->pcond = p;
if((q = p->pcond) != P) if((q = p->pcond) != P)
if(q->back != 2) if(q->back != 2)
n = 1; n = 1;
p->back = n; p->back = n;
if(p->as == AADJSP) { if(p->as == AADJSP) {
p->to.type = D_SP; p->to.type = D_SP;
v = -p->from.offset; v = -p->from.offset;
p->from.offset = v;
p->as = AADDL;
if(v < 0) {
p->as = ASUBL;
v = -v;
p->from.offset = v; p->from.offset = v;
p->as = AADDL;
if(v < 0) {
p->as = ASUBL;
v = -v;
p->from.offset = v;
}
if(v == 0)
p->as = ANOP;
} }
if(v == 0)
p->as = ANOP;
}
} }
n = 0; n = 0;
...@@ -89,24 +89,24 @@ start: ...@@ -89,24 +89,24 @@ start:
if(HEADTYPE == 8) if(HEADTYPE == 8)
c = (c+31)&~31; c = (c+31)&~31;
} }
if(p->to.type == D_BRANCH) if(p->to.type == D_BRANCH)
if(p->back) if(p->back)
p->pc = c; p->pc = c;
if(n == 0 || HEADTYPE == 8 || p->to.type == D_BRANCH) { if(n == 0 || HEADTYPE == 8 || p->to.type == D_BRANCH) {
if(HEADTYPE == 8) if(HEADTYPE == 8)
p->pc = c;
asmins(p);
m = andptr-and;
if(p->mark != m)
again = 1;
p->mark = m;
}
if(HEADTYPE == 8) {
c = p->pc + p->mark;
} else {
p->pc = c; p->pc = c;
asmins(p); c += p->mark;
m = andptr-and; }
if(p->mark != m)
again = 1;
p->mark = m;
}
if(HEADTYPE == 8) {
c = p->pc + p->mark;
} else {
p->pc = c;
c += p->mark;
}
} }
textsize = c; textsize = c;
n++; n++;
......
...@@ -945,65 +945,65 @@ asmlc(void) ...@@ -945,65 +945,65 @@ asmlc(void)
oldpc = INITTEXT; oldpc = INITTEXT;
oldlc = 0; oldlc = 0;
for(p = firstp; p != P; p = p->link) { for(p = firstp; p != P; p = p->link) {
if(p->line == oldlc || p->as == ATEXT || p->as == ANOP) { if(p->line == oldlc || p->as == ATEXT || p->as == ANOP) {
if(p->as == ATEXT) if(p->as == ATEXT)
curtext = p; curtext = p;
if(debug['O']) if(debug['O'])
Bprint(&bso, "%6llux %P\n", Bprint(&bso, "%6llux %P\n",
p->pc, p); p->pc, p);
continue; continue;
} }
if(debug['O'])
Bprint(&bso, "\t\t%6ld", lcsize);
v = (p->pc - oldpc) / MINLC;
while(v) {
s = 127;
if(v < 127)
s = v;
cput(s+128); /* 129-255 +pc */
if(debug['O']) if(debug['O'])
Bprint(&bso, " pc+%ld*%d(%ld)", s, MINLC, s+128); Bprint(&bso, "\t\t%6ld", lcsize);
v -= s; v = (p->pc - oldpc) / MINLC;
lcsize++; while(v) {
} s = 127;
s = p->line - oldlc; if(v < 127)
oldlc = p->line; s = v;
oldpc = p->pc + MINLC; cput(s+128); /* 129-255 +pc */
if(s > 64 || s < -64) { if(debug['O'])
cput(0); /* 0 vv +lc */ Bprint(&bso, " pc+%ld*%d(%ld)", s, MINLC, s+128);
cput(s>>24); v -= s;
cput(s>>16); lcsize++;
cput(s>>8);
cput(s);
if(debug['O']) {
if(s > 0)
Bprint(&bso, " lc+%ld(%d,%ld)\n",
s, 0, s);
else
Bprint(&bso, " lc%ld(%d,%ld)\n",
s, 0, s);
Bprint(&bso, "%6llux %P\n",
p->pc, p);
} }
lcsize += 5; s = p->line - oldlc;
continue; oldlc = p->line;
} oldpc = p->pc + MINLC;
if(s > 0) { if(s > 64 || s < -64) {
cput(0+s); /* 1-64 +lc */ cput(0); /* 0 vv +lc */
if(debug['O']) { cput(s>>24);
Bprint(&bso, " lc+%ld(%ld)\n", s, 0+s); cput(s>>16);
Bprint(&bso, "%6llux %P\n", cput(s>>8);
p->pc, p); cput(s);
if(debug['O']) {
if(s > 0)
Bprint(&bso, " lc+%ld(%d,%ld)\n",
s, 0, s);
else
Bprint(&bso, " lc%ld(%d,%ld)\n",
s, 0, s);
Bprint(&bso, "%6llux %P\n",
p->pc, p);
}
lcsize += 5;
continue;
} }
} else { if(s > 0) {
cput(64-s); /* 65-128 -lc */ cput(0+s); /* 1-64 +lc */
if(debug['O']) { if(debug['O']) {
Bprint(&bso, " lc%ld(%ld)\n", s, 64-s); Bprint(&bso, " lc+%ld(%ld)\n", s, 0+s);
Bprint(&bso, "%6llux %P\n", Bprint(&bso, "%6llux %P\n",
p->pc, p); p->pc, p);
}
} else {
cput(64-s); /* 65-128 -lc */
if(debug['O']) {
Bprint(&bso, " lc%ld(%ld)\n", s, 64-s);
Bprint(&bso, "%6llux %P\n",
p->pc, p);
}
} }
} lcsize++;
lcsize++;
} }
while(lcsize & 1) { while(lcsize & 1) {
s = 129; s = 129;
......
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