Files
wasm2elf-mb/asm.mbt
T
SpriteandClaude Sonnet 5.5 f0d996cee9 Add f32/f64 support
Floats live in the existing 8-byte stack slots as raw bits and are computed
with scalar SSE ops via xmm0/xmm1. Adds consts, loads/stores, comparisons
(NaN-correct), abs/neg/copysign, ceil/floor/trunc/nearest (roundss/sd),
sqrt, add/sub/mul/div, wasm-semantics min/max (NaN propagating, -0 < +0),
int<->float conversions (incl. unsigned 64-bit), demote/promote,
reinterpret, and float globals. Trapping truncation traps on NaN
(new 'invalid conversion to integer' message) and on out-of-range values.

Adds examples/float.wat (52 self-checks) and examples/fptrap.wat.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-21 09:55:08 +00:00

82 lines
1.6 KiB
MoonBit

///|
/// Growable x86-64 machine-code buffer with rel32 patching helpers.
priv struct Asm {
code : Array[Byte]
}
///|
fn Asm::new() -> Asm {
{ code: [], }
}
///|
fn Asm::here(self : Asm) -> Int32 {
self.code.length()
}
///|
fn Asm::b(self : Asm, x : Int32) -> Unit {
self.code.push((x & 0xFF).to_byte())
}
///|
fn Asm::bs(self : Asm, xs : Array[Int32]) -> Unit {
for x in xs {
self.b(x)
}
}
///|
fn Asm::i32(self : Asm, x : Int32) -> Unit {
self.b(x)
self.b(x >> 8)
self.b(x >> 16)
self.b(x >> 24)
}
///|
/// Emit opcode bytes followed by a rel32 placeholder; returns the placeholder's position.
fn Asm::rel(self : Asm, op : Array[Int32]) -> Int32 {
self.bs(op)
let at = self.here()
self.i32(0)
at
}
///|
/// Point the rel32 placeholder at `at` to `target`.
fn Asm::patch(self : Asm, at : Int32, target : Int32) -> Unit {
let v = target - (at + 4)
self.code[at] = (v & 0xFF).to_byte()
self.code[at + 1] = ((v >> 8) & 0xFF).to_byte()
self.code[at + 2] = ((v >> 16) & 0xFF).to_byte()
self.code[at + 3] = ((v >> 24) & 0xFF).to_byte()
}
///|
fn Asm::patch_here(self : Asm, at : Int32) -> Unit {
self.patch(at, self.here())
}
///|
fn Asm::i64(self : Asm, x : Int64) -> Unit {
for k in 0..<8 {
self.b((x >> (8 * k)).to_int())
}
}
///|
/// Emit opcode bytes followed by a rel8 placeholder; returns its position.
fn Asm::rel8(self : Asm, op : Array[Int]) -> Int {
self.bs(op)
let at = self.here()
self.b(0)
at
}
///|
/// Point the rel8 placeholder at `at` to the current position.
fn Asm::patch_here8(self : Asm, at : Int) -> Unit {
self.code[at] = (self.here() - (at + 1)).to_byte()
}