blob: 6fbc2c906959b78f0eabd929e81b3d2601a9ff6e [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 */
Damien Georgeebde3c62015-01-01 18:07:43 +000026#ifndef __MICROPY_INCLUDED_PY_ASMX64_H__
27#define __MICROPY_INCLUDED_PY_ASMX64_H__
28
29#include "py/mpconfig.h"
30#include "py/misc.h"
Damien George04b91472014-05-03 23:27:38 +010031
Damien George81057362014-09-07 01:06:19 +010032// AMD64 calling convention is:
33// - args pass in: RDI, RSI, RDX, RCX, R08, R09
34// - return value in RAX
35// - stack must be aligned on a 16-byte boundary before all calls
36// - RAX, RCX, RDX, RSI, RDI, R08, R09, R10, R11 are caller-save
37// - RBX, RBP, R12, R13, R14, R15 are callee-save
38
Damien George3112cde2014-09-29 18:45:42 +010039// In the functions below, argument order follows x86 docs and generally
40// the destination is the first argument.
41// NOTE: this is a change from the old convention used in this file and
42// some functions still use the old (reverse) convention.
43
Damien George36db6bc2014-05-07 17:24:22 +010044#define ASM_X64_PASS_COMPUTE (1)
45#define ASM_X64_PASS_EMIT (2)
Damien429d7192013-10-04 19:53:11 +010046
Damien George0b610de2014-09-29 16:25:04 +010047#define ASM_X64_REG_RAX (0)
48#define ASM_X64_REG_RCX (1)
49#define ASM_X64_REG_RDX (2)
50#define ASM_X64_REG_RBX (3)
51#define ASM_X64_REG_RSP (4)
52#define ASM_X64_REG_RBP (5)
53#define ASM_X64_REG_RSI (6)
54#define ASM_X64_REG_RDI (7)
55#define ASM_X64_REG_R08 (8)
56#define ASM_X64_REG_R09 (9)
57#define ASM_X64_REG_R10 (10)
58#define ASM_X64_REG_R11 (11)
59#define ASM_X64_REG_R12 (12)
60#define ASM_X64_REG_R13 (13)
61#define ASM_X64_REG_R14 (14)
62#define ASM_X64_REG_R15 (15)
Damien429d7192013-10-04 19:53:11 +010063
Paul Sokolovsky2efbc622013-12-30 21:03:41 +020064// condition codes, used for jcc and setcc (despite their j-name!)
Damien Georgec90f59e2014-09-06 23:06:36 +010065#define ASM_X64_CC_JB (0x2) // below, unsigned
66#define ASM_X64_CC_JZ (0x4)
67#define ASM_X64_CC_JE (0x4)
68#define ASM_X64_CC_JNZ (0x5)
69#define ASM_X64_CC_JNE (0x5)
70#define ASM_X64_CC_JL (0xc) // less, signed
Damien George3112cde2014-09-29 18:45:42 +010071#define ASM_X64_CC_JGE (0xd) // greater or equal, signed
72#define ASM_X64_CC_JLE (0xe) // less or equal, signed
Damien Georged66e4862014-09-29 10:13:49 +010073#define ASM_X64_CC_JG (0xf) // greater, signed
Damien429d7192013-10-04 19:53:11 +010074
Damien429d7192013-10-04 19:53:11 +010075typedef struct _asm_x64_t asm_x64_t;
76
Damien George0b610de2014-09-29 16:25:04 +010077asm_x64_t* asm_x64_new(mp_uint_t max_num_labels);
Damien429d7192013-10-04 19:53:11 +010078void asm_x64_free(asm_x64_t* as, bool free_code);
Damien George36db6bc2014-05-07 17:24:22 +010079void asm_x64_start_pass(asm_x64_t *as, uint pass);
Damien429d7192013-10-04 19:53:11 +010080void asm_x64_end_pass(asm_x64_t *as);
Damien George99886182015-04-06 22:38:53 +010081mp_uint_t asm_x64_get_code_pos(asm_x64_t *as);
Damien George0b610de2014-09-29 16:25:04 +010082mp_uint_t asm_x64_get_code_size(asm_x64_t* as);
Damien429d7192013-10-04 19:53:11 +010083void* asm_x64_get_code(asm_x64_t* as);
84
Damien George99886182015-04-06 22:38:53 +010085void asm_x64_align(asm_x64_t *as, mp_uint_t align);
86void asm_x64_data(asm_x64_t *as, mp_uint_t bytesize, mp_uint_t val);
87
Damien429d7192013-10-04 19:53:11 +010088void asm_x64_nop(asm_x64_t* as);
89void asm_x64_push_r64(asm_x64_t* as, int src_r64);
Damien429d7192013-10-04 19:53:11 +010090void asm_x64_pop_r64(asm_x64_t* as, int dest_r64);
Damien George3112cde2014-09-29 18:45:42 +010091void asm_x64_mov_r64_r64(asm_x64_t* as, int dest_r64, int src_r64);
Damien429d7192013-10-04 19:53:11 +010092void asm_x64_mov_i64_to_r64(asm_x64_t* as, int64_t src_i64, int dest_r64);
Damien429d7192013-10-04 19:53:11 +010093void 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 +010094void asm_x64_mov_i64_to_r64_aligned(asm_x64_t *as, int64_t src_i64, int dest_r64);
Damien George91cfd412014-10-12 16:59:29 +010095void asm_x64_mov_r8_to_mem8(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp);
96void asm_x64_mov_r16_to_mem16(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp);
Damien Georgeb8f9ac52015-10-13 00:50:17 +010097void asm_x64_mov_r32_to_mem32(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp);
Damien George91cfd412014-10-12 16:59:29 +010098void asm_x64_mov_r64_to_mem64(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp);
99void asm_x64_mov_mem8_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64);
100void asm_x64_mov_mem16_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64);
Damien Georgeb8f9ac52015-10-13 00:50:17 +0100101void asm_x64_mov_mem32_to_r64zx(asm_x64_t *as, int src_r64, int src_disp, int dest_r64);
Damien George91cfd412014-10-12 16:59:29 +0100102void asm_x64_mov_mem64_to_r64(asm_x64_t *as, int src_r64, int src_disp, int dest_r64);
Damien George1ef23482014-10-12 14:21:06 +0100103void asm_x64_and_r64_r64(asm_x64_t *as, int dest_r64, int src_r64);
104void asm_x64_or_r64_r64(asm_x64_t *as, int dest_r64, int src_r64);
Damien George3112cde2014-09-29 18:45:42 +0100105void asm_x64_xor_r64_r64(asm_x64_t *as, int dest_r64, int src_r64);
106void asm_x64_shl_r64_cl(asm_x64_t* as, int dest_r64);
107void asm_x64_sar_r64_cl(asm_x64_t* as, int dest_r64);
108void asm_x64_add_r64_r64(asm_x64_t* as, int dest_r64, int src_r64);
109void asm_x64_sub_r64_r64(asm_x64_t* as, int dest_r64, int src_r64);
Damien George567b3492015-06-04 14:00:29 +0000110void asm_x64_mul_r64_r64(asm_x64_t* as, int dest_r64, int src_r64);
Damien429d7192013-10-04 19:53:11 +0100111void asm_x64_cmp_r64_with_r64(asm_x64_t* as, int src_r64_a, int src_r64_b);
Damien429d7192013-10-04 19:53:11 +0100112void asm_x64_test_r8_with_r8(asm_x64_t* as, int src_r64_a, int src_r64_b);
113void asm_x64_setcc_r8(asm_x64_t* as, int jcc_type, int dest_r8);
Damien Georgee5bcbcd2015-01-22 14:08:58 +0000114void asm_x64_label_assign(asm_x64_t* as, mp_uint_t label);
115void asm_x64_jmp_label(asm_x64_t* as, mp_uint_t label);
116void asm_x64_jcc_label(asm_x64_t* as, int jcc_type, mp_uint_t label);
Damien429d7192013-10-04 19:53:11 +0100117void asm_x64_entry(asm_x64_t* as, int num_locals);
118void asm_x64_exit(asm_x64_t* as);
Damien429d7192013-10-04 19:53:11 +0100119void asm_x64_mov_local_to_r64(asm_x64_t* as, int src_local_num, int dest_r64);
120void asm_x64_mov_r64_to_local(asm_x64_t* as, int src_r64, int dest_local_num);
121void asm_x64_mov_local_addr_to_r64(asm_x64_t* as, int local_num, int dest_r64);
Damien429d7192013-10-04 19:53:11 +0100122void asm_x64_call_ind(asm_x64_t* as, void* ptr, int temp_r32);
Damien Georgeebde3c62015-01-01 18:07:43 +0000123
124#endif // __MICROPY_INCLUDED_PY_ASMX64_H__