asmx86.c 18 KB

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