Port of MicroPython to the mbed platform. See micropython-repl for an interactive program.

Dependents:   micropython-repl

This a port of MicroPython to the mbed Classic platform.

This provides an interpreter running on the board's USB serial connection.

Getting Started

Import the micropython-repl program into your IDE workspace on developer.mbed.org. Compile and download to your board. Connect to the USB serial port in your usual manner. You should get a startup message similar to the following:

  MicroPython v1.7-155-gdddcdd8 on 2016-04-23; K64F with ARM
  Type "help()" for more information.
  >>>

Then you can start using micropython. For example:

  >>> from mbed import DigitalOut
  >>> from pins import LED1
  >>> led = DigitalOut(LED1)
  >>> led.write(1)

Requirements

You need approximately 100K of flash memory, so this will be no good for boards with smaller amounts of storage.

Caveats

This can be considered an alpha release of the port; things may not work; APIs may change in later releases. It is NOT an official part part the micropython project, so if anything doesn't work, blame me. If it does work, most of the credit is due to micropython.

  • Only a few of the mbed classes are available in micropython so far, and not all methods of those that are.
  • Only a few boards have their full range of pin names available; for others, only a few standard ones (USBTX, USBRX, LED1) are implemented.
  • The garbage collector is not yet implemented. The interpreter will gradually consume memory and then fail.
  • Exceptions from the mbed classes are not yet handled.
  • Asynchronous processing (e.g. events on inputs) is not supported.

Credits

  • Damien P. George and other contributors who created micropython.
  • Colin Hogben, author of this port.
Committer:
pythontech
Date:
Sat Apr 16 17:11:56 2016 +0000
Revision:
0:5868e8752d44
Split off library from repl

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pythontech 0:5868e8752d44 1 /*
pythontech 0:5868e8752d44 2 * This file is part of the Micro Python project, http://micropython.org/
pythontech 0:5868e8752d44 3 *
pythontech 0:5868e8752d44 4 * The MIT License (MIT)
pythontech 0:5868e8752d44 5 *
pythontech 0:5868e8752d44 6 * Copyright (c) 2014 Damien P. George
pythontech 0:5868e8752d44 7 *
pythontech 0:5868e8752d44 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
pythontech 0:5868e8752d44 9 * of this software and associated documentation files (the "Software"), to deal
pythontech 0:5868e8752d44 10 * in the Software without restriction, including without limitation the rights
pythontech 0:5868e8752d44 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
pythontech 0:5868e8752d44 12 * copies of the Software, and to permit persons to whom the Software is
pythontech 0:5868e8752d44 13 * furnished to do so, subject to the following conditions:
pythontech 0:5868e8752d44 14 *
pythontech 0:5868e8752d44 15 * The above copyright notice and this permission notice shall be included in
pythontech 0:5868e8752d44 16 * all copies or substantial portions of the Software.
pythontech 0:5868e8752d44 17 *
pythontech 0:5868e8752d44 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
pythontech 0:5868e8752d44 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
pythontech 0:5868e8752d44 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
pythontech 0:5868e8752d44 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
pythontech 0:5868e8752d44 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
pythontech 0:5868e8752d44 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
pythontech 0:5868e8752d44 24 * THE SOFTWARE.
pythontech 0:5868e8752d44 25 */
pythontech 0:5868e8752d44 26
pythontech 0:5868e8752d44 27 #include <stdint.h>
pythontech 0:5868e8752d44 28 #include <stdio.h>
pythontech 0:5868e8752d44 29 #include <assert.h>
pythontech 0:5868e8752d44 30 #include <string.h>
pythontech 0:5868e8752d44 31
pythontech 0:5868e8752d44 32 #include "py/mpconfig.h"
pythontech 0:5868e8752d44 33
pythontech 0:5868e8752d44 34 // wrapper around everything in this file
pythontech 0:5868e8752d44 35 #if MICROPY_EMIT_X86
pythontech 0:5868e8752d44 36
pythontech 0:5868e8752d44 37 #include "py/asmx86.h"
pythontech 0:5868e8752d44 38
pythontech 0:5868e8752d44 39 /* all offsets are measured in multiples of 4 bytes */
pythontech 0:5868e8752d44 40 #define WORD_SIZE (4)
pythontech 0:5868e8752d44 41
pythontech 0:5868e8752d44 42 #define OPCODE_NOP (0x90)
pythontech 0:5868e8752d44 43 #define OPCODE_PUSH_R32 (0x50)
pythontech 0:5868e8752d44 44 //#define OPCODE_PUSH_I32 (0x68)
pythontech 0:5868e8752d44 45 //#define OPCODE_PUSH_M32 (0xff) /* /6 */
pythontech 0:5868e8752d44 46 #define OPCODE_POP_R32 (0x58)
pythontech 0:5868e8752d44 47 #define OPCODE_RET (0xc3)
pythontech 0:5868e8752d44 48 //#define OPCODE_MOV_I8_TO_R8 (0xb0) /* +rb */
pythontech 0:5868e8752d44 49 #define OPCODE_MOV_I32_TO_R32 (0xb8)
pythontech 0:5868e8752d44 50 //#define OPCODE_MOV_I32_TO_RM32 (0xc7)
pythontech 0:5868e8752d44 51 #define OPCODE_MOV_R8_TO_RM8 (0x88) /* /r */
pythontech 0:5868e8752d44 52 #define OPCODE_MOV_R32_TO_RM32 (0x89) /* /r */
pythontech 0:5868e8752d44 53 #define OPCODE_MOV_RM32_TO_R32 (0x8b) /* /r */
pythontech 0:5868e8752d44 54 #define OPCODE_MOVZX_RM8_TO_R32 (0xb6) /* 0x0f 0xb6/r */
pythontech 0:5868e8752d44 55 #define OPCODE_MOVZX_RM16_TO_R32 (0xb7) /* 0x0f 0xb7/r */
pythontech 0:5868e8752d44 56 #define OPCODE_LEA_MEM_TO_R32 (0x8d) /* /r */
pythontech 0:5868e8752d44 57 #define OPCODE_AND_R32_TO_RM32 (0x21) /* /r */
pythontech 0:5868e8752d44 58 #define OPCODE_OR_R32_TO_RM32 (0x09) /* /r */
pythontech 0:5868e8752d44 59 #define OPCODE_XOR_R32_TO_RM32 (0x31) /* /r */
pythontech 0:5868e8752d44 60 #define OPCODE_ADD_R32_TO_RM32 (0x01)
pythontech 0:5868e8752d44 61 #define OPCODE_ADD_I32_TO_RM32 (0x81) /* /0 */
pythontech 0:5868e8752d44 62 #define OPCODE_ADD_I8_TO_RM32 (0x83) /* /0 */
pythontech 0:5868e8752d44 63 #define OPCODE_SUB_R32_FROM_RM32 (0x29)
pythontech 0:5868e8752d44 64 #define OPCODE_SUB_I32_FROM_RM32 (0x81) /* /5 */
pythontech 0:5868e8752d44 65 #define OPCODE_SUB_I8_FROM_RM32 (0x83) /* /5 */
pythontech 0:5868e8752d44 66 //#define OPCODE_SHL_RM32_BY_I8 (0xc1) /* /4 */
pythontech 0:5868e8752d44 67 //#define OPCODE_SHR_RM32_BY_I8 (0xc1) /* /5 */
pythontech 0:5868e8752d44 68 //#define OPCODE_SAR_RM32_BY_I8 (0xc1) /* /7 */
pythontech 0:5868e8752d44 69 #define OPCODE_SHL_RM32_CL (0xd3) /* /4 */
pythontech 0:5868e8752d44 70 #define OPCODE_SAR_RM32_CL (0xd3) /* /7 */
pythontech 0:5868e8752d44 71 //#define OPCODE_CMP_I32_WITH_RM32 (0x81) /* /7 */
pythontech 0:5868e8752d44 72 //#define OPCODE_CMP_I8_WITH_RM32 (0x83) /* /7 */
pythontech 0:5868e8752d44 73 #define OPCODE_CMP_R32_WITH_RM32 (0x39)
pythontech 0:5868e8752d44 74 //#define OPCODE_CMP_RM32_WITH_R32 (0x3b)
pythontech 0:5868e8752d44 75 #define OPCODE_TEST_R8_WITH_RM8 (0x84) /* /r */
pythontech 0:5868e8752d44 76 #define OPCODE_JMP_REL8 (0xeb)
pythontech 0:5868e8752d44 77 #define OPCODE_JMP_REL32 (0xe9)
pythontech 0:5868e8752d44 78 #define OPCODE_JCC_REL8 (0x70) /* | jcc type */
pythontech 0:5868e8752d44 79 #define OPCODE_JCC_REL32_A (0x0f)
pythontech 0:5868e8752d44 80 #define OPCODE_JCC_REL32_B (0x80) /* | jcc type */
pythontech 0:5868e8752d44 81 #define OPCODE_SETCC_RM8_A (0x0f)
pythontech 0:5868e8752d44 82 #define OPCODE_SETCC_RM8_B (0x90) /* | jcc type, /0 */
pythontech 0:5868e8752d44 83 #define OPCODE_CALL_REL32 (0xe8)
pythontech 0:5868e8752d44 84 #define OPCODE_CALL_RM32 (0xff) /* /2 */
pythontech 0:5868e8752d44 85 #define OPCODE_LEAVE (0xc9)
pythontech 0:5868e8752d44 86
pythontech 0:5868e8752d44 87 #define MODRM_R32(x) ((x) << 3)
pythontech 0:5868e8752d44 88 #define MODRM_RM_DISP0 (0x00)
pythontech 0:5868e8752d44 89 #define MODRM_RM_DISP8 (0x40)
pythontech 0:5868e8752d44 90 #define MODRM_RM_DISP32 (0x80)
pythontech 0:5868e8752d44 91 #define MODRM_RM_REG (0xc0)
pythontech 0:5868e8752d44 92 #define MODRM_RM_R32(x) (x)
pythontech 0:5868e8752d44 93
pythontech 0:5868e8752d44 94 #define OP_SIZE_PREFIX (0x66)
pythontech 0:5868e8752d44 95
pythontech 0:5868e8752d44 96 #define IMM32_L0(x) ((x) & 0xff)
pythontech 0:5868e8752d44 97 #define IMM32_L1(x) (((x) >> 8) & 0xff)
pythontech 0:5868e8752d44 98 #define IMM32_L2(x) (((x) >> 16) & 0xff)
pythontech 0:5868e8752d44 99 #define IMM32_L3(x) (((x) >> 24) & 0xff)
pythontech 0:5868e8752d44 100
pythontech 0:5868e8752d44 101 #define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
pythontech 0:5868e8752d44 102
pythontech 0:5868e8752d44 103 struct _asm_x86_t {
pythontech 0:5868e8752d44 104 uint pass;
pythontech 0:5868e8752d44 105 mp_uint_t code_offset;
pythontech 0:5868e8752d44 106 mp_uint_t code_size;
pythontech 0:5868e8752d44 107 byte *code_base;
pythontech 0:5868e8752d44 108 byte dummy_data[8];
pythontech 0:5868e8752d44 109
pythontech 0:5868e8752d44 110 mp_uint_t max_num_labels;
pythontech 0:5868e8752d44 111 mp_uint_t *label_offsets;
pythontech 0:5868e8752d44 112 int num_locals;
pythontech 0:5868e8752d44 113 };
pythontech 0:5868e8752d44 114
pythontech 0:5868e8752d44 115 asm_x86_t *asm_x86_new(mp_uint_t max_num_labels) {
pythontech 0:5868e8752d44 116 asm_x86_t *as;
pythontech 0:5868e8752d44 117
pythontech 0:5868e8752d44 118 as = m_new0(asm_x86_t, 1);
pythontech 0:5868e8752d44 119 as->max_num_labels = max_num_labels;
pythontech 0:5868e8752d44 120 as->label_offsets = m_new(mp_uint_t, max_num_labels);
pythontech 0:5868e8752d44 121
pythontech 0:5868e8752d44 122 return as;
pythontech 0:5868e8752d44 123 }
pythontech 0:5868e8752d44 124
pythontech 0:5868e8752d44 125 void asm_x86_free(asm_x86_t *as, bool free_code) {
pythontech 0:5868e8752d44 126 if (free_code) {
pythontech 0:5868e8752d44 127 MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
pythontech 0:5868e8752d44 128 }
pythontech 0:5868e8752d44 129 m_del(mp_uint_t, as->label_offsets, as->max_num_labels);
pythontech 0:5868e8752d44 130 m_del_obj(asm_x86_t, as);
pythontech 0:5868e8752d44 131 }
pythontech 0:5868e8752d44 132
pythontech 0:5868e8752d44 133 void asm_x86_start_pass(asm_x86_t *as, mp_uint_t pass) {
pythontech 0:5868e8752d44 134 if (pass == ASM_X86_PASS_COMPUTE) {
pythontech 0:5868e8752d44 135 // reset all labels
pythontech 0:5868e8752d44 136 memset(as->label_offsets, -1, as->max_num_labels * sizeof(mp_uint_t));
pythontech 0:5868e8752d44 137 } else if (pass == ASM_X86_PASS_EMIT) {
pythontech 0:5868e8752d44 138 MP_PLAT_ALLOC_EXEC(as->code_offset, (void**)&as->code_base, &as->code_size);
pythontech 0:5868e8752d44 139 if (as->code_base == NULL) {
pythontech 0:5868e8752d44 140 assert(0);
pythontech 0:5868e8752d44 141 }
pythontech 0:5868e8752d44 142 }
pythontech 0:5868e8752d44 143 as->pass = pass;
pythontech 0:5868e8752d44 144 as->code_offset = 0;
pythontech 0:5868e8752d44 145 }
pythontech 0:5868e8752d44 146
pythontech 0:5868e8752d44 147 void asm_x86_end_pass(asm_x86_t *as) {
pythontech 0:5868e8752d44 148 (void)as;
pythontech 0:5868e8752d44 149 }
pythontech 0:5868e8752d44 150
pythontech 0:5868e8752d44 151 // all functions must go through this one to emit bytes
pythontech 0:5868e8752d44 152 STATIC byte *asm_x86_get_cur_to_write_bytes(asm_x86_t *as, int num_bytes_to_write) {
pythontech 0:5868e8752d44 153 //printf("emit %d\n", num_bytes_to_write);
pythontech 0:5868e8752d44 154 if (as->pass < ASM_X86_PASS_EMIT) {
pythontech 0:5868e8752d44 155 as->code_offset += num_bytes_to_write;
pythontech 0:5868e8752d44 156 return as->dummy_data;
pythontech 0:5868e8752d44 157 } else {
pythontech 0:5868e8752d44 158 assert(as->code_offset + num_bytes_to_write <= as->code_size);
pythontech 0:5868e8752d44 159 byte *c = as->code_base + as->code_offset;
pythontech 0:5868e8752d44 160 as->code_offset += num_bytes_to_write;
pythontech 0:5868e8752d44 161 return c;
pythontech 0:5868e8752d44 162 }
pythontech 0:5868e8752d44 163 }
pythontech 0:5868e8752d44 164
pythontech 0:5868e8752d44 165 mp_uint_t asm_x86_get_code_pos(asm_x86_t *as) {
pythontech 0:5868e8752d44 166 return as->code_offset;
pythontech 0:5868e8752d44 167 }
pythontech 0:5868e8752d44 168
pythontech 0:5868e8752d44 169 mp_uint_t asm_x86_get_code_size(asm_x86_t *as) {
pythontech 0:5868e8752d44 170 return as->code_size;
pythontech 0:5868e8752d44 171 }
pythontech 0:5868e8752d44 172
pythontech 0:5868e8752d44 173 void *asm_x86_get_code(asm_x86_t *as) {
pythontech 0:5868e8752d44 174 return as->code_base;
pythontech 0:5868e8752d44 175 }
pythontech 0:5868e8752d44 176
pythontech 0:5868e8752d44 177 STATIC void asm_x86_write_byte_1(asm_x86_t *as, byte b1) {
pythontech 0:5868e8752d44 178 byte* c = asm_x86_get_cur_to_write_bytes(as, 1);
pythontech 0:5868e8752d44 179 c[0] = b1;
pythontech 0:5868e8752d44 180 }
pythontech 0:5868e8752d44 181
pythontech 0:5868e8752d44 182 STATIC void asm_x86_write_byte_2(asm_x86_t *as, byte b1, byte b2) {
pythontech 0:5868e8752d44 183 byte* c = asm_x86_get_cur_to_write_bytes(as, 2);
pythontech 0:5868e8752d44 184 c[0] = b1;
pythontech 0:5868e8752d44 185 c[1] = b2;
pythontech 0:5868e8752d44 186 }
pythontech 0:5868e8752d44 187
pythontech 0:5868e8752d44 188 STATIC void asm_x86_write_byte_3(asm_x86_t *as, byte b1, byte b2, byte b3) {
pythontech 0:5868e8752d44 189 byte* c = asm_x86_get_cur_to_write_bytes(as, 3);
pythontech 0:5868e8752d44 190 c[0] = b1;
pythontech 0:5868e8752d44 191 c[1] = b2;
pythontech 0:5868e8752d44 192 c[2] = b3;
pythontech 0:5868e8752d44 193 }
pythontech 0:5868e8752d44 194
pythontech 0:5868e8752d44 195 STATIC void asm_x86_write_word32(asm_x86_t *as, int w32) {
pythontech 0:5868e8752d44 196 byte* c = asm_x86_get_cur_to_write_bytes(as, 4);
pythontech 0:5868e8752d44 197 c[0] = IMM32_L0(w32);
pythontech 0:5868e8752d44 198 c[1] = IMM32_L1(w32);
pythontech 0:5868e8752d44 199 c[2] = IMM32_L2(w32);
pythontech 0:5868e8752d44 200 c[3] = IMM32_L3(w32);
pythontech 0:5868e8752d44 201 }
pythontech 0:5868e8752d44 202
pythontech 0:5868e8752d44 203 // align must be a multiple of 2
pythontech 0:5868e8752d44 204 void asm_x86_align(asm_x86_t* as, mp_uint_t align) {
pythontech 0:5868e8752d44 205 // TODO fill unused data with NOPs?
pythontech 0:5868e8752d44 206 as->code_offset = (as->code_offset + align - 1) & (~(align - 1));
pythontech 0:5868e8752d44 207 }
pythontech 0:5868e8752d44 208
pythontech 0:5868e8752d44 209 void asm_x86_data(asm_x86_t* as, mp_uint_t bytesize, mp_uint_t val) {
pythontech 0:5868e8752d44 210 byte *c = asm_x86_get_cur_to_write_bytes(as, bytesize);
pythontech 0:5868e8752d44 211 // machine is little endian
pythontech 0:5868e8752d44 212 for (uint i = 0; i < bytesize; i++) {
pythontech 0:5868e8752d44 213 *c++ = val;
pythontech 0:5868e8752d44 214 val >>= 8;
pythontech 0:5868e8752d44 215 }
pythontech 0:5868e8752d44 216 }
pythontech 0:5868e8752d44 217
pythontech 0:5868e8752d44 218 STATIC void asm_x86_write_r32_disp(asm_x86_t *as, int r32, int disp_r32, int disp_offset) {
pythontech 0:5868e8752d44 219 assert(disp_r32 != ASM_X86_REG_ESP);
pythontech 0:5868e8752d44 220
pythontech 0:5868e8752d44 221 if (disp_offset == 0 && disp_r32 != ASM_X86_REG_EBP) {
pythontech 0:5868e8752d44 222 asm_x86_write_byte_1(as, MODRM_R32(r32) | MODRM_RM_DISP0 | MODRM_RM_R32(disp_r32));
pythontech 0:5868e8752d44 223 } else if (SIGNED_FIT8(disp_offset)) {
pythontech 0:5868e8752d44 224 asm_x86_write_byte_2(as, MODRM_R32(r32) | MODRM_RM_DISP8 | MODRM_RM_R32(disp_r32), IMM32_L0(disp_offset));
pythontech 0:5868e8752d44 225 } else {
pythontech 0:5868e8752d44 226 asm_x86_write_byte_1(as, MODRM_R32(r32) | MODRM_RM_DISP32 | MODRM_RM_R32(disp_r32));
pythontech 0:5868e8752d44 227 asm_x86_write_word32(as, disp_offset);
pythontech 0:5868e8752d44 228 }
pythontech 0:5868e8752d44 229 }
pythontech 0:5868e8752d44 230
pythontech 0:5868e8752d44 231 STATIC void asm_x86_generic_r32_r32(asm_x86_t *as, int dest_r32, int src_r32, int op) {
pythontech 0:5868e8752d44 232 asm_x86_write_byte_2(as, op, MODRM_R32(src_r32) | MODRM_RM_REG | MODRM_RM_R32(dest_r32));
pythontech 0:5868e8752d44 233 }
pythontech 0:5868e8752d44 234
pythontech 0:5868e8752d44 235 STATIC void asm_x86_nop(asm_x86_t *as) {
pythontech 0:5868e8752d44 236 asm_x86_write_byte_1(as, OPCODE_NOP);
pythontech 0:5868e8752d44 237 }
pythontech 0:5868e8752d44 238
pythontech 0:5868e8752d44 239 STATIC void asm_x86_push_r32(asm_x86_t *as, int src_r32) {
pythontech 0:5868e8752d44 240 asm_x86_write_byte_1(as, OPCODE_PUSH_R32 | src_r32);
pythontech 0:5868e8752d44 241 }
pythontech 0:5868e8752d44 242
pythontech 0:5868e8752d44 243 #if 0
pythontech 0:5868e8752d44 244 void asm_x86_push_i32(asm_x86_t *as, int src_i32) {
pythontech 0:5868e8752d44 245 asm_x86_write_byte_1(as, OPCODE_PUSH_I32);
pythontech 0:5868e8752d44 246 asm_x86_write_word32(as, src_i32);
pythontech 0:5868e8752d44 247 }
pythontech 0:5868e8752d44 248
pythontech 0:5868e8752d44 249 void asm_x86_push_disp(asm_x86_t *as, int src_r32, int src_offset) {
pythontech 0:5868e8752d44 250 asm_x86_write_byte_1(as, OPCODE_PUSH_M32);
pythontech 0:5868e8752d44 251 asm_x86_write_r32_disp(as, 6, src_r32, src_offset);
pythontech 0:5868e8752d44 252 }
pythontech 0:5868e8752d44 253 #endif
pythontech 0:5868e8752d44 254
pythontech 0:5868e8752d44 255 STATIC void asm_x86_pop_r32(asm_x86_t *as, int dest_r32) {
pythontech 0:5868e8752d44 256 asm_x86_write_byte_1(as, OPCODE_POP_R32 | dest_r32);
pythontech 0:5868e8752d44 257 }
pythontech 0:5868e8752d44 258
pythontech 0:5868e8752d44 259 STATIC void asm_x86_ret(asm_x86_t *as) {
pythontech 0:5868e8752d44 260 asm_x86_write_byte_1(as, OPCODE_RET);
pythontech 0:5868e8752d44 261 }
pythontech 0:5868e8752d44 262
pythontech 0:5868e8752d44 263 void asm_x86_mov_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
pythontech 0:5868e8752d44 264 asm_x86_generic_r32_r32(as, dest_r32, src_r32, OPCODE_MOV_R32_TO_RM32);
pythontech 0:5868e8752d44 265 }
pythontech 0:5868e8752d44 266
pythontech 0:5868e8752d44 267 void asm_x86_mov_r8_to_mem8(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp) {
pythontech 0:5868e8752d44 268 asm_x86_write_byte_1(as, OPCODE_MOV_R8_TO_RM8);
pythontech 0:5868e8752d44 269 asm_x86_write_r32_disp(as, src_r32, dest_r32, dest_disp);
pythontech 0:5868e8752d44 270 }
pythontech 0:5868e8752d44 271
pythontech 0:5868e8752d44 272 void asm_x86_mov_r16_to_mem16(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp) {
pythontech 0:5868e8752d44 273 asm_x86_write_byte_2(as, OP_SIZE_PREFIX, OPCODE_MOV_R32_TO_RM32);
pythontech 0:5868e8752d44 274 asm_x86_write_r32_disp(as, src_r32, dest_r32, dest_disp);
pythontech 0:5868e8752d44 275 }
pythontech 0:5868e8752d44 276
pythontech 0:5868e8752d44 277 void asm_x86_mov_r32_to_mem32(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp) {
pythontech 0:5868e8752d44 278 asm_x86_write_byte_1(as, OPCODE_MOV_R32_TO_RM32);
pythontech 0:5868e8752d44 279 asm_x86_write_r32_disp(as, src_r32, dest_r32, dest_disp);
pythontech 0:5868e8752d44 280 }
pythontech 0:5868e8752d44 281
pythontech 0:5868e8752d44 282 void asm_x86_mov_mem8_to_r32zx(asm_x86_t *as, int src_r32, int src_disp, int dest_r32) {
pythontech 0:5868e8752d44 283 asm_x86_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM8_TO_R32);
pythontech 0:5868e8752d44 284 asm_x86_write_r32_disp(as, dest_r32, src_r32, src_disp);
pythontech 0:5868e8752d44 285 }
pythontech 0:5868e8752d44 286
pythontech 0:5868e8752d44 287 void asm_x86_mov_mem16_to_r32zx(asm_x86_t *as, int src_r32, int src_disp, int dest_r32) {
pythontech 0:5868e8752d44 288 asm_x86_write_byte_2(as, 0x0f, OPCODE_MOVZX_RM16_TO_R32);
pythontech 0:5868e8752d44 289 asm_x86_write_r32_disp(as, dest_r32, src_r32, src_disp);
pythontech 0:5868e8752d44 290 }
pythontech 0:5868e8752d44 291
pythontech 0:5868e8752d44 292 void asm_x86_mov_mem32_to_r32(asm_x86_t *as, int src_r32, int src_disp, int dest_r32) {
pythontech 0:5868e8752d44 293 asm_x86_write_byte_1(as, OPCODE_MOV_RM32_TO_R32);
pythontech 0:5868e8752d44 294 asm_x86_write_r32_disp(as, dest_r32, src_r32, src_disp);
pythontech 0:5868e8752d44 295 }
pythontech 0:5868e8752d44 296
pythontech 0:5868e8752d44 297 STATIC void asm_x86_lea_disp_to_r32(asm_x86_t *as, int src_r32, int src_disp, int dest_r32) {
pythontech 0:5868e8752d44 298 asm_x86_write_byte_1(as, OPCODE_LEA_MEM_TO_R32);
pythontech 0:5868e8752d44 299 asm_x86_write_r32_disp(as, dest_r32, src_r32, src_disp);
pythontech 0:5868e8752d44 300 }
pythontech 0:5868e8752d44 301
pythontech 0:5868e8752d44 302 #if 0
pythontech 0:5868e8752d44 303 void asm_x86_mov_i8_to_r8(asm_x86_t *as, int src_i8, int dest_r32) {
pythontech 0:5868e8752d44 304 asm_x86_write_byte_2(as, OPCODE_MOV_I8_TO_R8 | dest_r32, src_i8);
pythontech 0:5868e8752d44 305 }
pythontech 0:5868e8752d44 306 #endif
pythontech 0:5868e8752d44 307
pythontech 0:5868e8752d44 308 void asm_x86_mov_i32_to_r32(asm_x86_t *as, int32_t src_i32, int dest_r32) {
pythontech 0:5868e8752d44 309 asm_x86_write_byte_1(as, OPCODE_MOV_I32_TO_R32 | dest_r32);
pythontech 0:5868e8752d44 310 asm_x86_write_word32(as, src_i32);
pythontech 0:5868e8752d44 311 }
pythontech 0:5868e8752d44 312
pythontech 0:5868e8752d44 313 // src_i32 is stored as a full word in the code, and aligned to machine-word boundary
pythontech 0:5868e8752d44 314 void asm_x86_mov_i32_to_r32_aligned(asm_x86_t *as, int32_t src_i32, int dest_r32) {
pythontech 0:5868e8752d44 315 // mov instruction uses 1 byte for the instruction, before the i32
pythontech 0:5868e8752d44 316 while (((as->code_offset + 1) & (WORD_SIZE - 1)) != 0) {
pythontech 0:5868e8752d44 317 asm_x86_nop(as);
pythontech 0:5868e8752d44 318 }
pythontech 0:5868e8752d44 319 asm_x86_mov_i32_to_r32(as, src_i32, dest_r32);
pythontech 0:5868e8752d44 320 }
pythontech 0:5868e8752d44 321
pythontech 0:5868e8752d44 322 void asm_x86_and_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
pythontech 0:5868e8752d44 323 asm_x86_generic_r32_r32(as, dest_r32, src_r32, OPCODE_AND_R32_TO_RM32);
pythontech 0:5868e8752d44 324 }
pythontech 0:5868e8752d44 325
pythontech 0:5868e8752d44 326 void asm_x86_or_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
pythontech 0:5868e8752d44 327 asm_x86_generic_r32_r32(as, dest_r32, src_r32, OPCODE_OR_R32_TO_RM32);
pythontech 0:5868e8752d44 328 }
pythontech 0:5868e8752d44 329
pythontech 0:5868e8752d44 330 void asm_x86_xor_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
pythontech 0:5868e8752d44 331 asm_x86_generic_r32_r32(as, dest_r32, src_r32, OPCODE_XOR_R32_TO_RM32);
pythontech 0:5868e8752d44 332 }
pythontech 0:5868e8752d44 333
pythontech 0:5868e8752d44 334 void asm_x86_shl_r32_cl(asm_x86_t* as, int dest_r32) {
pythontech 0:5868e8752d44 335 asm_x86_generic_r32_r32(as, dest_r32, 4, OPCODE_SHL_RM32_CL);
pythontech 0:5868e8752d44 336 }
pythontech 0:5868e8752d44 337
pythontech 0:5868e8752d44 338 void asm_x86_sar_r32_cl(asm_x86_t* as, int dest_r32) {
pythontech 0:5868e8752d44 339 asm_x86_generic_r32_r32(as, dest_r32, 7, OPCODE_SAR_RM32_CL);
pythontech 0:5868e8752d44 340 }
pythontech 0:5868e8752d44 341
pythontech 0:5868e8752d44 342 void asm_x86_add_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
pythontech 0:5868e8752d44 343 asm_x86_generic_r32_r32(as, dest_r32, src_r32, OPCODE_ADD_R32_TO_RM32);
pythontech 0:5868e8752d44 344 }
pythontech 0:5868e8752d44 345
pythontech 0:5868e8752d44 346 STATIC void asm_x86_add_i32_to_r32(asm_x86_t *as, int src_i32, int dest_r32) {
pythontech 0:5868e8752d44 347 if (SIGNED_FIT8(src_i32)) {
pythontech 0:5868e8752d44 348 asm_x86_write_byte_2(as, OPCODE_ADD_I8_TO_RM32, MODRM_R32(0) | MODRM_RM_REG | MODRM_RM_R32(dest_r32));
pythontech 0:5868e8752d44 349 asm_x86_write_byte_1(as, src_i32 & 0xff);
pythontech 0:5868e8752d44 350 } else {
pythontech 0:5868e8752d44 351 asm_x86_write_byte_2(as, OPCODE_ADD_I32_TO_RM32, MODRM_R32(0) | MODRM_RM_REG | MODRM_RM_R32(dest_r32));
pythontech 0:5868e8752d44 352 asm_x86_write_word32(as, src_i32);
pythontech 0:5868e8752d44 353 }
pythontech 0:5868e8752d44 354 }
pythontech 0:5868e8752d44 355
pythontech 0:5868e8752d44 356 void asm_x86_sub_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
pythontech 0:5868e8752d44 357 asm_x86_generic_r32_r32(as, dest_r32, src_r32, OPCODE_SUB_R32_FROM_RM32);
pythontech 0:5868e8752d44 358 }
pythontech 0:5868e8752d44 359
pythontech 0:5868e8752d44 360 STATIC void asm_x86_sub_r32_i32(asm_x86_t *as, int dest_r32, int src_i32) {
pythontech 0:5868e8752d44 361 if (SIGNED_FIT8(src_i32)) {
pythontech 0:5868e8752d44 362 // defaults to 32 bit operation
pythontech 0:5868e8752d44 363 asm_x86_write_byte_2(as, OPCODE_SUB_I8_FROM_RM32, MODRM_R32(5) | MODRM_RM_REG | MODRM_RM_R32(dest_r32));
pythontech 0:5868e8752d44 364 asm_x86_write_byte_1(as, src_i32 & 0xff);
pythontech 0:5868e8752d44 365 } else {
pythontech 0:5868e8752d44 366 // defaults to 32 bit operation
pythontech 0:5868e8752d44 367 asm_x86_write_byte_2(as, OPCODE_SUB_I32_FROM_RM32, MODRM_R32(5) | MODRM_RM_REG | MODRM_RM_R32(dest_r32));
pythontech 0:5868e8752d44 368 asm_x86_write_word32(as, src_i32);
pythontech 0:5868e8752d44 369 }
pythontech 0:5868e8752d44 370 }
pythontech 0:5868e8752d44 371
pythontech 0:5868e8752d44 372 void asm_x86_mul_r32_r32(asm_x86_t *as, int dest_r32, int src_r32) {
pythontech 0:5868e8752d44 373 // imul reg32, reg/mem32 -- 0x0f 0xaf /r
pythontech 0:5868e8752d44 374 asm_x86_write_byte_3(as, 0x0f, 0xaf, MODRM_R32(dest_r32) | MODRM_RM_REG | MODRM_RM_R32(src_r32));
pythontech 0:5868e8752d44 375 }
pythontech 0:5868e8752d44 376
pythontech 0:5868e8752d44 377 #if 0
pythontech 0:5868e8752d44 378 /* shifts not tested */
pythontech 0:5868e8752d44 379 void asm_x86_shl_r32_by_imm(asm_x86_t *as, int r32, int imm) {
pythontech 0:5868e8752d44 380 asm_x86_write_byte_2(as, OPCODE_SHL_RM32_BY_I8, MODRM_R32(4) | MODRM_RM_REG | MODRM_RM_R32(r32));
pythontech 0:5868e8752d44 381 asm_x86_write_byte_1(as, imm);
pythontech 0:5868e8752d44 382 }
pythontech 0:5868e8752d44 383
pythontech 0:5868e8752d44 384 void asm_x86_shr_r32_by_imm(asm_x86_t *as, int r32, int imm) {
pythontech 0:5868e8752d44 385 asm_x86_write_byte_2(as, OPCODE_SHR_RM32_BY_I8, MODRM_R32(5) | MODRM_RM_REG | MODRM_RM_R32(r32));
pythontech 0:5868e8752d44 386 asm_x86_write_byte_1(as, imm);
pythontech 0:5868e8752d44 387 }
pythontech 0:5868e8752d44 388
pythontech 0:5868e8752d44 389 void asm_x86_sar_r32_by_imm(asm_x86_t *as, int r32, int imm) {
pythontech 0:5868e8752d44 390 asm_x86_write_byte_2(as, OPCODE_SAR_RM32_BY_I8, MODRM_R32(7) | MODRM_RM_REG | MODRM_RM_R32(r32));
pythontech 0:5868e8752d44 391 asm_x86_write_byte_1(as, imm);
pythontech 0:5868e8752d44 392 }
pythontech 0:5868e8752d44 393 #endif
pythontech 0:5868e8752d44 394
pythontech 0:5868e8752d44 395 void asm_x86_cmp_r32_with_r32(asm_x86_t *as, int src_r32_a, int src_r32_b) {
pythontech 0:5868e8752d44 396 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));
pythontech 0:5868e8752d44 397 }
pythontech 0:5868e8752d44 398
pythontech 0:5868e8752d44 399 #if 0
pythontech 0:5868e8752d44 400 void asm_x86_cmp_i32_with_r32(asm_x86_t *as, int src_i32, int src_r32) {
pythontech 0:5868e8752d44 401 if (SIGNED_FIT8(src_i32)) {
pythontech 0:5868e8752d44 402 asm_x86_write_byte_2(as, OPCODE_CMP_I8_WITH_RM32, MODRM_R32(7) | MODRM_RM_REG | MODRM_RM_R32(src_r32));
pythontech 0:5868e8752d44 403 asm_x86_write_byte_1(as, src_i32 & 0xff);
pythontech 0:5868e8752d44 404 } else {
pythontech 0:5868e8752d44 405 asm_x86_write_byte_2(as, OPCODE_CMP_I32_WITH_RM32, MODRM_R32(7) | MODRM_RM_REG | MODRM_RM_R32(src_r32));
pythontech 0:5868e8752d44 406 asm_x86_write_word32(as, src_i32);
pythontech 0:5868e8752d44 407 }
pythontech 0:5868e8752d44 408 }
pythontech 0:5868e8752d44 409 #endif
pythontech 0:5868e8752d44 410
pythontech 0:5868e8752d44 411 void asm_x86_test_r8_with_r8(asm_x86_t *as, int src_r32_a, int src_r32_b) {
pythontech 0:5868e8752d44 412 // TODO implement for other registers
pythontech 0:5868e8752d44 413 assert(src_r32_a == ASM_X86_REG_EAX);
pythontech 0:5868e8752d44 414 assert(src_r32_b == ASM_X86_REG_EAX);
pythontech 0:5868e8752d44 415 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));
pythontech 0:5868e8752d44 416 }
pythontech 0:5868e8752d44 417
pythontech 0:5868e8752d44 418 void asm_x86_setcc_r8(asm_x86_t *as, mp_uint_t jcc_type, int dest_r8) {
pythontech 0:5868e8752d44 419 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));
pythontech 0:5868e8752d44 420 }
pythontech 0:5868e8752d44 421
pythontech 0:5868e8752d44 422 void asm_x86_label_assign(asm_x86_t *as, mp_uint_t label) {
pythontech 0:5868e8752d44 423 assert(label < as->max_num_labels);
pythontech 0:5868e8752d44 424 if (as->pass < ASM_X86_PASS_EMIT) {
pythontech 0:5868e8752d44 425 // assign label offset
pythontech 0:5868e8752d44 426 assert(as->label_offsets[label] == (mp_uint_t)-1);
pythontech 0:5868e8752d44 427 as->label_offsets[label] = as->code_offset;
pythontech 0:5868e8752d44 428 } else {
pythontech 0:5868e8752d44 429 // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
pythontech 0:5868e8752d44 430 //printf("l%d: (at %d=%ld)\n", label, as->label_offsets[label], as->code_offset);
pythontech 0:5868e8752d44 431 assert(as->label_offsets[label] == as->code_offset);
pythontech 0:5868e8752d44 432 }
pythontech 0:5868e8752d44 433 }
pythontech 0:5868e8752d44 434
pythontech 0:5868e8752d44 435 STATIC mp_uint_t get_label_dest(asm_x86_t *as, mp_uint_t label) {
pythontech 0:5868e8752d44 436 assert(label < as->max_num_labels);
pythontech 0:5868e8752d44 437 return as->label_offsets[label];
pythontech 0:5868e8752d44 438 }
pythontech 0:5868e8752d44 439
pythontech 0:5868e8752d44 440 void asm_x86_jmp_label(asm_x86_t *as, mp_uint_t label) {
pythontech 0:5868e8752d44 441 mp_uint_t dest = get_label_dest(as, label);
pythontech 0:5868e8752d44 442 mp_int_t rel = dest - as->code_offset;
pythontech 0:5868e8752d44 443 if (dest != (mp_uint_t)-1 && rel < 0) {
pythontech 0:5868e8752d44 444 // is a backwards jump, so we know the size of the jump on the first pass
pythontech 0:5868e8752d44 445 // calculate rel assuming 8 bit relative jump
pythontech 0:5868e8752d44 446 rel -= 2;
pythontech 0:5868e8752d44 447 if (SIGNED_FIT8(rel)) {
pythontech 0:5868e8752d44 448 asm_x86_write_byte_2(as, OPCODE_JMP_REL8, rel & 0xff);
pythontech 0:5868e8752d44 449 } else {
pythontech 0:5868e8752d44 450 rel += 2;
pythontech 0:5868e8752d44 451 goto large_jump;
pythontech 0:5868e8752d44 452 }
pythontech 0:5868e8752d44 453 } else {
pythontech 0:5868e8752d44 454 // is a forwards jump, so need to assume it's large
pythontech 0:5868e8752d44 455 large_jump:
pythontech 0:5868e8752d44 456 rel -= 5;
pythontech 0:5868e8752d44 457 asm_x86_write_byte_1(as, OPCODE_JMP_REL32);
pythontech 0:5868e8752d44 458 asm_x86_write_word32(as, rel);
pythontech 0:5868e8752d44 459 }
pythontech 0:5868e8752d44 460 }
pythontech 0:5868e8752d44 461
pythontech 0:5868e8752d44 462 void asm_x86_jcc_label(asm_x86_t *as, mp_uint_t jcc_type, mp_uint_t label) {
pythontech 0:5868e8752d44 463 mp_uint_t dest = get_label_dest(as, label);
pythontech 0:5868e8752d44 464 mp_int_t rel = dest - as->code_offset;
pythontech 0:5868e8752d44 465 if (dest != (mp_uint_t)-1 && rel < 0) {
pythontech 0:5868e8752d44 466 // is a backwards jump, so we know the size of the jump on the first pass
pythontech 0:5868e8752d44 467 // calculate rel assuming 8 bit relative jump
pythontech 0:5868e8752d44 468 rel -= 2;
pythontech 0:5868e8752d44 469 if (SIGNED_FIT8(rel)) {
pythontech 0:5868e8752d44 470 asm_x86_write_byte_2(as, OPCODE_JCC_REL8 | jcc_type, rel & 0xff);
pythontech 0:5868e8752d44 471 } else {
pythontech 0:5868e8752d44 472 rel += 2;
pythontech 0:5868e8752d44 473 goto large_jump;
pythontech 0:5868e8752d44 474 }
pythontech 0:5868e8752d44 475 } else {
pythontech 0:5868e8752d44 476 // is a forwards jump, so need to assume it's large
pythontech 0:5868e8752d44 477 large_jump:
pythontech 0:5868e8752d44 478 rel -= 6;
pythontech 0:5868e8752d44 479 asm_x86_write_byte_2(as, OPCODE_JCC_REL32_A, OPCODE_JCC_REL32_B | jcc_type);
pythontech 0:5868e8752d44 480 asm_x86_write_word32(as, rel);
pythontech 0:5868e8752d44 481 }
pythontech 0:5868e8752d44 482 }
pythontech 0:5868e8752d44 483
pythontech 0:5868e8752d44 484 void asm_x86_entry(asm_x86_t *as, mp_uint_t num_locals) {
pythontech 0:5868e8752d44 485 asm_x86_push_r32(as, ASM_X86_REG_EBP);
pythontech 0:5868e8752d44 486 asm_x86_mov_r32_r32(as, ASM_X86_REG_EBP, ASM_X86_REG_ESP);
pythontech 0:5868e8752d44 487 if (num_locals > 0) {
pythontech 0:5868e8752d44 488 asm_x86_sub_r32_i32(as, ASM_X86_REG_ESP, num_locals * WORD_SIZE);
pythontech 0:5868e8752d44 489 }
pythontech 0:5868e8752d44 490 asm_x86_push_r32(as, ASM_X86_REG_EBX);
pythontech 0:5868e8752d44 491 asm_x86_push_r32(as, ASM_X86_REG_ESI);
pythontech 0:5868e8752d44 492 asm_x86_push_r32(as, ASM_X86_REG_EDI);
pythontech 0:5868e8752d44 493 // TODO align stack on 16-byte boundary
pythontech 0:5868e8752d44 494 as->num_locals = num_locals;
pythontech 0:5868e8752d44 495 }
pythontech 0:5868e8752d44 496
pythontech 0:5868e8752d44 497 void asm_x86_exit(asm_x86_t *as) {
pythontech 0:5868e8752d44 498 asm_x86_pop_r32(as, ASM_X86_REG_EDI);
pythontech 0:5868e8752d44 499 asm_x86_pop_r32(as, ASM_X86_REG_ESI);
pythontech 0:5868e8752d44 500 asm_x86_pop_r32(as, ASM_X86_REG_EBX);
pythontech 0:5868e8752d44 501 asm_x86_write_byte_1(as, OPCODE_LEAVE);
pythontech 0:5868e8752d44 502 asm_x86_ret(as);
pythontech 0:5868e8752d44 503 }
pythontech 0:5868e8752d44 504
pythontech 0:5868e8752d44 505 #if 0
pythontech 0:5868e8752d44 506 void asm_x86_push_arg(asm_x86_t *as, int src_arg_num) {
pythontech 0:5868e8752d44 507 asm_x86_push_disp(as, ASM_X86_REG_EBP, 2 * WORD_SIZE + src_arg_num * WORD_SIZE);
pythontech 0:5868e8752d44 508 }
pythontech 0:5868e8752d44 509 #endif
pythontech 0:5868e8752d44 510
pythontech 0:5868e8752d44 511 void asm_x86_mov_arg_to_r32(asm_x86_t *as, int src_arg_num, int dest_r32) {
pythontech 0:5868e8752d44 512 asm_x86_mov_mem32_to_r32(as, ASM_X86_REG_EBP, 2 * WORD_SIZE + src_arg_num * WORD_SIZE, dest_r32);
pythontech 0:5868e8752d44 513 }
pythontech 0:5868e8752d44 514
pythontech 0:5868e8752d44 515 #if 0
pythontech 0:5868e8752d44 516 void asm_x86_mov_r32_to_arg(asm_x86_t *as, int src_r32, int dest_arg_num) {
pythontech 0:5868e8752d44 517 asm_x86_mov_r32_to_mem32(as, src_r32, ASM_X86_REG_EBP, 2 * WORD_SIZE + dest_arg_num * WORD_SIZE);
pythontech 0:5868e8752d44 518 }
pythontech 0:5868e8752d44 519 #endif
pythontech 0:5868e8752d44 520
pythontech 0:5868e8752d44 521 // locals:
pythontech 0:5868e8752d44 522 // - stored on the stack in ascending order
pythontech 0:5868e8752d44 523 // - numbered 0 through as->num_locals-1
pythontech 0:5868e8752d44 524 // - EBP points above the last local
pythontech 0:5868e8752d44 525 //
pythontech 0:5868e8752d44 526 // | EBP
pythontech 0:5868e8752d44 527 // v
pythontech 0:5868e8752d44 528 // l0 l1 l2 ... l(n-1)
pythontech 0:5868e8752d44 529 // ^ ^
pythontech 0:5868e8752d44 530 // | low address | high address in RAM
pythontech 0:5868e8752d44 531 //
pythontech 0:5868e8752d44 532 STATIC int asm_x86_local_offset_from_ebp(asm_x86_t *as, int local_num) {
pythontech 0:5868e8752d44 533 return (-as->num_locals + local_num) * WORD_SIZE;
pythontech 0:5868e8752d44 534 }
pythontech 0:5868e8752d44 535
pythontech 0:5868e8752d44 536 void asm_x86_mov_local_to_r32(asm_x86_t *as, int src_local_num, int dest_r32) {
pythontech 0:5868e8752d44 537 asm_x86_mov_mem32_to_r32(as, ASM_X86_REG_EBP, asm_x86_local_offset_from_ebp(as, src_local_num), dest_r32);
pythontech 0:5868e8752d44 538 }
pythontech 0:5868e8752d44 539
pythontech 0:5868e8752d44 540 void asm_x86_mov_r32_to_local(asm_x86_t *as, int src_r32, int dest_local_num) {
pythontech 0:5868e8752d44 541 asm_x86_mov_r32_to_mem32(as, src_r32, ASM_X86_REG_EBP, asm_x86_local_offset_from_ebp(as, dest_local_num));
pythontech 0:5868e8752d44 542 }
pythontech 0:5868e8752d44 543
pythontech 0:5868e8752d44 544 void asm_x86_mov_local_addr_to_r32(asm_x86_t *as, int local_num, int dest_r32) {
pythontech 0:5868e8752d44 545 int offset = asm_x86_local_offset_from_ebp(as, local_num);
pythontech 0:5868e8752d44 546 if (offset == 0) {
pythontech 0:5868e8752d44 547 asm_x86_mov_r32_r32(as, dest_r32, ASM_X86_REG_EBP);
pythontech 0:5868e8752d44 548 } else {
pythontech 0:5868e8752d44 549 asm_x86_lea_disp_to_r32(as, ASM_X86_REG_EBP, offset, dest_r32);
pythontech 0:5868e8752d44 550 }
pythontech 0:5868e8752d44 551 }
pythontech 0:5868e8752d44 552
pythontech 0:5868e8752d44 553 #if 0
pythontech 0:5868e8752d44 554 void asm_x86_push_local(asm_x86_t *as, int local_num) {
pythontech 0:5868e8752d44 555 asm_x86_push_disp(as, ASM_X86_REG_EBP, asm_x86_local_offset_from_ebp(as, local_num));
pythontech 0:5868e8752d44 556 }
pythontech 0:5868e8752d44 557
pythontech 0:5868e8752d44 558 void asm_x86_push_local_addr(asm_x86_t *as, int local_num, int temp_r32)
pythontech 0:5868e8752d44 559 {
pythontech 0:5868e8752d44 560 asm_x86_mov_r32_r32(as, temp_r32, ASM_X86_REG_EBP);
pythontech 0:5868e8752d44 561 asm_x86_add_i32_to_r32(as, asm_x86_local_offset_from_ebp(as, local_num), temp_r32);
pythontech 0:5868e8752d44 562 asm_x86_push_r32(as, temp_r32);
pythontech 0:5868e8752d44 563 }
pythontech 0:5868e8752d44 564 #endif
pythontech 0:5868e8752d44 565
pythontech 0:5868e8752d44 566 void asm_x86_call_ind(asm_x86_t *as, void *ptr, mp_uint_t n_args, int temp_r32) {
pythontech 0:5868e8752d44 567 // TODO align stack on 16-byte boundary before the call
pythontech 0:5868e8752d44 568 assert(n_args <= 5);
pythontech 0:5868e8752d44 569 if (n_args > 4) {
pythontech 0:5868e8752d44 570 asm_x86_push_r32(as, ASM_X86_REG_ARG_5);
pythontech 0:5868e8752d44 571 }
pythontech 0:5868e8752d44 572 if (n_args > 3) {
pythontech 0:5868e8752d44 573 asm_x86_push_r32(as, ASM_X86_REG_ARG_4);
pythontech 0:5868e8752d44 574 }
pythontech 0:5868e8752d44 575 if (n_args > 2) {
pythontech 0:5868e8752d44 576 asm_x86_push_r32(as, ASM_X86_REG_ARG_3);
pythontech 0:5868e8752d44 577 }
pythontech 0:5868e8752d44 578 if (n_args > 1) {
pythontech 0:5868e8752d44 579 asm_x86_push_r32(as, ASM_X86_REG_ARG_2);
pythontech 0:5868e8752d44 580 }
pythontech 0:5868e8752d44 581 if (n_args > 0) {
pythontech 0:5868e8752d44 582 asm_x86_push_r32(as, ASM_X86_REG_ARG_1);
pythontech 0:5868e8752d44 583 }
pythontech 0:5868e8752d44 584 #ifdef __LP64__
pythontech 0:5868e8752d44 585 // We wouldn't run x86 code on an x64 machine. This is here to enable
pythontech 0:5868e8752d44 586 // testing of the x86 emitter only.
pythontech 0:5868e8752d44 587 asm_x86_mov_i32_to_r32(as, (int32_t)(int64_t)ptr, temp_r32);
pythontech 0:5868e8752d44 588 #else
pythontech 0:5868e8752d44 589 // If we get here, sizeof(int) == sizeof(void*).
pythontech 0:5868e8752d44 590 asm_x86_mov_i32_to_r32(as, (int32_t)ptr, temp_r32);
pythontech 0:5868e8752d44 591 #endif
pythontech 0:5868e8752d44 592 asm_x86_write_byte_2(as, OPCODE_CALL_RM32, MODRM_R32(2) | MODRM_RM_REG | MODRM_RM_R32(temp_r32));
pythontech 0:5868e8752d44 593 // this reduces code size by 2 bytes per call, but doesn't seem to speed it up at all
pythontech 0:5868e8752d44 594 /*
pythontech 0:5868e8752d44 595 asm_x86_write_byte_1(as, OPCODE_CALL_REL32);
pythontech 0:5868e8752d44 596 asm_x86_write_word32(as, ptr - (void*)(as->code_base + as->code_offset + 4));
pythontech 0:5868e8752d44 597 */
pythontech 0:5868e8752d44 598
pythontech 0:5868e8752d44 599 // the caller must clean up the stack
pythontech 0:5868e8752d44 600 if (n_args > 0) {
pythontech 0:5868e8752d44 601 asm_x86_add_i32_to_r32(as, WORD_SIZE * n_args, ASM_X86_REG_ESP);
pythontech 0:5868e8752d44 602 }
pythontech 0:5868e8752d44 603 }
pythontech 0:5868e8752d44 604
pythontech 0:5868e8752d44 605 #endif // MICROPY_EMIT_X86