mach5.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. #define RCSID5 "$Id$"
  6. /*
  7. * INTEL 8086 special routines
  8. */
  9. void ea_1(int param)
  10. {
  11. if ((mrg_1 & 070) || (param & ~070)) {
  12. serror("bad operand");
  13. }
  14. emit1(mrg_1 | param);
  15. switch(mrg_1 >> 6) {
  16. case 0:
  17. if (mrg_1 == 6 || (mrg_1 & 040)) {
  18. #ifdef RELOCATION
  19. RELOMOVE(relonami, rel_1);
  20. newrelo(exp_1.typ, RELO2);
  21. #endif
  22. emit2(exp_1.val);
  23. }
  24. break;
  25. case 1:
  26. #ifdef RELOCATION
  27. RELOMOVE(relonami, rel_1);
  28. newrelo(exp_1.typ, RELO1);
  29. #endif
  30. emit1(exp_1.val);
  31. break;
  32. case 2:
  33. #ifdef RELOCATION
  34. RELOMOVE(relonami, rel_1);
  35. newrelo(exp_1.typ, RELO2);
  36. #endif
  37. emit2(exp_1.val);
  38. break;
  39. }
  40. }
  41. void ea_2(int param)
  42. {
  43. mrg_1 = mrg_2;
  44. exp_1 = exp_2;
  45. RELOMOVE(rel_1, rel_2);
  46. ea_1(param);
  47. }
  48. void reverse()
  49. {
  50. int m, r; expr_t e;
  51. m = mrg_1; mrg_1 = mrg_2; mrg_2 = m;
  52. e = exp_1; exp_1 = exp_2; exp_2 = e;
  53. #ifndef ASLD
  54. r = rel_1; rel_1 = rel_2; rel_2 = r;
  55. #endif
  56. }
  57. void badsyntax()
  58. {
  59. serror("bad operands");
  60. }
  61. void regsize(int sz)
  62. {
  63. int bit;
  64. sz <<= 3;
  65. bit = 010;
  66. sz &= bit;
  67. if ((mrg_1 >= 0300 && (mrg_1 & bit) != sz) ||
  68. (mrg_2 >= 0300 && (mrg_2 & bit) != sz))
  69. serror("register error");
  70. mrg_1 &= ~bit;
  71. mrg_2 &= ~bit;
  72. }
  73. void indexed()
  74. {
  75. int sm1, sm2;
  76. if (mrg_2 & ~7)
  77. serror("register error");
  78. sm1 = exp_2.typ == S_ABS && fitb((short)(exp_2.val));
  79. if (sm1) {
  80. sm2 = exp_2.val == 0 && mrg_2 != 6;
  81. }
  82. else sm2 = 0;
  83. if (small(sm1, 1)) {
  84. if (small(sm2, 1)) {
  85. }
  86. else mrg_2 |= 0100;
  87. }
  88. else {
  89. if (small(0, 1)) {}
  90. mrg_2 |= 0200;
  91. }
  92. }
  93. void branch(int opc, expr_t exp)
  94. {
  95. int sm,dist;
  96. int saving = opc == 0353 ? 1 : 3;
  97. dist = exp.val - (DOTVAL + 2);
  98. if (pass == PASS_2 && dist > 0 && !(exp.typ & S_DOT))
  99. dist -= DOTGAIN;
  100. sm = dist > 0 ? fitb(dist-saving) : fitb(dist);
  101. if ((exp.typ & ~S_DOT) != DOTTYP)
  102. sm = 0;
  103. if ((opc & 0370) == 0340) {
  104. fit(sm);
  105. sm = 1;
  106. } else {
  107. if ((sm = small(sm,saving)) == 0) {
  108. if (opc != 0353) {
  109. emit1(opc^1);
  110. emit1(3);
  111. dist -= 2;
  112. }
  113. opc = 0351;
  114. dist--;
  115. }
  116. }
  117. emit1(opc);
  118. if (sm == 0) {
  119. #ifdef RELOCATION
  120. newrelo(exp.typ, RELPC | RELO2);
  121. #endif
  122. emit2(dist);
  123. } else
  124. emit1(dist);
  125. }
  126. void pushop(int opc)
  127. {
  128. regsize(1);
  129. if (mrg_1 & 020) {
  130. if ( (mrg_1&3) == 1 && opc==1 ) badsyntax() ;
  131. emit1(6 | opc | (mrg_1&3)<<3);
  132. } else if (mrg_1 >= 0300) {
  133. emit1(0120 | opc<<3 | (mrg_1&7));
  134. } else if (opc == 0) {
  135. if (mrg_1 & 040) { /* 070 ??? */
  136. if (small(exp_1.typ == S_ABS && fitb((short)exp_1.val),1)) {
  137. emit1(0152);
  138. emit1((int) exp_1.val);
  139. } else {
  140. emit1(0150);
  141. RELOMOVE(relonami, rel_1);
  142. #ifdef RELOCATION
  143. newrelo(exp_1.typ, RELO2);
  144. emit2((int) exp_1.val);
  145. #endif
  146. }
  147. } else {
  148. emit1(0377); ea_1(6<<3);
  149. }
  150. } else {
  151. emit1(0217); ea_1(0<<3);
  152. }
  153. }
  154. void addop(int opc)
  155. {
  156. regsize(opc);
  157. if (mrg_2 >= 0300) {
  158. emit1(opc); ea_1((mrg_2&7)<<3);
  159. } else if ((mrg_2 & 040) && mrg_1 == 0300) {
  160. emit1(opc | 4);
  161. #ifdef RELOCATION
  162. RELOMOVE(relonami, rel_2);
  163. newrelo(exp_2.typ, (opc&1)? RELO2 : RELO1);
  164. #endif
  165. emitx(exp_2.val, (opc&1)+1);
  166. } else if (mrg_2 & 040) {
  167. if ((opc&1) == 0) {
  168. emit1(0200);
  169. } else {
  170. int sm = exp_2.typ == S_ABS && fitb((short)exp_2.val) &&
  171. opc != 011 && opc != 041 && opc != 061;
  172. if (small(sm, 1)) {
  173. emit1(0203); opc &= ~1;
  174. }
  175. else emit1(0201);
  176. }
  177. ea_1(opc & 070);
  178. #ifdef RELOCATION
  179. RELOMOVE(relonami, rel_2);
  180. newrelo(exp_2.typ, (opc&1)? RELO2 : RELO1);
  181. #endif
  182. emitx(exp_2.val, (opc&1)+1);
  183. } else if (mrg_1 >= 0300) {
  184. emit1(opc | 2);
  185. ea_2((mrg_1&7)<<3);
  186. } else
  187. badsyntax();
  188. }
  189. void rolop(int opc)
  190. {
  191. int cmrg;
  192. cmrg = mrg_2;
  193. mrg_2 = mrg_1;
  194. regsize(opc);
  195. if (cmrg == 0301) {
  196. emit1(0322 | (opc&1)); ea_1(opc&070);
  197. } else if (cmrg & 040) {
  198. if (small(exp_2.val == 1, 1)) {
  199. emit1(0320 | (opc&1)); ea_1(opc&070);
  200. } else {
  201. fit(fitb(exp_2.val));
  202. emit1(0300|(opc&1)); ea_1(opc&070);
  203. emit1((int)exp_2.val);
  204. }
  205. } else
  206. badsyntax();
  207. }
  208. void incop(int opc)
  209. {
  210. regsize(opc);
  211. if ((opc&1) && mrg_1>=0300) {
  212. emit1(0100 | (opc&010) | (mrg_1&7));
  213. } else {
  214. emit1(0376 | (opc&1));
  215. ea_1(opc & 010);
  216. }
  217. }
  218. void callop(int opc)
  219. {
  220. regsize(1);
  221. if (mrg_1 & 040) {
  222. if (opc == (040+(0351<<8))) {
  223. RELOMOVE(relonami, rel_1);
  224. branch(0353,exp_1);
  225. } else {
  226. exp_1.val -= (DOTVAL+3);
  227. emit1(opc>>8);
  228. #ifdef RELOCATION
  229. RELOMOVE(relonami, rel_1);
  230. newrelo(exp_1.typ, RELPC | RELO2);
  231. #endif
  232. emit2(exp_1.val);
  233. }
  234. } else {
  235. emit1(0377); ea_1(opc&070);
  236. }
  237. }
  238. void xchg(int opc)
  239. {
  240. regsize(opc);
  241. if (mrg_2 == 0300 || mrg_1 < 0300)
  242. reverse();
  243. if (opc == 1 && mrg_1 == 0300 && mrg_2 >= 0300) {
  244. emit1(0220 | (mrg_2&7));
  245. } else if (mrg_1 >= 0300) {
  246. emit1(0206 | opc); ea_2((mrg_1&7)<<3);
  247. } else
  248. badsyntax();
  249. }
  250. void test(int opc)
  251. {
  252. regsize(opc);
  253. if ((mrg_1 & 040) || mrg_2 >= 0300)
  254. reverse();
  255. if ((mrg_2 & 040) && mrg_1 == 0300) {
  256. emit1(0250 | opc);
  257. #ifdef RELOCATION
  258. RELOMOVE(relonami, rel_2);
  259. newrelo(exp_2.typ, (opc&1)? RELO2 : RELO1);
  260. #endif
  261. emitx(exp_2.val, (opc&1)+1);
  262. } else if (mrg_2 & 040) {
  263. emit1(0366 | opc);
  264. ea_1(0<<3);
  265. #ifdef RELOCATION
  266. RELOMOVE(relonami, rel_2);
  267. newrelo(exp_2.typ, (opc&1)? RELO2 : RELO1);
  268. #endif
  269. emitx(exp_2.val, (opc&1)+1);
  270. } else if (mrg_1 >= 0300) {
  271. emit1(0204 | opc); ea_2((mrg_1&7)<<3);
  272. } else
  273. badsyntax();
  274. }
  275. void mov(int opc)
  276. {
  277. regsize(opc);
  278. if (mrg_1 & 020) {
  279. emit1(0216); ea_2((mrg_1&3)<<3);
  280. } else if (mrg_2 & 020) {
  281. emit1(0214); ea_1((mrg_2&3)<<3);
  282. } else if (mrg_2 & 040) {
  283. if (mrg_1 >= 0300) {
  284. emit1(0260 | opc<<3 | (mrg_1&7));
  285. #ifdef RELOCATION
  286. RELOMOVE(relonami, rel_2);
  287. newrelo(exp_2.typ, (opc&1)? RELO2 : RELO1);
  288. #endif
  289. emitx(exp_2.val, (opc&1)+1);
  290. } else {
  291. emit1(0306 | opc); ea_1(0<<3);
  292. #ifdef RELOCATION
  293. RELOMOVE(relonami, rel_2);
  294. newrelo(exp_2.typ, (opc&1)? RELO2 : RELO1);
  295. #endif
  296. emitx(exp_2.val, (opc&1)+1);
  297. }
  298. } else if (mrg_2 == 0300 && mrg_1 == 6) {
  299. emit1(0242 | opc);
  300. #ifdef RELOCATION
  301. RELOMOVE(relonami, rel_1);
  302. newrelo(exp_1.typ, RELO2);
  303. #endif
  304. emit2(exp_1.val);
  305. } else if (mrg_1 == 0300 && mrg_2 == 6) {
  306. emit1(0240 | opc);
  307. #ifdef RELOCATION
  308. RELOMOVE(relonami, rel_2);
  309. newrelo(exp_2.typ, RELO2);
  310. #endif
  311. emit2(exp_2.val);
  312. } else if (mrg_2 >= 0300) {
  313. emit1(0210 | opc); ea_1((mrg_2&7)<<3);
  314. } else if (mrg_1 >= 0300) {
  315. emit1(0212 | opc); ea_2((mrg_1&7)<<3);
  316. } else {
  317. badsyntax();
  318. }
  319. }
  320. void imul(int opc)
  321. {
  322. regsize(opc);
  323. if (exp_2.typ != S_ABS || ((mrg_2 & 040) == 0)) {
  324. serror("bad operand");
  325. } else {
  326. if (small(exp_2.typ == S_ABS && fitb((short)exp_2.val),1)) {
  327. emit1(0153);
  328. ea_1((mrg_2&7)<<3);
  329. emit1((int)exp_2.val);
  330. } else {
  331. emit1(0151);
  332. ea_1((mrg_2&7)<<3);
  333. RELOMOVE(relonami, rel_2);
  334. #ifdef RELOCATION
  335. newrelo(exp_2.typ, RELO2);
  336. emit2((int) exp_2.val);
  337. #endif
  338. }
  339. }
  340. }