#! @file x86.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the instructions of x86-64 that `jit.sakura` compiles functions to, #! each encoded into the bytes of the code being made: moves, arithmetic, and #! comparisons of words in registers and in memory, through a base register and #! a displacement; jumps and calls; and the SSE2 arithmetic of doubles. A #! register is its number in the encoding, so `r8` to `r15` set a REX bit. module sakura::machine; # The general registers, numbered as the encoding numbers them. const rax: u8 = 0; const rcx: u8 = 1; const rdx: u8 = 2; const rbx: u8 = 3; const rsp: u8 = 4; const rbp: u8 = 5; const rsi: u8 = 6; const rdi: u8 = 7; const r8: u8 = 8; const r9: u8 = 9; const r10: u8 = 10; const r11: u8 = 11; const r12: u8 = 12; const r13: u8 = 13; const r14: u8 = 14; const r15: u8 = 15; # The conditions of `jcc`, `setcc`, and `cmovcc`, as the encoding numbers them. const cc_b: u8 = 0x2; #! Below, unsigned. const cc_ae: u8 = 0x3; #! Above or equal, unsigned. const cc_e: u8 = 0x4; #! Equal. const cc_ne: u8 = 0x5; #! Not equal. const cc_be: u8 = 0x6; #! Below or equal, unsigned. const cc_a: u8 = 0x7; #! Above, unsigned. const cc_s: u8 = 0x8; #! Sign. const cc_p: u8 = 0xA; #! Parity, as an unordered comparison of reals sets. const cc_np: u8 = 0xB; #! No parity. const cc_l: u8 = 0xC; #! Less, signed. const cc_ge: u8 = 0xD; #! Greater or equal, signed. const cc_le: u8 = 0xE; #! Less or equal, signed. const cc_g: u8 = 0xF; #! Greater, signed. # The extensions of the instructions of a register or memory and an immediate, # which pick the operation. const alu_add: u8 = 0; const alu_or: u8 = 1; const alu_and: u8 = 4; const alu_sub: u8 = 5; const alu_xor: u8 = 6; const alu_cmp: u8 = 7; # The extensions of the shifts. const shift_left: u8 = 4; const shift_right: u8 = 5; const shift_arithmetic: u8 = 7; #! The bytes of code being made. struct machine_code : owning { var bytes: vector; } #! Empty code. #! @returns The code. def machine_code::new(): machine_code { return machine_code { bytes = vector::::new() }; } #! How many bytes the code has. val machine_code::length: u64 { get => this.bytes.length; } def machine_code::close(&mut this): void { this.bytes.close(); } #! Adds a byte. def machine_code::byte(&mut this, value: u64): void { this.bytes.append(value as u8); } #! Adds four bytes, the low first. def machine_code::u32(&mut this, value: u64): void { for i in 0..4u { this.bytes.append((value >> (8 * i)) as u8); } } #! Adds eight bytes, the low first. def machine_code::u64(&mut this, value: u64): void { for i in 0..8u { this.bytes.append((value >> (8 * i)) as u8); } } #! Writes four bytes over those at a place, the low first. def machine_code::patch32(&mut this, at: u64, value: u64): void { for i in 0..4u { this.bytes.set(at + i, (value >> (8 * i)) as u8); } } #! Whether a word is a 32-bit immediate's sign extension. def fits32(value: u64): bool { val signed := value as i64; return signed >= -2147483648 && signed <= 2147483647; } #! Whether a word is an 8-bit immediate's sign extension. def fits8(value: u64): bool { val signed := value as i64; return signed >= -128 && signed <= 127; } # The REX prefix: `w` for 64 bits, then the high bits of the register field, the # index, and the base. One is written where any is set, or where `byte` asks, # as reaching `spl` to `dil` as bytes does. def machine_code::rex(&mut this, w: bool, reg: u8, index: u8, base: u8, byte: bool): void { var made: u64 = 0x40; if w { made = made | 8; } if reg >= 8 { made = made | 4; } if index >= 8 { made = made | 2; } if base >= 8 { made = made | 1; } if made != 0x40 || (byte && (reg >= 4 || base >= 4)) { this.byte(made); } } # The ModRM byte and what follows it for memory at a base and a displacement, # the register field `reg`: the shortest displacement, a SIB byte for `rsp` and # `r12`, and a displacement always for `rbp` and `r13`. def machine_code::memory(&mut this, reg: u8, base: u8, disp: u64): void { val r := (reg & 7) as u64; val b := (base & 7) as u64; if disp == 0 && b != 5 { this.byte((r << 3) | b); if b == 4 { this.byte(0x24); } } else if fits8(disp) { this.byte(0x40 | (r << 3) | b); if b == 4 { this.byte(0x24); } this.byte(disp & 0xFF); } else { this.byte(0x80 | (r << 3) | b); if b == 4 { this.byte(0x24); } this.u32(disp); } } # The ModRM byte of two registers. def machine_code::registers(&mut this, reg: u8, rm: u8): void { this.byte(0xC0 | (((reg & 7) as u64) << 3) | (rm & 7) as u64); } # An instruction of a register and memory: its prefix, if any, REX, its opcode # bytes, then the memory. def machine_code::op_mem(&mut this, prefix: u64, w: bool, opcode: u64, reg: u8, base: u8, disp: u64): void { if prefix != 0 { this.byte(prefix); } this.rex(w, reg, 0, base, false); if opcode > 0xFF { this.byte(opcode >> 8); } this.byte(opcode & 0xFF); this.memory(reg, base, disp); } # An instruction of two registers, likewise. def machine_code::op_reg(&mut this, prefix: u64, w: bool, opcode: u64, reg: u8, rm: u8): void { if prefix != 0 { this.byte(prefix); } this.rex(w, reg, 0, rm, false); if opcode > 0xFF { this.byte(opcode >> 8); } this.byte(opcode & 0xFF); this.registers(reg, rm); } #! `mov reg, [base + disp]`. def machine_code::load(&mut this, reg: u8, base: u8, disp: u64): void { this.op_mem(0, true, 0x8B, reg, base, disp); } #! `mov [base + disp], reg`. def machine_code::store(&mut this, base: u8, disp: u64, reg: u8): void { this.op_mem(0, true, 0x89, reg, base, disp); } #! `mov qword [base + disp], imm32`, sign extended. def machine_code::store_imm(&mut this, base: u8, disp: u64, imm: u64): void { this.op_mem(0, true, 0xC7, 0, base, disp); this.u32(imm); } #! `mov to, from`. def machine_code::mov(&mut this, to: u8, from: u8): void { this.op_reg(0, true, 0x89, from, to); } #! A word into a register, by the shortest encoding. def machine_code::mov_imm(&mut this, reg: u8, value: u64): void { if value <= 0xFFFFFFFF { this.rex(false, 0, 0, reg, false); this.byte(0xB8 + (reg & 7) as u64); this.u32(value); } else if fits32(value) { this.rex(true, 0, 0, reg, false); this.byte(0xC7); this.registers(0, reg); this.u32(value); } else { this.rex(true, 0, 0, reg, false); this.byte(0xB8 + (reg & 7) as u64); this.u64(value); } } #! `mov rax, [address]`, of an address of 64 bits. def machine_code::load_rax_absolute(&mut this, address: u64): void { this.byte(0x48); this.byte(0xA1); this.u64(address); } #! `lea reg, [base + disp]`. def machine_code::lea(&mut this, reg: u8, base: u8, disp: u64): void { this.op_mem(0, true, 0x8D, reg, base, disp); } #! `lea reg, [base + index * 2^scale]`. def machine_code::lea_index(&mut this, reg: u8, base: u8, index: u8, scale: u64): void { this.rex(true, reg, index, base, false); this.byte(0x8D); if (base & 7) == 5 { this.byte(0x44 | (((reg & 7) as u64) << 3)); this.byte((scale << 6) | (((index & 7) as u64) << 3) | (base & 7) as u64); this.byte(0); } else { this.byte(0x04 | (((reg & 7) as u64) << 3)); this.byte((scale << 6) | (((index & 7) as u64) << 3) | (base & 7) as u64); } } #! An arithmetic of two registers: `add`, `or`, `and`, `sub`, `xor`, or `cmp`, by #! its extension. def machine_code::alu(&mut this, ext: u8, to: u8, from: u8): void { this.op_reg(0, true, (ext as u64) * 8 + 1, from, to); } #! An arithmetic of a register and memory, the register written. def machine_code::alu_mem(&mut this, ext: u8, reg: u8, base: u8, disp: u64): void { this.op_mem(0, true, (ext as u64) * 8 + 3, reg, base, disp); } #! An arithmetic of a register and an immediate, sign extended from 32 bits. def machine_code::alu_imm(&mut this, ext: u8, reg: u8, imm: u64): void { this.rex(true, 0, 0, reg, false); if fits8(imm) { this.byte(0x83); this.registers(ext, reg); this.byte(imm & 0xFF); } else { this.byte(0x81); this.registers(ext, reg); this.u32(imm); } } #! An arithmetic of memory and an immediate, sign extended from 32 bits. def machine_code::alu_mem_imm(&mut this, ext: u8, base: u8, disp: u64, imm: u64): void { if fits8(imm) { this.op_mem(0, true, 0x83, ext, base, disp); this.byte(imm & 0xFF); } else { this.op_mem(0, true, 0x81, ext, base, disp); this.u32(imm); } } #! `test a, b`. def machine_code::test(&mut this, a: u8, b: u8): void { this.op_reg(0, true, 0x85, b, a); } #! `test eax, eax`. def machine_code::test_status(&mut this): void { this.byte(0x85); this.byte(0xC0); } #! `imul reg, from`. def machine_code::imul(&mut this, reg: u8, from: u8): void { this.op_reg(0, true, 0x0FAF, reg, from); } #! `imul reg, [base + disp]`. def machine_code::imul_mem(&mut this, reg: u8, base: u8, disp: u64): void { this.op_mem(0, true, 0x0FAF, reg, base, disp); } #! `imul reg, from, imm32`. def machine_code::imul_imm(&mut this, reg: u8, from: u8, imm: u64): void { this.op_reg(0, true, 0x69, reg, from); this.u32(imm); } #! A shift of a register by `cl`. def machine_code::shift_cl(&mut this, ext: u8, reg: u8): void { this.op_reg(0, true, 0xD3, ext, reg); } #! A shift of a register by an immediate. def machine_code::shift_imm(&mut this, ext: u8, reg: u8, count: u64): void { this.op_reg(0, true, 0xC1, ext, reg); this.byte(count & 0xFF); } #! An operation of one register, `F7` and its extension: `not` 2, `neg` 3, `div` #! 6, and `idiv` 7. def machine_code::unary(&mut this, ext: u8, reg: u8): void { this.op_reg(0, true, 0xF7, ext, reg); } #! `cqo`. def machine_code::cqo(&mut this): void { this.byte(0x48); this.byte(0x99); } #! `setcc al`, then `movzx eax, al`. def machine_code::set_rax(&mut this, cc: u8): void { this.byte(0x0F); this.byte(0x90 + cc as u64); this.byte(0xC0); this.byte(0x0F); this.byte(0xB6); this.byte(0xC0); } #! `setcc` of the low byte of `rax`, `rcx`, or `rdx`. def machine_code::set_low(&mut this, cc: u8, reg: u8): void { this.byte(0x0F); this.byte(0x90 + cc as u64); this.byte(0xC0 + reg as u64); } #! `cmovcc to, from`. def machine_code::cmov(&mut this, cc: u8, to: u8, from: u8): void { this.op_reg(0, true, 0x0F40 + cc as u64, to, from); } #! `btc reg, 63`, which turns a double's sign. def machine_code::flip_sign(&mut this, reg: u8): void { this.op_reg(0, true, 0x0FBA, 7, reg); this.byte(63); } #! `popcnt`, `bsr`, or `bsf` of a register, by its second opcode byte. def machine_code::bits(&mut this, prefix: u64, opcode: u64, to: u8, from: u8): void { this.op_reg(prefix, true, 0x0F00 + opcode, to, from); } #! `bswap reg`. def machine_code::bswap(&mut this, reg: u8): void { this.rex(true, 0, 0, reg, false); this.byte(0x0F); this.byte(0xC8 + (reg & 7) as u64); } #! A load narrower than a word, by its opcode: `movzx` and `movsx` of bytes and #! halves, `mov` of 32 bits, which clears the high half, or `movsxd`. def machine_code::load_narrow(&mut this, opcode: u64, w: bool, reg: u8, base: u8, disp: u64): void { this.op_mem(0, w, opcode, reg, base, disp); } #! A store of the low bytes of a register: 1, 2, or 4. def machine_code::store_narrow(&mut this, size: u64, base: u8, disp: u64, reg: u8): void { if size == 2 { this.byte(0x66); } this.rex(false, reg, 0, base, size == 1); this.byte(size == 1 ? 0x88 : 0x89); this.memory(reg, base, disp); } #! A store of an immediate of 1, 2, or 4 bytes. def machine_code::store_narrow_imm(&mut this, size: u64, base: u8, disp: u64, imm: u64): void { if size == 2 { this.byte(0x66); } this.rex(false, 0, 0, base, false); this.byte(size == 1 ? 0xC6 : 0xC7); this.memory(0, base, disp); if size == 1 { this.byte(imm & 0xFF); } else if size == 2 { this.byte(imm & 0xFF); this.byte((imm >> 8) & 0xFF); } else { this.u32(imm); } } #! An SSE instruction of an XMM register and memory: `movsd` loads with `F2 10`, #! stores with `F2 11`, and the arithmetic of doubles. def machine_code::sse_mem(&mut this, prefix: u64, opcode: u64, xmm: u8, base: u8, disp: u64): void { this.op_mem(prefix, false, 0x0F00 + opcode, xmm, base, disp); } #! `movaps to, from`, copying the whole of an XMM register. def machine_code::movaps(&mut this, to: u8, from: u8): void { return if to == from; this.op_reg(0, false, 0x0F28, to, from); } #! `movdqu [base + disp], xmm`, keeping the whole of a register. def machine_code::save_xmm(&mut this, base: u8, disp: u64, xmm: u8): void { this.op_mem(0xF3, false, 0x0F7F, xmm, base, disp); } #! `movdqu xmm, [base + disp]`. def machine_code::restore_xmm(&mut this, xmm: u8, base: u8, disp: u64): void { this.op_mem(0xF3, false, 0x0F6F, xmm, base, disp); } #! An SSE instruction of two XMM registers. def machine_code::sse_reg(&mut this, prefix: u64, opcode: u64, to: u8, from: u8): void { this.op_reg(prefix, false, 0x0F00 + opcode, to, from); } #! `movq xmm, reg`. def machine_code::movq_to_xmm(&mut this, xmm: u8, reg: u8): void { this.op_reg(0x66, true, 0x0F6E, xmm, reg); } #! `movq reg, xmm`. def machine_code::movq_from_xmm(&mut this, reg: u8, xmm: u8): void { this.op_reg(0x66, true, 0x0F7E, xmm, reg); } #! `cvtsi2sd xmm, reg`. def machine_code::cvtsi2sd(&mut this, xmm: u8, reg: u8): void { this.op_reg(0xF2, true, 0x0F2A, xmm, reg); } #! `cvttsd2si reg, xmm`. def machine_code::cvttsd2si(&mut this, reg: u8, xmm: u8): void { this.op_reg(0xF2, true, 0x0F2C, reg, xmm); } #! `jcc rel32` to a place not yet known, giving where its displacement is. def machine_code::jcc(&mut this, cc: u8): u64 { this.byte(0x0F); this.byte(0x80 + cc as u64); val at := this.length; this.u32(0); return at; } #! `jmp rel32` to a place not yet known, giving where its displacement is. def machine_code::jmp(&mut this): u64 { this.byte(0xE9); val at := this.length; this.u32(0); return at; } #! Points the displacement of a jump at a place in the code. def machine_code::land(&mut this, at: u64, target: u64): void { this.patch32(at, (target - (at + 4)) & 0xFFFFFFFF); } #! `call reg`. def machine_code::call_reg(&mut this, reg: u8): void { this.rex(false, 0, 0, reg, false); this.byte(0xFF); this.registers(2, reg); } #! Calls a function at an address through `rax`. def machine_code::call_absolute(&mut this, address: u64): void { this.mov_imm(rax, address); this.call_reg(rax); } #! `ret`. def machine_code::ret(&mut this): void { this.byte(0xC3); } #! `push reg`. def machine_code::push(&mut this, reg: u8): void { if reg >= 8 { this.byte(0x41); } this.byte(0x50 + (reg & 7) as u64); } #! `pop reg`. def machine_code::pop(&mut this, reg: u8): void { if reg >= 8 { this.byte(0x41); } this.byte(0x58 + (reg & 7) as u64); }