1127 lines
40 KiB
Plaintext
1127 lines
40 KiB
Plaintext
#! @file operations.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains the code of the operations that compute: arithmetic of words and
|
||
|
|
#! reals, comparisons, conversions, and reading, writing, copying, and clearing
|
||
|
|
#! memory. Each computes as the machine does, to the bit: division by zero and
|
||
|
|
#! of the least word by -1, shifts of 64 bits or more, and conversions of reals
|
||
|
|
#! past the range of words saturating.
|
||
|
|
|
||
|
|
import sakura::compiler;
|
||
|
|
import binary::amd64;
|
||
|
|
module sakura::native;
|
||
|
|
|
||
|
|
|
||
|
|
#! Applies arithmetic to a register and an operand.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param kind The arithmetic.
|
||
|
|
#! @param dst The register, written.
|
||
|
|
#! @param src The operand.
|
||
|
|
def selector::apply(&mut this, kind: alu, dst: reg, src: &operand_of): void {
|
||
|
|
match src.kind {
|
||
|
|
0 => this.b.text.alu(kind, dst, src.r);
|
||
|
|
1 => this.b.text.alu_load(kind, dst, src.m);
|
||
|
|
else => this.b.text.alu_imm(kind, dst, src.imm);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Puts the word an instruction reads into a register.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param value The value.
|
||
|
|
#! @param index The instruction reading it.
|
||
|
|
#! @param into The register.
|
||
|
|
def selector::load_into(&mut this, value: u32, index: u32, into: reg): void {
|
||
|
|
val held := this.read(value, index);
|
||
|
|
if held.kind == place::gpr {
|
||
|
|
if held.number as reg != into {
|
||
|
|
this.b.text.mov(into, held.number as reg);
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
val got := this.word_reg(value, index, into);
|
||
|
|
if got != into {
|
||
|
|
this.b.text.mov(into, got);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Puts the real an instruction reads into an SSE register.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param value The value.
|
||
|
|
#! @param index The instruction reading it.
|
||
|
|
#! @param into The register's number.
|
||
|
|
def selector::load_real_into(&mut this, value: u32, index: u32, into: u8): void {
|
||
|
|
val got := this.real_reg(value, index, into);
|
||
|
|
if got != into {
|
||
|
|
this.b.text.movaps(into, got);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a value an instruction reads is in a general register.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param value The value.
|
||
|
|
#! @param index The instruction reading it.
|
||
|
|
#! @param r The register.
|
||
|
|
#! @returns Whether it is.
|
||
|
|
def selector::is_in(&this, value: u32, index: u32, r: reg): bool {
|
||
|
|
val held := this.read(value, index);
|
||
|
|
return held.kind == place::gpr && held.number == r as u8;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a value an instruction reads is in an SSE register.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param value The value.
|
||
|
|
#! @param index The instruction reading it.
|
||
|
|
#! @param x The register's number.
|
||
|
|
#! @returns Whether it is.
|
||
|
|
def selector::is_in_xmm(&this, value: u32, index: u32, x: u8): bool {
|
||
|
|
return false if this.single[value];
|
||
|
|
val held := this.read(value, index);
|
||
|
|
return held.kind == place::xmm && held.number == x;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
# Words.
|
||
|
|
|
||
|
|
#! The code of an operator of two words.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The instruction.
|
||
|
|
#! @param held The instruction.
|
||
|
|
def selector::binary(&mut this, index: u32, held: &inst): void {
|
||
|
|
val a := this.body.arg(index, 0);
|
||
|
|
val b := this.body.arg(index, 1);
|
||
|
|
val result := held.result;
|
||
|
|
match held.kind {
|
||
|
|
op::add => this.arithmetic(index, alu::add, a, b, result, true);
|
||
|
|
op::sub => this.arithmetic(index, alu::sub, a, b, result, false);
|
||
|
|
op::band => this.arithmetic(index, alu::band, a, b, result, true);
|
||
|
|
op::bor => this.arithmetic(index, alu::bor, a, b, result, true);
|
||
|
|
op::bxor => this.arithmetic(index, alu::bxor, a, b, result, true);
|
||
|
|
op::mul => this.multiply(index, a, b, result);
|
||
|
|
op::divs, op::divu, op::rems, op::remu => this.divide(index, held.kind, a, b, result);
|
||
|
|
op::shl, op::shrs, op::shru => this.shift_by(index, held.kind, a, b, result);
|
||
|
|
op::eq, op::ne, op::lts, op::ltu, op::les, op::leu => {
|
||
|
|
return if !this.wanted(result);
|
||
|
|
val condition := this.compare(index, held.kind, a, b);
|
||
|
|
val into := this.target(result, reg::rax);
|
||
|
|
this.b.text.setcc(condition, into);
|
||
|
|
this.put(result, into);
|
||
|
|
}
|
||
|
|
op::eqsu, op::ltsu, op::lesu, op::ltus, op::leus => this.mixed(index, held.kind, a, b, result);
|
||
|
|
op::fadd => this.real_arithmetic(index, sse::addsd, a, b, result, true);
|
||
|
|
op::fsub => this.real_arithmetic(index, sse::subsd, a, b, result, false);
|
||
|
|
op::fmul => this.real_arithmetic(index, sse::mulsd, a, b, result, true);
|
||
|
|
op::fdiv => this.real_arithmetic(index, sse::divsd, a, b, result, false);
|
||
|
|
op::frem => this.real_remainder(index, a, b, result);
|
||
|
|
op::feq, op::fne, op::flt, op::fle => this.real_compare(index, held.kind, a, b, result);
|
||
|
|
else => {}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Arithmetic of two words in place: `add`, `sub`, `and`, `or`, and `xor`.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The instruction.
|
||
|
|
#! @param kind The arithmetic.
|
||
|
|
#! @param a The first operand.
|
||
|
|
#! @param b The second.
|
||
|
|
#! @param result The result.
|
||
|
|
#! @param commutes Whether the operands may be swapped.
|
||
|
|
def selector::arithmetic(&mut this, index: u32, kind: alu, a: u32, b: u32, result: u32, commutes: bool): void {
|
||
|
|
return if !this.wanted(result);
|
||
|
|
val into := this.target(result, reg::rax);
|
||
|
|
|
||
|
|
# A sum one of whose operands is a shift folded into it scales the word
|
||
|
|
# shifted as an index.
|
||
|
|
if kind == alu::add && this.scales(a, b) {
|
||
|
|
this.scaled_sum(index, result, into);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
# A sum of a register and a constant, or of two registers, into a third.
|
||
|
|
if kind == alu::add && !this.is_in(a, index, into) && !this.is_in(b, index, into) {
|
||
|
|
var imm: i32 = 0;
|
||
|
|
val left := this.read(a, index);
|
||
|
|
val right := this.read(b, index);
|
||
|
|
if left.kind == place::gpr && this.immediate(b, &mut imm) {
|
||
|
|
this.b.text.lea(into, at(left.number as reg, imm));
|
||
|
|
this.put(result, into);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if left.kind == place::gpr && right.kind == place::gpr {
|
||
|
|
this.b.text.lea(into, indexed(left.number as reg, right.number as reg, 1, 0));
|
||
|
|
this.put(result, into);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if this.is_in(a, index, into) {
|
||
|
|
val other := this.word_operand(b, index, reg::rcx);
|
||
|
|
this.apply(kind, into, &other);
|
||
|
|
} else if commutes && this.is_in(b, index, into) {
|
||
|
|
val other := this.word_operand(a, index, reg::rcx);
|
||
|
|
this.apply(kind, into, &other);
|
||
|
|
} else if this.is_in(b, index, into) {
|
||
|
|
# The second operand is where the result goes, so the first is computed
|
||
|
|
# in `rax`.
|
||
|
|
this.load_into(a, index, reg::rax);
|
||
|
|
this.b.text.alu(kind, reg::rax, into);
|
||
|
|
this.b.text.mov(into, reg::rax);
|
||
|
|
} else {
|
||
|
|
this.load_into(a, index, into);
|
||
|
|
val other := this.word_operand(b, index, reg::rcx);
|
||
|
|
this.apply(kind, into, &other);
|
||
|
|
}
|
||
|
|
this.put(result, into);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether either of two operands is a shift folded into the sum reading them.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param a One operand.
|
||
|
|
#! @param b The other.
|
||
|
|
#! @returns Whether it is.
|
||
|
|
def selector::scales(&this, a: u32, b: u32): bool {
|
||
|
|
for k in 0..2u {
|
||
|
|
val made := this.body.defining(k == 0 ? a : b);
|
||
|
|
if made != 0 && this.p.folded[made] && this.body.insts[made].kind == op::shl {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! A sum scaling the word a shift folded into it shifts, by `lea`: of the
|
||
|
|
#! other operand, a constant as the displacement, or the frame's or the
|
||
|
|
#! image's address as its own, and the index scaled.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The sum.
|
||
|
|
#! @param result The result.
|
||
|
|
#! @param into The register the result is computed into.
|
||
|
|
def selector::scaled_sum(&mut this, index: u32, result: u32, into: reg): void {
|
||
|
|
var base: u32 = 0;
|
||
|
|
var scale: u8 = 1;
|
||
|
|
val scaled := this.sum_parts(index, &mut base, &mut scale);
|
||
|
|
val by := this.word_reg(scaled, index, reg::rcx);
|
||
|
|
var imm: i32 = 0;
|
||
|
|
var below: u64 = 0;
|
||
|
|
if this.immediate(base, &mut imm) {
|
||
|
|
this.b.text.lea(into, indexed(reg::none, by, scale, imm));
|
||
|
|
} else if this.p.remade[base] && this.frame_address(base, &mut below) {
|
||
|
|
this.b.text.lea(into, indexed(reg::rbp, by, scale, -(below as i64) as i32));
|
||
|
|
} else {
|
||
|
|
this.b.text.lea(into, indexed(this.word_reg(base, index, reg::rax), by, scale, 0));
|
||
|
|
}
|
||
|
|
this.put(result, into);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `mul` of two words.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The instruction.
|
||
|
|
#! @param a The first operand.
|
||
|
|
#! @param b The second.
|
||
|
|
#! @param result The result.
|
||
|
|
def selector::multiply(&mut this, index: u32, a: u32, b: u32, result: u32): void {
|
||
|
|
return if !this.wanted(result);
|
||
|
|
val into := this.target(result, reg::rax);
|
||
|
|
var imm: i32 = 0;
|
||
|
|
if this.immediate(b, &mut imm) {
|
||
|
|
this.multiply_by(into, this.word_reg(a, index, reg::rcx), imm);
|
||
|
|
this.put(result, into);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if this.immediate(a, &mut imm) {
|
||
|
|
this.multiply_by(into, this.word_reg(b, index, reg::rcx), imm);
|
||
|
|
this.put(result, into);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
var first := a;
|
||
|
|
var second := b;
|
||
|
|
if this.is_in(b, index, into) {
|
||
|
|
first = b;
|
||
|
|
second = a;
|
||
|
|
}
|
||
|
|
this.load_into(first, index, into);
|
||
|
|
val other := this.word_operand(second, index, reg::rcx);
|
||
|
|
if other.kind == 1 {
|
||
|
|
this.b.text.imul_load(into, other.m);
|
||
|
|
} else if other.kind == 0 {
|
||
|
|
this.b.text.imul(into, other.r);
|
||
|
|
} else {
|
||
|
|
this.b.text.imul_imm(into, into, other.imm);
|
||
|
|
}
|
||
|
|
this.put(result, into);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Multiplies a register by a constant into another: a power of two by a
|
||
|
|
#! shift, or by `lea` adding it to itself for 2, and 3, 5, or 9 times a power
|
||
|
|
#! of two by `lea` adding it to itself scaled, then a shift, which are quicker
|
||
|
|
#! than `imul`; any other by `imul`.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param into The register written.
|
||
|
|
#! @param from The register multiplied.
|
||
|
|
#! @param by The constant.
|
||
|
|
def selector::multiply_by(&mut this, into: reg, from: reg, by: i32): void {
|
||
|
|
if by > 0 {
|
||
|
|
var odd := by as u64;
|
||
|
|
var shift: u8 = 0;
|
||
|
|
while (odd & 1) == 0 {
|
||
|
|
odd = odd >> 1;
|
||
|
|
shift++;
|
||
|
|
}
|
||
|
|
if odd == 1 && shift == 1 {
|
||
|
|
this.b.text.lea(into, indexed(from, from, 1, 0));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if odd == 1 || odd == 3 || odd == 5 || odd == 9 {
|
||
|
|
if odd != 1 {
|
||
|
|
this.b.text.lea(into, indexed(from, from, (odd - 1) as u8, 0));
|
||
|
|
} else if into != from {
|
||
|
|
this.b.text.mov(into, from);
|
||
|
|
}
|
||
|
|
if shift != 0 {
|
||
|
|
this.b.text.shift_imm(shift::shl, into, shift);
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
this.b.text.imul_imm(into, from, by);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Division of words, and what is left of it: by zero it stops the program, as
|
||
|
|
#! the processor faults, which the runtime reports, and by -1 a signed division
|
||
|
|
#! negates, leaving nothing over, without the hardware's overflow of the least
|
||
|
|
#! word.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The instruction.
|
||
|
|
#! @param kind The operation.
|
||
|
|
#! @param a The dividend.
|
||
|
|
#! @param b The divisor.
|
||
|
|
#! @param result The result.
|
||
|
|
def selector::divide(&mut this, index: u32, kind: op, a: u32, b: u32, result: u32): void {
|
||
|
|
val signed := kind == op::divs || kind == op::rems;
|
||
|
|
val rest := kind == op::rems || kind == op::remu;
|
||
|
|
var bits: u64 = 0;
|
||
|
|
val constant := this.p.remade[b] && this.constant_bits(b, &mut bits);
|
||
|
|
if constant && bits == 0 {
|
||
|
|
this.trap_always(fault::division as u32, index);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
return if constant && !this.wanted(result);
|
||
|
|
|
||
|
|
this.load_into(b, index, reg::rcx);
|
||
|
|
this.load_into(a, index, reg::rax);
|
||
|
|
if !signed {
|
||
|
|
this.b.text.mov_imm(reg::rdx, 0);
|
||
|
|
this.b.text.unary(unary::div, reg::rcx);
|
||
|
|
this.put(result, rest ? reg::rdx : reg::rax);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if constant && bits == 0xFFFFFFFFFFFFFFFF {
|
||
|
|
if rest {
|
||
|
|
this.b.text.mov_imm(reg::rax, 0);
|
||
|
|
} else {
|
||
|
|
this.b.text.unary(unary::neg, reg::rax);
|
||
|
|
}
|
||
|
|
this.put(result, reg::rax);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
var done: u64 = never as u64;
|
||
|
|
if !constant {
|
||
|
|
this.b.text.alu_imm(alu::cmp, reg::rcx, -1);
|
||
|
|
val normal := this.b.text.jcc(cc::not_equal);
|
||
|
|
if rest {
|
||
|
|
this.b.text.mov_imm(reg::rdx, 0);
|
||
|
|
} else {
|
||
|
|
this.b.text.unary(unary::neg, reg::rax);
|
||
|
|
}
|
||
|
|
done = this.b.text.jmp();
|
||
|
|
this.b.text.land(normal);
|
||
|
|
}
|
||
|
|
this.b.text.cqo();
|
||
|
|
this.b.text.unary(unary::idiv, reg::rcx);
|
||
|
|
if done != never as u64 {
|
||
|
|
this.b.text.land(done);
|
||
|
|
}
|
||
|
|
this.put(result, rest ? reg::rdx : reg::rax);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Shifts of words, by 64 or more leaving nothing, or the sign.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The instruction.
|
||
|
|
#! @param kind The shift.
|
||
|
|
#! @param a The word shifted.
|
||
|
|
#! @param b How far.
|
||
|
|
#! @param result The result.
|
||
|
|
def selector::shift_by(&mut this, index: u32, kind: op, a: u32, b: u32, result: u32): void {
|
||
|
|
return if !this.wanted(result);
|
||
|
|
val how := kind == op::shl ? shift::shl : kind == op::shru ? shift::shr : shift::sar;
|
||
|
|
var bits: u64 = 0;
|
||
|
|
if this.p.remade[b] && this.constant_bits(b, &mut bits) {
|
||
|
|
val into := this.target(result, reg::rax);
|
||
|
|
if bits >= 64 {
|
||
|
|
if kind == op::shrs {
|
||
|
|
this.load_into(a, index, into);
|
||
|
|
this.b.text.shift_imm(shift::sar, into, 63);
|
||
|
|
} else {
|
||
|
|
this.b.text.mov_imm(into, 0);
|
||
|
|
}
|
||
|
|
this.put(result, into);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.load_into(a, index, into);
|
||
|
|
if bits != 0 {
|
||
|
|
this.b.text.shift_imm(how, into, bits as u8);
|
||
|
|
}
|
||
|
|
this.put(result, into);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
this.load_into(b, index, reg::rcx);
|
||
|
|
this.load_into(a, index, reg::rax);
|
||
|
|
this.b.text.alu_imm(alu::cmp, reg::rcx, 64);
|
||
|
|
val within := this.b.text.jcc(cc::below);
|
||
|
|
if kind == op::shrs {
|
||
|
|
this.b.text.shift_imm(shift::sar, reg::rax, 63);
|
||
|
|
} else {
|
||
|
|
this.b.text.mov_imm(reg::rax, 0);
|
||
|
|
}
|
||
|
|
val done := this.b.text.jmp();
|
||
|
|
this.b.text.land(within);
|
||
|
|
this.b.text.shift_cl(how, reg::rax);
|
||
|
|
this.b.text.land(done);
|
||
|
|
this.put(result, reg::rax);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The condition `cmp a, b` leaves for a comparison.
|
||
|
|
#! @param kind The comparison.
|
||
|
|
#! @returns The condition.
|
||
|
|
pub def condition_of(kind: op): cc {
|
||
|
|
return match kind {
|
||
|
|
op::eq => cc::equal,
|
||
|
|
op::ne => cc::not_equal,
|
||
|
|
op::lts => cc::less,
|
||
|
|
op::ltu => cc::below,
|
||
|
|
op::les => cc::less_equal,
|
||
|
|
op::leu => cc::below_equal,
|
||
|
|
op::flt => cc::above,
|
||
|
|
op::fle => cc::above_equal,
|
||
|
|
else => cc::equal
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The condition holding where another does with the operands swapped.
|
||
|
|
#! @param condition The condition.
|
||
|
|
#! @returns The condition.
|
||
|
|
pub def swapped(condition: cc): cc {
|
||
|
|
return match condition {
|
||
|
|
cc::less => cc::greater,
|
||
|
|
cc::greater => cc::less,
|
||
|
|
cc::less_equal => cc::greater_equal,
|
||
|
|
cc::greater_equal => cc::less_equal,
|
||
|
|
cc::below => cc::above,
|
||
|
|
cc::above => cc::below,
|
||
|
|
cc::below_equal => cc::above_equal,
|
||
|
|
cc::above_equal => cc::below_equal,
|
||
|
|
else => condition
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The condition holding where another does not.
|
||
|
|
#! @param condition The condition.
|
||
|
|
#! @returns The condition.
|
||
|
|
pub def opposite(condition: cc): cc {
|
||
|
|
return (condition as u8 ^ 1) as cc;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Compares two values an instruction reads, as a comparison asks, leaving the
|
||
|
|
#! flags for a condition: words by `cmp`, and reals by `ucomisd`.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The instruction reading the operands.
|
||
|
|
#! @param kind The comparison.
|
||
|
|
#! @param a The first operand.
|
||
|
|
#! @param b The second.
|
||
|
|
#! @returns The condition that holds where the comparison does.
|
||
|
|
def selector::compare(&mut this, index: u32, kind: op, a: u32, b: u32): cc {
|
||
|
|
if kind == op::flt || kind == op::fle {
|
||
|
|
# `a < b` is `b > a`, which holds only where neither is not a number.
|
||
|
|
val x := this.real_reg(b, index, 0);
|
||
|
|
val y := this.real_reg(a, index, 1);
|
||
|
|
this.b.text.ucomisd(x, y);
|
||
|
|
return condition_of(kind);
|
||
|
|
}
|
||
|
|
var first := a;
|
||
|
|
var second := b;
|
||
|
|
var condition := condition_of(kind);
|
||
|
|
var imm: i32 = 0;
|
||
|
|
if this.immediate(a, &mut imm) && !this.p.remade[b] {
|
||
|
|
first = b;
|
||
|
|
second = a;
|
||
|
|
condition = swapped(condition);
|
||
|
|
}
|
||
|
|
val left := this.word_reg(first, index, reg::rax);
|
||
|
|
val right := this.word_operand(second, index, reg::rcx);
|
||
|
|
if right.kind == 2 && right.imm == 0 {
|
||
|
|
return this.test_zero(left, condition);
|
||
|
|
}
|
||
|
|
this.apply(alu::cmp, left, &right);
|
||
|
|
return condition;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Compares a register with 0 by `test`, which leaves the flags as `cmp` would,
|
||
|
|
#! the carry clear: so a word is at most 0 unsigned where it is 0, and above it
|
||
|
|
#! where it is not.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param r The register.
|
||
|
|
#! @param condition The condition the comparison asks for.
|
||
|
|
#! @returns The condition that holds where it does.
|
||
|
|
def selector::test_zero(&mut this, r: reg, condition: cc): cc {
|
||
|
|
this.b.text.test(r, r);
|
||
|
|
return match condition {
|
||
|
|
cc::below_equal => cc::equal,
|
||
|
|
cc::above => cc::not_equal,
|
||
|
|
else => condition
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Comparisons of a signed and an unsigned word as the numbers they are.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The instruction.
|
||
|
|
#! @param kind The comparison.
|
||
|
|
#! @param a The first operand.
|
||
|
|
#! @param b The second.
|
||
|
|
#! @param result The result.
|
||
|
|
def selector::mixed(&mut this, index: u32, kind: op, a: u32, b: u32, result: u32): void {
|
||
|
|
return if !this.wanted(result);
|
||
|
|
this.load_into(b, index, reg::rcx);
|
||
|
|
this.load_into(a, index, reg::rax);
|
||
|
|
val signed_first := kind == op::eqsu || kind == op::ltsu || kind == op::lesu;
|
||
|
|
this.b.text.test(signed_first ? reg::rax : reg::rcx, signed_first ? reg::rax : reg::rcx);
|
||
|
|
val sign := kind == op::eqsu || kind == op::ltus || kind == op::leus ? cc::no_sign : cc::sign;
|
||
|
|
this.b.text.setcc(sign, reg::rdx);
|
||
|
|
this.b.text.alu(alu::cmp, reg::rax, reg::rcx);
|
||
|
|
val holds := match kind {
|
||
|
|
op::eqsu => cc::equal,
|
||
|
|
op::ltsu, op::ltus => cc::below,
|
||
|
|
else => cc::below_equal
|
||
|
|
};
|
||
|
|
this.b.text.setcc(holds, reg::rax);
|
||
|
|
val joins := kind == op::ltsu || kind == op::lesu ? alu::bor : alu::band;
|
||
|
|
this.b.text.alu(joins, reg::rax, reg::rdx);
|
||
|
|
this.put(result, reg::rax);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The code of an operator of one value.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The instruction.
|
||
|
|
#! @param held The instruction.
|
||
|
|
def selector::unary_op(&mut this, index: u32, held: &inst): void {
|
||
|
|
val a := this.body.arg(index, 0);
|
||
|
|
val result := held.result;
|
||
|
|
return if !this.wanted(result);
|
||
|
|
match held.kind {
|
||
|
|
op::neg, op::bnot => {
|
||
|
|
val into := this.target(result, reg::rax);
|
||
|
|
this.load_into(a, index, into);
|
||
|
|
this.b.text.unary(held.kind == op::neg ? unary::neg : unary::bnot, into);
|
||
|
|
this.put(result, into);
|
||
|
|
}
|
||
|
|
op::lnot => {
|
||
|
|
val tested := this.word_reg(a, index, reg::rax);
|
||
|
|
this.b.text.test(tested, tested);
|
||
|
|
val into := this.target(result, reg::rax);
|
||
|
|
this.b.text.setcc(cc::equal, into);
|
||
|
|
this.put(result, into);
|
||
|
|
}
|
||
|
|
op::wrap => {
|
||
|
|
val bits := held.aux as u64;
|
||
|
|
val signed := held.imm != 0;
|
||
|
|
val into := this.target(result, reg::rax);
|
||
|
|
if bits == 8 || bits == 16 || bits == 32 {
|
||
|
|
this.b.text.extend(bits / 8, signed, into, this.word_reg(a, index, reg::rcx));
|
||
|
|
} else {
|
||
|
|
this.load_into(a, index, into);
|
||
|
|
if bits < 64 {
|
||
|
|
this.b.text.shift_imm(shift::shl, into, (64 - bits) as u8);
|
||
|
|
this.b.text.shift_imm(signed ? shift::sar : shift::shr, into, (64 - bits) as u8);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
this.put(result, into);
|
||
|
|
}
|
||
|
|
op::fneg => {
|
||
|
|
val into := this.real_target(result);
|
||
|
|
this.b.text.mov_imm(reg::rax, 0x8000000000000000);
|
||
|
|
this.b.text.movq_to_xmm(1, reg::rax);
|
||
|
|
this.load_real_into(a, index, into);
|
||
|
|
this.b.text.xorpd(into, 1);
|
||
|
|
this.put_real(result, into);
|
||
|
|
}
|
||
|
|
op::itof => {
|
||
|
|
val into := this.real_target(result);
|
||
|
|
this.b.text.cvtsi2sd(into, this.word_reg(a, index, reg::rax));
|
||
|
|
this.put_real(result, into);
|
||
|
|
}
|
||
|
|
op::utof => {
|
||
|
|
# Past the largest signed word, half the value is converted and doubled,
|
||
|
|
# keeping its lowest bit so it rounds as the whole value would.
|
||
|
|
val into := this.real_target(result);
|
||
|
|
this.load_into(a, index, reg::rax);
|
||
|
|
this.b.text.test(reg::rax, reg::rax);
|
||
|
|
val large := this.b.text.jcc(cc::sign);
|
||
|
|
this.b.text.cvtsi2sd(into, reg::rax);
|
||
|
|
val done := this.b.text.jmp();
|
||
|
|
this.b.text.land(large);
|
||
|
|
this.b.text.mov(reg::rcx, reg::rax);
|
||
|
|
this.b.text.shift_imm(shift::shr, reg::rcx, 1);
|
||
|
|
this.b.text.alu_imm(alu::band, reg::rax, 1);
|
||
|
|
this.b.text.alu(alu::bor, reg::rcx, reg::rax);
|
||
|
|
this.b.text.cvtsi2sd(into, reg::rcx);
|
||
|
|
this.b.text.sse(sse::addsd, into, into);
|
||
|
|
this.b.text.land(done);
|
||
|
|
this.put_real(result, into);
|
||
|
|
}
|
||
|
|
op::ftoi => {
|
||
|
|
# Not a number is 0, and values past the range saturate.
|
||
|
|
this.load_real_into(a, index, 0);
|
||
|
|
this.b.text.mov_imm(reg::rax, 0);
|
||
|
|
this.b.text.ucomisd(0, 0);
|
||
|
|
val nan := this.b.text.jcc(cc::parity);
|
||
|
|
this.b.text.mov_imm(reg::rdx, 0x43E0000000000000);
|
||
|
|
this.b.text.movq_to_xmm(1, reg::rdx);
|
||
|
|
this.b.text.ucomisd(0, 1);
|
||
|
|
val high := this.b.text.jcc(cc::above_equal);
|
||
|
|
this.b.text.cvttsd2si(reg::rax, 0);
|
||
|
|
val done := this.b.text.jmp();
|
||
|
|
this.b.text.land(high);
|
||
|
|
this.b.text.mov_imm(reg::rax, 0x7FFFFFFFFFFFFFFF);
|
||
|
|
this.b.text.land(nan);
|
||
|
|
this.b.text.land(done);
|
||
|
|
this.put(result, reg::rax);
|
||
|
|
}
|
||
|
|
op::ftou => {
|
||
|
|
this.load_real_into(a, index, 0);
|
||
|
|
this.b.text.mov_imm(reg::rax, 0);
|
||
|
|
this.b.text.mov_imm(reg::rdx, 0);
|
||
|
|
this.b.text.movq_to_xmm(1, reg::rdx);
|
||
|
|
this.b.text.ucomisd(0, 1);
|
||
|
|
val low := this.b.text.jcc(cc::below_equal);
|
||
|
|
this.b.text.mov_imm(reg::rdx, 0x43F0000000000000);
|
||
|
|
this.b.text.movq_to_xmm(1, reg::rdx);
|
||
|
|
this.b.text.ucomisd(0, 1);
|
||
|
|
val full := this.b.text.jcc(cc::above_equal);
|
||
|
|
this.b.text.mov_imm(reg::rdx, 0x43E0000000000000);
|
||
|
|
this.b.text.movq_to_xmm(1, reg::rdx);
|
||
|
|
this.b.text.ucomisd(0, 1);
|
||
|
|
val upper := this.b.text.jcc(cc::above_equal);
|
||
|
|
this.b.text.cvttsd2si(reg::rax, 0);
|
||
|
|
val done := this.b.text.jmp();
|
||
|
|
this.b.text.land(upper);
|
||
|
|
this.b.text.sse(sse::subsd, 0, 1);
|
||
|
|
this.b.text.cvttsd2si(reg::rax, 0);
|
||
|
|
this.b.text.mov_imm(reg::rcx, 0x8000000000000000);
|
||
|
|
this.b.text.alu(alu::bxor, reg::rax, reg::rcx);
|
||
|
|
val finished := this.b.text.jmp();
|
||
|
|
this.b.text.land(full);
|
||
|
|
this.b.text.mov_imm(reg::rax, 0xFFFFFFFFFFFFFFFF);
|
||
|
|
this.b.text.land(low);
|
||
|
|
this.b.text.land(done);
|
||
|
|
this.b.text.land(finished);
|
||
|
|
this.put(result, reg::rax);
|
||
|
|
}
|
||
|
|
op::fsingle => this.round_single(index, a, result);
|
||
|
|
op::fsqrt => {
|
||
|
|
if this.works[index] {
|
||
|
|
this.single_root(index, a, result);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
val into := this.real_target(result);
|
||
|
|
this.b.text.sse(sse::sqrtsd, into, this.real_reg(a, index, 0));
|
||
|
|
this.put_real(result, into);
|
||
|
|
}
|
||
|
|
op::ones => {
|
||
|
|
if this.body.wide {
|
||
|
|
this.count_bits(index, held, a, result);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
# Counted in pairs of bits, then nibbles, then bytes, whose counts a multiply
|
||
|
|
# sums into the top byte, since SSE2 alone has no `popcnt`.
|
||
|
|
this.load_into(a, index, reg::rax);
|
||
|
|
this.b.text.mov(reg::rcx, reg::rax);
|
||
|
|
this.b.text.shift_imm(shift::shr, reg::rcx, 1);
|
||
|
|
this.b.text.mov_imm(reg::rdx, 0x5555555555555555);
|
||
|
|
this.b.text.alu(alu::band, reg::rcx, reg::rdx);
|
||
|
|
this.b.text.alu(alu::sub, reg::rax, reg::rcx);
|
||
|
|
this.b.text.mov_imm(reg::rdx, 0x3333333333333333);
|
||
|
|
this.b.text.mov(reg::rcx, reg::rax);
|
||
|
|
this.b.text.shift_imm(shift::shr, reg::rcx, 2);
|
||
|
|
this.b.text.alu(alu::band, reg::rax, reg::rdx);
|
||
|
|
this.b.text.alu(alu::band, reg::rcx, reg::rdx);
|
||
|
|
this.b.text.alu(alu::add, reg::rax, reg::rcx);
|
||
|
|
this.b.text.mov(reg::rcx, reg::rax);
|
||
|
|
this.b.text.shift_imm(shift::shr, reg::rcx, 4);
|
||
|
|
this.b.text.alu(alu::add, reg::rax, reg::rcx);
|
||
|
|
this.b.text.mov_imm(reg::rdx, 0x0F0F0F0F0F0F0F0F);
|
||
|
|
this.b.text.alu(alu::band, reg::rax, reg::rdx);
|
||
|
|
this.b.text.mov_imm(reg::rdx, 0x0101010101010101);
|
||
|
|
this.b.text.imul(reg::rax, reg::rdx);
|
||
|
|
this.b.text.shift_imm(shift::shr, reg::rax, 56);
|
||
|
|
this.put(result, reg::rax);
|
||
|
|
}
|
||
|
|
op::clz, op::ctz => {
|
||
|
|
if this.body.wide {
|
||
|
|
this.count_bits(index, held, a, result);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
# `bsr` and `bsf` say nothing of zero, which is 64 of either.
|
||
|
|
this.load_into(a, index, reg::rax);
|
||
|
|
this.b.text.test(reg::rax, reg::rax);
|
||
|
|
val zero := this.b.text.jcc(cc::equal);
|
||
|
|
if held.kind == op::clz {
|
||
|
|
this.b.text.bsr(reg::rax, reg::rax);
|
||
|
|
this.b.text.alu_imm(alu::bxor, reg::rax, 63);
|
||
|
|
} else {
|
||
|
|
this.b.text.bsf(reg::rax, reg::rax);
|
||
|
|
}
|
||
|
|
val done := this.b.text.jmp();
|
||
|
|
this.b.text.land(zero);
|
||
|
|
this.b.text.mov_imm(reg::rax, 64);
|
||
|
|
this.b.text.land(done);
|
||
|
|
this.put(result, reg::rax);
|
||
|
|
}
|
||
|
|
op::bswap => {
|
||
|
|
val into := this.target(result, reg::rax);
|
||
|
|
this.load_into(a, index, into);
|
||
|
|
this.b.text.bswap(into);
|
||
|
|
this.put(result, into);
|
||
|
|
}
|
||
|
|
else => {}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
# The instructions counting a word's bits, of x86-64-v3, as `F3 REX.W 0F` and
|
||
|
|
# these opcodes write them.
|
||
|
|
const count_popcnt: u8 = 0xB8;
|
||
|
|
const count_tzcnt: u8 = 0xBC;
|
||
|
|
const count_lzcnt: u8 = 0xBD;
|
||
|
|
|
||
|
|
#! `ones`, `clz`, or `ctz` in a version for x86-64-v3: `popcnt`, `lzcnt`, or
|
||
|
|
#! `tzcnt`, each one instruction, which counts 64 bits of zero as the operations
|
||
|
|
#! do.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The instruction.
|
||
|
|
#! @param held The instruction.
|
||
|
|
#! @param a The word.
|
||
|
|
#! @param result The count.
|
||
|
|
def selector::count_bits(&mut this, index: u32, held: &inst, a: u32, result: u32): void {
|
||
|
|
val into := this.target(result, reg::rax);
|
||
|
|
val from := this.word_reg(a, index, reg::rax);
|
||
|
|
val code := held.kind == op::ones ? count_popcnt : held.kind == op::clz ? count_lzcnt : count_tzcnt;
|
||
|
|
this.b.text.byte(0xF3);
|
||
|
|
this.b.text.rex(true, into as u32, reg::none, from, false);
|
||
|
|
this.b.text.byte(0x0F);
|
||
|
|
this.b.text.byte(code);
|
||
|
|
this.b.text.byte((0xC0 | ((into as u32 & 7) << 3) | (from as u32 & 7)) as u8);
|
||
|
|
this.put(result, into);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `select`: a word chosen by `cmov` after testing the condition, or after
|
||
|
|
#! comparing for it where a comparison is folded into it, the other put where
|
||
|
|
#! the result goes first, or a real by a short branch, as SSE2 moves none on a
|
||
|
|
#! condition.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The instruction.
|
||
|
|
#! @param held The instruction.
|
||
|
|
def selector::choose(&mut this, index: u32, held: &inst): void {
|
||
|
|
val result := held.result;
|
||
|
|
return if !this.wanted(result);
|
||
|
|
val cond := this.body.arg(index, 0);
|
||
|
|
val a := this.body.arg(index, 1);
|
||
|
|
val b := this.body.arg(index, 2);
|
||
|
|
val reader := this.p.reader_of(this.body, index);
|
||
|
|
if reader != index {
|
||
|
|
# The comparison sets the flags first, which moving and loading leave as
|
||
|
|
# they are, and what it compared is read no more.
|
||
|
|
val kind: op = this.body.insts[reader].kind;
|
||
|
|
val condition := this.compare(index, kind, this.body.arg(reader, 0), this.body.arg(reader, 1));
|
||
|
|
var into := this.target(result, reg::rax);
|
||
|
|
if this.is_in(a, index, into) {
|
||
|
|
into = reg::rax;
|
||
|
|
}
|
||
|
|
this.load_into(b, index, into);
|
||
|
|
this.b.text.cmov(condition, into, this.word_reg(a, index, reg::rdx));
|
||
|
|
this.put(result, into);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if this.p.real[result] {
|
||
|
|
this.load_real_into(b, index, 0);
|
||
|
|
val tested := this.word_reg(cond, index, reg::rax);
|
||
|
|
this.b.text.test(tested, tested);
|
||
|
|
val skip := this.b.text.jcc(cc::equal);
|
||
|
|
this.load_real_into(a, index, 0);
|
||
|
|
this.b.text.land(skip);
|
||
|
|
this.put_real(result, 0);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
# The result is computed where it goes, unless the condition or the word
|
||
|
|
# chosen where it holds is there.
|
||
|
|
var into := this.target(result, reg::rax);
|
||
|
|
if this.is_in(cond, index, into) || this.is_in(a, index, into) {
|
||
|
|
into = reg::rax;
|
||
|
|
}
|
||
|
|
this.load_into(b, index, into);
|
||
|
|
val chosen := this.word_reg(a, index, reg::rdx);
|
||
|
|
val tested := this.word_reg(cond, index, reg::rcx);
|
||
|
|
this.b.text.test(tested, tested);
|
||
|
|
this.b.text.cmov(cc::not_equal, into, chosen);
|
||
|
|
this.put(result, into);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
# Reals.
|
||
|
|
|
||
|
|
#! Arithmetic of two reals in place.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The instruction.
|
||
|
|
#! @param kind The arithmetic.
|
||
|
|
#! @param a The first operand.
|
||
|
|
#! @param b The second.
|
||
|
|
#! @param result The result.
|
||
|
|
#! @param commutes Whether the operands may be swapped.
|
||
|
|
def selector::real_arithmetic(&mut this, index: u32, kind: sse, a: u32, b: u32, result: u32, commutes: bool): void {
|
||
|
|
return if !this.wanted(result);
|
||
|
|
if this.works[index] {
|
||
|
|
this.single_arithmetic(index, kind, a, b, result, commutes);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
val into := this.real_target(result);
|
||
|
|
if this.is_in_xmm(a, index, into) {
|
||
|
|
this.b.text.sse(kind, into, this.real_reg(b, index, 1));
|
||
|
|
} else if commutes && this.is_in_xmm(b, index, into) {
|
||
|
|
this.b.text.sse(kind, into, this.real_reg(a, index, 1));
|
||
|
|
} else if this.is_in_xmm(b, index, into) {
|
||
|
|
this.load_real_into(a, index, 0);
|
||
|
|
this.b.text.sse(kind, 0, into);
|
||
|
|
this.b.text.movaps(into, 0);
|
||
|
|
} else if this.is_single_in(b, index, into) {
|
||
|
|
# The second held as an `f32` where the result goes, widened first, as
|
||
|
|
# loading the first there would lose it.
|
||
|
|
val second := this.real_reg(b, index, 1);
|
||
|
|
this.load_real_into(a, index, into);
|
||
|
|
this.b.text.sse(kind, into, second);
|
||
|
|
} else {
|
||
|
|
this.load_real_into(a, index, into);
|
||
|
|
this.b.text.sse(kind, into, this.real_reg(b, index, 1));
|
||
|
|
}
|
||
|
|
this.put_real(result, into);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! What is left dividing reals, whose sign is the dividend's, through the
|
||
|
|
#! x87's exact `fprem`, which reads its operands from the stack.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The instruction.
|
||
|
|
#! @param a The dividend.
|
||
|
|
#! @param b The divisor.
|
||
|
|
#! @param result The result.
|
||
|
|
def selector::real_remainder(&mut this, index: u32, a: u32, b: u32, result: u32): void {
|
||
|
|
return if !this.wanted(result);
|
||
|
|
val dividend := this.real_reg(a, index, 0);
|
||
|
|
val divisor := this.real_reg(b, index, 1);
|
||
|
|
this.b.text.alu_imm(alu::sub, reg::rsp, 16);
|
||
|
|
this.b.text.movsd_store(at(reg::rsp, 0), dividend);
|
||
|
|
this.b.text.movsd_store(at(reg::rsp, 8), divisor);
|
||
|
|
this.b.text.fmod_stack();
|
||
|
|
val into := this.real_target(result);
|
||
|
|
this.b.text.movsd_load(into, at(reg::rsp, 0));
|
||
|
|
this.b.text.alu_imm(alu::add, reg::rsp, 16);
|
||
|
|
this.put_real(result, into);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Comparisons of reals, giving a word: false where either is not a number,
|
||
|
|
#! but for `!=`.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The instruction.
|
||
|
|
#! @param kind The comparison.
|
||
|
|
#! @param a The first operand.
|
||
|
|
#! @param b The second.
|
||
|
|
#! @param result The result.
|
||
|
|
def selector::real_compare(&mut this, index: u32, kind: op, a: u32, b: u32, result: u32): void {
|
||
|
|
return if !this.wanted(result);
|
||
|
|
if kind == op::flt || kind == op::fle {
|
||
|
|
val condition := this.compare(index, kind, a, b);
|
||
|
|
this.b.text.setcc(condition, reg::rax);
|
||
|
|
this.put(result, reg::rax);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
val x := this.real_reg(a, index, 0);
|
||
|
|
val y := this.real_reg(b, index, 1);
|
||
|
|
this.b.text.ucomisd(x, y);
|
||
|
|
val equal := kind == op::feq;
|
||
|
|
this.b.text.setcc(equal ? cc::equal : cc::not_equal, reg::rax);
|
||
|
|
this.b.text.setcc(equal ? cc::no_parity : cc::parity, reg::rcx);
|
||
|
|
this.b.text.alu(equal ? alu::band : alu::bor, reg::rax, reg::rcx);
|
||
|
|
this.put(result, reg::rax);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
# Memory.
|
||
|
|
|
||
|
|
#! `load`: the value at an address.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The instruction.
|
||
|
|
#! @param held The instruction.
|
||
|
|
def selector::load(&mut this, index: u32, held: &inst): void {
|
||
|
|
val how := held.aux as access;
|
||
|
|
val result := held.result;
|
||
|
|
var direct := false;
|
||
|
|
val from := this.address(this.body.arg(index, 0), index, held.imm, reg::rax, reg::rcx, &mut direct);
|
||
|
|
if !direct {
|
||
|
|
this.null_check(from.base, held.imm + how.width(), index);
|
||
|
|
}
|
||
|
|
if how.value_type().lanes() {
|
||
|
|
this.load_lanes(index, held, from);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if this.p.real[result] {
|
||
|
|
val into := this.real_target(result);
|
||
|
|
if how == access::f32 {
|
||
|
|
this.b.text.movss_load(into, from);
|
||
|
|
if !this.single[result] {
|
||
|
|
this.b.text.cvtss2sd(into, into);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
this.b.text.movsd_load(into, from);
|
||
|
|
}
|
||
|
|
this.put_real(result, into);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
val into := this.target(result, reg::rax);
|
||
|
|
match how {
|
||
|
|
access::i8 => this.b.text.load(1, true, into, from);
|
||
|
|
access::u8 => this.b.text.load(1, false, into, from);
|
||
|
|
access::i16 => this.b.text.load(2, true, into, from);
|
||
|
|
access::u16 => this.b.text.load(2, false, into, from);
|
||
|
|
access::i32 => this.b.text.load(4, true, into, from);
|
||
|
|
access::u32 => this.b.text.load(4, false, into, from);
|
||
|
|
else => this.b.text.load(8, false, into, from);
|
||
|
|
}
|
||
|
|
this.put(result, into);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `store`: writes a value at an address.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The instruction.
|
||
|
|
#! @param held The instruction.
|
||
|
|
def selector::store(&mut this, index: u32, held: &inst): void {
|
||
|
|
val how := held.aux as access;
|
||
|
|
val value := this.body.arg(index, 1);
|
||
|
|
var direct := false;
|
||
|
|
# The index of a sum folded into the address is in neither register the
|
||
|
|
# value is loaded into: `rcx` for a word, and `rdx` for a real made again.
|
||
|
|
val real := (how == access::f32 || how == access::f64) && this.p.real[value];
|
||
|
|
val to := this.address(this.body.arg(index, 0), index, held.imm, reg::rax, real ? reg::rcx : reg::rdx, &mut direct);
|
||
|
|
if !direct {
|
||
|
|
this.null_check(to.base, held.imm + how.width(), index);
|
||
|
|
}
|
||
|
|
if how.value_type().lanes() {
|
||
|
|
this.store_lanes(index, held, to);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if how == access::f32 || how == access::f64 {
|
||
|
|
if this.p.real[value] {
|
||
|
|
if how == access::f32 && this.single[value] {
|
||
|
|
this.b.text.movss_store(to, this.single_reg(value, index, 0));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
val from := this.real_reg(value, index, 0);
|
||
|
|
if how == access::f32 {
|
||
|
|
this.b.text.cvtsd2ss(0, from);
|
||
|
|
this.b.text.movss_store(to, 0);
|
||
|
|
} else {
|
||
|
|
this.b.text.movsd_store(to, from);
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
val width := how.width();
|
||
|
|
var bits: u64 = 0;
|
||
|
|
if this.p.remade[value] && this.constant_bits(value, &mut bits) && (width < 8 || fits_i32(bits)) {
|
||
|
|
this.b.text.store_imm_width(width, to, bits);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.b.text.store(width, to, this.word_reg(value, index, reg::rcx));
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `copy`: copies bytes from one address to another, a word at a time where
|
||
|
|
#! they are few, and by `rep movsb` otherwise, which `rsi` and `rdi` are kept
|
||
|
|
#! around.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The instruction.
|
||
|
|
#! @param held The instruction.
|
||
|
|
def selector::copy(&mut this, index: u32, held: &inst): void {
|
||
|
|
val size := held.imm;
|
||
|
|
return if size == 0;
|
||
|
|
var to_direct := false;
|
||
|
|
var from_direct := false;
|
||
|
|
val to := this.address(this.body.arg(index, 0), index, 0, reg::rax, reg::rdx, &mut to_direct);
|
||
|
|
val from := this.address(this.body.arg(index, 1), index, 0, reg::rcx, reg::rdx, &mut from_direct);
|
||
|
|
if !to_direct {
|
||
|
|
this.null_check(to.base, size, index);
|
||
|
|
}
|
||
|
|
if !from_direct {
|
||
|
|
this.null_check(from.base, size, index);
|
||
|
|
}
|
||
|
|
if size <= 128 {
|
||
|
|
this.b.text.copy_small(to, from, size, reg::rdx, true);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.b.text.lea(reg::rax, to);
|
||
|
|
this.b.text.lea(reg::rdx, from);
|
||
|
|
this.b.text.push(reg::rsi);
|
||
|
|
this.b.text.push(reg::rdi);
|
||
|
|
this.b.text.mov(reg::rdi, reg::rax);
|
||
|
|
this.b.text.mov(reg::rsi, reg::rdx);
|
||
|
|
this.b.text.mov_imm(reg::rcx, size);
|
||
|
|
this.b.text.rep_movsb();
|
||
|
|
this.b.text.pop(reg::rdi);
|
||
|
|
this.b.text.pop(reg::rsi);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `clear`: zeroes bytes from an address.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The instruction.
|
||
|
|
#! @param held The instruction.
|
||
|
|
def selector::clear(&mut this, index: u32, held: &inst): void {
|
||
|
|
val size := held.imm;
|
||
|
|
return if size == 0;
|
||
|
|
var direct := false;
|
||
|
|
val to := this.address(this.body.arg(index, 0), index, 0, reg::rax, reg::rcx, &mut direct);
|
||
|
|
if !direct {
|
||
|
|
this.null_check(to.base, size, index);
|
||
|
|
}
|
||
|
|
if size <= 128 {
|
||
|
|
this.b.text.zero_small(to, size);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
this.b.text.lea(reg::rdx, to);
|
||
|
|
this.b.text.push(reg::rdi);
|
||
|
|
this.b.text.mov(reg::rdi, reg::rdx);
|
||
|
|
this.b.text.mov_imm(reg::rax, 0);
|
||
|
|
this.b.text.mov_imm(reg::rcx, size);
|
||
|
|
this.b.text.rep_stosb();
|
||
|
|
this.b.text.pop(reg::rdi);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Memory a number of bytes further on.
|
||
|
|
#! @param m The memory.
|
||
|
|
#! @param bytes How many bytes.
|
||
|
|
#! @returns The memory.
|
||
|
|
pub def offset_by(m: mem, bytes: u64): mem {
|
||
|
|
return mem { base = m.base, index = m.index, scale = m.scale, disp = (m.disp as i64 + bytes as i64) as i32 };
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Copies a few bytes, at most 128, through a register: sixteen at a time
|
||
|
|
#! through `xmm0` where it is free, then eight at a time, then as many as are
|
||
|
|
#! left of four, two, and one. Copying 16 bytes at once is what lets a vector
|
||
|
|
#! read back whole what was written whole, which the processor forwards.
|
||
|
|
#! @param this The code.
|
||
|
|
#! @param to Where they are copied to.
|
||
|
|
#! @param from Where they are copied from.
|
||
|
|
#! @param size How many there are.
|
||
|
|
#! @param temp The register they go through.
|
||
|
|
#! @param wide Whether `xmm0` is free to copy 16 bytes at a time through.
|
||
|
|
pub def assembly::copy_small(&mut this, to: mem, from: mem, size: u64, temp: reg, wide: bool): void {
|
||
|
|
var at_byte: u64 = 0;
|
||
|
|
while wide && size - at_byte >= 16 {
|
||
|
|
this.packed_mem(vec_movdqu_load, 0, offset_by(from, at_byte));
|
||
|
|
this.packed_mem(vec_movdqu_store, 0, offset_by(to, at_byte));
|
||
|
|
at_byte += 16;
|
||
|
|
}
|
||
|
|
while at_byte < size {
|
||
|
|
val width := piece_width(size - at_byte);
|
||
|
|
this.load(width, false, temp, offset_by(from, at_byte));
|
||
|
|
this.store(width, offset_by(to, at_byte), temp);
|
||
|
|
at_byte += width;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Zeroes a few bytes, at most 128, as `copy_small` copies them, sixteen at a
|
||
|
|
#! time through `xmm0`, which it is free to use.
|
||
|
|
#! @param this The code.
|
||
|
|
#! @param to Where they are.
|
||
|
|
#! @param size How many there are.
|
||
|
|
pub def assembly::zero_small(&mut this, to: mem, size: u64): void {
|
||
|
|
var at_byte: u64 = 0;
|
||
|
|
if size >= 16 {
|
||
|
|
this.packed(vec_pxor, 0, 0);
|
||
|
|
}
|
||
|
|
while size - at_byte >= 16 {
|
||
|
|
this.packed_mem(vec_movdqu_store, 0, offset_by(to, at_byte));
|
||
|
|
at_byte += 16;
|
||
|
|
}
|
||
|
|
while at_byte < size {
|
||
|
|
val width := piece_width(size - at_byte);
|
||
|
|
this.store_imm_width(width, offset_by(to, at_byte), 0);
|
||
|
|
at_byte += width;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How many of the bytes left a piece of a small copy moves.
|
||
|
|
#! @param left How many are left.
|
||
|
|
#! @returns 8, 4, 2, or 1.
|
||
|
|
def piece_width(left: u64): u64 {
|
||
|
|
return left >= 8 ? 8 : left >= 4 ? 4 : left >= 2 ? 2 : 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `retrait`: the vtable a reference to another trait takes, from the map.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The instruction.
|
||
|
|
#! @param held The instruction.
|
||
|
|
def selector::retrait(&mut this, index: u32, held: &inst): void {
|
||
|
|
this.load_into(this.body.arg(index, 0), index, reg::rax);
|
||
|
|
if this.checking() {
|
||
|
|
this.b.text.mov_imm(reg::rcx, this.b.retrait_count(held.aux));
|
||
|
|
this.b.text.alu(alu::cmp, reg::rax, reg::rcx);
|
||
|
|
this.trap_if(cc::above_equal, fault::null_reference as u32, index);
|
||
|
|
}
|
||
|
|
val into := this.target(held.result, reg::rax);
|
||
|
|
this.b.text.load(8, false, into, indexed(reg::none, reg::rax, 8, this.b.retrait_address(held.aux) as i32));
|
||
|
|
this.put(held.result, into);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! `check`: stops the program with an error where a condition is false, testing
|
||
|
|
#! a comparison folded into it for itself.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param index The instruction.
|
||
|
|
#! @param held The instruction.
|
||
|
|
def selector::check(&mut this, index: u32, held: &inst): void {
|
||
|
|
val tested := this.body.arg(index, 0);
|
||
|
|
val reader := this.p.reader_of(this.body, index);
|
||
|
|
if reader != index {
|
||
|
|
# The kind is read through the code, as the C compiler passes a narrow field
|
||
|
|
# of a struct copied to the frame as the whole word holding it.
|
||
|
|
val kind: op = this.body.insts[reader].kind;
|
||
|
|
val condition := this.compare(index, kind, this.body.arg(reader, 0), this.body.arg(reader, 1));
|
||
|
|
this.trap_if(opposite(condition), held.aux, index);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
var bits: u64 = 0;
|
||
|
|
if this.p.remade[tested] && this.constant_bits(tested, &mut bits) {
|
||
|
|
if bits == 0 {
|
||
|
|
this.trap_always(held.aux, index);
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
val r := this.word_reg(tested, index, reg::rax);
|
||
|
|
this.b.text.test(r, r);
|
||
|
|
this.trap_if(cc::equal, held.aux, index);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Stops the program with an error at once, jumping to the stub that reports
|
||
|
|
#! it.
|
||
|
|
#! @param this The selector.
|
||
|
|
#! @param code The error.
|
||
|
|
#! @param index The instruction, whose place the report names.
|
||
|
|
def selector::trap_always(&mut this, code: u32, index: u32): void {
|
||
|
|
val held: inst = this.body.insts[index];
|
||
|
|
this.traps.append(trap_site { patch = this.b.text.jmp(), code = code, at = held.at, site = held.site, index = index });
|
||
|
|
}
|