blob: 544786cf7fb3bae1fbd372203df1050db607d340 [file] [log] [blame]
Damien Georgec90f59e2014-09-06 23:06:36 +01001/*
2 * This file is part of the Micro Python 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 */
Damien Georgeebde3c62015-01-01 18:07:43 +000026#ifndef __MICROPY_INCLUDED_PY_ASMX86_H__
27#define __MICROPY_INCLUDED_PY_ASMX86_H__
28
29#include "py/mpconfig.h"
30#include "py/misc.h"
Damien Georgec90f59e2014-09-06 23:06:36 +010031
Damien George03281b32014-09-06 22:38:50 +000032// x86 cdecl calling convention is:
33// - args passed on the stack in reverse order
34// - return value in EAX
35// - caller cleans up the stack after a call
36// - stack must be aligned to 16-byte boundary before all calls
37// - EAX, ECX, EDX are caller-save
38// - EBX, ESI, EDI, EBP, ESP, EIP are callee-save
39
Damien George3112cde2014-09-29 18:45:42 +010040// In the functions below, argument order follows x86 docs and generally
41// the destination is the first argument.
42// NOTE: this is a change from the old convention used in this file and
43// some functions still use the old (reverse) convention.
44
Damien Georgec90f59e2014-09-06 23:06:36 +010045#define ASM_X86_PASS_COMPUTE (1)
46#define ASM_X86_PASS_EMIT (2)
47
Damien George0b610de2014-09-29 16:25:04 +010048#define ASM_X86_REG_EAX (0)
49#define ASM_X86_REG_ECX (1)
50#define ASM_X86_REG_EDX (2)
51#define ASM_X86_REG_EBX (3)
52#define ASM_X86_REG_ESP (4)
53#define ASM_X86_REG_EBP (5)
54#define ASM_X86_REG_ESI (6)
55#define ASM_X86_REG_EDI (7)
Damien Georgec90f59e2014-09-06 23:06:36 +010056
Damien George6eae8612014-09-08 22:16:35 +000057// x86 passes values on the stack, but the emitter is register based, so we need
58// to define registers that can temporarily hold the function arguments. They
59// need to be defined here so that asm_x86_call_ind can push them onto the stack
60// before the call.
Damien George0b610de2014-09-29 16:25:04 +010061#define ASM_X86_REG_ARG_1 ASM_X86_REG_EAX
62#define ASM_X86_REG_ARG_2 ASM_X86_REG_ECX
63#define ASM_X86_REG_ARG_3 ASM_X86_REG_EDX
Damien George6eae8612014-09-08 22:16:35 +000064
Damien Georgec90f59e2014-09-06 23:06:36 +010065// condition codes, used for jcc and setcc (despite their j-name!)
66#define ASM_X86_CC_JB (0x2) // below, 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_JL (0xc) // less, signed
Damien George3112cde2014-09-29 18:45:42 +010072#define ASM_X86_CC_JGE (0xd) // greater or equal, signed
73#define ASM_X86_CC_JLE (0xe) // less or equal, signed
Damien Georged66e4862014-09-29 10:13:49 +010074#define ASM_X86_CC_JG (0xf) // greater, signed
Damien Georgec90f59e2014-09-06 23:06:36 +010075
Damien Georgec90f59e2014-09-06 23:06:36 +010076typedef struct _asm_x86_t asm_x86_t;
77
78asm_x86_t* asm_x86_new(mp_uint_t max_num_labels);
79void asm_x86_free(asm_x86_t* as, bool free_code);
80void asm_x86_start_pass(asm_x86_t *as, mp_uint_t pass);
81void asm_x86_end_pass(asm_x86_t *as);
82mp_uint_t asm_x86_get_code_size(asm_x86_t* as);
83void* asm_x86_get_code(asm_x86_t* as);
84
Damien George3112cde2014-09-29 18:45:42 +010085void asm_x86_mov_r32_r32(asm_x86_t* as, int dest_r32, int src_r32);
Damien Georgec90f59e2014-09-06 23:06:36 +010086void asm_x86_mov_i32_to_r32(asm_x86_t *as, int32_t src_i32, int dest_r32);
87void asm_x86_mov_i32_to_r32_aligned(asm_x86_t *as, int32_t src_i32, int dest_r32);
Damien George91cfd412014-10-12 16:59:29 +010088void asm_x86_mov_r8_to_mem8(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp);
89void asm_x86_mov_r16_to_mem16(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp);
90void asm_x86_mov_r32_to_mem32(asm_x86_t *as, int src_r32, int dest_r32, int dest_disp);
91void asm_x86_mov_mem8_to_r32zx(asm_x86_t *as, int src_r32, int src_disp, int dest_r32);
92void asm_x86_mov_mem16_to_r32zx(asm_x86_t *as, int src_r32, int src_disp, int dest_r32);
93void asm_x86_mov_mem32_to_r32(asm_x86_t *as, int src_r32, int src_disp, int dest_r32);
Damien George1ef23482014-10-12 14:21:06 +010094void asm_x86_and_r32_r32(asm_x86_t *as, int dest_r32, int src_r32);
95void asm_x86_or_r32_r32(asm_x86_t *as, int dest_r32, int src_r32);
Damien George3112cde2014-09-29 18:45:42 +010096void asm_x86_xor_r32_r32(asm_x86_t *as, int dest_r32, int src_r32);
97void asm_x86_shl_r32_cl(asm_x86_t* as, int dest_r32);
98void asm_x86_sar_r32_cl(asm_x86_t* as, int dest_r32);
99void asm_x86_add_r32_r32(asm_x86_t* as, int dest_r32, int src_r32);
100void asm_x86_sub_r32_r32(asm_x86_t* as, int dest_r32, int src_r32);
Damien Georgec90f59e2014-09-06 23:06:36 +0100101void asm_x86_cmp_r32_with_r32(asm_x86_t* as, int src_r32_a, int src_r32_b);
102void asm_x86_test_r8_with_r8(asm_x86_t* as, int src_r32_a, int src_r32_b);
103void asm_x86_setcc_r8(asm_x86_t* as, mp_uint_t jcc_type, int dest_r8);
104void asm_x86_label_assign(asm_x86_t* as, mp_uint_t label);
105void asm_x86_jmp_label(asm_x86_t* as, mp_uint_t label);
106void asm_x86_jcc_label(asm_x86_t* as, mp_uint_t jcc_type, mp_uint_t label);
107void asm_x86_entry(asm_x86_t* as, mp_uint_t num_locals);
108void asm_x86_exit(asm_x86_t* as);
Damien George03281b32014-09-06 22:38:50 +0000109void asm_x86_mov_arg_to_r32(asm_x86_t *as, int src_arg_num, int dest_r32);
Damien Georgec90f59e2014-09-06 23:06:36 +0100110void asm_x86_mov_local_to_r32(asm_x86_t* as, int src_local_num, int dest_r32);
111void asm_x86_mov_r32_to_local(asm_x86_t* as, int src_r32, int dest_local_num);
112void asm_x86_mov_local_addr_to_r32(asm_x86_t* as, int local_num, int dest_r32);
113void asm_x86_call_ind(asm_x86_t* as, void* ptr, mp_uint_t n_args, int temp_r32);
Damien Georgeebde3c62015-01-01 18:07:43 +0000114
115#endif // __MICROPY_INCLUDED_PY_ASMX86_H__