Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 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 George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 27 | #define ASM_X64_PASS_COMPUTE (1) |
| 28 | #define ASM_X64_PASS_EMIT (2) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 29 | |
| 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 Sokolovsky | 2efbc62 | 2013-12-30 21:03:41 +0200 | [diff] [blame] | 39 | // condition codes, used for jcc and setcc (despite their j-name!) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 40 | #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 | |
| 52 | typedef struct _asm_x64_t asm_x64_t; |
| 53 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 54 | asm_x64_t* asm_x64_new(uint max_num_labels); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 55 | void asm_x64_free(asm_x64_t* as, bool free_code); |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 56 | void asm_x64_start_pass(asm_x64_t *as, uint pass); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 57 | void asm_x64_end_pass(asm_x64_t *as); |
| 58 | uint asm_x64_get_code_size(asm_x64_t* as); |
| 59 | void* asm_x64_get_code(asm_x64_t* as); |
| 60 | |
| 61 | void asm_x64_nop(asm_x64_t* as); |
| 62 | void asm_x64_push_r64(asm_x64_t* as, int src_r64); |
| 63 | void asm_x64_push_i32(asm_x64_t* as, int src_i32); // will be sign extended to 64 bits |
| 64 | void asm_x64_push_disp(asm_x64_t* as, int src_r32, int src_offset); |
| 65 | void asm_x64_pop_r64(asm_x64_t* as, int dest_r64); |
| 66 | void asm_x64_mov_r64_to_r64(asm_x64_t* as, int src_r64, int dest_r64); |
| 67 | void asm_x64_mov_r32_to_disp(asm_x64_t* as, int src_r32, int dest_r32, int dest_disp); |
| 68 | void asm_x64_mov_disp_to_r32(asm_x64_t* as, int src_r32, int src_disp, int dest_r32); |
| 69 | void asm_x64_mov_i32_to_r64(asm_x64_t* as, int src_i32, int dest_r64); |
| 70 | void asm_x64_mov_i64_to_r64(asm_x64_t* as, int64_t src_i64, int dest_r64); |
| 71 | void asm_x64_mov_i32_to_disp(asm_x64_t* as, int src_i32, int dest_r32, int dest_disp); |
| 72 | void asm_x64_mov_i64_to_r64_optimised(asm_x64_t *as, int64_t src_i64, int dest_r64); |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 73 | void asm_x64_mov_i64_to_r64_aligned(asm_x64_t *as, int64_t src_i64, int dest_r64); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 74 | void asm_x64_xor_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64); |
| 75 | void asm_x64_add_r64_to_r64(asm_x64_t* as, int src_r64, int dest_r64); |
| 76 | void asm_x64_add_i32_to_r32(asm_x64_t* as, int src_i32, int dest_r32); |
| 77 | void asm_x64_sub_r32_from_r32(asm_x64_t* as, int src_r32, int dest_r32); |
| 78 | void asm_x64_sub_i32_from_r32(asm_x64_t* as, int src_i32, int dest_r32); |
| 79 | void asm_x64_shl_r32_by_imm(asm_x64_t* as, int r32, int imm); |
| 80 | void asm_x64_shr_r32_by_imm(asm_x64_t* as, int r32, int imm); |
| 81 | void asm_x64_sar_r32_by_imm(asm_x64_t* as, int r32, int imm); |
| 82 | void asm_x64_cmp_r64_with_r64(asm_x64_t* as, int src_r64_a, int src_r64_b); |
| 83 | void asm_x64_cmp_r32_with_disp(asm_x64_t* as, int src_r32_a, int src_r32_b, int src_disp_b); |
| 84 | void asm_x64_cmp_disp_with_r32(asm_x64_t* as, int src_r32_a, int src_disp_a, int src_r32_b); |
| 85 | void asm_x64_cmp_i32_with_r32(asm_x64_t* as, int src_i32, int src_r32); |
| 86 | void asm_x64_test_r8_with_r8(asm_x64_t* as, int src_r64_a, int src_r64_b); |
| 87 | void asm_x64_setcc_r8(asm_x64_t* as, int jcc_type, int dest_r8); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 88 | void asm_x64_label_assign(asm_x64_t* as, int label); |
| 89 | void asm_x64_jmp_label(asm_x64_t* as, int label); |
| 90 | void asm_x64_jcc_label(asm_x64_t* as, int jcc_type, int label); |
| 91 | void asm_x64_entry(asm_x64_t* as, int num_locals); |
| 92 | void asm_x64_exit(asm_x64_t* as); |
| 93 | void asm_x64_push_arg(asm_x64_t* as, int src_arg_num); |
| 94 | void asm_x64_mov_arg_to_r32(asm_x64_t* as, int src_arg_num, int dest_r32); |
| 95 | void asm_x64_mov_r32_to_arg(asm_x64_t* as, int src_r32, int dest_arg_num); |
| 96 | void asm_x64_mov_local_to_r64(asm_x64_t* as, int src_local_num, int dest_r64); |
| 97 | void asm_x64_mov_r64_to_local(asm_x64_t* as, int src_r64, int dest_local_num); |
| 98 | void asm_x64_mov_local_addr_to_r64(asm_x64_t* as, int local_num, int dest_r64); |
| 99 | void asm_x64_push_local(asm_x64_t* as, int local_num); |
| 100 | void asm_x64_push_local_addr(asm_x64_t* as, int local_num, int temp_r32); |
| 101 | void asm_x64_call_ind(asm_x64_t* as, void* ptr, int temp_r32); |