mach5.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /* $Header$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. /*
  7. * Motorola 68020 auxiliary functions
  8. */
  9. /* With pc-relative modes the offset is calulated from the address of the
  10. * extension word. This address is not known until the instruction opcode(s)
  11. * have been emitted. Since this address is unknown, the offset from pc
  12. * cannot be calculated correctly, so it cannot immediately be decided whether
  13. * to use mode 072 (pc-relative with 16 bit offset) or mode 073 (pc_relative
  14. * with possibly 32 bit offset) Because of this, instruction opcodes
  15. * are not really emitted right away, but temporarily stored. This way
  16. * the address of the extension word is known so the offset can be calculated
  17. * correctly and it then can be decided to use mode 072 or 073; this can be
  18. * altered in the instruction opcode, if necessary. For the sake of consistency
  19. * the effective address(es) are also stored temporarily. The instruction is
  20. * then emitted in one go, by emit_instr().
  21. */
  22. emit_instr()
  23. {
  24. register instr_t *ip;
  25. for (ip=instr; ip<instrp; emit2(ip++->i_word)) {
  26. #ifdef RELOCATION
  27. RELOMOVE(relonami, ip->i_relonami);
  28. if (ip->i_reloinfo)
  29. newrelo(ip->i_relotype, ip->i_reloinfo | RELBR | RELWR);
  30. #endif
  31. }
  32. }
  33. #ifdef RELOCATION
  34. t_emit2(word, relotype, reloinfo, relnm)
  35. short word;
  36. short relotype;
  37. valu_t relnm;
  38. #else
  39. t_emit2(word)
  40. short word;
  41. #endif
  42. {
  43. #ifdef RELOCATION
  44. if (instrp->i_reloinfo = reloinfo) {
  45. RELOMOVE(instrp->i_relonami, relnm);
  46. instrp->i_relotype = relotype;
  47. }
  48. #endif
  49. instrp++->i_word = word;
  50. dot_offset += 2;
  51. }
  52. #ifdef RELOCATION
  53. t_emit4(words, relotype, reloinfo, relnm)
  54. long words;
  55. short relotype;
  56. valu_t relnm;
  57. #else
  58. t_emit4(words)
  59. long words;
  60. #endif
  61. {
  62. T_EMIT2((short)(words>>16), relotype, reloinfo, relnm);
  63. T_EMIT2((short)(words), 0, 0, 0);
  64. }
  65. ea_1(sz, bits)
  66. {
  67. /* Because displacements come in three sizes (null displacement,
  68. * word and long displacement), each displacement requires
  69. * two bits in the bittable, so two calls to small. Sometimes
  70. * one of these calls is a dummy call.
  71. */
  72. register flag;
  73. register sm, sm1, sm2;
  74. if (mrg_1 > 074)
  75. serror("no specials");
  76. if ((flag = eamode[mrg_1>>3]) == 0)
  77. if ((flag = eamode[010 + (mrg_1&07)]) == 0)
  78. flag = eamode[015 + (sz>>6)];
  79. if ((mrg_1 & 070) == 010)
  80. checksize(sz, 2|4);
  81. bits &= ~flag;
  82. if (bits)
  83. serror("bad addressing category");
  84. if (mrg_1==073 && (ffew_1 & 0200) == 0 && (bd_1.typ & ~S_DOT) == DOTTYP)
  85. bd_1.val -= (DOTVAL + dot_offset);
  86. if ( (mrg_1==073) || (mrg_1&070)==060 ) {
  87. sm = (
  88. (mrg_1==073 && (bd_1.typ & ~S_DOT)==DOTTYP)
  89. ||
  90. (bd_1.typ == S_ABS)
  91. );
  92. if (small(sm && fitw(bd_1.val), 2)) {
  93. sm = (
  94. (sm1 = ((ffew_1 & 0307)==0 && fitb(bd_1.val)))
  95. ||
  96. (sm2 = ((ffew_1 & 0307)==0100 && mrg_1==073))
  97. ||
  98. (bd_1.val==0)
  99. );
  100. if (small(sm,2)) {
  101. if (sm1) { /* brief format extension */
  102. T_EMIT2((ffew_1&0177000) | lowb(bd_1.val),
  103. 0, 0, 0);
  104. return;
  105. }
  106. if (sm2) {
  107. /* change mode to 072 in opcode word */
  108. instr->i_word &= ~1;
  109. T_EMIT2(loww(bd_1.val), 0, 0, 0);
  110. return;
  111. }
  112. ffew_1 &= ~040; /* null displacement */
  113. }
  114. else
  115. ffew_1 &= ~020; /* word displacement */
  116. } else
  117. sm = small(0,2); /* dummy call */
  118. if (ffew_1 & 3) {
  119. sm = (od_1.typ == S_ABS);
  120. if (small(sm && fitw(od_1.val), 2))
  121. ffew_1 &= small(od_1.val==0, 2) ? ~2 : ~1;
  122. else
  123. sm = small(0,2); /* dummy call */
  124. }
  125. assert((ffew_1 & 0410) == 0400);
  126. T_EMIT2(ffew_1, 0, 0, 0);
  127. assert(ffew_1 & 060);
  128. switch(ffew_1 & 060) {
  129. case 020:
  130. break;
  131. case 040:
  132. T_EMIT2(loww(bd_1.val), 0, 0, 0);
  133. break;
  134. case 060:
  135. T_EMIT4( bd_1.val,
  136. bd_1.typ,
  137. (mrg_1 == 073 && (ffew_1 & 0200) == 0)
  138. ? RELPC|RELO4
  139. : RELO4,
  140. bd_rel1
  141. );
  142. }
  143. if (ffew_1 & 3) {
  144. switch(ffew_1 & 3) {
  145. case 1:
  146. break;
  147. case 2:
  148. T_EMIT2(loww(od_1.val), 0, 0, 0);
  149. break;
  150. case 3:
  151. T_EMIT4(od_1.val, od_1.typ, RELO4, od_rel1);
  152. }
  153. }
  154. return; /* mode 060 and 073 have been dealt with */
  155. }
  156. if (flag & FITW)
  157. if (
  158. ! fitw(bd_1.val)
  159. &&
  160. (mrg_1 != 074 || ! fit16(bd_1.val))
  161. )
  162. nofit();
  163. if (flag & FITB) {
  164. if (
  165. ! fitb(bd_1.val)
  166. &&
  167. (mrg_1 != 074 || ! fit8(bd_1.val))
  168. )
  169. nofit();
  170. if (mrg_1 == 074)
  171. bd_1.val &= 0xFF;
  172. }
  173. if (flag & PUTL)
  174. T_EMIT4(bd_1.val, bd_1.typ, (flag>>8), bd_rel1);
  175. if (flag & PUTW)
  176. T_EMIT2(loww(bd_1.val), bd_1.typ, (flag>>8), bd_rel1);
  177. }
  178. ea_2(sz, bits)
  179. {
  180. mrg_1 = mrg_2;
  181. bd_1 = bd_2;
  182. od_1 = od_2;
  183. ffew_1 = ffew_2;
  184. RELOMOVE(bd_rel1, bd_rel2);
  185. RELOMOVE(od_rel1, od_rel2);
  186. ea_1(sz, bits);
  187. }
  188. checksize(sz, bits)
  189. {
  190. if ((bits & (1 << (sz>>6))) == 0)
  191. serror("bad size");
  192. }
  193. checkscale(val)
  194. valu_t val;
  195. {
  196. switch(val) {
  197. case 1: return 0;
  198. case 2: return 1<<9;
  199. case 4: return 2<<9;
  200. case 8: return 3<<9;
  201. default: serror("bad scale"); return 0;
  202. }
  203. }
  204. badoperand()
  205. {
  206. serror("bad operands");
  207. }
  208. shift_op(opc, sz)
  209. {
  210. if (mrg_1 < 010 && mrg_2 < 010) {
  211. T_EMIT2((opc & 0170470) | sz | mrg_1<<9 | mrg_2, 0, 0, 0);
  212. return;
  213. }
  214. if (bd_1.typ != S_ABS || mrg_1 != 074) {
  215. badoperand();
  216. return;
  217. }
  218. if (mrg_2 < 010) {
  219. fit(fit3(bd_1.val));
  220. T_EMIT2((opc & 0170430) | sz | low3(bd_1.val)<<9 | mrg_2,0,0,0);
  221. return;
  222. }
  223. checksize(sz, 2);
  224. fit(bd_1.val == 1);
  225. T_EMIT2((opc & 0177700) | mrg_2, 0, 0, 0);
  226. ea_2(SIZE_W, MEM|ALT);
  227. }
  228. bitop(opc)
  229. {
  230. register bits;
  231. bits = DTA|ALT;
  232. if (opc == 0 && (mrg_1 < 010 || mrg_2 != 074))
  233. bits = DTA;
  234. if (mrg_1 < 010) {
  235. T_EMIT2(opc | 0400 | mrg_1<<9 | mrg_2, 0, 0, 0);
  236. ea_2(0, bits);
  237. return;
  238. }
  239. if (mrg_1 == 074) {
  240. T_EMIT2(opc | 04000 | mrg_2, 0, 0, 0);
  241. ea_1(SIZE_W, 0);
  242. ea_2(0, bits);
  243. return;
  244. }
  245. badoperand();
  246. }
  247. bitfield(opc, extension)
  248. {
  249. T_EMIT2(opc | mrg_2, 0, 0, 0);
  250. T_EMIT2(extension, 0, 0, 0);
  251. ea_2(SIZE_L, (mrg_2 < 010) ? 0 : (CTR | ALT));
  252. }
  253. add(opc, sz)
  254. {
  255. if ((mrg_2 & 070) == 010)
  256. checksize(sz, 2|4);
  257. if (
  258. mrg_1 == 074
  259. &&
  260. small(
  261. bd_1.typ==S_ABS && fit3(bd_1.val),
  262. sz==SIZE_L ? 4 : 2
  263. )
  264. ) {
  265. T_EMIT2((opc&0400) | 050000 | low3(bd_1.val)<<9 | sz | mrg_2,
  266. 0, 0, 0);
  267. ea_2(sz, ALT);
  268. return;
  269. }
  270. if (mrg_1 == 074 && (mrg_2 & 070) != 010) {
  271. T_EMIT2((opc&03000) | sz | mrg_2, 0, 0, 0);
  272. ea_1(sz, 0);
  273. ea_2(sz, DTA|ALT);
  274. return;
  275. }
  276. if ((mrg_2 & 070) == 010) {
  277. T_EMIT2((opc&0170300) | (mrg_2&7)<<9 | sz<<1 | mrg_1, 0, 0, 0);
  278. ea_1(sz, 0);
  279. return;
  280. }
  281. if (to_dreg(opc, sz, 0))
  282. return;
  283. if (from_dreg(opc, sz, ALT|MEM))
  284. return;
  285. badoperand();
  286. }
  287. and(opc, sz)
  288. {
  289. if (mrg_1 == 074 && mrg_2 >= 076) { /* ccr or sr */
  290. if (sz != SIZE_NON)
  291. checksize(sz, mrg_2==076 ? 1 : 2);
  292. else
  293. sz = (mrg_2==076 ? SIZE_B : SIZE_W);
  294. T_EMIT2((opc&07400) | sz | 074, 0, 0, 0);
  295. ea_1(sz, 0);
  296. return;
  297. }
  298. if (sz == SIZE_NON)
  299. sz = SIZE_DEF;
  300. if (mrg_1 == 074) {
  301. T_EMIT2((opc&07400) | sz | mrg_2, 0, 0, 0);
  302. ea_1(sz, 0);
  303. ea_2(sz, DTA|ALT);
  304. return;
  305. }
  306. if ((opc & 010000) == 0 && to_dreg(opc, sz, DTA))
  307. return;
  308. if (from_dreg(opc, sz, (opc & 010000) ? DTA|ALT : ALT|MEM))
  309. return;
  310. badoperand();
  311. }
  312. to_dreg(opc, sz, bits)
  313. {
  314. if ((mrg_2 & 070) != 000)
  315. return(0);
  316. T_EMIT2((opc & 0170000) | sz | (mrg_2&7)<<9 | mrg_1, 0, 0, 0);
  317. ea_1(sz, bits);
  318. return(1);
  319. }
  320. from_dreg(opc, sz, bits)
  321. {
  322. if ((mrg_1 & 070) != 000)
  323. return(0);
  324. T_EMIT2((opc & 0170000) | sz | (mrg_1&7)<<9 | 0400 | mrg_2, 0, 0, 0);
  325. ea_2(sz, bits);
  326. return(1);
  327. }
  328. cmp(sz)
  329. {
  330. register opc;
  331. if ((mrg_1&070) == 030 && (mrg_2&070) == 030) {
  332. T_EMIT2(0130410 | sz | (mrg_1&7) | (mrg_2&7)<<9, 0, 0, 0);
  333. return;
  334. }
  335. if (mrg_1 == 074 && (mrg_2 & 070) != 010) {
  336. if (mrg_2==074)
  337. badoperand();
  338. T_EMIT2(06000 | sz | mrg_2, 0, 0, 0);
  339. ea_1(sz, 0);
  340. ea_2(sz, DTA);
  341. return;
  342. }
  343. if (mrg_2 < 020) {
  344. if (mrg_2 >= 010) {
  345. checksize(sz, 2|4);
  346. opc = 0130300 | sz<<1;
  347. mrg_2 &= 7;
  348. } else
  349. opc = 0130000 | sz;
  350. T_EMIT2(opc | mrg_2<<9 | mrg_1, 0, 0, 0);
  351. ea_1(sz, 0);
  352. return;
  353. }
  354. badoperand();
  355. }
  356. link(sz, areg)
  357. {
  358. if (sz == SIZE_NON) {
  359. if (bd_2.typ == S_ABS && fitw(bd_2.val))
  360. sz = SIZE_W;
  361. else
  362. sz = SIZE_L;
  363. }
  364. checksize(sz, 2|4);
  365. if (sz == SIZE_W)
  366. T_EMIT2(047120 | areg, 0, 0, 0);
  367. else /* sz == SIZE_L */
  368. T_EMIT2(044010 | areg, 0, 0, 0);
  369. ea_2(sz, 0);
  370. }
  371. move(sz)
  372. {
  373. register opc;
  374. if (mrg_1 > 074 || mrg_2 > 074) {
  375. move_special(sz);
  376. return;
  377. }
  378. if (sz == SIZE_NON)
  379. sz = SIZE_DEF;
  380. if (
  381. mrg_2<010
  382. &&
  383. mrg_1==074
  384. &&
  385. sz==SIZE_L
  386. &&
  387. small(bd_1.typ==S_ABS && fitb(bd_1.val), 4)
  388. ) {
  389. T_EMIT2(070000 | mrg_2<<9 | lowb(bd_1.val), 0, 0, 0);
  390. return;
  391. }
  392. switch (sz) {
  393. case SIZE_B: opc = 010000; break;
  394. case SIZE_W: opc = 030000; break;
  395. case SIZE_L: opc = 020000; break;
  396. }
  397. T_EMIT2(opc | mrg_1 | (mrg_2&7)<<9 | (mrg_2&070)<<3, 0, 0, 0);
  398. ea_1(sz, 0);
  399. ea_2(sz, ALT);
  400. }
  401. move_special(sz)
  402. {
  403. if (mrg_2 >= 076) {
  404. if (sz != SIZE_NON)
  405. checksize(sz, 2);
  406. T_EMIT2(042300 | (mrg_2==076?0:01000) | mrg_1, 0, 0, 0);
  407. ea_1(SIZE_W, DTA);
  408. return;
  409. }
  410. if (mrg_1 >= 076) {
  411. if (sz != SIZE_NON)
  412. checksize(sz, 2);
  413. T_EMIT2(040300 | (mrg_1==076?01000:0) | mrg_2, 0, 0, 0);
  414. ea_2(SIZE_W, DTA|ALT);
  415. return;
  416. }
  417. if (sz != SIZE_NON)
  418. checksize(sz, 4);
  419. if (mrg_1==075 && (mrg_2&070)==010) {
  420. T_EMIT2(047150 | (mrg_2&7), 0, 0, 0);
  421. return;
  422. }
  423. if (mrg_2==075 && (mrg_1&070)==010) {
  424. T_EMIT2(047140 | (mrg_1&7), 0, 0, 0);
  425. return;
  426. }
  427. badoperand();
  428. }
  429. movem(dr, sz, regs)
  430. {
  431. register i;
  432. register r;
  433. if ((mrg_2>>3) == 04) {
  434. r = regs; regs = 0;
  435. for (i = 0; i < 16; i++) {
  436. regs <<= 1;
  437. if (r & 1)
  438. regs++;
  439. r >>= 1;
  440. }
  441. }
  442. checksize(sz, 2|4);
  443. if ((mrg_2>>3)-3 == dr)
  444. badoperand();
  445. T_EMIT2(044200 | dr<<10 | (sz & 0200) >> 1 | mrg_2, 0, 0, 0);
  446. T_EMIT2(regs, 0, 0, 0);
  447. i = CTR;
  448. if (dr == 0 && (mrg_2&070) == 040)
  449. i = MEM;
  450. if (dr != 0 && (mrg_2&070) == 030)
  451. i = MEM;
  452. if (dr == 0)
  453. i |= ALT;
  454. ea_2(sz, i);
  455. }
  456. movep(sz)
  457. {
  458. checksize(sz, 2|4);
  459. if (mrg_1<010 && (mrg_2&070)==050) {
  460. T_EMIT2(0610 | (sz & 0200)>>1 | mrg_1<<9 | (mrg_2&7), 0, 0, 0);
  461. ea_2(sz, 0);
  462. return;
  463. }
  464. if (mrg_2<010 && (mrg_1&070)==050) {
  465. T_EMIT2(0410 | (sz & 0200)>>1 | mrg_2<<9 | (mrg_1&7), 0, 0, 0);
  466. ea_1(sz, 0);
  467. return;
  468. }
  469. badoperand();
  470. }
  471. branch(opc, exp)
  472. expr_t exp;
  473. {
  474. register sm;
  475. exp.val -= (DOTVAL + 2);
  476. if ((pass == PASS_2)
  477. &&
  478. (exp.val > 0)
  479. &&
  480. ((exp.typ & S_DOT) == 0)
  481. )
  482. exp.val -= DOTGAIN;
  483. sm = fitw(exp.val);
  484. if ((exp.typ & ~S_DOT) != DOTTYP)
  485. sm = 0;
  486. if (small(sm,2)) {
  487. if (small(fitb(exp.val),2)) {
  488. if (exp.val == 0)
  489. T_EMIT2(047161, 0, 0, 0); /* NOP */
  490. else if (exp.val == -1) {
  491. T_EMIT2(047161, 0, 0, 0);
  492. serror("bad branch offset");
  493. } else
  494. T_EMIT2(opc | lowb(exp.val), 0, 0, 0);
  495. } else {
  496. T_EMIT2(opc, 0, 0, 0);
  497. T_EMIT2(loww(exp.val), 0, 0, 0);
  498. }
  499. return;
  500. }
  501. sm = small(0,2); /* dummy call; two calls to small per branch */
  502. T_EMIT2(opc | 0377, 0, 0, 0); /* 4 byte offset */
  503. T_EMIT4(exp.val, exp.typ, RELPC|RELO4, relonami);
  504. }
  505. cpbcc(opc, exp)
  506. expr_t exp;
  507. {
  508. register sm;
  509. exp.val -= (DOTVAL + 2);
  510. if ((pass == PASS_2)
  511. &&
  512. (exp.val > 0)
  513. &&
  514. ((exp.typ & S_DOT) == 0)
  515. )
  516. exp.val -= DOTGAIN;
  517. sm = fitw(exp.val);
  518. if ((exp.typ & ~S_DOT) != DOTTYP)
  519. sm = 0;
  520. if (small(sm,2)) {
  521. T_EMIT2(opc, 0, 0, 0);
  522. T_EMIT2(loww(exp.val), 0, 0, 0);
  523. return;
  524. }
  525. T_EMIT2(opc | 0100, 0, 0, 0); /* 4 byte offset */
  526. /* NB: no coprocessor defined extension words are emitted */
  527. T_EMIT4(exp.val, exp.typ, RELPC|RELO4, relonami);
  528. }
  529. ea7071(sz)
  530. {
  531. mrg_2 = 071;
  532. switch (sz) {
  533. case SIZE_B:
  534. badoperand();
  535. case SIZE_W:
  536. mrg_2 = 070;
  537. case SIZE_L:
  538. return;
  539. case SIZE_NON:
  540. break;
  541. }
  542. /* If this absolute address is in program space, and if we
  543. * can assume that the only references to program space are made
  544. * by instructins like 'jsr', 'jmp', 'lea' and 'pea', it might
  545. * be possible to use a (PC,d16) effective address mode instead
  546. * of absolute long. This is done here. If this scheme is in
  547. * some way undesirable (e.g. when references to program space
  548. * are made by instructions with more than one opcode word or by
  549. * second effective addresses in instructions), the rest
  550. * of this routine can simply be removed and replaced by the
  551. * next two lines (which of course are in comment brackets now).
  552. if (small(bd_2.typ == S_ABS && fitw(bd_2.val), 2))
  553. mrg_2 = 070;
  554. */
  555. if (pass == PASS_1) {
  556. /* Reserve a bit in the bittable; in the following
  557. * passes one call to small() will be done, but know yet
  558. * which one, because bd_2.typ cannot be trusted yet.
  559. */
  560. small(0, 2);
  561. return;
  562. }
  563. if ((bd_2.typ & ~S_DOT) == DOTTYP) {
  564. if (small(fitw(bd_2.val-(DOTVAL+2)), 2)) {
  565. bd_2.val -= (DOTVAL+2);
  566. mrg_2 = 072;
  567. }
  568. } else
  569. if (small(bd_2.typ == S_ABS && fitw(bd_2.val), 2))
  570. mrg_2 = 070;
  571. }