jit_x86.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. char* regnames[] = {
  2. "%eax",
  3. "%edi",
  4. "%esi",
  5. "%edx",
  6. "%ecx",
  7. "%ebx",
  8. "%esp"
  9. };
  10. enum jit_reg {
  11. R0 = 0,
  12. R1,
  13. R2,
  14. R3,
  15. R4,
  16. R5,
  17. R6
  18. };
  19. enum arg_reg {
  20. ARGR0 = 1,
  21. ARGR1 = 2,
  22. ARGR2 = 3
  23. };
  24. uint8_t regi[] = {
  25. 0,
  26. 7,
  27. 6,
  28. 2,
  29. 1,
  30. 3,
  31. 4,
  32. 0
  33. };
  34. #define RSP R6
  35. static uint8_t* code;
  36. static uint32_t code_idx;
  37. #define JIT_MAX_LABELS 32
  38. static int label_idx = 0;
  39. static Label jit_labels[JIT_MAX_LABELS];
  40. static Label jit_labels_unres[JIT_MAX_LABELS]; // unresolved (forward) labels
  41. static int unres_labels = 0;
  42. static int jit_max_size = 0;
  43. void jit_init(uint8_t* dest, int max_size) {
  44. code = dest;
  45. // cleans up jit state
  46. label_idx = 0;
  47. unres_labels = 0;
  48. code_idx = 0;
  49. jit_max_size = max_size; // TODO enforce
  50. for (int i=0; i<JIT_MAX_LABELS; i++) {
  51. if (jit_labels[i].name) free(jit_labels[i].name);
  52. jit_labels[i].name = NULL;
  53. jit_labels[i].idx = 0;
  54. if (jit_labels_unres[i].name) free(jit_labels_unres[i].name);
  55. jit_labels_unres[i].name = NULL;
  56. jit_labels_unres[i].idx = 0;
  57. }
  58. }
  59. void jit_imm(int imm) {
  60. code[code_idx++] = imm&0xff; imm>>=8;
  61. code[code_idx++] = imm&0xff; imm>>=8;
  62. code[code_idx++] = imm&0xff; imm>>=8;
  63. code[code_idx++] = imm&0xff;
  64. }
  65. Label* find_label(char* label) {
  66. int i;
  67. for (i=0; i<label_idx; i++) {
  68. if (jit_labels[i].name && (strcmp(jit_labels[i].name,label)==0)) {
  69. return &jit_labels[i];
  70. }
  71. }
  72. return NULL;
  73. }
  74. Label* find_unresolved_label(char* label) {
  75. int i;
  76. for (i=0; i<unres_labels; i++) {
  77. if (jit_labels_unres[i].name && (strcmp(jit_labels_unres[i].name,label)==0)) {
  78. return &jit_labels_unres[i];
  79. }
  80. }
  81. return NULL;
  82. }
  83. void jit_emit_branch(char* label) {
  84. Label* lbl = find_label(label);
  85. if (lbl) {
  86. int offset = (lbl->idx - code_idx);
  87. //printf("offset to %s: %d (*4)\r\n",label,offset);
  88. if (offset<0) {
  89. offset = 0xffffffff-(-offset)+1 - 4;
  90. jit_imm(offset);
  91. }
  92. } else {
  93. //printf("! label not found %s, adding unresolved.\r\n",label);
  94. jit_labels_unres[unres_labels].name = strdup(label);
  95. jit_labels_unres[unres_labels].idx = code_idx;
  96. jit_imm(0);
  97. unres_labels++;
  98. }
  99. }
  100. void jit_movi(int reg, int imm) {
  101. code[code_idx++] = 0xb8 | regi[reg];
  102. jit_imm(imm);
  103. }
  104. void jit_movr(int dreg, int sreg) {
  105. if (dreg == sreg) return;
  106. code[code_idx++] = 0x89;
  107. code[code_idx++] = 0xc0 | (regi[sreg]<<3) | regi[dreg];
  108. }
  109. void jit_movneg(int dreg, int sreg) {
  110. code[code_idx++] = 0x0f;
  111. code[code_idx++] = 0x48;
  112. code[code_idx++] = 0xc0 | (regi[dreg]<<3) | regi[sreg];
  113. }
  114. void jit_movne(int dreg, int sreg) {
  115. code[code_idx++] = 0x0f;
  116. code[code_idx++] = 0x45;
  117. code[code_idx++] = 0xc0 | (regi[dreg]<<3) | regi[sreg];
  118. }
  119. void jit_moveq(int dreg, int sreg) {
  120. code[code_idx++] = 0x0f;
  121. code[code_idx++] = 0x44;
  122. code[code_idx++] = 0xc0 | (regi[dreg]<<3) | regi[sreg];
  123. }
  124. void jit_lea(int reg, void* addr) {
  125. jit_movi(reg, (uint32_t)addr);
  126. }
  127. void jit_addr(int dreg, int sreg) {
  128. code[code_idx++] = 0x01;
  129. code[code_idx++] = 0xc0 + (regi[sreg]<<3) + regi[dreg];
  130. }
  131. // TODO: smaller immediate encodings
  132. void jit_addi(int dreg, int imm) {
  133. if (dreg == R0) {
  134. code[code_idx++] = 0x05;
  135. } else {
  136. code[code_idx++] = 0x81;
  137. code[code_idx++] = 0xc0 | regi[dreg];
  138. }
  139. jit_imm(imm);
  140. }
  141. void jit_subi(int dreg, int imm) {
  142. if (dreg == R0) {
  143. code[code_idx++] = 0x2d;
  144. } else {
  145. code[code_idx++] = 0x81;
  146. code[code_idx++] = 0xe8 | regi[dreg];
  147. }
  148. jit_imm(imm);
  149. }
  150. void jit_andr(int dreg, int sreg) {
  151. code[code_idx++] = 0x21;
  152. code[code_idx++] = 0xc0 | (regi[sreg]<<3) | regi[dreg];
  153. }
  154. void jit_andi(int dreg, int imm) {
  155. if (dreg == R0) {
  156. code[code_idx++] = 0x25;
  157. } else {
  158. code[code_idx++] = 0x81;
  159. code[code_idx++] = 0xe0 | regi[dreg];
  160. }
  161. jit_imm(imm);
  162. }
  163. void jit_notr(int dreg) {
  164. code[code_idx++] = 0xf7;
  165. code[code_idx++] = 0xd0 | regi[dreg];
  166. }
  167. void jit_orr(int dreg, int sreg) {
  168. code[code_idx++] = 0x09;
  169. code[code_idx++] = 0xc0 | (regi[sreg]<<3) | regi[dreg];
  170. }
  171. void jit_xorr(int dreg, int sreg) {
  172. code[code_idx++] = 0x31;
  173. code[code_idx++] = 0xc0 | (regi[sreg]<<3) | regi[dreg];
  174. }
  175. void jit_shrr(int dreg, int sreg) {
  176. jit_movr(R3,R4);
  177. jit_movr(R4,sreg);
  178. code[code_idx++] = 0xd3; // shr %cl, dreg
  179. code[code_idx++] = 0xe8 | (regi[dreg]);
  180. jit_movr(R4,R3);
  181. }
  182. void jit_shlr(int dreg, int sreg) {
  183. jit_movr(R3,R4);
  184. jit_movr(R4,sreg);
  185. code[code_idx++] = 0xd3; // shr %cl, dreg
  186. code[code_idx++] = 0xe0 | (regi[dreg]);
  187. jit_movr(R4,R3);
  188. }
  189. void jit_subr(int dreg, int sreg) {
  190. code[code_idx++] = 0x29;
  191. code[code_idx++] = 0xc0 | (regi[sreg]<<3) | regi[dreg];
  192. }
  193. void jit_mulr(int dreg, int sreg) {
  194. code[code_idx++] = 0x0f;
  195. code[code_idx++] = 0xaf;
  196. code[code_idx++] = 0xc0 | (regi[dreg]<<3) | regi[sreg];
  197. }
  198. void jit_divr(int dreg, int sreg) {
  199. jit_movr(R0, dreg);
  200. code[code_idx++] = 0x99; // sign-extend rax to edx:rax (cdq)
  201. code[code_idx++] = 0xf7;
  202. code[code_idx++] = 0xf8 | regi[sreg]; // idiv goes to %rax
  203. jit_movr(dreg, R0);
  204. }
  205. void jit_ldr(int reg) {
  206. code[code_idx++] = 0x8b;
  207. code[code_idx++] = (regi[reg]<<3) | regi[reg];
  208. }
  209. void jit_ldr_stack(int dreg, int offset) {
  210. code[code_idx++] = 0x8b;
  211. code[code_idx++] = 0x44 | (regi[dreg]<<3);
  212. code[code_idx++] = 0x24;
  213. code[code_idx++] = (char)offset;
  214. }
  215. void jit_str_stack(int sreg, int offset) {
  216. code[code_idx++] = 0x89;
  217. code[code_idx++] = 0x44 | (regi[sreg]<<3);
  218. code[code_idx++] = 0x24;
  219. code[code_idx++] = (char)offset;
  220. }
  221. // clobbers rdx!
  222. void jit_ldrb(int reg) {
  223. code[code_idx++] = 0x8a; // movb (reg), %dl
  224. code[code_idx++] = 0x10 | regi[reg];
  225. jit_andi(R3, 0xff);
  226. jit_movr(reg, R3);
  227. }
  228. void jit_ldrs(int reg) {
  229. code[code_idx++] = 0x66; // movw (reg), %dx
  230. code[code_idx++] = 0x8b;
  231. code[code_idx++] = 0x10 | regi[reg];
  232. jit_andi(R3, 0xffff);
  233. jit_movr(reg, R3);
  234. }
  235. void jit_ldrw(int reg) {
  236. code[code_idx++] = 0x8b; // movl (reg), %dx
  237. code[code_idx++] = 0x10 | regi[reg];
  238. }
  239. // 8 bit only from rdx!
  240. void jit_strb(int reg) {
  241. code[code_idx++] = 0x88; // movb %dl, (reg)
  242. code[code_idx++] = 0x10 | regi[reg];
  243. }
  244. void jit_strw(int reg) {
  245. code[code_idx++] = 0x89; // movl %edx, (reg)
  246. code[code_idx++] = 0x10 | regi[reg];
  247. }
  248. void jit_strs(int reg) {
  249. code[code_idx++] = 0x66;
  250. code[code_idx++] = 0x89; // movw %dx, (reg)
  251. code[code_idx++] = 0x10 | regi[reg];
  252. }
  253. #define jit_stra jit_strw
  254. void jit_call(void* func, char* note) {
  255. jit_lea(R0, func);
  256. code[code_idx++] = 0x57; // push edi
  257. code[code_idx++] = 0xff; // call *eax
  258. code[code_idx++] = 0xd0;
  259. code[code_idx++] = 0x83;
  260. code[code_idx++] = 0xc4;
  261. code[code_idx++] = 0x04; // add $4, esp
  262. }
  263. void jit_call2(void* func, char* note) {
  264. jit_lea(R0, func);
  265. code[code_idx++] = 0x56; // push esi
  266. code[code_idx++] = 0x57; // push edi
  267. code[code_idx++] = 0xff; // call *eax
  268. code[code_idx++] = 0xd0;
  269. code[code_idx++] = 0x83;
  270. code[code_idx++] = 0xc4;
  271. code[code_idx++] = 0x08; // add $8, esp
  272. }
  273. void jit_call3(void* func, char* note) {
  274. jit_lea(R0, func);
  275. code[code_idx++] = 0x52; // push edx
  276. code[code_idx++] = 0x56; // push esi
  277. code[code_idx++] = 0x57; // push edi
  278. code[code_idx++] = 0xff; // call *eax
  279. code[code_idx++] = 0xd0;
  280. code[code_idx++] = 0x83;
  281. code[code_idx++] = 0xc4;
  282. code[code_idx++] = 12; // add $12, esp
  283. }
  284. void jit_callr(int dreg) {
  285. code[code_idx++] = 0xff;
  286. code[code_idx++] = 0xd0 | regi[dreg];
  287. }
  288. int inline_mod(int a, int b) {
  289. return a%b;
  290. }
  291. void jit_modr(int dreg, int sreg) {
  292. jit_movr(ARGR0,dreg);
  293. jit_movr(ARGR1,sreg);
  294. jit_call2(inline_mod,"mod");
  295. if (dreg!=0) jit_movr(dreg,0);
  296. }
  297. void jit_cmpi(int dreg, int imm) {
  298. if (dreg == R0) {
  299. code[code_idx++] = 0x3d;
  300. } else {
  301. code[code_idx++] = 0x81;
  302. code[code_idx++] = 0xf8 | regi[dreg];
  303. }
  304. jit_imm(imm);
  305. }
  306. void jit_cmpr(int sreg, int dreg) {
  307. code[code_idx++] = 0x39;
  308. code[code_idx++] = 0xc0 | (regi[sreg]<<3) | regi[dreg];
  309. }
  310. void jit_je(char* label) {
  311. code[code_idx++] = 0x0f;
  312. code[code_idx++] = 0x84;
  313. jit_emit_branch(label);
  314. }
  315. void jit_jne(char* label) {
  316. code[code_idx++] = 0x0f;
  317. code[code_idx++] = 0x85;
  318. jit_emit_branch(label);
  319. }
  320. void jit_jneg(char* label) {
  321. code[code_idx++] = 0x0f;
  322. code[code_idx++] = 0x88;
  323. jit_emit_branch(label);
  324. }
  325. void jit_jmp(char* label) {
  326. code[code_idx++] = 0xe9;
  327. jit_emit_branch(label);
  328. }
  329. void jit_label(char* label) {
  330. Label* unres_lbl = NULL;
  331. jit_labels[label_idx].name = strdup(label);
  332. jit_labels[label_idx].idx = code_idx;
  333. while ((unres_lbl = find_unresolved_label(label))) {
  334. //printf("! forward label to %s at idx %d resolved.\r\n",label,unres_lbl->idx);
  335. int offset = (code_idx - unres_lbl->idx);
  336. int imm = offset-4;
  337. code[unres_lbl->idx] = imm&0xff; imm>>=8;
  338. code[unres_lbl->idx+1] = imm&0xff; imm>>=8;
  339. code[unres_lbl->idx+2] = imm&0xff; imm>>=8;
  340. code[unres_lbl->idx+3] = imm&0xff; imm>>=8;
  341. free(unres_lbl->name);
  342. unres_lbl->name = NULL;
  343. unres_lbl->idx = 0;
  344. }
  345. label_idx++;
  346. }
  347. void jit_ret() {
  348. code[code_idx++] = 0xc3;
  349. }
  350. void jit_push(int r1, int r2) {
  351. for (int i=r1; i<=r2; i++) {
  352. code[code_idx++] = 0x50|regi[i];
  353. }
  354. }
  355. void jit_pop(int r1, int r2) {
  356. for (int i=r2; i>=r1; i--) {
  357. code[code_idx++] = 0x58|regi[i];
  358. }
  359. }
  360. void jit_inc_stack(int offset) {
  361. jit_addi(RSP, offset);
  362. }
  363. void jit_dec_stack(int offset) {
  364. jit_subi(RSP, offset);
  365. }
  366. // do any needed stack alignment etc. here for host ABI
  367. void jit_host_call_enter() {
  368. }
  369. void jit_host_call_exit() {
  370. }
  371. void jit_comment(char* comment) {
  372. }
  373. void debug_handler() {
  374. }