jit_arm_raw.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. #include <stdint.h>
  2. enum jit_reg {
  3. R0 = 0,
  4. R1,
  5. R2,
  6. R3,
  7. R4,
  8. R5,
  9. R6,
  10. R7,
  11. R8,
  12. R9,
  13. R10,
  14. R11,
  15. R12,
  16. R13, // SP Stack Pointer
  17. R14, // LR Link Register
  18. R15 // PC Program Counter
  19. };
  20. enum arg_reg {
  21. ARGR0 = 0,
  22. ARGR1,
  23. ARGR2
  24. };
  25. #define RSP R13
  26. static uint32_t* code;
  27. static uint32_t code_idx;
  28. static uint32_t cpool_idx; // constant pool
  29. #define JIT_MAX_LABELS 32
  30. static int label_idx = 0;
  31. static Label jit_labels[JIT_MAX_LABELS];
  32. static Label jit_labels_unres[JIT_MAX_LABELS]; // unresolved (forward) labels
  33. static int unres_labels = 0;
  34. /*
  35. 31-28 cond
  36. 27: 0
  37. 26: 1
  38. 25: 0
  39. 24: P (1 = offset addressing)
  40. 23: U (1 = +, 0 = -)
  41. 22: 0 (1 = byte access)
  42. 21: W (0 = offset addressing)
  43. 20: L (0 = store 1 = load)
  44. */
  45. void jit_init(int gap) {
  46. // cleans up jit state
  47. label_idx = 0;
  48. unres_labels = 0;
  49. code_idx = 0;
  50. cpool_idx = gap;
  51. for (int i=0; i<JIT_MAX_LABELS; i++) {
  52. if (jit_labels[i].name) free(jit_labels[i].name);
  53. jit_labels[i].name = NULL;
  54. jit_labels[i].idx = 0;
  55. if (jit_labels_unres[i].name) free(jit_labels_unres[i].name);
  56. jit_labels_unres[i].name = NULL;
  57. jit_labels_unres[i].idx = 0;
  58. }
  59. }
  60. void jit_movi(int reg, uint32_t imm) {
  61. // load from constant pool
  62. // template: 0b11000101100100000000000000000000
  63. // 0x5900000
  64. uint32_t op = 0xe5900000;
  65. op |= (15<<16); // base reg = pc
  66. op |= (reg<<12); // dreg
  67. op |= ((cpool_idx-code_idx-2)*4);
  68. *(code+cpool_idx) = imm; // store in constant pool
  69. cpool_idx++;
  70. code[code_idx] = op;
  71. code_idx++;
  72. }
  73. void jit_movr(int dreg, int sreg) {
  74. uint32_t op = 0xe1a00000;
  75. op |= (sreg<<0);
  76. op |= (dreg<<12);
  77. code[code_idx++] = op;
  78. }
  79. void jit_movneg(int dreg, int sreg) {
  80. uint32_t op = 0x41a00000;
  81. op |= (sreg<<0); // base reg = pc
  82. op |= (dreg<<12); // dreg
  83. code[code_idx++] = op;
  84. }
  85. void jit_movne(int dreg, int sreg) {
  86. uint32_t op = 0x11a00000;
  87. op |= (sreg<<0); // base reg = pc
  88. op |= (dreg<<12); // dreg
  89. code[code_idx++] = op;
  90. }
  91. void jit_moveq(int dreg, int sreg) {
  92. uint32_t op = 0x01a00000;
  93. op |= (sreg<<0); // base reg = pc
  94. op |= (dreg<<12); // dreg
  95. code[code_idx++] = op;
  96. }
  97. void jit_lea(int reg, void* addr) {
  98. jit_movi(reg, (uint32_t)addr);
  99. }
  100. void jit_ldr(int reg) {
  101. uint32_t op = 0xe5900000;
  102. op |= (reg<<16); // base reg = pc
  103. op |= (reg<<12); // dreg
  104. code[code_idx++] = op;
  105. }
  106. void jit_ldr_stack(int dreg, int offset) {
  107. uint32_t op = 0xe59d0000;
  108. if (offset<0) {
  109. op = 0xe51d0000;
  110. offset = -1*offset;
  111. }
  112. op |= (offset)&0xfff;
  113. op |= (dreg<<12); // dreg
  114. code[code_idx++] = op;
  115. }
  116. void jit_str_stack(int sreg, int offset) {
  117. uint32_t op = 0xe58d0000;
  118. if (offset<0) {
  119. op = 0xe51d0000;
  120. offset = -1*offset;
  121. }
  122. op |= (offset)&0xfff;
  123. op |= (sreg<<12); // dreg
  124. code[code_idx++] = op;
  125. }
  126. void jit_inc_stack(int offset) {
  127. uint32_t op = 0xe28dd000;
  128. op |= (offset)&0xfff;
  129. code[code_idx++] = op;
  130. }
  131. void jit_dec_stack(int offset) {
  132. uint32_t op = 0xe24dd000;
  133. op |= (offset)&0xfff;
  134. code[code_idx++] = op;
  135. }
  136. void jit_ldrw(int reg) {
  137. jit_ldr(reg);
  138. }
  139. // 8 bit only from rdx! (R3)
  140. void jit_ldrb(int reg) {
  141. uint32_t op = 0xe5900000;
  142. op |= 1<<22; // byte access
  143. op |= (reg<<16); // r3
  144. op |= (3<<12); // dreg
  145. code[code_idx++] = op;
  146. }
  147. // 8 bit only from rdx! (R3)
  148. void jit_ldrs(int reg) {
  149. uint32_t op = 0xe1d000b0; // ldrh
  150. op |= 1<<22; // byte access
  151. op |= (reg<<16); // r3
  152. op |= (3<<12); // dreg
  153. code[code_idx++] = op;
  154. }
  155. // 8 bit only from rdx! (R3)
  156. void jit_strb(int reg) {
  157. uint32_t op = 0xe5800000;
  158. op |= 1<<22; // byte access
  159. op |= (reg<<16); // r3
  160. op |= (3<<12); // dreg
  161. code[code_idx++] = op;
  162. }
  163. // 16 bit only from rdx! (R3)
  164. void jit_strs(int reg) {
  165. uint32_t op = 0xe1c000b0; // strh
  166. op |= 1<<22; // byte access
  167. op |= (reg<<16); // r3
  168. op |= (3<<12); // dreg
  169. code[code_idx++] = op;
  170. }
  171. // 32 bit only from rdx!
  172. void jit_strw(int reg) {
  173. uint32_t op = 0xe5800000;
  174. op |= (reg<<16); // r3
  175. op |= (3<<12); // dreg
  176. code[code_idx++] = op;
  177. }
  178. #define jit_stra jit_strw
  179. void jit_addr(int dreg, int sreg) {
  180. uint32_t op = 0xe0900000;
  181. op |= (sreg<<0);
  182. op |= (dreg<<12);
  183. op |= (dreg<<16);
  184. code[code_idx++] = op;
  185. }
  186. void jit_addi(int dreg, int imm) {
  187. jit_movi(9,imm);
  188. jit_addr(dreg,9);
  189. }
  190. void jit_subr(int dreg, int sreg) {
  191. uint32_t op = 0xe0500000;
  192. op |= (sreg<<0);
  193. op |= (dreg<<12);
  194. op |= (dreg<<16);
  195. code[code_idx++] = op;
  196. }
  197. void jit_mulr(int dreg, int sreg) {
  198. uint32_t op = 0xe0100090;
  199. op |= (sreg<<8);
  200. op |= (dreg<<0);
  201. op |= (dreg<<16);
  202. code[code_idx++] = op;
  203. }
  204. void jit_andr(int dreg, int sreg) {
  205. uint32_t op = 0xe0000000;
  206. op |= (sreg<<0);
  207. op |= (dreg<<12);
  208. op |= (dreg<<16);
  209. code[code_idx++] = op;
  210. }
  211. void jit_notr(int dreg) {
  212. uint32_t op = 0xe1e00000;
  213. op |= (dreg<<0);
  214. op |= (dreg<<12);
  215. code[code_idx++] = op;
  216. }
  217. void jit_orr(int dreg, int sreg) {
  218. uint32_t op = 0xe1800000;
  219. op |= (sreg<<0);
  220. op |= (dreg<<12);
  221. op |= (dreg<<16);
  222. code[code_idx++] = op;
  223. }
  224. void jit_xorr(int dreg, int sreg) {
  225. uint32_t op = 0xe0200000;
  226. op |= (sreg<<0);
  227. op |= (dreg<<12);
  228. op |= (dreg<<16);
  229. code[code_idx++] = op;
  230. }
  231. void jit_shlr(int dreg, int sreg) {
  232. uint32_t op = 0xe1a00010;
  233. op |= (sreg<<8);
  234. op |= (dreg<<12);
  235. op |= (dreg<<0);
  236. code[code_idx++] = op;
  237. }
  238. void jit_shrr(int dreg, int sreg) {
  239. uint32_t op = 0xe1a00030;
  240. op |= (sreg<<8);
  241. op |= (dreg<<12);
  242. op |= (dreg<<0);
  243. code[code_idx++] = op;
  244. }
  245. void jit_call(void* func, char* note) {
  246. code[code_idx++] = 0xe92d4000; // stmfd sp!, {lr}
  247. jit_movr(14,15);
  248. jit_lea(15,func);
  249. code[code_idx++] = 0xe8bd4000; // ldmfd sp!, {lr}
  250. }
  251. #define jit_call2 jit_call
  252. #define jit_call3 jit_call
  253. void jit_callr(int reg) {
  254. code[code_idx++] = 0xe92d4000; // stmfd sp!, {lr}
  255. jit_movr(14,15);
  256. jit_movr(15,reg);
  257. code[code_idx++] = 0xe8bd4000; // ldmfd sp!, {lr}
  258. }
  259. int32_t inline_div(int32_t a, int32_t b) {
  260. return a/b;
  261. }
  262. void jit_divr(int dreg, int sreg) {
  263. jit_movr(0,dreg);
  264. jit_movr(1,sreg);
  265. jit_call(inline_div,"div");
  266. if (dreg!=0) jit_movr(dreg,0);
  267. // call the c lib function
  268. // later: http://thinkingeek.com/2013/08/11/arm-assembler-raspberry-pi-chapter-15/
  269. }
  270. int32_t inline_mod(int32_t a, int32_t b) {
  271. return a%b;
  272. }
  273. void jit_modr(int dreg, int sreg) {
  274. jit_movr(0,dreg);
  275. jit_movr(1,sreg);
  276. jit_call(inline_mod,"mod");
  277. if (dreg!=0) jit_movr(dreg,0);
  278. }
  279. void jit_cmpr(int sreg, int dreg) {
  280. uint32_t op = 0xe1500000;
  281. op|=(sreg<<0);
  282. op|=(dreg<<16);
  283. code[code_idx++] = op;
  284. }
  285. void jit_cmpi(int sreg, int imm) {
  286. uint32_t op = 0xe3500000;
  287. op|=imm&0xffff; // TODO double check
  288. op|=(sreg<<16);
  289. code[code_idx++] = op;
  290. }
  291. Label* find_label(char* label) {
  292. for (int i=0; i<label_idx; i++) {
  293. if (jit_labels[i].name) {
  294. //printf("find_label %s label vs %s\r\n",label,jit_labels[i].name);
  295. }
  296. if (jit_labels[i].name && (strcmp(jit_labels[i].name,label)==0)) {
  297. return &jit_labels[i];
  298. }
  299. }
  300. return NULL;
  301. }
  302. Label* find_unresolved_label(char* label) {
  303. for (int i=0; i<unres_labels; i++) {
  304. if (jit_labels_unres[i].name) {
  305. //printf("find_unres_label %s label vs %s\r\n",label,jit_labels_unres[i].name);
  306. }
  307. if (jit_labels_unres[i].name && (strcmp(jit_labels_unres[i].name,label)==0)) {
  308. return &jit_labels_unres[i];
  309. }
  310. }
  311. return NULL;
  312. }
  313. void jit_emit_branch(uint32_t op, char* label) {
  314. Label* lbl = find_label(label);
  315. if (lbl) {
  316. int offset = (lbl->idx - code_idx) - 2;
  317. //printf("offset to %s: %d (*4)\r\n",label,offset);
  318. if (offset<0) {
  319. offset = 0x1000000-(-offset);
  320. op|=offset;
  321. code[code_idx++] = op;
  322. }
  323. } else {
  324. //printf("! label not found %s, adding unresolved.\r\n",label);
  325. jit_labels_unres[unres_labels].name = strdup(label);
  326. jit_labels_unres[unres_labels].idx = code_idx;
  327. code[code_idx++] = op;
  328. unres_labels++;
  329. }
  330. }
  331. void jit_je(char* label) {
  332. uint32_t op = 0x0a000000; // beq
  333. jit_emit_branch(op, label);
  334. }
  335. void jit_jge(char* label) {
  336. uint32_t op = 0xaa000000; // bge
  337. jit_emit_branch(op, label);
  338. }
  339. void jit_jne(char* label) {
  340. uint32_t op = 0x1a000000; // bne
  341. jit_emit_branch(op, label);
  342. }
  343. void jit_jneg(char* label) {
  344. uint32_t op = 0x4a000000; // bmi
  345. jit_emit_branch(op, label);
  346. }
  347. void jit_jmp(char* label) {
  348. //printf("jmp to label: %s\r\n",label);
  349. uint32_t op = 0xea000000; // b
  350. jit_emit_branch(op, label);
  351. }
  352. void jit_label(char* label) {
  353. jit_labels[label_idx].name = strdup(label);
  354. jit_labels[label_idx].idx = code_idx;
  355. Label* unres_lbl = NULL;
  356. while ((unres_lbl = find_unresolved_label(label))) {
  357. //printf("! forward label to %s at idx %d resolved.\r\n",label,unres_lbl->idx);
  358. code[unres_lbl->idx] |= (code_idx - unres_lbl->idx) - 2;
  359. free(unres_lbl->name);
  360. unres_lbl->name = NULL;
  361. unres_lbl->idx = 0;
  362. }
  363. label_idx++;
  364. }
  365. void jit_ret() {
  366. jit_movr(15,14); // lr -> pc
  367. }
  368. void jit_push(int r1, int r2) {
  369. uint32_t op = 0xe92d0000;
  370. for (int i=r1; i<=r2; i++) {
  371. op |= (1<<i); // build bit pattern of registers to push
  372. }
  373. code[code_idx++] = op;
  374. }
  375. void jit_pop(int r1, int r2) {
  376. uint32_t op = 0xe8bd0000;
  377. for (int i=r1; i<=r2; i++) {
  378. op |= (1<<i); // build bit pattern of registers to pop
  379. }
  380. code[code_idx++] = op;
  381. }
  382. // do any needed stack alignment etc. here for host ABI
  383. void jit_host_call_enter() {
  384. }
  385. void jit_host_call_exit() {
  386. }
  387. void jit_comment(char* comment) {
  388. }
  389. void debug_handler() {
  390. // NYI
  391. }