asmx86.c 19 KB

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