diff options
Diffstat (limited to 'utils/il')
| -rw-r--r-- | utils/il/asm.c | 966 | ||||
| -rw-r--r-- | utils/il/compress.c | 270 | ||||
| -rw-r--r-- | utils/il/l.h | 359 | ||||
| -rw-r--r-- | utils/il/list.c | 252 | ||||
| -rw-r--r-- | utils/il/mkfile | 39 | ||||
| -rw-r--r-- | utils/il/noop.c | 341 | ||||
| -rw-r--r-- | utils/il/obj.c | 1517 | ||||
| -rw-r--r-- | utils/il/optab.c | 194 | ||||
| -rw-r--r-- | utils/il/pass.c | 589 | ||||
| -rw-r--r-- | utils/il/span.c | 525 |
10 files changed, 5052 insertions, 0 deletions
diff --git a/utils/il/asm.c b/utils/il/asm.c new file mode 100644 index 00000000..b0b1ce8f --- /dev/null +++ b/utils/il/asm.c @@ -0,0 +1,966 @@ +#include "l.h" + +long OFFSET; + +xlong +entryvalue(void) +{ + char *a; + Sym *s; + + a = INITENTRY; + if(*a >= '0' && *a <= '9') + return atolwhex(a); + s = lookup(a, 0); + if(s->type == 0) + return INITTEXT; + if(s->type != STEXT && s->type != SLEAF) + diag("entry not text: %s", s->name); + return s->value + INITTEXT; +} + +void +asmb(void) +{ + Prog *p; + long t, etext; + Optab *o; + + if(debug['v']) + Bprint(&bso, "%5.2f asm\n", cputime()); + Bflush(&bso); + OFFSET = HEADR; + seek(cout, OFFSET, 0); + pc = 0; + for(p = firstp; p != P; p = p->link) { + if(p->as == ATEXT) { + /* align on word boundary */ + if(!debug['c'] && (pc & 2) != 0){ + nopalign.pc = pc; + pc += asmout(&nopalign, oplook(&nopalign), 0); + } + curtext = p; + autosize = p->to.offset + ptrsize; + } + if(p->pc != pc) { + diag("phase error %lux sb %lux", + p->pc, pc); + if(!debug['a']) + prasm(curp); + pc = p->pc; + } + curp = p; + o = oplook(p); /* could probably avoid this call */ + pc += asmout(p, o, 0); + } + if(debug['a']) + Bprint(&bso, "\n"); + Bflush(&bso); + cflush(); + + etext = textsize; + for(t = pc; t < etext; t += sizeof(buf)-100) { + if(etext-t > sizeof(buf)-100) + datblk(t, sizeof(buf)-100, 1); + else + datblk(t, etext-t, 1); + } + + Bflush(&bso); + cflush(); + + curtext = P; + switch(HEADTYPE) { + case 0: + case 4: + OFFSET = rnd(HEADR+textsize, 4096); + seek(cout, OFFSET, 0); + break; + case 1: + case 2: + case 3: + case 5: + case 6: + OFFSET = HEADR+textsize; + seek(cout, OFFSET, 0); + break; + } + for(t = 0; t < datsize; t += sizeof(buf)-100) { + if(datsize-t > sizeof(buf)-100) + datblk(t, sizeof(buf)-100, 0); + else + datblk(t, datsize-t, 0); + } + + symsize = 0; + lcsize = 0; + if(!debug['s']) { + if(debug['v']) + Bprint(&bso, "%5.2f sym\n", cputime()); + Bflush(&bso); + switch(HEADTYPE) { + case 0: + case 4: + OFFSET = rnd(HEADR+textsize, 4096)+datsize; + seek(cout, OFFSET, 0); + break; + case 3: + case 2: + case 1: + case 5: + case 6: + OFFSET = HEADR+textsize+datsize; + seek(cout, OFFSET, 0); + break; + } + if(!debug['s']) + asmsym(); + if(debug['v']) + Bprint(&bso, "%5.2f pc\n", cputime()); + Bflush(&bso); + if(!debug['s']) + asmlc(); + cflush(); + } + + if(debug['v']) + Bprint(&bso, "%5.2f header\n", cputime()); + Bflush(&bso); + OFFSET = 0; + seek(cout, OFFSET, 0); + switch(HEADTYPE) { + case 1: + break; + case 2: + /* XXX expanded header needed? */ + t = thechar == 'j' ? 30 : 29; + lput(((((4*t)+0)*t)+7)); /* magic */ + lput(textsize); /* sizes */ + lput(datsize); + lput(bsssize); + lput(symsize); /* nsyms */ + lput(entryvalue()); /* va of entry */ + lput(0L); + lput(lcsize); + break; + case 5: + if(thechar == 'j') + elf64(243, ELFDATA2LSB, 0, nil); /* 243 is RISCV */ + else + elf32(243, ELFDATA2LSB, 0, nil); + } + cflush(); +} + +void +strnput(char *s, int n) +{ + for(; *s; s++){ + cput(*s); + n--; + } + for(; n > 0; n--) + cput(0); +} + +void +cput(int c) +{ + cbp[0] = c; + cbp++; + cbc--; + if(cbc <= 0) + cflush(); +} + +void +wput(long l) +{ + + cbp[0] = l>>8; + cbp[1] = l; + cbp += 2; + cbc -= 2; + if(cbc <= 0) + cflush(); +} + +void +wputl(long l) +{ + + cbp[0] = l; + cbp[1] = l>>8; + cbp += 2; + cbc -= 2; + if(cbc <= 0) + cflush(); +} + +void +lput(long l) +{ + + cbp[0] = l>>24; + cbp[1] = l>>16; + cbp[2] = l>>8; + cbp[3] = l; + cbp += 4; + cbc -= 4; + if(cbc <= 0) + cflush(); +} + +void +lputl(long l) +{ + + cbp[3] = l>>24; + cbp[2] = l>>16; + cbp[1] = l>>8; + cbp[0] = l; + cbp += 4; + cbc -= 4; + if(cbc <= 0) + cflush(); +} + +void +llput(vlong v) +{ + lput(v>>32); + lput(v); +} + +void +llputl(vlong v) +{ + lputl(v); + lputl(v>>32); +} + +void +cflush(void) +{ + int n; + + n = sizeof(buf.cbuf) - cbc; + if(n) + write(cout, buf.cbuf, n); + cbp = buf.cbuf; + cbc = sizeof(buf.cbuf); +} + +void +nopstat(char *f, Count *c) +{ + if(c->outof) + Bprint(&bso, "%s delay %ld/%ld (%.2f)\n", f, + c->outof - c->count, c->outof, + (double)(c->outof - c->count)/c->outof); +} + +void +asmsym(void) +{ + Prog *p; + Auto *a; + Sym *s; + int h; + + s = lookup("etext", 0); + if(s->type == STEXT) + putsymb(s->name, 'T', s->value+INITTEXT, s->version); + + for(h=0; h<NHASH; h++) + for(s=hash[h]; s!=S; s=s->link) + switch(s->type) { + case SCONST: + putsymb(s->name, 'D', s->value, s->version); + continue; + + case SSTRING: + putsymb(s->name, 'T', s->value, s->version); + continue; + + case SDATA: + putsymb(s->name, 'D', s->value+INITDAT, s->version); + continue; + + case SBSS: + putsymb(s->name, 'B', s->value+INITDAT, s->version); + continue; + + case SFILE: + putsymb(s->name, 'f', s->value, s->version); + continue; + } + + for(p=textp; p!=P; p=p->cond) { + s = p->from.sym; + if(s->type != STEXT && s->type != SLEAF) + continue; + + /* filenames first */ + for(a=p->to.autom; a; a=a->link) + if(a->type == D_FILE) + putsymb(a->asym->name, 'z', a->aoffset, 0); + else + if(a->type == D_FILE1) + putsymb(a->asym->name, 'Z', a->aoffset, 0); + + if(s->type == STEXT) + putsymb(s->name, 'T', s->value+INITTEXT, s->version); + else + putsymb(s->name, 'L', s->value+INITTEXT, s->version); + + /* frame, auto and param after */ + putsymb(".frame", 'm', p->to.offset+ptrsize, 0); + for(a=p->to.autom; a; a=a->link) + if(a->type == D_AUTO) + putsymb(a->asym->name, 'a', -a->aoffset, 0); + else + if(a->type == D_PARAM) + putsymb(a->asym->name, 'p', a->aoffset, 0); + } + if(debug['v'] || debug['n']) + Bprint(&bso, "symsize = %lud\n", symsize); + Bflush(&bso); +} + +void +putsymb(char *s, int t, vlong v, int ver) +{ + int i, f, l; + + if(t == 'f') + s++; + if(thechar == 'j'){ + l = 8; + llput(v); + }else{ + l = 4; + lput(v); + } + if(ver) + t += 'a' - 'A'; + cput(t+0x80); /* 0x80 is variable length */ + + if(t == 'Z' || t == 'z') { + cput(s[0]); + for(i=1; s[i] != 0 || s[i+1] != 0; i += 2) { + cput(s[i]); + cput(s[i+1]); + } + cput(0); + cput(0); + i++; + } + else { + for(i=0; s[i]; i++) + cput(s[i]); + cput(0); + } + symsize += l + 1 + i + 1; + + if(debug['n']) { + if(t == 'z' || t == 'Z') { + Bprint(&bso, "%c %.8llux ", t, v); + for(i=1; s[i] != 0 || s[i+1] != 0; i+=2) { + f = ((s[i]&0xff) << 8) | (s[i+1]&0xff); + Bprint(&bso, "/%x", f); + } + Bprint(&bso, "\n"); + return; + } + if(ver) + Bprint(&bso, "%c %.8llux %s<%d>\n", t, v, s, ver); + else + Bprint(&bso, "%c %.8llux %s\n", t, v, s); + } +} + +#define MINLC 4 +void +asmlc(void) +{ + long oldpc, oldlc; + Prog *p; + long v, s; + + oldpc = 0; + oldlc = 0; + for(p = firstp; p != P; p = p->link) { + if(p->line == oldlc || p->as == ATEXT || p->as == ANOP) { + if(p->as == ATEXT) + curtext = p; + if(debug['L']) + Bprint(&bso, "%6lux %P\n", + p->pc, p); + continue; + } + if(debug['L']) + 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['L']) + Bprint(&bso, " pc+%ld*%d(%ld)", s, MINLC, s+128); + v -= s; + lcsize++; + } + s = p->line - oldlc; + oldlc = p->line; + oldpc = p->pc + MINLC; + if(s > 64 || s < -64) { + cput(0); /* 0 vv +lc */ + cput(s>>24); + cput(s>>16); + cput(s>>8); + cput(s); + if(debug['L']) { + 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, "%6lux %P\n", + p->pc, p); + } + lcsize += 5; + continue; + } + if(s > 0) { + cput(0+s); /* 1-64 +lc */ + if(debug['L']) { + Bprint(&bso, " lc+%ld(%ld)\n", s, 0+s); + Bprint(&bso, "%6lux %P\n", + p->pc, p); + } + } else { + cput(64-s); /* 65-128 -lc */ + if(debug['L']) { + Bprint(&bso, " lc%ld(%ld)\n", s, 64-s); + Bprint(&bso, "%6lux %P\n", + p->pc, p); + } + } + lcsize++; + } + while(lcsize & 1) { + s = 129; + cput(s); + lcsize++; + } + if(debug['v'] || debug['L']) + Bprint(&bso, "lcsize = %ld\n", lcsize); + Bflush(&bso); +} + +void +datblk(long s, long n, int str) +{ + Prog *p; + char *cast; + vlong d; + long l, fl, j; + int i, c; + + memset(buf.dbuf, 0, n+100); + for(p = datap; p != P; p = p->link) { + curp = p; + if(str != (p->from.sym->type == SSTRING)) + continue; + l = p->from.sym->value + p->from.offset - s; + c = p->reg; + i = 0; + if(l < 0) { + if(l+c <= 0) + continue; + while(l < 0) { + l++; + i++; + } + } + if(l >= n) + continue; + if(p->as != AINIT && p->as != ADYNT) { + for(j=l+(c-i)-1; j>=l; j--) + if(buf.dbuf[j]) { + print("%P\n", p); + diag("multiple initialization"); + break; + } + } + switch(p->to.type) { + default: + diag("unknown mode in initialization\n%P", p); + break; + + case D_FCONST: + switch(c) { + default: + case 4: + fl = ieeedtof(p->to.ieee); + cast = (char*)&fl; + for(; i<c; i++) { + buf.dbuf[l] = cast[fnuxi8[i+4]]; + l++; + } + break; + case 8: + cast = (char*)p->to.ieee; + for(; i<c; i++) { + buf.dbuf[l] = cast[fnuxi8[i]]; + l++; + } + break; + } + break; + + case D_SCONST: + for(; i<c; i++) { + buf.dbuf[l] = p->to.sval[i]; + l++; + } + break; + + case D_VCONST: + d = *p->to.vval; + goto dconst; + + case D_CONST: + d = p->to.offset; + dconst: + if(p->to.sym) { + switch(p->to.sym->type) { + case STEXT: + case SLEAF: + case SSTRING: + d += (p->to.sym->value + INITTEXT); + break; + case SDATA: + case SBSS: + d += (p->to.sym->value + INITDAT); + break; + } + } + cast = (char*)&d; + switch(c) { + default: + diag("bad nuxi %d %d\n%P", c, i, curp); + break; + case 1: + for(; i<c; i++) { + buf.dbuf[l] = cast[inuxi1[i]]; + l++; + } + break; + case 2: + for(; i<c; i++) { + buf.dbuf[l] = cast[inuxi2[i]]; + l++; + } + break; + case 4: + for(; i<c; i++) { + buf.dbuf[l] = cast[inuxi4[i]]; + l++; + } + break; + case 8: + for(; i<c; i++) { + buf.dbuf[l] = cast[inuxi8[i]]; + l++; + } + break; + } + break; + } + if(debug['d'] && i == c) { + Bprint(&bso, "%.8llux", l+s+INITDAT-c); + for(j = -c; j<0; j++) + Bprint(&bso, " %.2ux", ((uchar*)buf.dbuf)[l + j]); + Bprint(&bso, "\t%P\n", curp); + } + } + write(cout, buf.dbuf, n); +} + +#define R(r) ((r)&0x1F) +#define OPX (0x3 | o->op<<2) +#define OPF (OPX | o->func3<<12) +#define OP_R(rs1,rs2,rd)\ + (OPF | rd<<7 | R(rs1)<<15 | R(rs2)<<20 | o->param<<25) +#define OP_RF(rs1,rs2,rd,rm)\ + (OPX | rm<<12 | rd<<7 | R(rs1)<<15 | R(rs2)<<20 | o->param<<25) +#define OP_RO(rs1,rs2,rd)\ + (0x3 | OOP<<2 | o->func3<<12 | rd<<7 | R(rs1)<<15 | R(rs2)<<20) +#define OP_ADD(rs1,rs2,rd)\ + (0x3 | OOP<<2 | rd<<7 | R(rs1)<<15 | R(rs2)<<20) +#define OP_I(rs1,rd,imm)\ + (OPF | rd<<7 | R(rs1)<<15 | (imm)<<20) +#define OP_FI(func3,rs1,rd,imm)\ + (OPX | func3<<12 | rd<<7 | R(rs1)<<15 | (imm)<<20) +#define OP_S(rs1,rs2,imm)\ + (OPF | (imm&0x1F)<<7 | R(rs1)<<15 | R(rs2)<<20 | (imm>>5)<<25) +#define OP_B(rs1,rs2,imm)\ + (OPF | R(rs1)<<15 | R(rs2)<<20 | (imm&0x800)>>4 | (imm&0x1E)<<7 | \ + (imm&0x7E0)<<20 | (imm&0x1000)<<19) +#define OP_U(rd,imm)\ + (0x3 | OLUI<<2 | rd<<7 | (imm&0xFFFFF000)) +#define OP_UP(rd,imm)\ + (0x3 | OAUIPC<<2 | rd<<7 | (imm&0xFFFFF000)) +#define OP_J(rd,imm)\ + (OPX | rd<<7 | (imm&0xff000) | (imm&0x800)<<9 | (imm&0x7FE)<<20 | (imm&0x100000)<<11) + +/* + * aflag: 0 - assemble to object file + * 1 - return assembled instruction + * 2 - first pass, return length of assembled instruction + * 3 - later pass, return length of assembled instruction + */ +int +asmout(Prog *p, Optab *o, int aflag) +{ + vlong vv; + long o1, o2, o3, v; + int r; + + o1 = 0; + o2 = 0; + o3 = 0; + r = p->reg; + if(r == NREG) switch(p->as){ + case AMOVF: + case AMOVD: + if(p->from.type == D_FREG && p->to.type == D_FREG) + r = p->from.reg; + break; + case AMOV: + case AJMP: + r = REGZERO; + break; + case AJAL: + r = REGLINK; + break; + default: + r = p->to.reg; + break; + } + if(!debug['c'] && o->ctype){ + o1 = asmcompressed(p, o, r, aflag == 2); + if(o1 != 0){ + switch(aflag){ + case 1: + return o1; + case 2: + case 3: + return 2; + } + if(debug['a']){ + v = p->pc + INITTEXT; + Bprint(&bso, " %.8lux: %.4lux \t%P\n", v, o1, p); + } + wputl(o1); + return 2; + } + } + if(aflag >= 2) + return o->size; + switch(o->type) { + default: + diag("unknown type %d", o->type); + if(!debug['a']) + prasm(p); + break; + + case 0: /* add S,[R,]D */ + o1 = OP_R(r, p->from.reg, p->to.reg); + break; + + case 1: /* slli $I,[R,]D */ + v = p->from.offset & 0x3F; + v |= (o->param<<5); + o1 = OP_I(r, p->to.reg, v); + break; + + case 2: /* addi $I,[R,]D */ + v = p->from.offset; + if(v < -BIG || v >= BIG) + diag("imm out of range\n%P", p); + o1 = OP_I(r, p->to.reg, v); + break; + + case 3: /* beq S,[R,]L */ + if(r == NREG) + r = REGZERO; + if(p->cond == P) + v = 0; + else + v = (p->cond->pc - pc); + if(v < -0x1000 || v >= 0x1000) + diag("branch out of range\n%P", p); + o1 = OP_B(r, p->from.reg, v); + break; + + case 4: /* jal [D,]L */ + if(p->cond == P) + v = 0; + else + v = (p->cond->pc - pc); + if(v < -0x100000 || v >= 0x100000) + diag("jump out of range\n%P", p); + o1 = OP_J(r, v); + break; + + case 5: /* jalr [D,]I(S) */ + v = regoff(&p->to); + if(v < -BIG || v >= BIG) + diag("imm out of range\n%P", p); + o1 = OP_I(classreg(&p->to), r, v); + break; + + case 6: /* sb R,I(S) */ + v = regoff(&p->to); + r = classreg(&p->to); + if(v < -BIG || v >= BIG) + diag("imm out of range\n%P", p); + o1 = OP_S(r, p->from.reg, v); + break; + + case 7: /* lb I(S),D */ + v = regoff(&p->from); + r = classreg(&p->from); + if(v < -BIG || v >= BIG) + diag("imm out of range\n%P", p); + o1 = OP_I(r, p->to.reg, v); + break; + + case 8: /* lui I,D */ + v = p->from.offset; + o1 = OP_U(p->to.reg, v); + break; + + case 9: /* lui I1,D; addi I0,D */ + v = regoff(&p->from); + if(thechar == 'j' && v >= 0x7ffff800){ + /* awkward edge case */ + o1 = OP_U(p->to.reg, -v); + v &= 0xFFF; + o2 = OP_FI(4, p->to.reg, p->to.reg, v); /* xori */ + break; + } + if(v & 0x800) + v += 0x1000; + o1 = OP_U(p->to.reg, v); + v &= 0xFFF; + o2 = OP_I(p->to.reg, p->to.reg, v); + break; + + case 10: /* sign extend */ + if(p->as == AMOVBU) { + o1 = OP_I(p->from.reg, p->to.reg, o->param); + break; + } + v = o->param; + if(thechar == 'j') + v += 32; + o1 = OP_FI(1, p->from.reg, p->to.reg, v & 0x3F); /* slli */ + o2 = OP_I(p->to.reg, p->to.reg, v); /* srli or srai */ + break; + + case 11: /* addi $I,R,D */ + v = regoff(&p->from); + if(v < -BIG || v >= BIG) + diag("imm out of range\n%P", p); + o1 = OP_I(classreg(&p->from), p->to.reg, v); + break; + + case 12: /* mov r,lext => lui/auipc I1,T; sb r,I0(T) */ + v = regoff(&p->to); + if(thechar == 'j'){ + vv = v + INITDAT + BIG - INITTEXT - pc; + v = (long)vv; + if(v != vv || (v&~0x7FF) == 0x7FFFF800) + diag("address out of range\n%P", p); + }else + v += INITDAT + BIG; + if(v & 0x800) + v += 0x1000; + o1 = thechar == 'j' ? OP_UP(REGTMP, v) : OP_U(REGTMP, v); + v &= 0xFFF; + o2 = OP_S(REGTMP, p->from.reg, v); + break; + + case 13: /* mov lext, r => lui/auipc I1,T; lb r,I0(T) */ + v = regoff(&p->from); + if(thechar == 'j'){ + vv = v + INITDAT + BIG - INITTEXT - pc; + v = (long)vv; + if(v != vv || (v&~0x7FF) == 0x7FFFF800) + diag("address out of range\n%P", p); + }else + v += INITDAT + BIG; + if(v & 0x800) + v += 0x1000; + o1 = thechar == 'j' ? OP_UP(REGTMP, v) : OP_U(REGTMP, v); + v &= 0xFFF; + o2 = OP_I(REGTMP, p->to.reg, v); + break; + + case 14: /* op $lcon,[r,]d => lui L1,T; addi $L0,T,T; op T,r,d */ + v = regoff(&p->from); + if(p->as == AMOVW || p->as == AMOV) + r = classreg(&p->from); + if(thechar == 'j' && v >= 0x7ffff800){ + /* awkward edge case */ + o1 = OP_U(p->to.reg, -v); + v &= 0xFFF; + o2 = OP_FI(4, p->to.reg, p->to.reg, v); /* xori */ + }else{ + if(v & 0x800) + v += 0x1000; + o1 = OP_U(REGTMP, v); + v &= 0xFFF; + o2 = OP_FI(0, REGTMP, REGTMP, v); /* addi */ + } + o3 = OP_RO(r, REGTMP, p->to.reg); + break; + + case 15: /* mov r,L(s) => lui L1,T; add s,T,T; sb r,L0(T) */ + v = regoff(&p->to); + r = classreg(&p->to); + if(v & 0x800) + v += 0x1000; + o1 = OP_U(REGTMP, v); + v &= 0xFFF; + o2 = OP_ADD(r, REGTMP, REGTMP); + o3 = OP_S(REGTMP, p->from.reg, v); + break; + + case 16: /* mov L(s),r => lui L1,T; add s,T,T; lb r,L0(T) */ + v = regoff(&p->from); + r = classreg(&p->from); + if(v & 0x800) + v += 0x1000; + o1 = OP_U(REGTMP, v); + v &= 0xFFF; + o2 = OP_ADD(r, REGTMP, REGTMP); + o3 = OP_I(REGTMP, p->to.reg, v); + break; + + case 17: /* fcvt S,D */ + v = 7; /* dynamic rounding mode */ + if(o->a3 == C_REG) /* convert to int? */ + v = 1; /* round towards zero */ + o1 = OP_RF(p->from.reg, o->func3, p->to.reg, v); + break; + + case 18: /* lui L1, T; jal [r,]L0(T) */ + if(p->cond == P) + v = 0; + else + v = p->cond->pc; + if(thechar == 'j'){ + vv = v + INITTEXT; + v = (long)vv; + if(v != vv || (v&~0x7FF) == 0x7FFFF800) + diag("branch out of range\n%P", p); + }else + v += INITTEXT; + if(v & 0x800) + v += 0x1000; + o1 = thechar == 'j' ? OP_UP(REGTMP, v) : OP_U(REGTMP, v); + v &= 0xFFF; + o2 = OP_I(REGTMP, r, v); + break; + + case 19: /* addiw $0, rs, rd */ + v = 0; + o1 = OP_I(p->from.reg, p->to.reg, v); + break; + + case 20: /* lui/auipc I1,D; addi I0; D */ + if(thechar == 'j'){ + vv = regoff(&p->from) + instoffx - (pc + INITTEXT); + v = (long)vv; + if(v != vv || (v&~0x7FF) == 0x7FFFF800) + diag("address %llux out of range\n%P", regoff(&p->from) + instoffx, p); + }else{ + v = regoff(&p->from) + instoffx; + } + if(v & 0x800) + v += 0x1000; + o1 = thechar == 'j' ? OP_UP(p->to.reg, v) : OP_U(p->to.reg, v); + v &= 0xFFF; + o2 = OP_I(p->to.reg, p->to.reg, v); + break; + + case 21: /* lui I,D; s[lr]ai N,D */ + vv = *p->from.vval; + v = vconshift(vv); + if(v < 0) + diag("64 bit constant error:\n%P", p); + if(v < 12) + vv <<= 12 - v; + else + vv >>= v - 12; + o1 = OP_U(p->to.reg, (long)vv); + if (v > 12) + o2 = OP_FI(1, p->to.reg, p->to.reg, v - 12); /* slli */ + else + o2 = OP_I(p->to.reg, p->to.reg, 12 - v); /* srai */ + break; + + case 22: /* CSRRx C, rs, rd */ + v = p->from.offset & 0xFFF; + o1 = OP_I(p->reg, p->to.reg, v); + break; + + case 24: /* SYS */ + v = p->to.offset & 0xFFF; + o1 = OP_I(0, 0, v); + break; + + case 25: /* word $x */ + o1 = regoff(&p->to); + break; + + case 26: /* pseudo ops */ + break; + } + if(aflag) + return o1; + v = p->pc + INITTEXT; + switch(o->size) { + default: + if(debug['a']) + Bprint(&bso, " %.8lux:\t\t%P\n", v, p); + break; + case 4: + if(debug['a']) + Bprint(&bso, " %.8lux: %.8lux\t%P\n", v, o1, p); + lputl(o1); + break; + case 8: + if(debug['a']) + Bprint(&bso, " %.8lux: %.8lux %.8lux%P\n", v, o1, o2, p); + lputl(o1); + lputl(o2); + break; + case 12: + if(debug['a']) + Bprint(&bso, " %.8lux: %.8lux %.8lux %.8lux%P\n", v, o1, o2, o3, p); + lputl(o1); + lputl(o2); + lputl(o3); + break; + } + return o->size; +} + diff --git a/utils/il/compress.c b/utils/il/compress.c new file mode 100644 index 00000000..e0063615 --- /dev/null +++ b/utils/il/compress.c @@ -0,0 +1,270 @@ +#include "l.h" + +#define COMPREG(r) ((r & ~0x7) == 0x8) +#define COP_CR(op,rs,rd)\ + (op | (rs)<<2 | (rd)<<7) +#define COP_CI(op,rd,i)\ + (op | (rd)<<7 | ((i)&0x20)<<7 | ((i)&0x1F)<<2) +#define COP_CIW(op,rd,i)\ + (op | (rd)<<2 | (i)<<5) +#define COP_CJ(op,i)\ + (op | (i)<<2) +#define COP_CB(op,rs,i)\ + (op | (rs)<<7 | ((i)&0x1f)<<2 | ((i)&0xE0)<<5) +#define COP_CL(op,rs,rd,i)\ + (op | (rs)<<7 | (rd)<<2 | ((i)&0x7)<<4 | ((i)&0x38)<<7) +#define COP_CSS(op,rs,i)\ + (op | (rs)<<2 | ((i)&0x3F)<<7) +#define COP_CS(op,rs2,rs1,i)\ + (op | (rs2)<<2 | (rs1)<<7 | ((i)&0x7)<<4 | ((i)&0x38)<<7) + +static int +immbits(int v, char *permute) +{ + int r, i, c; + + r = 0; + for(i = 0; (c = *permute++) != 0; i++) + r |= ((v>>c) & 0x01) << i; + return r; +} + +long +asmcjmp(int a, Prog *p, int first) +{ + long v; + + if(p->cond == P) + v = 0; + else + v = (p->cond->pc - p->pc); + if(first || ((v&0x01) == 0 && v >= -0x800 && v < 0x800)) + return COP_CJ(a, immbits(v, "\5\1\2\3\7\6\12\10\11\4\13")); + return 0; +} + +int +asmcbz(int a, Prog *p, int first) +{ + long v; + int r; + + r = p->reg; + if(r == REGZERO || r == NREG) + r = p->from.reg; + else if(p->from.reg != REGZERO) + return 0; + if(!COMPREG(r)) + return 0; + if(p->cond == P) + v = 0; + else + v = (p->cond->pc - p->pc); + if(first || ((v&0x01) == 0 && v >= -0x100 && v < 0x100)) + return COP_CB(a, r&0x7, immbits(v, "\5\1\2\6\7\3\4\10")); + return 0; +} + +int +asmcload(Prog *p, int a, uint len, uint maxoff) +{ + int v; + int r; + + v = regoff(&p->from); + if((v & (len-1)) != 0 || v < 0) + return 0; + r = classreg(&p->from); + if(COMPREG(r) && COMPREG(p->to.reg)){ + if(v < maxoff){ + v |= (v>>5) & ~0x1; + return COP_CL(a, r&0x7, p->to.reg&0x7, v); + } + }else if(r == REGSP){ + if(v < 2*maxoff){ + v |= (v>>6); + return COP_CI(a | 0x2, p->to.reg, v); + } + } + return 0; +} + +int +asmcstore(Prog *p, int a, uint len, uint maxoff) +{ + int v; + int r; + + v = regoff(&p->to); + if((v & (len-1)) != 0 || v < 0) + return 0; + r = classreg(&p->to); + if(COMPREG(r) && COMPREG(p->from.reg)){ + if(v < maxoff){ + v |= (v>>5) & ~0x1; + return COP_CS(a, p->from.reg&0x7, r&0x7, v); + } + }else if(r == REGSP){ + if(v < 2*maxoff){ + v |= (v>>6); + return COP_CSS(a | 0x2, (p->from.reg&0x1F), v); + } + } + return 0; +} + +int +asmcompressed(Prog *p, Optab *o, int r, int first) +{ + long v; + int a; + + switch(o->ctype){ + case 1: /* C.ADD */ + if(p->from.reg != REGZERO && p->to.reg != REGZERO && r == p->to.reg) + return COP_CR(0x9002, p->from.reg, r); + break; + case 2: /* C.MV */ + if(p->from.type != D_REG) + diag("compress MOVW R,R doesn't apply\n%P", p); + if(p->to.reg != REGZERO){ + if(p->from.type == D_REG && p->from.reg != REGZERO) + return COP_CR(0x8002, p->from.reg, p->to.reg); + else + return COP_CI(0x4001, p->to.reg, 0); + } + break; + case 3: /* C.JALR */ + if(r == REGLINK && p->to.reg != REGZERO && p->to.offset == 0) + return COP_CR(0x9002, 0, p->to.reg); + break; + case 4: /* C.JR */ + if(p->to.reg != REGZERO &&p->to.offset == 0) + return COP_CR(0x8002, 0, p->to.reg); + break; + case 5: /* C.AND C.OR C.SUB C.XOR */ + if(r == p->to.reg && COMPREG(p->from.reg) && COMPREG(p->to.reg)){ + v = 0x8C01; + switch(o->as){ + case AXOR: + v |= 1<<5; + break; + case AOR: + v |= 2<<5; + break; + case AAND: + v |= 3<<5; + break; + } + return COP_CR(v, p->from.reg&0x7, p->to.reg&0x7); + } + break; + case 6: /* C.LI */ + v = p->from.offset; + if(p->to.reg != REGZERO && v >= -0x20 && v < 0x20){ + return COP_CI(0x4001, p->to.reg, v); + } + break; + case 7: /* C.LUI */ + v = p->from.offset; + if((v&0xFFF) != 0) + return 0; + v >>= 12; + if(p->to.reg != REGZERO && p->to.reg != REGSP && v >= -0x20 && v < 0x20) + return COP_CI(0x6001, p->to.reg, v); + break; + case 8: /* C.SLLI */ + v = p->from.offset; + if((v & 0x20) != 0 && thechar == 'i') + break; + if(r == p->to.reg && r != REGZERO && v != 0 && (v & ~0x3F) == 0) + return COP_CI(0x0002, p->to.reg, v); + break; + case 9: /* C.SRAI C.SRLI */ + v = p->from.offset; + if((v & 0x20) != 0 && thechar == 'i') + break; + a = (o->as == ASRA) << 10; + if(r == p->to.reg && COMPREG(r) && v != 0 && (v & ~0x3F) == 0) + return COP_CI(0x8001 | a, r&0x7, v); + break; + case 10: /* C.ADDI C.ADDI16SP C.ADDI4SPN C.NOP */ + v = p->from.offset; + if(r == p->to.reg && r != REGZERO && v != 0 && v >= -0x20 && v < 0x20) + return COP_CI(0x0001, p->to.reg, v); + if(r == p->to.reg && r == REGSP && v != 0 && (v&0xF) == 0 && v >= -0x200 && v < 0x200) + return COP_CI(0x6001, REGSP, immbits(v, "\5\7\10\6\4\11")); + if(r == REGSP && COMPREG(p->to.reg) && (v&0x3) == 0 && v > 0 && v < 0x400) + return COP_CIW(0x0000, p->to.reg&0x7, immbits(v, "\3\2\6\7\10\11\4\5")); + if(r == p->to.reg && r == REGZERO && v == 0) + return COP_CI(0x0001, 0, 0); + break; + case 11: /* C.JAL (rv32) */ + if(thechar != 'i') + break; + if(r == REGLINK) + return asmcjmp(0x2001, p, first); + break; + case 12: /* C.J */ + return asmcjmp(0xA001, p, first); + break; + case 13: /* C.ANDI */ + v = p->from.offset; + if(r == p->to.reg && COMPREG(r) && v >= -0x20 && v < 0x20) + return COP_CI(0x8801, r&0x7, v); + break; + case 14: /* C.BEQZ */ + return asmcbz(0xC001, p, first); + case 15: /* C.BNEZ */ + return asmcbz(0xE001, p, first); + case 16: /* C.LW C.LWSP */ + return asmcload(p, 0x4000, 4, 0x80); + case 17: /* C.FLW, C.FLWSP (rv32) */ + if(thechar != 'i') + break; + return asmcload(p, 0x6000, 4, 0x80); + case 18: /* C.FLD, C.FLDSP */ + return asmcload(p, 0x2000, 8, 0x100); + case 19: /* C.SW C.SWSP */ + return asmcstore(p, 0xC000, 4, 0x80); + case 20: /* C.FSW, C.FSWSP (rv32) */ + if(thechar != 'i') + break; + return asmcstore(p, 0xE000, 4, 0x80); + case 21: /* C.FSD, C.FSDSP */ + return asmcstore(p, 0xA000, 8, 0x100); + case 22: /* C.ADDW C.SUBW (rv64) */ + if(thechar != 'j') + break; + if(r == p->to.reg && COMPREG(p->from.reg) && COMPREG(p->to.reg)){ + v = 0x9C01; + switch(o->as){ + case AADDW: + v |= 1<<5; + break; + } + return COP_CR(v, p->from.reg&0x7, p->to.reg&0x7); + } + break; + case 23: /* C.ADDIW (rv64) */ + if(thechar != 'j') + break; + v = p->from.offset; + if(p->as == AMOVW){ + v = 0; + r = p->from.reg; + } + if(r == p->to.reg && r != REGZERO && v >= -0x20 && v < 0x20) + return COP_CI(0x2001, p->to.reg, v); + break; + case 24: /* C.LD (rv64) */ + if(thechar != 'j') + break; + return asmcload(p, 0x6000, 8, 0x100); + case 25: /* C.SD (rv64) */ + if(thechar != 'j') + break; + return asmcstore(p, 0xE000, 8, 0x100); + } + return 0; +} diff --git a/utils/il/l.h b/utils/il/l.h new file mode 100644 index 00000000..3cb106af --- /dev/null +++ b/utils/il/l.h @@ -0,0 +1,359 @@ +#include <lib9.h> +#include <bio.h> +#include "../ic/i.out.h" +#include "../ld/elf.h" + +#ifndef EXTERN +#define EXTERN extern +#endif + +typedef vlong xlong; + +typedef struct Adr Adr; +typedef struct Sym Sym; +typedef struct Autom Auto; +typedef struct Prog Prog; +typedef struct Optab Optab; +typedef struct Oprang Oprang; +typedef uchar Opcross[32][32]; +typedef struct Count Count; + +#define P ((Prog*)0) +#define S ((Sym*)0) +#define TNAME (curtext&&curtext->from.sym?curtext->from.sym->name:noname) + +struct Adr +{ + union + { + long u0offset; + char* u0sval; + Ieee* u0ieee; + vlong* u0vval; + } u0; + union + { + Auto* u1autom; + Sym* u1sym; + } u1; + char type; + char reg; + char name; + char class; +}; + +#define offset u0.u0offset +#define sval u0.u0sval +#define ieee u0.u0ieee +#define vval u0.u0vval + +#define autom u1.u1autom +#define sym u1.u1sym + +struct Prog +{ + Adr from; + Adr to; + union + { + long u0regused; + Prog* u0forwd; + } u0; + Prog* cond; + Prog* link; + long pc; + long line; + uchar mark; + uchar optab; + char as; + char reg; +}; +#define regused u0.u0regused +#define forwd u0.u0forwd + +struct Sym +{ + char *name; + short type; + short version; + short become; + short frame; + ushort file; + long value; + long sig; + Sym* link; +}; +struct Autom +{ + Sym* asym; + Auto* link; + long aoffset; + short type; +}; +struct Optab +{ + char as; + char a1; + char a3; + char type; + char ctype; + char size; + char op; + char func3; + short param; +}; +struct Oprang +{ + Optab* start; + Optab* stop; +}; +struct Count +{ + long count; + long outof; +}; + +enum +{ + STEXT = 1, + SDATA, + SBSS, + SDATA1, + SXREF, + SLEAF, + SFILE, + SCONST, + SSTRING, + + C_NONE = 0, + C_REG, + C_CTLREG, + C_FREG, + C_ZREG, + C_ZCON, + C_SCON, + C_UCON, + C_LCON, + C_VCON, + C_FCON, + C_SACON, + C_SECON, + C_LACON, + C_LECON, + C_SRCON, + C_LRCON, + C_SBRA, + C_LBRA, + C_SAUTO, + C_ZOREG, + C_SOREG, + C_LOREG, + C_LAUTO, + C_SEXT, + C_LEXT, + C_GOK, + + NSCHED = 20, + +/* mark flags */ + FOLL = 1<<0, + LABEL = 1<<1, + LEAF = 1<<2, + SYNC = 1<<3, + BRANCH = 1<<4, + COMPR = 1<<5, + SPASS = 1<<6, + + BIG = 2048, + STRINGSZ = 200, + NHASH = 10007, + NHUNK = 100000, + MINSIZ = 64, + NENT = 100, + MAXIO = 8192, + MAXHIST = 20, /* limit of path elements for history symbols */ +}; + +/* Major opcodes */ +enum { + OLOAD, OLOAD_FP, Ocustom_0, OMISC_MEM, OOP_IMM, OAUIPC, OOP_IMM_32, O48b, + OSTORE, OSTORE_FP, Ocustom_1, OAMO, OOP, OLUI, OOP_32, O64b, + OMADD, OMSUB, ONMSUB, ONMADD, OOP_FP, Ores_0, Ocustom_2, O48b_2, + OBRANCH, OJALR, Ores_1, OJAL, OSYSTEM, Ores_2, Ocustom_3, O80b +}; + +EXTERN union +{ + struct + { + uchar obuf[MAXIO]; /* output buffer */ + uchar ibuf[MAXIO]; /* input buffer */ + } u; + char dbuf[1]; +} buf; + +#define cbuf u.obuf +#define xbuf u.ibuf + +EXTERN long HEADR; /* length of header */ +EXTERN int HEADTYPE; /* type of header */ +EXTERN xlong INITDAT; /* data location */ +EXTERN xlong INITRND; /* data round above text location */ +EXTERN xlong INITTEXT; /* text location */ +EXTERN xlong INITTEXTP; /* text location (physical) */ +EXTERN char* INITENTRY; /* entry point */ +EXTERN long autosize; +EXTERN Biobuf bso; +EXTERN long bsssize; +EXTERN int cbc; +EXTERN uchar* cbp; +EXTERN int cout; +EXTERN Auto* curauto; +EXTERN Auto* curhist; +EXTERN Prog* curp; +EXTERN Prog* curtext; +EXTERN Prog* datap; +EXTERN long datsize; +EXTERN char debug[128]; +EXTERN Prog* etextp; +EXTERN Prog* firstp; +EXTERN char fnuxi4[4]; /* for 3l [sic] */ +EXTERN char fnuxi8[8]; +EXTERN char* noname; +EXTERN Sym* hash[NHASH]; +EXTERN Sym* histfrog[MAXHIST]; +EXTERN int histfrogp; +EXTERN int histgen; +EXTERN char* library[50]; +EXTERN char* libraryobj[50]; +EXTERN int libraryp; +EXTERN int xrefresolv; +EXTERN char* hunk; +EXTERN char inuxi1[1]; +EXTERN char inuxi2[2]; +EXTERN char inuxi4[4]; +EXTERN char inuxi8[8]; +EXTERN Prog* lastp; +EXTERN long lcsize; +EXTERN char literal[32]; +EXTERN int nerrors; +EXTERN long nhunk; +EXTERN Prog nopalign; +EXTERN long instoffset; +EXTERN vlong instoffx; +EXTERN Opcross opcross[10]; +EXTERN Oprang oprange[ALAST]; +EXTERN char* outfile; +EXTERN long pc; +EXTERN int ptrsize; +EXTERN uchar repop[ALAST]; +EXTERN long symsize; +EXTERN Prog* textp; +EXTERN long textsize; +EXTERN long thunk; +EXTERN int version; +EXTERN char xcmp[32][32]; +EXTERN Prog zprg; +EXTERN int dtype; +EXTERN int little; + +EXTERN struct +{ + Count branch; + Count fcmp; + Count load; + Count mfrom; + Count page; + Count jump; +} nop; + +extern char* anames[]; +extern Optab optab[]; +extern char thechar; + +#pragma varargck type "A" int +#pragma varargck type "D" Adr* +#pragma varargck type "N" Adr* +#pragma varargck type "P" Prog* +#pragma varargck type "S" char* + +#pragma varargck argpos diag 1 + +int Aconv(Fmt*); +int Dconv(Fmt*); +int Nconv(Fmt*); +int Pconv(Fmt*); +int Sconv(Fmt*); +int aclass(Adr*); +void addhist(long, int); +void append(Prog*, Prog*); +void asmb(void); +int asmcompressed(Prog*, Optab*, int, int); +void asmlc(void); +int asmout(Prog*, Optab*, int); +void asmsym(void); +vlong atolwhex(char*); +Prog* brloop(Prog*); +void buildop(void); +void buildrep(int, int); +void cflush(void); +int cmp(int, int); +int compound(Prog*); +double cputime(void); +void datblk(long, long, int); +void diag(char*, ...); +void dodata(void); +void doprof1(void); +void doprof2(void); +vlong entryvalue(void); +void errorexit(void); +void exchange(Prog*); +int find1(long, int); +void follow(void); +void gethunk(void); +void histtoauto(void); +double ieeedtod(Ieee*); +long ieeedtof(Ieee*); +int isnop(Prog*); +void ldobj(int, long, char*); +void loadlib(void); +void listinit(void); +Sym* lookup(char*, int); +void cput(int); +void llput(vlong); +void llputl(vlong); +void lput(long); +void lputl(long); +void bput(long); +void mkfwd(void); +void* mysbrk(ulong); +void names(void); +void nocache(Prog*); +void noops(void); +void nuxiinit(void); +void objfile(char*); +int ocmp(void*, void*); +long opirr(int); +Optab* oplook(Prog*); +long oprrr(int); +void patch(void); +void prasm(Prog*); +void prepend(Prog*, Prog*); +Prog* prg(void); +int pseudo(Prog*); +void putsymb(char*, int, vlong, int); +long regoff(Adr*); +int classreg(Adr*); +int relinv(int); +int relrev(int); +vlong rnd(vlong, vlong); +void span(void); +void strnput(char*, int); +void undef(void); +int vconshift(vlong); +void wput(long); +void wputl(long); +void xdefine(char*, int, long); +void xfol(Prog*); +void xfol(Prog*); +void nopstat(char*, Count*); diff --git a/utils/il/list.c b/utils/il/list.c new file mode 100644 index 00000000..93989a97 --- /dev/null +++ b/utils/il/list.c @@ -0,0 +1,252 @@ +#include "l.h" + +void +listinit(void) +{ + + fmtinstall('A', Aconv); + fmtinstall('D', Dconv); + fmtinstall('P', Pconv); + fmtinstall('S', Sconv); + fmtinstall('N', Nconv); +} + +void +prasm(Prog *p) +{ + print("%P\n", p); +} + +int +Pconv(Fmt *fp) +{ + char str[STRINGSZ], *s; + Prog *p; + int a; + + p = va_arg(fp->args, Prog*); + curp = p; + a = p->as; + if(a == ADATA || a == ADYNT || a == AINIT) + sprint(str, "(%ld) %A %D/%d,%D", + p->line, a, &p->from, p->reg, &p->to); + else{ + s = str; + s += sprint(s, "(%ld)", p->line); + if(p->reg == NREG) + sprint(s, " %A %D,%D", + a, &p->from, &p->to); + else + if(p->from.type != D_FREG) + sprint(s, " %A %D,R%d,%D", + a, &p->from, p->reg, &p->to); + else + sprint(s, " %A %D,F%d,%D", + a, &p->from, p->reg, &p->to); + } + return fmtstrcpy(fp, str); +} + +int +Aconv(Fmt *fp) +{ + char *s; + int a; + + a = va_arg(fp->args, int); + s = "???"; + if(a >= AXXX && a < ALAST) + s = anames[a]; + return fmtstrcpy(fp, s); +} + +int +Dconv(Fmt *fp) +{ + char str[STRINGSZ]; + Adr *a; + long v; + + a = va_arg(fp->args, Adr*); + switch(a->type) { + + default: + sprint(str, "GOK-type(%d)", a->type); + break; + + case D_NONE: + str[0] = 0; + if(a->name != D_NONE || a->reg != NREG || a->sym != S) + sprint(str, "%N(R%d)(NONE)", a, a->reg); + break; + + case D_CONST: + if(a->reg == NREG) + sprint(str, "$%N", a); + else + sprint(str, "$%N(R%d)", a, a->reg); + break; + + case D_VCONST: + sprint(str, "$%lld", *a->vval); + break; + + case D_OREG: + if(a->reg != NREG) + sprint(str, "%N(R%d)", a, a->reg); + else + sprint(str, "%N", a); + break; + + case D_REG: + sprint(str, "R%d", a->reg); + if(a->name != D_NONE || a->sym != S) + sprint(str, "%N(R%d)(REG)", a, a->reg); + break; + + case D_FREG: + sprint(str, "F%d", a->reg); + if(a->name != D_NONE || a->sym != S) + sprint(str, "%N(R%d)(REG)", a, a->reg); + break; + + case D_CTLREG: + sprint(str, "CSR(0x%lx)", a->offset); + break; + + case D_BRANCH: /* botch */ + if(curp->cond != P) { + v = curp->cond->pc + INITTEXT; + if(a->sym != S) + sprint(str, "%s+%.5lux(BRANCH)", a->sym->name, v); + else + sprint(str, "%.5lux(BRANCH)", v); + } else + if(a->sym != S) + sprint(str, "%s+%ld(APC)", a->sym->name, a->offset); + else + sprint(str, "%ld(APC)", a->offset); + break; + + case D_FCONST: + sprint(str, "$%e", ieeedtod(a->ieee)); + break; + + case D_SCONST: + sprint(str, "$\"%S\"", a->sval); + break; + } + return fmtstrcpy(fp, str); +} + +int +Nconv(Fmt *fp) +{ + char str[STRINGSZ]; + Adr *a; + Sym *s; + + a = va_arg(fp->args, Adr*); + s = a->sym; + switch(a->name) { + default: + sprint(str, "GOK-name(%d)", a->name); + break; + + case D_NONE: + sprint(str, "%ld", a->offset); + break; + + case D_EXTERN: + if(s == S) + sprint(str, "%ld(SB)", a->offset); + else + sprint(str, "%s+%ld(SB)", s->name, a->offset); + break; + + case D_STATIC: + if(s == S) + sprint(str, "<>+%ld(SB)", a->offset); + else + sprint(str, "%s<>+%ld(SB)", s->name, a->offset); + break; + + case D_AUTO: + if(s == S) + sprint(str, "%ld(SP)", a->offset); + else + sprint(str, "%s-%ld(SP)", s->name, -a->offset); + break; + + case D_PARAM: + if(s == S) + sprint(str, "%ld(FP)", a->offset); + else + sprint(str, "%s+%ld(FP)", s->name, a->offset); + break; + } + + return fmtstrcpy(fp, str); +} + +int +Sconv(Fmt *fp) +{ + int i, c; + char str[STRINGSZ], *p, *a; + + a = va_arg(fp->args, char*); + p = str; + for(i=0; i<sizeof(long); i++) { + c = a[i] & 0xff; + if(c >= 'a' && c <= 'z' || + c >= 'A' && c <= 'Z' || + c >= '0' && c <= '9' || + c == ' ' || c == '%') { + *p++ = c; + continue; + } + *p++ = '\\'; + switch(c) { + case 0: + *p++ = 'z'; + continue; + case '\\': + case '"': + *p++ = c; + continue; + case '\n': + *p++ = 'n'; + continue; + case '\t': + *p++ = 't'; + continue; + } + *p++ = (c>>6) + '0'; + *p++ = ((c>>3) & 7) + '0'; + *p++ = (c & 7) + '0'; + } + *p = 0; + return fmtstrcpy(fp, str); +} + +void +diag(char *fmt, ...) +{ + char buf[STRINGSZ], *tn; + va_list arg; + + tn = "??none??"; + if(curtext != P && curtext->from.sym != S) + tn = curtext->from.sym->name; + va_start(arg, fmt); + vseprint(buf, buf+sizeof(buf), fmt, arg); + va_end(arg); + print("%s: %s\n", tn, buf); + + nerrors++; + if(nerrors > 10) { + print("too many errors\n"); + errorexit(); + } +} diff --git a/utils/il/mkfile b/utils/il/mkfile new file mode 100644 index 00000000..4a655086 --- /dev/null +++ b/utils/il/mkfile @@ -0,0 +1,39 @@ +<../../mkconfig + +TARG=il # also makes jl (for riscv64) + +OFILES=\ + asm.$O\ + compress.$O\ + list.$O\ + noop.$O\ + obj.$O\ + optab.$O\ + pass.$O\ + span.$O\ + enam.$O\ + $TARGMODEL.$O\ + elf.$O\ + +HFILES=\ + l.h\ + ../ic/i.out.h\ + ../ld/elf.h\ + +LIBS=bio 9 # order is important + +BIN=$ROOT/$OBJDIR/bin + +<$ROOT/mkfiles/mkone-$SHELLTYPE + +CFLAGS= $CFLAGS -I../include -I. + +enam.$O: ../ic/enam.c + $CC $CFLAGS ../ic/enam.c +elf.$O: ../ld/elf.c + $CC $CFLAGS ../ld/elf.c + +$TARGMODEL.$O: ../ld/$TARGMODEL.c + $CC $CFLAGS ../ld/$TARGMODEL.c + +install:V: $BIN/jl diff --git a/utils/il/noop.c b/utils/il/noop.c new file mode 100644 index 00000000..05426130 --- /dev/null +++ b/utils/il/noop.c @@ -0,0 +1,341 @@ +#include "l.h" + +void +noops(void) +{ + Prog *p, *q, *q1; + int o, curframe, curbecome, maxbecome; + + /* + * find leaf subroutines + * become sizes + * frame sizes + * strip NOPs + * expand RET + * expand BECOME pseudo + */ + + if(debug['v']) + Bprint(&bso, "%5.2f noops\n", cputime()); + Bflush(&bso); + + curframe = 0; + curbecome = 0; + maxbecome = 0; + curtext = 0; + + q = P; + for(p = firstp; p != P; p = p->link) { + + /* find out how much arg space is used in this TEXT */ + if(p->to.type == D_OREG && p->to.reg == REGSP) + if(p->to.offset > curframe) + curframe = p->to.offset; + + switch(p->as) { + case ATEXT: + if(curtext && curtext->from.sym) { + curtext->from.sym->frame = curframe; + curtext->from.sym->become = curbecome; + if(curbecome > maxbecome) + maxbecome = curbecome; + } + curframe = 0; + curbecome = 0; + + p->mark |= LABEL|LEAF|SYNC; + if(p->link) + p->link->mark |= LABEL; + curtext = p; + break; + + /* too hard, just leave alone */ + case AMOVW: + case AMOV: + if(p->to.type == D_CTLREG) { + p->mark |= LABEL|SYNC; + break; + } + if(p->from.type == D_CTLREG) { + p->mark |= LABEL|SYNC; + break; + } + break; + + /* too hard, just leave alone */ + case ASYS: + case AWORD: + p->mark |= LABEL|SYNC; + break; + + case ARET: + /* special form of RET is BECOME */ + if(p->from.type == D_CONST) + if(p->from.offset > curbecome) + curbecome = p->from.offset; + + if(p->link != P) + p->link->mark |= LABEL; + break; + + case ANOP: + q1 = p->link; + q->link = q1; /* q is non-nop */ + q1->mark |= p->mark; + continue; + + case AJAL: + if(curtext != P) + curtext->mark &= ~LEAF; + + case AJMP: + case ABEQ: + case ABGE: + case ABGEU: + case ABLT: + case ABLTU: + case ABNE: + p->mark |= BRANCH; + + q1 = p->cond; + if(q1 != P) { + while(q1->as == ANOP) { + q1 = q1->link; + p->cond = q1; + } + if(!(q1->mark & LEAF)) + q1->mark |= LABEL; + } else + p->mark |= LABEL; + q1 = p->link; + if(q1 != P) + q1->mark |= LABEL; + break; + } + q = p; + } + + if(curtext && curtext->from.sym) { + curtext->from.sym->frame = curframe; + curtext->from.sym->become = curbecome; + if(curbecome > maxbecome) + maxbecome = curbecome; + } + + if(debug['b']) + print("max become = %d\n", maxbecome); + xdefine("ALEFbecome", STEXT, maxbecome); + + curtext = 0; + for(p = firstp; p != P; p = p->link) { + switch(p->as) { + case ATEXT: + curtext = p; + break; + case AJAL: + if(curtext != P && curtext->from.sym != S && curtext->to.offset >= 0) { + o = maxbecome - curtext->from.sym->frame; + if(o <= 0) + break; + /* calling a become or calling a variable */ + if(p->to.sym == S || p->to.sym->become) { + curtext->to.offset += o; + if(debug['b']) { + curp = p; + print("%D calling %D increase %d\n", + &curtext->from, &p->to, o); + } + } + } + break; + } + } + + for(p = firstp; p != P; p = p->link) { + o = p->as; + switch(o) { + case ATEXT: + curtext = p; + autosize = p->to.offset + ptrsize; + if(autosize <= ptrsize) { + if(curtext->mark & LEAF || autosize <= 0) { + p->to.offset = -ptrsize; + autosize = 0; + } else if(ptrsize == 4) { + p->to.offset = 4; + autosize = 8; + } + } + + q = p; + if(autosize) { + q = prg(); + q->as = AADD; + q->line = p->line; + 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 + if(!(curtext->mark & LEAF)) { + if(debug['v']) + Bprint(&bso, "save suppressed in: %s\n", + curtext->from.sym->name); + Bflush(&bso); + curtext->mark |= LEAF; + } + + if(curtext->mark & LEAF) { + if(curtext->from.sym) + curtext->from.sym->type = SLEAF; + break; + } + + q1 = prg(); + q1->as = thechar == 'j' ? AMOV : AMOVW; + q1->line = p->line; + q1->from.type = D_REG; + q1->from.reg = REGLINK; + q1->to.type = D_OREG; + q1->from.offset = 0; + q1->to.reg = REGSP; + + q1->link = q->link; + q->link = q1; + break; + + case ARET: + nocache(p); + if(p->from.type == D_CONST) + goto become; + if(curtext->mark & LEAF) { + if(!autosize) { + p->as = AJMP; + p->from = zprg.from; + p->to.type = D_OREG; + p->to.offset = 0; + p->to.reg = REGLINK; + p->mark |= BRANCH; + break; + } + + p->as = AADD; + p->from.type = D_CONST; + p->from.offset = autosize; + p->to.type = D_REG; + p->to.reg = REGSP; + + q = prg(); + q->as = AJMP; + q->line = p->line; + q->to.type = D_OREG; + q->to.offset = 0; + q->to.reg = REGLINK; + q->mark |= BRANCH; + + q->link = p->link; + p->link = q; + break; + } + p->as = thechar == 'j' ? AMOV : AMOVW; + p->from.type = D_OREG; + p->from.offset = 0; + p->from.reg = REGSP; + p->to.type = D_REG; + p->to.reg = REGLINK; + + q = p; + if(autosize) { + q = prg(); + q->as = AADD; + q->line = p->line; + 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; + } + + q1 = prg(); + q1->as = AJMP; + q1->line = p->line; + q1->to.type = D_OREG; + q1->to.offset = 0; + q1->to.reg = REGLINK; + q1->mark |= BRANCH; + + q1->link = q->link; + q->link = q1; + break; + + become: + if(curtext->mark & LEAF) { + + q = prg(); + q->line = p->line; + q->as = AJMP; + q->from = zprg.from; + q->to = p->to; + q->cond = p->cond; + q->link = p->link; + q->mark |= BRANCH; + p->link = q; + + p->as = AADD; + p->from = zprg.from; + p->from.type = D_CONST; + p->from.offset = autosize; + p->to = zprg.to; + p->to.type = D_REG; + p->to.reg = REGSP; + + break; + } + q = prg(); + q->line = p->line; + q->as = AJMP; + q->from = zprg.from; + q->to = p->to; + q->cond = p->cond; + q->link = p->link; + q->mark |= BRANCH; + p->link = q; + + q = prg(); + q->line = p->line; + 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; + + p->as = thechar == 'j' ? AMOV : AMOVW; + p->from = zprg.from; + p->from.type = D_OREG; + p->from.offset = 0; + p->from.reg = REGSP; + p->to = zprg.to; + p->to.type = D_REG; + p->to.reg = REGLINK; + + break; + } + } + + curtext = P; +} + +void +nocache(Prog *p) +{ + p->optab = 0; + p->from.class = 0; + p->to.class = 0; +} diff --git a/utils/il/obj.c b/utils/il/obj.c new file mode 100644 index 00000000..42cca8ac --- /dev/null +++ b/utils/il/obj.c @@ -0,0 +1,1517 @@ +#define EXTERN +#include "l.h" +#include <ar.h> + +#ifndef DEFAULT +#define DEFAULT '9' +#endif + +char *noname = "<none>"; +char symname[] = SYMDEF; +char thechar = 'i'; +char *thestring = "riscv"; + +/* + * -H1 is headerless + * -H2 -T4128 -R4096 is plan9 format + * -H5 -T0x4000A0 -R4 is elf executable + */ + +int little; + +void +main(int argc, char *argv[]) +{ + int c; + char *a; + + Binit(&bso, 1, OWRITE); + cout = -1; + listinit(); + outfile = 0; + nerrors = 0; + curtext = P; + HEADTYPE = -1; + INITTEXT = -1; + INITTEXTP = -1; + INITDAT = -1; + INITRND = -1; + INITENTRY = 0; + ptrsize = 4; + + a = strrchr(argv[0], '/'); + if(a == nil) + a = argv[0]; + else + a++; + if(*a == 'j') + thechar = 'j'; + ARGBEGIN { + default: + c = ARGC(); + if(c >= 0 && c < sizeof(debug)) + debug[c]++; + break; + case 'o': + outfile = ARGF(); + break; + case 'E': + a = ARGF(); + if(a) + INITENTRY = a; + break; + case 'T': + a = ARGF(); + if(a) + INITTEXT = atolwhex(a); + break; + case 'P': + a = ARGF(); + if(a) + INITTEXTP = atolwhex(a); + break; + case 'D': + a = ARGF(); + if(a) + INITDAT = atolwhex(a); + break; + case 'R': + a = ARGF(); + if(a) + INITRND = atolwhex(a); + break; + case 'H': + a = ARGF(); + if(a) + HEADTYPE = atolwhex(a); + break; + } ARGEND + + USED(argc); + + if(*argv == 0) { + diag("usage: %cl [-options] objects", thechar); + errorexit(); + } + if(debug['j']) + thechar = 'j'; + if(thechar == 'j'){ + thestring = "riscv64"; + ptrsize = 8; + } + if(!debug['9'] && !debug['U'] && !debug['B']) + debug[DEFAULT] = 1; + if(HEADTYPE == -1) { + if(debug['U']) + HEADTYPE = 5; + if(debug['B']) + HEADTYPE = 1; + if(debug['9']) + HEADTYPE = 2; + } + switch(HEADTYPE) { + default: + diag("unknown -H option"); + errorexit(); + + case 1: /* headerless */ + HEADR = 0; + if(INITTEXT == -1) + INITTEXT = 0; + if(INITDAT == -1) + INITDAT = 0; + if(INITRND == -1) + INITRND = 8; + break; + case 2: /* plan 9 */ + HEADR = 32L; + if(INITTEXT == -1) + INITTEXT = 4128; + if(INITDAT == -1) + INITDAT = 0; + if(INITRND == -1) + INITRND = 4096; + break; + case 5: /* elf executable */ + HEADR = rnd(52L+3*32L, 16); + if(INITTEXT == -1) + INITTEXT = 0; + if(INITDAT == -1) + INITDAT = 0; + if(INITRND == -1) + INITRND = 8; + break; + } + if (INITTEXTP == -1) + INITTEXTP = INITTEXT; + if(INITDAT != 0 && INITRND != 0) + print("warning: -D0x%llux is ignored because of -R0x%llux\n", + INITDAT, INITRND); + if(debug['v']) + Bprint(&bso, "HEADER = -H0x%d -T0x%llux -D0x%llux -R0x%llux\n", + HEADTYPE, INITTEXT, INITDAT, INITRND); + Bflush(&bso); + zprg.as = AGOK; + zprg.reg = NREG; + zprg.from.name = D_NONE; + zprg.from.type = D_NONE; + zprg.from.reg = NREG; + zprg.to = zprg.from; + nopalign = zprg; + nopalign.as = AADD; + nopalign.from.type = D_CONST; + nopalign.to.type = D_REG; + nopalign.to.reg = REGZERO; + nopalign.reg = REGZERO; + buildop(); + histgen = 0; + textp = P; + datap = P; + pc = 0; + dtype = 4; + if(outfile == 0) { + static char name[20]; + + snprint(name, sizeof name, "%c.out", thechar); + outfile = name; + } + cout = create(outfile, 1, 0775); + if(cout < 0) { + diag("%s: cannot create", outfile); + errorexit(); + } + nuxiinit(); + + version = 0; + cbp = buf.cbuf; + cbc = sizeof(buf.cbuf); + firstp = prg(); + lastp = firstp; + + if(INITENTRY == 0) { + INITENTRY = "_main"; + if(debug['p']) + INITENTRY = "_mainp"; + if(!debug['l']) + lookup(INITENTRY, 0)->type = SXREF; + } else + lookup(INITENTRY, 0)->type = SXREF; + + while(*argv) + objfile(*argv++); + if(!debug['l']) + loadlib(); + firstp = firstp->link; + if(firstp == P) + goto out; + patch(); + if(debug['p']) + if(debug['1']) + doprof1(); + else + doprof2(); + dodata(); + follow(); + if(firstp == P) + goto out; + noops(); + span(); + asmb(); + undef(); + +out: + if(debug['v']) { + Bprint(&bso, "%5.2f cpu time\n", cputime()); + Bprint(&bso, "%ld memory used\n", thunk); + Bprint(&bso, "%d sizeof adr\n", sizeof(Adr)); + Bprint(&bso, "%d sizeof prog\n", sizeof(Prog)); + } + Bflush(&bso); + errorexit(); +} + +void +loadlib(void) +{ + int i; + long h; + Sym *s; + +loop: + xrefresolv = 0; + for(i=0; i<libraryp; i++) { + if(debug['v']) + Bprint(&bso, "%5.2f autolib: %s (from %s)\n", cputime(), library[i], libraryobj[i]); + objfile(library[i]); + } + if(xrefresolv) + for(h=0; h<nelem(hash); h++) + for(s = hash[h]; s != S; s = s->link) + if(s->type == SXREF) + goto loop; +} + +void +errorexit(void) +{ + + if(nerrors) { + if(cout >= 0) + remove(outfile); + exits("error"); + } + exits(0); +} + +void +objfile(char *file) +{ + long off, esym, cnt, l; + int f, work; + Sym *s; + char magbuf[SARMAG]; + char name[100], pname[150]; + struct ar_hdr arhdr; + char *e, *start, *stop; + + if(file[0] == '-' && file[1] == 'l') { + if(debug['9']) + sprint(name, "/%s/lib/lib", thestring); + else + sprint(name, "/usr/%clib/lib", thechar); + strcat(name, file+2); + strcat(name, ".a"); + file = name; + } + if(debug['v']) + Bprint(&bso, "%5.2f ldobj: %s\n", cputime(), file); + Bflush(&bso); + f = open(file, 0); + if(f < 0) { + diag("cannot open file: %s", file); + errorexit(); + } + l = read(f, magbuf, SARMAG); + if(l != SARMAG || strncmp(magbuf, ARMAG, SARMAG)){ + /* load it as a regular file */ + l = seek(f, 0L, 2); + seek(f, 0L, 0); + ldobj(f, l, file); + close(f); + return; + } + + if(debug['v']) + Bprint(&bso, "%5.2f ldlib: %s\n", cputime(), file); + l = read(f, &arhdr, SAR_HDR); + if(l != SAR_HDR) { + diag("%s: short read on archive file symbol header", file); + goto out; + } + if(strncmp(arhdr.name, symname, strlen(symname))) { + diag("%s: first entry not symbol header", file); + goto out; + } + + esym = SARMAG + SAR_HDR + atolwhex(arhdr.size); + off = SARMAG + SAR_HDR; + + /* + * just bang the whole symbol file into memory + */ + seek(f, off, 0); + cnt = esym - off; + start = malloc(cnt + 10); + cnt = read(f, start, cnt); + if(cnt <= 0){ + close(f); + return; + } + stop = &start[cnt]; + memset(stop, 0, 10); + + work = 1; + while(work){ + if(debug['v']) + Bprint(&bso, "%5.2f library pass: %s\n", cputime(), file); + Bflush(&bso); + work = 0; + for(e = start; e < stop; e = strchr(e+5, 0) + 1) { + s = lookup(e+5, 0); + if(s->type != SXREF) + continue; + sprint(pname, "%s(%s)", file, s->name); + if(debug['v']) + Bprint(&bso, "%5.2f library: %s\n", cputime(), pname); + Bflush(&bso); + l = e[1] & 0xff; + l |= (e[2] & 0xff) << 8; + l |= (e[3] & 0xff) << 16; + l |= (e[4] & 0xff) << 24; + seek(f, l, 0); + l = read(f, &arhdr, SAR_HDR); + if(l != SAR_HDR) + goto bad; + if(strncmp(arhdr.fmag, ARFMAG, sizeof(arhdr.fmag))) + goto bad; + l = atolwhex(arhdr.size); + ldobj(f, l, pname); + if(s->type == SXREF) { + diag("%s: failed to load: %s", file, s->name); + errorexit(); + } + work = 1; + xrefresolv = 1; + } + } + return; + +bad: + diag("%s: bad or out of date archive", file); +out: + close(f); +} + +int +zaddr(uchar *p, Adr *a, Sym *h[]) +{ + int i, c; + long l; + Sym *s; + Auto *u; + + c = p[2]; + if(c < 0 || c > NSYM){ + print("sym out of range: %d\n", c); + p[0] = ALAST+1; + return 0; + } + a->type = p[0]; + a->reg = p[1]; + a->sym = h[c]; + a->name = p[3]; + c = 4; + + if(a->reg < 0 || a->reg > NREG) { + print("register out of range %d\n", a->reg); + p[0] = ALAST+1; + return 0; /* force real diagnostic */ + } + + switch(a->type) { + default: + print("unknown type %d\n", a->type); + p[0] = ALAST+1; + return 0; /* force real diagnostic */ + + case D_NONE: + case D_REG: + case D_FREG: + break; + + case D_CTLREG: + case D_BRANCH: + case D_OREG: + case D_CONST: + a->offset = p[4] | (p[5]<<8) | + (p[6]<<16) | (p[7]<<24); + c += 4; + break; + + case D_SCONST: + while(nhunk < NSNAME) + gethunk(); + a->sval = (char*)hunk; + nhunk -= NSNAME; + hunk += NSNAME; + + memmove(a->sval, p+4, NSNAME); + c += NSNAME; + break; + + case D_VCONST: + while(nhunk < 12) + gethunk(); + if((uintptr)hunk & 2){ + nhunk -= 4; + hunk += 4; + } + a->vval = (vlong*)hunk; + nhunk -= 8; + hunk += 8; + *(long*)a->vval = p[4] | (p[5]<<8) | + (p[6]<<16) | (p[7]<<24); + *((long*)a->vval + 1) = p[8] | (p[9]<<8) | + (p[10]<<16) | (p[11]<<24); + c += 8; + break; + + case D_FCONST: + while(nhunk < sizeof(Ieee)) + gethunk(); + a->ieee = (Ieee*)hunk; + nhunk -= NSNAME; + hunk += NSNAME; + + a->ieee->l = p[4] | (p[5]<<8) | + (p[6]<<16) | (p[7]<<24); + a->ieee->h = p[8] | (p[9]<<8) | + (p[10]<<16) | (p[11]<<24); + c += 8; + break; + } + s = a->sym; + if(s == S) + return c; + i = a->name; + if(i != D_AUTO && i != D_PARAM) + return c; + + l = a->offset; + for(u=curauto; u; u=u->link) + if(u->asym == s) + if(u->type == i) { + if(u->aoffset > l) + u->aoffset = l; + return c; + } + + while(nhunk < sizeof(Auto)) + gethunk(); + u = (Auto*)hunk; + nhunk -= sizeof(Auto); + hunk += sizeof(Auto); + + u->link = curauto; + curauto = u; + u->asym = s; + u->aoffset = l; + u->type = i; + return c; +} + +void +addlib(char *obj) +{ + char name[1024], comp[256], *p; + int i; + + if(histfrogp <= 0) + return; + + if(histfrog[0]->name[1] == '/') { + sprint(name, ""); + i = 1; + } else + if(histfrog[0]->name[1] == '.') { + sprint(name, "."); + i = 0; + } else { + if(debug['9']) + sprint(name, "/%s/lib", thestring); + else + sprint(name, "/usr/%clib", thechar); + i = 0; + } + + for(; i<histfrogp; i++) { + snprint(comp, sizeof comp, histfrog[i]->name+1); + for(;;) { + p = strstr(comp, "$O"); + if(p == 0) + break; + memmove(p+1, p+2, strlen(p+2)+1); + p[0] = thechar; + } + for(;;) { + p = strstr(comp, "$M"); + if(p == 0) + break; + if(strlen(comp)+strlen(thestring)-2+1 >= sizeof comp) { + diag("library component too long"); + return; + } + memmove(p+strlen(thestring), p+2, strlen(p+2)+1); + memmove(p, thestring, strlen(thestring)); + } + if(strlen(name) + strlen(comp) + 3 >= sizeof(name)) { + diag("library component too long"); + return; + } + strcat(name, "/"); + strcat(name, comp); + } + for(i=0; i<libraryp; i++) + if(strcmp(name, library[i]) == 0) + return; + if(libraryp == nelem(library)){ + diag("too many autolibs; skipping %s", name); + return; + } + + p = malloc(strlen(name) + 1); + strcpy(p, name); + library[libraryp] = p; + p = malloc(strlen(obj) + 1); + strcpy(p, obj); + libraryobj[libraryp] = p; + libraryp++; +} + +void +addhist(long line, int type) +{ + Auto *u; + Sym *s; + int i, j, k; + + u = malloc(sizeof(Auto)); + s = malloc(sizeof(Sym)); + s->name = malloc(2*(histfrogp+1) + 1); + + u->asym = s; + u->type = type; + u->aoffset = line; + u->link = curhist; + curhist = u; + + j = 1; + for(i=0; i<histfrogp; i++) { + k = histfrog[i]->value; + s->name[j+0] = k>>8; + s->name[j+1] = k; + j += 2; + } +} + +void +histtoauto(void) +{ + Auto *l; + + while(l = curhist) { + curhist = l->link; + l->link = curauto; + curauto = l; + } +} + +void +collapsefrog(Sym *s) +{ + int i; + + /* + * bad encoding of path components only allows + * MAXHIST components. if there is an overflow, + * first try to collapse xxx/.. + */ + for(i=1; i<histfrogp; i++) + if(strcmp(histfrog[i]->name+1, "..") == 0) { + memmove(histfrog+i-1, histfrog+i+1, + (histfrogp-i-1)*sizeof(histfrog[0])); + histfrogp--; + goto out; + } + + /* + * next try to collapse . + */ + for(i=0; i<histfrogp; i++) + if(strcmp(histfrog[i]->name+1, ".") == 0) { + memmove(histfrog+i, histfrog+i+1, + (histfrogp-i-1)*sizeof(histfrog[0])); + goto out; + } + + /* + * last chance, just truncate from front + */ + memmove(histfrog+0, histfrog+1, + (histfrogp-1)*sizeof(histfrog[0])); + +out: + histfrog[histfrogp-1] = s; +} + +void +nopout(Prog *p) +{ + p->as = ANOP; + p->from.type = D_NONE; + p->to.type = D_NONE; +} + +uchar* +readsome(int f, uchar *buf, uchar *good, uchar *stop, int max) +{ + int n; + + n = stop - good; + memmove(buf, good, stop - good); + stop = buf + n; + n = MAXIO - n; + if(n > max) + n = max; + n = read(f, stop, n); + if(n <= 0) + return 0; + return stop + n; +} + +void +ldobj(int f, long c, char *pn) +{ + long ipc; + Prog *p, *t; + uchar *bloc, *bsize, *stop; + Sym *h[NSYM], *s, *di; + int v, o, r, skip; + ulong sig; + static int files; + static char **filen; + char **nfilen; + + if((files&15) == 0){ + nfilen = malloc((files+16)*sizeof(char*)); + memmove(nfilen, filen, files*sizeof(char*)); + free(filen); + filen = nfilen; + } + filen[files++] = strdup(pn); + + bsize = buf.xbuf; + bloc = buf.xbuf; + di = S; + +newloop: + memset(h, 0, sizeof(h)); + version++; + histfrogp = 0; + ipc = pc; + skip = 0; + +loop: + if(c <= 0) + goto eof; + r = bsize - bloc; + if(r < 100 && r < c) { /* enough for largest prog */ + bsize = readsome(f, buf.xbuf, bloc, bsize, c); + if(bsize == 0) + goto eof; + bloc = buf.xbuf; + goto loop; + } + o = bloc[0]; /* as */ + if(o <= AXXX || o >= ALAST) { + diag("%s: line %ld: opcode out of range %d", pn, pc-ipc, o); + print(" probably not a .%c file\n", thechar); + errorexit(); + } + if(o == ANAME || o == ASIGNAME) { + sig = 0; + if(o == ASIGNAME){ + sig = bloc[1] | (bloc[2]<<8) | (bloc[3]<<16) | (bloc[4]<<24); + bloc += 4; + c -= 4; + } + stop = memchr(&bloc[3], 0, bsize-&bloc[3]); + if(stop == 0){ + bsize = readsome(f, buf.xbuf, bloc, bsize, c); + if(bsize == 0) + goto eof; + bloc = buf.xbuf; + stop = memchr(&bloc[3], 0, bsize-&bloc[3]); + if(stop == 0){ + fprint(2, "%s: name too long\n", pn); + errorexit(); + } + } + v = bloc[1]; /* type */ + o = bloc[2]; /* sym */ + bloc += 3; + c -= 3; + + r = 0; + if(v == D_STATIC) + r = version; + s = lookup((char*)bloc, r); + c -= &stop[1] - bloc; + bloc = stop + 1; + + if(debug['S'] && r == 0) + sig = 1729; + if(sig != 0){ + if(s->sig != 0 && s->sig != sig) + diag("incompatible type signatures %lux(%s) and %lux(%s) for %s", s->sig, filen[s->file], sig, pn, s->name); + s->sig = sig; + s->file = files-1; + } + + if(debug['W']) + print(" ANAME %s\n", s->name); + h[o] = s; + if((v == D_EXTERN || v == D_STATIC) && s->type == 0) + s->type = SXREF; + if(v == D_FILE) { + if(s->type != SFILE) { + histgen++; + s->type = SFILE; + s->value = histgen; + } + if(histfrogp < MAXHIST) { + histfrog[histfrogp] = s; + histfrogp++; + } else + collapsefrog(s); + } + goto loop; + } + + if(nhunk < sizeof(Prog)) + gethunk(); + p = (Prog*)hunk; + nhunk -= sizeof(Prog); + hunk += sizeof(Prog); + + p->as = o; + p->reg = bloc[1]; + p->line = bloc[2] | (bloc[3]<<8) | (bloc[4]<<16) | (bloc[5]<<24); + + r = zaddr(bloc+6, &p->from, h) + 6; + r += zaddr(bloc+r, &p->to, h); + bloc += r; + c -= r; + + if(p->reg < 0 || p->reg > NREG) + diag("register out of range %d", p->reg); + + p->link = P; + p->cond = P; + + if(debug['W']) + print("%P\n", p); + + if(thechar == 'i') { + switch(o) { + case AMOV: + if(p->from.type == D_OREG || p->to.type == D_OREG) + o = AMOVW; + break; + + case AMOVW: + case AMOVWU: + if(p->from.type == D_OREG || p->to.type == D_OREG) + o = AMOVW; + else + o = AMOV; + break; + + case ADIVW: + case ADIVUW: + o = ADIV; + break; + + case AREMW: + case AREMUW: + o = AREM; + break; + + case AADDW: o = AADD; break; + case ASUBW: o = ASUB; break; + case ASLLW: o = ASLL; break; + case ASRLW: o = ASRL; break; + case ASRAW: o = ASRA; break; + case AMULW: o = AMUL; break; + } + p->as = o; + } + + switch(o) { + case AHISTORY: + if(p->to.offset == -1) { + addlib(pn); + histfrogp = 0; + goto loop; + } + addhist(p->line, D_FILE); /* 'z' */ + if(p->to.offset) + addhist(p->to.offset, D_FILE1); /* 'Z' */ + histfrogp = 0; + goto loop; + + case AEND: + histtoauto(); + if(curtext != P) + curtext->to.autom = curauto; + curauto = 0; + curtext = P; + if(c) + goto newloop; + return; + + case AGLOBL: + s = p->from.sym; + if(s == S) { + diag("GLOBL must have a name\n%P", p); + errorexit(); + } + if(s->type == 0 || s->type == SXREF) { + s->type = SBSS; + s->value = 0; + } + if(s->type != SBSS) { + diag("redefinition: %s\n%P", s->name, p); + s->type = SBSS; + s->value = 0; + } + if(p->to.offset > s->value) + s->value = p->to.offset; + break; + + case ADYNT: + if(p->to.sym == S) { + diag("DYNT without a sym\n%P", p); + break; + } + di = p->to.sym; + p->reg = 4; + if(di->type == SXREF) { + if(debug['z']) + Bprint(&bso, "%P set to %d\n", p, dtype); + di->type = SCONST; + di->value = dtype; + dtype += 4; + } + if(p->from.sym == S) + break; + + p->from.offset = di->value; + p->from.sym->type = SDATA; + if(curtext == P) { + diag("DYNT not in text: %P", p); + break; + } + p->to.sym = curtext->from.sym; + p->to.type = D_CONST; + p->link = datap; + datap = p; + break; + + case AINIT: + if(p->from.sym == S) { + diag("INIT without a sym\n%P", p); + break; + } + if(di == S) { + diag("INIT without previous DYNT\n%P", p); + break; + } + p->from.offset = di->value; + p->from.sym->type = SDATA; + p->link = datap; + datap = p; + break; + + case ADATA: + if(p->from.sym == S) { + diag("DATA without a sym\n%P", p); + break; + } + p->link = datap; + datap = p; + break; + + case AGOK: + diag("unknown opcode\n%P", p); + p->pc = pc; + pc++; + break; + + case ATEXT: + if(curtext != P) { + histtoauto(); + curtext->to.autom = curauto; + curauto = 0; + } + skip = 0; + curtext = p; + autosize = p->to.offset; + if(autosize < 0) + autosize = -ptrsize; + else if(autosize != 0){ + autosize = (autosize + 3) & ~3; + if(thechar == 'j'){ + if((autosize & 4) != 0) + autosize += 4; + }else{ + if((autosize & 4) == 0) + autosize += 4; + } + } + p->to.offset = autosize; + s = p->from.sym; + if(s == S) { + diag("TEXT must have a name\n%P", p); + errorexit(); + } + if(s->type != 0 && s->type != SXREF) { + if(p->reg & DUPOK) { + skip = 1; + goto casedef; + } + diag("redefinition: %s\n%P", s->name, p); + } + s->type = STEXT; + s->value = pc; + lastp->link = p; + lastp = p; + p->pc = pc; + pc++; + if(textp == P) { + textp = p; + etextp = p; + goto loop; + } + etextp->cond = p; + etextp = p; + break; + + case ASUB: + case ASUBW: + if(p->from.type == D_CONST) + if(p->from.name == D_NONE) { + p->from.offset = -p->from.offset; + p->as = o == ASUB ? AADD : AADDW; + } + goto casedef; + + case AMOVF: + if(skip) + goto casedef; + + if(p->from.type == D_FCONST) { + /* size sb 9 max */ + sprint(literal, "$%lux", ieeedtof(p->from.ieee)); + s = lookup(literal, 0); + if(s->type == 0) { + s->type = SBSS; + s->value = 4; + t = prg(); + t->as = ADATA; + t->line = p->line; + t->from.type = D_OREG; + t->from.sym = s; + t->from.name = D_EXTERN; + t->reg = 4; + t->to = p->from; + t->link = datap; + datap = t; + } + p->from.type = D_OREG; + p->from.sym = s; + p->from.name = D_EXTERN; + p->from.offset = 0; + } + goto casedef; + + case AMOVD: + if(skip) + goto casedef; + + if(p->from.type == D_FCONST) { + /* size sb 18 max */ + sprint(literal, "$%lux.%lux", + p->from.ieee->l, p->from.ieee->h); + s = lookup(literal, 0); + if(s->type == 0) { + s->type = SBSS; + s->value = 8; + t = prg(); + t->as = ADATA; + t->line = p->line; + t->from.type = D_OREG; + t->from.sym = s; + t->from.name = D_EXTERN; + t->reg = 8; + t->to = p->from; + t->link = datap; + datap = t; + } + p->from.type = D_OREG; + p->from.sym = s; + p->from.name = D_EXTERN; + p->from.offset = 0; + } + goto casedef; + + case ABGT: + case ABGTU: + case ABLE: + case ABLEU: + if(p->from.type == D_REG){ + p->as = relrev(p->as); + if(p->reg == NREG) + p->reg = REGZERO; + else{ + r = p->from.reg; + p->from.reg = p->reg; + p->reg = r; + } + } + goto casedef; + + case ASGT: + case ASGTU: + r = p->from.reg; + p->from.reg = p->reg; + p->reg = r; + p->as = p->as == ASGT ? ASLT : ASLTU; + goto casedef; + + case AMOV: + if(p->from.type == D_CONST && + p->from.name == D_NONE && + p->from.reg != NREG && + p->from.reg != REGZERO){ + p->as = AADD; + p->reg = p->from.reg; + p->from.reg = NREG; + } + goto casedef; + +/* + case AMUL: + case AMULXSS: + case AMULXSU: + case AMULXUU: + if(!debug['m']) + goto casedef; + if(o != AMUL || p->from.type != D_REG){ + diag("unsupported multiply operation"); + if(!debug['v']) + prasm(p); + break; + } + print("transforming multiply"); + prasm(p); + if(p->reg == NREG) + p->reg = p->to.reg; + p->as = ACUSTOM; + p->from.type = D_CONST; + p->from.name = D_NONE; + p->from.offset = 0; + p->to.type = D_CONST; + p->to.name = D_NONE; + p->to.offset = p->from.reg<<16 | p->reg<<8 | p->to.reg; + p->from.reg = NREG; + p->to.reg = NREG; + prasm(p); + goto casedef; +*/ + + default: + casedef: + if(skip) + nopout(p); + + if(p->to.type == D_BRANCH) + p->to.offset += ipc; + lastp->link = p; + lastp = p; + p->pc = pc; + pc++; + break; + } + goto loop; + +eof: + diag("truncated object file: %s", pn); +} + +Sym* +lookup(char *symb, int v) +{ + Sym *s; + char *p; + long h; + int c, l; + + h = v; + for(p=symb; c = *p; p++) + h = h+h+h + c; + l = (p - symb) + 1; + if(h < 0) + h = ~h; + h %= NHASH; + for(s = hash[h]; s != S; s = s->link) + if(s->version == v) + if(memcmp(s->name, symb, l) == 0) + return s; + + while(nhunk < sizeof(Sym)) + gethunk(); + s = (Sym*)hunk; + nhunk -= sizeof(Sym); + hunk += sizeof(Sym); + + s->name = malloc(l); + memmove(s->name, symb, l); + + s->link = hash[h]; + s->type = 0; + s->version = v; + s->value = 0; + s->sig = 0; + hash[h] = s; + return s; +} + +Prog* +prg(void) +{ + Prog *p; + + while(nhunk < sizeof(Prog)) + gethunk(); + p = (Prog*)hunk; + nhunk -= sizeof(Prog); + hunk += sizeof(Prog); + + *p = zprg; + return p; +} + +void +gethunk(void) +{ + char *h; + long nh; + + nh = NHUNK; + if(thunk >= 5L*NHUNK) { + nh = 5L*NHUNK; + if(thunk >= 25L*NHUNK) + nh = 25L*NHUNK; + } + h = mysbrk(nh); + if(h == (char*)-1) { + diag("out of memory"); + errorexit(); + } + hunk = h; + nhunk = nh; + thunk += nh; +} + +void +doprof1(void) +{ + Sym *s; + long n; + Prog *p, *q; + + if(debug['v']) + Bprint(&bso, "%5.2f profile 1\n", cputime()); + Bflush(&bso); + s = lookup("__mcount", 0); + n = 1; + for(p = firstp->link; p != P; p = p->link) { + if(p->as == ATEXT) { + q = prg(); + q->line = p->line; + q->link = datap; + datap = q; + q->as = ADATA; + q->from.type = D_OREG; + q->from.name = D_EXTERN; + q->from.offset = n*4; + q->from.sym = s; + q->reg = 4; + q->to = p->from; + q->to.type = D_CONST; + + q = prg(); + q->line = p->line; + q->pc = p->pc; + q->link = p->link; + p->link = q; + p = q; + p->as = AMOVW; + p->from.type = D_OREG; + p->from.name = D_EXTERN; + p->from.sym = s; + p->from.offset = n*4 + 4; + p->to.type = D_REG; + p->to.reg = REGTMP; + + q = prg(); + q->line = p->line; + q->pc = p->pc; + q->link = p->link; + p->link = q; + p = q; + p->as = AADD; + p->from.type = D_CONST; + p->from.offset = 1; + p->to.type = D_REG; + p->to.reg = REGTMP; + + q = prg(); + q->line = p->line; + q->pc = p->pc; + q->link = p->link; + p->link = q; + p = q; + p->as = AMOVW; + p->from.type = D_REG; + p->from.reg = REGTMP; + p->to.type = D_OREG; + p->to.name = D_EXTERN; + p->to.sym = s; + p->to.offset = n*4 + 4; + + n += 2; + continue; + } + } + q = prg(); + q->line = 0; + q->link = datap; + datap = q; + + q->as = ADATA; + q->from.type = D_OREG; + q->from.name = D_EXTERN; + q->from.sym = s; + q->reg = 4; + q->to.type = D_CONST; + q->to.offset = n; + + s->type = SBSS; + s->value = n*4; +} + +void +doprof2(void) +{ + Sym *s2, *s4; + Prog *p, *q, *q2, *ps2, *ps4; + + if(debug['v']) + Bprint(&bso, "%5.2f profile 2\n", cputime()); + Bflush(&bso); + + if(debug['e']){ + s2 = lookup("_tracein", 0); + s4 = lookup("_traceout", 0); + }else{ + s2 = lookup("_profin", 0); + s4 = lookup("_profout", 0); + } + if(s2->type != STEXT || s4->type != STEXT) { + if(debug['e']) + diag("_tracein/_traceout not defined %d %d", s2->type, s4->type); + else + diag("_profin/_profout not defined"); + return; + } + + ps2 = P; + ps4 = P; + for(p = firstp; p != P; p = p->link) { + if(p->as == ATEXT) { + if(p->from.sym == s2) { + ps2 = p; + p->reg = 1; + } + if(p->from.sym == s4) { + ps4 = p; + p->reg = 1; + } + } + } + for(p = firstp; p != P; p = p->link) { + if(p->as == ATEXT) { + if(p->reg & NOPROF) { + for(;;) { + q = p->link; + if(q == P) + break; + if(q->as == ATEXT) + break; + p = q; + } + continue; + } + + /* + * CALL profin, R2 + */ + q = prg(); + q->line = p->line; + q->pc = p->pc; + q->link = p->link; + if(debug['e']){ /* embedded tracing */ + q2 = prg(); + p->link = q2; + q2->link = q; + + q2->line = p->line; + q2->pc = p->pc; + + q2->as = AJMP; + q2->to.type = D_BRANCH; + q2->to.sym = p->to.sym; + q2->cond = q->link; + }else + p->link = q; + p = q; + p->as = AJAL; + p->to.type = D_BRANCH; + p->cond = ps2; + p->to.sym = s2; + + continue; + } + if(p->as == ARET) { + /* + * RET (default) + */ + if(debug['e']){ /* embedded tracing */ + q = prg(); + q->line = p->line; + q->pc = p->pc; + q->link = p->link; + p->link = q; + p = q; + } + /* + * RET + */ + q = prg(); + q->as = ARET; + q->from = p->from; + q->to = p->to; + q->link = p->link; + p->link = q; + + /* + * CALL profout + */ + p->as = AJAL; + p->from = zprg.from; + p->to = zprg.to; + p->to.type = D_BRANCH; + p->cond = ps4; + p->to.sym = s4; + + p = q; + + continue; + } + } +} + +void +nuxiinit(void) +{ + int i, c; + + for(i=0; i<4; i++){ + c = find1(0x04030201L, i+1); + if(i < 2) + inuxi2[i] = c; + if(i < 1) + inuxi1[i] = c; + inuxi4[i] = c; + inuxi8[i] = c; + inuxi8[i+4] = c+4; + fnuxi4[i] = c; + fnuxi8[i] = c; + fnuxi8[i+4] = c+4; + } + if(debug['v']) { + Bprint(&bso, "inuxi = "); + for(i=0; i<1; i++) + Bprint(&bso, "%d", inuxi1[i]); + Bprint(&bso, " "); + for(i=0; i<2; i++) + Bprint(&bso, "%d", inuxi2[i]); + Bprint(&bso, " "); + for(i=0; i<4; i++) + Bprint(&bso, "%d", inuxi4[i]); + Bprint(&bso, " "); + for(i=0; i<8; i++) + Bprint(&bso, "%d", inuxi8[i]); + Bprint(&bso, "\nfnuxi = "); + for(i=0; i<8; i++) + Bprint(&bso, "%d", fnuxi8[i]); + Bprint(&bso, "\n"); + } + Bflush(&bso); +} + +int +find1(long l, int c) +{ + char *p; + int i; + + p = (char*)&l; + for(i=0; i<4; i++) + if(*p++ == c) + return i; + return 0; +} + +long +ieeedtof(Ieee *ieeep) +{ + int exp; + long v; + + if(ieeep->h == 0) + return 0; + exp = (ieeep->h>>20) & ((1L<<11)-1L); + exp -= (1L<<10) - 2L; + v = (ieeep->h & 0xfffffL) << 3; + v |= (ieeep->l >> 29) & 0x7L; + if((ieeep->l >> 28) & 1) { + v++; + if(v & 0x800000L) { + v = (v & 0x7fffffL) >> 1; + exp++; + } + } + if(exp <= -126 || exp >= 130) + diag("double fp to single fp overflow"); + v |= ((exp + 126) & 0xffL) << 23; + v |= ieeep->h & 0x80000000L; + return v; +} + +double +ieeedtod(Ieee *ieeep) +{ + Ieee e; + double fr; + int exp; + + if(ieeep->h & (1L<<31)) { + e.h = ieeep->h & ~(1L<<31); + e.l = ieeep->l; + return -ieeedtod(&e); + } + if(ieeep->l == 0 && ieeep->h == 0) + return 0; + fr = ieeep->l & ((1L<<16)-1L); + fr /= 1L<<16; + fr += (ieeep->l>>16) & ((1L<<16)-1L); + fr /= 1L<<16; + fr += (ieeep->h & (1L<<20)-1L) | (1L<<20); + fr /= 1L<<21; + exp = (ieeep->h>>20) & ((1L<<11)-1L); + exp -= (1L<<10) - 2L; + return ldexp(fr, exp); +} diff --git a/utils/il/optab.c b/utils/il/optab.c new file mode 100644 index 00000000..1bf79169 --- /dev/null +++ b/utils/il/optab.c @@ -0,0 +1,194 @@ +#include "l.h" + +Optab optab[] = +{ + /* add */ AADD, C_REG, C_REG, 0,1, 4, OOP, 0, 0, + /* sub */ ASUB, C_REG, C_REG, 0,5, 4, OOP, 0, 0x20, + /* sll */ ASLL, C_REG, C_REG, 0,0, 4, OOP, 1, 0, + /* slt */ ASLT, C_REG, C_REG, 0,0, 4, OOP, 2, 0, + /* sltu */ ASLTU, C_REG, C_REG, 0,0, 4, OOP, 3, 0, + /* xor */ AXOR, C_REG, C_REG, 0,5, 4, OOP, 4, 0, + /* srl */ ASRL, C_REG, C_REG, 0,0, 4, OOP, 5, 0, + /* sra */ ASRA, C_REG, C_REG, 0,0, 4, OOP, 5, 0x20, + /* or */ AOR, C_REG, C_REG, 0,5, 4, OOP, 6, 0, + /* and */ AAND, C_REG, C_REG, 0,5, 4, OOP, 7, 0, + /* mul */ AMUL, C_REG, C_REG, 0,0, 4, OOP, 0, 0x01, + /* mulh */ AMULH, C_REG, C_REG, 0,0, 4, OOP, 1, 0x01, + /* mulhsu */ AMULHSU, C_REG, C_REG, 0,0, 4, OOP, 2, 0x01, + /* mulhu */ AMULHU, C_REG, C_REG, 0,0, 4, OOP, 3, 0x01, + /* div */ ADIV, C_REG, C_REG, 0,0, 4, OOP, 4, 0x01, + /* divu */ ADIVU, C_REG, C_REG, 0,0, 4, OOP, 5, 0x01, + /* rem */ AREM, C_REG, C_REG, 0,0, 4, OOP, 6, 0x01, + /* remu */ AREMU, C_REG, C_REG, 0,0, 4, OOP, 7, 0x01, + + /* addw */ AADDW, C_REG, C_REG, 0,22, 4, OOP_32, 0, 0, + /* subw */ ASUBW, C_REG, C_REG, 0,22, 4, OOP_32, 0, 0x20, + /* sllw */ ASLLW, C_REG, C_REG, 0,0, 4, OOP_32, 1, 0, + /* srlw */ ASRLW, C_REG, C_REG, 0,0, 4, OOP_32, 5, 0, + /* sraw */ ASRAW, C_REG, C_REG, 0,0, 4, OOP_32, 5, 0x20, + + /* mulw */ AMULW, C_REG, C_REG, 0,0, 4, OOP_32, 0, 0x01, + /* divw */ ADIVW, C_REG, C_REG, 0,0, 4, OOP_32, 4, 0x01, + /* divuw */ ADIVUW, C_REG, C_REG, 0,0, 4, OOP_32, 5, 0x01, + /* remw */ AREMW, C_REG, C_REG, 0,0, 4, OOP_32, 6, 0x01, + /* remuw */ AREMUW, C_REG, C_REG, 0,0, 4, OOP_32, 7, 0x01, + + /* slli */ ASLL, C_SCON, C_REG, 1,8, 4, OOP_IMM, 1, 0, + /* srli */ ASRL, C_SCON, C_REG, 1,9, 4, OOP_IMM, 5, 0, + /* srai */ ASRA, C_SCON, C_REG, 1,9, 4, OOP_IMM, 5, 0x20, + + /* addi */ AADD, C_SCON, C_REG, 2,10, 4, OOP_IMM, 0, 0, + /* slti */ ASLT, C_SCON, C_REG, 2,0, 4, OOP_IMM, 2, 0, + /* sltiu */ ASLTU, C_SCON, C_REG, 2,0, 4, OOP_IMM, 3, 0, + /* xori */ AXOR, C_SCON, C_REG, 2,0, 4, OOP_IMM, 4, 0, + /* ori */ AOR, C_SCON, C_REG, 2,0, 4, OOP_IMM, 6, 0, + /* andi */ AAND, C_SCON, C_REG, 2,13, 4, OOP_IMM, 7, 0, + + /* addiw */ AADDW, C_SCON, C_REG, 2,23, 4, OOP_IMM_32, 0, 0, + /* slliw */ ASLLW, C_SCON, C_REG, 2,0, 4, OOP_IMM_32, 1, 0, + /* srliw */ ASRLW, C_SCON, C_REG, 2,0, 4, OOP_IMM_32, 5, 0, + /* sraiw */ ASRAW, C_SCON, C_REG, 2,0, 4, OOP_IMM_32, 5, 0x20, + + /* beq */ ABEQ, C_REG, C_SBRA, 3,14, 4, OBRANCH, 0, 0, + /* bne */ ABNE, C_REG, C_SBRA, 3,15, 4, OBRANCH, 1, 0, + /* blt */ ABLT, C_REG, C_SBRA, 3,0, 4, OBRANCH, 4, 0, + /* bge */ ABGE, C_REG, C_SBRA, 3,0, 4, OBRANCH, 5, 0, + /* bltu */ ABLTU, C_REG, C_SBRA, 3,0, 4, OBRANCH, 6, 0, + /* bgeu */ ABGEU, C_REG, C_SBRA, 3,0, 4, OBRANCH, 7, 0, + + /* jal */ AJAL, C_NONE, C_SBRA, 4,11, 4, OJAL, 0, REGLINK, + /* jal */ AJMP, C_NONE, C_SBRA, 4,12, 4, OJAL, 0, REGZERO, + /* jal */ AJAL, C_NONE, C_LBRA, 18,0, 8, OJALR, 0, REGLINK, + /* jal */ AJMP, C_NONE, C_LBRA, 18,0, 8, OJALR, 0, REGZERO, + /* jalr */ AJAL, C_NONE, C_SOREG, 5,3, 4, OJALR, 0, REGLINK, + /* jalr */ AJMP, C_NONE, C_SOREG, 5,4, 4, OJALR, 0, REGZERO, + + /* sb */ AMOVB, C_ZREG, C_SOREG, 6,0, 4, OSTORE, 0, 0, + /* sb */ AMOVBU, C_ZREG, C_SOREG, 6,0, 4, OSTORE, 0, 0, + /* sh */ AMOVH, C_ZREG, C_SOREG, 6,0, 4, OSTORE, 1, 0, + /* sw */ AMOVW, C_ZREG, C_SOREG, 6,19, 4, OSTORE, 2, 0, + /* sd */ AMOV, C_ZREG, C_SOREG, 6,25, 4, OSTORE, 3, 0, + /* fsw */ AMOVF, C_FREG, C_SOREG, 6,20, 4, OSTORE_FP, 2, 0, + /* fsd */ AMOVD, C_FREG, C_SOREG, 6,21, 4, OSTORE_FP, 3, 0, + + /* sb */ AMOVB, C_ZREG, C_LEXT, 12,0, 8, OSTORE, 0, 0, + /* sb */ AMOVBU, C_ZREG, C_LEXT, 12,0, 8, OSTORE, 0, 0, + /* sh */ AMOVH, C_ZREG, C_LEXT, 12,0, 8, OSTORE, 1, 0, + /* sw */ AMOVW, C_ZREG, C_LEXT, 12,0, 8, OSTORE, 2, 0, + /* sd */ AMOV, C_ZREG, C_LEXT, 12,0, 8, OSTORE, 3, 0, + /* fsw */ AMOVF, C_FREG, C_LEXT, 12,0, 8, OSTORE_FP, 2, 0, + /* fsd */ AMOVD, C_FREG, C_LEXT, 12,0, 8, OSTORE_FP, 3, 0, + + /* sb */ AMOVB, C_ZREG, C_LOREG, 15,0, 12, OSTORE, 0, 0, + /* sb */ AMOVBU, C_ZREG, C_LOREG, 15,0, 12, OSTORE, 0, 0, + /* sh */ AMOVH, C_ZREG, C_LOREG, 15,0, 12, OSTORE, 1, 0, + /* sw */ AMOVW, C_ZREG, C_LOREG, 15,0, 12, OSTORE, 2, 0, + /* sd */ AMOV, C_ZREG, C_LOREG, 15,0, 12, OSTORE, 3, 0, + /* fsw */ AMOVF, C_FREG, C_LOREG, 15,0, 12, OSTORE_FP, 2, 0, + /* fsd */ AMOVD, C_FREG, C_LOREG, 15,0, 12, OSTORE_FP, 3, 0, + + /* lb */ AMOVB, C_SOREG, C_REG, 7,0, 4, OLOAD, 0, 0, + /* lh */ AMOVH, C_SOREG, C_REG, 7,0, 4, OLOAD, 1, 0, + /* lw */ AMOVW, C_SOREG, C_REG, 7,16, 4, OLOAD, 2, 0, + /* ld */ AMOV, C_SOREG, C_REG, 7,24, 4, OLOAD, 3, 0, + /* lbu */ AMOVBU, C_SOREG, C_REG, 7,0, 4, OLOAD, 4, 0, + /* lhu */ AMOVHU, C_SOREG, C_REG, 7,0, 4, OLOAD, 5, 0, + /* lwu */ AMOVWU, C_SOREG, C_REG, 7,0, 4, OLOAD, 6, 0, + /* flw */ AMOVF, C_SOREG, C_FREG, 7,17, 4, OLOAD_FP, 2, 0, + /* fld */ AMOVD, C_SOREG, C_FREG, 7,18, 4, OLOAD_FP, 3, 0, + + /* lui */ AMOV, C_UCON, C_REG, 8,7, 4, OLUI, 0, 0, + + /* lb */ AMOVB, C_LEXT, C_REG, 13,0, 8, OLOAD, 0, 0, + /* lh */ AMOVH, C_LEXT, C_REG, 13,0, 8, OLOAD, 1, 0, + /* lw */ AMOVW, C_LEXT, C_REG, 13,0, 8, OLOAD, 2, 0, + /* ld */ AMOV, C_LEXT, C_REG, 13,0, 8, OLOAD, 3, 0, + /* lbu */ AMOVBU, C_LEXT, C_REG, 13,0, 8, OLOAD, 4, 0, + /* lhu */ AMOVHU, C_LEXT, C_REG, 13,0, 8, OLOAD, 5, 0, + /* lwu */ AMOVWU, C_LEXT, C_REG, 13,0, 8, OLOAD, 6, 0, + /* flw */ AMOVF, C_LEXT, C_FREG, 13,0, 8, OLOAD_FP, 2, 0, + /* fld */ AMOVD, C_LEXT, C_FREG, 13,0, 8, OLOAD_FP, 3, 0, + + /* lb */ AMOVB, C_LOREG, C_REG, 16,0, 12, OLOAD, 0, 0, + /* lh */ AMOVH, C_LOREG, C_REG, 16,0, 12, OLOAD, 1, 0, + /* lw */ AMOVW, C_LOREG, C_REG, 16,0, 12, OLOAD, 2, 0, + /* ld */ AMOV, C_LOREG, C_REG, 16,0, 12, OLOAD, 3, 0, + /* lbu */ AMOVBU, C_LOREG, C_REG, 16,0, 12, OLOAD, 4, 0, + /* lhu */ AMOVHU, C_LOREG, C_REG, 16,0, 12, OLOAD, 5, 0, + /* lwu */ AMOVWU, C_LOREG, C_REG, 16,0, 12, OLOAD, 6, 0, + /* flw */ AMOVF, C_LOREG, C_FREG, 16,0, 12, OLOAD_FP, 2, 0, + /* fld */ AMOVD, C_LOREG, C_FREG, 16,0, 12, OLOAD_FP, 3, 0, + + /* addi */ AMOVW, C_SCON, C_REG, 11,6, 4, OOP_IMM, 0, 0, + /* addi */ AMOVW, C_SECON, C_REG, 11,0, 4, OOP_IMM, 0, 0, + /* addi */ AMOVW, C_SACON, C_REG, 11,0, 4, OOP_IMM, 0, 0, + /* lui,addi */ AMOVW, C_LCON, C_REG, 9,0, 8, OOP_IMM, 0, 0, + /* lui,addi */ AMOVW, C_LECON, C_REG, 9,0, 8, OOP_IMM, 0, 0, + /* ",",add */ AMOVW, C_LACON, C_REG, 14,0, 12, OOP_IMM, 0, 0, + + /* add */ AMOV, C_REG, C_REG, 0,2, 4, OOP, 0, 0, + /* addi */ AMOV, C_SCON, C_REG, 11,6, 4, OOP_IMM, 0, 0, + /* addi */ AMOV, C_SECON, C_REG, 11,0, 4, OOP_IMM, 0, 0, + /* addi */ AMOV, C_SACON, C_REG, 11,0, 4, OOP_IMM, 0, 0, + /* lui,addi */ AMOV, C_LCON, C_REG, 9,0, 8, OOP_IMM, 0, 0, + /* lui,addi */ AMOV, C_LECON, C_REG, 20,0, 8, OOP_IMM, 0, 0, + /* lui,s[rl]ai */ AMOV, C_VCON, C_REG, 21,0, 8, OOP_IMM, 5, 0x20, + /* ",",add */ AMOV, C_LACON, C_REG, 14,0, 12, OOP_IMM, 0, 0, + /* ",",add */ AADD, C_LCON, C_REG, 14,0, 12, OOP_IMM, 0, 0, + /* ",",and */ AAND, C_LCON, C_REG, 14,0, 12, OOP_IMM, 7, 0, + /* ",",or */ AOR, C_LCON, C_REG, 14,0, 12, OOP_IMM, 6, 0, + /* ",",xor */ AXOR, C_LCON, C_REG, 14,0, 12, OOP_IMM, 4, 0, + + /* addiw */ AMOVW, C_REG, C_REG, 19,23, 4, OOP_IMM_32, 0, 0, + /* andi */ AMOVBU, C_ZREG, C_REG, 10,0, 4, OOP_IMM, 7, 0xFF, + /* slli,srli */ AMOVHU, C_ZREG, C_REG, 10,0, 8, OOP_IMM, 5, 16, + /* slli,srli */ AMOVWU, C_ZREG, C_REG, 10,0, 8, OOP_IMM, 5, 0, + /* slli,srai */ AMOVB, C_ZREG, C_REG, 10,0, 8, OOP_IMM, 5, 24+(0x20<<5), + /* slli,srai */ AMOVH, C_ZREG, C_REG, 10,0, 8, OOP_IMM, 5, 16+(0x20<<5), + + ASYS, C_NONE, C_SCON, 24,0, 4, OSYSTEM, 0, 0, + ACSRRW, C_CTLREG, C_REG, 22,0, 4, OSYSTEM, 1, 0, + ACSRRS, C_CTLREG, C_REG, 22,0, 4, OSYSTEM, 2, 0, + ACSRRC, C_CTLREG, C_REG, 22,0, 4, OSYSTEM, 3, 0, + ACSRRWI, C_CTLREG, C_REG, 22,0, 4, OSYSTEM, 5, 0, + ACSRRSI, C_CTLREG, C_REG, 22,0, 4, OSYSTEM, 6, 0, + ACSRRCI, C_CTLREG, C_REG, 22,0, 4, OSYSTEM, 7, 0, + + AADDF, C_FREG, C_FREG, 0,0, 4, OOP_FP, 0x7, 0x00, + ASUBF, C_FREG, C_FREG, 0,0, 4, OOP_FP, 0x7, 0x04, + AMULF, C_FREG, C_FREG, 0,0, 4, OOP_FP, 0x7, 0x08, + ADIVF, C_FREG, C_FREG, 0,0, 4, OOP_FP, 0x7, 0x0c, + AADDD, C_FREG, C_FREG, 0,0, 4, OOP_FP, 0x7, 0x01, + ASUBD, C_FREG, C_FREG, 0,0, 4, OOP_FP, 0x7, 0x05, + AMULD, C_FREG, C_FREG, 0,0, 4, OOP_FP, 0x7, 0x09, + ADIVD, C_FREG, C_FREG, 0,0, 4, OOP_FP, 0x7, 0x0d, + + ACMPEQF, C_FREG, C_REG, 0,0, 4, OOP_FP, 0x2, 0x50, + ACMPLEF, C_FREG, C_REG, 0,0, 4, OOP_FP, 0x0, 0x50, + ACMPLTF, C_FREG, C_REG, 0,0, 4, OOP_FP, 0x1, 0x50, + ACMPEQD, C_FREG, C_REG, 0,0, 4, OOP_FP, 0x2, 0x51, + ACMPLED, C_FREG, C_REG, 0,0, 4, OOP_FP, 0x0, 0x51, + ACMPLTD, C_FREG, C_REG, 0,0, 4, OOP_FP, 0x1, 0x51, + + /* float move */ AMOVF, C_FREG, C_FREG, 0,0, 4, OOP_FP, 0x0, 0x10, + /* dbl move */ AMOVD, C_FREG, C_FREG, 0,0, 4, OOP_FP, 0x0, 0x11, + + /* float->dbl */ AMOVFD, C_FREG, C_FREG, 17,0, 4, OOP_FP, 0x0, 0x21, + /* dbl->float */ AMOVDF, C_FREG, C_FREG, 17,0, 4, OOP_FP, 0x1, 0x20, + /* float->int */ AMOVFW, C_FREG, C_REG, 17,0, 4, OOP_FP, 0x0, 0x60, + /* dbl->int */ AMOVDW, C_FREG, C_REG, 17,0, 4, OOP_FP, 0x0, 0x61, + /* int->float */ AMOVWF, C_REG, C_FREG, 17,0, 4, OOP_FP, 0x0, 0x68, + /* uint->float */ AMOVUF, C_REG, C_FREG, 17,0, 4, OOP_FP, 0x1, 0x68, + /* int->dbl */ AMOVWD, C_REG, C_FREG, 17,0, 4, OOP_FP, 0x0, 0x69, + /* uint->dbl */ AMOVUD, C_REG, C_FREG, 17,0, 4, OOP_FP, 0x1, 0x69, + /* float->vlong*/ AMOVFV, C_FREG, C_REG, 17,0, 4, OOP_FP, 0x2, 0x60, + /* dbl->vlong */ AMOVDV, C_FREG, C_REG, 17,0, 4, OOP_FP, 0x2, 0x61, + /* vlong->float*/ AMOVVF, C_REG, C_FREG, 17,0, 4, OOP_FP, 0x2, 0x68, + /* uvlong->float*/ AMOVUVF, C_REG, C_FREG, 17,0, 4, OOP_FP, 0x3, 0x68, + /* vlong->dbl */ AMOVVD, C_REG, C_FREG, 17,0, 4, OOP_FP, 0x2, 0x69, + /* uvlong->dbl */ AMOVUVD, C_REG, C_FREG, 17,0, 4, OOP_FP, 0x3, 0x69, + + /* - */ AWORD, C_NONE, C_LCON, 25,0, 4, 0, 0, 0, + /* - */ ATEXT, C_LEXT, C_LCON, 26,0, 0, 0, 0, 0, + /* - */ AXXX, C_NONE, C_NONE, 0,0, 0, 0, 0, 0, +}; diff --git a/utils/il/pass.c b/utils/il/pass.c new file mode 100644 index 00000000..8d3b435b --- /dev/null +++ b/utils/il/pass.c @@ -0,0 +1,589 @@ +#include "l.h" + +void +dodata(void) +{ + int i, t; + Sym *s; + Prog *p, *p1; + long orig, orig1, v; + int odd; + long long vv; + + if(debug['v']) + Bprint(&bso, "%5.2f dodata\n", cputime()); + Bflush(&bso); + for(p = datap; p != P; p = p->link) { + s = p->from.sym; + if(p->as == ADYNT || p->as == AINIT) + s->value = dtype; + if(s->type == SBSS) + s->type = SDATA; + if(s->type != SDATA) + diag("initialize non-data (%d): %s\n%P", + s->type, s->name, p); + v = p->from.offset + p->reg; + if(v > s->value) + diag("initialize bounds (%ld): %s\n%P", + s->value, s->name, p); + } + + if(debug['t']) { + /* + * pull out string constants + */ + for(p = datap; p != P; p = p->link) { + s = p->from.sym; + if(p->to.type == D_SCONST) + s->type = SSTRING; + } + } + + /* + * pass 1 + * assign 'small' variables to data segment + * (rational is that data segment is more easily + * addressed through offset on SB) + */ + odd = 4; + orig = 0; + for(i=0; i<NHASH; i++) + for(s = hash[i]; s != S; s = s->link) { + t = s->type; + if(t != SDATA && t != SBSS) + continue; + v = s->value; + if(v == 0) { + diag("%s: no size", s->name); + v = 1; + } + while(v & 3) + v++; + s->value = v; + if(v > MINSIZ) + continue; + if(v >= 8 && (orig & odd) != 0) + orig += odd; + s->value = orig; + orig += v; + s->type = SDATA1; + } + orig1 = orig; + + /* + * pass 2 + * assign 'data' variables to data segment + */ + for(i=0; i<NHASH; i++) + for(s = hash[i]; s != S; s = s->link) { + t = s->type; + if(t != SDATA) { + if(t == SDATA1) + s->type = SDATA; + continue; + } + v = s->value; + if((orig & odd) != 0) + orig += odd; + s->value = orig; + orig += v; + s->type = SDATA1; + } + + while(orig & 7) + orig++; + datsize = orig; + + /* + * pass 3 + * everything else to bss segment + */ + for(i=0; i<NHASH; i++) + for(s = hash[i]; s != S; s = s->link) { + if(s->type != SBSS) + continue; + v = s->value; + if((orig & odd) != 0) + orig += odd; + s->value = orig; + orig += v; + } + while(orig & 7) + orig++; + bsssize = orig-datsize; + + /* + * pass 4 + * add literals to all large values. + * at this time: + * small data is allocated DATA + * large data is allocated DATA1 + * large bss is allocated BSS + * the new literals are loaded between + * small data and large data. + */ + orig = 0; + for(p = firstp; p != P; p = p->link) { + if(p->as != AMOV && p->as != AMOVW) + continue; + if(p->from.type != D_CONST && p->from.type != D_VCONST) + continue; + if(s = p->from.sym) { + if(!debug['r']) + continue; + t = s->type; + if(t != SDATA && t != SDATA1 && t != SBSS) + continue; + t = p->from.name; + if(t != D_EXTERN && t != D_STATIC) + continue; + v = s->value + p->from.offset; + if(v >= 0 && v <= 2*BIG) + continue; + if(!strcmp(s->name, "setSB")) + continue; + /* size should be 19 max */ + if(strlen(s->name) >= 10) /* has loader address */ + sprint(literal, "$%p.%lux", s, p->from.offset); + else + sprint(literal, "$%s.%d.%lux", s->name, s->version, p->from.offset); + } else { + if(p->from.type == D_VCONST){ + vv = *p->from.vval; + if( 0 && (v = vconshift(vv)) >= 0 && !debug['r']){ + if(v < 12) + vv <<= 12 - v; + else + vv >>= v - 12; + p->from.type = D_CONST; + p->from.offset = (long)vv & ~0xFFF; + p1 = prg(); + p1->line = p->line; + p1->to = p->to; + p1->from.type = D_CONST; + if(v < 12) { + p1->as = ASRA; + p1->from.offset = 12 - v; + } else { + p1->as = ASLL; + p1->from.offset = v - 12; + } + p1->link = p->link; + p->link = p1; + continue; + } + sprint(literal, "$%llux", vv); + } else { + if(!debug['r']) + continue; + if(p->from.name != D_NONE) + continue; + if(p->from.reg != NREG) + continue; + v = p->from.offset; + if(v >= -BIG && v < BIG) + continue; + if((v & (BIG-1)) == 0) + continue; + /* size should be 9 max */ + sprint(literal, "$%lux", v); + } + } + s = lookup(literal, 0); + if(s->type == 0) { + s->type = SDATA; + if(p->from.type == D_VCONST && (orig & odd) != 0) + orig += odd; + s->value = orig1+orig; + p1 = prg(); + p1->line = p->line; + p1->as = ADATA; + p1->from.type = D_OREG; + p1->from.sym = s; + p1->from.name = D_EXTERN; + p1->reg = p->from.type == D_VCONST ? 8 : 4; + p1->to = p->from; + p1->link = datap; + orig += p1->reg; + datap = p1; + } + if(s->type != SDATA) + diag("literal not data: %s", s->name); + if(thechar == 'i' && p->as == AMOV) + p->as = AMOVW; + p->from.type = D_OREG; + p->from.sym = s; + p->from.name = D_EXTERN; + p->from.offset = 0; + continue; + } + while(orig & 7) + orig++; + + /* + * pass 5 + * re-adjust offsets + */ + for(i=0; i<NHASH; i++) + for(s = hash[i]; s != S; s = s->link) { + t = s->type; + if(t == SBSS) { + s->value += orig; + continue; + } + if(t == SDATA1) { + s->type = SDATA; + s->value += orig; + continue; + } + } + datsize += orig; + xdefine("setSB", SDATA, 0L+BIG); + xdefine("bdata", SDATA, 0L); + xdefine("edata", SDATA, datsize); + xdefine("end", SBSS, datsize+bsssize); + xdefine("etext", STEXT, 0L); +} + +void +undef(void) +{ + int i; + Sym *s; + + for(i=0; i<NHASH; i++) + for(s = hash[i]; s != S; s = s->link) + if(s->type == SXREF) + diag("%s: not defined", s->name); +} + +int +relinv(int a) +{ + + switch(a) { + case ABEQ: return ABNE; + case ABNE: return ABEQ; + + case ABLT: return ABGE; + case ABGE: return ABLT; + + case ABLTU: return ABGEU; + case ABGEU: return ABLTU; + } + return 0; +} + +int +relrev(int a) +{ + switch (a) { + case ABGT: return ABLT; + case ABGTU: return ABLTU; + case ABLE: return ABGE; + case ABLEU: return ABGEU; + } + return 0; +} + +void +follow(void) +{ + if(debug['v']) + Bprint(&bso, "%5.2f follow\n", cputime()); + Bflush(&bso); + + firstp = prg(); + lastp = firstp; + xfol(textp); + + firstp = firstp->link; + lastp->link = P; +} + +void +xfol(Prog *p) +{ + Prog *q, *r; + int a, b, i; + +loop: + if(p == P) + return; + a = p->as; + if(a == ATEXT) + curtext = p; + if(a == AJMP) { + q = p->cond; + if(q != P) { + p->mark |= FOLL; + p = q; + if(!(p->mark & FOLL)) + goto loop; + } + } + if(p->mark & FOLL) { + for(i=0,q=p; i<4; i++,q=q->link) { + if(q == lastp) + break; + b = 0; /* set */ + a = q->as; + if(a == ANOP) { + i--; + continue; + } + if(a == AJMP || a == ARET) + goto copy; + if(!q->cond || (q->cond->mark&FOLL)) + continue; + b = relinv(a); + if(!b) + continue; + copy: + for(;;) { + r = prg(); + *r = *p; + if(!(r->mark&FOLL)) + print("cant happen 1\n"); + r->mark |= FOLL; + if(p != q) { + p = p->link; + lastp->link = r; + lastp = r; + continue; + } + lastp->link = r; + lastp = r; + if(a == AJMP || a == ARET) + return; + r->as = b; + r->cond = p->link; + r->link = p->cond; + if(!(r->link->mark&FOLL)) + xfol(r->link); + if(!(r->cond->mark&FOLL)) + print("cant happen 2\n"); + return; + } + } + a = AJMP; + q = prg(); + q->as = a; + q->line = p->line; + q->to.type = D_BRANCH; + q->to.offset = p->pc; + q->cond = p; + p = q; + } + p->mark |= FOLL; + lastp->link = p; + lastp = p; + if(a == AJMP || a == ARET){ + return; + } + if(p->cond != P) + if(a != AJAL && p->link != P) { + xfol(p->link); + p = p->cond; + if(p == P || (p->mark&FOLL)) + return; + goto loop; + } + p = p->link; + goto loop; +} + +void +patch(void) +{ + long c, vexit; + Prog *p, *q; + Sym *s; + int a; + + if(debug['v']) + Bprint(&bso, "%5.2f patch\n", cputime()); + Bflush(&bso); + mkfwd(); + s = lookup("exit", 0); + vexit = s->value; + for(p = firstp; p != P; p = p->link) { + a = p->as; + if(a == ATEXT) + curtext = p; + if((a == AJAL || a == AJMP || a == ARET) && + p->to.type != D_BRANCH && p->to.sym != S) { + s = p->to.sym; + if(s->type != STEXT) { + diag("undefined: %s\n%P", s->name, p); + s->type = STEXT; + s->value = vexit; + } + p->to.offset = s->value; + p->to.type = D_BRANCH; + } + if(p->to.type != D_BRANCH) + 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(q == P) { + diag("branch out of range %ld => %ld\n%P", p->pc, c, p); + p->to.type = D_NONE; + } + p->cond = q; + } + + for(p = firstp; p != P; p = p->link) { + if(p->as == ATEXT) + curtext = p; + if(p->cond != P) { + p->cond = brloop(p->cond); + if(p->cond != P) + if(p->to.type == D_BRANCH) + p->to.offset = p->cond->pc; + } + } +} + +#define LOG 5 +void +mkfwd(void) +{ + Prog *p; + long dwn[LOG], cnt[LOG], i; + Prog *lst[LOG]; + + for(i=0; i<LOG; i++) { + if(i == 0) + cnt[i] = 1; else + cnt[i] = LOG * cnt[i-1]; + dwn[i] = 1; + lst[i] = P; + } + i = 0; + for(p = firstp; p != P; p = p->link) { + if(p->as == ATEXT) + curtext = p; + i--; + if(i < 0) + i = LOG-1; + p->forwd = P; + dwn[i]--; + if(dwn[i] <= 0) { + dwn[i] = cnt[i]; + if(lst[i] != P) + lst[i]->forwd = p; + lst[i] = p; + } + } +} + +Prog* +brloop(Prog *p) +{ + Prog *q; + int c; + + for(c=0; p!=P;) { + if(p->as != AJMP) + return p; + q = p->cond; + if(q <= p) { + c++; + if(q == p || c > 5000) + break; + } + p = q; + } + return P; +} + +xlong +atolwhex(char *s) +{ + xlong n; + int f; + + n = 0; + f = 0; + while(*s == ' ' || *s == '\t') + s++; + if(*s == '-' || *s == '+') { + if(*s++ == '-') + f = 1; + while(*s == ' ' || *s == '\t') + s++; + } + if(s[0]=='0' && s[1]){ + if(s[1]=='x' || s[1]=='X'){ + s += 2; + for(;;){ + if(*s >= '0' && *s <= '9') + n = n*16 + *s++ - '0'; + else if(*s >= 'a' && *s <= 'f') + n = n*16 + *s++ - 'a' + 10; + else if(*s >= 'A' && *s <= 'F') + n = n*16 + *s++ - 'A' + 10; + else + break; + } + } else + while(*s >= '0' && *s <= '7') + n = n*8 + *s++ - '0'; + } else + while(*s >= '0' && *s <= '9') + n = n*10 + *s++ - '0'; + if(f) + n = -n; + return n; +} + +vlong +rnd(vlong v, vlong r) +{ + vlong c; + + if(r <= 0) + return v; + v += r - 1; + c = v % r; + if(c < 0) + c += r; + v -= c; + return v; +} + +int +vconshift(vlong constant) +{ + vlong orig; + long w; + int shift; + + orig = constant; + if(constant == 0) + return -1; + shift = 12; + while((constant & 0xFFF) != 0){ + shift--; + constant <<= 1; + } + while((constant & 0x1000) == 0){ + shift++; + constant >>= 1; + } + w = (long)constant; + constant = shift > 12? (vlong)w << shift - 12 : (vlong)w >> 12 - shift; + if(constant != orig) + return -1; + return shift; +} diff --git a/utils/il/span.c b/utils/il/span.c new file mode 100644 index 00000000..f54dfdc7 --- /dev/null +++ b/utils/il/span.c @@ -0,0 +1,525 @@ +#include "l.h" + +void +span(void) +{ + Prog *p, *q; + Sym *setext, *s; + Optab *o; + int m, bflag, i, spass; + long c, otxt, v; + + if(debug['v']) + Bprint(&bso, "%5.2f span\n", cputime()); + Bflush(&bso); + + bflag = 0; + c = 0; + otxt = c; + for(p = firstp; p != P; p = p->link) { + if(p->as == ATEXT) + c = (c + 3) & ~3; + p->pc = c; + o = oplook(p); + m = o->size; + if(!debug['c']){ + if(o->ctype && asmout(p, o, 2) == 2){ + bflag = 1; + p->mark |= COMPR; + m = 2; + } + } + if(m == 0) { + if(p->as == ATEXT) { + curtext = p; + autosize = p->to.offset + ptrsize; + if(p->from.sym != S) + p->from.sym->value = c; + /* need passes to resolve branches */ + if(c-otxt >= 0x1000) + bflag = 1; + otxt = c; + continue; + } + diag("zero-width instruction\n%P", p); + continue; + } + c += m; + } + + /* + * Multi-pass expansion of span dependent instructions + * Bcond JAL C.Bcond C.JAL C.JMP + */ + spass = 0; + while(bflag) { + if(debug['v']) + Bprint(&bso, "%5.2f span1\n", cputime()); + bflag = 0; + spass ^= SPASS; + c = 0; + for(p = firstp; p != P; p = p->link) { + o = oplook(p); + m = o->size; + if(p->mark&COMPR) + m = 2; + if((o->type == 3 || o->type == 4) && p->cond) { + if((p->cond->mark&SPASS) == spass) + p->pc = c; + if(m == 2){ + /* + * If instruction was compressed, check again in case + * branch range is now too large. + */ + m = asmout(p, o, 3); + if(m != 2){ + p->mark &= ~COMPR; + bflag = 1; + } + } + otxt = p->cond->pc - p->pc; + if(otxt < 0) + otxt = -otxt; + if(o->type == 3){ + /* + * If Bcond branch range exceeds 4K, replace it by the + * logically negated branch around a JMP. + */ + if(otxt >= 0x1000) { + q = prg(); + q->link = p->link; + q->line = p->line; + q->as = AJMP; + q->to.type = D_BRANCH; + q->cond = p->cond; + p->link = q; + p->as = relinv(p->as); + p->cond = q->link; + p->optab = 0; + o = oplook(p); + q->mark = spass ^ SPASS; + m = asmout(p, o, 2); + if(m == 2) + p->mark |= COMPR; + q->pc = p->pc + m; + bflag = 1; + } + }else{ + /* + * If JAL branch range exceeds 1M, change address class + * and recalculate instruction length. + */ + if(otxt >= 0x100000) { + p->to.class = C_LBRA + 1; + p->optab = 0; + o = oplook(p); + m = asmout(p, o, 3); + p->mark &= ~COMPR; + } + } + } + if(p->as == ATEXT) + c = (c + 3) & ~3; + p->pc = c; + p->mark ^= SPASS; + if(m == 0) { + if(p->as == ATEXT) { + curtext = p; + autosize = p->to.offset + ptrsize; + if(p->from.sym != S) + p->from.sym->value = c; + continue; + } + diag("zero-width instruction\n%P", p); + continue; + } + c += m; + } + } + + if(debug['t']) { + /* + * add strings to text segment + */ + c = rnd(c, 8); + for(i=0; i<NHASH; i++) + for(s = hash[i]; s != S; s = s->link) { + if(s->type != SSTRING) + continue; + v = s->value; + while(v & 3) + v++; + s->value = c; + c += v; + } + } + + c = rnd(c, 8); + + setext = lookup("etext", 0); + if(setext != S) { + setext->value = c; + textsize = c; + } + if(INITRND) + INITDAT = rnd(INITTEXT + c, INITRND); + if(debug['v']) + Bprint(&bso, "tsize = %lux\n", textsize); + Bflush(&bso); +} + +void +xdefine(char *p, int t, long v) +{ + Sym *s; + + s = lookup(p, 0); + if(s->type == 0 || s->type == SXREF) { + s->type = t; + s->value = v; + } +} + +long +regoff(Adr *a) +{ + + instoffset = 0; + a->class = aclass(a) + 1; + return instoffset; +} + +int +classreg(Adr *a) +{ + if(a->reg == NREG) { + switch(a->class - 1) { + case C_SEXT: + case C_SECON: + case C_LECON: + return REGSB; + case C_SAUTO: + case C_LAUTO: + case C_SACON: + case C_LACON: + return REGSP; + } + } + return a->reg; +} + +int +aclass(Adr *a) +{ + Sym *s; + int t; + + switch(a->type) { + case D_NONE: + return C_NONE; + + case D_REG: + return C_REG; + + case D_CTLREG: + return C_CTLREG; + + case D_FREG: + return C_FREG; + + case D_OREG: + switch(a->name) { + case D_EXTERN: + case D_STATIC: + if(a->sym == 0 || a->sym->name == 0) { + print("null sym external\n"); + print("%D\n", a); + return C_GOK; + } + t = a->sym->type; + if(t == 0 || t == SXREF) { + diag("undefined external: %s in %s", + a->sym->name, TNAME); + a->sym->type = SDATA; + } + instoffset = a->sym->value + a->offset - BIG; + if(instoffset >= -BIG && instoffset < BIG) + return C_SEXT; + return C_LEXT; + case D_AUTO: + instoffset = autosize + a->offset; + if(instoffset >= -BIG && instoffset < BIG) + return C_SAUTO; + return C_LAUTO; + + case D_PARAM: + instoffset = autosize + a->offset + ptrsize; + if(instoffset >= -BIG && instoffset < BIG) + return C_SAUTO; + return C_LAUTO; + case D_NONE: + instoffset = a->offset; + if(instoffset == 0) + return C_ZOREG; + if(instoffset >= -BIG && instoffset < BIG) + return C_SOREG; + return C_LOREG; + } + return C_GOK; + + case D_FCONST: + return C_FCON; + + case D_VCONST: + return C_VCON; + + case D_CONST: + switch(a->name) { + + case D_NONE: + instoffset = a->offset; + if(a->reg != NREG && a->reg != REGZERO){ + if(instoffset >= -BIG && instoffset < BIG) + return C_SRCON; + return C_LRCON; + } + consize: + if(instoffset == 0) + return C_ZCON; + if(instoffset >= -0x800 && instoffset <= 0x7ff) + return C_SCON; + if((instoffset & 0xfff) == 0) + return C_UCON; + return C_LCON; + + case D_EXTERN: + case D_STATIC: + instoffx = 0; + s = a->sym; + if(s == S) + break; + t = s->type; + switch(t) { + case 0: + case SXREF: + diag("undefined external: %s in %s", + s->name, TNAME); + s->type = SDATA; + break; + case SCONST: + instoffset = s->value + a->offset; + goto consize; + case STEXT: + case SLEAF: + case SSTRING: + instoffset = s->value + a->offset; + instoffx = INITTEXT; + return C_LECON; + } + instoffset = s->value + a->offset - BIG; + if(instoffset >= -BIG && instoffset < BIG && instoffset != 0L) + return C_SECON; + instoffset = s->value + a->offset; + instoffx = INITDAT; + return C_LECON; + + case D_AUTO: + instoffset = autosize + a->offset; + if(instoffset >= -BIG && instoffset < BIG) + return C_SACON; + return C_LACON; + + case D_PARAM: + instoffset = autosize + a->offset + ptrsize; + if(instoffset >= -BIG && instoffset < BIG) + return C_SACON; + return C_LACON; + } + return C_GOK; + + case D_BRANCH: + return C_SBRA; + } + return C_GOK; +} + +Optab* +oplook(Prog *p) +{ + int a1, a2, a3, r; + char *c1, *c3; + Optab *o, *e; + + a1 = p->optab; + if(a1) + return optab+(a1-1); + a1 = p->from.class; + if(a1 == 0) { + a1 = aclass(&p->from) + 1; + p->from.class = a1; + } + a1--; + a3 = p->to.class; + if(a3 == 0) { + a3 = aclass(&p->to) + 1; + p->to.class = a3; + } + a3--; + a2 = C_NONE; + if(p->reg != NREG) + a2 = C_REG; + r = p->as; + o = oprange[r].start; + if(o == 0) { + a1 = opcross[repop[r]][a1][a3]; + if(a1) { + p->optab = a1+1; + return optab+a1; + } + o = oprange[r].stop; /* just generate an error */ + a1 = p->from.class - 1; + } + e = oprange[r].stop; + + c1 = xcmp[a1]; + c3 = xcmp[a3]; + for(; o<e; o++) + if(c1[o->a1]) + if(c3[o->a3]) { + p->optab = (o-optab)+1; + return o; + } + diag("illegal combination %A %d %d %d", + p->as, a1, a2, a3); + if(!debug['a']) + prasm(p); + o = optab; + p->optab = (o-optab)+1; + return o; +} + +int +cmp(int a, int b) +{ + + if(a == b) + return 1; + switch(a) { + case C_LCON: + if(b == C_ZCON || b == C_SCON || b == C_UCON) + return 1; + break; + case C_UCON: + if(b == C_ZCON) + return 1; + break; + case C_SCON: + if(b == C_ZCON) + return 1; + break; + case C_LACON: + if(b == C_SACON) + return 1; + break; + case C_LRCON: + if(b == C_SRCON) + return 1; + break; + case C_LBRA: + if(b == C_SBRA) + return 1; + break; + case C_LEXT: + if(b == C_SEXT) + return 1; + break; + case C_LAUTO: + if(b == C_SAUTO) + return 1; + break; + case C_ZREG: + if(b == C_REG || b == C_ZCON) + return 1; + break; + case C_LOREG: + if(b == C_ZOREG || b == C_SOREG || b == C_SAUTO || b == C_LAUTO) + return 1; + break; + case C_SOREG: + if(b == C_ZOREG || b == C_SAUTO || b == C_SEXT) + return 1; + break; + } + return 0; +} + +int +ocmp(void *a1, void *a2) +{ + Optab *p1, *p2; + int n; + + p1 = (Optab*)a1; + p2 = (Optab*)a2; + n = p1->as - p2->as; + if(n) + return n; + n = p1->a1 - p2->a1; + if(n) + return n; + n = p1->a3 - p2->a3; + if(n) + return n; + return 0; +} + +void +buildop(void) +{ + int i, n, r; + + for(i=0; i<32; i++) + for(n=0; n<32; n++) + xcmp[i][n] = cmp(n, i); + for(n=0; optab[n].as != AXXX; n++) + ; + qsort(optab, n, sizeof(optab[0]), ocmp); + for(i=0; i<n; i++) { + r = optab[i].as; + oprange[r].start = optab+i; + while(optab[i].as == r) + i++; + oprange[r].stop = optab+i; + i--; + } + + buildrep(1, AMOVW); +} + +void +buildrep(int x, int as) +{ + Opcross *p; + Optab *e, *s, *o; + int a1, a3, n; + + if(C_GOK >= 32 || x >= nelem(opcross)) { + diag("assumptions fail in buildrep"); + errorexit(); + } + repop[as] = x; + p = (opcross + x); + s = oprange[as].start; + e = oprange[as].stop; + for(o=e-1; o>=s; o--) { + n = o-optab; + for(a1=0; a1<32; a1++) { + if(!xcmp[a1][o->a1]) + continue; + for(a3=0; a3<32; a3++) + if(xcmp[a3][o->a3]) + (*p)[a1][a3] = n; + } + } + oprange[as].start = 0; +} |
