| 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 | #ifndef MICROPY_INCLUDED_PY_ASMX86_H |
| 27 | #define MICROPY_INCLUDED_PY_ASMX86_H |
| 28 | |
| 29 | #include "py/mpconfig.h" |
| 30 | #include "py/misc.h" |
| 31 | #include "py/asmbase.h" |
| 32 | |
| 33 | // x86 cdecl calling convention is: |
| 34 | // - args passed on the stack in reverse order |
| 35 | // - return value in EAX |
| 36 | // - caller cleans up the stack after a call |
| 37 | // - stack must be aligned to 16-byte boundary before all calls |
| 38 | // - EAX, ECX, EDX are caller-save |
| 39 | // - EBX, ESI, EDI, EBP, ESP, EIP are callee-save |
| 40 | |
| 41 | // In the functions below, argument order follows x86 docs and generally |
| 42 | // the destination is the first argument. |
| 43 | // NOTE: this is a change from the old convention used in this file and |
| 44 | // some functions still use the old (reverse) convention. |
| 45 | |
| 46 | #define ASM_X86_REG_EAX (0) |
| 47 | #define ASM_X86_REG_ECX (1) |
| 48 | #define ASM_X86_REG_EDX (2) |
| 49 | #define ASM_X86_REG_EBX (3) |
| 50 | #define ASM_X86_REG_ESP (4) |
| 51 | #define ASM_X86_REG_EBP (5) |
| 52 | #define ASM_X86_REG_ESI (6) |
| 53 | #define ASM_X86_REG_EDI (7) |
| 54 | |
| 55 | // x86 passes values on the stack, but the emitter is register based, so we need |
| 56 | // to define registers that can temporarily hold the function arguments. They |
| 57 | // need to be defined here so that asm_x86_call_ind can push them onto the stack |
| 58 | // before the call. |
| 59 | #define ASM_X86_REG_ARG_1 ASM_X86_REG_EAX |
| 60 | #define ASM_X86_REG_ARG_2 ASM_X86_REG_ECX |
| 61 | #define ASM_X86_REG_ARG_3 ASM_X86_REG_EDX |
| 62 | #define ASM_X86_REG_ARG_4 ASM_X86_REG_EBX |
| 63 | |
| 64 | // condition codes, used for jcc and setcc (despite their j-name!) |
| 65 | #define ASM_X86_CC_JB (0x2) // below, unsigned |
| 66 | #define ASM_X86_CC_JAE (0x3) // above or equal, unsigned |
| 67 | #define ASM_X86_CC_JZ (0x4) |
| 68 | #define ASM_X86_CC_JE (0x4) |
| 69 | #define ASM_X86_CC_JNZ (0x5) |
| 70 | #define ASM_X86_CC_JNE (0x5) |
| 71 | #define ASM_X86_CC_JBE (0x6) // below or equal, unsigned |
| 72 | #define ASM_X86_CC_JA (0x7) // above, unsigned |
| 73 | #define ASM_X86_CC_JL (0xc) // less, signed |
| 74 | #define ASM_X86_CC_JGE (0xd) // greater or equal, signed |
| 75 | #define ASM_X86_CC_JLE (0xe) // less or equal, signed |
| 76 | #define ASM_X86_CC_JG (0xf) // greater, signed |
| 77 | |
| 78 | typedef struct _asm_x86_t { |
| 79 | mp_asm_base_t base; |
| 80 | int num_locals; |
| 81 | } asm_x86_t; |
| 82 | |
| 83 | static inline void asm_x86_end_pass(asm_x86_t *as) { |
| 84 | (void)as; |
| 85 | } |
| 86 | |
| 87 | void asm_x86_mov_r32_r32(asm_x86_t *as, int dest_r32, int src_r32); |
| 88 | size_t asm_x86_mov_i32_to_r32(asm_x86_t *as, int32_t src_i32, int dest_r32); |
| 89 | void asm_x86_mov_r8_to_mem8(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp); |
| 90 | void asm_x86_mov_r16_to_mem16(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp); |
| 91 | void asm_x86_mov_r32_to_mem32(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp); |
| 92 | void asm_x86_mov_mem8_to_r32zx(asm_x86_t *as, int src_r32, int src_disp, int dest_r32); |
| 93 | void asm_x86_mov_mem16_to_r32zx(asm_x86_t *as, int src_r32, int src_disp, int dest_r32); |
| 94 | void asm_x86_mov_mem32_to_r32(asm_x86_t *as, int src_r32, int src_disp, int dest_r32); |
| 95 | void asm_x86_and_r32_r32(asm_x86_t *as, int dest_r32, int src_r32); |
| 96 | void asm_x86_or_r32_r32(asm_x86_t *as, int dest_r32, int src_r32); |
| 97 | void asm_x86_xor_r32_r32(asm_x86_t *as, int dest_r32, int src_r32); |
| 98 | void asm_x86_shl_r32_cl(asm_x86_t *as, int dest_r32); |
| 99 | void asm_x86_shr_r32_cl(asm_x86_t *as, int dest_r32); |
| 100 | void asm_x86_sar_r32_cl(asm_x86_t *as, int dest_r32); |
| 101 | void asm_x86_add_r32_r32(asm_x86_t *as, int dest_r32, int src_r32); |
| 102 | void asm_x86_sub_r32_r32(asm_x86_t *as, int dest_r32, int src_r32); |
| 103 | void asm_x86_mul_r32_r32(asm_x86_t *as, int dest_r32, int src_r32); |
| 104 | void asm_x86_cmp_r32_with_r32(asm_x86_t *as, int src_r32_a, int src_r32_b); |
| 105 | void asm_x86_test_r8_with_r8(asm_x86_t *as, int src_r32_a, int src_r32_b); |
| 106 | void asm_x86_test_r32_with_r32(asm_x86_t *as, int src_r32_a, int src_r32_b); |
| 107 | void asm_x86_setcc_r8(asm_x86_t *as, mp_uint_t jcc_type, int dest_r8); |
| 108 | void asm_x86_jmp_reg(asm_x86_t *as, int src_r86); |
| 109 | void asm_x86_jmp_label(asm_x86_t *as, mp_uint_t label); |
| 110 | void asm_x86_jcc_label(asm_x86_t *as, mp_uint_t jcc_type, mp_uint_t label); |
| 111 | void asm_x86_entry(asm_x86_t *as, int num_locals); |
| 112 | void asm_x86_exit(asm_x86_t *as); |
| 113 | void asm_x86_mov_arg_to_r32(asm_x86_t *as, int src_arg_num, int dest_r32); |
| 114 | void asm_x86_mov_local_to_r32(asm_x86_t *as, int src_local_num, int dest_r32); |
| 115 | void asm_x86_mov_r32_to_local(asm_x86_t *as, int src_r32, int dest_local_num); |
| 116 | void asm_x86_mov_local_addr_to_r32(asm_x86_t *as, int local_num, int dest_r32); |
| 117 | void asm_x86_mov_reg_pcrel(asm_x86_t *as, int dest_r64, mp_uint_t label); |
| 118 | void asm_x86_call_ind(asm_x86_t *as, size_t fun_id, mp_uint_t n_args, int temp_r32); |
| 119 | |
| 120 | // Holds a pointer to mp_fun_table |
| 121 | #define ASM_X86_REG_FUN_TABLE ASM_X86_REG_EBP |
| 122 | |
| 123 | #if GENERIC_ASM_API |
| 124 | |
| 125 | // The following macros provide a (mostly) arch-independent API to |
| 126 | // generate native code, and are used by the native emitter. |
| 127 | |
| 128 | #define ASM_WORD_SIZE (4) |
| 129 | |
| 130 | #define REG_RET ASM_X86_REG_EAX |
| 131 | #define REG_ARG_1 ASM_X86_REG_ARG_1 |
| 132 | #define REG_ARG_2 ASM_X86_REG_ARG_2 |
| 133 | #define REG_ARG_3 ASM_X86_REG_ARG_3 |
| 134 | #define REG_ARG_4 ASM_X86_REG_ARG_4 |
| 135 | |
| 136 | // caller-save, so can be used as temporaries |
| 137 | #define REG_TEMP0 ASM_X86_REG_EAX |
| 138 | #define REG_TEMP1 ASM_X86_REG_ECX |
| 139 | #define REG_TEMP2 ASM_X86_REG_EDX |
| 140 | |
| 141 | // callee-save, so can be used as locals |
| 142 | #define REG_LOCAL_1 ASM_X86_REG_EBX |
| 143 | #define REG_LOCAL_2 ASM_X86_REG_ESI |
| 144 | #define REG_LOCAL_3 ASM_X86_REG_EDI |
| 145 | #define REG_LOCAL_NUM (3) |
| 146 | |
| 147 | // Holds a pointer to mp_fun_table |
| 148 | #define REG_FUN_TABLE ASM_X86_REG_FUN_TABLE |
| 149 | |
| 150 | #define ASM_T asm_x86_t |
| 151 | #define ASM_END_PASS asm_x86_end_pass |
| 152 | #define ASM_ENTRY asm_x86_entry |
| 153 | #define ASM_EXIT asm_x86_exit |
| 154 | |
| 155 | #define ASM_JUMP asm_x86_jmp_label |
| 156 | #define ASM_JUMP_IF_REG_ZERO(as, reg, label, bool_test) \ |
| 157 | do { \ |
| 158 | if (bool_test) { \ |
| 159 | asm_x86_test_r8_with_r8(as, reg, reg); \ |
| 160 | } else { \ |
| 161 | asm_x86_test_r32_with_r32(as, reg, reg); \ |
| 162 | } \ |
| 163 | asm_x86_jcc_label(as, ASM_X86_CC_JZ, label); \ |
| 164 | } while (0) |
| 165 | #define ASM_JUMP_IF_REG_NONZERO(as, reg, label, bool_test) \ |
| 166 | do { \ |
| 167 | if (bool_test) { \ |
| 168 | asm_x86_test_r8_with_r8(as, reg, reg); \ |
| 169 | } else { \ |
| 170 | asm_x86_test_r32_with_r32(as, reg, reg); \ |
| 171 | } \ |
| 172 | asm_x86_jcc_label(as, ASM_X86_CC_JNZ, label); \ |
| 173 | } while (0) |
| 174 | #define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \ |
| 175 | do { \ |
| 176 | asm_x86_cmp_r32_with_r32(as, reg1, reg2); \ |
| 177 | asm_x86_jcc_label(as, ASM_X86_CC_JE, label); \ |
| 178 | } while (0) |
| 179 | #define ASM_JUMP_REG(as, reg) asm_x86_jmp_reg((as), (reg)) |
| 180 | #define ASM_CALL_IND(as, idx) asm_x86_call_ind(as, idx, mp_f_n_args[idx], ASM_X86_REG_EAX) |
| 181 | |
| 182 | #define ASM_MOV_LOCAL_REG(as, local_num, reg_src) asm_x86_mov_r32_to_local((as), (reg_src), (local_num)) |
| 183 | #define ASM_MOV_REG_IMM(as, reg_dest, imm) asm_x86_mov_i32_to_r32((as), (imm), (reg_dest)) |
| 184 | #define ASM_MOV_REG_IMM_FIX_U16(as, reg_dest, imm) asm_x86_mov_i32_to_r32((as), (imm), (reg_dest)) |
| 185 | #define ASM_MOV_REG_IMM_FIX_WORD(as, reg_dest, imm) asm_x86_mov_i32_to_r32((as), (imm), (reg_dest)) |
| 186 | #define ASM_MOV_REG_LOCAL(as, reg_dest, local_num) asm_x86_mov_local_to_r32((as), (local_num), (reg_dest)) |
| 187 | #define ASM_MOV_REG_REG(as, reg_dest, reg_src) asm_x86_mov_r32_r32((as), (reg_dest), (reg_src)) |
| 188 | #define ASM_MOV_REG_LOCAL_ADDR(as, reg_dest, local_num) asm_x86_mov_local_addr_to_r32((as), (local_num), (reg_dest)) |
| 189 | #define ASM_MOV_REG_PCREL(as, reg_dest, label) asm_x86_mov_reg_pcrel((as), (reg_dest), (label)) |
| 190 | |
| 191 | #define ASM_LSL_REG(as, reg) asm_x86_shl_r32_cl((as), (reg)) |
| 192 | #define ASM_LSR_REG(as, reg) asm_x86_shr_r32_cl((as), (reg)) |
| 193 | #define ASM_ASR_REG(as, reg) asm_x86_sar_r32_cl((as), (reg)) |
| 194 | #define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x86_or_r32_r32((as), (reg_dest), (reg_src)) |
| 195 | #define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x86_xor_r32_r32((as), (reg_dest), (reg_src)) |
| 196 | #define ASM_AND_REG_REG(as, reg_dest, reg_src) asm_x86_and_r32_r32((as), (reg_dest), (reg_src)) |
| 197 | #define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x86_add_r32_r32((as), (reg_dest), (reg_src)) |
| 198 | #define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x86_sub_r32_r32((as), (reg_dest), (reg_src)) |
| 199 | #define ASM_MUL_REG_REG(as, reg_dest, reg_src) asm_x86_mul_r32_r32((as), (reg_dest), (reg_src)) |
| 200 | |
| 201 | #define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem32_to_r32((as), (reg_base), 0, (reg_dest)) |
| 202 | #define ASM_LOAD_REG_REG_OFFSET(as, reg_dest, reg_base, word_offset) asm_x86_mov_mem32_to_r32((as), (reg_base), 4 * (word_offset), (reg_dest)) |
| 203 | #define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem8_to_r32zx((as), (reg_base), 0, (reg_dest)) |
| 204 | #define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem16_to_r32zx((as), (reg_base), 0, (reg_dest)) |
| 205 | #define ASM_LOAD32_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem32_to_r32((as), (reg_base), 0, (reg_dest)) |
| 206 | |
| 207 | #define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_x86_mov_r32_to_mem32((as), (reg_src), (reg_base), 0) |
| 208 | #define ASM_STORE_REG_REG_OFFSET(as, reg_src, reg_base, word_offset) asm_x86_mov_r32_to_mem32((as), (reg_src), (reg_base), 4 * (word_offset)) |
| 209 | #define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x86_mov_r8_to_mem8((as), (reg_src), (reg_base), 0) |
| 210 | #define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_x86_mov_r16_to_mem16((as), (reg_src), (reg_base), 0) |
| 211 | #define ASM_STORE32_REG_REG(as, reg_src, reg_base) asm_x86_mov_r32_to_mem32((as), (reg_src), (reg_base), 0) |
| 212 | |
| 213 | #endif // GENERIC_ASM_API |
| 214 | |
| 215 | #endif // MICROPY_INCLUDED_PY_ASMX86_H |
| 216 | |