2014-09-06 18:06:36 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-09-06 18:06:36 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
all: Unify header guard usage.
The code conventions suggest using header guards, but do not define how
those should look like and instead point to existing files. However, not
all existing files follow the same scheme, sometimes omitting header guards
altogether, sometimes using non-standard names, making it easy to
accidentally pick a "wrong" example.
This commit ensures that all header files of the MicroPython project (that
were not simply copied from somewhere else) follow the same pattern, that
was already present in the majority of files, especially in the py folder.
The rules are as follows.
Naming convention:
* start with the words MICROPY_INCLUDED
* contain the full path to the file
* replace special characters with _
In addition, there are no empty lines before #ifndef, between #ifndef and
one empty line before #endif. #endif is followed by a comment containing
the name of the guard macro.
py/grammar.h cannot use header guards by design, since it has to be
included multiple times in a single C file. Several other files also do not
need header guards as they are only used internally and guaranteed to be
included only once:
* MICROPY_MPHALPORT_H
* mpconfigboard.h
* mpconfigport.h
* mpthreadport.h
* pin_defs_*.h
* qstrdefs*.h
2017-06-29 17:14:58 -04:00
|
|
|
#ifndef MICROPY_INCLUDED_PY_ASMX86_H
|
|
|
|
#define MICROPY_INCLUDED_PY_ASMX86_H
|
2015-01-01 13:07:43 -05:00
|
|
|
|
|
|
|
#include "py/mpconfig.h"
|
|
|
|
#include "py/misc.h"
|
2016-11-27 17:24:50 -05:00
|
|
|
#include "py/asmbase.h"
|
2014-09-06 18:06:36 -04:00
|
|
|
|
2014-09-06 18:38:50 -04:00
|
|
|
// x86 cdecl calling convention is:
|
|
|
|
// - args passed on the stack in reverse order
|
|
|
|
// - return value in EAX
|
|
|
|
// - caller cleans up the stack after a call
|
|
|
|
// - stack must be aligned to 16-byte boundary before all calls
|
|
|
|
// - EAX, ECX, EDX are caller-save
|
|
|
|
// - EBX, ESI, EDI, EBP, ESP, EIP are callee-save
|
|
|
|
|
2014-09-29 13:45:42 -04:00
|
|
|
// In the functions below, argument order follows x86 docs and generally
|
|
|
|
// the destination is the first argument.
|
|
|
|
// NOTE: this is a change from the old convention used in this file and
|
|
|
|
// some functions still use the old (reverse) convention.
|
|
|
|
|
2014-09-29 11:25:04 -04:00
|
|
|
#define ASM_X86_REG_EAX (0)
|
|
|
|
#define ASM_X86_REG_ECX (1)
|
|
|
|
#define ASM_X86_REG_EDX (2)
|
|
|
|
#define ASM_X86_REG_EBX (3)
|
|
|
|
#define ASM_X86_REG_ESP (4)
|
|
|
|
#define ASM_X86_REG_EBP (5)
|
|
|
|
#define ASM_X86_REG_ESI (6)
|
|
|
|
#define ASM_X86_REG_EDI (7)
|
2014-09-06 18:06:36 -04:00
|
|
|
|
2014-09-08 18:16:35 -04:00
|
|
|
// x86 passes values on the stack, but the emitter is register based, so we need
|
|
|
|
// to define registers that can temporarily hold the function arguments. They
|
|
|
|
// need to be defined here so that asm_x86_call_ind can push them onto the stack
|
|
|
|
// before the call.
|
2014-09-29 11:25:04 -04:00
|
|
|
#define ASM_X86_REG_ARG_1 ASM_X86_REG_EAX
|
|
|
|
#define ASM_X86_REG_ARG_2 ASM_X86_REG_ECX
|
|
|
|
#define ASM_X86_REG_ARG_3 ASM_X86_REG_EDX
|
2015-04-06 17:38:53 -04:00
|
|
|
#define ASM_X86_REG_ARG_4 ASM_X86_REG_EBX
|
2014-09-08 18:16:35 -04:00
|
|
|
|
2014-09-06 18:06:36 -04:00
|
|
|
// condition codes, used for jcc and setcc (despite their j-name!)
|
|
|
|
#define ASM_X86_CC_JB (0x2) // below, unsigned
|
|
|
|
#define ASM_X86_CC_JZ (0x4)
|
|
|
|
#define ASM_X86_CC_JE (0x4)
|
|
|
|
#define ASM_X86_CC_JNZ (0x5)
|
|
|
|
#define ASM_X86_CC_JNE (0x5)
|
|
|
|
#define ASM_X86_CC_JL (0xc) // less, signed
|
2014-09-29 13:45:42 -04:00
|
|
|
#define ASM_X86_CC_JGE (0xd) // greater or equal, signed
|
|
|
|
#define ASM_X86_CC_JLE (0xe) // less or equal, signed
|
2014-09-29 05:13:49 -04:00
|
|
|
#define ASM_X86_CC_JG (0xf) // greater, signed
|
2014-09-06 18:06:36 -04:00
|
|
|
|
2016-11-27 17:24:50 -05:00
|
|
|
typedef struct _asm_x86_t {
|
|
|
|
mp_asm_base_t base;
|
|
|
|
int num_locals;
|
|
|
|
} asm_x86_t;
|
2014-09-06 18:06:36 -04:00
|
|
|
|
2016-11-27 17:24:50 -05:00
|
|
|
static inline void asm_x86_end_pass(asm_x86_t *as) {
|
|
|
|
(void)as;
|
|
|
|
}
|
2015-04-06 17:38:53 -04:00
|
|
|
|
2014-09-29 13:45:42 -04:00
|
|
|
void asm_x86_mov_r32_r32(asm_x86_t* as, int dest_r32, int src_r32);
|
py: Add support to save native, viper and asm code to .mpy files.
This commit adds support for saving and loading .mpy files that contain
native code (native, viper and inline-asm). A lot of the ground work was
already done for this in the form of removing pointers from generated
native code. The changes here are mainly to link in qstr values to the
native code, and change the format of .mpy files to contain native code
blocks (possibly mixed with bytecode).
A top-level summary:
- @micropython.native, @micropython.viper and @micropython.asm_thumb/
asm_xtensa are now allowed in .py files when compiling to .mpy, and they
work transparently to the user.
- Entire .py files can be compiled to native via mpy-cross -X emit=native
and for the most part the generated .mpy files should work the same as
their bytecode version.
- The .mpy file format is changed to 1) specify in the header if the file
contains native code and if so the architecture (eg x86, ARMV7M, Xtensa);
2) for each function block the kind of code is specified (bytecode,
native, viper, asm).
- When native code is loaded from a .mpy file the native code must be
modified (in place) to link qstr values in, just like bytecode (see
py/persistentcode.c:arch_link_qstr() function).
In addition, this now defines a public, native ABI for dynamically loadable
native code generated by other languages, like C.
2019-02-20 23:18:33 -05:00
|
|
|
size_t asm_x86_mov_i32_to_r32(asm_x86_t *as, int32_t src_i32, int dest_r32);
|
2014-10-12 11:59:29 -04:00
|
|
|
void asm_x86_mov_r8_to_mem8(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp);
|
|
|
|
void asm_x86_mov_r16_to_mem16(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp);
|
|
|
|
void asm_x86_mov_r32_to_mem32(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp);
|
|
|
|
void asm_x86_mov_mem8_to_r32zx(asm_x86_t *as, int src_r32, int src_disp, int dest_r32);
|
|
|
|
void asm_x86_mov_mem16_to_r32zx(asm_x86_t *as, int src_r32, int src_disp, int dest_r32);
|
|
|
|
void asm_x86_mov_mem32_to_r32(asm_x86_t *as, int src_r32, int src_disp, int dest_r32);
|
2014-10-12 09:21:06 -04:00
|
|
|
void asm_x86_and_r32_r32(asm_x86_t *as, int dest_r32, int src_r32);
|
|
|
|
void asm_x86_or_r32_r32(asm_x86_t *as, int dest_r32, int src_r32);
|
2014-09-29 13:45:42 -04:00
|
|
|
void asm_x86_xor_r32_r32(asm_x86_t *as, int dest_r32, int src_r32);
|
|
|
|
void asm_x86_shl_r32_cl(asm_x86_t* as, int dest_r32);
|
|
|
|
void asm_x86_sar_r32_cl(asm_x86_t* as, int dest_r32);
|
|
|
|
void asm_x86_add_r32_r32(asm_x86_t* as, int dest_r32, int src_r32);
|
|
|
|
void asm_x86_sub_r32_r32(asm_x86_t* as, int dest_r32, int src_r32);
|
2015-06-04 10:00:29 -04:00
|
|
|
void asm_x86_mul_r32_r32(asm_x86_t* as, int dest_r32, int src_r32);
|
2014-09-06 18:06:36 -04:00
|
|
|
void asm_x86_cmp_r32_with_r32(asm_x86_t* as, int src_r32_a, int src_r32_b);
|
|
|
|
void asm_x86_test_r8_with_r8(asm_x86_t* as, int src_r32_a, int src_r32_b);
|
2018-08-04 08:03:49 -04:00
|
|
|
void asm_x86_test_r32_with_r32(asm_x86_t* as, int src_r32_a, int src_r32_b);
|
2014-09-06 18:06:36 -04:00
|
|
|
void asm_x86_setcc_r8(asm_x86_t* as, mp_uint_t jcc_type, int dest_r8);
|
2018-08-15 23:45:24 -04:00
|
|
|
void asm_x86_jmp_reg(asm_x86_t *as, int src_r86);
|
2014-09-06 18:06:36 -04:00
|
|
|
void asm_x86_jmp_label(asm_x86_t* as, mp_uint_t label);
|
|
|
|
void asm_x86_jcc_label(asm_x86_t* as, mp_uint_t jcc_type, mp_uint_t label);
|
2018-02-24 07:10:20 -05:00
|
|
|
void asm_x86_entry(asm_x86_t* as, int num_locals);
|
2014-09-06 18:06:36 -04:00
|
|
|
void asm_x86_exit(asm_x86_t* as);
|
2014-09-06 18:38:50 -04:00
|
|
|
void asm_x86_mov_arg_to_r32(asm_x86_t *as, int src_arg_num, int dest_r32);
|
2014-09-06 18:06:36 -04:00
|
|
|
void asm_x86_mov_local_to_r32(asm_x86_t* as, int src_local_num, int dest_r32);
|
|
|
|
void asm_x86_mov_r32_to_local(asm_x86_t* as, int src_r32, int dest_local_num);
|
|
|
|
void asm_x86_mov_local_addr_to_r32(asm_x86_t* as, int local_num, int dest_r32);
|
2018-08-15 23:45:24 -04:00
|
|
|
void asm_x86_mov_reg_pcrel(asm_x86_t *as, int dest_r64, mp_uint_t label);
|
2018-10-12 22:59:19 -04:00
|
|
|
void asm_x86_call_ind(asm_x86_t* as, size_t fun_id, mp_uint_t n_args, int temp_r32);
|
2015-01-01 13:07:43 -05:00
|
|
|
|
2018-10-12 23:53:35 -04:00
|
|
|
// Holds a pointer to mp_fun_table
|
|
|
|
#define ASM_X86_REG_FUN_TABLE ASM_X86_REG_EBP
|
|
|
|
|
2016-12-08 01:47:17 -05:00
|
|
|
#if GENERIC_ASM_API
|
|
|
|
|
|
|
|
// The following macros provide a (mostly) arch-independent API to
|
|
|
|
// generate native code, and are used by the native emitter.
|
|
|
|
|
|
|
|
#define ASM_WORD_SIZE (4)
|
|
|
|
|
|
|
|
#define REG_RET ASM_X86_REG_EAX
|
|
|
|
#define REG_ARG_1 ASM_X86_REG_ARG_1
|
|
|
|
#define REG_ARG_2 ASM_X86_REG_ARG_2
|
|
|
|
#define REG_ARG_3 ASM_X86_REG_ARG_3
|
|
|
|
#define REG_ARG_4 ASM_X86_REG_ARG_4
|
|
|
|
|
|
|
|
// caller-save, so can be used as temporaries
|
|
|
|
#define REG_TEMP0 ASM_X86_REG_EAX
|
|
|
|
#define REG_TEMP1 ASM_X86_REG_ECX
|
|
|
|
#define REG_TEMP2 ASM_X86_REG_EDX
|
|
|
|
|
|
|
|
// callee-save, so can be used as locals
|
|
|
|
#define REG_LOCAL_1 ASM_X86_REG_EBX
|
|
|
|
#define REG_LOCAL_2 ASM_X86_REG_ESI
|
|
|
|
#define REG_LOCAL_3 ASM_X86_REG_EDI
|
|
|
|
#define REG_LOCAL_NUM (3)
|
|
|
|
|
2018-10-12 23:53:35 -04:00
|
|
|
// Holds a pointer to mp_fun_table
|
|
|
|
#define REG_FUN_TABLE ASM_X86_REG_FUN_TABLE
|
|
|
|
|
2016-12-08 01:47:17 -05:00
|
|
|
#define ASM_T asm_x86_t
|
|
|
|
#define ASM_END_PASS asm_x86_end_pass
|
|
|
|
#define ASM_ENTRY asm_x86_entry
|
|
|
|
#define ASM_EXIT asm_x86_exit
|
|
|
|
|
|
|
|
#define ASM_JUMP asm_x86_jmp_label
|
2018-08-04 08:03:49 -04:00
|
|
|
#define ASM_JUMP_IF_REG_ZERO(as, reg, label, bool_test) \
|
2016-12-08 01:47:17 -05:00
|
|
|
do { \
|
2018-08-04 08:03:49 -04:00
|
|
|
if (bool_test) { \
|
|
|
|
asm_x86_test_r8_with_r8(as, reg, reg); \
|
|
|
|
} else { \
|
|
|
|
asm_x86_test_r32_with_r32(as, reg, reg); \
|
|
|
|
} \
|
2016-12-08 01:47:17 -05:00
|
|
|
asm_x86_jcc_label(as, ASM_X86_CC_JZ, label); \
|
|
|
|
} while (0)
|
2018-08-04 08:03:49 -04:00
|
|
|
#define ASM_JUMP_IF_REG_NONZERO(as, reg, label, bool_test) \
|
2016-12-08 01:47:17 -05:00
|
|
|
do { \
|
2018-08-04 08:03:49 -04:00
|
|
|
if (bool_test) { \
|
|
|
|
asm_x86_test_r8_with_r8(as, reg, reg); \
|
|
|
|
} else { \
|
|
|
|
asm_x86_test_r32_with_r32(as, reg, reg); \
|
|
|
|
} \
|
2016-12-08 01:47:17 -05:00
|
|
|
asm_x86_jcc_label(as, ASM_X86_CC_JNZ, label); \
|
|
|
|
} while (0)
|
|
|
|
#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
|
|
|
|
do { \
|
|
|
|
asm_x86_cmp_r32_with_r32(as, reg1, reg2); \
|
|
|
|
asm_x86_jcc_label(as, ASM_X86_CC_JE, label); \
|
|
|
|
} while (0)
|
2018-08-15 23:45:24 -04:00
|
|
|
#define ASM_JUMP_REG(as, reg) asm_x86_jmp_reg((as), (reg))
|
2018-10-13 00:13:55 -04:00
|
|
|
#define ASM_CALL_IND(as, idx) asm_x86_call_ind(as, idx, mp_f_n_args[idx], ASM_X86_REG_EAX)
|
2016-12-08 01:47:17 -05:00
|
|
|
|
2017-11-14 19:46:49 -05:00
|
|
|
#define ASM_MOV_LOCAL_REG(as, local_num, reg_src) asm_x86_mov_r32_to_local((as), (reg_src), (local_num))
|
|
|
|
#define ASM_MOV_REG_IMM(as, reg_dest, imm) asm_x86_mov_i32_to_r32((as), (imm), (reg_dest))
|
py: Add support to save native, viper and asm code to .mpy files.
This commit adds support for saving and loading .mpy files that contain
native code (native, viper and inline-asm). A lot of the ground work was
already done for this in the form of removing pointers from generated
native code. The changes here are mainly to link in qstr values to the
native code, and change the format of .mpy files to contain native code
blocks (possibly mixed with bytecode).
A top-level summary:
- @micropython.native, @micropython.viper and @micropython.asm_thumb/
asm_xtensa are now allowed in .py files when compiling to .mpy, and they
work transparently to the user.
- Entire .py files can be compiled to native via mpy-cross -X emit=native
and for the most part the generated .mpy files should work the same as
their bytecode version.
- The .mpy file format is changed to 1) specify in the header if the file
contains native code and if so the architecture (eg x86, ARMV7M, Xtensa);
2) for each function block the kind of code is specified (bytecode,
native, viper, asm).
- When native code is loaded from a .mpy file the native code must be
modified (in place) to link qstr values in, just like bytecode (see
py/persistentcode.c:arch_link_qstr() function).
In addition, this now defines a public, native ABI for dynamically loadable
native code generated by other languages, like C.
2019-02-20 23:18:33 -05:00
|
|
|
#define ASM_MOV_REG_IMM_FIX_U16(as, reg_dest, imm) asm_x86_mov_i32_to_r32((as), (imm), (reg_dest))
|
|
|
|
#define ASM_MOV_REG_IMM_FIX_WORD(as, reg_dest, imm) asm_x86_mov_i32_to_r32((as), (imm), (reg_dest))
|
2017-11-14 19:46:49 -05:00
|
|
|
#define ASM_MOV_REG_LOCAL(as, reg_dest, local_num) asm_x86_mov_local_to_r32((as), (local_num), (reg_dest))
|
2016-12-08 01:47:17 -05:00
|
|
|
#define ASM_MOV_REG_REG(as, reg_dest, reg_src) asm_x86_mov_r32_r32((as), (reg_dest), (reg_src))
|
2017-11-14 19:46:49 -05:00
|
|
|
#define ASM_MOV_REG_LOCAL_ADDR(as, reg_dest, local_num) asm_x86_mov_local_addr_to_r32((as), (local_num), (reg_dest))
|
2018-08-15 23:45:24 -04:00
|
|
|
#define ASM_MOV_REG_PCREL(as, reg_dest, label) asm_x86_mov_reg_pcrel((as), (reg_dest), (label))
|
2016-12-08 01:47:17 -05:00
|
|
|
|
|
|
|
#define ASM_LSL_REG(as, reg) asm_x86_shl_r32_cl((as), (reg))
|
|
|
|
#define ASM_ASR_REG(as, reg) asm_x86_sar_r32_cl((as), (reg))
|
|
|
|
#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x86_or_r32_r32((as), (reg_dest), (reg_src))
|
|
|
|
#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x86_xor_r32_r32((as), (reg_dest), (reg_src))
|
|
|
|
#define ASM_AND_REG_REG(as, reg_dest, reg_src) asm_x86_and_r32_r32((as), (reg_dest), (reg_src))
|
|
|
|
#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x86_add_r32_r32((as), (reg_dest), (reg_src))
|
|
|
|
#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x86_sub_r32_r32((as), (reg_dest), (reg_src))
|
|
|
|
#define ASM_MUL_REG_REG(as, reg_dest, reg_src) asm_x86_mul_r32_r32((as), (reg_dest), (reg_src))
|
|
|
|
|
|
|
|
#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem32_to_r32((as), (reg_base), 0, (reg_dest))
|
|
|
|
#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))
|
|
|
|
#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem8_to_r32zx((as), (reg_base), 0, (reg_dest))
|
|
|
|
#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem16_to_r32zx((as), (reg_base), 0, (reg_dest))
|
|
|
|
#define ASM_LOAD32_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem32_to_r32((as), (reg_base), 0, (reg_dest))
|
|
|
|
|
|
|
|
#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_x86_mov_r32_to_mem32((as), (reg_src), (reg_base), 0)
|
|
|
|
#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))
|
|
|
|
#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x86_mov_r8_to_mem8((as), (reg_src), (reg_base), 0)
|
|
|
|
#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_x86_mov_r16_to_mem16((as), (reg_src), (reg_base), 0)
|
|
|
|
#define ASM_STORE32_REG_REG(as, reg_src, reg_base) asm_x86_mov_r32_to_mem32((as), (reg_src), (reg_base), 0)
|
|
|
|
|
|
|
|
#endif // GENERIC_ASM_API
|
|
|
|
|
all: Unify header guard usage.
The code conventions suggest using header guards, but do not define how
those should look like and instead point to existing files. However, not
all existing files follow the same scheme, sometimes omitting header guards
altogether, sometimes using non-standard names, making it easy to
accidentally pick a "wrong" example.
This commit ensures that all header files of the MicroPython project (that
were not simply copied from somewhere else) follow the same pattern, that
was already present in the majority of files, especially in the py folder.
The rules are as follows.
Naming convention:
* start with the words MICROPY_INCLUDED
* contain the full path to the file
* replace special characters with _
In addition, there are no empty lines before #ifndef, between #ifndef and
one empty line before #endif. #endif is followed by a comment containing
the name of the guard macro.
py/grammar.h cannot use header guards by design, since it has to be
included multiple times in a single C file. Several other files also do not
need header guards as they are only used internally and guaranteed to be
included only once:
* MICROPY_MPHALPORT_H
* mpconfigboard.h
* mpconfigport.h
* mpthreadport.h
* pin_defs_*.h
* qstrdefs*.h
2017-06-29 17:14:58 -04:00
|
|
|
#endif // MICROPY_INCLUDED_PY_ASMX86_H
|