jit_m68k.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. char* regnames[] = {
  2. "d0",
  3. "d1",
  4. "d2",
  5. "d3",
  6. "d4",
  7. "d5",
  8. "d6",
  9. "d7",
  10. "a0",
  11. "a1",
  12. "a2",
  13. "a3",
  14. "a4",
  15. "a5",
  16. "a6",
  17. "a7" // stack pointer
  18. };
  19. enum jit_reg {
  20. R0 = 0,
  21. R1,
  22. R2,
  23. R3,
  24. R4,
  25. R5,
  26. R6,
  27. R7,
  28. R8,
  29. R9,
  30. R10,
  31. R11,
  32. R12,
  33. R13,
  34. R14,
  35. R15
  36. };
  37. enum arg_reg {
  38. ARGR0 = 0,
  39. ARGR1 = 1,
  40. ARGR2 = 2
  41. };
  42. uint8_t regi[] = {
  43. 0,
  44. 1,
  45. 2,
  46. 3,
  47. 4,
  48. 5,
  49. 6,
  50. 7,
  51. 8,
  52. 9,
  53. 10,
  54. 11,
  55. 12,
  56. 13,
  57. 14,
  58. 15
  59. };
  60. #define RSP R15
  61. static uint8_t* code;
  62. static uint32_t code_idx;
  63. typedef struct Label {
  64. char* name;
  65. uint32_t idx;
  66. } Label;
  67. #define JIT_MAX_LABELS 32
  68. static int label_idx = 0;
  69. static Label jit_labels[JIT_MAX_LABELS];
  70. static Label jit_labels_unres[JIT_MAX_LABELS]; // unresolved (forward) labels
  71. static int unres_labels = 0;
  72. void jit_init() {
  73. int i;
  74. // cleans up jit state
  75. label_idx = 0;
  76. unres_labels = 0;
  77. code_idx = 0;
  78. for (i=0; i<JIT_MAX_LABELS; i++) {
  79. if (jit_labels[i].name) free(jit_labels[i].name);
  80. jit_labels[i].name = NULL;
  81. jit_labels[i].idx = 0;
  82. if (jit_labels_unres[i].name) free(jit_labels_unres[i].name);
  83. jit_labels_unres[i].name = NULL;
  84. jit_labels_unres[i].idx = 0;
  85. }
  86. }
  87. void jit_movi(int reg, int imm) {
  88. code[code_idx++] = 0x20 | regi[reg]<<1;
  89. code[code_idx++] = 0x3c;
  90. code[code_idx+3] = imm&0xff; imm>>=8;
  91. code[code_idx+2] = imm&0xff; imm>>=8;
  92. code[code_idx+1] = imm&0xff; imm>>=8;
  93. code[code_idx] = imm&0xff;
  94. code_idx+=4;
  95. }
  96. void jit_movr(int dreg, int sreg) {
  97. code[code_idx++] = 0x20 | regi[dreg]<<1;
  98. code[code_idx++] = 0x00 | regi[sreg];
  99. }
  100. void jit_movneg(int dreg, int sreg) {
  101. code[code_idx++] = 0x6a; // bpl
  102. code[code_idx++] = 0x00;
  103. code[code_idx++] = 0x00; // skip
  104. code[code_idx++] = 0x04;
  105. jit_movr(dreg,sreg);
  106. }
  107. void jit_movne(int dreg, int sreg) {
  108. code[code_idx++] = 0x67; // beq
  109. code[code_idx++] = 0x00;
  110. code[code_idx++] = 0x00; // skip
  111. code[code_idx++] = 0x04;
  112. jit_movr(dreg,sreg);
  113. }
  114. void jit_moveq(int dreg, int sreg) {
  115. code[code_idx++] = 0x66; // bne
  116. code[code_idx++] = 0x00;
  117. code[code_idx++] = 0x00; // skip
  118. code[code_idx++] = 0x04;
  119. jit_movr(dreg,sreg);
  120. }
  121. void jit_lea(int reg, void* addr) {
  122. jit_movi(reg, (uint32_t)addr);
  123. }
  124. void jit_ldr(int reg) {
  125. code[code_idx++] = 0x20;
  126. code[code_idx++] = 0x40|regi[reg];
  127. code[code_idx++] = 0x20|(regi[reg]<<1);
  128. code[code_idx++] = 0x10;
  129. }
  130. void jit_ldr_stack(int dreg, int offset) {
  131. code[code_idx++] = 0x20|(regi[dreg]<<1); // move from sp indexed
  132. code[code_idx++] = 0x2f;
  133. code[code_idx++] = (offset&0xff00)>>8;
  134. code[code_idx++] = offset&0xff;
  135. }
  136. void jit_str_stack(int sreg, int offset) {
  137. code[code_idx++] = 0x2f; // move to sp indexed
  138. code[code_idx++] = 0x40|regi[sreg];
  139. code[code_idx++] = (offset&0xff00)>>8;
  140. code[code_idx++] = offset&0xff;
  141. }
  142. void jit_inc_stack(int imm) {
  143. code[code_idx++] = 0xdf; // adda.l
  144. code[code_idx++] = 0xfc;
  145. code[code_idx+3] = imm&0xff; imm>>=8;
  146. code[code_idx+2] = imm&0xff; imm>>=8;
  147. code[code_idx+1] = imm&0xff; imm>>=8;
  148. code[code_idx] = imm&0xff;
  149. code_idx+=4;
  150. }
  151. void jit_dec_stack(int imm) {
  152. code[code_idx++] = 0x9f; // adda.l
  153. code[code_idx++] = 0xfc;
  154. code[code_idx+3] = imm&0xff; imm>>=8;
  155. code[code_idx+2] = imm&0xff; imm>>=8;
  156. code[code_idx+1] = imm&0xff; imm>>=8;
  157. code[code_idx] = imm&0xff;
  158. code_idx+=4;
  159. }
  160. // clobbers rdx!
  161. void jit_ldrb(int reg) {
  162. code[code_idx++] = 0x16; // moveb @(00000000,%d3:l),%d0
  163. code[code_idx++] = 0x30;
  164. code[code_idx++] = 0x09 | regi[reg]<<4;
  165. code[code_idx++] = 0x90;
  166. }
  167. void jit_ldrw(int reg) {
  168. }
  169. // 8 bit only from R3! (d3)
  170. void jit_strb(int dreg) {
  171. code[code_idx++] = 0x11; //
  172. code[code_idx++] = 0x83; // the 3 is d3
  173. code[code_idx++] = 0x09 | regi[dreg]<<4;
  174. code[code_idx++] = 0x90;
  175. //fprintf(jit_out, "movb %%dl, (%s)\n", regnames[reg]);
  176. }
  177. // 32 bit only from rdx!
  178. void jit_strw(int reg) {
  179. //fprintf(jit_out, "movl %%edx, (%s)\n", regnames[reg]);
  180. }
  181. void jit_addr(int dreg, int sreg) {
  182. code[code_idx++] = 0xd0|regi[dreg]<<1;
  183. code[code_idx++] = 0x80|regi[sreg];
  184. }
  185. void jit_addi(int dreg, int imm) {
  186. code[code_idx++] = 0x06;
  187. code[code_idx++] = 0x80|regi[dreg];
  188. code[code_idx+3] = imm&0xff; imm>>=8;
  189. code[code_idx+2] = imm&0xff; imm>>=8;
  190. code[code_idx+1] = imm&0xff; imm>>=8;
  191. code[code_idx] = imm&0xff;
  192. code_idx+=4;
  193. }
  194. void jit_andr(int dreg, int sreg) {
  195. code[code_idx++] = 0xc0|regi[dreg]<<1;
  196. code[code_idx++] = 0x80|regi[sreg];
  197. }
  198. void jit_orr(int dreg, int sreg) {
  199. code[code_idx++] = 0x80|regi[dreg]<<1;
  200. code[code_idx++] = 0x80|regi[sreg];
  201. }
  202. void jit_xorr(int dreg, int sreg) {
  203. code[code_idx++] = 0xb1|regi[sreg]<<1;
  204. code[code_idx++] = 0x80|regi[dreg];
  205. }
  206. void jit_shrr(int dreg, int sreg) {
  207. code[code_idx++] = 0xe0|regi[sreg]<<1;
  208. code[code_idx++] = 0xa8|regi[dreg];
  209. }
  210. void jit_shlr(int dreg, int sreg) {
  211. code[code_idx++] = 0xe1|regi[sreg]<<1;
  212. code[code_idx++] = 0xa8|regi[dreg];
  213. }
  214. void jit_subr(int dreg, int sreg) {
  215. code[code_idx++] = 0x90|regi[dreg]<<1;
  216. code[code_idx++] = 0x80|regi[sreg];
  217. }
  218. void jit_mulr(int dreg, int sreg) {
  219. code[code_idx++] = 0x4c;
  220. code[code_idx++] = 0x00|regi[sreg];
  221. code[code_idx++] = 0x08|regi[dreg]<<4;
  222. code[code_idx++] = 0x00;
  223. }
  224. void jit_divr(int dreg, int sreg) {
  225. code[code_idx++] = 0x4c;
  226. code[code_idx++] = 0x40|regi[sreg];
  227. code[code_idx++] = 0x08|regi[dreg]<<4;
  228. code[code_idx++] = 0x00|regi[dreg];
  229. }
  230. void jit_call(void* func, char* note) {
  231. uint32_t imm = (uint32_t)func;
  232. code[code_idx++] = 0x2f; // move d0, -(sp)
  233. code[code_idx++] = 0x00; // d0
  234. code[code_idx++] = 0x20; // move function pointer to a0
  235. code[code_idx++] = 0x7c;
  236. code[code_idx+3] = imm&0xff; imm>>=8;
  237. code[code_idx+2] = imm&0xff; imm>>=8;
  238. code[code_idx+1] = imm&0xff; imm>>=8;
  239. code[code_idx] = imm&0xff;
  240. code_idx+=4;
  241. code[code_idx++] = 0x4e; // jsr (a0)
  242. code[code_idx++] = 0x90;
  243. code[code_idx++] = 0x58; // addq.l #4, sp
  244. code[code_idx++] = 0x8f;
  245. }
  246. void jit_call2(void* func, char* note) {
  247. uint32_t imm = (uint32_t)func;
  248. code[code_idx++] = 0x2f; // move d1, -(sp)
  249. code[code_idx++] = 0x01; // d1
  250. code[code_idx++] = 0x2f; // move d0, -(sp)
  251. code[code_idx++] = 0x00; // d0
  252. code[code_idx++] = 0x20; // move function pointer to a0
  253. code[code_idx++] = 0x7c;
  254. code[code_idx+3] = imm&0xff; imm>>=8;
  255. code[code_idx+2] = imm&0xff; imm>>=8;
  256. code[code_idx+1] = imm&0xff; imm>>=8;
  257. code[code_idx] = imm&0xff;
  258. code_idx+=4;
  259. code[code_idx++] = 0x4e; // jsr (a0)
  260. code[code_idx++] = 0x90;
  261. code[code_idx++] = 0x50; // addq.l #8, sp
  262. code[code_idx++] = 0x8f;
  263. }
  264. void jit_call3(void* func, char* note) {
  265. uint32_t imm = (uint32_t)func;
  266. code[code_idx++] = 0x2f; // move d2, -(sp)
  267. code[code_idx++] = 0x02; // d2
  268. code[code_idx++] = 0x2f; // move d1, -(sp)
  269. code[code_idx++] = 0x01; // d1
  270. code[code_idx++] = 0x2f; // move d0, -(sp)
  271. code[code_idx++] = 0x00; // d0
  272. code[code_idx++] = 0x20; // move function pointer to a0
  273. code[code_idx++] = 0x7c;
  274. code[code_idx+3] = imm&0xff; imm>>=8;
  275. code[code_idx+2] = imm&0xff; imm>>=8;
  276. code[code_idx+1] = imm&0xff; imm>>=8;
  277. code[code_idx] = imm&0xff;
  278. code_idx+=4;
  279. code[code_idx++] = 0x4e; // jsr (a0)
  280. code[code_idx++] = 0x90;
  281. code[code_idx++] = 0x50; // addq.l #8, sp
  282. code[code_idx++] = 0x8f;
  283. code[code_idx++] = 0x58; // addq.l #4, sp
  284. code[code_idx++] = 0x8f;
  285. }
  286. void jit_callr(int dreg) {
  287. code[code_idx++] = 0x20 | regi[dreg]; // move dx, a0
  288. code[code_idx++] = 0x40;
  289. code[code_idx++] = 0x4e; // jsr (a0)
  290. code[code_idx++] = 0x90;
  291. }
  292. int32_t inline_mod(int a, int b) {
  293. return a%b;
  294. }
  295. void jit_modr(int dreg, int sreg) {
  296. jit_movr(ARGR0,dreg);
  297. jit_movr(ARGR1,sreg);
  298. jit_call2(inline_mod,"mod");
  299. if (dreg!=0) jit_movr(dreg,0);
  300. }
  301. void jit_cmpi(int sreg, int imm) {
  302. code[code_idx++] = 0x0c;
  303. code[code_idx++] = 0x80|regi[sreg];
  304. code[code_idx+3] = imm&0xff; imm>>=8;
  305. code[code_idx+2] = imm&0xff; imm>>=8;
  306. code[code_idx+1] = imm&0xff; imm>>=8;
  307. code[code_idx] = imm&0xff;
  308. code_idx+=4;
  309. }
  310. void jit_cmpr(int sreg, int dreg) {
  311. code[code_idx++] = 0xb0|regi[dreg]<<1;
  312. code[code_idx++] = 0x80|regi[sreg];
  313. }
  314. Label* find_label(char* label) {
  315. int i;
  316. for (i=0; i<label_idx; i++) {
  317. if (jit_labels[i].name) {
  318. //printf("find_label %s label vs %s\r\n",label,jit_labels[i].name);
  319. }
  320. if (jit_labels[i].name && (strcmp(jit_labels[i].name,label)==0)) {
  321. return &jit_labels[i];
  322. }
  323. }
  324. return NULL;
  325. }
  326. Label* find_unresolved_label(char* label) {
  327. int i;
  328. for (i=0; i<unres_labels; i++) {
  329. if (jit_labels_unres[i].name) {
  330. //printf("find_unres_label %s label vs %s\r\n",label,jit_labels_unres[i].name);
  331. }
  332. if (jit_labels_unres[i].name && (strcmp(jit_labels_unres[i].name,label)==0)) {
  333. return &jit_labels_unres[i];
  334. }
  335. }
  336. return NULL;
  337. }
  338. // m68k offsets are 16 bit
  339. void jit_emit_branch(char* label) {
  340. Label* lbl = find_label(label);
  341. if (lbl) {
  342. int offset = (lbl->idx - code_idx);
  343. //printf("offset to %s: %d (*4)\r\n",label,offset);
  344. if (offset<0) {
  345. offset = 0x10000-(-offset);
  346. code[code_idx++] = (offset&0xff00)>>8;
  347. code[code_idx++] = offset&0xff;
  348. }
  349. } else {
  350. //printf("! label not found %s, adding unresolved.\r\n",label);
  351. jit_labels_unres[unres_labels].name = strdup(label);
  352. jit_labels_unres[unres_labels].idx = code_idx;
  353. code[code_idx++] = 0;
  354. code[code_idx++] = 0;
  355. unres_labels++;
  356. }
  357. }
  358. void jit_je(char* label) {
  359. code[code_idx++] = 0x67; // beq
  360. code[code_idx++] = 0x00;
  361. jit_emit_branch(label);
  362. }
  363. void jit_jneg(char* label) {
  364. code[code_idx++] = 0x6b; // bmi
  365. code[code_idx++] = 0x00;
  366. jit_emit_branch(label);
  367. }
  368. void jit_jmp(char* label) {
  369. code[code_idx++] = 0x60; // bra
  370. code[code_idx++] = 0x00;
  371. jit_emit_branch(label);
  372. }
  373. void jit_label(char* label) {
  374. Label* unres_lbl = NULL;
  375. jit_labels[label_idx].name = strdup(label);
  376. jit_labels[label_idx].idx = code_idx;
  377. while ((unres_lbl = find_unresolved_label(label))) {
  378. //printf("! forward label to %s at idx %d resolved.\r\n",label,unres_lbl->idx);
  379. int offset = (code_idx - unres_lbl->idx);
  380. code[unres_lbl->idx] = (offset&0xff00)>>8;
  381. code[unres_lbl->idx+1] = (offset&0xff);
  382. free(unres_lbl->name);
  383. unres_lbl->name = NULL;
  384. unres_lbl->idx = 0;
  385. }
  386. label_idx++;
  387. }
  388. void jit_ret() {
  389. code[code_idx++] = 0x4e;
  390. code[code_idx++] = 0x75;
  391. }
  392. void jit_push(int r1, int r2) {
  393. int i;
  394. for (i=r1; i<=r2; i++) {
  395. //fprintf(jit_out, "push %s\n",regnames[i]);
  396. code[code_idx++] = 0x2f; // move dx, -(sp)
  397. code[code_idx++] = regi[i];
  398. }
  399. }
  400. void jit_pop(int r1, int r2) {
  401. int i;
  402. for (i=r2; i>=r1; i--) {
  403. //fprintf(jit_out, "pop %s\n",regnames[i]);
  404. code[code_idx++] = 0x20|(regi[i]<<1); // move (sp)+, dx
  405. code[code_idx++] = 0x1f;
  406. }
  407. }
  408. void debug_handler() {
  409. }