asmx86.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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_AND_R32_TO_RM32 (0x21) /* /r */
  52. #define OPCODE_OR_R32_TO_RM32 (0x09) /* /r */
  53. #define OPCODE_XOR_R32_TO_RM32 (0x31) /* /r */
  54. #define OPCODE_ADD_R32_TO_RM32 (0x01)
  55. #define OPCODE_ADD_I32_TO_RM32 (0x81) /* /0 */
  56. #define OPCODE_ADD_I8_TO_RM32 (0x83) /* /0 */
  57. #define OPCODE_SUB_R32_FROM_RM32 (0x29)
  58. #define OPCODE_SUB_I32_FROM_RM32 (0x81) /* /5 */
  59. #define OPCODE_SUB_I8_FROM_RM32 (0x83) /* /5 */
  60. // #define OPCODE_SHL_RM32_BY_I8 (0xc1) /* /4 */
  61. // #define OPCODE_SHR_RM32_BY_I8 (0xc1) /* /5 */
  62. // #define OPCODE_SAR_RM32_BY_I8 (0xc1) /* /7 */
  63. #define OPCODE_SHL_RM32_CL (0xd3) /* /4 */
  64. #define OPCODE_SHR_RM32_CL (0xd3) /* /5 */
  65. #define OPCODE_SAR_RM32_CL (0xd3) /* /7 */
  66. // #define OPCODE_CMP_I32_WITH_RM32 (0x81) /* /7 */
  67. // #define OPCODE_CMP_I8_WITH_RM32 (0x83) /* /7 */
  68. #define OPCODE_CMP_R32_WITH_RM32 (0x39)
  69. // #define OPCODE_CMP_RM32_WITH_R32 (0x3b)
  70. #define OPCODE_TEST_R8_WITH_RM8 (0x84) /* /r */
  71. #define OPCODE_TEST_R32_WITH_RM32 (0x85) /* /r */
  72. #define OPCODE_JMP_REL8 (0xeb)
  73. #define OPCODE_JMP_REL32 (0xe9)
  74. #define OPCODE_JMP_RM32 (0xff) /* /4 */
  75. #define OPCODE_JCC_REL8 (0x70) /* | jcc type */
  76. #define OPCODE_JCC_REL32_A (0x0f)
  77. #define OPCODE_JCC_REL32_B (0x80) /* | jcc type */
  78. #define OPCODE_SETCC_RM8_A (0x0f)
  79. #define OPCODE_SETCC_RM8_B (0x90) /* | jcc type, /0 */
  80. #define OPCODE_CALL_REL32 (0xe8)
  81. #define OPCODE_CALL_RM32 (0xff) /* /2 */
  82. #define OPCODE_LEAVE (0xc9)
  83. #define MODRM_R32(x) ((x) << 3)
  84. #define MODRM_RM_DISP0 (0x00)
  85. #define MODRM_RM_DISP8 (0x40)
  86. #define MODRM_RM_DISP32 (0x80)
  87. #define MODRM_RM_REG (0xc0)
  88. #define MODRM_RM_R32(x) (x)
  89. #define OP_SIZE_PREFIX (0x66)
  90. #define IMM32_L0(x) ((x) & 0xff)
  91. #define IMM32_L1(x) (((x) >> 8) & 0xff)
  92. #define IMM32_L2(x) (((x) >> 16) & 0xff)
  93. #define IMM32_L3(x) (((x) >> 24) & 0xff)
  94. #define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
  95. STATIC void asm_x86_write_byte_1(asm_x86_t *as, byte b1) {
  96. byte *c = mp_asm_base_get_cur_to_write_bytes(&as->base, 1);
  97. if (c != NULL) {
  98. c[0] = b1;
  99. }
  100. }
  101. STATIC void asm_x86_write_byte_2(asm_x86_t *as, byte b1, byte b2) {
  102. byte *c = mp_asm_base_get_cur_to_write_bytes(&as->base, 2);
  103. if (c != NULL) {
  104. c[0] = b1;
  105. c[1] = b2;
  106. }
  107. }
  108. STATIC void asm_x86_write_byte_3(asm_x86_t *as, byte b1, byte b2, byte b3) {
  109. byte *c = mp_asm_base_get_cur_to_write_bytes(&as->base, 3);
  110. if (c != NULL) {
  111. c[0] = b1;
  112. c[1] = b2;
  113. c[2] = b3;
  114. }
  115. }
  116. STATIC void asm_x86_write_word32(asm_x86_t *as, int w32) {
  117. byte *c = mp_asm_base_get_cur_to_write_bytes(&as->base, 4);
  118. if (c != NULL) {
  119. c[0] = IMM32_L0(w32);
  120. c[1] = IMM32_L1(w32);
  121. c[2] = IMM32_L2(w32);
  122. c[3] = IMM32_L3(w32);
  123. }
  124. }
  125. STATIC void asm_x86_write_r32_disp(asm_x86_t *as, int r32, int disp_r32, int disp_offset) {
  126. uint8_t rm_disp;
  127. if (disp_offset == 0 && disp_r32 != ASM_X86_REG_EBP) {
  128. rm_disp = MODRM_RM_DISP0;
  129. } else if (SIGNED_FIT8(disp_offset)) {
  130. rm_disp = MODRM_RM_DISP8;
  131. } else {
  132. rm_disp = MODRM_RM_DISP32;
  133. }
  134. asm_x86_write_byte_1(as, MODRM_R32(r32) | rm_disp | MODRM_RM_R32(disp_r32));
  135. if (disp_r32 == ASM_X86_REG_ESP) {
  136. // Special case for esp, it needs a SIB byte
  137. asm_x86_write_byte_1(as, 0x24);
  138. }
  139. if (rm_disp == MODRM_RM_DISP8) {
  140. asm_x86_write_byte_1(as, IMM32_L0(disp_offset));
  141. } else if (rm_disp == MODRM_RM_DISP32) {
  142. asm_x86_write_word32(as, disp_offset);
  143. }
  144. }
  145. STATIC void asm_x86_generic_r32_r32(asm_x86_t *as, int dest_r32, int src_r32, int op) {
  146. asm_x86_write_byte_2(as, op, MODRM_R32(src_r32) | MODRM_RM_REG | MODRM_RM_R32(dest_r32));
  147. }
  148. #if 0
  149. STATIC void asm_x86_nop(asm_x86_t *as) {
  150. asm_x86_write_byte_1(as, OPCODE_NOP);
  151. }
  152. #endif
  153. STATIC void asm_x86_push_r32(asm_x86_t *as, int src_r32) {
  154. asm_x86_write_byte_1(as, OPCODE_PUSH_R32 | src_r32);
  155. }
  156. #if 0
  157. void asm_x86_push_i32(asm_x86_t *as, int src_i32) {
  158. asm_x86_write_byte_1(as, OPCODE_PUSH_I32);
  159. asm_x86_write_word32(as, src_i32);
  160. }
  161. void asm_x86_push_disp(asm_x86_t *as, int src_r32, int src_offset) {
  162. asm_x86_write_byte_1(as, OPCODE_PUSH_M32);
  163. asm_x86_write_r32_disp(as, 6, src_r32, src_offset);
  164. }
  165. #endif
  166. STATIC void asm_x86_pop_r32(asm_x86_t *as, int dest_r32) {
  167. asm_x86_write_byte_1(as, OPCODE_POP_R32 | dest_r32);
  168. }
  169. STATIC void asm_x86_ret(asm_x86_t *as) {
  170. asm_x86_write_byte_1(as, OPCODE_RET);
  171. }
  172. void asm_x86_mov_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
  173. asm_x86_generic_r32_r32(as, dest_r32, src_r32, OPCODE_MOV_R32_TO_RM32);
  174. }
  175. void asm_x86_mov_r8_to_mem8(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp) {
  176. asm_x86_write_byte_1(as, OPCODE_MOV_R8_TO_RM8);
  177. asm_x86_write_r32_disp(as, src_r32, dest_r32, dest_disp);
  178. }
  179. void asm_x86_mov_r16_to_mem16(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp) {
  180. asm_x86_write_byte_2(as, OP_SIZE_PREFIX, OPCODE_MOV_R32_TO_RM32);
  181. asm_x86_write_r32_disp(as, src_r32, dest_r32, dest_disp);
  182. }
  183. void asm_x86_mov_r32_to_mem32(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp) {
  184. asm_x86_write_byte_1(as, OPCODE_MOV_R32_TO_RM32);
  185. asm_x86_write_r32_disp(as, src_r32, dest_r32, dest_disp);
  186. }
  187. void asm_x86_mov_mem8_to_r32zx(asm_x86_t *as, int src_r32, int src_disp, int dest_r32) {
  188. asm_x86_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM8_TO_R32);
  189. asm_x86_write_r32_disp(as, dest_r32, src_r32, src_disp);
  190. }
  191. void asm_x86_mov_mem16_to_r32zx(asm_x86_t *as, int src_r32, int src_disp, int dest_r32) {
  192. asm_x86_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM16_TO_R32);
  193. asm_x86_write_r32_disp(as, dest_r32, src_r32, src_disp);
  194. }
  195. void asm_x86_mov_mem32_to_r32(asm_x86_t *as, int src_r32, int src_disp, int dest_r32) {
  196. asm_x86_write_byte_1(as, OPCODE_MOV_RM32_TO_R32);
  197. asm_x86_write_r32_disp(as, dest_r32, src_r32, src_disp);
  198. }
  199. STATIC void asm_x86_lea_disp_to_r32(asm_x86_t *as, int src_r32, int src_disp, int dest_r32) {
  200. asm_x86_write_byte_1(as, OPCODE_LEA_MEM_TO_R32);
  201. asm_x86_write_r32_disp(as, dest_r32, src_r32, src_disp);
  202. }
  203. #if 0
  204. void asm_x86_mov_i8_to_r8(asm_x86_t *as, int src_i8, int dest_r32) {
  205. asm_x86_write_byte_2(as, OPCODE_MOV_I8_TO_R8 | dest_r32, src_i8);
  206. }
  207. #endif
  208. size_t asm_x86_mov_i32_to_r32(asm_x86_t *as, int32_t src_i32, int dest_r32) {
  209. asm_x86_write_byte_1(as, OPCODE_MOV_I32_TO_R32 | dest_r32);
  210. size_t loc = mp_asm_base_get_code_pos(&as->base);
  211. asm_x86_write_word32(as, src_i32);
  212. return loc;
  213. }
  214. void asm_x86_and_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
  215. asm_x86_generic_r32_r32(as, dest_r32, src_r32, OPCODE_AND_R32_TO_RM32);
  216. }
  217. void asm_x86_or_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
  218. asm_x86_generic_r32_r32(as, dest_r32, src_r32, OPCODE_OR_R32_TO_RM32);
  219. }
  220. void asm_x86_xor_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
  221. asm_x86_generic_r32_r32(as, dest_r32, src_r32, OPCODE_XOR_R32_TO_RM32);
  222. }
  223. void asm_x86_shl_r32_cl(asm_x86_t *as, int dest_r32) {
  224. asm_x86_generic_r32_r32(as, dest_r32, 4, OPCODE_SHL_RM32_CL);
  225. }
  226. void asm_x86_shr_r32_cl(asm_x86_t *as, int dest_r32) {
  227. asm_x86_generic_r32_r32(as, dest_r32, 5, OPCODE_SHR_RM32_CL);
  228. }
  229. void asm_x86_sar_r32_cl(asm_x86_t *as, int dest_r32) {
  230. asm_x86_generic_r32_r32(as, dest_r32, 7, OPCODE_SAR_RM32_CL);
  231. }
  232. void asm_x86_add_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
  233. asm_x86_generic_r32_r32(as, dest_r32, src_r32, OPCODE_ADD_R32_TO_RM32);
  234. }
  235. STATIC void asm_x86_add_i32_to_r32(asm_x86_t *as, int src_i32, int dest_r32) {
  236. if (SIGNED_FIT8(src_i32)) {
  237. asm_x86_write_byte_2(as, OPCODE_ADD_I8_TO_RM32, MODRM_R32(0) | MODRM_RM_REG | MODRM_RM_R32(dest_r32));
  238. asm_x86_write_byte_1(as, src_i32 & 0xff);
  239. } else {
  240. asm_x86_write_byte_2(as, OPCODE_ADD_I32_TO_RM32, MODRM_R32(0) | MODRM_RM_REG | MODRM_RM_R32(dest_r32));
  241. asm_x86_write_word32(as, src_i32);
  242. }
  243. }
  244. void asm_x86_sub_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
  245. asm_x86_generic_r32_r32(as, dest_r32, src_r32, OPCODE_SUB_R32_FROM_RM32);
  246. }
  247. STATIC void asm_x86_sub_r32_i32(asm_x86_t *as, int dest_r32, int src_i32) {
  248. if (SIGNED_FIT8(src_i32)) {
  249. // defaults to 32 bit operation
  250. asm_x86_write_byte_2(as, OPCODE_SUB_I8_FROM_RM32, MODRM_R32(5) | MODRM_RM_REG | MODRM_RM_R32(dest_r32));
  251. asm_x86_write_byte_1(as, src_i32 & 0xff);
  252. } else {
  253. // defaults to 32 bit operation
  254. asm_x86_write_byte_2(as, OPCODE_SUB_I32_FROM_RM32, MODRM_R32(5) | MODRM_RM_REG | MODRM_RM_R32(dest_r32));
  255. asm_x86_write_word32(as, src_i32);
  256. }
  257. }
  258. void asm_x86_mul_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
  259. // imul reg32, reg/mem32 -- 0x0f 0xaf /r
  260. asm_x86_write_byte_3(as, 0x0f, 0xaf, MODRM_R32(dest_r32) | MODRM_RM_REG | MODRM_RM_R32(src_r32));
  261. }
  262. #if 0
  263. /* shifts not tested */
  264. void asm_x86_shl_r32_by_imm(asm_x86_t *as, int r32, int imm) {
  265. asm_x86_write_byte_2(as, OPCODE_SHL_RM32_BY_I8, MODRM_R32(4) | MODRM_RM_REG | MODRM_RM_R32(r32));
  266. asm_x86_write_byte_1(as, imm);
  267. }
  268. void asm_x86_shr_r32_by_imm(asm_x86_t *as, int r32, int imm) {
  269. asm_x86_write_byte_2(as, OPCODE_SHR_RM32_BY_I8, MODRM_R32(5) | MODRM_RM_REG | MODRM_RM_R32(r32));
  270. asm_x86_write_byte_1(as, imm);
  271. }
  272. void asm_x86_sar_r32_by_imm(asm_x86_t *as, int r32, int imm) {
  273. asm_x86_write_byte_2(as, OPCODE_SAR_RM32_BY_I8, MODRM_R32(7) | MODRM_RM_REG | MODRM_RM_R32(r32));
  274. asm_x86_write_byte_1(as, imm);
  275. }
  276. #endif
  277. void asm_x86_cmp_r32_with_r32(asm_x86_t *as, int src_r32_a, int src_r32_b) {
  278. asm_x86_generic_r32_r32(as, src_r32_b, src_r32_a, OPCODE_CMP_R32_WITH_RM32);
  279. }
  280. #if 0
  281. void asm_x86_cmp_i32_with_r32(asm_x86_t *as, int src_i32, int src_r32) {
  282. if (SIGNED_FIT8(src_i32)) {
  283. asm_x86_write_byte_2(as, OPCODE_CMP_I8_WITH_RM32, MODRM_R32(7) | MODRM_RM_REG | MODRM_RM_R32(src_r32));
  284. asm_x86_write_byte_1(as, src_i32 & 0xff);
  285. } else {
  286. asm_x86_write_byte_2(as, OPCODE_CMP_I32_WITH_RM32, MODRM_R32(7) | MODRM_RM_REG | MODRM_RM_R32(src_r32));
  287. asm_x86_write_word32(as, src_i32);
  288. }
  289. }
  290. #endif
  291. void asm_x86_test_r8_with_r8(asm_x86_t *as, int src_r32_a, int src_r32_b) {
  292. 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));
  293. }
  294. void asm_x86_test_r32_with_r32(asm_x86_t *as, int src_r32_a, int src_r32_b) {
  295. asm_x86_generic_r32_r32(as, src_r32_b, src_r32_a, OPCODE_TEST_R32_WITH_RM32);
  296. }
  297. void asm_x86_setcc_r8(asm_x86_t *as, mp_uint_t jcc_type, int dest_r8) {
  298. 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));
  299. }
  300. void asm_x86_jmp_reg(asm_x86_t *as, int src_r32) {
  301. asm_x86_write_byte_2(as, OPCODE_JMP_RM32, MODRM_R32(4) | MODRM_RM_REG | MODRM_RM_R32(src_r32));
  302. }
  303. STATIC mp_uint_t get_label_dest(asm_x86_t *as, mp_uint_t label) {
  304. assert(label < as->base.max_num_labels);
  305. return as->base.label_offsets[label];
  306. }
  307. void asm_x86_jmp_label(asm_x86_t *as, mp_uint_t label) {
  308. mp_uint_t dest = get_label_dest(as, label);
  309. mp_int_t rel = dest - as->base.code_offset;
  310. if (dest != (mp_uint_t)-1 && rel < 0) {
  311. // is a backwards jump, so we know the size of the jump on the first pass
  312. // calculate rel assuming 8 bit relative jump
  313. rel -= 2;
  314. if (SIGNED_FIT8(rel)) {
  315. asm_x86_write_byte_2(as, OPCODE_JMP_REL8, rel & 0xff);
  316. } else {
  317. rel += 2;
  318. goto large_jump;
  319. }
  320. } else {
  321. // is a forwards jump, so need to assume it's large
  322. large_jump:
  323. rel -= 5;
  324. asm_x86_write_byte_1(as, OPCODE_JMP_REL32);
  325. asm_x86_write_word32(as, rel);
  326. }
  327. }
  328. void asm_x86_jcc_label(asm_x86_t *as, mp_uint_t jcc_type, mp_uint_t label) {
  329. mp_uint_t dest = get_label_dest(as, label);
  330. mp_int_t rel = dest - as->base.code_offset;
  331. if (dest != (mp_uint_t)-1 && rel < 0) {
  332. // is a backwards jump, so we know the size of the jump on the first pass
  333. // calculate rel assuming 8 bit relative jump
  334. rel -= 2;
  335. if (SIGNED_FIT8(rel)) {
  336. asm_x86_write_byte_2(as, OPCODE_JCC_REL8 | jcc_type, rel & 0xff);
  337. } else {
  338. rel += 2;
  339. goto large_jump;
  340. }
  341. } else {
  342. // is a forwards jump, so need to assume it's large
  343. large_jump:
  344. rel -= 6;
  345. asm_x86_write_byte_2(as, OPCODE_JCC_REL32_A, OPCODE_JCC_REL32_B | jcc_type);
  346. asm_x86_write_word32(as, rel);
  347. }
  348. }
  349. void asm_x86_entry(asm_x86_t *as, int num_locals) {
  350. assert(num_locals >= 0);
  351. asm_x86_push_r32(as, ASM_X86_REG_EBP);
  352. asm_x86_push_r32(as, ASM_X86_REG_EBX);
  353. asm_x86_push_r32(as, ASM_X86_REG_ESI);
  354. asm_x86_push_r32(as, ASM_X86_REG_EDI);
  355. num_locals |= 3; // make it odd so stack is aligned on 16 byte boundary
  356. asm_x86_sub_r32_i32(as, ASM_X86_REG_ESP, num_locals * WORD_SIZE);
  357. as->num_locals = num_locals;
  358. }
  359. void asm_x86_exit(asm_x86_t *as) {
  360. asm_x86_sub_r32_i32(as, ASM_X86_REG_ESP, -as->num_locals * WORD_SIZE);
  361. asm_x86_pop_r32(as, ASM_X86_REG_EDI);
  362. asm_x86_pop_r32(as, ASM_X86_REG_ESI);
  363. asm_x86_pop_r32(as, ASM_X86_REG_EBX);
  364. asm_x86_pop_r32(as, ASM_X86_REG_EBP);
  365. asm_x86_ret(as);
  366. }
  367. STATIC int asm_x86_arg_offset_from_esp(asm_x86_t *as, size_t arg_num) {
  368. // Above esp are: locals, 4 saved registers, return eip, arguments
  369. return (as->num_locals + 4 + 1 + arg_num) * WORD_SIZE;
  370. }
  371. #if 0
  372. void asm_x86_push_arg(asm_x86_t *as, int src_arg_num) {
  373. asm_x86_push_disp(as, ASM_X86_REG_ESP, asm_x86_arg_offset_from_esp(as, src_arg_num));
  374. }
  375. #endif
  376. void asm_x86_mov_arg_to_r32(asm_x86_t *as, int src_arg_num, int dest_r32) {
  377. asm_x86_mov_mem32_to_r32(as, ASM_X86_REG_ESP, asm_x86_arg_offset_from_esp(as, src_arg_num), dest_r32);
  378. }
  379. #if 0
  380. void asm_x86_mov_r32_to_arg(asm_x86_t *as, int src_r32, int dest_arg_num) {
  381. asm_x86_mov_r32_to_mem32(as, src_r32, ASM_X86_REG_ESP, asm_x86_arg_offset_from_esp(as, dest_arg_num));
  382. }
  383. #endif
  384. // locals:
  385. // - stored on the stack in ascending order
  386. // - numbered 0 through as->num_locals-1
  387. // - ESP points to the first local
  388. //
  389. // | ESP
  390. // v
  391. // l0 l1 l2 ... l(n-1)
  392. // ^ ^
  393. // | low address | high address in RAM
  394. //
  395. STATIC int asm_x86_local_offset_from_esp(asm_x86_t *as, int local_num) {
  396. (void)as;
  397. // Stack is full descending, ESP points to local0
  398. return local_num * WORD_SIZE;
  399. }
  400. void asm_x86_mov_local_to_r32(asm_x86_t *as, int src_local_num, int dest_r32) {
  401. asm_x86_mov_mem32_to_r32(as, ASM_X86_REG_ESP, asm_x86_local_offset_from_esp(as, src_local_num), dest_r32);
  402. }
  403. void asm_x86_mov_r32_to_local(asm_x86_t *as, int src_r32, int dest_local_num) {
  404. asm_x86_mov_r32_to_mem32(as, src_r32, ASM_X86_REG_ESP, asm_x86_local_offset_from_esp(as, dest_local_num));
  405. }
  406. void asm_x86_mov_local_addr_to_r32(asm_x86_t *as, int local_num, int dest_r32) {
  407. int offset = asm_x86_local_offset_from_esp(as, local_num);
  408. if (offset == 0) {
  409. asm_x86_mov_r32_r32(as, dest_r32, ASM_X86_REG_ESP);
  410. } else {
  411. asm_x86_lea_disp_to_r32(as, ASM_X86_REG_ESP, offset, dest_r32);
  412. }
  413. }
  414. void asm_x86_mov_reg_pcrel(asm_x86_t *as, int dest_r32, mp_uint_t label) {
  415. asm_x86_write_byte_1(as, OPCODE_CALL_REL32);
  416. asm_x86_write_word32(as, 0);
  417. mp_uint_t dest = get_label_dest(as, label);
  418. mp_int_t rel = dest - as->base.code_offset;
  419. asm_x86_pop_r32(as, dest_r32);
  420. // PC rel is usually a forward reference, so need to assume it's large
  421. asm_x86_write_byte_2(as, OPCODE_ADD_I32_TO_RM32, MODRM_R32(0) | MODRM_RM_REG | MODRM_RM_R32(dest_r32));
  422. asm_x86_write_word32(as, rel);
  423. }
  424. #if 0
  425. void asm_x86_push_local(asm_x86_t *as, int local_num) {
  426. asm_x86_push_disp(as, ASM_X86_REG_ESP, asm_x86_local_offset_from_esp(as, local_num));
  427. }
  428. void asm_x86_push_local_addr(asm_x86_t *as, int local_num, int temp_r32) {
  429. asm_x86_mov_r32_r32(as, temp_r32, ASM_X86_REG_ESP);
  430. asm_x86_add_i32_to_r32(as, asm_x86_local_offset_from_esp(as, local_num), temp_r32);
  431. asm_x86_push_r32(as, temp_r32);
  432. }
  433. #endif
  434. void asm_x86_call_ind(asm_x86_t *as, size_t fun_id, mp_uint_t n_args, int temp_r32) {
  435. assert(n_args <= 4);
  436. // Align stack on 16-byte boundary during the call
  437. unsigned int align = ((n_args + 3) & ~3) - n_args;
  438. if (align) {
  439. asm_x86_sub_r32_i32(as, ASM_X86_REG_ESP, align * WORD_SIZE);
  440. }
  441. if (n_args > 3) {
  442. asm_x86_push_r32(as, ASM_X86_REG_ARG_4);
  443. }
  444. if (n_args > 2) {
  445. asm_x86_push_r32(as, ASM_X86_REG_ARG_3);
  446. }
  447. if (n_args > 1) {
  448. asm_x86_push_r32(as, ASM_X86_REG_ARG_2);
  449. }
  450. if (n_args > 0) {
  451. asm_x86_push_r32(as, ASM_X86_REG_ARG_1);
  452. }
  453. // Load the pointer to the function and make the call
  454. asm_x86_mov_mem32_to_r32(as, ASM_X86_REG_FUN_TABLE, fun_id * WORD_SIZE, temp_r32);
  455. asm_x86_write_byte_2(as, OPCODE_CALL_RM32, MODRM_R32(2) | MODRM_RM_REG | MODRM_RM_R32(temp_r32));
  456. // the caller must clean up the stack
  457. if (n_args > 0) {
  458. asm_x86_add_i32_to_r32(as, (n_args + align) * WORD_SIZE, ASM_X86_REG_ESP);
  459. }
  460. }
  461. #endif // MICROPY_EMIT_X86