jit_arm_raw.c 8.6 KB

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