asmx86.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdint.h>
  27. #include <stdio.h>
  28. #include <assert.h>
  29. #include <string.h>
  30. #include "py/mpconfig.h"
  31. // wrapper around everything in this file
  32. #if MICROPY_EMIT_X86
  33. #include "py/asmx86.h"
  34. /* all offsets are measured in multiples of 4 bytes */
  35. #define WORD_SIZE (4)
  36. #define OPCODE_NOP (0x90)
  37. #define OPCODE_PUSH_R32 (0x50)
  38. // #define OPCODE_PUSH_I32 (0x68)
  39. // #define OPCODE_PUSH_M32 (0xff) /* /6 */
  40. #define OPCODE_POP_R32 (0x58)
  41. #define OPCODE_RET (0xc3)
  42. // #define OPCODE_MOV_I8_TO_R8 (0xb0) /* +rb */
  43. #define OPCODE_MOV_I32_TO_R32 (0xb8)
  44. // #define OPCODE_MOV_I32_TO_RM32 (0xc7)
  45. #define OPCODE_MOV_R8_TO_RM8 (0x88) /* /r */
  46. #define OPCODE_MOV_R32_TO_RM32 (0x89) /* /r */
  47. #define OPCODE_MOV_RM32_TO_R32 (0x8b) /* /r */
  48. #define OPCODE_MOVZX_RM8_TO_R32 (0xb6) /* 0x0f 0xb6/r */
  49. #define OPCODE_MOVZX_RM16_TO_R32 (0xb7) /* 0x0f 0xb7/r */
  50. #define OPCODE_LEA_MEM_TO_R32 (0x8d) /* /r */
  51. #define OPCODE_NOT_RM32 (0xf7) /* /2 */
  52. #define OPCODE_NEG_RM32 (0xf7) /* /3 */
  53. #define OPCODE_AND_R32_TO_RM32 (0x21) /* /r */
  54. #define OPCODE_OR_R32_TO_RM32 (0x09) /* /r */
  55. #define OPCODE_XOR_R32_TO_RM32 (0x31) /* /r */
  56. #define OPCODE_ADD_R32_TO_RM32 (0x01)
  57. #define OPCODE_ADD_I32_TO_RM32 (0x81) /* /0 */
  58. #define OPCODE_ADD_I8_TO_RM32 (0x83) /* /0 */
  59. #define OPCODE_SUB_R32_FROM_RM32 (0x29)
  60. #define OPCODE_SUB_I32_FROM_RM32 (0x81) /* /5 */
  61. #define OPCODE_SUB_I8_FROM_RM32 (0x83) /* /5 */
  62. // #define OPCODE_SHL_RM32_BY_I8 (0xc1) /* /4 */
  63. // #define OPCODE_SHR_RM32_BY_I8 (0xc1) /* /5 */
  64. // #define OPCODE_SAR_RM32_BY_I8 (0xc1) /* /7 */
  65. #define OPCODE_SHL_RM32_CL (0xd3) /* /4 */
  66. #define OPCODE_SHR_RM32_CL (0xd3) /* /5 */
  67. #define OPCODE_SAR_RM32_CL (0xd3) /* /7 */
  68. // #define OPCODE_CMP_I32_WITH_RM32 (0x81) /* /7 */
  69. // #define OPCODE_CMP_I8_WITH_RM32 (0x83) /* /7 */
  70. #define OPCODE_CMP_R32_WITH_RM32 (0x39)
  71. // #define OPCODE_CMP_RM32_WITH_R32 (0x3b)
  72. #define OPCODE_TEST_R8_WITH_RM8 (0x84) /* /r */
  73. #define OPCODE_TEST_R32_WITH_RM32 (0x85) /* /r */
  74. #define OPCODE_JMP_REL8 (0xeb)
  75. #define OPCODE_JMP_REL32 (0xe9)
  76. #define OPCODE_JMP_RM32 (0xff) /* /4 */
  77. #define OPCODE_JCC_REL8 (0x70) /* | jcc type */
  78. #define OPCODE_JCC_REL32_A (0x0f)
  79. #define OPCODE_JCC_REL32_B (0x80) /* | jcc type */
  80. #define OPCODE_SETCC_RM8_A (0x0f)
  81. #define OPCODE_SETCC_RM8_B (0x90) /* | jcc type, /0 */
  82. #define OPCODE_CALL_REL32 (0xe8)
  83. #define OPCODE_CALL_RM32 (0xff) /* /2 */
  84. #define OPCODE_LEAVE (0xc9)
  85. #define MODRM_R32(x) ((x) << 3)
  86. #define MODRM_RM_DISP0 (0x00)
  87. #define MODRM_RM_DISP8 (0x40)
  88. #define MODRM_RM_DISP32 (0x80)
  89. #define MODRM_RM_REG (0xc0)
  90. #define MODRM_RM_R32(x) (x)
  91. #define OP_SIZE_PREFIX (0x66)
  92. #define IMM32_L0(x) ((x) & 0xff)
  93. #define IMM32_L1(x) (((x) >> 8) & 0xff)
  94. #define IMM32_L2(x) (((x) >> 16) & 0xff)
  95. #define IMM32_L3(x) (((x) >> 24) & 0xff)
  96. #define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
  97. static void asm_x86_write_byte_1(asm_x86_t *as, byte b1) {
  98. byte *c = mp_asm_base_get_cur_to_write_bytes(&as->base, 1);
  99. if (c != NULL) {
  100. c[0] = b1;
  101. }
  102. }
  103. static void asm_x86_write_byte_2(asm_x86_t *as, byte b1, byte b2) {
  104. byte *c = mp_asm_base_get_cur_to_write_bytes(&as->base, 2);
  105. if (c != NULL) {
  106. c[0] = b1;
  107. c[1] = b2;
  108. }
  109. }
  110. static void asm_x86_write_byte_3(asm_x86_t *as, byte b1, byte b2, byte b3) {
  111. byte *c = mp_asm_base_get_cur_to_write_bytes(&as->base, 3);
  112. if (c != NULL) {
  113. c[0] = b1;
  114. c[1] = b2;
  115. c[2] = b3;
  116. }
  117. }
  118. static void asm_x86_write_word32(asm_x86_t *as, int w32) {
  119. byte *c = mp_asm_base_get_cur_to_write_bytes(&as->base, 4);
  120. if (c != NULL) {
  121. c[0] = IMM32_L0(w32);
  122. c[1] = IMM32_L1(w32);
  123. c[2] = IMM32_L2(w32);
  124. c[3] = IMM32_L3(w32);
  125. }
  126. }
  127. static void asm_x86_write_r32_disp(asm_x86_t *as, int r32, int disp_r32, int disp_offset) {
  128. uint8_t rm_disp;
  129. if (disp_offset == 0 && disp_r32 != ASM_X86_REG_EBP) {
  130. rm_disp = MODRM_RM_DISP0;
  131. } else if (SIGNED_FIT8(disp_offset)) {
  132. rm_disp = MODRM_RM_DISP8;
  133. } else {
  134. rm_disp = MODRM_RM_DISP32;
  135. }
  136. asm_x86_write_byte_1(as, MODRM_R32(r32) | rm_disp | MODRM_RM_R32(disp_r32));
  137. if (disp_r32 == ASM_X86_REG_ESP) {
  138. // Special case for esp, it needs a SIB byte
  139. asm_x86_write_byte_1(as, 0x24);
  140. }
  141. if (rm_disp == MODRM_RM_DISP8) {
  142. asm_x86_write_byte_1(as, IMM32_L0(disp_offset));
  143. } else if (rm_disp == MODRM_RM_DISP32) {
  144. asm_x86_write_word32(as, disp_offset);
  145. }
  146. }
  147. static void asm_x86_generic_r32_r32(asm_x86_t *as, int dest_r32, int src_r32, int op) {
  148. asm_x86_write_byte_2(as, op, MODRM_R32(src_r32) | MODRM_RM_REG | MODRM_RM_R32(dest_r32));
  149. }
  150. #if 0
  151. static void asm_x86_nop(asm_x86_t *as) {
  152. asm_x86_write_byte_1(as, OPCODE_NOP);
  153. }
  154. #endif
  155. static void asm_x86_push_r32(asm_x86_t *as, int src_r32) {
  156. asm_x86_write_byte_1(as, OPCODE_PUSH_R32 | src_r32);
  157. }
  158. #if 0
  159. void asm_x86_push_i32(asm_x86_t *as, int src_i32) {
  160. asm_x86_write_byte_1(as, OPCODE_PUSH_I32);
  161. asm_x86_write_word32(as, src_i32);
  162. }
  163. void asm_x86_push_disp(asm_x86_t *as, int src_r32, int src_offset) {
  164. asm_x86_write_byte_1(as, OPCODE_PUSH_M32);
  165. asm_x86_write_r32_disp(as, 6, src_r32, src_offset);
  166. }
  167. #endif
  168. static void asm_x86_pop_r32(asm_x86_t *as, int dest_r32) {
  169. asm_x86_write_byte_1(as, OPCODE_POP_R32 | dest_r32);
  170. }
  171. static void asm_x86_ret(asm_x86_t *as) {
  172. asm_x86_write_byte_1(as, OPCODE_RET);
  173. }
  174. void asm_x86_mov_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
  175. asm_x86_generic_r32_r32(as, dest_r32, src_r32, OPCODE_MOV_R32_TO_RM32);
  176. }
  177. void asm_x86_mov_r8_to_mem8(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp) {
  178. asm_x86_write_byte_1(as, OPCODE_MOV_R8_TO_RM8);
  179. asm_x86_write_r32_disp(as, src_r32, dest_r32, dest_disp);
  180. }
  181. void asm_x86_mov_r16_to_mem16(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp) {
  182. asm_x86_write_byte_2(as, OP_SIZE_PREFIX, OPCODE_MOV_R32_TO_RM32);
  183. asm_x86_write_r32_disp(as, src_r32, dest_r32, dest_disp);
  184. }
  185. void asm_x86_mov_r32_to_mem32(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp) {
  186. asm_x86_write_byte_1(as, OPCODE_MOV_R32_TO_RM32);
  187. asm_x86_write_r32_disp(as, src_r32, dest_r32, dest_disp);
  188. }
  189. void asm_x86_mov_mem8_to_r32zx(asm_x86_t *as, int src_r32, int src_disp, int dest_r32) {
  190. asm_x86_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM8_TO_R32);
  191. asm_x86_write_r32_disp(as, dest_r32, src_r32, src_disp);
  192. }
  193. void asm_x86_mov_mem16_to_r32zx(asm_x86_t *as, int src_r32, int src_disp, int dest_r32) {
  194. asm_x86_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM16_TO_R32);
  195. asm_x86_write_r32_disp(as, dest_r32, src_r32, src_disp);
  196. }
  197. void asm_x86_mov_mem32_to_r32(asm_x86_t *as, int src_r32, int src_disp, int dest_r32) {
  198. asm_x86_write_byte_1(as, OPCODE_MOV_RM32_TO_R32);
  199. asm_x86_write_r32_disp(as, dest_r32, src_r32, src_disp);
  200. }
  201. static void asm_x86_lea_disp_to_r32(asm_x86_t *as, int src_r32, int src_disp, int dest_r32) {
  202. asm_x86_write_byte_1(as, OPCODE_LEA_MEM_TO_R32);
  203. asm_x86_write_r32_disp(as, dest_r32, src_r32, src_disp);
  204. }
  205. #if 0
  206. void asm_x86_mov_i8_to_r8(asm_x86_t *as, int src_i8, int dest_r32) {
  207. asm_x86_write_byte_2(as, OPCODE_MOV_I8_TO_R8 | dest_r32, src_i8);
  208. }
  209. #endif
  210. size_t asm_x86_mov_i32_to_r32(asm_x86_t *as, int32_t src_i32, int dest_r32) {
  211. asm_x86_write_byte_1(as, OPCODE_MOV_I32_TO_R32 | dest_r32);
  212. size_t loc = mp_asm_base_get_code_pos(&as->base);
  213. asm_x86_write_word32(as, src_i32);
  214. return loc;
  215. }
  216. void asm_x86_not_r32(asm_x86_t *as, int dest_r32) {
  217. asm_x86_generic_r32_r32(as, dest_r32, 2, OPCODE_NOT_RM32);
  218. }
  219. void asm_x86_neg_r32(asm_x86_t *as, int dest_r32) {
  220. asm_x86_generic_r32_r32(as, dest_r32, 3, OPCODE_NEG_RM32);
  221. }
  222. void asm_x86_and_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
  223. asm_x86_generic_r32_r32(as, dest_r32, src_r32, OPCODE_AND_R32_TO_RM32);
  224. }
  225. void asm_x86_or_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
  226. asm_x86_generic_r32_r32(as, dest_r32, src_r32, OPCODE_OR_R32_TO_RM32);
  227. }
  228. void asm_x86_xor_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
  229. asm_x86_generic_r32_r32(as, dest_r32, src_r32, OPCODE_XOR_R32_TO_RM32);
  230. }
  231. void asm_x86_shl_r32_cl(asm_x86_t *as, int dest_r32) {
  232. asm_x86_generic_r32_r32(as, dest_r32, 4, OPCODE_SHL_RM32_CL);
  233. }
  234. void asm_x86_shr_r32_cl(asm_x86_t *as, int dest_r32) {
  235. asm_x86_generic_r32_r32(as, dest_r32, 5, OPCODE_SHR_RM32_CL);
  236. }
  237. void asm_x86_sar_r32_cl(asm_x86_t *as, int dest_r32) {
  238. asm_x86_generic_r32_r32(as, dest_r32, 7, OPCODE_SAR_RM32_CL);
  239. }
  240. void asm_x86_add_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
  241. asm_x86_generic_r32_r32(as, dest_r32, src_r32, OPCODE_ADD_R32_TO_RM32);
  242. }
  243. static void asm_x86_add_i32_to_r32(asm_x86_t *as, int src_i32, int dest_r32) {
  244. if (SIGNED_FIT8(src_i32)) {
  245. asm_x86_write_byte_2(as, OPCODE_ADD_I8_TO_RM32, MODRM_R32(0) | MODRM_RM_REG | MODRM_RM_R32(dest_r32));
  246. asm_x86_write_byte_1(as, src_i32 & 0xff);
  247. } else {
  248. asm_x86_write_byte_2(as, OPCODE_ADD_I32_TO_RM32, MODRM_R32(0) | MODRM_RM_REG | MODRM_RM_R32(dest_r32));
  249. asm_x86_write_word32(as, src_i32);
  250. }
  251. }
  252. void asm_x86_sub_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
  253. asm_x86_generic_r32_r32(as, dest_r32, src_r32, OPCODE_SUB_R32_FROM_RM32);
  254. }
  255. static void asm_x86_sub_r32_i32(asm_x86_t *as, int dest_r32, int src_i32) {
  256. if (SIGNED_FIT8(src_i32)) {
  257. // defaults to 32 bit operation
  258. asm_x86_write_byte_2(as, OPCODE_SUB_I8_FROM_RM32, MODRM_R32(5) | MODRM_RM_REG | MODRM_RM_R32(dest_r32));
  259. asm_x86_write_byte_1(as, src_i32 & 0xff);
  260. } else {
  261. // defaults to 32 bit operation
  262. asm_x86_write_byte_2(as, OPCODE_SUB_I32_FROM_RM32, MODRM_R32(5) | MODRM_RM_REG | MODRM_RM_R32(dest_r32));
  263. asm_x86_write_word32(as, src_i32);
  264. }
  265. }
  266. void asm_x86_mul_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
  267. // imul reg32, reg/mem32 -- 0x0f 0xaf /r
  268. asm_x86_write_byte_3(as, 0x0f, 0xaf, MODRM_R32(dest_r32) | MODRM_RM_REG | MODRM_RM_R32(src_r32));
  269. }
  270. #if 0
  271. /* shifts not tested */
  272. void asm_x86_shl_r32_by_imm(asm_x86_t *as, int r32, int imm) {
  273. asm_x86_write_byte_2(as, OPCODE_SHL_RM32_BY_I8, MODRM_R32(4) | MODRM_RM_REG | MODRM_RM_R32(r32));
  274. asm_x86_write_byte_1(as, imm);
  275. }
  276. void asm_x86_shr_r32_by_imm(asm_x86_t *as, int r32, int imm) {
  277. asm_x86_write_byte_2(as, OPCODE_SHR_RM32_BY_I8, MODRM_R32(5) | MODRM_RM_REG | MODRM_RM_R32(r32));
  278. asm_x86_write_byte_1(as, imm);
  279. }
  280. void asm_x86_sar_r32_by_imm(asm_x86_t *as, int r32, int imm) {
  281. asm_x86_write_byte_2(as, OPCODE_SAR_RM32_BY_I8, MODRM_R32(7) | MODRM_RM_REG | MODRM_RM_R32(r32));
  282. asm_x86_write_byte_1(as, imm);
  283. }
  284. #endif
  285. void asm_x86_cmp_r32_with_r32(asm_x86_t *as, int src_r32_a, int src_r32_b) {
  286. asm_x86_generic_r32_r32(as, src_r32_b, src_r32_a, OPCODE_CMP_R32_WITH_RM32);
  287. }
  288. #if 0
  289. void asm_x86_cmp_i32_with_r32(asm_x86_t *as, int src_i32, int src_r32) {
  290. if (SIGNED_FIT8(src_i32)) {
  291. asm_x86_write_byte_2(as, OPCODE_CMP_I8_WITH_RM32, MODRM_R32(7) | MODRM_RM_REG | MODRM_RM_R32(src_r32));
  292. asm_x86_write_byte_1(as, src_i32 & 0xff);
  293. } else {
  294. asm_x86_write_byte_2(as, OPCODE_CMP_I32_WITH_RM32, MODRM_R32(7) | MODRM_RM_REG | MODRM_RM_R32(src_r32));
  295. asm_x86_write_word32(as, src_i32);
  296. }
  297. }
  298. #endif
  299. void asm_x86_test_r8_with_r8(asm_x86_t *as, int src_r32_a, int src_r32_b) {
  300. asm_x86_write_byte_2(as, OPCODE_TEST_R8_WITH_RM8, MODRM_R32(src_r32_a) | MODRM_RM_REG | MODRM_RM_R32(src_r32_b));
  301. }
  302. void asm_x86_test_r32_with_r32(asm_x86_t *as, int src_r32_a, int src_r32_b) {
  303. asm_x86_generic_r32_r32(as, src_r32_b, src_r32_a, OPCODE_TEST_R32_WITH_RM32);
  304. }
  305. void asm_x86_setcc_r8(asm_x86_t *as, mp_uint_t jcc_type, int dest_r8) {
  306. asm_x86_write_byte_3(as, OPCODE_SETCC_RM8_A, OPCODE_SETCC_RM8_B | jcc_type, MODRM_R32(0) | MODRM_RM_REG | MODRM_RM_R32(dest_r8));
  307. }
  308. void asm_x86_jmp_reg(asm_x86_t *as, int src_r32) {
  309. asm_x86_write_byte_2(as, OPCODE_JMP_RM32, MODRM_R32(4) | MODRM_RM_REG | MODRM_RM_R32(src_r32));
  310. }
  311. static mp_uint_t get_label_dest(asm_x86_t *as, mp_uint_t label) {
  312. assert(label < as->base.max_num_labels);
  313. return as->base.label_offsets[label];
  314. }
  315. void asm_x86_jmp_label(asm_x86_t *as, mp_uint_t label) {
  316. mp_uint_t dest = get_label_dest(as, label);
  317. mp_int_t rel = dest - as->base.code_offset;
  318. if (dest != (mp_uint_t)-1 && rel < 0) {
  319. // is a backwards jump, so we know the size of the jump on the first pass
  320. // calculate rel assuming 8 bit relative jump
  321. rel -= 2;
  322. if (SIGNED_FIT8(rel)) {
  323. asm_x86_write_byte_2(as, OPCODE_JMP_REL8, rel & 0xff);
  324. } else {
  325. rel += 2;
  326. goto large_jump;
  327. }
  328. } else {
  329. // is a forwards jump, so need to assume it's large
  330. large_jump:
  331. rel -= 5;
  332. asm_x86_write_byte_1(as, OPCODE_JMP_REL32);
  333. asm_x86_write_word32(as, rel);
  334. }
  335. }
  336. void asm_x86_jcc_label(asm_x86_t *as, mp_uint_t jcc_type, mp_uint_t label) {
  337. mp_uint_t dest = get_label_dest(as, label);
  338. mp_int_t rel = dest - as->base.code_offset;
  339. if (dest != (mp_uint_t)-1 && rel < 0) {
  340. // is a backwards jump, so we know the size of the jump on the first pass
  341. // calculate rel assuming 8 bit relative jump
  342. rel -= 2;
  343. if (SIGNED_FIT8(rel)) {
  344. asm_x86_write_byte_2(as, OPCODE_JCC_REL8 | jcc_type, rel & 0xff);
  345. } else {
  346. rel += 2;
  347. goto large_jump;
  348. }
  349. } else {
  350. // is a forwards jump, so need to assume it's large
  351. large_jump:
  352. rel -= 6;
  353. asm_x86_write_byte_2(as, OPCODE_JCC_REL32_A, OPCODE_JCC_REL32_B | jcc_type);
  354. asm_x86_write_word32(as, rel);
  355. }
  356. }
  357. void asm_x86_entry(asm_x86_t *as, int num_locals) {
  358. assert(num_locals >= 0);
  359. asm_x86_push_r32(as, ASM_X86_REG_EBP);
  360. asm_x86_push_r32(as, ASM_X86_REG_EBX);
  361. asm_x86_push_r32(as, ASM_X86_REG_ESI);
  362. asm_x86_push_r32(as, ASM_X86_REG_EDI);
  363. num_locals |= 3; // make it odd so stack is aligned on 16 byte boundary
  364. asm_x86_sub_r32_i32(as, ASM_X86_REG_ESP, num_locals * WORD_SIZE);
  365. as->num_locals = num_locals;
  366. }
  367. void asm_x86_exit(asm_x86_t *as) {
  368. asm_x86_sub_r32_i32(as, ASM_X86_REG_ESP, -as->num_locals * WORD_SIZE);
  369. asm_x86_pop_r32(as, ASM_X86_REG_EDI);
  370. asm_x86_pop_r32(as, ASM_X86_REG_ESI);
  371. asm_x86_pop_r32(as, ASM_X86_REG_EBX);
  372. asm_x86_pop_r32(as, ASM_X86_REG_EBP);
  373. asm_x86_ret(as);
  374. }
  375. static int asm_x86_arg_offset_from_esp(asm_x86_t *as, size_t arg_num) {
  376. // Above esp are: locals, 4 saved registers, return eip, arguments
  377. return (as->num_locals + 4 + 1 + arg_num) * WORD_SIZE;
  378. }
  379. #if 0
  380. void asm_x86_push_arg(asm_x86_t *as, int src_arg_num) {
  381. asm_x86_push_disp(as, ASM_X86_REG_ESP, asm_x86_arg_offset_from_esp(as, src_arg_num));
  382. }
  383. #endif
  384. void asm_x86_mov_arg_to_r32(asm_x86_t *as, int src_arg_num, int dest_r32) {
  385. asm_x86_mov_mem32_to_r32(as, ASM_X86_REG_ESP, asm_x86_arg_offset_from_esp(as, src_arg_num), dest_r32);
  386. }
  387. #if 0
  388. void asm_x86_mov_r32_to_arg(asm_x86_t *as, int src_r32, int dest_arg_num) {
  389. asm_x86_mov_r32_to_mem32(as, src_r32, ASM_X86_REG_ESP, asm_x86_arg_offset_from_esp(as, dest_arg_num));
  390. }
  391. #endif
  392. // locals:
  393. // - stored on the stack in ascending order
  394. // - numbered 0 through as->num_locals-1
  395. // - ESP points to the first local
  396. //
  397. // | ESP
  398. // v
  399. // l0 l1 l2 ... l(n-1)
  400. // ^ ^
  401. // | low address | high address in RAM
  402. //
  403. static int asm_x86_local_offset_from_esp(asm_x86_t *as, int local_num) {
  404. (void)as;
  405. // Stack is full descending, ESP points to local0
  406. return local_num * WORD_SIZE;
  407. }
  408. void asm_x86_mov_local_to_r32(asm_x86_t *as, int src_local_num, int dest_r32) {
  409. asm_x86_mov_mem32_to_r32(as, ASM_X86_REG_ESP, asm_x86_local_offset_from_esp(as, src_local_num), dest_r32);
  410. }
  411. void asm_x86_mov_r32_to_local(asm_x86_t *as, int src_r32, int dest_local_num) {
  412. asm_x86_mov_r32_to_mem32(as, src_r32, ASM_X86_REG_ESP, asm_x86_local_offset_from_esp(as, dest_local_num));
  413. }
  414. void asm_x86_mov_local_addr_to_r32(asm_x86_t *as, int local_num, int dest_r32) {
  415. int offset = asm_x86_local_offset_from_esp(as, local_num);
  416. if (offset == 0) {
  417. asm_x86_mov_r32_r32(as, dest_r32, ASM_X86_REG_ESP);
  418. } else {
  419. asm_x86_lea_disp_to_r32(as, ASM_X86_REG_ESP, offset, dest_r32);
  420. }
  421. }
  422. void asm_x86_mov_reg_pcrel(asm_x86_t *as, int dest_r32, mp_uint_t label) {
  423. asm_x86_write_byte_1(as, OPCODE_CALL_REL32);
  424. asm_x86_write_word32(as, 0);
  425. mp_uint_t dest = get_label_dest(as, label);
  426. mp_int_t rel = dest - as->base.code_offset;
  427. asm_x86_pop_r32(as, dest_r32);
  428. // PC rel is usually a forward reference, so need to assume it's large
  429. asm_x86_write_byte_2(as, OPCODE_ADD_I32_TO_RM32, MODRM_R32(0) | MODRM_RM_REG | MODRM_RM_R32(dest_r32));
  430. asm_x86_write_word32(as, rel);
  431. }
  432. #if 0
  433. void asm_x86_push_local(asm_x86_t *as, int local_num) {
  434. asm_x86_push_disp(as, ASM_X86_REG_ESP, asm_x86_local_offset_from_esp(as, local_num));
  435. }
  436. void asm_x86_push_local_addr(asm_x86_t *as, int local_num, int temp_r32) {
  437. asm_x86_mov_r32_r32(as, temp_r32, ASM_X86_REG_ESP);
  438. asm_x86_add_i32_to_r32(as, asm_x86_local_offset_from_esp(as, local_num), temp_r32);
  439. asm_x86_push_r32(as, temp_r32);
  440. }
  441. #endif
  442. void asm_x86_call_ind(asm_x86_t *as, size_t fun_id, mp_uint_t n_args, int temp_r32) {
  443. assert(n_args <= 4);
  444. // Align stack on 16-byte boundary during the call
  445. unsigned int align = ((n_args + 3) & ~3) - n_args;
  446. if (align) {
  447. asm_x86_sub_r32_i32(as, ASM_X86_REG_ESP, align * WORD_SIZE);
  448. }
  449. if (n_args > 3) {
  450. asm_x86_push_r32(as, ASM_X86_REG_ARG_4);
  451. }
  452. if (n_args > 2) {
  453. asm_x86_push_r32(as, ASM_X86_REG_ARG_3);
  454. }
  455. if (n_args > 1) {
  456. asm_x86_push_r32(as, ASM_X86_REG_ARG_2);
  457. }
  458. if (n_args > 0) {
  459. asm_x86_push_r32(as, ASM_X86_REG_ARG_1);
  460. }
  461. // Load the pointer to the function and make the call
  462. asm_x86_mov_mem32_to_r32(as, ASM_X86_REG_FUN_TABLE, fun_id * WORD_SIZE, temp_r32);
  463. asm_x86_write_byte_2(as, OPCODE_CALL_RM32, MODRM_R32(2) | MODRM_RM_REG | MODRM_RM_R32(temp_r32));
  464. // the caller must clean up the stack
  465. if (n_args > 0) {
  466. asm_x86_add_i32_to_r32(as, (n_args + align) * WORD_SIZE, ASM_X86_REG_ESP);
  467. }
  468. }
  469. #endif // MICROPY_EMIT_X86