14 if(*a >= '0' && *a <= '9')
25 return s->value+INITDAT;
27 diag("entry not text: %s", s->name);
40 Bprint(&bso, "%5.2f asm\n", cputime());
43 seek(cout, OFFSET, 0);
45 for(p = firstp; p != P; p = p->link) {
48 autosize = p->to.offset + 4;
51 diag("phase error %lux sb %lux",
58 o = oplook(p); /* could probably avoid this call */
68 /* output strings in text segment */
69 etext = INITTEXT + textsize;
70 for(t = pc; t < etext; t += sizeof(buf)-100) {
71 if(etext-t > sizeof(buf)-100)
72 datblk(t, sizeof(buf)-100, 1);
74 datblk(t, etext-t, 1);
84 OFFSET = HEADR+textsize;
85 seek(cout, OFFSET, 0);
88 case 6: /* no header, padded segments */
89 OFFSET = rnd(HEADR+textsize, 4096);
90 seek(cout, OFFSET, 0);
96 write(cout, buf, INITDAT-textsize);
99 for(t = 0; t < datsize; t += sizeof(buf)-100) {
100 if(datsize-t > sizeof(buf)-100)
101 datblk(t, sizeof(buf)-100, 0);
103 datblk(t, datsize-t, 0);
110 Bprint(&bso, "%5.2f sym\n", cputime());
120 OFFSET = HEADR+textsize+datsize;
121 seek(cout, OFFSET, 0);
124 case 6: /* no header, padded segments */
125 OFFSET += rnd(datsize, 4096);
126 seek(cout, OFFSET, 0);
134 Bprint(&bso, "%5.2f pc\n", cputime());
143 seek(cout, HEADR+textsize+datsize, 0);
149 Bprint(&bso, "%5.2f header\n", cputime());
152 seek(cout, OFFSET, 0);
154 case 0: /* no header */
155 case 6: /* no header, padded segments */
157 case 1: /* aif for risc os */
158 lputl(0xe1a00000); /* NOP - decompress code */
159 lputl(0xe1a00000); /* NOP - relocation code */
160 lputl(0xeb000000 + 12); /* BL - zero init code */
166 - 8) / 4); /* BL - entry code */
168 lputl(0xef000011); /* SWI - exit code */
169 lputl(textsize+HEADR); /* text size */
170 lputl(datsize); /* data size */
171 lputl(0); /* sym size */
173 lputl(bsssize); /* bss size */
174 lputl(0); /* sym type */
175 lputl(INITTEXT-HEADR); /* text addr */
176 lputl(0); /* workspace - ignored */
178 lputl(32); /* addr mode / data addr flag */
179 lputl(0); /* data addr */
181 lputl(0); /* reserved */
184 lputl(0xe1a00000); /* NOP - zero init code */
185 lputl(0xe1a0f00e); /* B (R14) - zero init return */
189 lput(0x80000000|0x647); /* magic */
191 lput(0x647); /* magic */
192 lput(textsize); /* sizes */
195 lput(symsize); /* nsyms */
196 lput(entryvalue()); /* va of entry */
200 case 3: /* boot for NetBSD */
201 lput((143<<16)|0413); /* magic */
202 lputl(rnd(HEADR+textsize, 4096));
203 lputl(rnd(datsize, 4096));
205 lputl(symsize); /* nsyms */
206 lputl(entryvalue()); /* va of entry */
210 case 4: /* boot for IXP1200 */
212 case 5: /* boot for ipaq */
213 lputl(0xe3300000); /* nop */
214 lputl(0xe3300000); /* nop */
215 lputl(0xe3300000); /* nop */
216 lputl(0xe3300000); /* nop */
219 strnput("\177ELF", 4); /* e_ident */
220 cput(1); /* class = 32 bit */
221 cput(2); /* data = MSB */
222 cput(1); /* version = CURRENT */
224 lput((2L<<16)|40); /* type = EXEC; machine = ARM */
225 lput(1L); /* version = CURRENT */
226 lput(entryvalue()); /* entry vaddr */
227 lput(52L); /* offset to first phdr */
229 debug['S'] = 1; /* no symbol table */
231 lput(HEADR+textsize+datsize+symsize); /* offset to first shdr */
232 lput(0L); /* flags = PPC */
233 lput((52L<<16)|32L); /* Ehdr & Phdr sizes*/
234 lput((4L<<16)|40L); /* # Phdrs & Shdr size */
235 lput((4L<<16)|2L); /* # Shdrs & shdr string size */
239 lput(0L); /* flags = PPC */
240 lput((52L<<16)|32L); /* Ehdr & Phdr sizes*/
241 lput((4L<<16)|0L); /* # Phdrs & Shdr size */
242 lput((4L<<16)|0L); /* # Shdrs & shdr string size */
245 lput(1L); /* text - type = PT_LOAD */
246 lput(HEADR); /* file offset */
247 lput(INITTEXT); /* vaddr */
248 lput(INITTEXT); /* paddr */
249 lput(textsize); /* file size */
250 lput(textsize); /* memory size */
251 lput(0x05L); /* protections = RX */
252 lput(0); /* alignment */
254 lput(1L); /* data - type = PT_LOAD */
255 lput(HEADR+textsize); /* file offset */
256 lput(INITDAT); /* vaddr */
257 lput(INITDAT); /* paddr */
258 lput(datsize); /* file size */
259 lput(datsize+bsssize); /* memory size */
260 lput(0x07L); /* protections = RWX */
261 lput(0); /* alignment */
263 lput(0L); /* data - type = PT_NULL */
264 lput(HEADR+textsize+datsize); /* file offset */
265 lput(0L); /* vaddr */
266 lput(0L); /* paddr */
267 lput(symsize); /* symbol table size */
268 lput(lcsize); /* line number size */
269 lput(0x04L); /* protections = R */
270 lput(0x04L); /* alignment code?? */
277 strnput(char *s, int n)
342 n = sizeof(buf.cbuf) - cbc;
344 write(cout, buf.cbuf, n);
346 cbc = sizeof(buf.cbuf);
350 nopstat(char *f, Count *c)
353 Bprint(&bso, "%s delay %ld/%ld (%.2f)\n", f,
354 c->outof - c->count, c->outof,
355 (double)(c->outof - c->count)/c->outof);
366 s = lookup("etext", 0);
368 putsymb(s->name, 'T', s->value, s->version);
370 for(h=0; h<NHASH; h++)
371 for(s=hash[h]; s!=S; s=s->link)
374 putsymb(s->name, 'D', s->value, s->version);
378 putsymb(s->name, 'D', s->value+INITDAT, s->version);
382 putsymb(s->name, 'B', s->value+INITDAT, s->version);
386 putsymb(s->name, 'T', s->value, s->version);
390 putsymb(s->name, 'f', s->value, s->version);
394 for(p=textp; p!=P; p=p->cond) {
396 if(s->type != STEXT && s->type != SLEAF)
399 /* filenames first */
400 for(a=p->to.autom; a; a=a->link)
401 if(a->type == D_FILE)
402 putsymb(a->asym->name, 'z', a->aoffset, 0);
404 if(a->type == D_FILE1)
405 putsymb(a->asym->name, 'Z', a->aoffset, 0);
408 putsymb(s->name, 'T', s->value, s->version);
410 putsymb(s->name, 'L', s->value, s->version);
412 /* frame, auto and param after */
413 putsymb(".frame", 'm', p->to.offset+4, 0);
414 for(a=p->to.autom; a; a=a->link)
415 if(a->type == D_AUTO)
416 putsymb(a->asym->name, 'a', -a->aoffset, 0);
418 if(a->type == D_PARAM)
419 putsymb(a->asym->name, 'p', a->aoffset, 0);
421 if(debug['v'] || debug['n'])
422 Bprint(&bso, "symsize = %lud\n", symsize);
427 putsymb(char *s, int t, long v, int ver)
436 cput(t+0x80); /* 0x80 is variable length */
438 if(t == 'Z' || t == 'z') {
440 for(i=1; s[i] != 0 || s[i+1] != 0; i += 2) {
453 symsize += 4 + 1 + i + 1;
456 if(t == 'z' || t == 'Z') {
457 Bprint(&bso, "%c %.8lux ", t, v);
458 for(i=1; s[i] != 0 || s[i+1] != 0; i+=2) {
459 f = ((s[i]&0xff) << 8) | (s[i+1]&0xff);
460 Bprint(&bso, "/%x", f);
466 Bprint(&bso, "%c %.8lux %s<%d>\n", t, v, s, ver);
468 Bprint(&bso, "%c %.8lux %s\n", t, v, s);
482 for(p = firstp; p != P; p = p->link) {
483 if(p->line == oldlc || p->as == ATEXT || p->as == ANOP) {
487 Bprint(&bso, "%6lux %P\n",
492 Bprint(&bso, "\t\t%6ld", lcsize);
493 v = (p->pc - oldpc) / MINLC;
498 cput(s+128); /* 129-255 +pc */
500 Bprint(&bso, " pc+%ld*%d(%ld)", s, MINLC, s+128);
506 oldpc = p->pc + MINLC;
507 if(s > 64 || s < -64) {
508 cput(0); /* 0 vv +lc */
515 Bprint(&bso, " lc+%ld(%d,%ld)\n",
518 Bprint(&bso, " lc%ld(%d,%ld)\n",
520 Bprint(&bso, "%6lux %P\n",
527 cput(0+s); /* 1-64 +lc */
529 Bprint(&bso, " lc+%ld(%ld)\n", s, 0+s);
530 Bprint(&bso, "%6lux %P\n",
534 cput(64-s); /* 65-128 -lc */
536 Bprint(&bso, " lc%ld(%ld)\n", s, 64-s);
537 Bprint(&bso, "%6lux %P\n",
548 if(debug['v'] || debug['V'])
549 Bprint(&bso, "lcsize = %ld\n", lcsize);
554 datblk(long s, long n, int str)
562 memset(buf.dbuf, 0, n+100);
563 for(p = datap; p != P; p = p->link) {
564 if(str != (p->from.sym->type == SSTRING))
567 a = p->from.sym->value + p->from.offset;
581 if(p->as != AINIT && p->as != ADYNT && !p->from.sym->dupok) {
582 for(j=l+(c-i)-1; j>=l; j--)
585 diag("multiple initialization");
591 diag("unknown mode in initialization%P", p);
598 fl = ieeedtof(p->to.ieee);
601 buf.dbuf[l] = cast[fnuxi4[i]];
606 cast = (char*)p->to.ieee;
608 buf.dbuf[l] = cast[fnuxi8[i]];
617 buf.dbuf[l] = p->to.sval[i];
632 d += p->to.sym->value;
636 d += p->to.sym->value + INITDAT;
639 dynreloc(v, a+INITDAT, 1);
644 diag("bad nuxi %d %d%P", c, i, curp);
648 buf.dbuf[l] = cast[inuxi1[i]];
654 buf.dbuf[l] = cast[inuxi2[i]];
660 buf.dbuf[l] = cast[inuxi4[i]];
668 write(cout, buf.dbuf, n);
672 asmout(Prog *p, Optab *o)
674 long o1, o2, o3, o4, o5, o6, v;
687 diag("unknown asm %d", o->type);
691 case 0: /* pseudo ops */
694 case 1: /* op R,[R],R */
695 o1 = oprrr(p->as, p->scond);
699 if(p->to.type == D_NONE)
701 if(p->as == AMOVW || p->as == AMVN)
705 o1 |= rf | (r<<16) | (rt<<12);
708 case 2: /* movbu $I,[R],R */
710 o1 = oprrr(p->as, p->scond);
711 o1 |= immrot(instoffset);
714 if(p->to.type == D_NONE)
716 if(p->as == AMOVW || p->as == AMVN)
720 o1 |= (r<<16) | (rt<<12);
723 case 3: /* add R<<[IR],[R],R */
726 o1 = oprrr(p->as, p->scond);
727 o1 |= p->from.offset;
730 if(p->to.type == D_NONE)
732 if(p->as == AMOVW || p->as == AMVN)
736 o1 |= (r<<16) | (rt<<12);
739 case 4: /* add $I,[R],R */
741 o1 = oprrr(AADD, p->scond);
742 o1 |= immrot(instoffset);
747 o1 |= p->to.reg << 12;
754 if(s->type != SUNDEF)
755 diag("bad branch sym type");
756 v = (ulong)s->value >> (Roffset-2);
757 dynreloc(s, p->pc, 0);
759 else if(p->cond != P)
760 v = (p->cond->pc - pc) - 8;
761 o1 = opbra(p->as, p->scond);
762 o1 |= (v >> 2) & 0xffffff;
765 case 6: /* b ,O(R) -> add $O,R,PC */
767 o1 = oprrr(AADD, p->scond);
768 o1 |= immrot(instoffset);
769 o1 |= p->to.reg << 16;
773 case 7: /* bl ,O(R) -> mov PC,link; add $O,R,PC */
775 o1 = oprrr(AADD, p->scond);
780 o2 = oprrr(AADD, p->scond);
781 o2 |= immrot(instoffset);
782 o2 |= p->to.reg << 16;
786 case 8: /* sll $c,[R],R -> mov (R<<$c),R */
788 o1 = oprrr(p->as, p->scond);
793 o1 |= (instoffset&31) << 7;
794 o1 |= p->to.reg << 12;
797 case 9: /* sll R,[R],R -> mov (R<<R),R */
798 o1 = oprrr(p->as, p->scond);
803 o1 |= (p->from.reg << 8) | (1<<4);
804 o1 |= p->to.reg << 12;
807 case 10: /* swi [$con] */
808 o1 = oprrr(p->as, p->scond);
809 if(p->to.type != D_NONE) {
811 o1 |= instoffset & 0xffffff;
816 switch(aclass(&p->to)) {
820 if(p->to.name != D_EXTERN && p->to.name != D_STATIC)
823 if(p->to.sym->type == SUNDEF)
824 ckoff(p->to.sym, p->to.offset);
825 dynreloc(p->to.sym, p->pc, 1);
830 case 12: /* movw $lcon, reg */
831 o1 = omvl(p, &p->from, p->to.reg);
834 case 13: /* op $lcon, [R], R */
835 o1 = omvl(p, &p->from, REGTMP);
838 o2 = oprrr(p->as, p->scond);
841 if(p->as == AMOVW || p->as == AMVN)
846 if(p->to.type != D_NONE)
847 o2 |= p->to.reg << 12;
850 case 14: /* movb/movbu/movh/movhu R,R */
851 o1 = oprrr(ASLL, p->scond);
853 if(p->as == AMOVBU || p->as == AMOVHU)
854 o2 = oprrr(ASRL, p->scond);
856 o2 = oprrr(ASRA, p->scond);
859 o1 |= (p->from.reg)|(r<<12);
861 if(p->as == AMOVB || p->as == AMOVBU) {
870 case 15: /* mul r,[r,]r */
871 o1 = oprrr(p->as, p->scond);
882 if(rt == r || rf == REGPC || r == REGPC || rt == REGPC) {
883 diag("bad registers in MUL");
886 o1 |= (rf<<8) | r | (rt<<16);
890 case 16: /* div r,[r,]r */
896 o1 = oprrr(p->as, p->scond);
901 o1 |= (rf<<8) | r | (rt<<16) | (rt2<<12);
904 case 20: /* mov/movb/movbu R,O(R) */
909 o1 = osr(p->as, p->from.reg, instoffset, r, p->scond);
912 case 21: /* mov/movbu O(R),R -> lr */
917 o1 = olr(instoffset, r, p->to.reg, p->scond);
922 case 22: /* movb/movh/movhu O(R),R -> lr,shl,shr */
927 o1 = olr(instoffset, r, p->to.reg, p->scond);
929 o2 = oprrr(ASLL, p->scond);
930 o3 = oprrr(ASRA, p->scond);
933 o2 |= (24<<7)|(r)|(r<<12);
934 o3 |= (24<<7)|(r)|(r<<12);
936 o2 |= (16<<7)|(r)|(r<<12);
938 o3 = oprrr(ASRL, p->scond);
939 o3 |= (16<<7)|(r)|(r<<12);
943 case 23: /* movh/movhu R,O(R) -> sb,sb */
948 o1 = osr(AMOVH, p->from.reg, instoffset, r, p->scond);
950 o2 = oprrr(ASRL, p->scond);
951 o2 |= (8<<7)|(p->from.reg)|(REGTMP<<12);
953 o3 = osr(AMOVH, REGTMP, instoffset+1, r, p->scond);
956 case 30: /* mov/movb/movbu R,L(R) */
957 o1 = omvl(p, &p->to, REGTMP);
963 o2 = osrr(p->from.reg, REGTMP,r, p->scond);
968 case 31: /* mov/movbu L(R),R -> lr[b] */
969 case 32: /* movh/movb L(R),R -> lr[b] */
970 o1 = omvl(p, &p->from, REGTMP);
976 o2 = olrr(REGTMP,r, p->to.reg, p->scond);
977 if(p->as == AMOVBU || p->as == AMOVB)
982 o3 = oprrr(ASLL, p->scond);
984 if(p->as == AMOVBU || p->as == AMOVHU)
985 o4 = oprrr(ASRL, p->scond);
987 o4 = oprrr(ASRA, p->scond);
992 if(p->as == AMOVB || p->as == AMOVBU) {
1001 case 33: /* movh/movhu R,L(R) -> sb, sb */
1002 o1 = omvl(p, &p->to, REGTMP);
1008 o2 = osrr(p->from.reg, REGTMP, r, p->scond);
1011 o3 = oprrr(ASRL, p->scond);
1012 o3 |= (8<<7)|(p->from.reg)|(p->from.reg<<12);
1013 o3 |= (1<<6); /* ROR 8 */
1015 o4 = oprrr(AADD, p->scond);
1016 o4 |= (REGTMP << 12) | (REGTMP << 16);
1019 o5 = osrr(p->from.reg, REGTMP,r,p->scond);
1022 o6 = oprrr(ASRL, p->scond);
1023 o6 |= (24<<7)|(p->from.reg)|(p->from.reg<<12);
1024 o6 |= (1<<6); /* ROL 8 */
1028 case 34: /* mov $lacon,R */
1029 o1 = omvl(p, &p->from, REGTMP);
1033 o2 = oprrr(AADD, p->scond);
1039 if(p->to.type != D_NONE)
1040 o2 |= p->to.reg << 12;
1043 case 35: /* mov PSR,R */
1044 o1 = (2<<23) | (0xf<<16) | (0<<0);
1045 o1 |= (p->scond & C_SCOND) << 28;
1046 o1 |= (p->from.reg & 1) << 22;
1047 o1 |= p->to.reg << 12;
1050 case 36: /* mov R,PSR */
1051 o1 = (2<<23) | (0x29f<<12) | (0<<4);
1052 if(p->scond & C_FBIT)
1054 o1 |= (p->scond & C_SCOND) << 28;
1055 o1 |= (p->to.reg & 1) << 22;
1056 o1 |= p->from.reg << 0;
1059 case 37: /* mov $con,PSR */
1061 o1 = (2<<23) | (0x29f<<12) | (0<<4);
1062 if(p->scond & C_FBIT)
1064 o1 |= (p->scond & C_SCOND) << 28;
1065 o1 |= immrot(instoffset);
1066 o1 |= (p->to.reg & 1) << 22;
1067 o1 |= p->from.reg << 0;
1070 case 38: /* movm $con,oreg -> stm */
1072 o1 |= p->from.offset & 0xffff;
1073 o1 |= p->to.reg << 16;
1077 case 39: /* movm oreg,$con -> ldm */
1078 o1 = (0x4 << 25) | (1 << 20);
1079 o1 |= p->to.offset & 0xffff;
1080 o1 |= p->from.reg << 16;
1084 diag("offset must be zero in MOVM");
1085 o1 |= (p->scond & C_SCOND) << 28;
1086 if(p->scond & C_PBIT)
1088 if(p->scond & C_UBIT)
1090 if(p->scond & C_SBIT)
1092 if(p->scond & C_WBIT)
1096 case 40: /* swp oreg,reg,reg */
1097 if(p->as != ASTREX){
1100 diag("offset must be zero in SWP");
1102 o1 = (0x2<<23) | (0x9<<4);
1105 else if(p->as == ALDREX || p->as == ASTREX){
1106 o1 |= (1 << 23) | 0xf00;
1108 o1 |= (1 << 20) | 0xf;
1110 o1 |= p->from.reg << 16;
1112 o1 |= p->to.reg << 12;
1113 o1 |= (p->scond & C_SCOND) << 28;
1116 case 41: /* rfe -> movm.s.w.u 0(r13),[r15] */
1120 case 42: /* clrex */
1124 case 50: /* floating point store */
1129 o1 = ofsr(p->as, p->from.reg, v, r, p->scond, p);
1132 case 51: /* floating point load */
1133 v = regoff(&p->from);
1137 o1 = ofsr(p->as, p->to.reg, v, r, p->scond, p) | (1<<20);
1140 case 52: /* floating point store, long offset UGLY */
1141 o1 = omvl(p, &p->to, REGTMP);
1147 o2 = oprrr(AADD, p->scond) | (REGTMP << 12) | (REGTMP << 16) | r;
1148 o3 = ofsr(p->as, p->from.reg, 0, REGTMP, p->scond, p);
1151 case 53: /* floating point load, long offset UGLY */
1152 o1 = omvl(p, &p->from, REGTMP);
1158 o2 = oprrr(AADD, p->scond) | (REGTMP << 12) | (REGTMP << 16) | r;
1159 o3 = ofsr(p->as, p->to.reg, 0, REGTMP, p->scond, p) | (1<<20);
1162 case 54: /* floating point arith */
1163 o1 = oprrr(p->as, p->scond);
1164 if(p->from.type == D_FCONST) {
1165 rf = chipfloat(p->from.ieee);
1167 diag("invalid floating-point immediate\n%P", p);
1175 if(p->to.type == D_NONE)
1176 rt = 0; /* CMP[FD] */
1177 else if(o1 & (1<<15))
1178 r = 0; /* monadic */
1181 o1 |= rf | (r<<16) | (rt<<12);
1184 case 55: /* floating point fix and float */
1185 o1 = oprrr(p->as, p->scond);
1188 if(p->to.type == D_NONE){
1190 diag("to.type==D_NONE (asm/fp)");
1192 if(p->from.type == D_REG)
1193 o1 |= (rf<<12) | (rt<<16);
1195 o1 |= rf | (rt<<12);
1198 /* old arm 7500 fp using coproc 1 (1<<8) */
1199 case 56: /* move to FP[CS]R */
1200 o1 = ((p->scond & C_SCOND) << 28) | (0xe << 24) | (1<<8) | (1<<4);
1201 o1 |= ((p->to.reg+1)<<21) | (p->from.reg << 12);
1204 case 57: /* move from FP[CS]R */
1205 o1 = ((p->scond & C_SCOND) << 28) | (0xe << 24) | (1<<8) | (1<<4);
1206 o1 |= ((p->from.reg+1)<<21) | (p->to.reg<<12) | (1<<20);
1208 case 58: /* movbu R,R */
1209 o1 = oprrr(AAND, p->scond);
1213 if(p->to.type == D_NONE)
1217 o1 |= (r<<16) | (rt<<12);
1220 case 59: /* movw/bu R<<I(R),R -> ldr indexed */
1221 if(p->from.reg == NREG) {
1223 diag("byte MOV from shifter operand");
1226 if(p->from.offset&(1<<4))
1227 diag("bad shift in LDR");
1228 o1 = olrr(p->from.offset, p->from.reg, p->to.reg, p->scond);
1233 case 60: /* movb R(R),R -> ldrsb indexed */
1234 if(p->from.reg == NREG) {
1235 diag("byte MOV from shifter operand");
1238 if(p->from.offset&(~0xf))
1239 diag("bad shift in LDRSB");
1240 o1 = olhrr(p->from.offset, p->from.reg, p->to.reg, p->scond);
1241 o1 ^= (1<<5)|(1<<6);
1244 case 61: /* movw/b/bu R,R<<[IR](R) -> str indexed */
1245 if(p->to.reg == NREG)
1246 diag("MOV to shifter operand");
1247 o1 = osrr(p->from.reg, p->to.offset, p->to.reg, p->scond);
1248 if(p->as == AMOVB || p->as == AMOVBU)
1252 case 62: /* case R -> movw R<<2(PC),PC */
1253 o1 = olrr(p->from.reg, REGPC, REGPC, p->scond);
1257 case 63: /* bcase */
1261 dynreloc(S, p->pc, 1);
1266 case 64: /* mov/movb/movbu R,addr */
1267 o1 = omvl(p, &p->to, REGTMP);
1270 o2 = osr(p->as, p->from.reg, 0, REGTMP, p->scond);
1273 case 65: /* mov/movbu addr,R */
1274 case 66: /* movh/movhu/movb addr,R */
1275 o1 = omvl(p, &p->from, REGTMP);
1278 o2 = olr(0, REGTMP, p->to.reg, p->scond);
1279 if(p->as == AMOVBU || p->as == AMOVB)
1284 o3 = oprrr(ASLL, p->scond);
1286 if(p->as == AMOVBU || p->as == AMOVHU)
1287 o4 = oprrr(ASRL, p->scond);
1289 o4 = oprrr(ASRA, p->scond);
1294 if(p->as == AMOVB || p->as == AMOVBU) {
1303 case 67: /* movh/movhu R,addr -> sb, sb */
1304 o1 = omvl(p, &p->to, REGTMP);
1307 o2 = osr(p->as, p->from.reg, 0, REGTMP, p->scond);
1309 o3 = oprrr(ASRL, p->scond);
1310 o3 |= (8<<7)|(p->from.reg)|(p->from.reg<<12);
1311 o3 |= (1<<6); /* ROR 8 */
1313 o4 = oprrr(AADD, p->scond);
1314 o4 |= (REGTMP << 12) | (REGTMP << 16);
1317 o5 = osr(p->as, p->from.reg, 0, REGTMP, p->scond);
1319 o6 = oprrr(ASRL, p->scond);
1320 o6 |= (24<<7)|(p->from.reg)|(p->from.reg<<12);
1321 o6 |= (1<<6); /* ROL 8 */
1324 case 68: /* floating point store -> ADDR */
1325 o1 = omvl(p, &p->to, REGTMP);
1328 o2 = ofsr(p->as, p->from.reg, 0, REGTMP, p->scond, p);
1331 case 69: /* floating point load <- ADDR */
1332 o1 = omvl(p, &p->from, REGTMP);
1335 o2 = ofsr(p->as, p->to.reg, 0, REGTMP, p->scond, p) | (1<<20);
1339 case 70: /* movh/movhu R,O(R) -> strh */
1344 o1 = oshr(p->from.reg, instoffset, r, p->scond);
1346 case 71: /* movb/movh/movhu O(R),R -> ldrsb/ldrsh/ldrh */
1351 o1 = olhr(instoffset, r, p->to.reg, p->scond);
1353 o1 ^= (1<<5)|(1<<6);
1354 else if(p->as == AMOVH)
1357 case 72: /* movh/movhu R,L(R) -> strh */
1358 o1 = omvl(p, &p->to, REGTMP);
1364 o2 = oshrr(p->from.reg, REGTMP,r, p->scond);
1366 case 73: /* movb/movh/movhu L(R),R -> ldrsb/ldrsh/ldrh */
1367 o1 = omvl(p, &p->from, REGTMP);
1373 o2 = olhrr(REGTMP, r, p->to.reg, p->scond);
1375 o2 ^= (1<<5)|(1<<6);
1376 else if(p->as == AMOVH)
1381 case 74: /* vfp floating point arith */
1382 o1 = opvfprrr(p->as, p->scond);
1384 if(p->from.type == D_FCONST) {
1385 diag("invalid floating-point immediate\n%P", p);
1393 if(((o1>>20)&0xf) == 0xb)
1396 o1 |= r<<16 | rf<<0;
1398 case 75: /* vfp floating point compare */
1399 o1 = opvfprrr(p->as, p->scond);
1401 if(p->from.type == D_FCONST) {
1402 if(p->from.ieee->h != 0 || p->from.ieee->l != 0)
1403 diag("invalid floating-point immediate\n%P", p);
1408 o1 |= rt<<12 | rf<<0;
1409 o2 = 0x0ef1fa10; /* MRS APSR_nzcv, FPSCR */
1410 o2 |= (p->scond & C_SCOND) << 28;
1412 case 76: /* vfp floating point fix and float */
1413 o1 = opvfprrr(p->as, p->scond);
1416 if(p->from.type == D_REG) {
1417 o2 = o1 | rt<<12 | rt<<0;
1418 o1 = 0x0e000a10; /* VMOV F,R */
1419 o1 |= (p->scond & C_SCOND) << 28 | rt<<16 | rf<<12;
1421 o1 |= FREGTMP<<12 | rf<<0;
1422 o2 = 0x0e100a10; /* VMOV R,F */
1423 o2 |= (p->scond & C_SCOND) << 28 | FREGTMP<<16 | rt<<12;
1429 Bprint(&bso, "%2d ", o->type);
1435 Bprint(&bso, " %.8lux:\t\t%P\n", v, p);
1439 Bprint(&bso, " %.8lux: %.8lux\t%P\n", v, o1, p);
1444 Bprint(&bso, " %.8lux: %.8lux %.8lux%P\n", v, o1, o2, p);
1450 Bprint(&bso, " %.8lux: %.8lux %.8lux %.8lux%P\n", v, o1, o2, o3, p);
1457 Bprint(&bso, " %.8lux: %.8lux %.8lux %.8lux %.8lux%P\n",
1458 v, o1, o2, o3, o4, p);
1466 Bprint(&bso, " %.8lux: %.8lux %.8lux %.8lux %.8lux %.8lux%P\n",
1467 v, o1, o2, o3, o4, o5, p);
1476 Bprint(&bso, " %.8lux: %.8lux %.8lux %.8lux %.8lux %.8lux %.8lux%P\n",
1477 v, o1, o2, o3, o4, o5, o6, p);
1489 oprrr(int a, int sc)
1493 o = (sc & C_SCOND) << 28;
1496 if(sc & (C_PBIT|C_WBIT))
1497 diag(".P/.W on dp instruction");
1500 case AMUL: return o | (0x0<<21) | (0x9<<4);
1501 case AMULA: return o | (0x1<<21) | (0x9<<4);
1502 case AMULLU: return o | (0x4<<21) | (0x9<<4);
1503 case AMULL: return o | (0x6<<21) | (0x9<<4);
1504 case AMULALU: return o | (0x5<<21) | (0x9<<4);
1505 case AMULAL: return o | (0x7<<21) | (0x9<<4);
1506 case AAND: return o | (0x0<<21);
1507 case AEOR: return o | (0x1<<21);
1508 case ASUB: return o | (0x2<<21);
1509 case ARSB: return o | (0x3<<21);
1510 case AADD: return o | (0x4<<21);
1511 case AADC: return o | (0x5<<21);
1512 case ASBC: return o | (0x6<<21);
1513 case ARSC: return o | (0x7<<21);
1514 case ATST: return o | (0x8<<21) | (1<<20);
1515 case ATEQ: return o | (0x9<<21) | (1<<20);
1516 case ACMP: return o | (0xa<<21) | (1<<20);
1517 case ACMN: return o | (0xb<<21) | (1<<20);
1518 case AORR: return o | (0xc<<21);
1519 case AMOVW: return o | (0xd<<21);
1520 case ABIC: return o | (0xe<<21);
1521 case AMVN: return o | (0xf<<21);
1522 case ASLL: return o | (0xd<<21) | (0<<5);
1523 case ASRL: return o | (0xd<<21) | (1<<5);
1524 case ASRA: return o | (0xd<<21) | (2<<5);
1525 case AROR: return o | (0xd<<21) | (3<<5);
1526 case ASWI: return o | (0xf<<24);
1528 /* old arm 7500 fp using coproc 1 (1<<8) */
1529 case AADDD: return o | (0xe<<24) | (0x0<<20) | (1<<8) | (1<<7);
1530 case AADDF: return o | (0xe<<24) | (0x0<<20) | (1<<8);
1531 case AMULD: return o | (0xe<<24) | (0x1<<20) | (1<<8) | (1<<7);
1532 case AMULF: return o | (0xe<<24) | (0x1<<20) | (1<<8);
1533 case ASUBD: return o | (0xe<<24) | (0x2<<20) | (1<<8) | (1<<7);
1534 case ASUBF: return o | (0xe<<24) | (0x2<<20) | (1<<8);
1535 case ADIVD: return o | (0xe<<24) | (0x4<<20) | (1<<8) | (1<<7);
1536 case ADIVF: return o | (0xe<<24) | (0x4<<20) | (1<<8);
1538 case ACMPF: return o | (0xe<<24) | (0x9<<20) | (0xF<<12) | (1<<8) | (1<<4); /* arguably, ACMPF should expand to RNDF, CMPD */
1541 case AMOVDF: return o | (0xe<<24) | (0x0<<20) | (1<<15) | (1<<8);
1543 case AMOVFD: return o | (0xe<<24) | (0x0<<20) | (1<<15) | (1<<8) | (1<<7);
1545 case AMOVWF: return o | (0xe<<24) | (0<<20) | (1<<8) | (1<<4);
1546 case AMOVWD: return o | (0xe<<24) | (0<<20) | (1<<8) | (1<<4) | (1<<7);
1547 case AMOVFW: return o | (0xe<<24) | (1<<20) | (1<<8) | (1<<4);
1548 case AMOVDW: return o | (0xe<<24) | (1<<20) | (1<<8) | (1<<4) | (1<<7);
1550 diag("bad rrr %d", a);
1556 opvfprrr(int a, int sc)
1560 o = (sc & C_SCOND) << 28;
1561 if(sc & (C_SBIT|C_PBIT|C_WBIT))
1562 diag(".S/.P/.W on vfp instruction");
1565 case AMOVWD: return o | 0xb<<8 | 0xb<<20 | 1<<6 | 0x8<<16 | 1<<7;
1566 case AMOVWF: return o | 0xa<<8 | 0xb<<20 | 1<<6 | 0x8<<16 | 1<<7;
1567 case AMOVDW: return o | 0xb<<8 | 0xb<<20 | 1<<6 | 0xD<<16 | 1<<7;
1568 case AMOVFW: return o | 0xa<<8 | 0xb<<20 | 1<<6 | 0xD<<16 | 1<<7;
1569 case AMOVFD: return o | 0xa<<8 | 0xb<<20 | 1<<6 | 0x7<<16 | 1<<7;
1570 case AMOVDF: return o | 0xb<<8 | 0xb<<20 | 1<<6 | 0x7<<16 | 1<<7;
1571 case AMOVF: return o | 0xa<<8 | 0xb<<20 | 1<<6 | 0x0<<16 | 0<<7;
1572 case AMOVD: return o | 0xb<<8 | 0xb<<20 | 1<<6 | 0x0<<16 | 0<<7;
1573 case ACMPF: return o | 0xa<<8 | 0xb<<20 | 1<<6 | 0x4<<16 | 0<<7;
1574 case ACMPD: return o | 0xb<<8 | 0xb<<20 | 1<<6 | 0x4<<16 | 0<<7;
1575 case AADDF: return o | 0xa<<8 | 0x3<<20;
1576 case AADDD: return o | 0xb<<8 | 0x3<<20;
1577 case ASUBF: return o | 0xa<<8 | 0x3<<20 | 1<<6;
1578 case ASUBD: return o | 0xb<<8 | 0x3<<20 | 1<<6;
1579 case AMULF: return o | 0xa<<8 | 0x2<<20;
1580 case AMULD: return o | 0xb<<8 | 0x2<<20;
1581 case ADIVF: return o | 0xa<<8 | 0x8<<20;
1582 case ADIVD: return o | 0xb<<8 | 0x8<<20;
1584 diag("bad vfp rrr %d", a);
1590 opbra(int a, int sc)
1593 if(sc & (C_SBIT|C_PBIT|C_WBIT))
1594 diag(".S/.P/.W on bra instruction");
1597 return (sc<<28)|(0x5<<25)|(0x1<<24);
1599 diag(".COND on bcond instruction");
1601 case ABEQ: return (0x0<<28)|(0x5<<25);
1602 case ABNE: return (0x1<<28)|(0x5<<25);
1603 case ABCS: return (0x2<<28)|(0x5<<25);
1604 case ABHS: return (0x2<<28)|(0x5<<25);
1605 case ABCC: return (0x3<<28)|(0x5<<25);
1606 case ABLO: return (0x3<<28)|(0x5<<25);
1607 case ABMI: return (0x4<<28)|(0x5<<25);
1608 case ABPL: return (0x5<<28)|(0x5<<25);
1609 case ABVS: return (0x6<<28)|(0x5<<25);
1610 case ABVC: return (0x7<<28)|(0x5<<25);
1611 case ABHI: return (0x8<<28)|(0x5<<25);
1612 case ABLS: return (0x9<<28)|(0x5<<25);
1613 case ABGE: return (0xa<<28)|(0x5<<25);
1614 case ABLT: return (0xb<<28)|(0x5<<25);
1615 case ABGT: return (0xc<<28)|(0x5<<25);
1616 case ABLE: return (0xd<<28)|(0x5<<25);
1617 case AB: return (0xe<<28)|(0x5<<25);
1619 diag("bad bra %A", a);
1625 olr(long v, int b, int r, int sc)
1630 diag(".S on LDR/STR instruction");
1631 o = (sc & C_SCOND) << 28;
1638 o |= (0x1<<26) | (1<<20);
1644 diag("literal span too large: %ld (R%d)\n%P", v, b, PP);
1652 olhr(long v, int b, int r, int sc)
1657 diag(".S on LDRH/STRH instruction");
1658 o = (sc & C_SCOND) << 28;
1663 o |= (1<<23) | (1<<20)|(0xb<<4);
1669 diag("literal span too large: %ld (R%d)\n%P", v, b, PP);
1670 o |= (v&0xf)|((v>>4)<<8)|(1<<22);
1677 osr(int a, int r, long v, int b, int sc)
1681 o = olr(v, b, r, sc) ^ (1<<20);
1688 oshr(int r, long v, int b, int sc)
1692 o = olhr(v, b, r, sc) ^ (1<<20);
1698 osrr(int r, int i, int b, int sc)
1701 return olr(i, b, r, sc) ^ ((1<<25) | (1<<20));
1705 oshrr(int r, int i, int b, int sc)
1707 return olhr(i, b, r, sc) ^ ((1<<22) | (1<<20));
1711 olrr(int i, int b, int r, int sc)
1714 return olr(i, b, r, sc) ^ (1<<25);
1718 olhrr(int i, int b, int r, int sc)
1720 return olhr(i, b, r, sc) ^ (1<<22);
1724 ovfpmem(int a, int r, long v, int b, int sc, Prog *p)
1728 if(sc & (C_SBIT|C_PBIT|C_WBIT))
1729 diag(".S/.P/.W on VLDR/VSTR instruction");
1730 o = (sc & C_SCOND) << 28;
1731 o |= 0xd<<24 | (1<<23);
1737 diag("odd offset for floating point op: %ld\n%P", v, p);
1738 else if(v >= (1<<10))
1739 diag("literal span too large: %ld\n%P", v, p);
1745 diag("bad fst %A", a);
1757 ofsr(int a, int r, long v, int b, int sc, Prog *p)
1762 return ovfpmem(a, r, v, b, sc, p);
1764 diag(".S on FLDR/FSTR instruction");
1765 o = (sc & C_SCOND) << 28;
1770 o |= (6<<25) | (1<<24) | (1<<23);
1776 diag("odd offset for floating point op: %ld\n%P", v, p);
1777 else if(v >= (1<<10))
1778 diag("literal span too large: %ld\n%P", v, p);
1786 diag("bad fst %A", a);
1796 omvl(Prog *p, Adr *a, int dr)
1801 v = immrot(~instoffset);
1803 diag("missing literal");
1807 o1 = oprrr(AMVN, p->scond&C_SCOND);
1811 v = p->cond->pc - p->pc - 8;
1812 o1 = olr(v, REGPC, dr, p->scond&C_SCOND);
1817 static Ieee chipfloats[] = {
1818 {0x00000000, 0x00000000}, /* 0 */
1819 {0x00000000, 0x3ff00000}, /* 1 */
1820 {0x00000000, 0x40000000}, /* 2 */
1821 {0x00000000, 0x40080000}, /* 3 */
1822 {0x00000000, 0x40100000}, /* 4 */
1823 {0x00000000, 0x40140000}, /* 5 */
1824 {0x00000000, 0x3fe00000}, /* .5 */
1825 {0x00000000, 0x40240000}, /* 10 */
1836 for(n = sizeof(chipfloats)/sizeof(chipfloats[0]); --n >= 0;){
1838 if(p->l == e->l && p->h == e->h)