blob: 1d5d6c7c21de72c8e2eada36bff96be2a224e5a7 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 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 George36db6bc2014-05-07 17:24:22 +010027#define ASM_X64_PASS_COMPUTE (1)
28#define ASM_X64_PASS_EMIT (2)
Damien429d7192013-10-04 19:53:11 +010029
30#define REG_RAX (0)
31#define REG_RCX (1)
32#define REG_RDX (2)
33#define REG_RBX (3)
34#define REG_RSP (4)
35#define REG_RBP (5)
36#define REG_RSI (6)
37#define REG_RDI (7)
38
Paul Sokolovsky2efbc622013-12-30 21:03:41 +020039// condition codes, used for jcc and setcc (despite their j-name!)
Damien429d7192013-10-04 19:53:11 +010040#define JCC_JB (0x2) // below, unsigned
41#define JCC_JZ (0x4)
42#define JCC_JE (0x4)
43#define JCC_JNZ (0x5)
44#define JCC_JNE (0x5)
45#define JCC_JL (0xc) // less, signed
46
47#define REG_RET REG_RAX
48#define REG_ARG_1 REG_RDI
49#define REG_ARG_2 REG_RSI
50#define REG_ARG_3 REG_RDX
51
52typedef struct _asm_x64_t asm_x64_t;
53
Damienb05d7072013-10-05 13:37:10 +010054asm_x64_t* asm_x64_new(uint max_num_labels);
Damien429d7192013-10-04 19:53:11 +010055void asm_x64_free(asm_x64_t* as, bool free_code);
Damien George36db6bc2014-05-07 17:24:22 +010056void asm_x64_start_pass(asm_x64_t *as, uint pass);
Damien429d7192013-10-04 19:53:11 +010057void asm_x64_end_pass(asm_x64_t *as);
58uint asm_x64_get_code_size(asm_x64_t* as);
59void* asm_x64_get_code(asm_x64_t* as);
60
61void asm_x64_nop(asm_x64_t* as);
62void asm_x64_push_r64(asm_x64_t* as, int src_r64);
63void asm_x64_push_i32(asm_x64_t* as, int src_i32); // will be sign extended to 64 bits
64void asm_x64_push_disp(asm_x64_t* as, int src_r32, int src_offset);
65void asm_x64_pop_r64(asm_x64_t* as, int dest_r64);
66void asm_x64_mov_r64_to_r64(asm_x64_t* as, int src_r64, int dest_r64);
67void asm_x64_mov_r32_to_disp(asm_x64_t* as, int src_r32, int dest_r32, int dest_disp);
68void asm_x64_mov_disp_to_r32(asm_x64_t* as, int src_r32, int src_disp, int dest_r32);
69void asm_x64_mov_i32_to_r64(asm_x64_t* as, int src_i32, int dest_r64);
70void asm_x64_mov_i64_to_r64(asm_x64_t* as, int64_t src_i64, int dest_r64);
71void asm_x64_mov_i32_to_disp(asm_x64_t* as, int src_i32, int dest_r32, int dest_disp);
72void asm_x64_mov_i64_to_r64_optimised(asm_x64_t *as, int64_t src_i64, int dest_r64);
Damien George36db6bc2014-05-07 17:24:22 +010073void asm_x64_mov_i64_to_r64_aligned(asm_x64_t *as, int64_t src_i64, int dest_r64);
Damien429d7192013-10-04 19:53:11 +010074void asm_x64_xor_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64);
75void asm_x64_add_r64_to_r64(asm_x64_t* as, int src_r64, int dest_r64);
76void asm_x64_add_i32_to_r32(asm_x64_t* as, int src_i32, int dest_r32);
77void asm_x64_sub_r32_from_r32(asm_x64_t* as, int src_r32, int dest_r32);
78void asm_x64_sub_i32_from_r32(asm_x64_t* as, int src_i32, int dest_r32);
79void asm_x64_shl_r32_by_imm(asm_x64_t* as, int r32, int imm);
80void asm_x64_shr_r32_by_imm(asm_x64_t* as, int r32, int imm);
81void asm_x64_sar_r32_by_imm(asm_x64_t* as, int r32, int imm);
82void asm_x64_cmp_r64_with_r64(asm_x64_t* as, int src_r64_a, int src_r64_b);
83void asm_x64_cmp_r32_with_disp(asm_x64_t* as, int src_r32_a, int src_r32_b, int src_disp_b);
84void asm_x64_cmp_disp_with_r32(asm_x64_t* as, int src_r32_a, int src_disp_a, int src_r32_b);
85void asm_x64_cmp_i32_with_r32(asm_x64_t* as, int src_i32, int src_r32);
86void asm_x64_test_r8_with_r8(asm_x64_t* as, int src_r64_a, int src_r64_b);
87void asm_x64_setcc_r8(asm_x64_t* as, int jcc_type, int dest_r8);
Damien429d7192013-10-04 19:53:11 +010088void asm_x64_label_assign(asm_x64_t* as, int label);
89void asm_x64_jmp_label(asm_x64_t* as, int label);
90void asm_x64_jcc_label(asm_x64_t* as, int jcc_type, int label);
91void asm_x64_entry(asm_x64_t* as, int num_locals);
92void asm_x64_exit(asm_x64_t* as);
93void asm_x64_push_arg(asm_x64_t* as, int src_arg_num);
94void asm_x64_mov_arg_to_r32(asm_x64_t* as, int src_arg_num, int dest_r32);
95void asm_x64_mov_r32_to_arg(asm_x64_t* as, int src_r32, int dest_arg_num);
96void asm_x64_mov_local_to_r64(asm_x64_t* as, int src_local_num, int dest_r64);
97void asm_x64_mov_r64_to_local(asm_x64_t* as, int src_r64, int dest_local_num);
98void asm_x64_mov_local_addr_to_r64(asm_x64_t* as, int local_num, int dest_r64);
99void asm_x64_push_local(asm_x64_t* as, int local_num);
100void asm_x64_push_local_addr(asm_x64_t* as, int local_num, int temp_r32);
101void asm_x64_call_ind(asm_x64_t* as, void* ptr, int temp_r32);