jit_x86.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. 0
  32. };
  33. #define RSP R6
  34. static uint8_t* code;
  35. static uint32_t code_idx;
  36. typedef struct Label {
  37. char* name;
  38. uint32_t idx;
  39. } Label;
  40. #define JIT_MAX_LABELS 32
  41. static int label_idx = 0;
  42. static Label jit_labels[JIT_MAX_LABELS];
  43. static Label jit_labels_unres[JIT_MAX_LABELS]; // unresolved (forward) labels
  44. static int unres_labels = 0;
  45. void jit_init() {
  46. // cleans up jit state
  47. label_idx = 0;
  48. unres_labels = 0;
  49. code_idx = 0;
  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_movi(int reg, int imm) {
  60. /*if (reg==R0) {
  61. code[code_idx++] = 0xa1;
  62. } else {
  63. code[code_idx++] = 0x8b;
  64. code[code_idx++] = reg_code(reg);
  65. }*/
  66. code[code_idx++] = 0xb8 | regi[reg];
  67. code[code_idx++] = imm&0xff; imm>>=8;
  68. code[code_idx++] = imm&0xff; imm>>=8;
  69. code[code_idx++] = imm&0xff; imm>>=8;
  70. code[code_idx++] = imm&0xff;
  71. }
  72. void jit_movr(int dreg, int sreg) {
  73. code[code_idx++] = 0x89;
  74. code[code_idx++] = 0xc0 + (regi[sreg]<<3) + regi[dreg];
  75. }
  76. void jit_movneg(int dreg, int sreg) {
  77. }
  78. void jit_movne(int dreg, int sreg) {
  79. }
  80. void jit_moveq(int dreg, int sreg) {
  81. }
  82. void jit_lea(int reg, void* addr) {
  83. jit_movi(reg, (uint32_t)addr);
  84. }
  85. void jit_ldr(int reg) {
  86. code[code_idx++] = 0x8b;
  87. code[code_idx++] = (regi[reg]<<3) + regi[reg];
  88. }
  89. void jit_ldr_stack(int dreg, int offset) {
  90. }
  91. void jit_str_stack(int sreg, int offset) {
  92. }
  93. void jit_inc_stack(int offset) {
  94. }
  95. void jit_dec_stack(int offset) {
  96. }
  97. // clobbers rdx!
  98. void jit_ldrb(int reg) {
  99. /*fprintf(jit_out, "movb (%s), %%dl\n", regnames[reg]);
  100. fprintf(jit_out, "andq $0xff, %rdx\n", regnames[reg]);
  101. if (reg!=3) {
  102. fprintf(jit_out, "movq %%rdx, %s\n", regnames[reg]);
  103. }*/
  104. }
  105. // clobbers rdx!
  106. void jit_ldrw(int reg) {
  107. /*fprintf(jit_out, "movl (%s), %%edx\n", regnames[reg]);
  108. if (reg!=3) {
  109. fprintf(jit_out, "movq %%rdx, %s\n", regnames[reg]);
  110. }*/
  111. }
  112. // 8 bit only from rdx!
  113. void jit_strb(int reg) {
  114. //fprintf(jit_out, "movb %%dl, (%s)\n", regnames[reg]);
  115. }
  116. // 32 bit only from rdx!
  117. void jit_strw(int reg) {
  118. //fprintf(jit_out, "movl %%edx, (%s)\n", regnames[reg]);
  119. }
  120. void jit_addr(int dreg, int sreg) {
  121. code[code_idx++] = 0x01;
  122. code[code_idx++] = 0xc0 + (regi[sreg]<<3) + regi[dreg];
  123. }
  124. void jit_addi(int dreg, int imm) {
  125. }
  126. void jit_andr(int dreg, int sreg) {
  127. }
  128. void jit_orr(int dreg, int sreg) {
  129. }
  130. void jit_xorr(int dreg, int sreg) {
  131. }
  132. void jit_shrr(int dreg, int sreg) {
  133. //fprintf(jit_out, "movq %s, %%rcx\n", regnames[sreg]);
  134. //fprintf(jit_out, "shr %%cl, %s\n", regnames[dreg]);
  135. }
  136. void jit_shlr(int dreg, int sreg) {
  137. //fprintf(jit_out, "movq %s, %%rcx\n", regnames[sreg]);
  138. //fprintf(jit_out, "shl %%cl, %s\n", regnames[dreg]);
  139. }
  140. void jit_subr(int dreg, int sreg) {
  141. //fprintf(jit_out, "subq %s, %s\n", regnames[sreg], regnames[dreg]);
  142. }
  143. void jit_mulr(int dreg, int sreg) {
  144. //fprintf(jit_out, "imulq %s, %s\n", regnames[sreg], regnames[dreg]);
  145. }
  146. void jit_divr(int dreg, int sreg) {
  147. /*fprintf(jit_out, "movq %s, %%rax\n", regnames[dreg]);
  148. fprintf(jit_out, "cqto\n");
  149. fprintf(jit_out, "idivq %s\n", regnames[sreg]);
  150. fprintf(jit_out, "movq %%rax, %s\n", regnames[dreg]);*/
  151. }
  152. void jit_call(void* func, char* note) {
  153. jit_lea(R0, func);
  154. code[code_idx++] = 0x57; // push edi
  155. code[code_idx++] = 0xff; // call *eax
  156. code[code_idx++] = 0xd0;
  157. code[code_idx++] = 0x83;
  158. code[code_idx++] = 0xc4;
  159. code[code_idx++] = 0x04; // add $4, esp
  160. }
  161. void jit_call2(void* func, char* note) {
  162. jit_lea(R0, func);
  163. code[code_idx++] = 0x56; // push esi
  164. code[code_idx++] = 0x57; // push edi
  165. code[code_idx++] = 0xff; // call *eax
  166. code[code_idx++] = 0xd0;
  167. code[code_idx++] = 0x83;
  168. code[code_idx++] = 0xc4;
  169. code[code_idx++] = 0x08; // add $8, esp
  170. }
  171. void jit_call3(void* func, char* note) {
  172. jit_lea(R0, func);
  173. code[code_idx++] = 0x52; // push edx
  174. code[code_idx++] = 0x56; // push esi
  175. code[code_idx++] = 0x57; // push edi
  176. code[code_idx++] = 0xff; // call *eax
  177. code[code_idx++] = 0xd0;
  178. code[code_idx++] = 0x83;
  179. code[code_idx++] = 0xc4;
  180. code[code_idx++] = 12; // add $12, esp
  181. }
  182. void jit_callr(int dreg) {
  183. }
  184. int32_t inline_mod(int a, int b) {
  185. return a%b;
  186. }
  187. void jit_modr(int dreg, int sreg) {
  188. /*jit_movr(ARGR0,dreg);
  189. jit_movr(ARGR1,sreg);
  190. jit_call(inline_mod,"mod");
  191. if (dreg!=0) jit_movr(dreg,0);*/
  192. }
  193. void jit_cmpi(int sreg, int imm) {
  194. //fprintf(jit_out, "cmp $%d, %s\n", imm, regnames[sreg]);
  195. }
  196. void jit_cmpr(int sreg, int dreg) {
  197. //fprintf(jit_out, "cmp %s, %s\n", regnames[dreg], regnames[sreg]);
  198. }
  199. void jit_je(char* label) {
  200. //fprintf(jit_out, "je %s\n", label);
  201. }
  202. void jit_jneg(char* label) {
  203. //fprintf(jit_out, "js %s\n", label);
  204. }
  205. void jit_jmp(char* label) {
  206. }
  207. void jit_label(char* label) {
  208. }
  209. void jit_ret() {
  210. code[code_idx++] = 0xc3;
  211. }
  212. void jit_push(int r1, int r2) {
  213. /*for (int i=r1; i<=r2; i++) {
  214. fprintf(jit_out, "push %s\n",regnames[i]);
  215. }*/
  216. }
  217. void jit_pop(int r1, int r2) {
  218. /*for (int i=r2; i>=r1; i--) {
  219. fprintf(jit_out, "pop %s\n",regnames[i]);
  220. }*/
  221. }
  222. void debug_handler() {
  223. }