blob: 3f111781f266526905ecf3bc23ce642938e8fdc8 [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
Markus Siemens19ccc6b2014-01-27 22:53:28 +010027#include <stdint.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdio.h>
29#include <assert.h>
Damien429d7192013-10-04 19:53:11 +010030#include <string.h>
31
Damien Georgee67ed5d2014-01-04 13:55:24 +000032#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030033#include "misc.h"
Damien Georgee67ed5d2014-01-04 13:55:24 +000034
35// wrapper around everything in this file
36#if MICROPY_EMIT_X64
Damien429d7192013-10-04 19:53:11 +010037
Damien Georged3ebe482014-01-07 15:20:33 +000038#include "asmx64.h"
39
Damien429d7192013-10-04 19:53:11 +010040/* all offsets are measured in multiples of 8 bytes */
41#define WORD_SIZE (8)
42
43#define OPCODE_NOP (0x90)
Damien George81057362014-09-07 01:06:19 +010044#define OPCODE_PUSH_R64 (0x50) /* +rq */
Damien429d7192013-10-04 19:53:11 +010045#define OPCODE_PUSH_I64 (0x68)
46#define OPCODE_PUSH_M64 (0xff) /* /6 */
Damien George81057362014-09-07 01:06:19 +010047#define OPCODE_POP_R64 (0x58) /* +rq */
Damien429d7192013-10-04 19:53:11 +010048#define OPCODE_RET (0xc3)
49#define OPCODE_MOV_I8_TO_R8 (0xb0) /* +rb */
Damien George81057362014-09-07 01:06:19 +010050#define OPCODE_MOV_I64_TO_R64 (0xb8) /* +rq */
Damien429d7192013-10-04 19:53:11 +010051#define OPCODE_MOV_I32_TO_RM32 (0xc7)
Damien Georged66e4862014-09-29 10:13:49 +010052#define OPCODE_MOV_R8_TO_RM8 (0x88) /* /r */
Damien George81057362014-09-07 01:06:19 +010053#define OPCODE_MOV_R64_TO_RM64 (0x89) /* /r */
Damien429d7192013-10-04 19:53:11 +010054#define OPCODE_MOV_RM64_TO_R64 (0x8b)
55#define OPCODE_LEA_MEM_TO_R64 (0x8d) /* /r */
56#define OPCODE_XOR_R64_TO_RM64 (0x31) /* /r */
Damien George3112cde2014-09-29 18:45:42 +010057#define OPCODE_ADD_R64_TO_RM64 (0x01) /* /r */
Damien429d7192013-10-04 19:53:11 +010058#define OPCODE_ADD_I32_TO_RM32 (0x81) /* /0 */
59#define OPCODE_ADD_I8_TO_RM32 (0x83) /* /0 */
60#define OPCODE_SUB_R64_FROM_RM64 (0x29)
61#define OPCODE_SUB_I32_FROM_RM64 (0x81) /* /5 */
62#define OPCODE_SUB_I8_FROM_RM64 (0x83) /* /5 */
Damien George3112cde2014-09-29 18:45:42 +010063//#define OPCODE_SHL_RM32_BY_I8 (0xc1) /* /4 */
64//#define OPCODE_SHR_RM32_BY_I8 (0xc1) /* /5 */
65//#define OPCODE_SAR_RM32_BY_I8 (0xc1) /* /7 */
66#define OPCODE_SHL_RM64_CL (0xd3) /* /4 */
67#define OPCODE_SAR_RM64_CL (0xd3) /* /7 */
68//#define OPCODE_CMP_I32_WITH_RM32 (0x81) /* /7 */
69//#define OPCODE_CMP_I8_WITH_RM32 (0x83) /* /7 */
70#define OPCODE_CMP_R64_WITH_RM64 (0x39) /* /r */
71//#define OPCODE_CMP_RM32_WITH_R32 (0x3b)
Damien429d7192013-10-04 19:53:11 +010072#define OPCODE_TEST_R8_WITH_RM8 (0x84) /* /r */
73#define OPCODE_JMP_REL8 (0xeb)
74#define OPCODE_JMP_REL32 (0xe9)
75#define OPCODE_JCC_REL8 (0x70) /* | jcc type */
76#define OPCODE_JCC_REL32_A (0x0f)
77#define OPCODE_JCC_REL32_B (0x80) /* | jcc type */
78#define OPCODE_SETCC_RM8_A (0x0f)
79#define OPCODE_SETCC_RM8_B (0x90) /* | jcc type, /0 */
80#define OPCODE_CALL_REL32 (0xe8)
81#define OPCODE_CALL_RM32 (0xff) /* /2 */
82#define OPCODE_LEAVE (0xc9)
83
Damien George81057362014-09-07 01:06:19 +010084#define MODRM_R64(x) (((x) & 0x7) << 3)
Damien429d7192013-10-04 19:53:11 +010085#define MODRM_RM_DISP0 (0x00)
86#define MODRM_RM_DISP8 (0x40)
87#define MODRM_RM_DISP32 (0x80)
88#define MODRM_RM_REG (0xc0)
Damien George81057362014-09-07 01:06:19 +010089#define MODRM_RM_R64(x) ((x) & 0x7)
Damien429d7192013-10-04 19:53:11 +010090
Damien Georged66e4862014-09-29 10:13:49 +010091#define OP_SIZE_PREFIX (0x66)
92
Damien429d7192013-10-04 19:53:11 +010093#define REX_PREFIX (0x40)
94#define REX_W (0x08) // width
95#define REX_R (0x04) // register
96#define REX_X (0x02) // index
97#define REX_B (0x01) // base
98
99#define IMM32_L0(x) ((x) & 0xff)
100#define IMM32_L1(x) (((x) >> 8) & 0xff)
101#define IMM32_L2(x) (((x) >> 16) & 0xff)
102#define IMM32_L3(x) (((x) >> 24) & 0xff)
103#define IMM64_L4(x) (((x) >> 32) & 0xff)
104#define IMM64_L5(x) (((x) >> 40) & 0xff)
105#define IMM64_L6(x) (((x) >> 48) & 0xff)
106#define IMM64_L7(x) (((x) >> 56) & 0xff)
107
108#define UNSIGNED_FIT8(x) (((x) & 0xffffffffffffff00) == 0)
109#define UNSIGNED_FIT32(x) (((x) & 0xffffffff00000000) == 0)
110#define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80)
111
112struct _asm_x64_t {
Damien George36db6bc2014-05-07 17:24:22 +0100113 uint pass;
Fabian Vogtb7235b82014-09-03 16:59:33 +0200114 mp_uint_t code_offset;
115 mp_uint_t code_size;
Damien429d7192013-10-04 19:53:11 +0100116 byte *code_base;
117 byte dummy_data[8];
118
Damien George0b610de2014-09-29 16:25:04 +0100119 mp_uint_t max_num_labels;
120 mp_uint_t *label_offsets;
Damien Georgecd82e022014-02-02 13:11:48 +0000121 int num_locals;
Damien429d7192013-10-04 19:53:11 +0100122};
123
Damien George0b610de2014-09-29 16:25:04 +0100124asm_x64_t *asm_x64_new(mp_uint_t max_num_labels) {
Damien Georgecd82e022014-02-02 13:11:48 +0000125 asm_x64_t *as;
Damien429d7192013-10-04 19:53:11 +0100126
Damien George36db6bc2014-05-07 17:24:22 +0100127 as = m_new0(asm_x64_t, 1);
Damien054848a2013-10-05 13:44:41 +0100128 as->max_num_labels = max_num_labels;
Damien George0b610de2014-09-29 16:25:04 +0100129 as->label_offsets = m_new(mp_uint_t, max_num_labels);
Damien429d7192013-10-04 19:53:11 +0100130
131 return as;
132}
133
Damien Georgecd82e022014-02-02 13:11:48 +0000134void asm_x64_free(asm_x64_t *as, bool free_code) {
Damien429d7192013-10-04 19:53:11 +0100135 if (free_code) {
Fabian Vogtb7235b82014-09-03 16:59:33 +0200136 MP_PLAT_FREE_EXEC(as->code_base, as->code_size);
Damien429d7192013-10-04 19:53:11 +0100137 }
Damien George0b610de2014-09-29 16:25:04 +0100138 m_del(mp_uint_t, as->label_offsets, as->max_num_labels);
Damien732407f2013-12-29 19:33:23 +0000139 m_del_obj(asm_x64_t, as);
Damien429d7192013-10-04 19:53:11 +0100140}
141
Damien George36db6bc2014-05-07 17:24:22 +0100142void asm_x64_start_pass(asm_x64_t *as, uint pass) {
Damien429d7192013-10-04 19:53:11 +0100143 as->pass = pass;
144 as->code_offset = 0;
Damien George36db6bc2014-05-07 17:24:22 +0100145 if (pass == ASM_X64_PASS_COMPUTE) {
Damien054848a2013-10-05 13:44:41 +0100146 // reset all labels
Damien George0b610de2014-09-29 16:25:04 +0100147 memset(as->label_offsets, -1, as->max_num_labels * sizeof(mp_uint_t));
Damien429d7192013-10-04 19:53:11 +0100148 }
149}
150
151void asm_x64_end_pass(asm_x64_t *as) {
Damien George36db6bc2014-05-07 17:24:22 +0100152 if (as->pass == ASM_X64_PASS_COMPUTE) {
Fabian Vogtb7235b82014-09-03 16:59:33 +0200153 MP_PLAT_ALLOC_EXEC(as->code_offset, (void**) &as->code_base, &as->code_size);
Damien Georgedda46462014-09-03 22:47:23 +0100154 if(as->code_base == NULL) {
Fabian Vogtb7235b82014-09-03 16:59:33 +0200155 assert(0);
156 }
Damien Georgee2e3d112014-01-06 22:13:00 +0000157 //printf("code_size: %u\n", as->code_size);
Damien429d7192013-10-04 19:53:11 +0100158 }
159
160 /*
161 // check labels are resolved
162 if (as->label != NULL)
163 {
164 int i;
165 for (i = 0; i < as->label->len; ++i)
166 if (g_array_index(as->label, Label, i).unresolved != NULL)
167 return false;
168 }
169 */
170}
171
172// all functions must go through this one to emit bytes
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200173STATIC byte *asm_x64_get_cur_to_write_bytes(asm_x64_t *as, int num_bytes_to_write) {
Damien429d7192013-10-04 19:53:11 +0100174 //printf("emit %d\n", num_bytes_to_write);
Damien George36db6bc2014-05-07 17:24:22 +0100175 if (as->pass < ASM_X64_PASS_EMIT) {
Damien429d7192013-10-04 19:53:11 +0100176 as->code_offset += num_bytes_to_write;
177 return as->dummy_data;
178 } else {
179 assert(as->code_offset + num_bytes_to_write <= as->code_size);
180 byte *c = as->code_base + as->code_offset;
181 as->code_offset += num_bytes_to_write;
182 return c;
183 }
184}
185
Damien George0b610de2014-09-29 16:25:04 +0100186mp_uint_t asm_x64_get_code_size(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100187 return as->code_size;
188}
189
Damien Georgecd82e022014-02-02 13:11:48 +0000190void *asm_x64_get_code(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100191 return as->code_base;
192}
193
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200194STATIC void asm_x64_write_byte_1(asm_x64_t *as, byte b1) {
Damien429d7192013-10-04 19:53:11 +0100195 byte* c = asm_x64_get_cur_to_write_bytes(as, 1);
196 c[0] = b1;
197}
198
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200199STATIC void asm_x64_write_byte_2(asm_x64_t *as, byte b1, byte b2) {
Damien429d7192013-10-04 19:53:11 +0100200 byte* c = asm_x64_get_cur_to_write_bytes(as, 2);
201 c[0] = b1;
202 c[1] = b2;
203}
204
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200205STATIC void asm_x64_write_byte_3(asm_x64_t *as, byte b1, byte b2, byte b3) {
Damien429d7192013-10-04 19:53:11 +0100206 byte* c = asm_x64_get_cur_to_write_bytes(as, 3);
207 c[0] = b1;
208 c[1] = b2;
209 c[2] = b3;
210}
211
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200212STATIC void asm_x64_write_word32(asm_x64_t *as, int w32) {
Damien429d7192013-10-04 19:53:11 +0100213 byte* c = asm_x64_get_cur_to_write_bytes(as, 4);
214 c[0] = IMM32_L0(w32);
215 c[1] = IMM32_L1(w32);
216 c[2] = IMM32_L2(w32);
217 c[3] = IMM32_L3(w32);
218}
219
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200220STATIC void asm_x64_write_word64(asm_x64_t *as, int64_t w64) {
Damien429d7192013-10-04 19:53:11 +0100221 byte* c = asm_x64_get_cur_to_write_bytes(as, 8);
222 c[0] = IMM32_L0(w64);
223 c[1] = IMM32_L1(w64);
224 c[2] = IMM32_L2(w64);
225 c[3] = IMM32_L3(w64);
226 c[4] = IMM64_L4(w64);
227 c[5] = IMM64_L5(w64);
228 c[6] = IMM64_L6(w64);
229 c[7] = IMM64_L7(w64);
230}
231
232/* unused
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200233STATIC void asm_x64_write_word32_to(asm_x64_t *as, int offset, int w32) {
Damien429d7192013-10-04 19:53:11 +0100234 byte* c;
235 assert(offset + 4 <= as->code_size);
236 c = as->code_base + offset;
237 c[0] = IMM32_L0(w32);
238 c[1] = IMM32_L1(w32);
239 c[2] = IMM32_L2(w32);
240 c[3] = IMM32_L3(w32);
241}
242*/
243
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200244STATIC void asm_x64_write_r64_disp(asm_x64_t *as, int r64, int disp_r64, int disp_offset) {
Damien George81057362014-09-07 01:06:19 +0100245 assert(disp_r64 < 8);
Damien George0b610de2014-09-29 16:25:04 +0100246 assert(disp_r64 != ASM_X64_REG_RSP);
Damien429d7192013-10-04 19:53:11 +0100247
Damien George0b610de2014-09-29 16:25:04 +0100248 if (disp_offset == 0 && disp_r64 != ASM_X64_REG_RBP) {
Damien429d7192013-10-04 19:53:11 +0100249 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP0 | MODRM_RM_R64(disp_r64));
250 } else if (SIGNED_FIT8(disp_offset)) {
251 asm_x64_write_byte_2(as, MODRM_R64(r64) | MODRM_RM_DISP8 | MODRM_RM_R64(disp_r64), IMM32_L0(disp_offset));
252 } else {
253 asm_x64_write_byte_1(as, MODRM_R64(r64) | MODRM_RM_DISP32 | MODRM_RM_R64(disp_r64));
254 asm_x64_write_word32(as, disp_offset);
255 }
256}
257
Damien George3112cde2014-09-29 18:45:42 +0100258STATIC void asm_x64_generic_r64_r64(asm_x64_t *as, int dest_r64, int src_r64, int op) {
259 asm_x64_write_byte_3(as, REX_PREFIX | REX_W | (src_r64 < 8 ? 0 : REX_R) | (dest_r64 < 8 ? 0 : REX_B), op, MODRM_R64(src_r64) | MODRM_RM_REG | MODRM_RM_R64(dest_r64));
260}
261
Damien Georgecd82e022014-02-02 13:11:48 +0000262void asm_x64_nop(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100263 asm_x64_write_byte_1(as, OPCODE_NOP);
264}
265
Damien Georgecd82e022014-02-02 13:11:48 +0000266void asm_x64_push_r64(asm_x64_t *as, int src_r64) {
Damien George81057362014-09-07 01:06:19 +0100267 if (src_r64 < 8) {
268 asm_x64_write_byte_1(as, OPCODE_PUSH_R64 | src_r64);
269 } else {
270 asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_PUSH_R64 | (src_r64 & 7));
271 }
Damien429d7192013-10-04 19:53:11 +0100272}
273
Damien George81057362014-09-07 01:06:19 +0100274/*
Damien Georgecd82e022014-02-02 13:11:48 +0000275void asm_x64_push_i32(asm_x64_t *as, int src_i32) {
Damien429d7192013-10-04 19:53:11 +0100276 asm_x64_write_byte_1(as, OPCODE_PUSH_I64);
277 asm_x64_write_word32(as, src_i32); // will be sign extended to 64 bits
278}
Damien George81057362014-09-07 01:06:19 +0100279*/
Damien429d7192013-10-04 19:53:11 +0100280
Damien Georgecd82e022014-02-02 13:11:48 +0000281void asm_x64_push_disp(asm_x64_t *as, int src_r64, int src_offset) {
Damien George81057362014-09-07 01:06:19 +0100282 assert(src_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100283 asm_x64_write_byte_1(as, OPCODE_PUSH_M64);
284 asm_x64_write_r64_disp(as, 6, src_r64, src_offset);
285}
286
Damien Georgecd82e022014-02-02 13:11:48 +0000287void asm_x64_pop_r64(asm_x64_t *as, int dest_r64) {
Damien George81057362014-09-07 01:06:19 +0100288 if (dest_r64 < 8) {
289 asm_x64_write_byte_1(as, OPCODE_POP_R64 | dest_r64);
290 } else {
291 asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_POP_R64 | (dest_r64 & 7));
292 }
Damien429d7192013-10-04 19:53:11 +0100293}
294
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200295STATIC void asm_x64_ret(asm_x64_t *as) {
Damien429d7192013-10-04 19:53:11 +0100296 asm_x64_write_byte_1(as, OPCODE_RET);
297}
298
Damien George3112cde2014-09-29 18:45:42 +0100299void asm_x64_mov_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
300 asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_MOV_R64_TO_RM64);
Damien429d7192013-10-04 19:53:11 +0100301}
302
Damien Georged66e4862014-09-29 10:13:49 +0100303void asm_x64_mov_r8_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
304 assert(dest_r64 < 8);
305 if (src_r64 < 8) {
306 asm_x64_write_byte_1(as, OPCODE_MOV_R8_TO_RM8);
307 } else {
308 asm_x64_write_byte_2(as, REX_PREFIX | REX_R, OPCODE_MOV_R8_TO_RM8);
309 }
310 asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
311}
312
313void asm_x64_mov_r16_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
314 assert(src_r64 < 8);
315 assert(dest_r64 < 8);
316 asm_x64_write_byte_2(as, OP_SIZE_PREFIX, OPCODE_MOV_R64_TO_RM64);
317 asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
318}
319
Damien Georgecd82e022014-02-02 13:11:48 +0000320void asm_x64_mov_r64_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
Damien429d7192013-10-04 19:53:11 +0100321 // use REX prefix for 64 bit operation
Damien George81057362014-09-07 01:06:19 +0100322 assert(dest_r64 < 8);
323 asm_x64_write_byte_2(as, REX_PREFIX | REX_W | (src_r64 < 8 ? 0 : REX_R), OPCODE_MOV_R64_TO_RM64);
Damien429d7192013-10-04 19:53:11 +0100324 asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
325}
326
Damien Georgecd82e022014-02-02 13:11:48 +0000327void asm_x64_mov_disp_to_r64(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100328 // use REX prefix for 64 bit operation
Damien George81057362014-09-07 01:06:19 +0100329 assert(src_r64 < 8);
330 asm_x64_write_byte_2(as, REX_PREFIX | REX_W | (dest_r64 < 8 ? 0 : REX_R), OPCODE_MOV_RM64_TO_R64);
Damien429d7192013-10-04 19:53:11 +0100331 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
332}
333
Damien Georgecd82e022014-02-02 13:11:48 +0000334void asm_x64_lea_disp_to_r64(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100335 // use REX prefix for 64 bit operation
Damien George81057362014-09-07 01:06:19 +0100336 assert(src_r64 < 8);
337 assert(dest_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100338 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_LEA_MEM_TO_R64);
339 asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
340}
341
342void asm_x64_mov_i8_to_r8(asm_x64_t *as, int src_i8, int dest_r64) {
Damien George81057362014-09-07 01:06:19 +0100343 assert(dest_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100344 asm_x64_write_byte_2(as, OPCODE_MOV_I8_TO_R8 | dest_r64, src_i8);
345}
346
Damien George81057362014-09-07 01:06:19 +0100347STATIC void asm_x64_mov_i32_to_r64(asm_x64_t *as, int src_i32, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100348 // cpu defaults to i32 to r64, with zero extension
Damien George81057362014-09-07 01:06:19 +0100349 if (dest_r64 < 8) {
350 asm_x64_write_byte_1(as, OPCODE_MOV_I64_TO_R64 | dest_r64);
351 } else {
352 asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_MOV_I64_TO_R64 | (dest_r64 & 7));
353 }
Damien429d7192013-10-04 19:53:11 +0100354 asm_x64_write_word32(as, src_i32);
355}
356
Damien Georgecd82e022014-02-02 13:11:48 +0000357void asm_x64_mov_i64_to_r64(asm_x64_t *as, int64_t src_i64, int dest_r64) {
Damien429d7192013-10-04 19:53:11 +0100358 // cpu defaults to i32 to r64
359 // to mov i64 to r64 need to use REX prefix
Damien George81057362014-09-07 01:06:19 +0100360 assert(dest_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100361 asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_I64_TO_R64 | dest_r64);
362 asm_x64_write_word64(as, src_i64);
363}
364
365void asm_x64_mov_i64_to_r64_optimised(asm_x64_t *as, int64_t src_i64, int dest_r64) {
Damien Georged66e4862014-09-29 10:13:49 +0100366 // TODO use movzx, movsx if possible
Damien429d7192013-10-04 19:53:11 +0100367 if (UNSIGNED_FIT32(src_i64)) {
368 // 5 bytes
369 asm_x64_mov_i32_to_r64(as, src_i64 & 0xffffffff, dest_r64);
370 } else {
371 // 10 bytes
372 asm_x64_mov_i64_to_r64(as, src_i64, dest_r64);
373 }
374}
375
Damien George36db6bc2014-05-07 17:24:22 +0100376// src_i64 is stored as a full word in the code, and aligned to machine-word boundary
377void asm_x64_mov_i64_to_r64_aligned(asm_x64_t *as, int64_t src_i64, int dest_r64) {
378 // mov instruction uses 2 bytes for the instruction, before the i64
379 while (((as->code_offset + 2) & (WORD_SIZE - 1)) != 0) {
380 asm_x64_nop(as);
381 }
382 asm_x64_mov_i64_to_r64(as, src_i64, dest_r64);
383}
384
Damien George3112cde2014-09-29 18:45:42 +0100385void asm_x64_xor_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
386 asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_XOR_R64_TO_RM64);
Damien429d7192013-10-04 19:53:11 +0100387}
388
Damien George3112cde2014-09-29 18:45:42 +0100389void asm_x64_shl_r64_cl(asm_x64_t* as, int dest_r64) {
390 asm_x64_generic_r64_r64(as, dest_r64, 4, OPCODE_SHL_RM64_CL);
Damien429d7192013-10-04 19:53:11 +0100391}
392
Damien George3112cde2014-09-29 18:45:42 +0100393void asm_x64_sar_r64_cl(asm_x64_t* as, int dest_r64) {
394 asm_x64_generic_r64_r64(as, dest_r64, 7, OPCODE_SAR_RM64_CL);
Damien429d7192013-10-04 19:53:11 +0100395}
396
Damien George3112cde2014-09-29 18:45:42 +0100397void asm_x64_add_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
398 asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_ADD_R64_TO_RM64);
399}
400
401void asm_x64_sub_r64_r64(asm_x64_t *as, int dest_r64, int src_r64) {
402 asm_x64_generic_r64_r64(as, dest_r64, src_r64, OPCODE_SUB_R64_FROM_RM64);
Damien429d7192013-10-04 19:53:11 +0100403}
404
Damien George81057362014-09-07 01:06:19 +0100405/*
Damien Georgecd82e022014-02-02 13:11:48 +0000406void asm_x64_sub_i32_from_r32(asm_x64_t *as, int src_i32, int dest_r32) {
Damien429d7192013-10-04 19:53:11 +0100407 if (SIGNED_FIT8(src_i32)) {
408 // defaults to 32 bit operation
409 asm_x64_write_byte_2(as, OPCODE_SUB_I8_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
410 asm_x64_write_byte_1(as, src_i32 & 0xff);
411 } else {
412 // defaults to 32 bit operation
413 asm_x64_write_byte_2(as, OPCODE_SUB_I32_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
414 asm_x64_write_word32(as, src_i32);
415 }
416}
Damien George81057362014-09-07 01:06:19 +0100417*/
Damien429d7192013-10-04 19:53:11 +0100418
Damien George3112cde2014-09-29 18:45:42 +0100419STATIC void asm_x64_sub_r64_i32(asm_x64_t *as, int dest_r64, int src_i32) {
Damien George81057362014-09-07 01:06:19 +0100420 assert(dest_r64 < 8);
Damien429d7192013-10-04 19:53:11 +0100421 if (SIGNED_FIT8(src_i32)) {
422 // use REX prefix for 64 bit operation
423 asm_x64_write_byte_3(as, REX_PREFIX | REX_W, OPCODE_SUB_I8_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r64));
424 asm_x64_write_byte_1(as, src_i32 & 0xff);
425 } else {
426 // use REX prefix for 64 bit operation
427 asm_x64_write_byte_3(as, REX_PREFIX | REX_W, OPCODE_SUB_I32_FROM_RM64, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(dest_r64));
428 asm_x64_write_word32(as, src_i32);
429 }
430}
431
Damien George81057362014-09-07 01:06:19 +0100432/*
Damien Georgecd82e022014-02-02 13:11:48 +0000433void asm_x64_shl_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100434 asm_x64_write_byte_2(as, OPCODE_SHL_RM32_BY_I8, MODRM_R64(4) | MODRM_RM_REG | MODRM_RM_R64(r32));
435 asm_x64_write_byte_1(as, imm);
436}
437
Damien Georgecd82e022014-02-02 13:11:48 +0000438void asm_x64_shr_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100439 asm_x64_write_byte_2(as, OPCODE_SHR_RM32_BY_I8, MODRM_R64(5) | MODRM_RM_REG | MODRM_RM_R64(r32));
440 asm_x64_write_byte_1(as, imm);
441}
442
Damien Georgecd82e022014-02-02 13:11:48 +0000443void asm_x64_sar_r32_by_imm(asm_x64_t *as, int r32, int imm) {
Damien429d7192013-10-04 19:53:11 +0100444 asm_x64_write_byte_2(as, OPCODE_SAR_RM32_BY_I8, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(r32));
445 asm_x64_write_byte_1(as, imm);
446}
Damien George81057362014-09-07 01:06:19 +0100447*/
Damien429d7192013-10-04 19:53:11 +0100448
Damien Georgecd82e022014-02-02 13:11:48 +0000449void asm_x64_cmp_r64_with_r64(asm_x64_t *as, int src_r64_a, int src_r64_b) {
Damien George3112cde2014-09-29 18:45:42 +0100450 asm_x64_generic_r64_r64(as, src_r64_b, src_r64_a, OPCODE_CMP_R64_WITH_RM64);
Damien429d7192013-10-04 19:53:11 +0100451}
452
Damien George81057362014-09-07 01:06:19 +0100453/*
Damien Georgecd82e022014-02-02 13:11:48 +0000454void asm_x64_cmp_i32_with_r32(asm_x64_t *as, int src_i32, int src_r32) {
Damien429d7192013-10-04 19:53:11 +0100455 if (SIGNED_FIT8(src_i32)) {
456 asm_x64_write_byte_2(as, OPCODE_CMP_I8_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
457 asm_x64_write_byte_1(as, src_i32 & 0xff);
458 } else {
459 asm_x64_write_byte_2(as, OPCODE_CMP_I32_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
460 asm_x64_write_word32(as, src_i32);
461 }
462}
Damien George81057362014-09-07 01:06:19 +0100463*/
Damien429d7192013-10-04 19:53:11 +0100464
Damien Georgecd82e022014-02-02 13:11:48 +0000465void asm_x64_test_r8_with_r8(asm_x64_t *as, int src_r64_a, int src_r64_b) {
Damien7af3d192013-10-07 00:02:49 +0100466 // TODO implement for other registers
Damien George0b610de2014-09-29 16:25:04 +0100467 assert(src_r64_a == ASM_X64_REG_RAX);
468 assert(src_r64_b == ASM_X64_REG_RAX);
Damien429d7192013-10-04 19:53:11 +0100469 asm_x64_write_byte_2(as, OPCODE_TEST_R8_WITH_RM8, MODRM_R64(src_r64_a) | MODRM_RM_REG | MODRM_RM_R64(src_r64_b));
470}
471
Damien Georgecd82e022014-02-02 13:11:48 +0000472void asm_x64_setcc_r8(asm_x64_t *as, int jcc_type, int dest_r8) {
Damien George81057362014-09-07 01:06:19 +0100473 assert(dest_r8 < 8);
Damien429d7192013-10-04 19:53:11 +0100474 asm_x64_write_byte_3(as, OPCODE_SETCC_RM8_A, OPCODE_SETCC_RM8_B | jcc_type, MODRM_R64(0) | MODRM_RM_REG | MODRM_RM_R64(dest_r8));
475}
476
Damien Georgecd82e022014-02-02 13:11:48 +0000477void asm_x64_label_assign(asm_x64_t *as, int label) {
Damien054848a2013-10-05 13:44:41 +0100478 assert(label < as->max_num_labels);
Damien George36db6bc2014-05-07 17:24:22 +0100479 if (as->pass < ASM_X64_PASS_EMIT) {
Damien054848a2013-10-05 13:44:41 +0100480 // assign label offset
481 assert(as->label_offsets[label] == -1);
482 as->label_offsets[label] = as->code_offset;
Damien George36db6bc2014-05-07 17:24:22 +0100483 } else {
484 // ensure label offset has not changed from PASS_COMPUTE to PASS_EMIT
Damien George0b610de2014-09-29 16:25:04 +0100485 //printf("l%d: (at %ld=%ld)\n", label, as->label_offsets[label], as->code_offset);
Damien054848a2013-10-05 13:44:41 +0100486 assert(as->label_offsets[label] == as->code_offset);
Damien429d7192013-10-04 19:53:11 +0100487 }
488}
489
Damien George0b610de2014-09-29 16:25:04 +0100490STATIC mp_uint_t get_label_dest(asm_x64_t *as, int label) {
Damien054848a2013-10-05 13:44:41 +0100491 assert(label < as->max_num_labels);
492 return as->label_offsets[label];
493}
494
495void asm_x64_jmp_label(asm_x64_t *as, int label) {
Damien George0b610de2014-09-29 16:25:04 +0100496 mp_uint_t dest = get_label_dest(as, label);
497 mp_int_t rel = dest - as->code_offset;
498 if (dest != -1 && rel < 0) {
Damien054848a2013-10-05 13:44:41 +0100499 // is a backwards jump, so we know the size of the jump on the first pass
500 // calculate rel assuming 8 bit relative jump
501 rel -= 2;
502 if (SIGNED_FIT8(rel)) {
503 asm_x64_write_byte_2(as, OPCODE_JMP_REL8, rel & 0xff);
Damien429d7192013-10-04 19:53:11 +0100504 } else {
Damien054848a2013-10-05 13:44:41 +0100505 rel += 2;
506 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100507 }
Damien054848a2013-10-05 13:44:41 +0100508 } else {
509 // is a forwards jump, so need to assume it's large
510 large_jump:
511 rel -= 5;
512 asm_x64_write_byte_1(as, OPCODE_JMP_REL32);
513 asm_x64_write_word32(as, rel);
Damien429d7192013-10-04 19:53:11 +0100514 }
515}
516
Damien054848a2013-10-05 13:44:41 +0100517void asm_x64_jcc_label(asm_x64_t *as, int jcc_type, int label) {
Damien George0b610de2014-09-29 16:25:04 +0100518 mp_uint_t dest = get_label_dest(as, label);
519 mp_int_t rel = dest - as->code_offset;
520 if (dest != -1 && rel < 0) {
Damien054848a2013-10-05 13:44:41 +0100521 // is a backwards jump, so we know the size of the jump on the first pass
522 // calculate rel assuming 8 bit relative jump
523 rel -= 2;
524 if (SIGNED_FIT8(rel)) {
525 asm_x64_write_byte_2(as, OPCODE_JCC_REL8 | jcc_type, rel & 0xff);
Damien429d7192013-10-04 19:53:11 +0100526 } else {
Damien054848a2013-10-05 13:44:41 +0100527 rel += 2;
528 goto large_jump;
Damien429d7192013-10-04 19:53:11 +0100529 }
Damien054848a2013-10-05 13:44:41 +0100530 } else {
531 // is a forwards jump, so need to assume it's large
532 large_jump:
533 rel -= 6;
534 asm_x64_write_byte_2(as, OPCODE_JCC_REL32_A, OPCODE_JCC_REL32_B | jcc_type);
535 asm_x64_write_word32(as, rel);
Damien429d7192013-10-04 19:53:11 +0100536 }
537}
538
Damien Georgecd82e022014-02-02 13:11:48 +0000539void asm_x64_entry(asm_x64_t *as, int num_locals) {
Damien George0b610de2014-09-29 16:25:04 +0100540 asm_x64_push_r64(as, ASM_X64_REG_RBP);
Damien George3112cde2014-09-29 18:45:42 +0100541 asm_x64_mov_r64_r64(as, ASM_X64_REG_RBP, ASM_X64_REG_RSP);
Damien429d7192013-10-04 19:53:11 +0100542 if (num_locals < 0) {
543 num_locals = 0;
544 }
545 num_locals |= 1; // make it odd so stack is aligned on 16 byte boundary
Damien George3112cde2014-09-29 18:45:42 +0100546 asm_x64_sub_r64_i32(as, ASM_X64_REG_RSP, num_locals * WORD_SIZE);
Damien George0b610de2014-09-29 16:25:04 +0100547 asm_x64_push_r64(as, ASM_X64_REG_RBX);
548 asm_x64_push_r64(as, ASM_X64_REG_R12);
549 asm_x64_push_r64(as, ASM_X64_REG_R13);
Damien Georgecd82e022014-02-02 13:11:48 +0000550 as->num_locals = num_locals;
Damien429d7192013-10-04 19:53:11 +0100551}
552
Damien Georgecd82e022014-02-02 13:11:48 +0000553void asm_x64_exit(asm_x64_t *as) {
Damien George0b610de2014-09-29 16:25:04 +0100554 asm_x64_pop_r64(as, ASM_X64_REG_R13);
555 asm_x64_pop_r64(as, ASM_X64_REG_R12);
556 asm_x64_pop_r64(as, ASM_X64_REG_RBX);
Damien429d7192013-10-04 19:53:11 +0100557 asm_x64_write_byte_1(as, OPCODE_LEAVE);
558 asm_x64_ret(as);
559}
560
Damien Georgecd82e022014-02-02 13:11:48 +0000561// locals:
562// - stored on the stack in ascending order
563// - numbered 0 through as->num_locals-1
564// - RBP points above the last local
565//
566// | RPB
567// v
568// l0 l1 l2 ... l(n-1)
569// ^ ^
570// | low address | high address in RAM
571//
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200572STATIC int asm_x64_local_offset_from_ebp(asm_x64_t *as, int local_num) {
Damien Georgecd82e022014-02-02 13:11:48 +0000573 return (-as->num_locals + local_num) * WORD_SIZE;
Damien429d7192013-10-04 19:53:11 +0100574}
575
Damien Georgecd82e022014-02-02 13:11:48 +0000576void asm_x64_mov_local_to_r64(asm_x64_t *as, int src_local_num, int dest_r64) {
Damien George0b610de2014-09-29 16:25:04 +0100577 asm_x64_mov_disp_to_r64(as, ASM_X64_REG_RBP, asm_x64_local_offset_from_ebp(as, src_local_num), dest_r64);
Damien429d7192013-10-04 19:53:11 +0100578}
579
Damien Georgecd82e022014-02-02 13:11:48 +0000580void asm_x64_mov_r64_to_local(asm_x64_t *as, int src_r64, int dest_local_num) {
Damien George0b610de2014-09-29 16:25:04 +0100581 asm_x64_mov_r64_to_disp(as, src_r64, ASM_X64_REG_RBP, asm_x64_local_offset_from_ebp(as, dest_local_num));
Damien429d7192013-10-04 19:53:11 +0100582}
583
Damien Georgecd82e022014-02-02 13:11:48 +0000584void asm_x64_mov_local_addr_to_r64(asm_x64_t *as, int local_num, int dest_r64) {
585 int offset = asm_x64_local_offset_from_ebp(as, local_num);
Damien429d7192013-10-04 19:53:11 +0100586 if (offset == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100587 asm_x64_mov_r64_r64(as, dest_r64, ASM_X64_REG_RBP);
Damien429d7192013-10-04 19:53:11 +0100588 } else {
Damien George0b610de2014-09-29 16:25:04 +0100589 asm_x64_lea_disp_to_r64(as, ASM_X64_REG_RBP, offset, dest_r64);
Damien429d7192013-10-04 19:53:11 +0100590 }
591}
592
Damien George81057362014-09-07 01:06:19 +0100593/*
Damien Georgecd82e022014-02-02 13:11:48 +0000594void asm_x64_push_local(asm_x64_t *as, int local_num) {
Damien George0b610de2014-09-29 16:25:04 +0100595 asm_x64_push_disp(as, ASM_X64_REG_RBP, asm_x64_local_offset_from_ebp(as, local_num));
Damien429d7192013-10-04 19:53:11 +0100596}
597
Damien Georgecd82e022014-02-02 13:11:48 +0000598void asm_x64_push_local_addr(asm_x64_t *as, int local_num, int temp_r64)
Damien429d7192013-10-04 19:53:11 +0100599{
Damien George3112cde2014-09-29 18:45:42 +0100600 asm_x64_mov_r64_r64(as, temp_r64, ASM_X64_REG_RBP);
Damien Georgecd82e022014-02-02 13:11:48 +0000601 asm_x64_add_i32_to_r32(as, asm_x64_local_offset_from_ebp(as, local_num), temp_r64);
Damien429d7192013-10-04 19:53:11 +0100602 asm_x64_push_r64(as, temp_r64);
603}
Damien George81057362014-09-07 01:06:19 +0100604*/
Damien429d7192013-10-04 19:53:11 +0100605
606/*
607 can't use these because code might be relocated when resized
608
Damien Georgecd82e022014-02-02 13:11:48 +0000609void asm_x64_call(asm_x64_t *as, void* func)
Damien429d7192013-10-04 19:53:11 +0100610{
Damien George0b610de2014-09-29 16:25:04 +0100611 asm_x64_sub_i32_from_r32(as, 8, ASM_X64_REG_RSP);
Damien429d7192013-10-04 19:53:11 +0100612 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
613 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
Damien George3112cde2014-09-29 18:45:42 +0100614 asm_x64_mov_r64_r64(as, ASM_X64_REG_RSP, ASM_X64_REG_RBP);
Damien429d7192013-10-04 19:53:11 +0100615}
616
Damien Georgecd82e022014-02-02 13:11:48 +0000617void asm_x64_call_i1(asm_x64_t *as, void* func, int i1)
Damien429d7192013-10-04 19:53:11 +0100618{
Damien George0b610de2014-09-29 16:25:04 +0100619 asm_x64_sub_i32_from_r32(as, 8, ASM_X64_REG_RSP);
620 asm_x64_sub_i32_from_r32(as, 12, ASM_X64_REG_RSP);
Damien429d7192013-10-04 19:53:11 +0100621 asm_x64_push_i32(as, i1);
622 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
623 asm_x64_write_word32(as, func - (void*)(as->code_cur + 4));
Damien George0b610de2014-09-29 16:25:04 +0100624 asm_x64_add_i32_to_r32(as, 16, ASM_X64_REG_RSP);
Damien George3112cde2014-09-29 18:45:42 +0100625 asm_x64_mov_r64_r64(as, ASM_X64_REG_RSP, ASM_X64_REG_RBP);
Damien429d7192013-10-04 19:53:11 +0100626}
627*/
628
Damien Georgecd82e022014-02-02 13:11:48 +0000629void asm_x64_call_ind(asm_x64_t *as, void *ptr, int temp_r64) {
Damien George81057362014-09-07 01:06:19 +0100630 assert(temp_r64 < 8);
Damien George212c2962013-12-30 12:52:32 +0000631#ifdef __LP64__
632 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)ptr, temp_r64);
633#else
634 // If we get here, sizeof(int) == sizeof(void*).
635 asm_x64_mov_i64_to_r64_optimised(as, (int64_t)(unsigned int)ptr, temp_r64);
636#endif
Damien429d7192013-10-04 19:53:11 +0100637 asm_x64_write_byte_2(as, OPCODE_CALL_RM32, MODRM_R64(2) | MODRM_RM_REG | MODRM_RM_R64(temp_r64));
Damien429d7192013-10-04 19:53:11 +0100638 // this reduces code size by 2 bytes per call, but doesn't seem to speed it up at all
Damien415eb6f2013-10-05 12:19:06 +0100639 // doesn't work anymore because calls are 64 bits away
640 /*
Damien429d7192013-10-04 19:53:11 +0100641 asm_x64_write_byte_1(as, OPCODE_CALL_REL32);
642 asm_x64_write_word32(as, ptr - (void*)(as->code_base + as->code_offset + 4));
Damien415eb6f2013-10-05 12:19:06 +0100643 */
Damien429d7192013-10-04 19:53:11 +0100644}
Damien Georgee67ed5d2014-01-04 13:55:24 +0000645
646#endif // MICROPY_EMIT_X64