py: Clean up x86-64 native assembler; allow use of extended regs.
Native x86-64 now has 3 locals in registers.
This commit is contained in:
parent
25d904105c
commit
8105736982
|
@ -48,12 +48,6 @@
|
|||
#define REG_LR (REG_R14)
|
||||
#define REG_PC (REG_R15)
|
||||
|
||||
#define REG_RET REG_R0
|
||||
#define REG_ARG_1 REG_R0
|
||||
#define REG_ARG_2 REG_R1
|
||||
#define REG_ARG_3 REG_R2
|
||||
#define REG_ARG_4 REG_R3
|
||||
|
||||
#define ARM_CC_EQ (0x0 << 28)
|
||||
#define ARM_CC_NE (0x1 << 28)
|
||||
#define ARM_CC_CS (0x2 << 28)
|
||||
|
|
|
@ -45,12 +45,6 @@
|
|||
#define REG_R15 (15)
|
||||
#define REG_LR (REG_R14)
|
||||
|
||||
#define REG_RET REG_R0
|
||||
#define REG_ARG_1 REG_R0
|
||||
#define REG_ARG_2 REG_R1
|
||||
#define REG_ARG_3 REG_R2
|
||||
#define REG_ARG_4 REG_R3
|
||||
|
||||
#define THUMB_CC_EQ (0x0)
|
||||
#define THUMB_CC_NE (0x1)
|
||||
#define THUMB_CC_CS (0x2)
|
||||
|
|
113
py/asmx64.c
113
py/asmx64.c
|
@ -41,15 +41,15 @@
|
|||
#define WORD_SIZE (8)
|
||||
|
||||
#define OPCODE_NOP (0x90)
|
||||
#define OPCODE_PUSH_R64 (0x50)
|
||||
#define OPCODE_PUSH_R64 (0x50) /* +rq */
|
||||
#define OPCODE_PUSH_I64 (0x68)
|
||||
#define OPCODE_PUSH_M64 (0xff) /* /6 */
|
||||
#define OPCODE_POP_R64 (0x58)
|
||||
#define OPCODE_POP_R64 (0x58) /* +rq */
|
||||
#define OPCODE_RET (0xc3)
|
||||
#define OPCODE_MOV_I8_TO_R8 (0xb0) /* +rb */
|
||||
#define OPCODE_MOV_I64_TO_R64 (0xb8)
|
||||
#define OPCODE_MOV_I64_TO_R64 (0xb8) /* +rq */
|
||||
#define OPCODE_MOV_I32_TO_RM32 (0xc7)
|
||||
#define OPCODE_MOV_R64_TO_RM64 (0x89)
|
||||
#define OPCODE_MOV_R64_TO_RM64 (0x89) /* /r */
|
||||
#define OPCODE_MOV_RM64_TO_R64 (0x8b)
|
||||
#define OPCODE_LEA_MEM_TO_R64 (0x8d) /* /r */
|
||||
#define OPCODE_XOR_R64_TO_RM64 (0x31) /* /r */
|
||||
|
@ -78,12 +78,12 @@
|
|||
#define OPCODE_CALL_RM32 (0xff) /* /2 */
|
||||
#define OPCODE_LEAVE (0xc9)
|
||||
|
||||
#define MODRM_R64(x) ((x) << 3)
|
||||
#define MODRM_R64(x) (((x) & 0x7) << 3)
|
||||
#define MODRM_RM_DISP0 (0x00)
|
||||
#define MODRM_RM_DISP8 (0x40)
|
||||
#define MODRM_RM_DISP32 (0x80)
|
||||
#define MODRM_RM_REG (0xc0)
|
||||
#define MODRM_RM_R64(x) (x)
|
||||
#define MODRM_RM_R64(x) ((x) & 0x7)
|
||||
|
||||
#define REX_PREFIX (0x40)
|
||||
#define REX_W (0x08) // width
|
||||
|
@ -248,6 +248,7 @@ STATIC void asm_x64_write_word32_to(asm_x64_t *as, int offset, int w32) {
|
|||
*/
|
||||
|
||||
STATIC void asm_x64_write_r64_disp(asm_x64_t *as, int r64, int disp_r64, int disp_offset) {
|
||||
assert(disp_r64 < 8);
|
||||
assert(disp_r64 != REG_RSP);
|
||||
|
||||
if (disp_offset == 0 && disp_r64 != REG_RBP) {
|
||||
|
@ -265,21 +266,32 @@ void asm_x64_nop(asm_x64_t *as) {
|
|||
}
|
||||
|
||||
void asm_x64_push_r64(asm_x64_t *as, int src_r64) {
|
||||
asm_x64_write_byte_1(as, OPCODE_PUSH_R64 | src_r64);
|
||||
if (src_r64 < 8) {
|
||||
asm_x64_write_byte_1(as, OPCODE_PUSH_R64 | src_r64);
|
||||
} else {
|
||||
asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_PUSH_R64 | (src_r64 & 7));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
void asm_x64_push_i32(asm_x64_t *as, int src_i32) {
|
||||
asm_x64_write_byte_1(as, OPCODE_PUSH_I64);
|
||||
asm_x64_write_word32(as, src_i32); // will be sign extended to 64 bits
|
||||
}
|
||||
*/
|
||||
|
||||
void asm_x64_push_disp(asm_x64_t *as, int src_r64, int src_offset) {
|
||||
assert(src_r64 < 8);
|
||||
asm_x64_write_byte_1(as, OPCODE_PUSH_M64);
|
||||
asm_x64_write_r64_disp(as, 6, src_r64, src_offset);
|
||||
}
|
||||
|
||||
void asm_x64_pop_r64(asm_x64_t *as, int dest_r64) {
|
||||
asm_x64_write_byte_1(as, OPCODE_POP_R64 | dest_r64);
|
||||
if (dest_r64 < 8) {
|
||||
asm_x64_write_byte_1(as, OPCODE_POP_R64 | dest_r64);
|
||||
} else {
|
||||
asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_POP_R64 | (dest_r64 & 7));
|
||||
}
|
||||
}
|
||||
|
||||
STATIC void asm_x64_ret(asm_x64_t *as) {
|
||||
|
@ -288,45 +300,57 @@ STATIC void asm_x64_ret(asm_x64_t *as) {
|
|||
|
||||
void asm_x64_mov_r32_to_r32(asm_x64_t *as, int src_r32, int dest_r32) {
|
||||
// defaults to 32 bit operation
|
||||
assert(src_r32 < 8);
|
||||
assert(dest_r32 < 8);
|
||||
asm_x64_write_byte_2(as, OPCODE_MOV_R64_TO_RM64, MODRM_R64(src_r32) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
|
||||
}
|
||||
|
||||
void asm_x64_mov_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
|
||||
// use REX prefix for 64 bit operation
|
||||
asm_x64_write_byte_3(as, REX_PREFIX | REX_W, OPCODE_MOV_R64_TO_RM64, MODRM_R64(src_r64) | MODRM_RM_REG | MODRM_RM_R64(dest_r64));
|
||||
asm_x64_write_byte_3(as, REX_PREFIX | REX_W | (src_r64 < 8 ? 0 : REX_R) | (dest_r64 < 8 ? 0 : REX_B), OPCODE_MOV_R64_TO_RM64, MODRM_R64(src_r64) | MODRM_RM_REG | MODRM_RM_R64(dest_r64));
|
||||
}
|
||||
|
||||
void asm_x64_mov_r64_to_disp(asm_x64_t *as, int src_r64, int dest_r64, int dest_disp) {
|
||||
// use REX prefix for 64 bit operation
|
||||
asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_R64_TO_RM64);
|
||||
assert(dest_r64 < 8);
|
||||
asm_x64_write_byte_2(as, REX_PREFIX | REX_W | (src_r64 < 8 ? 0 : REX_R), OPCODE_MOV_R64_TO_RM64);
|
||||
asm_x64_write_r64_disp(as, src_r64, dest_r64, dest_disp);
|
||||
}
|
||||
|
||||
void asm_x64_mov_disp_to_r64(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
|
||||
// use REX prefix for 64 bit operation
|
||||
asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_RM64_TO_R64);
|
||||
assert(src_r64 < 8);
|
||||
asm_x64_write_byte_2(as, REX_PREFIX | REX_W | (dest_r64 < 8 ? 0 : REX_R), OPCODE_MOV_RM64_TO_R64);
|
||||
asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
|
||||
}
|
||||
|
||||
void asm_x64_lea_disp_to_r64(asm_x64_t *as, int src_r64, int src_disp, int dest_r64) {
|
||||
// use REX prefix for 64 bit operation
|
||||
assert(src_r64 < 8);
|
||||
assert(dest_r64 < 8);
|
||||
asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_LEA_MEM_TO_R64);
|
||||
asm_x64_write_r64_disp(as, dest_r64, src_r64, src_disp);
|
||||
}
|
||||
|
||||
void asm_x64_mov_i8_to_r8(asm_x64_t *as, int src_i8, int dest_r64) {
|
||||
assert(dest_r64 < 8);
|
||||
asm_x64_write_byte_2(as, OPCODE_MOV_I8_TO_R8 | dest_r64, src_i8);
|
||||
}
|
||||
|
||||
void asm_x64_mov_i32_to_r64(asm_x64_t *as, int src_i32, int dest_r64) {
|
||||
STATIC void asm_x64_mov_i32_to_r64(asm_x64_t *as, int src_i32, int dest_r64) {
|
||||
// cpu defaults to i32 to r64, with zero extension
|
||||
asm_x64_write_byte_1(as, OPCODE_MOV_I64_TO_R64 | dest_r64);
|
||||
if (dest_r64 < 8) {
|
||||
asm_x64_write_byte_1(as, OPCODE_MOV_I64_TO_R64 | dest_r64);
|
||||
} else {
|
||||
asm_x64_write_byte_2(as, REX_PREFIX | REX_B, OPCODE_MOV_I64_TO_R64 | (dest_r64 & 7));
|
||||
}
|
||||
asm_x64_write_word32(as, src_i32);
|
||||
}
|
||||
|
||||
void asm_x64_mov_i64_to_r64(asm_x64_t *as, int64_t src_i64, int dest_r64) {
|
||||
// cpu defaults to i32 to r64
|
||||
// to mov i64 to r64 need to use REX prefix
|
||||
assert(dest_r64 < 8);
|
||||
asm_x64_write_byte_2(as, REX_PREFIX | REX_W, OPCODE_MOV_I64_TO_R64 | dest_r64);
|
||||
asm_x64_write_word64(as, src_i64);
|
||||
}
|
||||
|
@ -350,22 +374,19 @@ void asm_x64_mov_i64_to_r64_aligned(asm_x64_t *as, int64_t src_i64, int dest_r64
|
|||
asm_x64_mov_i64_to_r64(as, src_i64, dest_r64);
|
||||
}
|
||||
|
||||
void asm_x64_mov_i32_to_disp(asm_x64_t *as, int src_i32, int dest_r32, int dest_disp)
|
||||
{
|
||||
assert(0);
|
||||
asm_x64_write_byte_1(as, OPCODE_MOV_I32_TO_RM32);
|
||||
//asm_x64_write_r32_disp(as, 0, dest_r32, dest_disp);
|
||||
asm_x64_write_word32(as, src_i32);
|
||||
}
|
||||
|
||||
void asm_x64_xor_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
|
||||
assert(src_r64 < 8);
|
||||
assert(dest_r64 < 8);
|
||||
asm_x64_write_byte_3(as, REX_PREFIX | REX_W, OPCODE_XOR_R64_TO_RM64, MODRM_R64(src_r64) | MODRM_RM_REG | MODRM_RM_R64(dest_r64));
|
||||
}
|
||||
|
||||
void asm_x64_add_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64) {
|
||||
assert(src_r64 < 8);
|
||||
assert(dest_r64 < 8);
|
||||
asm_x64_write_byte_3(as, REX_PREFIX | REX_W, OPCODE_ADD_R64_TO_RM64, MODRM_R64(src_r64) | MODRM_RM_REG | MODRM_RM_R64(dest_r64));
|
||||
}
|
||||
|
||||
/*
|
||||
void asm_x64_add_i32_to_r32(asm_x64_t *as, int src_i32, int dest_r32)
|
||||
{
|
||||
assert(dest_r32 != REG_RSP); // in this case i think src_i32 must be 64 bits
|
||||
|
@ -380,17 +401,23 @@ void asm_x64_add_i32_to_r32(asm_x64_t *as, int src_i32, int dest_r32)
|
|||
asm_x64_write_word32(as, src_i32);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
void asm_x64_sub_r32_from_r32(asm_x64_t *as, int src_r32, int dest_r32) {
|
||||
// defaults to 32 bit operation
|
||||
asm_x64_write_byte_2(as, OPCODE_SUB_R64_FROM_RM64, MODRM_R64(src_r32) | MODRM_RM_REG | MODRM_RM_R64(dest_r32));
|
||||
}
|
||||
*/
|
||||
|
||||
void asm_x64_sub_r64_from_r64(asm_x64_t *as, int src_r64, int dest_r64) {
|
||||
// use REX prefix for 64 bit operation
|
||||
assert(src_r64 < 8);
|
||||
assert(dest_r64 < 8);
|
||||
asm_x64_write_byte_3(as, REX_PREFIX | REX_W, OPCODE_SUB_R64_FROM_RM64, MODRM_R64(src_r64) | MODRM_RM_REG | MODRM_RM_R64(dest_r64));
|
||||
}
|
||||
|
||||
/*
|
||||
void asm_x64_sub_i32_from_r32(asm_x64_t *as, int src_i32, int dest_r32) {
|
||||
if (SIGNED_FIT8(src_i32)) {
|
||||
// defaults to 32 bit operation
|
||||
|
@ -402,8 +429,10 @@ void asm_x64_sub_i32_from_r32(asm_x64_t *as, int src_i32, int dest_r32) {
|
|||
asm_x64_write_word32(as, src_i32);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
void asm_x64_sub_i32_from_r64(asm_x64_t *as, int src_i32, int dest_r64) {
|
||||
assert(dest_r64 < 8);
|
||||
if (SIGNED_FIT8(src_i32)) {
|
||||
// use REX prefix for 64 bit operation
|
||||
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));
|
||||
|
@ -415,7 +444,7 @@ void asm_x64_sub_i32_from_r64(asm_x64_t *as, int src_i32, int dest_r64) {
|
|||
}
|
||||
}
|
||||
|
||||
/* shifts not tested */
|
||||
/*
|
||||
void asm_x64_shl_r32_by_imm(asm_x64_t *as, int r32, int imm) {
|
||||
asm_x64_write_byte_2(as, OPCODE_SHL_RM32_BY_I8, MODRM_R64(4) | MODRM_RM_REG | MODRM_RM_R64(r32));
|
||||
asm_x64_write_byte_1(as, imm);
|
||||
|
@ -430,23 +459,15 @@ void asm_x64_sar_r32_by_imm(asm_x64_t *as, int r32, int imm) {
|
|||
asm_x64_write_byte_2(as, OPCODE_SAR_RM32_BY_I8, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(r32));
|
||||
asm_x64_write_byte_1(as, imm);
|
||||
}
|
||||
*/
|
||||
|
||||
void asm_x64_cmp_r64_with_r64(asm_x64_t *as, int src_r64_a, int src_r64_b) {
|
||||
assert(src_r64_a < 8);
|
||||
assert(src_r64_b < 8);
|
||||
asm_x64_write_byte_3(as, REX_PREFIX | REX_W, OPCODE_CMP_R64_WITH_RM64, MODRM_R64(src_r64_a) | MODRM_RM_REG | MODRM_RM_R64(src_r64_b));
|
||||
}
|
||||
|
||||
void asm_x64_cmp_r32_with_disp(asm_x64_t *as, int src_r32_a, int src_r32_b, int src_disp_b) {
|
||||
assert(0);
|
||||
asm_x64_write_byte_1(as, OPCODE_CMP_R64_WITH_RM64);
|
||||
//asm_x64_write_r32_disp(as, src_r32_a, src_r32_b, src_disp_b);
|
||||
}
|
||||
|
||||
void asm_x64_cmp_disp_with_r32(asm_x64_t *as, int src_r32_a, int src_disp_a, int src_r32_b) {
|
||||
assert(0);
|
||||
asm_x64_write_byte_1(as, OPCODE_CMP_RM32_WITH_R32);
|
||||
//asm_x64_write_r32_disp(as, src_r32_b, src_r32_a, src_disp_a);
|
||||
}
|
||||
|
||||
/*
|
||||
void asm_x64_cmp_i32_with_r32(asm_x64_t *as, int src_i32, int src_r32) {
|
||||
if (SIGNED_FIT8(src_i32)) {
|
||||
asm_x64_write_byte_2(as, OPCODE_CMP_I8_WITH_RM32, MODRM_R64(7) | MODRM_RM_REG | MODRM_RM_R64(src_r32));
|
||||
|
@ -456,6 +477,7 @@ void asm_x64_cmp_i32_with_r32(asm_x64_t *as, int src_i32, int src_r32) {
|
|||
asm_x64_write_word32(as, src_i32);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
void asm_x64_test_r8_with_r8(asm_x64_t *as, int src_r64_a, int src_r64_b) {
|
||||
// TODO implement for other registers
|
||||
|
@ -465,6 +487,7 @@ void asm_x64_test_r8_with_r8(asm_x64_t *as, int src_r64_a, int src_r64_b) {
|
|||
}
|
||||
|
||||
void asm_x64_setcc_r8(asm_x64_t *as, int jcc_type, int dest_r8) {
|
||||
assert(dest_r8 < 8);
|
||||
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));
|
||||
}
|
||||
|
||||
|
@ -539,30 +562,19 @@ void asm_x64_entry(asm_x64_t *as, int num_locals) {
|
|||
num_locals |= 1; // make it odd so stack is aligned on 16 byte boundary
|
||||
asm_x64_sub_i32_from_r64(as, num_locals * WORD_SIZE, REG_RSP);
|
||||
asm_x64_push_r64(as, REG_RBX);
|
||||
asm_x64_push_r64(as, REG_R12);
|
||||
asm_x64_push_r64(as, REG_R13);
|
||||
as->num_locals = num_locals;
|
||||
}
|
||||
|
||||
void asm_x64_exit(asm_x64_t *as) {
|
||||
asm_x64_pop_r64(as, REG_R13);
|
||||
asm_x64_pop_r64(as, REG_R12);
|
||||
asm_x64_pop_r64(as, REG_RBX);
|
||||
asm_x64_write_byte_1(as, OPCODE_LEAVE);
|
||||
asm_x64_ret(as);
|
||||
}
|
||||
|
||||
void asm_x64_push_arg(asm_x64_t *as, int src_arg_num) {
|
||||
assert(0);
|
||||
asm_x64_push_disp(as, REG_RBP, 8 + src_arg_num * WORD_SIZE);
|
||||
}
|
||||
|
||||
void asm_x64_mov_arg_to_r32(asm_x64_t *as, int src_arg_num, int dest_r32) {
|
||||
assert(0);
|
||||
//asm_x64_mov_disp_to_r32(as, REG_RBP, 8 + src_arg_num * WORD_SIZE, dest_r32);
|
||||
}
|
||||
|
||||
void asm_x64_mov_r32_to_arg(asm_x64_t *as, int src_r32, int dest_arg_num) {
|
||||
assert(0);
|
||||
//asm_x64_mov_r32_to_disp(as, src_r32, REG_RBP, 8 + dest_arg_num * WORD_SIZE);
|
||||
}
|
||||
|
||||
// locals:
|
||||
// - stored on the stack in ascending order
|
||||
// - numbered 0 through as->num_locals-1
|
||||
|
@ -595,6 +607,7 @@ void asm_x64_mov_local_addr_to_r64(asm_x64_t *as, int local_num, int dest_r64) {
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
void asm_x64_push_local(asm_x64_t *as, int local_num) {
|
||||
asm_x64_push_disp(as, REG_RBP, asm_x64_local_offset_from_ebp(as, local_num));
|
||||
}
|
||||
|
@ -605,6 +618,7 @@ void asm_x64_push_local_addr(asm_x64_t *as, int local_num, int temp_r64)
|
|||
asm_x64_add_i32_to_r32(as, asm_x64_local_offset_from_ebp(as, local_num), temp_r64);
|
||||
asm_x64_push_r64(as, temp_r64);
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
can't use these because code might be relocated when resized
|
||||
|
@ -630,6 +644,7 @@ void asm_x64_call_i1(asm_x64_t *as, void* func, int i1)
|
|||
*/
|
||||
|
||||
void asm_x64_call_ind(asm_x64_t *as, void *ptr, int temp_r64) {
|
||||
assert(temp_r64 < 8);
|
||||
#ifdef __LP64__
|
||||
asm_x64_mov_i64_to_r64_optimised(as, (int64_t)ptr, temp_r64);
|
||||
#else
|
||||
|
|
40
py/asmx64.h
40
py/asmx64.h
|
@ -24,6 +24,13 @@
|
|||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
// AMD64 calling convention is:
|
||||
// - args pass in: RDI, RSI, RDX, RCX, R08, R09
|
||||
// - return value in RAX
|
||||
// - stack must be aligned on a 16-byte boundary before all calls
|
||||
// - RAX, RCX, RDX, RSI, RDI, R08, R09, R10, R11 are caller-save
|
||||
// - RBX, RBP, R12, R13, R14, R15 are callee-save
|
||||
|
||||
#define ASM_X64_PASS_COMPUTE (1)
|
||||
#define ASM_X64_PASS_EMIT (2)
|
||||
|
||||
|
@ -35,6 +42,14 @@
|
|||
#define REG_RBP (5)
|
||||
#define REG_RSI (6)
|
||||
#define REG_RDI (7)
|
||||
#define REG_R08 (8)
|
||||
#define REG_R09 (9)
|
||||
#define REG_R10 (10)
|
||||
#define REG_R11 (11)
|
||||
#define REG_R12 (12)
|
||||
#define REG_R13 (13)
|
||||
#define REG_R14 (14)
|
||||
#define REG_R15 (15)
|
||||
|
||||
// condition codes, used for jcc and setcc (despite their j-name!)
|
||||
#define ASM_X64_CC_JB (0x2) // below, unsigned
|
||||
|
@ -44,11 +59,6 @@
|
|||
#define ASM_X64_CC_JNE (0x5)
|
||||
#define ASM_X64_CC_JL (0xc) // less, signed
|
||||
|
||||
#define REG_RET REG_RAX
|
||||
#define REG_ARG_1 REG_RDI
|
||||
#define REG_ARG_2 REG_RSI
|
||||
#define REG_ARG_3 REG_RDX
|
||||
|
||||
typedef struct _asm_x64_t asm_x64_t;
|
||||
|
||||
asm_x64_t* asm_x64_new(uint max_num_labels);
|
||||
|
@ -60,29 +70,14 @@ void* asm_x64_get_code(asm_x64_t* as);
|
|||
|
||||
void asm_x64_nop(asm_x64_t* as);
|
||||
void asm_x64_push_r64(asm_x64_t* as, int src_r64);
|
||||
void asm_x64_push_i32(asm_x64_t* as, int src_i32); // will be sign extended to 64 bits
|
||||
void asm_x64_push_disp(asm_x64_t* as, int src_r32, int src_offset);
|
||||
void asm_x64_pop_r64(asm_x64_t* as, int dest_r64);
|
||||
void asm_x64_mov_r64_to_r64(asm_x64_t* as, int src_r64, int dest_r64);
|
||||
void asm_x64_mov_r32_to_disp(asm_x64_t* as, int src_r32, int dest_r32, int dest_disp);
|
||||
void asm_x64_mov_disp_to_r32(asm_x64_t* as, int src_r32, int src_disp, int dest_r32);
|
||||
void asm_x64_mov_i32_to_r64(asm_x64_t* as, int src_i32, int dest_r64);
|
||||
void asm_x64_mov_i64_to_r64(asm_x64_t* as, int64_t src_i64, int dest_r64);
|
||||
void asm_x64_mov_i32_to_disp(asm_x64_t* as, int src_i32, int dest_r32, int dest_disp);
|
||||
void asm_x64_mov_i64_to_r64_optimised(asm_x64_t *as, int64_t src_i64, int dest_r64);
|
||||
void asm_x64_mov_i64_to_r64_aligned(asm_x64_t *as, int64_t src_i64, int dest_r64);
|
||||
void asm_x64_xor_r64_to_r64(asm_x64_t *as, int src_r64, int dest_r64);
|
||||
void asm_x64_add_r64_to_r64(asm_x64_t* as, int src_r64, int dest_r64);
|
||||
void asm_x64_add_i32_to_r32(asm_x64_t* as, int src_i32, int dest_r32);
|
||||
void asm_x64_sub_r32_from_r32(asm_x64_t* as, int src_r32, int dest_r32);
|
||||
void asm_x64_sub_i32_from_r32(asm_x64_t* as, int src_i32, int dest_r32);
|
||||
void asm_x64_shl_r32_by_imm(asm_x64_t* as, int r32, int imm);
|
||||
void asm_x64_shr_r32_by_imm(asm_x64_t* as, int r32, int imm);
|
||||
void asm_x64_sar_r32_by_imm(asm_x64_t* as, int r32, int imm);
|
||||
void asm_x64_cmp_r64_with_r64(asm_x64_t* as, int src_r64_a, int src_r64_b);
|
||||
void asm_x64_cmp_r32_with_disp(asm_x64_t* as, int src_r32_a, int src_r32_b, int src_disp_b);
|
||||
void asm_x64_cmp_disp_with_r32(asm_x64_t* as, int src_r32_a, int src_disp_a, int src_r32_b);
|
||||
void asm_x64_cmp_i32_with_r32(asm_x64_t* as, int src_i32, int src_r32);
|
||||
void asm_x64_test_r8_with_r8(asm_x64_t* as, int src_r64_a, int src_r64_b);
|
||||
void asm_x64_setcc_r8(asm_x64_t* as, int jcc_type, int dest_r8);
|
||||
void asm_x64_label_assign(asm_x64_t* as, int label);
|
||||
|
@ -90,12 +85,7 @@ void asm_x64_jmp_label(asm_x64_t* as, int label);
|
|||
void asm_x64_jcc_label(asm_x64_t* as, int jcc_type, int label);
|
||||
void asm_x64_entry(asm_x64_t* as, int num_locals);
|
||||
void asm_x64_exit(asm_x64_t* as);
|
||||
void asm_x64_push_arg(asm_x64_t* as, int src_arg_num);
|
||||
void asm_x64_mov_arg_to_r32(asm_x64_t* as, int src_arg_num, int dest_r32);
|
||||
void asm_x64_mov_r32_to_arg(asm_x64_t* as, int src_r32, int dest_arg_num);
|
||||
void asm_x64_mov_local_to_r64(asm_x64_t* as, int src_local_num, int dest_r64);
|
||||
void asm_x64_mov_r64_to_local(asm_x64_t* as, int src_r64, int dest_local_num);
|
||||
void asm_x64_mov_local_addr_to_r64(asm_x64_t* as, int local_num, int dest_r64);
|
||||
void asm_x64_push_local(asm_x64_t* as, int local_num);
|
||||
void asm_x64_push_local_addr(asm_x64_t* as, int local_num, int temp_r32);
|
||||
void asm_x64_call_ind(asm_x64_t* as, void* ptr, int temp_r32);
|
||||
|
|
|
@ -52,11 +52,6 @@
|
|||
#define ASM_X86_CC_JNE (0x5)
|
||||
#define ASM_X86_CC_JL (0xc) // less, signed
|
||||
|
||||
#define REG_RET REG_EAX
|
||||
#define REG_ARG_1 REG_EAX
|
||||
#define REG_ARG_2 REG_ECX
|
||||
#define REG_ARG_3 REG_EDX
|
||||
|
||||
typedef struct _asm_x86_t asm_x86_t;
|
||||
|
||||
asm_x86_t* asm_x86_new(mp_uint_t max_num_labels);
|
||||
|
|
|
@ -82,12 +82,22 @@
|
|||
|
||||
#define EXPORT_FUN(name) emit_native_x64_##name
|
||||
|
||||
#define REG_TEMP0 (REG_RAX)
|
||||
#define REG_TEMP1 (REG_RDI)
|
||||
#define REG_TEMP2 (REG_RSI)
|
||||
#define REG_RET REG_RAX
|
||||
#define REG_ARG_1 REG_RDI
|
||||
#define REG_ARG_2 REG_RSI
|
||||
#define REG_ARG_3 REG_RDX
|
||||
#define REG_ARG_4 REG_RCX
|
||||
|
||||
#define REG_LOCAL_1 (REG_RBX)
|
||||
#define REG_LOCAL_NUM (1)
|
||||
// caller-save
|
||||
#define REG_TEMP0 REG_RAX
|
||||
#define REG_TEMP1 REG_RDI
|
||||
#define REG_TEMP2 REG_RSI
|
||||
|
||||
// callee-save
|
||||
#define REG_LOCAL_1 REG_RBX
|
||||
#define REG_LOCAL_2 REG_R12
|
||||
#define REG_LOCAL_3 REG_R13
|
||||
#define REG_LOCAL_NUM (3)
|
||||
|
||||
#define ASM_PASS_COMPUTE ASM_X64_PASS_COMPUTE
|
||||
#define ASM_PASS_EMIT ASM_X64_PASS_EMIT
|
||||
|
@ -189,15 +199,20 @@ STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
|
|||
|
||||
#define EXPORT_FUN(name) emit_native_x86_##name
|
||||
|
||||
#define REG_RET REG_EAX
|
||||
#define REG_ARG_1 REG_EAX
|
||||
#define REG_ARG_2 REG_ECX
|
||||
#define REG_ARG_3 REG_EDX
|
||||
|
||||
// caller-save, so can be used as temporaries
|
||||
#define REG_TEMP0 (REG_EAX)
|
||||
#define REG_TEMP1 (REG_ECX)
|
||||
#define REG_TEMP2 (REG_EDX)
|
||||
#define REG_TEMP0 REG_EAX
|
||||
#define REG_TEMP1 REG_ECX
|
||||
#define REG_TEMP2 REG_EDX
|
||||
|
||||
// callee-save, so can be used as locals
|
||||
#define REG_LOCAL_1 (REG_EBX)
|
||||
#define REG_LOCAL_2 (REG_ESI)
|
||||
#define REG_LOCAL_3 (REG_EDI)
|
||||
#define REG_LOCAL_1 REG_EBX
|
||||
#define REG_LOCAL_2 REG_ESI
|
||||
#define REG_LOCAL_3 REG_EDI
|
||||
#define REG_LOCAL_NUM (3)
|
||||
|
||||
#define ASM_PASS_COMPUTE ASM_X86_PASS_COMPUTE
|
||||
|
@ -252,6 +267,12 @@ STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
|
|||
|
||||
#define EXPORT_FUN(name) emit_native_thumb_##name
|
||||
|
||||
#define REG_RET REG_R0
|
||||
#define REG_ARG_1 REG_R0
|
||||
#define REG_ARG_2 REG_R1
|
||||
#define REG_ARG_3 REG_R2
|
||||
#define REG_ARG_4 REG_R3
|
||||
|
||||
#define REG_TEMP0 (REG_R0)
|
||||
#define REG_TEMP1 (REG_R1)
|
||||
#define REG_TEMP2 (REG_R2)
|
||||
|
@ -313,6 +334,12 @@ STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
|
|||
|
||||
#define EXPORT_FUN(name) emit_native_arm_##name
|
||||
|
||||
#define REG_RET REG_R0
|
||||
#define REG_ARG_1 REG_R0
|
||||
#define REG_ARG_2 REG_R1
|
||||
#define REG_ARG_3 REG_R2
|
||||
#define REG_ARG_4 REG_R3
|
||||
|
||||
#define REG_TEMP0 (REG_R0)
|
||||
#define REG_TEMP1 (REG_R1)
|
||||
#define REG_TEMP2 (REG_R2)
|
||||
|
@ -517,9 +544,11 @@ STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scop
|
|||
if (i == 0) {
|
||||
asm_x64_mov_r64_to_r64(emit->as, REG_ARG_1, REG_LOCAL_1);
|
||||
} else if (i == 1) {
|
||||
asm_x64_mov_r64_to_local(emit->as, REG_ARG_2, i - REG_LOCAL_NUM);
|
||||
asm_x64_mov_r64_to_r64(emit->as, REG_ARG_2, REG_LOCAL_2);
|
||||
} else if (i == 2) {
|
||||
asm_x64_mov_r64_to_local(emit->as, REG_ARG_3, i - REG_LOCAL_NUM);
|
||||
asm_x64_mov_r64_to_r64(emit->as, REG_ARG_3, REG_LOCAL_3);
|
||||
} else if (i == 3) {
|
||||
asm_x64_mov_r64_to_local(emit->as, REG_ARG_4, i - REG_LOCAL_NUM);
|
||||
} else {
|
||||
// TODO not implemented
|
||||
assert(0);
|
||||
|
@ -1017,6 +1046,10 @@ STATIC void emit_native_load_fast(emit_t *emit, qstr qstr, uint id_flags, int lo
|
|||
#if N_X64
|
||||
if (local_num == 0) {
|
||||
emit_post_push_reg(emit, vtype, REG_LOCAL_1);
|
||||
} else if (local_num == 1) {
|
||||
emit_post_push_reg(emit, vtype, REG_LOCAL_2);
|
||||
} else if (local_num == 2) {
|
||||
emit_post_push_reg(emit, vtype, REG_LOCAL_3);
|
||||
} else {
|
||||
need_reg_single(emit, REG_RAX, 0);
|
||||
asm_x64_mov_local_to_r64(emit->as, local_num - REG_LOCAL_NUM, REG_RAX);
|
||||
|
@ -1123,6 +1156,10 @@ STATIC void emit_native_store_fast(emit_t *emit, qstr qstr, int local_num) {
|
|||
#if N_X64
|
||||
if (local_num == 0) {
|
||||
emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
|
||||
} else if (local_num == 1) {
|
||||
emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
|
||||
} else if (local_num == 2) {
|
||||
emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
|
||||
} else {
|
||||
emit_pre_pop_reg(emit, &vtype, REG_RAX);
|
||||
asm_x64_mov_r64_to_local(emit->as, REG_RAX, local_num - REG_LOCAL_NUM);
|
||||
|
|
Loading…
Reference in New Issue