834 lines
23 KiB
MoonBit
834 lines
23 KiB
MoonBit
///|
|
|
priv struct FuncType {
|
|
nparams : Int32
|
|
nresults : Int32
|
|
}
|
|
|
|
///|
|
|
priv struct Frame {
|
|
kind : Int32 // 0 = block, 1 = loop, 2 = if
|
|
base : Int32
|
|
arity : Int32
|
|
skip : Bool // opened while unreachable: emits nothing
|
|
loop_start : Int32
|
|
ends : Array[Int32]
|
|
mut else_patch : Int32
|
|
}
|
|
|
|
///|
|
|
priv struct Fn {
|
|
code : Asm
|
|
ft : FuncType
|
|
nlocals : Int32 // total locals including params
|
|
mem_bytes : Int32
|
|
nglobals : Int32
|
|
frames : Array[Frame]
|
|
calls : Array[(Int32, Int32)] // (rel32 position, callee function index)
|
|
traps : Array[(Int32, Int32)] // (rel32 position, trap kind)
|
|
mut height : Int32
|
|
mut dead : Bool
|
|
}
|
|
|
|
///|
|
|
const TRAP_UNREACHABLE : Int32 = 0
|
|
|
|
///|
|
|
const TRAP_DIV_ZERO : Int32 = 1
|
|
|
|
///|
|
|
const TRAP_OVERFLOW : Int32 = 2
|
|
|
|
///|
|
|
const TRAP_OOB : Int32 = 3
|
|
|
|
///|
|
|
const TRAP_INVALID : Int32 = 4
|
|
|
|
///|
|
|
fn Fn::push_rax(self : Fn) -> Unit {
|
|
self.code.b(0x50)
|
|
self.height += 1
|
|
}
|
|
|
|
///|
|
|
fn Fn::pop(self : Fn, reg : Int32) -> Unit {
|
|
// reg: 0 = rax, 1 = rcx, 2 = rdx
|
|
self.code.b(0x58 + reg)
|
|
self.height -= 1
|
|
}
|
|
|
|
///|
|
|
fn Fn::add_rsp(self : Fn, slots : Int32) -> Unit {
|
|
if slots > 0 {
|
|
self.code.bs([0x48, 0x81, 0xC4])
|
|
self.code.i32(slots * 8)
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn Fn::local_disp(self : Fn, idx : Int32) -> Int32 raise CompileError {
|
|
if idx < 0 || idx >= self.nlocals {
|
|
raise CompileError("local index out of range")
|
|
}
|
|
let np = self.ft.nparams
|
|
if idx < np {
|
|
16 + (np - 1 - idx) * 8
|
|
} else {
|
|
-8 * (idx - np + 1)
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn Fn::trap_jmp(self : Fn, kind : Int32, cond : Array[Int32]) -> Unit {
|
|
let at = self.code.rel(cond)
|
|
self.traps.push((at, kind))
|
|
}
|
|
|
|
///|
|
|
fn Fn::emit_br(self : Fn, depth : Int32, h : Int32) -> Unit raise CompileError {
|
|
if depth >= self.frames.length() {
|
|
raise CompileError("branch depth out of range")
|
|
}
|
|
let f = self.frames[self.frames.length() - 1 - depth]
|
|
let arity = if f.kind == 1 { 0 } else { f.arity }
|
|
if arity == 1 {
|
|
self.code.b(0x58) // pop rax
|
|
self.add_rsp(h - 1 - f.base)
|
|
self.code.b(0x50) // push rax
|
|
} else {
|
|
self.add_rsp(h - f.base)
|
|
}
|
|
if f.kind == 1 {
|
|
let at = self.code.rel([0xE9])
|
|
self.code.patch(at, f.loop_start)
|
|
} else {
|
|
f.ends.push(self.code.rel([0xE9]))
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn Fn::block_type(r : Reader) -> Int32 raise CompileError {
|
|
match r.byte() {
|
|
0x40 => 0
|
|
0x7C..<=0x7F => 1
|
|
b => raise CompileError("unsupported block type 0x\{b.to_string(radix=16)}")
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn Fn::open(self : Fn, kind : Int32, arity : Int32, skip : Bool) -> Unit {
|
|
self.frames.push({
|
|
kind,
|
|
base: self.height,
|
|
arity,
|
|
skip,
|
|
loop_start: self.code.here(),
|
|
ends: [],
|
|
else_patch: -1,
|
|
})
|
|
}
|
|
|
|
///|
|
|
fn Fn::binop(self : Fn, op : Array[Int32]) -> Unit {
|
|
self.pop(1)
|
|
self.pop(0)
|
|
self.code.bs(op)
|
|
self.push_rax()
|
|
}
|
|
|
|
///|
|
|
fn Fn::unop(self : Fn, op : Array[Int32]) -> Unit {
|
|
self.pop(0)
|
|
self.code.bs(op)
|
|
self.push_rax()
|
|
}
|
|
|
|
///|
|
|
fn Fn::cmp(self : Fn, setcc : Int32, wide : Bool) -> Unit {
|
|
self.pop(1)
|
|
self.pop(0)
|
|
if wide {
|
|
self.code.b(0x48)
|
|
}
|
|
self.code.bs([0x39, 0xC8, 0x0F, setcc, 0xC0, 0x0F, 0xB6, 0xC0])
|
|
self.push_rax()
|
|
}
|
|
|
|
///|
|
|
fn Fn::divrem(self : Fn, op : Int32, wide : Bool) -> Unit {
|
|
// op: 0x6D div_s, 0x6E div_u, 0x6F rem_s, 0x70 rem_u (i64 ops are mapped down by 0x12)
|
|
self.pop(1)
|
|
self.pop(0)
|
|
let w = if wide { [0x48] } else { [] }
|
|
self.code.bs(w)
|
|
self.trap_jmp(TRAP_DIV_ZERO, [0x85, 0xC9, 0x0F, 0x84]) // test rcx,rcx ; jz
|
|
match op {
|
|
0x6D => {
|
|
if wide {
|
|
self.code.bs([0x48, 0x83, 0xF9, 0xFF, 0x75, 0x13]) // cmp rcx,-1 ; jne L
|
|
self.code.bs([0x48, 0xBA]) // mov rdx, INT64_MIN
|
|
self.code.i64(-9223372036854775808L)
|
|
self.code.bs([0x48, 0x39, 0xD0]) // cmp rax,rdx
|
|
} else {
|
|
self.code.bs([0x83, 0xF9, 0xFF, 0x75, 0x0B]) // cmp ecx,-1 ; jne L
|
|
self.code.bs([0x3D, 0x00, 0x00, 0x00, 0x80]) // cmp eax,INT_MIN
|
|
}
|
|
let at = self.code.rel([0x0F, 0x84])
|
|
self.traps.push((at, TRAP_OVERFLOW))
|
|
self.code.bs(w)
|
|
self.code.bs([0x99]) // L: cdq / cqo
|
|
self.code.bs(w)
|
|
self.code.bs([0xF7, 0xF9]) // idiv
|
|
}
|
|
0x6E => {
|
|
self.code.bs([0x31, 0xD2]) // xor edx,edx
|
|
self.code.bs(w)
|
|
self.code.bs([0xF7, 0xF1]) // div
|
|
}
|
|
0x6F => {
|
|
self.code.bs(w)
|
|
self.code.bs([0x83, 0xF9, 0xFF, 0x75, 0x04]) // cmp rcx,-1 ; jne L
|
|
self.code.bs([0x31, 0xC0, 0xEB, if wide { 0x08 } else { 0x05 }]) // xor eax,eax ; jmp done
|
|
self.code.bs(w)
|
|
self.code.bs([0x99]) // L: cdq / cqo
|
|
self.code.bs(w)
|
|
self.code.bs([0xF7, 0xF9])
|
|
self.code.bs(w)
|
|
self.code.bs([0x89, 0xD0]) // mov rax,rdx
|
|
}
|
|
_ => {
|
|
self.code.bs([0x31, 0xD2])
|
|
self.code.bs(w)
|
|
self.code.bs([0xF7, 0xF1])
|
|
self.code.bs(w)
|
|
self.code.bs([0x89, 0xD0])
|
|
}
|
|
}
|
|
self.push_rax()
|
|
}
|
|
|
|
///|
|
|
/// Linear-memory load/store: bounds check, then access at MEM_BASE + addr + offset.
|
|
fn Fn::mem_access(self : Fn, r : Reader, op : Int32) -> Unit raise CompileError {
|
|
ignore(r.u32()) // alignment hint
|
|
let offset = r.u32()
|
|
if self.mem_bytes == 0 {
|
|
raise CompileError("memory access but the module defines no memory")
|
|
}
|
|
if offset < 0 {
|
|
raise CompileError("memory offset too large")
|
|
}
|
|
let size = match op {
|
|
0x29 | 0x2B | 0x37 | 0x39 => 8
|
|
0x28 | 0x2A | 0x34 | 0x35 | 0x36 | 0x38 | 0x3E => 4
|
|
0x2C | 0x2D | 0x30 | 0x31 | 0x3A | 0x3C => 1
|
|
_ => 2
|
|
}
|
|
let is_store = op >= 0x36
|
|
if is_store {
|
|
self.pop(1)
|
|
}
|
|
self.pop(0)
|
|
self.code.bs([0x89, 0xC0]) // mov eax,eax (zero-extend address)
|
|
self.code.bs([0x48, 0x05]) // add rax, offset
|
|
self.code.i32(offset)
|
|
self.code.bs([0x48, 0x3D]) // cmp rax, mem_bytes - size
|
|
self.code.i32(self.mem_bytes - size)
|
|
self.trap_jmp(TRAP_OOB, [0x0F, 0x87]) // ja trap
|
|
match op {
|
|
0x28 | 0x2A | 0x35 => self.code.bs([0x8B, 0x80])
|
|
0x29 | 0x2B => self.code.bs([0x48, 0x8B, 0x80])
|
|
0x2C => self.code.bs([0x0F, 0xBE, 0x80])
|
|
0x2D | 0x31 => self.code.bs([0x0F, 0xB6, 0x80])
|
|
0x2E => self.code.bs([0x0F, 0xBF, 0x80])
|
|
0x2F | 0x33 => self.code.bs([0x0F, 0xB7, 0x80])
|
|
0x30 => self.code.bs([0x48, 0x0F, 0xBE, 0x80])
|
|
0x32 => self.code.bs([0x48, 0x0F, 0xBF, 0x80])
|
|
0x34 => self.code.bs([0x48, 0x63, 0x80])
|
|
0x36 | 0x38 | 0x3E => self.code.bs([0x89, 0x88])
|
|
0x37 | 0x39 => self.code.bs([0x48, 0x89, 0x88])
|
|
0x3A | 0x3C => self.code.bs([0x88, 0x88])
|
|
_ => self.code.bs([0x66, 0x89, 0x88]) // 0x3B, 0x3D
|
|
}
|
|
self.code.i32(MEM_BASE)
|
|
if !is_store {
|
|
self.push_rax()
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn Fn::global_addr(self : Fn, idx : Int32) -> Int32 raise CompileError {
|
|
if idx < 0 || idx >= self.nglobals {
|
|
raise CompileError("global index out of range")
|
|
}
|
|
GLOB_BASE + 8 * idx
|
|
}
|
|
|
|
///|
|
|
/// Compile one function body. Returns the generated Fn (machine code + patch lists).
|
|
fn compile_body(
|
|
r : Reader,
|
|
body_end : Int32,
|
|
ft : FuncType,
|
|
nlocals : Int32,
|
|
all_types : Array[FuncType],
|
|
func_types : Array[Int32],
|
|
mem_bytes : Int32,
|
|
nglobals : Int32,
|
|
) -> Fn raise CompileError {
|
|
let f : Fn = {
|
|
code: Asm::new(),
|
|
ft,
|
|
nlocals,
|
|
mem_bytes,
|
|
nglobals,
|
|
frames: [],
|
|
calls: [],
|
|
traps: [],
|
|
height: 0,
|
|
dead: false,
|
|
}
|
|
// prologue: push rbp ; mov rbp,rsp ; zero-init non-param locals
|
|
f.code.bs([0x55, 0x48, 0x89, 0xE5])
|
|
for _ in 0..<(nlocals - ft.nparams) {
|
|
f.code.bs([0x6A, 0x00])
|
|
}
|
|
f.open(0, ft.nresults, false)
|
|
let mut finished = false
|
|
while !finished {
|
|
if r.pos >= body_end {
|
|
raise CompileError("function body missing end")
|
|
}
|
|
let op = r.byte()
|
|
let skipping = f.frames[f.frames.length() - 1].skip
|
|
let dead = f.dead
|
|
if skipping || dead {
|
|
// Unreachable code: parse immediates, only track control structure.
|
|
match op {
|
|
0x02 | 0x03 | 0x04 => {
|
|
let a = Fn::block_type(r)
|
|
f.open(
|
|
if op == 0x03 {
|
|
1
|
|
} else if op == 0x04 {
|
|
2
|
|
} else {
|
|
0
|
|
},
|
|
a,
|
|
true,
|
|
)
|
|
}
|
|
0x05 =>
|
|
if !skipping {
|
|
let fr = f.frames[f.frames.length() - 1]
|
|
if fr.else_patch >= 0 {
|
|
f.code.patch_here(fr.else_patch)
|
|
fr.else_patch = -1
|
|
}
|
|
f.height = fr.base
|
|
f.dead = false
|
|
}
|
|
0x0B => {
|
|
let fr = f.frames.pop().unwrap()
|
|
if !fr.skip {
|
|
if fr.else_patch >= 0 {
|
|
f.code.patch_here(fr.else_patch)
|
|
}
|
|
for e in fr.ends {
|
|
f.code.patch_here(e)
|
|
}
|
|
f.height = fr.base + fr.arity
|
|
f.dead = false
|
|
if f.frames.length() == 0 {
|
|
if ft.nresults == 1 {
|
|
f.code.b(0x58)
|
|
}
|
|
f.code.bs([0xC9, 0xC3])
|
|
finished = true
|
|
}
|
|
}
|
|
}
|
|
0x0C | 0x0D | 0x10 | 0x20 | 0x21 | 0x22 | 0x23 | 0x24 => ignore(r.u32())
|
|
0x41 => ignore(r.s32())
|
|
0x42 => ignore(r.s64())
|
|
0x43 => ignore(r.raw32())
|
|
0x44 => ignore(r.raw64())
|
|
0x3F | 0x40 => ignore(r.byte())
|
|
n if n >= 0x28 && n <= 0x3E => {
|
|
ignore(r.u32())
|
|
ignore(r.u32())
|
|
}
|
|
_ => ()
|
|
}
|
|
continue
|
|
}
|
|
match op {
|
|
0x00 => {
|
|
f.trap_jmp(TRAP_UNREACHABLE, [0xE9])
|
|
f.dead = true
|
|
}
|
|
0x01 => ()
|
|
0x02 | 0x03 | 0x04 => {
|
|
let a = Fn::block_type(r)
|
|
if op == 0x04 {
|
|
f.pop(0)
|
|
let at = f.code.rel([0x85, 0xC0, 0x0F, 0x84]) // test eax,eax ; jz else
|
|
f.open(2, a, false)
|
|
f.frames[f.frames.length() - 1].else_patch = at
|
|
} else {
|
|
f.open(if op == 0x03 { 1 } else { 0 }, a, false)
|
|
}
|
|
}
|
|
0x05 => {
|
|
let fr = f.frames[f.frames.length() - 1]
|
|
fr.ends.push(f.code.rel([0xE9]))
|
|
if fr.else_patch >= 0 {
|
|
f.code.patch_here(fr.else_patch)
|
|
fr.else_patch = -1
|
|
}
|
|
f.height = fr.base
|
|
}
|
|
0x0B => {
|
|
let fr = f.frames.pop().unwrap()
|
|
if fr.else_patch >= 0 {
|
|
f.code.patch_here(fr.else_patch)
|
|
}
|
|
for e in fr.ends {
|
|
f.code.patch_here(e)
|
|
}
|
|
f.height = fr.base + fr.arity
|
|
if f.frames.length() == 0 {
|
|
if ft.nresults == 1 {
|
|
f.code.b(0x58)
|
|
}
|
|
f.code.bs([0xC9, 0xC3])
|
|
finished = true
|
|
}
|
|
}
|
|
0x0C => {
|
|
f.emit_br(r.u32(), f.height)
|
|
f.dead = true
|
|
}
|
|
0x0D => {
|
|
let depth = r.u32()
|
|
f.pop(0)
|
|
let at = f.code.rel([0x85, 0xC0, 0x0F, 0x84]) // test eax,eax ; jz skip
|
|
f.emit_br(depth, f.height)
|
|
f.code.patch_here(at)
|
|
}
|
|
0x0F => {
|
|
f.emit_br(f.frames.length() - 1, f.height)
|
|
f.dead = true
|
|
}
|
|
0x10 => {
|
|
let callee = r.u32()
|
|
if callee >= func_types.length() {
|
|
raise CompileError("call to unknown function \{callee}")
|
|
}
|
|
let cft = all_types[func_types[callee]]
|
|
let at = f.code.rel([0xE8])
|
|
f.calls.push((at, callee))
|
|
f.add_rsp(cft.nparams)
|
|
f.height -= cft.nparams
|
|
if cft.nresults == 1 {
|
|
f.push_rax()
|
|
}
|
|
}
|
|
0x1A => {
|
|
f.code.bs([0x48, 0x83, 0xC4, 0x08])
|
|
f.height -= 1
|
|
}
|
|
0x1B => {
|
|
f.pop(2)
|
|
f.pop(1)
|
|
f.pop(0)
|
|
f.code.bs([0x85, 0xD2, 0x48, 0x0F, 0x44, 0xC1]) // test edx,edx ; cmovz rax,rcx
|
|
f.push_rax()
|
|
}
|
|
0x20 => {
|
|
let d = f.local_disp(r.u32())
|
|
f.code.bs([0x48, 0x8B, 0x85])
|
|
f.code.i32(d)
|
|
f.push_rax()
|
|
}
|
|
0x21 | 0x22 => {
|
|
let d = f.local_disp(r.u32())
|
|
f.pop(0)
|
|
if op == 0x22 {
|
|
f.push_rax()
|
|
}
|
|
f.code.bs([0x48, 0x89, 0x85])
|
|
f.code.i32(d)
|
|
}
|
|
0x23 => {
|
|
let a = f.global_addr(r.u32())
|
|
f.code.bs([0x48, 0x8B, 0x04, 0x25]) // mov rax,[abs]
|
|
f.code.i32(a)
|
|
f.push_rax()
|
|
}
|
|
0x24 => {
|
|
let a = f.global_addr(r.u32())
|
|
f.pop(0)
|
|
f.code.bs([0x48, 0x89, 0x04, 0x25]) // mov [abs],rax
|
|
f.code.i32(a)
|
|
}
|
|
n if n >= 0x28 && n <= 0x3E => f.mem_access(r, op)
|
|
0x3F => {
|
|
ignore(r.byte())
|
|
f.code.b(0x68)
|
|
f.code.i32(f.mem_bytes / 65536)
|
|
f.height += 1
|
|
}
|
|
0x40 => {
|
|
// memory is fixed-size: memory.grow always fails (-1)
|
|
ignore(r.byte())
|
|
f.pop(0)
|
|
f.code.bs([0x6A, 0xFF])
|
|
f.height += 1
|
|
}
|
|
0x41 => {
|
|
f.code.b(0x68)
|
|
f.code.i32(r.s32())
|
|
f.height += 1
|
|
}
|
|
0x45 => {
|
|
f.pop(0)
|
|
f.code.bs([0x85, 0xC0, 0x0F, 0x94, 0xC0, 0x0F, 0xB6, 0xC0])
|
|
f.push_rax()
|
|
}
|
|
0x42 => f.push_i64(r.s64())
|
|
0x43 => f.push_i64(r.raw32().to_int64())
|
|
0x44 => f.push_i64(r.raw64())
|
|
0x46..<=0x4F => f.cmp(setcc_of(op - 0x46), false)
|
|
0x50 => f.unop([0x48, 0x85, 0xC0, 0x0F, 0x94, 0xC0, 0x0F, 0xB6, 0xC0])
|
|
0x51..<=0x5A => f.cmp(setcc_of(op - 0x51), true)
|
|
0x6A => f.binop([0x01, 0xC8])
|
|
0x6B => f.binop([0x29, 0xC8])
|
|
0x6C => f.binop([0x0F, 0xAF, 0xC1])
|
|
0x6D | 0x6E | 0x6F | 0x70 => f.divrem(op, false)
|
|
0x71 => f.binop([0x21, 0xC8])
|
|
0x72 => f.binop([0x09, 0xC8])
|
|
0x73 => f.binop([0x31, 0xC8])
|
|
0x74 => f.binop([0xD3, 0xE0])
|
|
0x75 => f.binop([0xD3, 0xF8])
|
|
0x76 => f.binop([0xD3, 0xE8])
|
|
0x77 => f.binop([0xD3, 0xC0])
|
|
0x78 => f.binop([0xD3, 0xC8])
|
|
0x7C => f.binop([0x48, 0x01, 0xC8])
|
|
0x7D => f.binop([0x48, 0x29, 0xC8])
|
|
0x7E => f.binop([0x48, 0x0F, 0xAF, 0xC1])
|
|
0x7F..<=0x82 => f.divrem(op - 0x12, true)
|
|
0x83 => f.binop([0x48, 0x21, 0xC8])
|
|
0x84 => f.binop([0x48, 0x09, 0xC8])
|
|
0x85 => f.binop([0x48, 0x31, 0xC8])
|
|
0x86 => f.binop([0x48, 0xD3, 0xE0])
|
|
0x87 => f.binop([0x48, 0xD3, 0xF8])
|
|
0x88 => f.binop([0x48, 0xD3, 0xE8])
|
|
0x89 => f.binop([0x48, 0xD3, 0xC0])
|
|
0x8A => f.binop([0x48, 0xD3, 0xC8])
|
|
0xA7 | 0xAD => f.unop([0x89, 0xC0]) // i32.wrap_i64 / i64.extend_i32_u
|
|
0xAC => f.unop([0x48, 0x63, 0xC0]) // i64.extend_i32_s
|
|
0xC0 => f.unop([0x0F, 0xBE, 0xC0]) // i32.extend8_s
|
|
0xC1 => f.unop([0x0F, 0xBF, 0xC0]) // i32.extend16_s
|
|
0xC2 => f.unop([0x48, 0x0F, 0xBE, 0xC0]) // i64.extend8_s
|
|
0xC3 => f.unop([0x48, 0x0F, 0xBF, 0xC0]) // i64.extend16_s
|
|
0xC4 => f.unop([0x48, 0x63, 0xC0]) // i64.extend32_s
|
|
0x5B..<=0x60 => f.fp_cmp(op - 0x5B, false)
|
|
0x61..<=0x66 => f.fp_cmp(op - 0x61, true)
|
|
0x8B..<=0x91 => f.fp_unop(op - 0x8B, false)
|
|
0x99..<=0x9F => f.fp_unop(op - 0x99, true)
|
|
0x92..<=0x98 => f.fp_binop(op - 0x92, false)
|
|
0xA0..<=0xA6 => f.fp_binop(op - 0xA0, true)
|
|
0xA8 => f.fp_trunc(false, false, true) // i32.trunc_f32_s
|
|
0xA9 => f.fp_trunc(false, false, false)
|
|
0xAA => f.fp_trunc(true, false, true)
|
|
0xAB => f.fp_trunc(true, false, false)
|
|
0xAE => f.fp_trunc(false, true, true)
|
|
0xAF => f.fp_trunc(false, true, false)
|
|
0xB0 => f.fp_trunc(true, true, true)
|
|
0xB1 => f.fp_trunc(true, true, false)
|
|
0xB2 => f.fp_convert(false, false, true) // f32.convert_i32_s
|
|
0xB3 => f.fp_convert(false, false, false)
|
|
0xB4 => f.fp_convert(false, true, true)
|
|
0xB5 => f.fp_convert(false, true, false)
|
|
0xB6 => f.fp_resize(true, false) // f32.demote_f64
|
|
0xB7 => f.fp_convert(true, false, true)
|
|
0xB8 => f.fp_convert(true, false, false)
|
|
0xB9 => f.fp_convert(true, true, true)
|
|
0xBA => f.fp_convert(true, true, false)
|
|
0xBB => f.fp_resize(false, true) // f64.promote_f32
|
|
0xBC..<=0xBF => () // reinterpret: slots hold raw bits
|
|
_ => raise CompileError("unsupported opcode 0x\{op.to_string(radix=16)}")
|
|
}
|
|
}
|
|
f
|
|
}
|
|
|
|
///|
|
|
/// setcc opcode for comparison index eq, ne, lt_s, lt_u, gt_s, gt_u, le_s, le_u, ge_s, ge_u.
|
|
fn setcc_of(i : Int32) -> Int32 {
|
|
[0x94, 0x95, 0x9C, 0x92, 0x9F, 0x97, 0x9E, 0x96, 0x9D, 0x93][i]
|
|
}
|
|
|
|
///|
|
|
fn Fn::push_i64(self : Fn, v : Int64) -> Unit {
|
|
if v.to_int().to_int64() == v {
|
|
self.code.b(0x68) // push imm32 (sign-extended)
|
|
self.code.i32(v.to_int())
|
|
} else {
|
|
self.code.bs([0x48, 0xB8]) // mov rax, imm64
|
|
self.code.i64(v)
|
|
self.code.b(0x50)
|
|
}
|
|
self.height += 1
|
|
}
|
|
|
|
///|
|
|
/// Pop one (or two: a -> xmm0, b -> xmm1) float operands from the stack.
|
|
fn Fn::fp_load(self : Fn, wide : Bool, two : Bool) -> Unit {
|
|
if two {
|
|
self.pop(1)
|
|
}
|
|
self.pop(0)
|
|
let w = if wide { [0x66, 0x48, 0x0F, 0x6E] } else { [0x66, 0x0F, 0x6E] }
|
|
self.code.bs(w)
|
|
self.code.b(0xC0) // movq/movd xmm0,rax
|
|
if two {
|
|
self.code.bs(w)
|
|
self.code.b(0xC9) // movq/movd xmm1,rcx
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn Fn::fp_store(self : Fn, wide : Bool) -> Unit {
|
|
self.code.bs(
|
|
if wide {
|
|
[0x66, 0x48, 0x0F, 0x7E, 0xC0]
|
|
} else {
|
|
[0x66, 0x0F, 0x7E, 0xC0]
|
|
},
|
|
)
|
|
self.push_rax()
|
|
}
|
|
|
|
///|
|
|
/// Scalar-op prefix (F2 for f64, F3 for f32).
|
|
fn sp_of(wide : Bool) -> Int {
|
|
if wide {
|
|
0xF2
|
|
} else {
|
|
0xF3
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Packed/ucomis prefix (66 for f64, none for f32).
|
|
fn Fn::pu(self : Fn, wide : Bool) -> Unit {
|
|
if wide {
|
|
self.code.b(0x66)
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// idx: eq, ne, lt, gt, le, ge. NaN compares false (true for ne).
|
|
fn Fn::fp_cmp(self : Fn, idx : Int, wide : Bool) -> Unit {
|
|
self.fp_load(wide, true)
|
|
self.pu(wide)
|
|
// lt/le compare (b, a) so that "above" is false on unordered
|
|
self.code.bs([0x0F, 0x2E, if idx == 2 || idx == 4 { 0xC8 } else { 0xC1 }])
|
|
match idx {
|
|
0 => self.code.bs([0x0F, 0x94, 0xC0, 0x0F, 0x9B, 0xC1, 0x20, 0xC8]) // sete al ; setnp cl ; and al,cl
|
|
1 => self.code.bs([0x0F, 0x95, 0xC0, 0x0F, 0x9A, 0xC1, 0x08, 0xC8]) // setne al ; setp cl ; or al,cl
|
|
2 | 3 => self.code.bs([0x0F, 0x97, 0xC0]) // seta
|
|
_ => self.code.bs([0x0F, 0x93, 0xC0]) // setae
|
|
}
|
|
self.code.bs([0x0F, 0xB6, 0xC0])
|
|
self.push_rax()
|
|
}
|
|
|
|
///|
|
|
/// op: abs, neg, ceil, floor, trunc, nearest, sqrt.
|
|
fn Fn::fp_unop(self : Fn, op : Int, wide : Bool) -> Unit {
|
|
match op {
|
|
0 | 1 => {
|
|
self.pop(0)
|
|
match (op, wide) {
|
|
(0, true) => self.code.bs([0x48, 0xD1, 0xE0, 0x48, 0xD1, 0xE8]) // shl/shr rax,1
|
|
(0, false) => self.code.bs([0xD1, 0xE0, 0xD1, 0xE8])
|
|
(_, true) => self.code.bs([0x48, 0x0F, 0xBA, 0xF8, 0x3F]) // btc rax,63
|
|
(_, false) => self.code.bs([0x0F, 0xBA, 0xF8, 0x1F]) // btc eax,31
|
|
}
|
|
self.push_rax()
|
|
}
|
|
6 => {
|
|
self.fp_load(wide, false)
|
|
self.code.bs([sp_of(wide), 0x0F, 0x51, 0xC0])
|
|
self.fp_store(wide)
|
|
}
|
|
_ => {
|
|
// roundss/roundsd (SSE4.1): ceil=2, floor=1, trunc=3, nearest=0, no precision exception
|
|
self.fp_load(wide, false)
|
|
let mode = match op {
|
|
2 => 0x0A
|
|
3 => 0x09
|
|
4 => 0x0B
|
|
_ => 0x08
|
|
}
|
|
self.code.bs([0x66, 0x0F, 0x3A, if wide { 0x0B } else { 0x0A }, 0xC0, mode])
|
|
self.fp_store(wide)
|
|
}
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// op: add, sub, mul, div, min, max, copysign.
|
|
fn Fn::fp_binop(self : Fn, op : Int, wide : Bool) -> Unit {
|
|
if op == 6 {
|
|
self.pop(1)
|
|
self.pop(0)
|
|
if wide {
|
|
self.code.bs([0x48, 0xD1, 0xE0, 0x48, 0xD1, 0xE8]) // clear sign of a
|
|
self.code.bs([0x48, 0xC1, 0xE9, 0x3F, 0x48, 0xC1, 0xE1, 0x3F, 0x48, 0x09, 0xC8])
|
|
} else {
|
|
self.code.bs([0xD1, 0xE0, 0xD1, 0xE8])
|
|
self.code.bs([0xC1, 0xE9, 0x1F, 0xC1, 0xE1, 0x1F, 0x09, 0xC8])
|
|
}
|
|
self.push_rax()
|
|
return
|
|
}
|
|
self.fp_load(wide, true)
|
|
let sp = sp_of(wide)
|
|
if op < 4 {
|
|
self.code.bs([sp, 0x0F, [0x58, 0x5C, 0x59, 0x5E][op], 0xC1])
|
|
} else {
|
|
// wasm min/max: NaN-propagating, and -0 < +0
|
|
let is_min = op == 4
|
|
self.pu(wide)
|
|
self.code.bs([0x0F, 0x2E, 0xC1]) // ucomis xmm0,xmm1
|
|
let jp = self.code.rel8([0x7A])
|
|
let jne = self.code.rel8([0x75])
|
|
self.pu(wide)
|
|
self.code.bs([0x0F, if is_min { 0x56 } else { 0x54 }, 0xC1]) // equal: or/and the bits
|
|
let j1 = self.code.rel8([0xEB])
|
|
self.code.patch_here8(jne)
|
|
self.code.bs([sp, 0x0F, if is_min { 0x5D } else { 0x5F }, 0xC1])
|
|
let j2 = self.code.rel8([0xEB])
|
|
self.code.patch_here8(jp)
|
|
self.code.bs([sp, 0x0F, 0x58, 0xC1]) // NaN: a + b
|
|
self.code.patch_here8(j1)
|
|
self.code.patch_here8(j2)
|
|
}
|
|
self.fp_store(wide)
|
|
}
|
|
|
|
///|
|
|
fn Fn::fp_convert(
|
|
self : Fn,
|
|
dst_wide : Bool,
|
|
src64 : Bool,
|
|
signed : Bool,
|
|
) -> Unit {
|
|
self.pop(0)
|
|
let sp = sp_of(dst_wide)
|
|
let cvt = fn() {
|
|
self.code.b(sp)
|
|
if src64 || !signed {
|
|
self.code.b(0x48)
|
|
}
|
|
self.code.bs([0x0F, 0x2A, 0xC0]) // cvtsi2ss/sd xmm0,rax|eax
|
|
}
|
|
if signed {
|
|
cvt()
|
|
} else if !src64 {
|
|
self.code.bs([0x89, 0xC0]) // zero-extend, then signed 64-bit convert
|
|
cvt()
|
|
} else {
|
|
self.code.bs([0x48, 0x85, 0xC0]) // test rax,rax
|
|
let big = self.code.rel8([0x78]) // js big
|
|
cvt()
|
|
let done = self.code.rel8([0xEB])
|
|
self.code.patch_here8(big)
|
|
// halve with sticky low bit, convert, double
|
|
self.code.bs([0x48, 0x89, 0xC1, 0x48, 0xD1, 0xE8, 0x83, 0xE1, 0x01, 0x48, 0x09, 0xC8])
|
|
cvt()
|
|
self.code.bs([sp, 0x0F, 0x58, 0xC0])
|
|
self.code.patch_here8(done)
|
|
}
|
|
self.fp_store(dst_wide)
|
|
}
|
|
|
|
///|
|
|
/// f32.demote_f64 / f64.promote_f32.
|
|
fn Fn::fp_resize(self : Fn, src_wide : Bool, dst_wide : Bool) -> Unit {
|
|
self.fp_load(src_wide, false)
|
|
self.code.bs([sp_of(src_wide), 0x0F, 0x5A, 0xC0])
|
|
self.fp_store(dst_wide)
|
|
}
|
|
|
|
///|
|
|
/// Trapping float -> int truncation (NaN and out-of-range trap).
|
|
fn Fn::fp_trunc(self : Fn, src_wide : Bool, dst64 : Bool, signed : Bool) -> Unit {
|
|
self.fp_load(src_wide, false)
|
|
self.pu(src_wide)
|
|
self.code.bs([0x0F, 0x2E, 0xC0]) // ucomis xmm0,xmm0
|
|
self.trap_jmp(TRAP_INVALID, [0x0F, 0x8A]) // jp
|
|
let sp = sp_of(src_wide)
|
|
let cvtt = fn() { self.code.bs([sp, 0x48, 0x0F, 0x2C, 0xC0]) } // cvttsd2si rax,xmm0
|
|
let load_const = fn(hi_f64 : Int, f32_bits : Int) {
|
|
if src_wide {
|
|
self.code.bs([0x48, 0xB9]) // mov rcx, imm64
|
|
self.code.i32(0)
|
|
self.code.i32(hi_f64)
|
|
self.code.bs([0x66, 0x48, 0x0F, 0x6E, 0xC9])
|
|
} else {
|
|
self.code.b(0xB9)
|
|
self.code.i32(f32_bits)
|
|
self.code.bs([0x66, 0x0F, 0x6E, 0xC9])
|
|
}
|
|
}
|
|
match (dst64, signed) {
|
|
(false, true) => {
|
|
cvtt()
|
|
self.code.bs([0x48, 0x63, 0xD0, 0x48, 0x39, 0xC2]) // movsxd rdx,eax ; cmp rdx,rax
|
|
self.trap_jmp(TRAP_OVERFLOW, [0x0F, 0x85])
|
|
}
|
|
(false, false) => {
|
|
cvtt()
|
|
self.code.bs([0x48, 0x89, 0xC2, 0x48, 0xC1, 0xEA, 0x20]) // mov rdx,rax ; shr rdx,32
|
|
self.trap_jmp(TRAP_OVERFLOW, [0x0F, 0x85])
|
|
}
|
|
(true, true) => {
|
|
cvtt()
|
|
self.code.bs([0x48, 0xBA]) // mov rdx, INT64_MIN
|
|
self.code.i64(-9223372036854775808L)
|
|
self.code.bs([0x48, 0x39, 0xD0]) // cmp rax,rdx
|
|
let ok = self.code.rel8([0x75])
|
|
// indefinite result: valid only if the input is exactly -2^63
|
|
load_const(-1008730112, -553648128)
|
|
self.pu(src_wide)
|
|
self.code.bs([0x0F, 0x2E, 0xC1])
|
|
self.trap_jmp(TRAP_OVERFLOW, [0x0F, 0x85])
|
|
self.code.patch_here8(ok)
|
|
}
|
|
(true, false) => {
|
|
load_const(1138753536, 1593835520) // 2^63
|
|
self.pu(src_wide)
|
|
self.code.bs([0x0F, 0x2E, 0xC1])
|
|
let big = self.code.rel8([0x73]) // jae big
|
|
cvtt()
|
|
self.code.bs([0x48, 0x85, 0xC0])
|
|
self.trap_jmp(TRAP_OVERFLOW, [0x0F, 0x88]) // js
|
|
let done = self.code.rel8([0xEB])
|
|
self.code.patch_here8(big)
|
|
self.code.bs([sp, 0x0F, 0x5C, 0xC1]) // sub xmm0,xmm1
|
|
cvtt()
|
|
self.code.bs([0x48, 0x85, 0xC0])
|
|
self.trap_jmp(TRAP_OVERFLOW, [0x0F, 0x88])
|
|
self.code.bs([0x48, 0x0F, 0xBA, 0xF8, 0x3F]) // btc rax,63
|
|
self.code.patch_here8(done)
|
|
}
|
|
}
|
|
self.push_rax()
|
|
}
|