Files
binary/amd64/assembly.sakura
T

1069 lines
36 KiB
Plaintext
Raw Permalink Normal View History

2026-10-26 06:02:30 -05:00
#! @file assembly.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the encoder of AMD64's instructions, x86-64's: the registers, the
#! operands in memory, and `assembly`, which writes each instruction's bytes one
#! after another, with the jumps and calls it writes patched once their targets
#! are known. Every integer operation is on 64 bits unless it takes a width.
#! Registers are numbered as the hardware numbers them, so `reg::r8` is 8, and
#! SSE registers are numbers of their own in the same range, so `xmm1` is 1.
import std::io;
module binary::amd64;
#! The general registers, as the hardware numbers them.
pub enum reg : u8 {
rax, rcx, rdx, rbx, rsp, rbp, rsi, rdi,
r8, r9, r10, r11, r12, r13, r14, r15,
none, #! No base or index in a memory operand.
}
#! The conditions of `jcc`, `setcc`, and `cmovcc`, as the hardware numbers
#! them. A condition's opposite is the one its lowest bit flipped gives.
pub enum cc : u8 {
overflow, no_overflow, below, above_equal, equal, not_equal, below_equal, above,
sign, no_sign, parity, no_parity, less, greater_equal, less_equal, greater,
}
#! Arithmetic of two operands, as the hardware numbers it.
pub enum alu : u8 { add, bor, adc, sbb, band, sub, bxor, cmp }
#! Shifts, as the hardware numbers them.
pub enum shift : u8 { rol = 0, ror = 1, shl = 4, shr = 5, sar = 7 }
#! Operations of one operand, where multiplying and dividing use `rdx:rax`.
pub enum unary : u8 { bnot = 2, neg = 3, mul = 4, div = 6, idiv = 7 }
#! Scalar double arithmetic, by the second byte of its opcode.
pub enum sse : u8 { sqrtsd = 0x51, addsd = 0x58, mulsd = 0x59, subsd = 0x5C, divsd = 0x5E }
#! A memory operand, `[base + index * scale + disp]`, where either register
#! may be `none`. Without either, the displacement is an absolute address.
pub struct mem {
pub var base: reg; #! The base, or `none`.
pub var index: reg; #! The index, or `none`.
pub var scale: u8; #! What the index is multiplied by: 1, 2, 4, or 8.
pub var disp: i32; #! The displacement.
pub var own: bool = false; #! Whether it is within the running thread's own record, through `gs`.
}
#! The memory at an offset from a register.
#! @param base The register.
#! @param disp The offset.
#! @returns The operand.
pub def at(base: reg, disp: i32): mem {
return mem { base = base, index = reg::none, scale = 1, disp = disp };
}
#! The memory at an offset from a register, plus another register times a scale.
#! @param base The register.
#! @param index The other register.
#! @param scale What it is multiplied by: 1, 2, 4, or 8.
#! @param disp The offset.
#! @returns The operand.
pub def indexed(base: reg, index: reg, scale: u8, disp: i32): mem {
return mem { base = base, index = index, scale = scale, disp = disp };
}
#! The memory at an address, which fits in 31 bits as every address of an
#! executable's image does.
#! @param address The address.
#! @returns The operand.
pub def absolute(address: u64): mem {
return mem { base = reg::none, index = reg::none, scale = 1, disp = address as i32 };
}
#! The bytes at an offset within the running thread's own record, which `gs`
#! points at, as each thread's base of `gs` is its own.
#! @param offset The offset.
#! @returns The memory.
pub def own(offset: u64): mem {
return mem { base = reg::none, index = reg::none, scale = 1, disp = offset as i32, own = true };
}
#! The bytes at the offset a register holds from where `gs` points, as Windows
#! reaches a slot of its record of the running thread.
#! @param base The register.
#! @returns The memory.
pub def own_at(base: reg): mem {
return mem { base = base, index = reg::none, scale = 1, disp = 0, own = true };
}
#! Whether a word is what a sign-extended 32-bit immediate holds.
#! @param value The word.
#! @returns Whether it is.
pub def fits_i32(value: u64): bool {
val signed := value as i64;
return signed >= -2147483648 && signed <= 2147483647;
}
#! Machine code being made, a byte at a time, which a byte writer holds.
pub struct assembly : owning {
pub var bytes: byte_writer; #! The code.
pub var fixups: vector<u64>; #! Where `patch` wrote a displacement, then where it goes to, for each while `logging`.
pub var logging: bool; #! Whether `patch` notes what it writes in `fixups`.
pub var avx: bool; #! Whether SSE's instructions are written with VEX, as code for processors with AVX writes them, so none meets the upper half of a register holding 32 bytes.
}
#! Makes empty code.
#! @returns The code.
pub def assembly::new(): assembly {
return assembly { bytes = byte_writer::new(), fixups = vector::<u64>::new() };
}
#! How many bytes the code holds, which is where the next begins.
#! @param this The code.
#! @returns How many.
pub val assembly::length: u64 {
get => this.bytes.length;
}
#! Gives back the memory the code holds.
#! @param this The code.
pub def assembly::close(&mut this): void {
this.bytes.close();
this.fixups.close();
}
#! Adds a byte.
#! @param this The code.
#! @param value The byte.
pub def assembly::byte(&mut this, value: u8): void {
this.bytes.byte(value);
}
#! Adds a 16-bit word, least significant byte first, as x86-64 stores it.
#! @param this The code.
#! @param value The word.
pub def assembly::u16(&mut this, value: u64): void {
this.bytes.u16(value);
}
#! Adds a 32-bit word, least significant byte first.
#! @param this The code.
#! @param value The word.
pub def assembly::u32(&mut this, value: u64): void {
this.bytes.u32(value);
}
#! Adds a 64-bit word, least significant byte first.
#! @param this The code.
#! @param value The word.
pub def assembly::u64(&mut this, value: u64): void {
this.bytes.u64(value);
}
#! Writes a 32-bit word over the four bytes at a place.
#! @param this The code.
#! @param at The place, whose bytes are held.
#! @param value The word.
pub def assembly::set_u32(&mut this, at: u64, value: u64): void {
this.bytes.set_u32(at, value);
}
#! Writes a 64-bit word over the eight bytes at a place.
#! @param this The code.
#! @param at The place, whose bytes are held.
#! @param value The word.
pub def assembly::set_u64(&mut this, at: u64, value: u64): void {
this.bytes.set_u64(at, value);
}
#! Reads the 64-bit word at a place.
#! @param this The code.
#! @param at The place, whose bytes are held.
#! @returns The word.
pub def assembly::get_u64(&this, at: u64): u64 {
return this.bytes.get_u64(at);
}
#! Adds bytes.
#! @param this The code.
#! @param data The bytes.
pub def assembly::put(&mut this, data: span<u8>): void {
this.bytes.put(data);
}
#! Adds zeros until the length is a multiple of an alignment.
#! @param this The code.
#! @param align The alignment.
#! @returns The length after.
pub def assembly::align_to(&mut this, align: u64): u64 {
return this.bytes.align_to(align);
}
# Operands.
#! Adds a REX prefix for the registers where one is needed, or where `force`
#! makes the byte registers `spl`, `bpl`, `sil`, and `dil` reachable.
#! @param this The code.
#! @param wide Whether the operation is on 64 bits.
#! @param r ModRM's `reg`: a register, or an opcode's extension.
#! @param index The index register, or `reg::none`.
#! @param base The base register, or `reg::none`.
#! @param force Whether it is written though no register needs it.
pub def assembly::rex(&mut this, wide: bool, r: u32, index: reg, base: reg, force: bool): void {
var rex: u32 = 0x40;
if wide { rex = rex | 0x08; }
if (r & 8) != 0 { rex = rex | 0x04; }
if index != reg::none && (index as u32 & 8) != 0 { rex = rex | 0x02; }
if base != reg::none && (base as u32 & 8) != 0 { rex = rex | 0x01; }
if rex != 0x40 || force {
this.byte(rex as u8);
}
}
# The bits of ModRM's or SIB's scale.
def scale_bits(scale: u8): u32 {
return scale == 8 ? 3 : scale == 4 ? 2 : scale == 2 ? 1 : 0;
}
# Encodes a memory operand: a base of `rsp` or `r12`, or any index, takes a SIB
# byte, and a base of `rbp` or `r13` always takes a displacement.
def assembly::modrm_mem(&mut this, r: u32, m: mem): void {
val index: u32 = m.index == reg::none ? 4 : (m.index as u32 & 7);
if m.base == reg::none {
this.byte((0x04 | ((r & 7) << 3)) as u8);
this.byte(((scale_bits(m.scale) << 6) | (index << 3) | 5) as u8);
this.u32(m.disp as u64);
return;
}
val base := m.base as u32 & 7;
var mode: u32 = 2;
if m.disp == 0 && base != 5 {
mode = 0;
} else if m.disp >= -128 && m.disp <= 127 {
mode = 1;
}
if base == 4 || m.index != reg::none {
this.byte(((mode << 6) | ((r & 7) << 3) | 4) as u8);
this.byte(((scale_bits(m.scale) << 6) | (index << 3) | base) as u8);
} else {
this.byte(((mode << 6) | ((r & 7) << 3) | base) as u8);
}
if mode == 1 {
this.byte((m.disp as i64 & 0xFF) as u8);
} else if mode == 2 {
this.u32(m.disp as u64);
}
}
# An instruction of a register, or an opcode extension, and a memory operand:
# prefix, REX, the opcode's bytes, and ModRM. A prefix of 0 is none.
def assembly::mem_op(&mut this, prefix: u8, wide: bool, op1: u8, op2: u8, count: u32, r: u32, m: mem, force: bool): void {
if m.own {
this.byte(0x65);
}
if prefix != 0 {
this.byte(prefix);
}
this.rex(wide, r, m.index, m.base, force);
this.byte(op1);
if count > 1 { this.byte(op2); }
this.modrm_mem(r, m);
}
# The ModRM of two registers, or of an opcode extension and a register: `r` in
# its reg and `rm` in its rm.
def modrm_reg(r: u32, rm: u32): u8 {
return (0xC0 | ((r & 7) << 3) | (rm & 7)) as u8;
}
# An instruction of two register operands, where `r` goes in ModRM's reg and
# `rm` in its rm.
def assembly::reg_op(&mut this, prefix: u8, wide: bool, op1: u8, op2: u8, count: u32, r: u32, rm: u32, force: bool): void {
if prefix != 0 {
this.byte(prefix);
}
this.rex(wide, r, reg::none, (rm & 15) as reg, force);
this.byte(op1);
if count > 1 { this.byte(op2); }
this.byte(modrm_reg(r, rm));
}
# Whether a register's low byte needs a REX prefix to be named, as `sil` does.
def byte_needs_rex(r: reg): bool {
return r == reg::rsp || r == reg::rbp || r == reg::rsi || r == reg::rdi;
}
# Moves.
#! `mov dst, src` of 64 bits.
#! @param this The code.
#! @param dst The register written.
#! @param src The register read.
pub def assembly::mov(&mut this, dst: reg, src: reg): void {
this.reg_op(0, true, 0x89, 0, 1, src as u32, dst as u32, false);
}
#! Loads a constant into a register, as briefly as it can be written.
#! @param this The code.
#! @param dst The register.
#! @param value The constant.
pub def assembly::mov_imm(&mut this, dst: reg, value: u64): void {
if value <= 0xFFFFFFFF {
# Writing 32 bits zeroes the upper half.
this.rex(false, 0, reg::none, dst, false);
this.byte((0xB8 + (dst as u32 & 7)) as u8);
this.u32(value);
return;
}
if (value as i64) >= -2147483648 && (value as i64) < 0 {
this.rex(true, 0, reg::none, dst, false);
this.byte(0xC7);
this.byte(modrm_reg(0, dst as u32));
this.u32(value);
return;
}
this.rex(true, 0, reg::none, dst, false);
this.byte((0xB8 + (dst as u32 & 7)) as u8);
this.u64(value);
}
#! Loads 1, 2, 4, or 8 bytes, extending them by their sign or with zeros.
#! @param this The code.
#! @param width How many bytes.
#! @param sign Whether they extend by their sign.
#! @param dst The register written.
#! @param src The memory read.
pub def assembly::load(&mut this, width: u64, sign: bool, dst: reg, src: mem): void {
val r := dst as u32;
if width == 1 {
this.mem_op(0, sign, 0x0F, sign ? 0xBE : 0xB6, 2, r, src, false);
} else if width == 2 {
this.mem_op(0, sign, 0x0F, sign ? 0xBF : 0xB7, 2, r, src, false);
} else if width == 4 {
# Reading 32 bits zeroes the upper half.
this.mem_op(0, sign, sign ? 0x63 : 0x8B, 0, 1, r, src, false);
} else {
this.mem_op(0, true, 0x8B, 0, 1, r, src, false);
}
}
#! Stores the low 1, 2, 4, or 8 bytes of a register.
#! @param this The code.
#! @param width How many bytes.
#! @param dst The memory written.
#! @param src The register read.
pub def assembly::store(&mut this, width: u64, dst: mem, src: reg): void {
val r := src as u32;
if width == 1 {
this.mem_op(0, false, 0x88, 0, 1, r, dst, byte_needs_rex(src));
} else if width == 2 {
this.mem_op(0x66, false, 0x89, 0, 1, r, dst, false);
} else if width == 4 {
this.mem_op(0, false, 0x89, 0, 1, r, dst, false);
} else {
this.mem_op(0, true, 0x89, 0, 1, r, dst, false);
}
}
#! Stores a sign-extended 32-bit constant into 8 bytes.
#! @param this The code.
#! @param dst The memory written.
#! @param value The constant.
pub def assembly::store_imm(&mut this, dst: mem, value: i32): void {
this.mem_op(0, true, 0xC7, 0, 1, 0, dst, false);
this.u32(value as u64);
}
#! Stores a constant into 1, 2, or 4 bytes, or a sign-extended 32-bit one
#! into 8.
#! @param this The code.
#! @param width How many bytes.
#! @param dst The memory written.
#! @param value The constant, of which the low `width` bytes are written.
pub def assembly::store_imm_width(&mut this, width: u64, dst: mem, value: u64): void {
if width == 1 {
this.mem_op(0, false, 0xC6, 0, 1, 0, dst, false);
this.byte((value & 0xFF) as u8);
} else if width == 2 {
this.mem_op(0x66, false, 0xC7, 0, 1, 0, dst, false);
this.u16(value);
} else if width == 4 {
this.mem_op(0, false, 0xC7, 0, 1, 0, dst, false);
this.u32(value);
} else {
this.store_imm(dst, value as i32);
}
}
#! `lea dst, src`: the address of memory.
#! @param this The code.
#! @param dst The register written.
#! @param src The memory.
pub def assembly::lea(&mut this, dst: reg, src: mem): void {
this.mem_op(0, true, 0x8D, 0, 1, dst as u32, src, false);
}
#! `push r`.
#! @param this The code.
#! @param r The register.
pub def assembly::push(&mut this, r: reg): void {
this.rex(false, 0, reg::none, r, false);
this.byte((0x50 + (r as u32 & 7)) as u8);
}
#! `pop r`.
#! @param this The code.
#! @param r The register.
pub def assembly::pop(&mut this, r: reg): void {
this.rex(false, 0, reg::none, r, false);
this.byte((0x58 + (r as u32 & 7)) as u8);
}
#! `cmovcc dst, src`: moves where the condition holds.
#! @param this The code.
#! @param condition The condition.
#! @param dst The register written.
#! @param src The register read.
pub def assembly::cmov(&mut this, condition: cc, dst: reg, src: reg): void {
this.reg_op(0, true, 0x0F, (0x40 + condition as u32) as u8, 2, dst as u32, src as u32, false);
}
# Arithmetic.
#! `op dst, src` of two registers.
#! @param this The code.
#! @param op The operation.
#! @param dst The register written, and read first.
#! @param src The register read second.
pub def assembly::alu(&mut this, op: alu, dst: reg, src: reg): void {
this.reg_op(0, true, (op as u32 * 8 + 1) as u8, 0, 1, src as u32, dst as u32, false);
}
#! Writes a constant an instruction takes: a byte where it is small, and else
#! 32 bits.
#! @param this The code.
#! @param value The constant.
#! @param small Whether it is written as a byte.
def assembly::immediate(&mut this, value: i32, small: bool): void {
if small {
this.byte((value as i64 & 0xFF) as u8);
} else {
this.u32(value as u64);
}
}
#! Whether a constant fits a sign-extended byte.
#! @param value The constant.
#! @returns Whether it does.
def fits_byte(value: i32): bool {
return value >= -128 && value <= 127;
}
#! `op dst, value` of a register and a sign-extended 32-bit constant.
#! @param this The code.
#! @param op The operation.
#! @param dst The register.
#! @param value The constant.
pub def assembly::alu_imm(&mut this, op: alu, dst: reg, value: i32): void {
val small := fits_byte(value);
this.reg_op(0, true, small ? 0x83 : 0x81, 0, 1, op as u32, dst as u32, false);
this.immediate(value, small);
}
#! `op dst, [src]` of a register and the 8 bytes in memory.
#! @param this The code.
#! @param op The operation.
#! @param dst The register, written but by `cmp`.
#! @param src The memory.
pub def assembly::alu_load(&mut this, op: alu, dst: reg, src: mem): void {
this.mem_op(0, true, (op as u32 * 8 + 3) as u8, 0, 1, dst as u32, src, false);
}
#! `op [dst], value` of the 8 bytes in memory and a sign-extended 32-bit
#! constant.
#! @param this The code.
#! @param op The operation.
#! @param dst The memory, written but by `cmp`.
#! @param value The constant.
pub def assembly::alu_mem_imm(&mut this, op: alu, dst: mem, value: i32): void {
val small := fits_byte(value);
this.mem_op(0, true, small ? 0x83 : 0x81, 0, 1, op as u32, dst, false);
this.immediate(value, small);
}
#! `op [dst], src` of the 8 bytes in memory and a register.
#! @param this The code.
#! @param op The operation.
#! @param dst The memory, written but by `cmp`.
#! @param src The register.
pub def assembly::alu_store(&mut this, op: alu, dst: mem, src: reg): void {
this.mem_op(0, true, (op as u32 * 8 + 1) as u8, 0, 1, src as u32, dst, false);
}
#! Makes the instruction after it, which writes memory, one step no other
#! processor sees halfway.
#! @param this The code.
pub def assembly::lock(&mut this): void {
this.byte(0xF0);
}
#! Swaps the low 1, 2, 4, or 8 bytes of a register with memory, in one step no
#! other processor sees halfway, as `xchg` always is. Of fewer than 8, the rest
#! of the register is left as it was.
#! @param this The code.
#! @param width How many bytes.
#! @param m The memory.
#! @param r The register.
pub def assembly::xchg(&mut this, width: u64, m: mem, r: reg): void {
this.sized(width, false, false, 0x86, 0x87, r, m);
}
#! `lock xadd [m], r`: adds the low 1, 2, 4, or 8 bytes of a register to memory,
#! in one step, leaving in them what the memory held.
#! @param this The code.
#! @param width How many bytes.
#! @param m The memory.
#! @param r The register.
pub def assembly::lock_xadd(&mut this, width: u64, m: mem, r: reg): void {
this.sized(width, true, true, 0xC0, 0xC1, r, m);
}
#! `lock cmpxchg [m], r`: where the memory holds what the low 1, 2, 4, or 8
#! bytes of `rax` do, writes those of a register there and sets the zero flag,
#! and otherwise loads what it holds into them, in one step.
#! @param this The code.
#! @param width How many bytes.
#! @param m The memory.
#! @param r The register.
pub def assembly::lock_cmpxchg(&mut this, width: u64, m: mem, r: reg): void {
this.sized(width, true, true, 0xB0, 0xB1, r, m);
}
#! `mfence`: no read or write after it happens before every one before it has,
#! as every processor sees them.
#! @param this The code.
pub def assembly::mfence(&mut this): void {
this.byte(0x0F);
this.byte(0xAE);
this.byte(0xF0);
}
# An instruction of a register and memory of 1, 2, 4, or 8 bytes, its opcode
# `op8` for a byte and `op` otherwise, after 0x0F where it is extended, written
# as GNU as writes them: the operand size before `lock`.
def assembly::sized(&mut this, width: u64, locked: bool, extended: bool, op8: u8, op: u8, r: reg, m: mem): void {
if width == 2 {
this.byte(0x66);
}
if locked {
this.lock();
}
val code := width == 1 ? op8 : op;
val force := width == 1 && byte_needs_rex(r);
if extended {
this.mem_op(0, width == 8, 0x0F, code, 2, r as u32, m, force);
} else {
this.mem_op(0, width == 8, code, 0, 1, r as u32, m, force);
}
}
#! `test a, b`.
#! @param this The code.
#! @param a One register.
#! @param b The other.
pub def assembly::test(&mut this, a: reg, b: reg): void {
this.reg_op(0, true, 0x85, 0, 1, b as u32, a as u32, false);
}
#! `test [m], r` of the 8 bytes in memory and a register.
#! @param this The code.
#! @param m The memory.
#! @param r The register.
pub def assembly::test_mem(&mut this, m: mem, r: reg): void {
this.mem_op(0, true, 0x85, 0, 1, r as u32, m, false);
}
#! `imul dst, src`.
#! @param this The code.
#! @param dst The register written, and read first.
#! @param src The register read second.
pub def assembly::imul(&mut this, dst: reg, src: reg): void {
this.reg_op(0, true, 0x0F, 0xAF, 2, dst as u32, src as u32, false);
}
#! `imul dst, [src]`.
#! @param this The code.
#! @param dst The register written, and read first.
#! @param src The memory read second.
pub def assembly::imul_load(&mut this, dst: reg, src: mem): void {
this.mem_op(0, true, 0x0F, 0xAF, 2, dst as u32, src, false);
}
#! `imul dst, src, value`.
#! @param this The code.
#! @param dst The register written.
#! @param src The register read.
#! @param value The constant it is multiplied by.
pub def assembly::imul_imm(&mut this, dst: reg, src: reg, value: i32): void {
val small := fits_byte(value);
this.reg_op(0, true, small ? 0x6B : 0x69, 0, 1, dst as u32, src as u32, false);
this.immediate(value, small);
}
#! Copies the low 1, 2, or 4 bytes of a register into another, extending them
#! by their sign or with zeros, or all 8 bytes for a width of 8.
#! @param this The code.
#! @param width How many bytes.
#! @param sign Whether they extend by their sign.
#! @param dst The register written.
#! @param src The register read.
pub def assembly::extend(&mut this, width: u64, sign: bool, dst: reg, src: reg): void {
if width == 1 {
this.reg_op(0, sign, 0x0F, sign ? 0xBE : 0xB6, 2, dst as u32, src as u32, byte_needs_rex(src));
} else if width == 2 {
this.reg_op(0, sign, 0x0F, sign ? 0xBF : 0xB7, 2, dst as u32, src as u32, false);
} else if width == 4 {
# Writing 32 bits zeroes the upper half.
this.reg_op(0, sign, sign ? 0x63 : 0x8B, 0, 1, dst as u32, src as u32, false);
} else if dst != src {
this.mov(dst, src);
}
}
#! An operation of one register, as `neg`, `not`, `mul`, `div`, and `idiv`.
#! @param this The code.
#! @param op The operation.
#! @param r The register.
pub def assembly::unary(&mut this, op: unary, r: reg): void {
this.reg_op(0, true, 0xF7, 0, 1, op as u32, r as u32, false);
}
#! `bsr dst, src`: the index of the highest bit of `src` that is 1. Where `src` is
#! zero, it sets the zero flag and leaves `dst` as it was.
#! @param this The code.
#! @param dst The register written.
#! @param src The register read.
pub def assembly::bsr(&mut this, dst: reg, src: reg): void {
this.reg_op(0, true, 0x0F, 0xBD, 2, dst as u32, src as u32, false);
}
#! `bsf dst, src`: the index of the lowest bit of `src` that is 1, as `bsr` says
#! nothing of zero.
#! @param this The code.
#! @param dst The register written.
#! @param src The register read.
pub def assembly::bsf(&mut this, dst: reg, src: reg): void {
this.reg_op(0, true, 0x0F, 0xBC, 2, dst as u32, src as u32, false);
}
#! `bswap r`: the register's bytes in the reverse order.
#! @param this The code.
#! @param r The register.
pub def assembly::bswap(&mut this, r: reg): void {
this.rex(true, 0, reg::none, r, false);
this.byte(0x0F);
this.byte((0xC8 | (r as u32 & 7)) as u8);
}
#! Replaces the double at `[rsp]` with what is left dividing it by the double at
#! `[rsp + 8]`, as C's `fmod` does, through the x87's `fprem`, which is exact.
#! Clobbers `rax`.
#! @param this The code.
pub def assembly::fmod_stack(&mut this): void {
val code: u8[21] = [
0xDD, 0x44, 0x24, 0x08, # fld qword [rsp + 8]
0xDD, 0x04, 0x24, # fld qword [rsp]
0xD9, 0xF8, # fprem
0xDF, 0xE0, # fnstsw ax
0xF6, 0xC4, 0x04, # test ah, 4
0x75, 0xF7, # jnz back to fprem
0xDD, 0x1C, 0x24, # fstp qword [rsp]
0xDD, 0xD8, # fstp st(0)
];
this.put(code[:]);
}
#! `cqo`: extends `rax` by its sign into `rdx`.
#! @param this The code.
pub def assembly::cqo(&mut this): void {
this.byte(0x48);
this.byte(0x99);
}
#! Shifts a register by `cl`.
#! @param this The code.
#! @param op The shift.
#! @param r The register.
pub def assembly::shift_cl(&mut this, op: shift, r: reg): void {
this.reg_op(0, true, 0xD3, 0, 1, op as u32, r as u32, false);
}
#! Shifts a register by a constant.
#! @param this The code.
#! @param op The shift.
#! @param r The register.
#! @param count How far.
pub def assembly::shift_imm(&mut this, op: shift, r: reg, count: u8): void {
this.reg_op(0, true, 0xC1, 0, 1, op as u32, r as u32, false);
this.byte(count);
}
#! Sets a register to whether a condition holds: its low byte, then extended
#! with zeros.
#! @param this The code.
#! @param condition The condition.
#! @param r The register.
pub def assembly::setcc(&mut this, condition: cc, r: reg): void {
val force := byte_needs_rex(r);
this.reg_op(0, false, 0x0F, (0x90 + condition as u32) as u8, 2, 0, r as u32, force);
this.reg_op(0, false, 0x0F, 0xB6, 2, r as u32, r as u32, force);
}
# Control.
#! `ret`.
#! @param this The code.
pub def assembly::ret(&mut this): void {
this.byte(0xC3);
}
#! `leave`: puts back the stack and frame pointers of the caller.
#! @param this The code.
pub def assembly::leave(&mut this): void {
this.byte(0xC9);
}
#! `syscall`.
#! @param this The code.
pub def assembly::syscall(&mut this): void {
this.byte(0x0F);
this.byte(0x05);
}
#! `rep movsb`: copies `rcx` bytes from `rsi` to `rdi`.
#! @param this The code.
pub def assembly::rep_movsb(&mut this): void {
this.byte(0xF3);
this.byte(0xA4);
}
#! `rep stosb`: stores `al` into `rcx` bytes from `rdi`.
#! @param this The code.
pub def assembly::rep_stosb(&mut this): void {
this.byte(0xF3);
this.byte(0xAA);
}
#! `rep movsq`: copies `rcx` words from `rsi` to `rdi`.
#! @param this The code.
pub def assembly::rep_movsq(&mut this): void {
this.byte(0xF3);
this.byte(0x48);
this.byte(0xA5);
}
#! `rep stosq`: stores `rax` into `rcx` words from `rdi`.
#! @param this The code.
pub def assembly::rep_stosq(&mut this): void {
this.byte(0xF3);
this.byte(0x48);
this.byte(0xAB);
}
#! `ud2`, which stops the program.
#! @param this The code.
pub def assembly::ud2(&mut this): void {
this.byte(0x0F);
this.byte(0x0B);
}
#! `call r`.
#! @param this The code.
#! @param r The register holding the address called.
pub def assembly::call_reg(&mut this, r: reg): void {
this.reg_op(0, false, 0xFF, 0, 1, 2, r as u32, false);
}
#! `call [m]`.
#! @param this The code.
#! @param m The memory holding the address called.
pub def assembly::call_mem(&mut this, m: mem): void {
this.mem_op(0, false, 0xFF, 0, 1, 2, m, false);
}
#! `jmp r`.
#! @param this The code.
#! @param r The register holding the address jumped to.
pub def assembly::jmp_reg(&mut this, r: reg): void {
this.reg_op(0, false, 0xFF, 0, 1, 4, r as u32, false);
}
#! `jmp` to the address a word of memory holds, as an entry of a table.
#! @param this The code.
#! @param m The memory.
pub def assembly::jmp_mem(&mut this, m: mem): void {
this.mem_op(0, false, 0xFF, 0, 1, 4, m, false);
}
#! `call` of a place not yet known, patched once it is.
#! @param this The code.
#! @returns Where its 32-bit displacement is, for `patch`.
pub def assembly::call(&mut this): u64 {
this.byte(0xE8);
return this.displacement();
}
#! `jmp` to a place not yet known, patched once it is.
#! @param this The code.
#! @returns Where its 32-bit displacement is, for `patch`.
pub def assembly::jmp(&mut this): u64 {
this.byte(0xE9);
return this.displacement();
}
#! `jcc` to a place not yet known, patched once it is.
#! @param this The code.
#! @param condition Where it jumps.
#! @returns Where its 32-bit displacement is, for `patch`.
pub def assembly::jcc(&mut this, condition: cc): u64 {
this.byte(0x0F);
this.byte((0x80 + condition as u32) as u8);
return this.displacement();
}
#! A 32-bit displacement written as zero, to be patched once where it goes is
#! known.
#! @param this The code.
#! @returns Where it is, for `patch`.
def assembly::displacement(&mut this): u64 {
val at := this.bytes.length;
this.u32(0);
return at;
}
#! Points the displacement at a place to a place of the same code.
#! @param this The code.
#! @param at Where the displacement is.
#! @param target The place it goes to.
pub def assembly::patch(&mut this, at: u64, target: u64): void {
val relative := (target as i64 - (at as i64 + 4)) as u64;
this.set_u32(at, relative & 0xFFFFFFFF);
if this.logging {
this.fixups.append(at);
this.fixups.append(target);
}
}
#! Points the displacement at a place to where the code ends now, which is
#! where what is written next lands.
#! @param this The code.
#! @param at Where the displacement is.
pub def assembly::land(&mut this, at: u64): void {
this.patch(at, this.bytes.length);
}
#! Places that name what is not placed yet: where each is, and what it names,
#! by a number, each written once every one is placed.
pub struct fixup_list {
pub var at: vector<u64>; #! Where each place is, which moves with what holds it.
var target: vector<u32>; #! What each names.
}
#! Provides read only access to how many places a list holds.
#! @param this The list.
#! @returns How many.
pub val fixup_list::length: u64 {
get => this.at.length;
}
#! Makes a list of no places.
#! @param alloc The optional allocator for the memory backing of the list.
#! @returns The list.
pub def fixup_list::new(alloc: maybe<&mut allocator>): fixup_list {
return fixup_list { at = vector::<u64>::new(alloc), target = vector::<u32>::new(alloc) };
}
#! Adds a place naming what is not placed yet.
#! @param this The list.
#! @param at Where the place is.
#! @param target What it names.
pub def fixup_list::add(&mut this, at: u64, target: u32): void {
this.at.append(at);
this.target.append(target);
}
#! Points each displacement of code at where what it names was placed.
#! @param this The list.
#! @param text The code.
#! @param placed Where each thing named was placed in the code.
pub def fixup_list::resolve(&this, text: &mut assembly, placed: span<u64>): void {
for k in 0..this.at.length {
text.patch(this.at[k], placed[this.target[k]]);
}
}
#! Writes over each 32-bit place where what it names was placed.
#! @param this The list.
#! @param out The bytes holding the places.
#! @param placed Where each thing named was placed.
pub def fixup_list::resolve_words(&this, out: &mut byte_writer, placed: span<u32>): void {
for k in 0..this.at.length {
out.set_u32(this.at[k], placed[this.target[k]] as u64);
}
}
# Scalar floating point.
# An SSE instruction of two registers, `op r, rm`: with its mandatory prefix
# and the opcode after `0F`, or with VEX where the code is written so, naming
# `r` again as the operand the instruction merges where `merges`.
def assembly::sse_reg(&mut this, prefix: u8, wide: bool, opcode: u8, r: u32, rm: u32, merges: bool): void {
if !this.avx {
this.reg_op(prefix, wide, 0x0F, opcode, 2, r, rm, false);
return;
}
this.vex_opcode(sse_word(prefix, wide, opcode), false, r, merges ? r : 0, 0, rm);
this.byte(modrm_reg(r, rm));
}
# An SSE instruction of a register and memory, `op r, m`, as `sse_reg`.
def assembly::sse_mem(&mut this, prefix: u8, wide: bool, opcode: u8, r: u32, m: mem, merges: bool): void {
if !this.avx {
this.mem_op(prefix, wide, 0x0F, opcode, 2, r, m, false);
return;
}
val x: u32 = m.index != reg::none ? m.index as u32 : 0;
val b: u32 = m.base != reg::none ? m.base as u32 : 0;
this.vex_opcode(sse_word(prefix, wide, opcode), false, r, merges ? r : 0, x, b);
this.modrm_mem(r, m);
}
# The word of an SSE instruction, as `vector.sakura` writes them, from its
# mandatory prefix, whether it is 64 bits wide, and its opcode after `0F`.
def sse_word(prefix: u8, wide: bool, opcode: u8): u32 {
val pp: u32 = prefix == 0x66 ? 1 : prefix == 0xF3 ? 2 : prefix == 0xF2 ? 3 : 0;
return (wide ? vec_wide : 0) | (pp << 16) | 0x100 | opcode as u32;
}
#! `movq xmm, r`: the bits of a register into an SSE register.
#! @param this The code.
#! @param xmm The SSE register.
#! @param r The general register.
pub def assembly::movq_to_xmm(&mut this, xmm: u8, r: reg): void {
this.sse_reg(0x66, true, 0x6E, xmm as u32, r as u32, false);
}
#! `movq r, xmm`: the bits of an SSE register into a register.
#! @param this The code.
#! @param r The general register.
#! @param xmm The SSE register.
pub def assembly::movq_from_xmm(&mut this, r: reg, xmm: u8): void {
this.sse_reg(0x66, true, 0x7E, xmm as u32, r as u32, false);
}
#! `movaps dst, src`: copies an SSE register.
#! @param this The code.
#! @param dst The register written.
#! @param src The register read.
pub def assembly::movaps(&mut this, dst: u8, src: u8): void {
this.sse_reg(0, false, 0x28, dst as u32, src as u32, false);
}
#! `movsd xmm, [m]`: loads a double.
#! @param this The code.
#! @param xmm The register written.
#! @param src The memory read.
pub def assembly::movsd_load(&mut this, xmm: u8, src: mem): void {
this.sse_mem(0xF2, false, 0x10, xmm as u32, src, false);
}
#! `movsd [m], xmm`: stores a double.
#! @param this The code.
#! @param dst The memory written.
#! @param xmm The register read.
pub def assembly::movsd_store(&mut this, dst: mem, xmm: u8): void {
this.sse_mem(0xF2, false, 0x11, xmm as u32, dst, false);
}
#! An arithmetic of doubles: `op dst, src`.
#! @param this The code.
#! @param op The operation.
#! @param dst The register written, and read first.
#! @param src The register read second.
pub def assembly::sse(&mut this, op: sse, dst: u8, src: u8): void {
this.sse_reg(0xF2, false, op as u8, dst as u32, src as u32, true);
}
#! An arithmetic of doubles with one in memory: `op dst, [src]`.
#! @param this The code.
#! @param op The operation.
#! @param dst The register written, and read first.
#! @param src The memory read second.
pub def assembly::sse_load(&mut this, op: sse, dst: u8, src: mem): void {
this.sse_mem(0xF2, false, op as u8, dst as u32, src, true);
}
#! `xorpd dst, src`.
#! @param this The code.
#! @param dst The register written, and read first.
#! @param src The register read second.
pub def assembly::xorpd(&mut this, dst: u8, src: u8): void {
this.sse_reg(0x66, false, 0x57, dst as u32, src as u32, true);
}
#! `ucomisd a, b`: compares doubles, unordered where either is not a number.
#! @param this The code.
#! @param a One register.
#! @param b The other.
pub def assembly::ucomisd(&mut this, a: u8, b: u8): void {
this.sse_reg(0x66, false, 0x2E, a as u32, b as u32, false);
}
#! `cvtsi2sd xmm, r`: the double a signed word is.
#! @param this The code.
#! @param xmm The SSE register written.
#! @param r The general register read.
pub def assembly::cvtsi2sd(&mut this, xmm: u8, r: reg): void {
this.sse_reg(0xF2, true, 0x2A, xmm as u32, r as u32, true);
}
#! `cvttsd2si r, xmm`: the signed word a double is, toward zero.
#! @param this The code.
#! @param r The general register written.
#! @param xmm The SSE register read.
pub def assembly::cvttsd2si(&mut this, r: reg, xmm: u8): void {
this.sse_reg(0xF2, true, 0x2C, r as u32, xmm as u32, false);
}
#! `cvtsd2ss dst, src`: a double rounded to a float.
#! @param this The code.
#! @param dst The register written.
#! @param src The register read.
pub def assembly::cvtsd2ss(&mut this, dst: u8, src: u8): void {
this.sse_reg(0xF2, false, 0x5A, dst as u32, src as u32, true);
}
#! `cvtss2sd dst, src`: a float widened to a double.
#! @param this The code.
#! @param dst The register written.
#! @param src The register read.
pub def assembly::cvtss2sd(&mut this, dst: u8, src: u8): void {
this.sse_reg(0xF3, false, 0x5A, dst as u32, src as u32, true);
}
#! `movss xmm, [m]`: loads a float.
#! @param this The code.
#! @param xmm The register written.
#! @param src The memory read.
pub def assembly::movss_load(&mut this, xmm: u8, src: mem): void {
this.sse_mem(0xF3, false, 0x10, xmm as u32, src, false);
}
#! `movss [m], xmm`: stores a float.
#! @param this The code.
#! @param dst The memory written.
#! @param xmm The register read.
pub def assembly::movss_store(&mut this, dst: mem, xmm: u8): void {
this.sse_mem(0xF3, false, 0x11, xmm as u32, dst, false);
}