jit_m68k.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. #define JIT_MAX_LABELS 32
  64. static int label_idx = 0;
  65. static Label jit_labels[JIT_MAX_LABELS];
  66. static Label jit_labels_unres[JIT_MAX_LABELS]; // unresolved (forward) labels
  67. static int unres_labels = 0;
  68. void jit_init() {
  69. int i;
  70. // cleans up jit state
  71. label_idx = 0;
  72. unres_labels = 0;
  73. code_idx = 0;
  74. for (i=0; i<JIT_MAX_LABELS; i++) {
  75. if (jit_labels[i].name) free(jit_labels[i].name);
  76. jit_labels[i].name = NULL;
  77. jit_labels[i].idx = 0;
  78. if (jit_labels_unres[i].name) free(jit_labels_unres[i].name);
  79. jit_labels_unres[i].name = NULL;
  80. jit_labels_unres[i].idx = 0;
  81. }
  82. }
  83. void jit_movi(int reg, int imm) {
  84. code[code_idx++] = 0x20 | regi[reg]<<1;
  85. code[code_idx++] = 0x3c;
  86. code[code_idx+3] = imm&0xff; imm>>=8;
  87. code[code_idx+2] = imm&0xff; imm>>=8;
  88. code[code_idx+1] = imm&0xff; imm>>=8;
  89. code[code_idx] = imm&0xff;
  90. code_idx+=4;
  91. }
  92. void jit_movr(int dreg, int sreg) {
  93. code[code_idx++] = 0x20 | regi[dreg]<<1;
  94. code[code_idx++] = 0x00 | regi[sreg];
  95. }
  96. void jit_movneg(int dreg, int sreg) {
  97. code[code_idx++] = 0x6a; // bpl
  98. code[code_idx++] = 0x00;
  99. code[code_idx++] = 0x00; // skip
  100. code[code_idx++] = 0x04;
  101. jit_movr(dreg,sreg);
  102. }
  103. void jit_movne(int dreg, int sreg) {
  104. code[code_idx++] = 0x67; // beq
  105. code[code_idx++] = 0x00;
  106. code[code_idx++] = 0x00; // skip
  107. code[code_idx++] = 0x04;
  108. jit_movr(dreg,sreg);
  109. }
  110. void jit_moveq(int dreg, int sreg) {
  111. code[code_idx++] = 0x66; // bne
  112. code[code_idx++] = 0x00;
  113. code[code_idx++] = 0x00; // skip
  114. code[code_idx++] = 0x04;
  115. jit_movr(dreg,sreg);
  116. }
  117. void jit_lea(int reg, void* addr) {
  118. jit_movi(reg, (uint32_t)addr);
  119. }
  120. void jit_ldr(int reg) {
  121. code[code_idx++] = 0x20;
  122. code[code_idx++] = 0x40|regi[reg];
  123. code[code_idx++] = 0x20|(regi[reg]<<1);
  124. code[code_idx++] = 0x10;
  125. }
  126. void jit_ldr_stack(int dreg, int offset) {
  127. code[code_idx++] = 0x20|(regi[dreg]<<1); // move from sp indexed
  128. code[code_idx++] = 0x2f;
  129. code[code_idx++] = (offset&0xff00)>>8;
  130. code[code_idx++] = offset&0xff;
  131. }
  132. void jit_str_stack(int sreg, int offset) {
  133. code[code_idx++] = 0x2f; // move to sp indexed
  134. code[code_idx++] = 0x40|regi[sreg];
  135. code[code_idx++] = (offset&0xff00)>>8;
  136. code[code_idx++] = offset&0xff;
  137. }
  138. void jit_inc_stack(int imm) {
  139. code[code_idx++] = 0xdf; // adda.l
  140. code[code_idx++] = 0xfc;
  141. code[code_idx+3] = imm&0xff; imm>>=8;
  142. code[code_idx+2] = imm&0xff; imm>>=8;
  143. code[code_idx+1] = imm&0xff; imm>>=8;
  144. code[code_idx] = imm&0xff;
  145. code_idx+=4;
  146. }
  147. void jit_dec_stack(int imm) {
  148. code[code_idx++] = 0x9f; // adda.l
  149. code[code_idx++] = 0xfc;
  150. code[code_idx+3] = imm&0xff; imm>>=8;
  151. code[code_idx+2] = imm&0xff; imm>>=8;
  152. code[code_idx+1] = imm&0xff; imm>>=8;
  153. code[code_idx] = imm&0xff;
  154. code_idx+=4;
  155. }
  156. // clobbers rdx!
  157. void jit_ldrb(int reg) {
  158. code[code_idx++] = 0x16; // moveb @(00000000,%d3:l),%d0
  159. code[code_idx++] = 0x30;
  160. code[code_idx++] = 0x09 | regi[reg]<<4;
  161. code[code_idx++] = 0x90;
  162. }
  163. void jit_ldrw(int reg) {
  164. }
  165. // 8 bit only from R3! (d3)
  166. void jit_strb(int dreg) {
  167. code[code_idx++] = 0x11; //
  168. code[code_idx++] = 0x83; // the 3 is d3
  169. code[code_idx++] = 0x09 | regi[dreg]<<4;
  170. code[code_idx++] = 0x90;
  171. //fprintf(jit_out, "movb %%dl, (%s)\n", regnames[reg]);
  172. }
  173. // 32 bit only from rdx!
  174. void jit_strw(int reg) {
  175. //fprintf(jit_out, "movl %%edx, (%s)\n", regnames[reg]);
  176. }
  177. void jit_addr(int dreg, int sreg) {
  178. code[code_idx++] = 0xd0|regi[dreg]<<1;
  179. code[code_idx++] = 0x80|regi[sreg];
  180. }
  181. void jit_addi(int dreg, int imm) {
  182. code[code_idx++] = 0x06;
  183. code[code_idx++] = 0x80|regi[dreg];
  184. code[code_idx+3] = imm&0xff; imm>>=8;
  185. code[code_idx+2] = imm&0xff; imm>>=8;
  186. code[code_idx+1] = imm&0xff; imm>>=8;
  187. code[code_idx] = imm&0xff;
  188. code_idx+=4;
  189. }
  190. void jit_andr(int dreg, int sreg) {
  191. code[code_idx++] = 0xc0|regi[dreg]<<1;
  192. code[code_idx++] = 0x80|regi[sreg];
  193. }
  194. void jit_notr(int dreg) {
  195. code[code_idx++] = 0x46;
  196. code[code_idx++] = 0x80|regi[dreg];
  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_jne(char* label) {
  364. code[code_idx++] = 0x66; // bne
  365. code[code_idx++] = 0x00;
  366. jit_emit_branch(label);
  367. }
  368. void jit_jneg(char* label) {
  369. code[code_idx++] = 0x6b; // bmi
  370. code[code_idx++] = 0x00;
  371. jit_emit_branch(label);
  372. }
  373. void jit_jmp(char* label) {
  374. code[code_idx++] = 0x60; // bra
  375. code[code_idx++] = 0x00;
  376. jit_emit_branch(label);
  377. }
  378. void jit_label(char* label) {
  379. Label* unres_lbl = NULL;
  380. jit_labels[label_idx].name = strdup(label);
  381. jit_labels[label_idx].idx = code_idx;
  382. while ((unres_lbl = find_unresolved_label(label))) {
  383. //printf("! forward label to %s at idx %d resolved.\r\n",label,unres_lbl->idx);
  384. int offset = (code_idx - unres_lbl->idx);
  385. code[unres_lbl->idx] = (offset&0xff00)>>8;
  386. code[unres_lbl->idx+1] = (offset&0xff);
  387. free(unres_lbl->name);
  388. unres_lbl->name = NULL;
  389. unres_lbl->idx = 0;
  390. }
  391. label_idx++;
  392. }
  393. void jit_ret() {
  394. code[code_idx++] = 0x4e;
  395. code[code_idx++] = 0x75;
  396. }
  397. void jit_push(int r1, int r2) {
  398. int i;
  399. for (i=r1; i<=r2; i++) {
  400. //fprintf(jit_out, "push %s\n",regnames[i]);
  401. code[code_idx++] = 0x2f; // move dx, -(sp)
  402. code[code_idx++] = regi[i];
  403. }
  404. }
  405. void jit_pop(int r1, int r2) {
  406. int i;
  407. for (i=r2; i>=r1; i--) {
  408. //fprintf(jit_out, "pop %s\n",regnames[i]);
  409. code[code_idx++] = 0x20|(regi[i]<<1); // move (sp)+, dx
  410. code[code_idx++] = 0x1f;
  411. }
  412. }
  413. // do any needed stack alignment etc. here for host ABI
  414. void jit_host_call_enter() {
  415. }
  416. void jit_host_call_exit() {
  417. }
  418. void debug_handler() {
  419. }