Numworks Epsilon  1.4.1
Graphing Calculator Operating System
asmx64.h
Go to the documentation of this file.
1 /*
2  * This file is part of the MicroPython 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 #ifndef MICROPY_INCLUDED_PY_ASMX64_H
27 #define MICROPY_INCLUDED_PY_ASMX64_H
28 
29 #include "py/mpconfig.h"
30 #include "py/misc.h"
31 #include "py/asmbase.h"
32 
33 // AMD64 calling convention is:
34 // - args pass in: RDI, RSI, RDX, RCX, R08, R09
35 // - return value in RAX
36 // - stack must be aligned on a 16-byte boundary before all calls
37 // - RAX, RCX, RDX, RSI, RDI, R08, R09, R10, R11 are caller-save
38 // - RBX, RBP, R12, R13, R14, R15 are callee-save
39 
40 // 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 
45 #define ASM_X64_REG_RAX (0)
46 #define ASM_X64_REG_RCX (1)
47 #define ASM_X64_REG_RDX (2)
48 #define ASM_X64_REG_RBX (3)
49 #define ASM_X64_REG_RSP (4)
50 #define ASM_X64_REG_RBP (5)
51 #define ASM_X64_REG_RSI (6)
52 #define ASM_X64_REG_RDI (7)
53 #define ASM_X64_REG_R08 (8)
54 #define ASM_X64_REG_R09 (9)
55 #define ASM_X64_REG_R10 (10)
56 #define ASM_X64_REG_R11 (11)
57 #define ASM_X64_REG_R12 (12)
58 #define ASM_X64_REG_R13 (13)
59 #define ASM_X64_REG_R14 (14)
60 #define ASM_X64_REG_R15 (15)
61 
62 // condition codes, used for jcc and setcc (despite their j-name!)
63 #define ASM_X64_CC_JB (0x2) // below, unsigned
64 #define ASM_X64_CC_JZ (0x4)
65 #define ASM_X64_CC_JE (0x4)
66 #define ASM_X64_CC_JNZ (0x5)
67 #define ASM_X64_CC_JNE (0x5)
68 #define ASM_X64_CC_JL (0xc) // less, signed
69 #define ASM_X64_CC_JGE (0xd) // greater or equal, signed
70 #define ASM_X64_CC_JLE (0xe) // less or equal, signed
71 #define ASM_X64_CC_JG (0xf) // greater, signed
72 
73 typedef struct _asm_x64_t {
76 } asm_x64_t;
77 
78 static inline void asm_x64_end_pass(asm_x64_t *as) {
79  (void)as;
80 }
81 
82 void asm_x64_nop(asm_x64_t* as);
83 void asm_x64_push_r64(asm_x64_t* as, int src_r64);
84 void asm_x64_pop_r64(asm_x64_t* as, int dest_r64);
85 void asm_x64_mov_r64_r64(asm_x64_t* as, int dest_r64, int src_r64);
86 void asm_x64_mov_i64_to_r64(asm_x64_t* as, int64_t src_i64, int dest_r64);
87 void asm_x64_mov_i64_to_r64_optimised(asm_x64_t *as, int64_t src_i64, int dest_r64);
88 void asm_x64_mov_i64_to_r64_aligned(asm_x64_t *as, int64_t src_i64, int dest_r64);
89 void asm_x64_mov_r8_to_mem8(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp);
90 void asm_x64_mov_r16_to_mem16(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp);
91 void asm_x64_mov_r32_to_mem32(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp);
92 void asm_x64_mov_r64_to_mem64(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp);
93 void asm_x64_mov_mem8_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64);
94 void asm_x64_mov_mem16_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64);
95 void asm_x64_mov_mem32_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64);
96 void asm_x64_mov_mem64_to_r64(asm_x64_t *as, int src_r64, int src_disp, int dest_r64);
97 void asm_x64_and_r64_r64(asm_x64_t *as, int dest_r64, int src_r64);
98 void asm_x64_or_r64_r64(asm_x64_t *as, int dest_r64, int src_r64);
99 void asm_x64_xor_r64_r64(asm_x64_t *as, int dest_r64, int src_r64);
100 void asm_x64_shl_r64_cl(asm_x64_t* as, int dest_r64);
101 void asm_x64_sar_r64_cl(asm_x64_t* as, int dest_r64);
102 void asm_x64_add_r64_r64(asm_x64_t* as, int dest_r64, int src_r64);
103 void asm_x64_sub_r64_r64(asm_x64_t* as, int dest_r64, int src_r64);
104 void asm_x64_mul_r64_r64(asm_x64_t* as, int dest_r64, int src_r64);
105 void asm_x64_cmp_r64_with_r64(asm_x64_t* as, int src_r64_a, int src_r64_b);
106 void asm_x64_test_r8_with_r8(asm_x64_t* as, int src_r64_a, int src_r64_b);
107 void asm_x64_setcc_r8(asm_x64_t* as, int jcc_type, int dest_r8);
108 void asm_x64_jmp_label(asm_x64_t* as, mp_uint_t label);
109 void asm_x64_jcc_label(asm_x64_t* as, int jcc_type, mp_uint_t label);
110 void asm_x64_entry(asm_x64_t* as, int num_locals);
111 void asm_x64_exit(asm_x64_t* as);
112 void asm_x64_mov_local_to_r64(asm_x64_t* as, int src_local_num, int dest_r64);
113 void asm_x64_mov_r64_to_local(asm_x64_t* as, int src_r64, int dest_local_num);
114 void asm_x64_mov_local_addr_to_r64(asm_x64_t* as, int local_num, int dest_r64);
115 void asm_x64_call_ind(asm_x64_t* as, void* ptr, int temp_r32);
116 
117 #if GENERIC_ASM_API
118 
119 // The following macros provide a (mostly) arch-independent API to
120 // generate native code, and are used by the native emitter.
121 
122 #define ASM_WORD_SIZE (8)
123 
124 #define REG_RET ASM_X64_REG_RAX
125 #define REG_ARG_1 ASM_X64_REG_RDI
126 #define REG_ARG_2 ASM_X64_REG_RSI
127 #define REG_ARG_3 ASM_X64_REG_RDX
128 #define REG_ARG_4 ASM_X64_REG_RCX
129 #define REG_ARG_5 ASM_X64_REG_R08
130 
131 // caller-save
132 #define REG_TEMP0 ASM_X64_REG_RAX
133 #define REG_TEMP1 ASM_X64_REG_RDI
134 #define REG_TEMP2 ASM_X64_REG_RSI
135 
136 // callee-save
137 #define REG_LOCAL_1 ASM_X64_REG_RBX
138 #define REG_LOCAL_2 ASM_X64_REG_R12
139 #define REG_LOCAL_3 ASM_X64_REG_R13
140 #define REG_LOCAL_NUM (3)
141 
142 #define ASM_T asm_x64_t
143 #define ASM_END_PASS asm_x64_end_pass
144 #define ASM_ENTRY asm_x64_entry
145 #define ASM_EXIT asm_x64_exit
146 
147 #define ASM_JUMP asm_x64_jmp_label
148 #define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
149  do { \
150  asm_x64_test_r8_with_r8(as, reg, reg); \
151  asm_x64_jcc_label(as, ASM_X64_CC_JZ, label); \
152  } while (0)
153 #define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
154  do { \
155  asm_x64_test_r8_with_r8(as, reg, reg); \
156  asm_x64_jcc_label(as, ASM_X64_CC_JNZ, label); \
157  } while (0)
158 #define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
159  do { \
160  asm_x64_cmp_r64_with_r64(as, reg1, reg2); \
161  asm_x64_jcc_label(as, ASM_X64_CC_JE, label); \
162  } while (0)
163 #define ASM_CALL_IND(as, ptr, idx) asm_x64_call_ind(as, ptr, ASM_X64_REG_RAX)
164 
165 #define ASM_MOV_REG_TO_LOCAL asm_x64_mov_r64_to_local
166 #define ASM_MOV_IMM_TO_REG asm_x64_mov_i64_to_r64_optimised
167 #define ASM_MOV_ALIGNED_IMM_TO_REG asm_x64_mov_i64_to_r64_aligned
168 #define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
169  do { \
170  asm_x64_mov_i64_to_r64_optimised(as, (imm), (reg_temp)); \
171  asm_x64_mov_r64_to_local(as, (reg_temp), (local_num)); \
172  } while (false)
173 #define ASM_MOV_LOCAL_TO_REG asm_x64_mov_local_to_r64
174 #define ASM_MOV_REG_REG(as, reg_dest, reg_src) asm_x64_mov_r64_r64((as), (reg_dest), (reg_src))
175 #define ASM_MOV_LOCAL_ADDR_TO_REG asm_x64_mov_local_addr_to_r64
176 
177 #define ASM_LSL_REG(as, reg) asm_x64_shl_r64_cl((as), (reg))
178 #define ASM_ASR_REG(as, reg) asm_x64_sar_r64_cl((as), (reg))
179 #define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x64_or_r64_r64((as), (reg_dest), (reg_src))
180 #define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x64_xor_r64_r64((as), (reg_dest), (reg_src))
181 #define ASM_AND_REG_REG(as, reg_dest, reg_src) asm_x64_and_r64_r64((as), (reg_dest), (reg_src))
182 #define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x64_add_r64_r64((as), (reg_dest), (reg_src))
183 #define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x64_sub_r64_r64((as), (reg_dest), (reg_src))
184 #define ASM_MUL_REG_REG(as, reg_dest, reg_src) asm_x64_mul_r64_r64((as), (reg_dest), (reg_src))
185 
186 #define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem64_to_r64((as), (reg_base), 0, (reg_dest))
187 #define ASM_LOAD_REG_REG_OFFSET(as, reg_dest, reg_base, word_offset) asm_x64_mov_mem64_to_r64((as), (reg_base), 8 * (word_offset), (reg_dest))
188 #define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem8_to_r64zx((as), (reg_base), 0, (reg_dest))
189 #define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem16_to_r64zx((as), (reg_base), 0, (reg_dest))
190 #define ASM_LOAD32_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem32_to_r64zx((as), (reg_base), 0, (reg_dest))
191 
192 #define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_x64_mov_r64_to_mem64((as), (reg_src), (reg_base), 0)
193 #define ASM_STORE_REG_REG_OFFSET(as, reg_src, reg_base, word_offset) asm_x64_mov_r64_to_mem64((as), (reg_src), (reg_base), 8 * (word_offset))
194 #define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x64_mov_r8_to_mem8((as), (reg_src), (reg_base), 0)
195 #define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_x64_mov_r16_to_mem16((as), (reg_src), (reg_base), 0)
196 #define ASM_STORE32_REG_REG(as, reg_src, reg_base) asm_x64_mov_r32_to_mem32((as), (reg_src), (reg_base), 0)
197 
198 #endif // GENERIC_ASM_API
199 
200 #endif // MICROPY_INCLUDED_PY_ASMX64_H
void asm_x64_shl_r64_cl(asm_x64_t *as, int dest_r64)
void asm_x64_cmp_r64_with_r64(asm_x64_t *as, int src_r64_a, int src_r64_b)
uintptr_t mp_uint_t
Definition: mpconfigport.h:74
void asm_x64_mov_mem8_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64)
void asm_x64_mov_r64_to_local(asm_x64_t *as, int src_r64, int dest_local_num)
void asm_x64_sub_r64_r64(asm_x64_t *as, int dest_r64, int src_r64)
void asm_x64_call_ind(asm_x64_t *as, void *ptr, int temp_r32)
void asm_x64_mov_r32_to_mem32(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp)
void asm_x64_jcc_label(asm_x64_t *as, int jcc_type, mp_uint_t label)
void asm_x64_mul_r64_r64(asm_x64_t *as, int dest_r64, int src_r64)
void asm_x64_pop_r64(asm_x64_t *as, int dest_r64)
void asm_x64_setcc_r8(asm_x64_t *as, int jcc_type, int dest_r8)
void asm_x64_nop(asm_x64_t *as)
void asm_x64_mov_i64_to_r64(asm_x64_t *as, int64_t src_i64, int dest_r64)
void asm_x64_mov_r64_r64(asm_x64_t *as, int dest_r64, int src_r64)
void asm_x64_add_r64_r64(asm_x64_t *as, int dest_r64, int src_r64)
void asm_x64_entry(asm_x64_t *as, int num_locals)
struct _asm_x64_t asm_x64_t
void asm_x64_mov_i64_to_r64_optimised(asm_x64_t *as, int64_t src_i64, int dest_r64)
void asm_x64_xor_r64_r64(asm_x64_t *as, int dest_r64, int src_r64)
void asm_x64_push_r64(asm_x64_t *as, int src_r64)
void asm_x64_test_r8_with_r8(asm_x64_t *as, int src_r64_a, int src_r64_b)
void asm_x64_exit(asm_x64_t *as)
void asm_x64_jmp_label(asm_x64_t *as, mp_uint_t label)
void asm_x64_mov_r16_to_mem16(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp)
void asm_x64_mov_local_addr_to_r64(asm_x64_t *as, int local_num, int dest_r64)
void asm_x64_mov_mem64_to_r64(asm_x64_t *as, int src_r64, int src_disp, int dest_r64)
void asm_x64_sar_r64_cl(asm_x64_t *as, int dest_r64)
void asm_x64_mov_mem32_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64)
void asm_x64_mov_r8_to_mem8(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp)
int num_locals
Definition: asmx64.h:75
void asm_x64_mov_i64_to_r64_aligned(asm_x64_t *as, int64_t src_i64, int dest_r64)
void asm_x64_mov_mem16_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64)
signed long long int64_t
Definition: stdint.h:12
mp_asm_base_t base
Definition: asmx64.h:74
void asm_x64_or_r64_r64(asm_x64_t *as, int dest_r64, int src_r64)
void asm_x64_and_r64_r64(asm_x64_t *as, int dest_r64, int src_r64)
void asm_x64_mov_r64_to_mem64(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp)
void asm_x64_mov_local_to_r64(asm_x64_t *as, int src_local_num, int dest_r64)