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