blob: 6099325c7e3f0b69b2e42aa16210be705537770d [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 */
26
Damien George03281b32014-09-06 22:38:50 +000027// x86 cdecl calling convention is:
28// - args passed on the stack in reverse order
29// - return value in EAX
30// - caller cleans up the stack after a call
31// - stack must be aligned to 16-byte boundary before all calls
32// - EAX, ECX, EDX are caller-save
33// - EBX, ESI, EDI, EBP, ESP, EIP are callee-save
34
Damien Georgec90f59e2014-09-06 23:06:36 +010035#define ASM_X86_PASS_COMPUTE (1)
36#define ASM_X86_PASS_EMIT (2)
37
38#define REG_EAX (0)
39#define REG_ECX (1)
40#define REG_EDX (2)
41#define REG_EBX (3)
42#define REG_ESP (4)
43#define REG_EBP (5)
44#define REG_ESI (6)
45#define REG_EDI (7)
46
Damien George6eae8612014-09-08 22:16:35 +000047// x86 passes values on the stack, but the emitter is register based, so we need
48// to define registers that can temporarily hold the function arguments. They
49// need to be defined here so that asm_x86_call_ind can push them onto the stack
50// before the call.
51#define ASM_X86_REG_ARG_1 REG_EAX
52#define ASM_X86_REG_ARG_2 REG_ECX
53#define ASM_X86_REG_ARG_3 REG_EDX
54
Damien Georgec90f59e2014-09-06 23:06:36 +010055// condition codes, used for jcc and setcc (despite their j-name!)
56#define ASM_X86_CC_JB (0x2) // below, unsigned
57#define ASM_X86_CC_JZ (0x4)
58#define ASM_X86_CC_JE (0x4)
59#define ASM_X86_CC_JNZ (0x5)
60#define ASM_X86_CC_JNE (0x5)
61#define ASM_X86_CC_JL (0xc) // less, signed
62
Damien Georgec90f59e2014-09-06 23:06:36 +010063typedef struct _asm_x86_t asm_x86_t;
64
65asm_x86_t* asm_x86_new(mp_uint_t max_num_labels);
66void asm_x86_free(asm_x86_t* as, bool free_code);
67void asm_x86_start_pass(asm_x86_t *as, mp_uint_t pass);
68void asm_x86_end_pass(asm_x86_t *as);
69mp_uint_t asm_x86_get_code_size(asm_x86_t* as);
70void* asm_x86_get_code(asm_x86_t* as);
71
72void asm_x86_mov_r32_to_r32(asm_x86_t* as, int src_r32, int dest_r32);
73void asm_x86_mov_i32_to_r32(asm_x86_t *as, int32_t src_i32, int dest_r32);
74void asm_x86_mov_i32_to_r32_aligned(asm_x86_t *as, int32_t src_i32, int dest_r32);
75void asm_x86_xor_r32_to_r32(asm_x86_t *as, int src_r32, int dest_r32);
76void asm_x86_add_r32_to_r32(asm_x86_t* as, int src_r32, int dest_r32);
77void asm_x86_cmp_r32_with_r32(asm_x86_t* as, int src_r32_a, int src_r32_b);
78void asm_x86_test_r8_with_r8(asm_x86_t* as, int src_r32_a, int src_r32_b);
79void asm_x86_setcc_r8(asm_x86_t* as, mp_uint_t jcc_type, int dest_r8);
80void asm_x86_label_assign(asm_x86_t* as, mp_uint_t label);
81void asm_x86_jmp_label(asm_x86_t* as, mp_uint_t label);
82void asm_x86_jcc_label(asm_x86_t* as, mp_uint_t jcc_type, mp_uint_t label);
83void asm_x86_entry(asm_x86_t* as, mp_uint_t num_locals);
84void asm_x86_exit(asm_x86_t* as);
Damien George03281b32014-09-06 22:38:50 +000085void asm_x86_mov_arg_to_r32(asm_x86_t *as, int src_arg_num, int dest_r32);
Damien Georgec90f59e2014-09-06 23:06:36 +010086void asm_x86_mov_local_to_r32(asm_x86_t* as, int src_local_num, int dest_r32);
87void asm_x86_mov_r32_to_local(asm_x86_t* as, int src_r32, int dest_local_num);
88void asm_x86_mov_local_addr_to_r32(asm_x86_t* as, int local_num, int dest_r32);
89void asm_x86_call_ind(asm_x86_t* as, void* ptr, mp_uint_t n_args, int temp_r32);