asm.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "l.h"
  10. int32_t OFFSET;
  11. static Prog *PP;
  12. int32_t
  13. entryvalue(void)
  14. {
  15. char *a;
  16. Sym *s;
  17. a = INITENTRY;
  18. if(*a >= '0' && *a <= '9')
  19. return atolwhex(a);
  20. s = lookup(a, 0);
  21. if(s->type == 0)
  22. return INITTEXT;
  23. switch(s->type) {
  24. case STEXT:
  25. case SLEAF:
  26. break;
  27. case SDATA:
  28. if(dlm)
  29. return s->value+INITDAT;
  30. default:
  31. diag("entry not text: %s", s->name);
  32. }
  33. return s->value;
  34. }
  35. void
  36. asmb(void)
  37. {
  38. Prog *p;
  39. int32_t t, etext;
  40. Optab *o;
  41. if(debug['v'])
  42. Bprint(&bso, "%5.2f asm\n", cputime());
  43. Bflush(&bso);
  44. OFFSET = HEADR;
  45. seek(cout, OFFSET, 0);
  46. pc = INITTEXT;
  47. for(p = firstp; p != P; p = p->link) {
  48. if(p->as == ATEXT) {
  49. curtext = p;
  50. autosize = p->to.offset + 4;
  51. }
  52. if(p->pc != pc) {
  53. diag("phase error %lux sb %lux",
  54. p->pc, pc);
  55. if(!debug['a'])
  56. prasm(curp);
  57. pc = p->pc;
  58. }
  59. curp = p;
  60. o = oplook(p); /* could probably avoid this call */
  61. asmout(p, o);
  62. pc += o->size;
  63. }
  64. if(debug['a'])
  65. Bprint(&bso, "\n");
  66. Bflush(&bso);
  67. cflush();
  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);
  73. else
  74. datblk(t, etext-t, 1);
  75. }
  76. curtext = P;
  77. switch(HEADTYPE) {
  78. case 0:
  79. case 1:
  80. case 2:
  81. case 5:
  82. case 7:
  83. OFFSET = HEADR+textsize;
  84. seek(cout, OFFSET, 0);
  85. break;
  86. case 3:
  87. case 6: /* no header, padded segments */
  88. OFFSET = rnd(HEADR+textsize, 4096);
  89. seek(cout, OFFSET, 0);
  90. break;
  91. }
  92. if(dlm){
  93. char buf[8];
  94. write(cout, buf, INITDAT-textsize);
  95. textsize = INITDAT;
  96. }
  97. for(t = 0; t < datsize; t += sizeof(buf)-100) {
  98. if(datsize-t > sizeof(buf)-100)
  99. datblk(t, sizeof(buf)-100, 0);
  100. else
  101. datblk(t, datsize-t, 0);
  102. }
  103. symsize = 0;
  104. lcsize = 0;
  105. if(!debug['s']) {
  106. if(debug['v'])
  107. Bprint(&bso, "%5.2f sym\n", cputime());
  108. Bflush(&bso);
  109. switch(HEADTYPE) {
  110. case 0:
  111. case 1:
  112. case 4:
  113. case 5:
  114. debug['s'] = 1;
  115. break;
  116. case 2:
  117. OFFSET = HEADR+textsize+datsize;
  118. seek(cout, OFFSET, 0);
  119. break;
  120. case 3:
  121. case 6: /* no header, padded segments */
  122. OFFSET += rnd(datsize, 4096);
  123. seek(cout, OFFSET, 0);
  124. break;
  125. case 7:
  126. break;
  127. }
  128. if(!debug['s'])
  129. asmsym();
  130. if(debug['v'])
  131. Bprint(&bso, "%5.2f pc\n", cputime());
  132. Bflush(&bso);
  133. if(!debug['s'])
  134. asmlc();
  135. if(dlm)
  136. asmdyn();
  137. cflush();
  138. }
  139. else if(dlm){
  140. seek(cout, HEADR+textsize+datsize, 0);
  141. asmdyn();
  142. cflush();
  143. }
  144. if(debug['v'])
  145. Bprint(&bso, "%5.2f header\n", cputime());
  146. Bflush(&bso);
  147. OFFSET = 0;
  148. seek(cout, OFFSET, 0);
  149. switch(HEADTYPE) {
  150. case 0: /* no header */
  151. case 6: /* no header, padded segments */
  152. break;
  153. case 1: /* aif for risc os */
  154. lputl(0xe1a00000); /* NOP - decompress code */
  155. lputl(0xe1a00000); /* NOP - relocation code */
  156. lputl(0xeb000000 + 12); /* BL - zero init code */
  157. lputl(0xeb000000 +
  158. (entryvalue()
  159. - INITTEXT
  160. + HEADR
  161. - 12
  162. - 8) / 4); /* BL - entry code */
  163. lputl(0xef000011); /* SWI - exit code */
  164. lputl(textsize+HEADR); /* text size */
  165. lputl(datsize); /* data size */
  166. lputl(0); /* sym size */
  167. lputl(bsssize); /* bss size */
  168. lputl(0); /* sym type */
  169. lputl(INITTEXT-HEADR); /* text addr */
  170. lputl(0); /* workspace - ignored */
  171. lputl(32); /* addr mode / data addr flag */
  172. lputl(0); /* data addr */
  173. for(t=0; t<2; t++)
  174. lputl(0); /* reserved */
  175. for(t=0; t<15; t++)
  176. lputl(0xe1a00000); /* NOP - zero init code */
  177. lputl(0xe1a0f00e); /* B (R14) - zero init return */
  178. break;
  179. case 2: /* plan 9 */
  180. if(dlm)
  181. lput(0x80000000|0x647); /* magic */
  182. else
  183. lput(0x647); /* magic */
  184. lput(textsize); /* sizes */
  185. lput(datsize);
  186. lput(bsssize);
  187. lput(symsize); /* nsyms */
  188. lput(entryvalue()); /* va of entry */
  189. lput(0L);
  190. lput(lcsize);
  191. break;
  192. case 3: /* boot for NetBSD */
  193. lput((143<<16)|0413); /* magic */
  194. lputl(rnd(HEADR+textsize, 4096));
  195. lputl(rnd(datsize, 4096));
  196. lputl(bsssize);
  197. lputl(symsize); /* nsyms */
  198. lputl(entryvalue()); /* va of entry */
  199. lputl(0L);
  200. lputl(0L);
  201. break;
  202. case 4: /* boot for IXP1200 */
  203. break;
  204. case 5: /* boot for ipaq */
  205. lputl(0xe3300000); /* nop */
  206. lputl(0xe3300000); /* nop */
  207. lputl(0xe3300000); /* nop */
  208. lputl(0xe3300000); /* nop */
  209. break;
  210. case 7: /* elf */
  211. debug['S'] = 1; /* symbol table */
  212. elf32(ARM, ELFDATA2LSB, 0, nil);
  213. break;
  214. }
  215. cflush();
  216. }
  217. void
  218. strnput(char *s, int n)
  219. {
  220. for(; *s; s++){
  221. cput(*s);
  222. n--;
  223. }
  224. for(; n > 0; n--)
  225. cput(0);
  226. }
  227. void
  228. cput(int c)
  229. {
  230. cbp[0] = c;
  231. cbp++;
  232. cbc--;
  233. if(cbc <= 0)
  234. cflush();
  235. }
  236. void
  237. wput(int32_t l)
  238. {
  239. cbp[0] = l>>8;
  240. cbp[1] = l;
  241. cbp += 2;
  242. cbc -= 2;
  243. if(cbc <= 0)
  244. cflush();
  245. }
  246. void
  247. wputl(int32_t l)
  248. {
  249. cbp[0] = l;
  250. cbp[1] = l>>8;
  251. cbp += 2;
  252. cbc -= 2;
  253. if(cbc <= 0)
  254. cflush();
  255. }
  256. void
  257. lput(int32_t l)
  258. {
  259. cbp[0] = l>>24;
  260. cbp[1] = l>>16;
  261. cbp[2] = l>>8;
  262. cbp[3] = l;
  263. cbp += 4;
  264. cbc -= 4;
  265. if(cbc <= 0)
  266. cflush();
  267. }
  268. void
  269. lputl(int32_t l)
  270. {
  271. cbp[3] = l>>24;
  272. cbp[2] = l>>16;
  273. cbp[1] = l>>8;
  274. cbp[0] = l;
  275. cbp += 4;
  276. cbc -= 4;
  277. if(cbc <= 0)
  278. cflush();
  279. }
  280. void
  281. llput(int64_t v)
  282. {
  283. lput(v>>32);
  284. lput(v);
  285. }
  286. void
  287. llputl(int64_t v)
  288. {
  289. lputl(v);
  290. lputl(v>>32);
  291. }
  292. void
  293. cflush(void)
  294. {
  295. int n;
  296. n = sizeof(buf.cbuf) - cbc;
  297. if(n)
  298. write(cout, buf.cbuf, n);
  299. cbp = buf.cbuf;
  300. cbc = sizeof(buf.cbuf);
  301. }
  302. void
  303. nopstat(char *f, Count *c)
  304. {
  305. if(c->outof)
  306. Bprint(&bso, "%s delay %ld/%ld (%.2f)\n", f,
  307. c->outof - c->count, c->outof,
  308. (double)(c->outof - c->count)/c->outof);
  309. }
  310. void
  311. asmsym(void)
  312. {
  313. Prog *p;
  314. Auto *a;
  315. Sym *s;
  316. int h;
  317. s = lookup("etext", 0);
  318. if(s->type == STEXT)
  319. putsymb(s->name, 'T', s->value, s->version);
  320. for(h=0; h<NHASH; h++)
  321. for(s=hash[h]; s!=S; s=s->link)
  322. switch(s->type) {
  323. case SCONST:
  324. putsymb(s->name, 'D', s->value, s->version);
  325. continue;
  326. case SDATA:
  327. putsymb(s->name, 'D', s->value+INITDAT, s->version);
  328. continue;
  329. case SBSS:
  330. putsymb(s->name, 'B', s->value+INITDAT, s->version);
  331. continue;
  332. case SSTRING:
  333. putsymb(s->name, 'T', s->value, s->version);
  334. continue;
  335. case SFILE:
  336. putsymb(s->name, 'f', s->value, s->version);
  337. continue;
  338. }
  339. for(p=textp; p!=P; p=p->cond) {
  340. s = p->from.sym;
  341. if(s->type != STEXT && s->type != SLEAF)
  342. continue;
  343. /* filenames first */
  344. for(a=p->to.autom; a; a=a->link)
  345. if(a->type == D_FILE)
  346. putsymb(a->asym->name, 'z', a->aoffset, 0);
  347. else
  348. if(a->type == D_FILE1)
  349. putsymb(a->asym->name, 'Z', a->aoffset, 0);
  350. if(s->type == STEXT)
  351. putsymb(s->name, 'T', s->value, s->version);
  352. else
  353. putsymb(s->name, 'L', s->value, s->version);
  354. /* frame, auto and param after */
  355. putsymb(".frame", 'm', p->to.offset+4, 0);
  356. for(a=p->to.autom; a; a=a->link)
  357. if(a->type == D_AUTO)
  358. putsymb(a->asym->name, 'a', -a->aoffset, 0);
  359. else
  360. if(a->type == D_PARAM)
  361. putsymb(a->asym->name, 'p', a->aoffset, 0);
  362. }
  363. if(debug['v'] || debug['n'])
  364. Bprint(&bso, "symsize = %lud\n", symsize);
  365. Bflush(&bso);
  366. }
  367. void
  368. putsymb(char *s, int t, int32_t v, int ver)
  369. {
  370. int i, f;
  371. if(t == 'f')
  372. s++;
  373. lput(v);
  374. if(ver)
  375. t += 'a' - 'A';
  376. cput(t+0x80); /* 0x80 is variable length */
  377. if(t == 'Z' || t == 'z') {
  378. cput(s[0]);
  379. for(i=1; s[i] != 0 || s[i+1] != 0; i += 2) {
  380. cput(s[i]);
  381. cput(s[i+1]);
  382. }
  383. cput(0);
  384. cput(0);
  385. i++;
  386. }
  387. else {
  388. for(i=0; s[i]; i++)
  389. cput(s[i]);
  390. cput(0);
  391. }
  392. symsize += 4 + 1 + i + 1;
  393. if(debug['n']) {
  394. if(t == 'z' || t == 'Z') {
  395. Bprint(&bso, "%c %.8lux ", t, v);
  396. for(i=1; s[i] != 0 || s[i+1] != 0; i+=2) {
  397. f = ((s[i]&0xff) << 8) | (s[i+1]&0xff);
  398. Bprint(&bso, "/%x", f);
  399. }
  400. Bprint(&bso, "\n");
  401. return;
  402. }
  403. if(ver)
  404. Bprint(&bso, "%c %.8lux %s<%d>\n", t, v, s, ver);
  405. else
  406. Bprint(&bso, "%c %.8lux %s\n", t, v, s);
  407. }
  408. }
  409. #define MINLC 4
  410. void
  411. asmlc(void)
  412. {
  413. int32_t oldpc, oldlc;
  414. Prog *p;
  415. int32_t v, s;
  416. oldpc = INITTEXT;
  417. oldlc = 0;
  418. for(p = firstp; p != P; p = p->link) {
  419. if(p->line == oldlc || p->as == ATEXT || p->as == ANOP) {
  420. if(p->as == ATEXT)
  421. curtext = p;
  422. if(debug['V'])
  423. Bprint(&bso, "%6lux %P\n",
  424. p->pc, p);
  425. continue;
  426. }
  427. if(debug['V'])
  428. Bprint(&bso, "\t\t%6ld", lcsize);
  429. v = (p->pc - oldpc) / MINLC;
  430. while(v) {
  431. s = 127;
  432. if(v < 127)
  433. s = v;
  434. cput(s+128); /* 129-255 +pc */
  435. if(debug['V'])
  436. Bprint(&bso, " pc+%ld*%d(%ld)", s, MINLC, s+128);
  437. v -= s;
  438. lcsize++;
  439. }
  440. s = p->line - oldlc;
  441. oldlc = p->line;
  442. oldpc = p->pc + MINLC;
  443. if(s > 64 || s < -64) {
  444. cput(0); /* 0 vv +lc */
  445. cput(s>>24);
  446. cput(s>>16);
  447. cput(s>>8);
  448. cput(s);
  449. if(debug['V']) {
  450. if(s > 0)
  451. Bprint(&bso, " lc+%ld(%d,%ld)\n",
  452. s, 0, s);
  453. else
  454. Bprint(&bso, " lc%ld(%d,%ld)\n",
  455. s, 0, s);
  456. Bprint(&bso, "%6lux %P\n",
  457. p->pc, p);
  458. }
  459. lcsize += 5;
  460. continue;
  461. }
  462. if(s > 0) {
  463. cput(0+s); /* 1-64 +lc */
  464. if(debug['V']) {
  465. Bprint(&bso, " lc+%ld(%ld)\n", s, 0+s);
  466. Bprint(&bso, "%6lux %P\n",
  467. p->pc, p);
  468. }
  469. } else {
  470. cput(64-s); /* 65-128 -lc */
  471. if(debug['V']) {
  472. Bprint(&bso, " lc%ld(%ld)\n", s, 64-s);
  473. Bprint(&bso, "%6lux %P\n",
  474. p->pc, p);
  475. }
  476. }
  477. lcsize++;
  478. }
  479. while(lcsize & 1) {
  480. s = 129;
  481. cput(s);
  482. lcsize++;
  483. }
  484. if(debug['v'] || debug['V'])
  485. Bprint(&bso, "lcsize = %ld\n", lcsize);
  486. Bflush(&bso);
  487. }
  488. void
  489. datblk(int32_t s, int32_t n, int str)
  490. {
  491. Sym *v;
  492. Prog *p;
  493. char *cast;
  494. int32_t a, l, fl, j, d;
  495. int i, c;
  496. memset(buf.dbuf, 0, n+100);
  497. for(p = datap; p != P; p = p->link) {
  498. if(str != (p->from.sym->type == SSTRING))
  499. continue;
  500. curp = p;
  501. a = p->from.sym->value + p->from.offset;
  502. l = a - s;
  503. c = p->reg;
  504. i = 0;
  505. if(l < 0) {
  506. if(l+c <= 0)
  507. continue;
  508. while(l < 0) {
  509. l++;
  510. i++;
  511. }
  512. }
  513. if(l >= n)
  514. continue;
  515. if(p->as != AINIT && p->as != ADYNT) {
  516. for(j=l+(c-i)-1; j>=l; j--)
  517. if(buf.dbuf[j]) {
  518. print("%P\n", p);
  519. diag("multiple initialization");
  520. break;
  521. }
  522. }
  523. switch(p->to.type) {
  524. default:
  525. diag("unknown mode in initialization%P", p);
  526. break;
  527. case D_FCONST:
  528. switch(c) {
  529. default:
  530. case 4:
  531. fl = ieeedtof(p->to.ieee);
  532. cast = (char*)&fl;
  533. for(; i<c; i++) {
  534. buf.dbuf[l] = cast[fnuxi4[i]];
  535. l++;
  536. }
  537. break;
  538. case 8:
  539. cast = (char*)p->to.ieee;
  540. for(; i<c; i++) {
  541. buf.dbuf[l] = cast[fnuxi8[i]];
  542. l++;
  543. }
  544. break;
  545. }
  546. break;
  547. case D_SCONST:
  548. for(; i<c; i++) {
  549. buf.dbuf[l] = p->to.sval[i];
  550. l++;
  551. }
  552. break;
  553. case D_CONST:
  554. d = p->to.offset;
  555. v = p->to.sym;
  556. if(v) {
  557. switch(v->type) {
  558. case SUNDEF:
  559. ckoff(v, d);
  560. case STEXT:
  561. case SLEAF:
  562. case SSTRING:
  563. d += p->to.sym->value;
  564. break;
  565. case SDATA:
  566. case SBSS:
  567. d += p->to.sym->value + INITDAT;
  568. }
  569. if(dlm)
  570. dynreloc(v, a+INITDAT, 1);
  571. }
  572. cast = (char*)&d;
  573. switch(c) {
  574. default:
  575. diag("bad nuxi %d %d%P", c, i, curp);
  576. break;
  577. case 1:
  578. for(; i<c; i++) {
  579. buf.dbuf[l] = cast[inuxi1[i]];
  580. l++;
  581. }
  582. break;
  583. case 2:
  584. for(; i<c; i++) {
  585. buf.dbuf[l] = cast[inuxi2[i]];
  586. l++;
  587. }
  588. break;
  589. case 4:
  590. for(; i<c; i++) {
  591. buf.dbuf[l] = cast[inuxi4[i]];
  592. l++;
  593. }
  594. break;
  595. }
  596. break;
  597. }
  598. }
  599. write(cout, buf.dbuf, n);
  600. }
  601. void
  602. asmout(Prog *p, Optab *o)
  603. {
  604. int32_t o1, o2, o3, o4, o5, o6, v;
  605. int r, rf, rt, rt2;
  606. Sym *s;
  607. PP = p;
  608. o1 = 0;
  609. o2 = 0;
  610. o3 = 0;
  611. o4 = 0;
  612. o5 = 0;
  613. o6 = 0;
  614. switch(o->type) {
  615. default:
  616. diag("unknown asm %d", o->type);
  617. prasm(p);
  618. break;
  619. case 0: /* pseudo ops */
  620. break;
  621. case 1: /* op R,[R],R */
  622. o1 = oprrr(p->as, p->scond);
  623. rf = p->from.reg;
  624. rt = p->to.reg;
  625. r = p->reg;
  626. if(p->to.type == D_NONE)
  627. rt = 0;
  628. if(p->as == AMOVW || p->as == AMVN)
  629. r = 0;
  630. else if(r == NREG)
  631. r = rt;
  632. o1 |= rf | (r<<16) | (rt<<12);
  633. break;
  634. case 2: /* movbu $I,[R],R */
  635. aclass(&p->from);
  636. o1 = oprrr(p->as, p->scond);
  637. o1 |= immrot(instoffset);
  638. rt = p->to.reg;
  639. r = p->reg;
  640. if(p->to.type == D_NONE)
  641. rt = 0;
  642. if(p->as == AMOVW || p->as == AMVN)
  643. r = 0;
  644. else if(r == NREG)
  645. r = rt;
  646. o1 |= (r<<16) | (rt<<12);
  647. break;
  648. case 3: /* add R<<[IR],[R],R */
  649. mov:
  650. aclass(&p->from);
  651. o1 = oprrr(p->as, p->scond);
  652. o1 |= p->from.offset;
  653. rt = p->to.reg;
  654. r = p->reg;
  655. if(p->to.type == D_NONE)
  656. rt = 0;
  657. if(p->as == AMOVW || p->as == AMVN)
  658. r = 0;
  659. else if(r == NREG)
  660. r = rt;
  661. o1 |= (r<<16) | (rt<<12);
  662. break;
  663. case 4: /* add $I,[R],R */
  664. aclass(&p->from);
  665. o1 = oprrr(AADD, p->scond);
  666. o1 |= immrot(instoffset);
  667. r = p->from.reg;
  668. if(r == NREG)
  669. r = o->param;
  670. o1 |= r << 16;
  671. o1 |= p->to.reg << 12;
  672. break;
  673. case 5: /* bra s */
  674. v = -8;
  675. if(p->cond == UP) {
  676. s = p->to.sym;
  677. if(s->type != SUNDEF)
  678. diag("bad branch sym type");
  679. v = (uint32_t)s->value >> (Roffset-2);
  680. dynreloc(s, p->pc, 0);
  681. }
  682. else if(p->cond != P)
  683. v = (p->cond->pc - pc) - 8;
  684. o1 = opbra(p->as, p->scond);
  685. o1 |= (v >> 2) & 0xffffff;
  686. break;
  687. case 6: /* b ,O(R) -> add $O,R,PC */
  688. aclass(&p->to);
  689. o1 = oprrr(AADD, p->scond);
  690. o1 |= immrot(instoffset);
  691. o1 |= p->to.reg << 16;
  692. o1 |= REGPC << 12;
  693. break;
  694. case 7: /* bl ,O(R) -> mov PC,link; add $O,R,PC */
  695. aclass(&p->to);
  696. o1 = oprrr(AADD, p->scond);
  697. o1 |= immrot(0);
  698. o1 |= REGPC << 16;
  699. o1 |= REGLINK << 12;
  700. o2 = oprrr(AADD, p->scond);
  701. o2 |= immrot(instoffset);
  702. o2 |= p->to.reg << 16;
  703. o2 |= REGPC << 12;
  704. break;
  705. case 8: /* sll $c,[R],R -> mov (R<<$c),R */
  706. aclass(&p->from);
  707. o1 = oprrr(p->as, p->scond);
  708. r = p->reg;
  709. if(r == NREG)
  710. r = p->to.reg;
  711. o1 |= r;
  712. o1 |= (instoffset&31) << 7;
  713. o1 |= p->to.reg << 12;
  714. break;
  715. case 9: /* sll R,[R],R -> mov (R<<R),R */
  716. o1 = oprrr(p->as, p->scond);
  717. r = p->reg;
  718. if(r == NREG)
  719. r = p->to.reg;
  720. o1 |= r;
  721. o1 |= (p->from.reg << 8) | (1<<4);
  722. o1 |= p->to.reg << 12;
  723. break;
  724. case 10: /* swi [$con] */
  725. o1 = oprrr(p->as, p->scond);
  726. if(p->to.type != D_NONE) {
  727. aclass(&p->to);
  728. o1 |= instoffset & 0xffffff;
  729. }
  730. break;
  731. case 11: /* word */
  732. switch(aclass(&p->to)) {
  733. case C_LCON:
  734. if(!dlm)
  735. break;
  736. if(p->to.name != D_EXTERN && p->to.name != D_STATIC)
  737. break;
  738. case C_ADDR:
  739. if(p->to.sym->type == SUNDEF)
  740. ckoff(p->to.sym, p->to.offset);
  741. dynreloc(p->to.sym, p->pc, 1);
  742. }
  743. o1 = instoffset;
  744. break;
  745. case 12: /* movw $lcon, reg */
  746. o1 = omvl(p, &p->from, p->to.reg);
  747. break;
  748. case 13: /* op $lcon, [R], R */
  749. o1 = omvl(p, &p->from, REGTMP);
  750. if(!o1)
  751. break;
  752. o2 = oprrr(p->as, p->scond);
  753. o2 |= REGTMP;
  754. r = p->reg;
  755. if(p->as == AMOVW || p->as == AMVN)
  756. r = 0;
  757. else if(r == NREG)
  758. r = p->to.reg;
  759. o2 |= r << 16;
  760. if(p->to.type != D_NONE)
  761. o2 |= p->to.reg << 12;
  762. break;
  763. case 14: /* movb/movbu/movh/movhu R,R */
  764. o1 = oprrr(ASLL, p->scond);
  765. if(p->as == AMOVBU || p->as == AMOVHU)
  766. o2 = oprrr(ASRL, p->scond);
  767. else
  768. o2 = oprrr(ASRA, p->scond);
  769. r = p->to.reg;
  770. o1 |= (p->from.reg)|(r<<12);
  771. o2 |= (r)|(r<<12);
  772. if(p->as == AMOVB || p->as == AMOVBU) {
  773. o1 |= (24<<7);
  774. o2 |= (24<<7);
  775. } else {
  776. o1 |= (16<<7);
  777. o2 |= (16<<7);
  778. }
  779. break;
  780. case 15: /* mul r,[r,]r */
  781. o1 = oprrr(p->as, p->scond);
  782. rf = p->from.reg;
  783. rt = p->to.reg;
  784. r = p->reg;
  785. if(r == NREG)
  786. r = rt;
  787. if(rt == r) {
  788. r = rf;
  789. rf = rt;
  790. }
  791. if(0)
  792. if(rt == r || rf == REGPC || r == REGPC || rt == REGPC) {
  793. diag("bad registers in MUL");
  794. prasm(p);
  795. }
  796. o1 |= (rf<<8) | r | (rt<<16);
  797. break;
  798. case 16: /* div r,[r,]r */
  799. o1 = 0xf << 28;
  800. o2 = 0;
  801. break;
  802. case 17:
  803. o1 = oprrr(p->as, p->scond);
  804. rf = p->from.reg;
  805. rt = p->to.reg;
  806. rt2 = p->to.offset;
  807. r = p->reg;
  808. o1 |= (rf<<8) | r | (rt<<16) | (rt2<<12);
  809. break;
  810. case 20: /* mov/movb/movbu R,O(R) */
  811. aclass(&p->to);
  812. r = p->to.reg;
  813. if(r == NREG)
  814. r = o->param;
  815. o1 = osr(p->as, p->from.reg, instoffset, r, p->scond);
  816. break;
  817. case 21: /* mov/movbu O(R),R -> lr */
  818. aclass(&p->from);
  819. r = p->from.reg;
  820. if(r == NREG)
  821. r = o->param;
  822. o1 = olr(instoffset, r, p->to.reg, p->scond);
  823. if(p->as != AMOVW)
  824. o1 |= 1<<22;
  825. break;
  826. case 22: /* movb/movh/movhu O(R),R -> lr,shl,shr */
  827. aclass(&p->from);
  828. r = p->from.reg;
  829. if(r == NREG)
  830. r = o->param;
  831. o1 = olr(instoffset, r, p->to.reg, p->scond);
  832. o2 = oprrr(ASLL, p->scond);
  833. o3 = oprrr(ASRA, p->scond);
  834. r = p->to.reg;
  835. if(p->as == AMOVB) {
  836. o2 |= (24<<7)|(r)|(r<<12);
  837. o3 |= (24<<7)|(r)|(r<<12);
  838. } else {
  839. o2 |= (16<<7)|(r)|(r<<12);
  840. if(p->as == AMOVHU)
  841. o3 = oprrr(ASRL, p->scond);
  842. o3 |= (16<<7)|(r)|(r<<12);
  843. }
  844. break;
  845. case 23: /* movh/movhu R,O(R) -> sb,sb */
  846. aclass(&p->to);
  847. r = p->to.reg;
  848. if(r == NREG)
  849. r = o->param;
  850. o1 = osr(AMOVH, p->from.reg, instoffset, r, p->scond);
  851. o2 = oprrr(ASRL, p->scond);
  852. o2 |= (8<<7)|(p->from.reg)|(REGTMP<<12);
  853. o3 = osr(AMOVH, REGTMP, instoffset+1, r, p->scond);
  854. break;
  855. case 30: /* mov/movb/movbu R,L(R) */
  856. o1 = omvl(p, &p->to, REGTMP);
  857. if(!o1)
  858. break;
  859. r = p->to.reg;
  860. if(r == NREG)
  861. r = o->param;
  862. o2 = osrr(p->from.reg, REGTMP,r, p->scond);
  863. if(p->as != AMOVW)
  864. o2 |= 1<<22;
  865. break;
  866. case 31: /* mov/movbu L(R),R -> lr[b] */
  867. case 32: /* movh/movb L(R),R -> lr[b] */
  868. o1 = omvl(p, &p->from, REGTMP);
  869. if(!o1)
  870. break;
  871. r = p->from.reg;
  872. if(r == NREG)
  873. r = o->param;
  874. o2 = olrr(REGTMP,r, p->to.reg, p->scond);
  875. if(p->as == AMOVBU || p->as == AMOVB)
  876. o2 |= 1<<22;
  877. if(o->type == 31)
  878. break;
  879. o3 = oprrr(ASLL, p->scond);
  880. if(p->as == AMOVBU || p->as == AMOVHU)
  881. o4 = oprrr(ASRL, p->scond);
  882. else
  883. o4 = oprrr(ASRA, p->scond);
  884. r = p->to.reg;
  885. o3 |= (r)|(r<<12);
  886. o4 |= (r)|(r<<12);
  887. if(p->as == AMOVB || p->as == AMOVBU) {
  888. o3 |= (24<<7);
  889. o4 |= (24<<7);
  890. } else {
  891. o3 |= (16<<7);
  892. o4 |= (16<<7);
  893. }
  894. break;
  895. case 33: /* movh/movhu R,L(R) -> sb, sb */
  896. o1 = omvl(p, &p->to, REGTMP);
  897. if(!o1)
  898. break;
  899. r = p->to.reg;
  900. if(r == NREG)
  901. r = o->param;
  902. o2 = osrr(p->from.reg, REGTMP, r, p->scond);
  903. o2 |= (1<<22) ;
  904. o3 = oprrr(ASRL, p->scond);
  905. o3 |= (8<<7)|(p->from.reg)|(p->from.reg<<12);
  906. o3 |= (1<<6); /* ROR 8 */
  907. o4 = oprrr(AADD, p->scond);
  908. o4 |= (REGTMP << 12) | (REGTMP << 16);
  909. o4 |= immrot(1);
  910. o5 = osrr(p->from.reg, REGTMP,r,p->scond);
  911. o5 |= (1<<22);
  912. o6 = oprrr(ASRL, p->scond);
  913. o6 |= (24<<7)|(p->from.reg)|(p->from.reg<<12);
  914. o6 |= (1<<6); /* ROL 8 */
  915. break;
  916. case 34: /* mov $lacon,R */
  917. o1 = omvl(p, &p->from, REGTMP);
  918. if(!o1)
  919. break;
  920. o2 = oprrr(AADD, p->scond);
  921. o2 |= REGTMP;
  922. r = p->from.reg;
  923. if(r == NREG)
  924. r = o->param;
  925. o2 |= r << 16;
  926. if(p->to.type != D_NONE)
  927. o2 |= p->to.reg << 12;
  928. break;
  929. case 35: /* mov PSR,R */
  930. o1 = (2<<23) | (0xf<<16) | (0<<0);
  931. o1 |= (p->scond & C_SCOND) << 28;
  932. o1 |= (p->from.reg & 1) << 22;
  933. o1 |= p->to.reg << 12;
  934. break;
  935. case 36: /* mov R,PSR */
  936. o1 = (2<<23) | (0x29f<<12) | (0<<4);
  937. if(p->scond & C_FBIT)
  938. o1 ^= 0x010 << 12;
  939. o1 |= (p->scond & C_SCOND) << 28;
  940. o1 |= (p->to.reg & 1) << 22;
  941. o1 |= p->from.reg << 0;
  942. break;
  943. case 37: /* mov $con,PSR */
  944. aclass(&p->from);
  945. o1 = (2<<23) | (0x29f<<12) | (0<<4);
  946. if(p->scond & C_FBIT)
  947. o1 ^= 0x010 << 12;
  948. o1 |= (p->scond & C_SCOND) << 28;
  949. o1 |= immrot(instoffset);
  950. o1 |= (p->to.reg & 1) << 22;
  951. o1 |= p->from.reg << 0;
  952. break;
  953. case 38: /* movm $con,oreg -> stm */
  954. o1 = (0x4 << 25);
  955. o1 |= p->from.offset & 0xffff;
  956. o1 |= p->to.reg << 16;
  957. aclass(&p->to);
  958. goto movm;
  959. case 39: /* movm oreg,$con -> ldm */
  960. o1 = (0x4 << 25) | (1 << 20);
  961. o1 |= p->to.offset & 0xffff;
  962. o1 |= p->from.reg << 16;
  963. aclass(&p->from);
  964. movm:
  965. if(instoffset != 0)
  966. diag("offset must be zero in MOVM");
  967. o1 |= (p->scond & C_SCOND) << 28;
  968. if(p->scond & C_PBIT)
  969. o1 |= 1 << 24;
  970. if(p->scond & C_UBIT)
  971. o1 |= 1 << 23;
  972. if(p->scond & C_SBIT)
  973. o1 |= 1 << 22;
  974. if(p->scond & C_WBIT)
  975. o1 |= 1 << 21;
  976. break;
  977. case 40: /* swp oreg,reg,reg */
  978. aclass(&p->from);
  979. if(instoffset != 0)
  980. diag("offset must be zero in SWP");
  981. o1 = (0x2<<23) | (0x9<<4);
  982. if(p->as != ASWPW)
  983. o1 |= 1 << 22;
  984. o1 |= p->from.reg << 16;
  985. o1 |= p->reg << 0;
  986. o1 |= p->to.reg << 12;
  987. o1 |= (p->scond & C_SCOND) << 28;
  988. break;
  989. case 41: /* rfe -> movm.s.w.u 0(r13),[r15] */
  990. o1 = 0xe8fd8000;
  991. break;
  992. case 50: /* floating point store */
  993. v = regoff(&p->to);
  994. r = p->to.reg;
  995. if(r == NREG)
  996. r = o->param;
  997. o1 = ofsr(p->as, p->from.reg, v, r, p->scond, p);
  998. break;
  999. case 51: /* floating point load */
  1000. v = regoff(&p->from);
  1001. r = p->from.reg;
  1002. if(r == NREG)
  1003. r = o->param;
  1004. o1 = ofsr(p->as, p->to.reg, v, r, p->scond, p) | (1<<20);
  1005. break;
  1006. case 52: /* floating point store, long offset UGLY */
  1007. o1 = omvl(p, &p->to, REGTMP);
  1008. if(!o1)
  1009. break;
  1010. r = p->to.reg;
  1011. if(r == NREG)
  1012. r = o->param;
  1013. o2 = oprrr(AADD, p->scond) | (REGTMP << 12) | (REGTMP << 16) | r;
  1014. o3 = ofsr(p->as, p->from.reg, 0, REGTMP, p->scond, p);
  1015. break;
  1016. case 53: /* floating point load, long offset UGLY */
  1017. o1 = omvl(p, &p->from, REGTMP);
  1018. if(!o1)
  1019. break;
  1020. r = p->from.reg;
  1021. if(r == NREG)
  1022. r = o->param;
  1023. o2 = oprrr(AADD, p->scond) | (REGTMP << 12) | (REGTMP << 16) | r;
  1024. o3 = ofsr(p->as, p->to.reg, 0, REGTMP, p->scond, p) | (1<<20);
  1025. break;
  1026. case 54: /* floating point arith */
  1027. o1 = oprrr(p->as, p->scond);
  1028. if(p->from.type == D_FCONST) {
  1029. rf = chipfloat(p->from.ieee);
  1030. if(rf < 0){
  1031. diag("invalid floating-point immediate\n%P", p);
  1032. rf = 0;
  1033. }
  1034. rf |= (1<<3);
  1035. } else
  1036. rf = p->from.reg;
  1037. rt = p->to.reg;
  1038. r = p->reg;
  1039. if(p->to.type == D_NONE)
  1040. rt = 0; /* CMP[FD] */
  1041. else if(o1 & (1<<15))
  1042. r = 0; /* monadic */
  1043. else if(r == NREG)
  1044. r = rt;
  1045. o1 |= rf | (r<<16) | (rt<<12);
  1046. break;
  1047. case 55: /* floating point fix and float */
  1048. o1 = oprrr(p->as, p->scond);
  1049. rf = p->from.reg;
  1050. rt = p->to.reg;
  1051. if(p->to.type == D_NONE){
  1052. rt = 0;
  1053. diag("to.type==D_NONE (asm/fp)");
  1054. }
  1055. if(p->from.type == D_REG)
  1056. o1 |= (rf<<12) | (rt<<16);
  1057. else
  1058. o1 |= rf | (rt<<12);
  1059. break;
  1060. /* old arm 7500 fp using coproc 1 (1<<8) */
  1061. case 56: /* move to FP[CS]R */
  1062. o1 = ((p->scond & C_SCOND) << 28) | (0xe << 24) | (1<<8) | (1<<4);
  1063. o1 |= ((p->to.reg+1)<<21) | (p->from.reg << 12);
  1064. break;
  1065. case 57: /* move from FP[CS]R */
  1066. o1 = ((p->scond & C_SCOND) << 28) | (0xe << 24) | (1<<8) | (1<<4);
  1067. o1 |= ((p->from.reg+1)<<21) | (p->to.reg<<12) | (1<<20);
  1068. break;
  1069. case 58: /* movbu R,R */
  1070. o1 = oprrr(AAND, p->scond);
  1071. o1 |= immrot(0xff);
  1072. rt = p->to.reg;
  1073. r = p->from.reg;
  1074. if(p->to.type == D_NONE)
  1075. rt = 0;
  1076. if(r == NREG)
  1077. r = rt;
  1078. o1 |= (r<<16) | (rt<<12);
  1079. break;
  1080. case 59: /* movw/bu R<<I(R),R -> ldr indexed */
  1081. if(p->from.reg == NREG) {
  1082. if(p->as != AMOVW)
  1083. diag("byte MOV from shifter operand");
  1084. goto mov;
  1085. }
  1086. if(p->from.offset&(1<<4))
  1087. diag("bad shift in LDR");
  1088. o1 = olrr(p->from.offset, p->from.reg, p->to.reg, p->scond);
  1089. if(p->as == AMOVBU)
  1090. o1 |= 1<<22;
  1091. break;
  1092. case 60: /* movb R(R),R -> ldrsb indexed */
  1093. if(p->from.reg == NREG) {
  1094. diag("byte MOV from shifter operand");
  1095. goto mov;
  1096. }
  1097. if(p->from.offset&(~0xf))
  1098. diag("bad shift in LDRSB");
  1099. o1 = olhrr(p->from.offset, p->from.reg, p->to.reg, p->scond);
  1100. o1 ^= (1<<5)|(1<<6);
  1101. break;
  1102. case 61: /* movw/b/bu R,R<<[IR](R) -> str indexed */
  1103. if(p->to.reg == NREG)
  1104. diag("MOV to shifter operand");
  1105. o1 = osrr(p->from.reg, p->to.offset, p->to.reg, p->scond);
  1106. if(p->as == AMOVB || p->as == AMOVBU)
  1107. o1 |= 1<<22;
  1108. break;
  1109. case 62: /* case R -> movw R<<2(PC),PC */
  1110. o1 = olrr(p->from.reg, REGPC, REGPC, p->scond);
  1111. o1 |= 2<<7;
  1112. break;
  1113. case 63: /* bcase */
  1114. if(p->cond != P) {
  1115. o1 = p->cond->pc;
  1116. if(dlm)
  1117. dynreloc(S, p->pc, 1);
  1118. }
  1119. break;
  1120. /* reloc ops */
  1121. case 64: /* mov/movb/movbu R,addr */
  1122. o1 = omvl(p, &p->to, REGTMP);
  1123. if(!o1)
  1124. break;
  1125. o2 = osr(p->as, p->from.reg, 0, REGTMP, p->scond);
  1126. break;
  1127. case 65: /* mov/movbu addr,R */
  1128. case 66: /* movh/movhu/movb addr,R */
  1129. o1 = omvl(p, &p->from, REGTMP);
  1130. if(!o1)
  1131. break;
  1132. o2 = olr(0, REGTMP, p->to.reg, p->scond);
  1133. if(p->as == AMOVBU || p->as == AMOVB)
  1134. o2 |= 1<<22;
  1135. if(o->type == 65)
  1136. break;
  1137. o3 = oprrr(ASLL, p->scond);
  1138. if(p->as == AMOVBU || p->as == AMOVHU)
  1139. o4 = oprrr(ASRL, p->scond);
  1140. else
  1141. o4 = oprrr(ASRA, p->scond);
  1142. r = p->to.reg;
  1143. o3 |= (r)|(r<<12);
  1144. o4 |= (r)|(r<<12);
  1145. if(p->as == AMOVB || p->as == AMOVBU) {
  1146. o3 |= (24<<7);
  1147. o4 |= (24<<7);
  1148. } else {
  1149. o3 |= (16<<7);
  1150. o4 |= (16<<7);
  1151. }
  1152. break;
  1153. case 67: /* movh/movhu R,addr -> sb, sb */
  1154. o1 = omvl(p, &p->to, REGTMP);
  1155. if(!o1)
  1156. break;
  1157. o2 = osr(p->as, p->from.reg, 0, REGTMP, p->scond);
  1158. o3 = oprrr(ASRL, p->scond);
  1159. o3 |= (8<<7)|(p->from.reg)|(p->from.reg<<12);
  1160. o3 |= (1<<6); /* ROR 8 */
  1161. o4 = oprrr(AADD, p->scond);
  1162. o4 |= (REGTMP << 12) | (REGTMP << 16);
  1163. o4 |= immrot(1);
  1164. o5 = osr(p->as, p->from.reg, 0, REGTMP, p->scond);
  1165. o6 = oprrr(ASRL, p->scond);
  1166. o6 |= (24<<7)|(p->from.reg)|(p->from.reg<<12);
  1167. o6 |= (1<<6); /* ROL 8 */
  1168. break;
  1169. case 68: /* floating point store -> ADDR */
  1170. o1 = omvl(p, &p->to, REGTMP);
  1171. if(!o1)
  1172. break;
  1173. o2 = ofsr(p->as, p->from.reg, 0, REGTMP, p->scond, p);
  1174. break;
  1175. case 69: /* floating point load <- ADDR */
  1176. o1 = omvl(p, &p->from, REGTMP);
  1177. if(!o1)
  1178. break;
  1179. o2 = ofsr(p->as, p->to.reg, 0, REGTMP, p->scond, p) | (1<<20);
  1180. break;
  1181. /* ArmV4 ops: */
  1182. case 70: /* movh/movhu R,O(R) -> strh */
  1183. aclass(&p->to);
  1184. r = p->to.reg;
  1185. if(r == NREG)
  1186. r = o->param;
  1187. o1 = oshr(p->from.reg, instoffset, r, p->scond);
  1188. break;
  1189. case 71: /* movb/movh/movhu O(R),R -> ldrsb/ldrsh/ldrh */
  1190. aclass(&p->from);
  1191. r = p->from.reg;
  1192. if(r == NREG)
  1193. r = o->param;
  1194. o1 = olhr(instoffset, r, p->to.reg, p->scond);
  1195. if(p->as == AMOVB)
  1196. o1 ^= (1<<5)|(1<<6);
  1197. else if(p->as == AMOVH)
  1198. o1 ^= (1<<6);
  1199. break;
  1200. case 72: /* movh/movhu R,L(R) -> strh */
  1201. o1 = omvl(p, &p->to, REGTMP);
  1202. if(!o1)
  1203. break;
  1204. r = p->to.reg;
  1205. if(r == NREG)
  1206. r = o->param;
  1207. o2 = oshrr(p->from.reg, REGTMP,r, p->scond);
  1208. break;
  1209. case 73: /* movb/movh/movhu L(R),R -> ldrsb/ldrsh/ldrh */
  1210. o1 = omvl(p, &p->from, REGTMP);
  1211. if(!o1)
  1212. break;
  1213. r = p->from.reg;
  1214. if(r == NREG)
  1215. r = o->param;
  1216. o2 = olhrr(REGTMP, r, p->to.reg, p->scond);
  1217. if(p->as == AMOVB)
  1218. o2 ^= (1<<5)|(1<<6);
  1219. else if(p->as == AMOVH)
  1220. o2 ^= (1<<6);
  1221. break;
  1222. /* VFP ops: */
  1223. case 74: /* vfp floating point arith */
  1224. o1 = opvfprrr(p->as, p->scond);
  1225. rf = p->from.reg;
  1226. if(p->from.type == D_FCONST) {
  1227. diag("invalid floating-point immediate\n%P", p);
  1228. rf = 0;
  1229. }
  1230. rt = p->to.reg;
  1231. r = p->reg;
  1232. if(r == NREG)
  1233. r = rt;
  1234. o1 |= rt<<12;
  1235. if(((o1>>20)&0xf) == 0xb)
  1236. o1 |= rf<<0;
  1237. else
  1238. o1 |= r<<16 | rf<<0;
  1239. break;
  1240. case 75: /* vfp floating point compare */
  1241. o1 = opvfprrr(p->as, p->scond);
  1242. rf = p->from.reg;
  1243. if(p->from.type == D_FCONST) {
  1244. if(p->from.ieee->h != 0 || p->from.ieee->l != 0)
  1245. diag("invalid floating-point immediate\n%P", p);
  1246. o1 |= 1<<16;
  1247. rf = 0;
  1248. }
  1249. rt = p->reg;
  1250. o1 |= rt<<12 | rf<<0;
  1251. o2 = 0x0ef1fa10; /* MRS APSR_nzcv, FPSCR */
  1252. o2 |= (p->scond & C_SCOND) << 28;
  1253. break;
  1254. case 76: /* vfp floating point fix and float */
  1255. o1 = opvfprrr(p->as, p->scond);
  1256. rf = p->from.reg;
  1257. rt = p->to.reg;
  1258. if(p->from.type == D_REG) {
  1259. o2 = o1 | rt<<12 | rt<<0;
  1260. o1 = 0x0e000a10; /* VMOV F,R */
  1261. o1 |= (p->scond & C_SCOND) << 28 | rt<<16 | rf<<12;
  1262. } else {
  1263. o1 |= FREGTMP<<12 | rf<<0;
  1264. o2 = 0x0e100a10; /* VMOV R,F */
  1265. o2 |= (p->scond & C_SCOND) << 28 | FREGTMP<<16 | rt<<12;
  1266. }
  1267. break;
  1268. }
  1269. if(debug['a'] > 1)
  1270. Bprint(&bso, "%2d ", o->type);
  1271. v = p->pc;
  1272. switch(o->size) {
  1273. default:
  1274. if(debug['a'])
  1275. Bprint(&bso, " %.8lux:\t\t%P\n", v, p);
  1276. break;
  1277. case 4:
  1278. if(debug['a'])
  1279. Bprint(&bso, " %.8lux: %.8lux\t%P\n", v, o1, p);
  1280. lputl(o1);
  1281. break;
  1282. case 8:
  1283. if(debug['a'])
  1284. Bprint(&bso, " %.8lux: %.8lux %.8lux%P\n", v, o1, o2, p);
  1285. lputl(o1);
  1286. lputl(o2);
  1287. break;
  1288. case 12:
  1289. if(debug['a'])
  1290. Bprint(&bso, " %.8lux: %.8lux %.8lux %.8lux%P\n", v, o1, o2, o3, p);
  1291. lputl(o1);
  1292. lputl(o2);
  1293. lputl(o3);
  1294. break;
  1295. case 16:
  1296. if(debug['a'])
  1297. Bprint(&bso, " %.8lux: %.8lux %.8lux %.8lux %.8lux%P\n",
  1298. v, o1, o2, o3, o4, p);
  1299. lputl(o1);
  1300. lputl(o2);
  1301. lputl(o3);
  1302. lputl(o4);
  1303. break;
  1304. case 20:
  1305. if(debug['a'])
  1306. Bprint(&bso, " %.8lux: %.8lux %.8lux %.8lux %.8lux %.8lux%P\n",
  1307. v, o1, o2, o3, o4, o5, p);
  1308. lputl(o1);
  1309. lputl(o2);
  1310. lputl(o3);
  1311. lputl(o4);
  1312. lputl(o5);
  1313. break;
  1314. case 24:
  1315. if(debug['a'])
  1316. Bprint(&bso, " %.8lux: %.8lux %.8lux %.8lux %.8lux %.8lux %.8lux%P\n",
  1317. v, o1, o2, o3, o4, o5, o6, p);
  1318. lputl(o1);
  1319. lputl(o2);
  1320. lputl(o3);
  1321. lputl(o4);
  1322. lputl(o5);
  1323. lputl(o6);
  1324. break;
  1325. }
  1326. }
  1327. int32_t
  1328. oprrr(int a, int sc)
  1329. {
  1330. int32_t o;
  1331. o = (sc & C_SCOND) << 28;
  1332. if(sc & C_SBIT)
  1333. o |= 1 << 20;
  1334. if(sc & (C_PBIT|C_WBIT))
  1335. diag(".P/.W on dp instruction");
  1336. switch(a) {
  1337. case AMULU:
  1338. case AMUL: return o | (0x0<<21) | (0x9<<4);
  1339. case AMULA: return o | (0x1<<21) | (0x9<<4);
  1340. case AMULLU: return o | (0x4<<21) | (0x9<<4);
  1341. case AMULL: return o | (0x6<<21) | (0x9<<4);
  1342. case AMULALU: return o | (0x5<<21) | (0x9<<4);
  1343. case AMULAL: return o | (0x7<<21) | (0x9<<4);
  1344. case AAND: return o | (0x0<<21);
  1345. case AEOR: return o | (0x1<<21);
  1346. case ASUB: return o | (0x2<<21);
  1347. case ARSB: return o | (0x3<<21);
  1348. case AADD: return o | (0x4<<21);
  1349. case AADC: return o | (0x5<<21);
  1350. case ASBC: return o | (0x6<<21);
  1351. case ARSC: return o | (0x7<<21);
  1352. case ATST: return o | (0x8<<21) | (1<<20);
  1353. case ATEQ: return o | (0x9<<21) | (1<<20);
  1354. case ACMP: return o | (0xa<<21) | (1<<20);
  1355. case ACMN: return o | (0xb<<21) | (1<<20);
  1356. case AORR: return o | (0xc<<21);
  1357. case AMOVW: return o | (0xd<<21);
  1358. case ABIC: return o | (0xe<<21);
  1359. case AMVN: return o | (0xf<<21);
  1360. case ASLL: return o | (0xd<<21) | (0<<5);
  1361. case ASRL: return o | (0xd<<21) | (1<<5);
  1362. case ASRA: return o | (0xd<<21) | (2<<5);
  1363. case ASWI: return o | (0xf<<24);
  1364. /* old arm 7500 fp using coproc 1 (1<<8) */
  1365. case AADDD: return o | (0xe<<24) | (0x0<<20) | (1<<8) | (1<<7);
  1366. case AADDF: return o | (0xe<<24) | (0x0<<20) | (1<<8);
  1367. case AMULD: return o | (0xe<<24) | (0x1<<20) | (1<<8) | (1<<7);
  1368. case AMULF: return o | (0xe<<24) | (0x1<<20) | (1<<8);
  1369. case ASUBD: return o | (0xe<<24) | (0x2<<20) | (1<<8) | (1<<7);
  1370. case ASUBF: return o | (0xe<<24) | (0x2<<20) | (1<<8);
  1371. case ADIVD: return o | (0xe<<24) | (0x4<<20) | (1<<8) | (1<<7);
  1372. case ADIVF: return o | (0xe<<24) | (0x4<<20) | (1<<8);
  1373. case ACMPD:
  1374. case ACMPF: return o | (0xe<<24) | (0x9<<20) | (0xF<<12) | (1<<8) | (1<<4); /* arguably, ACMPF should expand to RNDF, CMPD */
  1375. case AMOVF:
  1376. case AMOVDF: return o | (0xe<<24) | (0x0<<20) | (1<<15) | (1<<8);
  1377. case AMOVD:
  1378. case AMOVFD: return o | (0xe<<24) | (0x0<<20) | (1<<15) | (1<<8) | (1<<7);
  1379. case AMOVWF: return o | (0xe<<24) | (0<<20) | (1<<8) | (1<<4);
  1380. case AMOVWD: return o | (0xe<<24) | (0<<20) | (1<<8) | (1<<4) | (1<<7);
  1381. case AMOVFW: return o | (0xe<<24) | (1<<20) | (1<<8) | (1<<4);
  1382. case AMOVDW: return o | (0xe<<24) | (1<<20) | (1<<8) | (1<<4) | (1<<7);
  1383. }
  1384. diag("bad rrr %d", a);
  1385. prasm(curp);
  1386. return 0;
  1387. }
  1388. int32_t
  1389. opvfprrr(int a, int sc)
  1390. {
  1391. int32_t o;
  1392. o = (sc & C_SCOND) << 28;
  1393. if(sc & (C_SBIT|C_PBIT|C_WBIT))
  1394. diag(".S/.P/.W on vfp instruction");
  1395. o |= 0xe<<24;
  1396. switch(a) {
  1397. case AMOVWD: return o | 0xb<<8 | 0xb<<20 | 1<<6 | 0x8<<16 | 1<<7;
  1398. case AMOVWF: return o | 0xa<<8 | 0xb<<20 | 1<<6 | 0x8<<16 | 1<<7;
  1399. case AMOVDW: return o | 0xb<<8 | 0xb<<20 | 1<<6 | 0xD<<16 | 1<<7;
  1400. case AMOVFW: return o | 0xa<<8 | 0xb<<20 | 1<<6 | 0xD<<16 | 1<<7;
  1401. case AMOVFD: return o | 0xa<<8 | 0xb<<20 | 1<<6 | 0x7<<16 | 1<<7;
  1402. case AMOVDF: return o | 0xb<<8 | 0xb<<20 | 1<<6 | 0x7<<16 | 1<<7;
  1403. case AMOVF: return o | 0xa<<8 | 0xb<<20 | 1<<6 | 0x0<<16 | 0<<7;
  1404. case AMOVD: return o | 0xb<<8 | 0xb<<20 | 1<<6 | 0x0<<16 | 0<<7;
  1405. case ACMPF: return o | 0xa<<8 | 0xb<<20 | 1<<6 | 0x4<<16 | 0<<7;
  1406. case ACMPD: return o | 0xb<<8 | 0xb<<20 | 1<<6 | 0x4<<16 | 0<<7;
  1407. case AADDF: return o | 0xa<<8 | 0x3<<20;
  1408. case AADDD: return o | 0xb<<8 | 0x3<<20;
  1409. case ASUBF: return o | 0xa<<8 | 0x3<<20 | 1<<6;
  1410. case ASUBD: return o | 0xb<<8 | 0x3<<20 | 1<<6;
  1411. case AMULF: return o | 0xa<<8 | 0x2<<20;
  1412. case AMULD: return o | 0xb<<8 | 0x2<<20;
  1413. case ADIVF: return o | 0xa<<8 | 0x8<<20;
  1414. case ADIVD: return o | 0xb<<8 | 0x8<<20;
  1415. }
  1416. diag("bad vfp rrr %d", a);
  1417. prasm(curp);
  1418. return 0;
  1419. }
  1420. int32_t
  1421. opbra(int a, int sc)
  1422. {
  1423. if(sc & (C_SBIT|C_PBIT|C_WBIT))
  1424. diag(".S/.P/.W on bra instruction");
  1425. sc &= C_SCOND;
  1426. if(a == ABL)
  1427. return (sc<<28)|(0x5<<25)|(0x1<<24);
  1428. if(sc != 0xe)
  1429. diag(".COND on bcond instruction");
  1430. switch(a) {
  1431. case ABEQ: return (0x0<<28)|(0x5<<25);
  1432. case ABNE: return (0x1<<28)|(0x5<<25);
  1433. case ABCS: return (0x2<<28)|(0x5<<25);
  1434. case ABHS: return (0x2<<28)|(0x5<<25);
  1435. case ABCC: return (0x3<<28)|(0x5<<25);
  1436. case ABLO: return (0x3<<28)|(0x5<<25);
  1437. case ABMI: return (0x4<<28)|(0x5<<25);
  1438. case ABPL: return (0x5<<28)|(0x5<<25);
  1439. case ABVS: return (0x6<<28)|(0x5<<25);
  1440. case ABVC: return (0x7<<28)|(0x5<<25);
  1441. case ABHI: return (0x8<<28)|(0x5<<25);
  1442. case ABLS: return (0x9<<28)|(0x5<<25);
  1443. case ABGE: return (0xa<<28)|(0x5<<25);
  1444. case ABLT: return (0xb<<28)|(0x5<<25);
  1445. case ABGT: return (0xc<<28)|(0x5<<25);
  1446. case ABLE: return (0xd<<28)|(0x5<<25);
  1447. case AB: return (0xe<<28)|(0x5<<25);
  1448. }
  1449. diag("bad bra %A", a);
  1450. prasm(curp);
  1451. return 0;
  1452. }
  1453. int32_t
  1454. olr(int32_t v, int b, int r, int sc)
  1455. {
  1456. int32_t o;
  1457. if(sc & C_SBIT)
  1458. diag(".S on LDR/STR instruction");
  1459. o = (sc & C_SCOND) << 28;
  1460. if(!(sc & C_PBIT))
  1461. o |= 1 << 24;
  1462. if(!(sc & C_UBIT))
  1463. o |= 1 << 23;
  1464. if(sc & C_WBIT)
  1465. o |= 1 << 21;
  1466. o |= (0x1<<26) | (1<<20);
  1467. if(v < 0) {
  1468. v = -v;
  1469. o ^= 1 << 23;
  1470. }
  1471. if(v >= (1<<12))
  1472. diag("literal span too large: %ld (R%d)\n%P", v, b, PP);
  1473. o |= v;
  1474. o |= b << 16;
  1475. o |= r << 12;
  1476. return o;
  1477. }
  1478. int32_t
  1479. olhr(int32_t v, int b, int r, int sc)
  1480. {
  1481. int32_t o;
  1482. if(sc & C_SBIT)
  1483. diag(".S on LDRH/STRH instruction");
  1484. o = (sc & C_SCOND) << 28;
  1485. if(!(sc & C_PBIT))
  1486. o |= 1 << 24;
  1487. if(sc & C_WBIT)
  1488. o |= 1 << 21;
  1489. o |= (1<<23) | (1<<20)|(0xb<<4);
  1490. if(v < 0) {
  1491. v = -v;
  1492. o ^= 1 << 23;
  1493. }
  1494. if(v >= (1<<8))
  1495. diag("literal span too large: %ld (R%d)\n%P", v, b, PP);
  1496. o |= (v&0xf)|((v>>4)<<8)|(1<<22);
  1497. o |= b << 16;
  1498. o |= r << 12;
  1499. return o;
  1500. }
  1501. int32_t
  1502. osr(int a, int r, int32_t v, int b, int sc)
  1503. {
  1504. int32_t o;
  1505. o = olr(v, b, r, sc) ^ (1<<20);
  1506. if(a != AMOVW)
  1507. o |= 1<<22;
  1508. return o;
  1509. }
  1510. int32_t
  1511. oshr(int r, int32_t v, int b, int sc)
  1512. {
  1513. int32_t o;
  1514. o = olhr(v, b, r, sc) ^ (1<<20);
  1515. return o;
  1516. }
  1517. int32_t
  1518. osrr(int r, int i, int b, int sc)
  1519. {
  1520. return olr(i, b, r, sc) ^ ((1<<25) | (1<<20));
  1521. }
  1522. int32_t
  1523. oshrr(int r, int i, int b, int sc)
  1524. {
  1525. return olhr(i, b, r, sc) ^ ((1<<22) | (1<<20));
  1526. }
  1527. int32_t
  1528. olrr(int i, int b, int r, int sc)
  1529. {
  1530. return olr(i, b, r, sc) ^ (1<<25);
  1531. }
  1532. int32_t
  1533. olhrr(int i, int b, int r, int sc)
  1534. {
  1535. return olhr(i, b, r, sc) ^ (1<<22);
  1536. }
  1537. int32_t
  1538. ovfpmem(int a, int r, int32_t v, int b, int sc, Prog *p)
  1539. {
  1540. int32_t o;
  1541. if(sc & (C_SBIT|C_PBIT|C_WBIT))
  1542. diag(".S/.P/.W on VLDR/VSTR instruction");
  1543. o = (sc & C_SCOND) << 28;
  1544. o |= 0xd<<24 | (1<<23);
  1545. if(v < 0) {
  1546. v = -v;
  1547. o ^= 1 << 23;
  1548. }
  1549. if(v & 3)
  1550. diag("odd offset for floating point op: %ld\n%P", v, p);
  1551. else if(v >= (1<<10))
  1552. diag("literal span too large: %ld\n%P", v, p);
  1553. o |= (v>>2) & 0xFF;
  1554. o |= b << 16;
  1555. o |= r << 12;
  1556. switch(a) {
  1557. default:
  1558. diag("bad fst %A", a);
  1559. case AMOVD:
  1560. o |= 0xb<<8;
  1561. break;
  1562. case AMOVF:
  1563. o |= 0xa<<8;
  1564. break;
  1565. }
  1566. return o;
  1567. }
  1568. int32_t
  1569. ofsr(int a, int r, int32_t v, int b, int sc, Prog *p)
  1570. {
  1571. int32_t o;
  1572. if(vfp)
  1573. return ovfpmem(a, r, v, b, sc, p);
  1574. if(sc & C_SBIT)
  1575. diag(".S on FLDR/FSTR instruction");
  1576. o = (sc & C_SCOND) << 28;
  1577. if(!(sc & C_PBIT))
  1578. o |= 1 << 24;
  1579. if(sc & C_WBIT)
  1580. o |= 1 << 21;
  1581. o |= (6<<25) | (1<<24) | (1<<23);
  1582. if(v < 0) {
  1583. v = -v;
  1584. o ^= 1 << 23;
  1585. }
  1586. if(v & 3)
  1587. diag("odd offset for floating point op: %ld\n%P", v, p);
  1588. else if(v >= (1<<10))
  1589. diag("literal span too large: %ld\n%P", v, p);
  1590. o |= (v>>2) & 0xFF;
  1591. o |= b << 16;
  1592. o |= r << 12;
  1593. o |= 1 << 8;
  1594. switch(a) {
  1595. default:
  1596. diag("bad fst %A", a);
  1597. case AMOVD:
  1598. o |= 1<<15;
  1599. case AMOVF:
  1600. break;
  1601. }
  1602. return o;
  1603. }
  1604. int32_t
  1605. omvl(Prog *p, Adr *a, int dr)
  1606. {
  1607. int32_t v, o1;
  1608. if(!p->cond) {
  1609. aclass(a);
  1610. v = immrot(~instoffset);
  1611. if(v == 0) {
  1612. diag("missing literal");
  1613. prasm(p);
  1614. return 0;
  1615. }
  1616. o1 = oprrr(AMVN, p->scond&C_SCOND);
  1617. o1 |= v;
  1618. o1 |= dr << 12;
  1619. } else {
  1620. v = p->cond->pc - p->pc - 8;
  1621. o1 = olr(v, REGPC, dr, p->scond&C_SCOND);
  1622. }
  1623. return o1;
  1624. }
  1625. static Ieee chipfloats[] = {
  1626. {0x00000000, 0x00000000}, /* 0 */
  1627. {0x00000000, 0x3ff00000}, /* 1 */
  1628. {0x00000000, 0x40000000}, /* 2 */
  1629. {0x00000000, 0x40080000}, /* 3 */
  1630. {0x00000000, 0x40100000}, /* 4 */
  1631. {0x00000000, 0x40140000}, /* 5 */
  1632. {0x00000000, 0x3fe00000}, /* .5 */
  1633. {0x00000000, 0x40240000}, /* 10 */
  1634. };
  1635. int
  1636. chipfloat(Ieee *e)
  1637. {
  1638. Ieee *p;
  1639. int n;
  1640. if(vfp)
  1641. return -1;
  1642. for(n = sizeof(chipfloats)/sizeof(chipfloats[0]); --n >= 0;){
  1643. p = &chipfloats[n];
  1644. if(p->l == e->l && p->h == e->h)
  1645. return n;
  1646. }
  1647. return -1;
  1648. }