diff --git a/README.mbt.md b/README.mbt.md index fc68704..23ed880 100644 --- a/README.mbt.md +++ b/README.mbt.md @@ -29,9 +29,12 @@ can build the example. ## Supported subset -i32 and i64; locals, params, calls, block/loop/if/else/br/br_if/return, select/drop, -integer arithmetic/comparison/shift/rotate (i64 add/sub/mul/div/rem/and/or/xor/shifts/ -comparisons, wrap/extend, sign-extension ops), one fixed-size linear memory (i32/i64 loads/stores incl. narrow -forms, with bounds checks, `memory.size`; `memory.grow` always returns -1), i32/i64 globals, active data -segments. Not supported: clz/ctz/popcnt, floats, tables/`call_indirect`, `br_table`, start function, -non-function imports. Input is assumed valid (`wasm-tools validate` it first). +i32, i64, f32 and f64; locals, params, calls, block/loop/if/else/br/br_if/return, +select/drop, integer arithmetic/comparison/shift/rotate, sign-extension ops, float +arithmetic/comparison/min/max/copysign/rounding/sqrt (SSE scalar ops; rounding needs SSE4.1), +all int/float conversions (`trunc` traps on NaN and out-of-range; no `trunc_sat`), +reinterpret, one fixed-size linear memory (all i32/i64/f32/f64 loads/stores incl. narrow +forms, with bounds checks, `memory.size`; `memory.grow` always returns -1), globals of any +numeric type, active data segments. Values live in 8-byte stack slots; floats are kept as +raw bits. Not supported: clz/ctz/popcnt, v128/reference types, tables/`call_indirect`, +`br_table`, start function, non-function imports. Input is assumed valid (`wasm-tools validate` it first). diff --git a/asm.mbt b/asm.mbt index 32d9180..479aaad 100644 --- a/asm.mbt +++ b/asm.mbt @@ -64,3 +64,18 @@ fn Asm::i64(self : Asm, x : Int64) -> Unit { 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() +} diff --git a/compile.mbt b/compile.mbt index 3e98ae8..5b90389 100644 --- a/compile.mbt +++ b/compile.mbt @@ -41,6 +41,9 @@ 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) @@ -107,7 +110,7 @@ fn Fn::emit_br(self : Fn, depth : Int32, h : Int32) -> Unit raise CompileError { fn Fn::block_type(r : Reader) -> Int32 raise CompileError { match r.byte() { 0x40 => 0 - 0x7F | 0x7E => 1 + 0x7C..<=0x7F => 1 b => raise CompileError("unsupported block type 0x\{b.to_string(radix=16)}") } } @@ -216,8 +219,8 @@ fn Fn::mem_access(self : Fn, r : Reader, op : Int32) -> Unit raise CompileError raise CompileError("memory offset too large") } let size = match op { - 0x29 | 0x37 => 8 - 0x28 | 0x34 | 0x35 | 0x36 | 0x3E => 4 + 0x29 | 0x2B | 0x37 | 0x39 => 8 + 0x28 | 0x2A | 0x34 | 0x35 | 0x36 | 0x38 | 0x3E => 4 0x2C | 0x2D | 0x30 | 0x31 | 0x3A | 0x3C => 1 _ => 2 } @@ -233,8 +236,8 @@ fn Fn::mem_access(self : Fn, r : Reader, op : Int32) -> Unit raise CompileError self.code.i32(self.mem_bytes - size) self.trap_jmp(TRAP_OOB, [0x0F, 0x87]) // ja trap match op { - 0x28 | 0x35 => self.code.bs([0x8B, 0x80]) - 0x29 => self.code.bs([0x48, 0x8B, 0x80]) + 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]) @@ -242,8 +245,8 @@ fn Fn::mem_access(self : Fn, r : Reader, op : Int32) -> Unit raise CompileError 0x30 => self.code.bs([0x48, 0x0F, 0xBE, 0x80]) 0x32 => self.code.bs([0x48, 0x0F, 0xBF, 0x80]) 0x34 => self.code.bs([0x48, 0x63, 0x80]) - 0x36 | 0x3E => self.code.bs([0x89, 0x88]) - 0x37 => self.code.bs([0x48, 0x89, 0x88]) + 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 } @@ -349,6 +352,8 @@ fn compile_body( 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()) @@ -468,12 +473,7 @@ fn compile_body( f.code.bs([0x48, 0x89, 0x04, 0x25]) // mov [abs],rax f.code.i32(a) } - n if n >= 0x28 && - n <= 0x3E && - n != 0x2A && - n != 0x2B && - n != 0x38 && - n != 0x39 => f.mem_access(r, op) + n if n >= 0x28 && n <= 0x3E => f.mem_access(r, op) 0x3F => { ignore(r.byte()) f.code.b(0x68) @@ -497,18 +497,9 @@ fn compile_body( f.code.bs([0x85, 0xC0, 0x0F, 0x94, 0xC0, 0x0F, 0xB6, 0xC0]) f.push_rax() } - 0x42 => { - let v = r.s64() - if v.to_int().to_int64() == v { - f.code.b(0x68) // push imm32 (sign-extended) - f.code.i32(v.to_int()) - } else { - f.code.bs([0x48, 0xB8]) // mov rax, imm64 - f.code.i64(v) - f.code.b(0x50) - } - f.height += 1 - } + 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) @@ -543,6 +534,31 @@ fn compile_body( 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)}") } } @@ -554,3 +570,264 @@ fn compile_body( 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() +} diff --git a/examples/float.wat b/examples/float.wat new file mode 100644 index 0000000..89e5709 --- /dev/null +++ b/examples/float.wat @@ -0,0 +1,78 @@ +;; f32/f64 arithmetic, comparisons, rounding, min/max, conversions, memory and globals. +;; Exits 0 if all checks pass, otherwise with the number of the first failing check. +(module + (memory 1) + (global $g (mut f64) (f64.const 1.5)) + (global $h (mut f32) (f32.const 2.5)) + (func $nan64 (result f64) (f64.div (f64.const 0) (f64.const 0))) + (func $nan32 (result f32) (f32.div (f32.const 0) (f32.const 0))) + (func $hyp (param $a f64) (param $b f64) (result f64) + (f64.sqrt (f64.add (f64.mul (local.get $a) (local.get $a)) (f64.mul (local.get $b) (local.get $b))))) + (func (export "main") (result i32) + (local $x f32) + ;; 1-4: basic arithmetic + (if (f64.ne (call $hyp (f64.const 3) (f64.const 4)) (f64.const 5)) (then (return (i32.const 1)))) + (if (f32.ne (f32.mul (f32.const 1.5) (f32.const 4)) (f32.const 6)) (then (return (i32.const 2)))) + (if (f64.ne (f64.div (f64.const 1) (f64.const 4)) (f64.const 0.25)) (then (return (i32.const 3)))) + (if (f32.ne (f32.sub (f32.const 0.5) (f32.const 2)) (f32.const -1.5)) (then (return (i32.const 4)))) + ;; 5-8: comparisons incl. NaN + (if (i32.eqz (f64.lt (f64.const 1) (f64.const 2))) (then (return (i32.const 5)))) + (if (i32.eqz (f64.ge (f64.const 2) (f64.const 2))) (then (return (i32.const 6)))) + (if (f64.eq (call $nan64) (call $nan64)) (then (return (i32.const 7)))) + (if (i32.eqz (f64.ne (call $nan64) (f64.const 1))) (then (return (i32.const 8)))) + (if (f64.lt (call $nan64) (f64.const 1)) (then (return (i32.const 9)))) + (if (f64.le (f64.const 1) (call $nan64)) (then (return (i32.const 10)))) + (if (f32.gt (call $nan32) (f32.const 1)) (then (return (i32.const 11)))) + (if (i32.eqz (f32.le (f32.const 1) (f32.const 1))) (then (return (i32.const 12)))) + ;; 13-18: rounding + (if (f64.ne (f64.ceil (f64.const 1.2)) (f64.const 2)) (then (return (i32.const 13)))) + (if (f64.ne (f64.floor (f64.const -1.2)) (f64.const -2)) (then (return (i32.const 14)))) + (if (f64.ne (f64.trunc (f64.const -1.7)) (f64.const -1)) (then (return (i32.const 15)))) + (if (f64.ne (f64.nearest (f64.const 2.5)) (f64.const 2)) (then (return (i32.const 16)))) + (if (f64.ne (f64.nearest (f64.const 3.5)) (f64.const 4)) (then (return (i32.const 17)))) + (if (f32.ne (f32.nearest (f32.const -0.5)) (f32.const -0)) (then (return (i32.const 18)))) + ;; 19-24: abs / neg / copysign / min / max (signed zero and NaN) + (if (f64.ne (f64.abs (f64.const -3)) (f64.const 3)) (then (return (i32.const 19)))) + (if (f32.ne (f32.neg (f32.const 3)) (f32.const -3)) (then (return (i32.const 20)))) + (if (f64.ne (f64.copysign (f64.const 2) (f64.const -0)) (f64.const -2)) (then (return (i32.const 21)))) + (if (f64.ne (f64.min (f64.const 1) (f64.const 2)) (f64.const 1)) (then (return (i32.const 22)))) + (if (f64.ne (f64.max (f64.const 1) (f64.const 2)) (f64.const 2)) (then (return (i32.const 23)))) + (if (i64.ne (i64.reinterpret_f64 (f64.min (f64.const 0) (f64.const -0))) (i64.const 0x8000000000000000)) (then (return (i32.const 24)))) + (if (i64.ne (i64.reinterpret_f64 (f64.max (f64.const -0) (f64.const 0))) (i64.const 0)) (then (return (i32.const 25)))) + (if (f64.eq (f64.min (call $nan64) (f64.const 1)) (f64.const 1)) (then (return (i32.const 26)))) + (if (f64.eq (f64.max (f64.const 1) (call $nan64)) (f64.const 1)) (then (return (i32.const 27)))) + (if (f32.ne (f32.min (f32.const 3) (f32.const -4)) (f32.const -4)) (then (return (i32.const 28)))) + ;; 29-34: int -> float + (if (f64.ne (f64.convert_i32_s (i32.const -7)) (f64.const -7)) (then (return (i32.const 29)))) + (if (f64.ne (f64.convert_i32_u (i32.const -1)) (f64.const 4294967295)) (then (return (i32.const 30)))) + (if (f64.ne (f64.convert_i64_s (i64.const -5000000000)) (f64.const -5000000000)) (then (return (i32.const 31)))) + (if (f64.ne (f64.convert_i64_u (i64.const -1)) (f64.const 18446744073709551616)) (then (return (i32.const 32)))) + (if (f32.ne (f32.convert_i32_s (i32.const 100)) (f32.const 100)) (then (return (i32.const 33)))) + (if (f32.ne (f32.convert_i64_u (i64.const 0x8000000000000000)) (f32.const 9223372036854775808)) (then (return (i32.const 34)))) + ;; 35-44: float -> int (truncating) + (if (i32.ne (i32.trunc_f64_s (f64.const -3.9)) (i32.const -3)) (then (return (i32.const 35)))) + (if (i32.ne (i32.trunc_f32_u (f32.const 4000000000)) (i32.const -294967296)) (then (return (i32.const 36)))) + (if (i32.ne (i32.trunc_f64_u (f64.const -0.5)) (i32.const 0)) (then (return (i32.const 37)))) + (if (i64.ne (i64.trunc_f64_s (f64.const -9223372036854775808)) (i64.const 0x8000000000000000)) (then (return (i32.const 38)))) + (if (i64.ne (i64.trunc_f64_s (f64.const 1e18)) (i64.const 1000000000000000000)) (then (return (i32.const 39)))) + (if (i64.ne (i64.trunc_f64_u (f64.const 1.8e19)) (i64.const 0xF9CCD8A1C5080000)) (then (return (i32.const 40)))) + (if (i64.ne (i64.trunc_f32_u (f32.const 3e9)) (i64.const 3000000000)) (then (return (i32.const 41)))) + (if (i64.ne (i64.trunc_f32_s (f32.const -2e9)) (i64.const -2000000000)) (then (return (i32.const 42)))) + ;; 43-45: demote / promote / reinterpret + (if (f32.ne (f32.demote_f64 (f64.const 0.1)) (f32.const 0.1)) (then (return (i32.const 43)))) + (if (f64.ne (f64.promote_f32 (f32.const 0.5)) (f64.const 0.5)) (then (return (i32.const 44)))) + (if (i32.ne (i32.reinterpret_f32 (f32.const 1)) (i32.const 0x3f800000)) (then (return (i32.const 45)))) + (if (f32.ne (f32.reinterpret_i32 (i32.const 0x40000000)) (f32.const 2)) (then (return (i32.const 46)))) + ;; 47-49: memory + (f64.store (i32.const 16) (f64.const 6.25)) + (if (f64.ne (f64.load (i32.const 16)) (f64.const 6.25)) (then (return (i32.const 47)))) + (f32.store (i32.const 32) (f32.const -0.75)) + (if (f32.ne (f32.load (i32.const 32)) (f32.const -0.75)) (then (return (i32.const 48)))) + (if (i32.ne (i32.load (i32.const 32)) (i32.const 0xbf400000)) (then (return (i32.const 49)))) + ;; 50-52: globals, locals, select + (global.set $g (f64.mul (global.get $g) (f64.const 2))) + (if (f64.ne (global.get $g) (f64.const 3)) (then (return (i32.const 50)))) + (if (f32.ne (global.get $h) (f32.const 2.5)) (then (return (i32.const 51)))) + (local.set $x (select (f32.const 7) (f32.const 8) (i32.const 0))) + (if (f32.ne (local.get $x) (f32.const 8)) (then (return (i32.const 52)))) + (i32.const 0))) diff --git a/examples/fptrap.wat b/examples/fptrap.wat new file mode 100644 index 0000000..107402d --- /dev/null +++ b/examples/fptrap.wat @@ -0,0 +1,4 @@ +;; float -> int conversion of an out-of-range value traps (exit 134) +(module + (func (export "main") (result i32) + (i32.trunc_f64_s (f64.const 3e9)))) diff --git a/module.mbt b/module.mbt index f6d5679..30d21d7 100644 --- a/module.mbt +++ b/module.mbt @@ -1,9 +1,9 @@ ///| -fn expect_int(r : Reader) -> Unit raise CompileError { +fn expect_num(r : Reader) -> Unit raise CompileError { let t = r.byte() - if t != 0x7F && t != 0x7E { + if t < 0x7C || t > 0x7F { raise CompileError( - "only i32/i64 value types are supported (got 0x\{t.to_string(radix=16)})", + "only numeric value types are supported (got 0x\{t.to_string(radix=16)})", ) } } @@ -32,7 +32,7 @@ const MEM_BASE : Int32 = 0x10001000 ///| let messages : Array[String] = [ "wasm trap: unreachable\n", "wasm trap: integer divide by zero\n", "wasm trap: integer overflow\n", - "wasm trap: out of bounds memory access\n", + "wasm trap: out of bounds memory access\n", "wasm trap: invalid conversion to integer\n", ] ///| @@ -104,7 +104,7 @@ fn module_exports_prefix(m : Bytes, prefix : String) -> Bool raise CompileError } ///| -/// Compile a WebAssembly core module (i32/i64 subset) or a component whose +/// Compile a WebAssembly core module (i32/i64/f32/f64 subset) or a component whose /// core module targets `wasi:cli` to a static x86-64 Linux ELF executable. /// The entry export is called from `_start` and its i32 result becomes the /// process exit code. @@ -166,14 +166,14 @@ fn compile_core(wasm : Bytes, entry : String?) -> Bytes raise CompileError { } let np = r.u32() for _ in 0.. 1 { raise CompileError("multi-value results are not supported") } for _ in 0.. Bytes raise CompileError { } 6 => for _ in 0.. globals.push(r.s32().to_int64()) 0x42 => globals.push(r.s64()) - _ => - raise CompileError( - "global initializers must be i32.const or i64.const", - ) + 0x43 => globals.push(r.raw32().to_int64() & 0xFFFFFFFFL) + 0x44 => globals.push(r.raw64()) + _ => raise CompileError("global initializers must be a numeric const") } if r.byte() != 0x0B { raise CompileError("bad global initializer") @@ -315,7 +314,7 @@ fn compile_core(wasm : Bytes, entry : String?) -> Bytes raise CompileError { let mut nlocals = ft.nparams for _ in 0.. Int64 raise CompileError { } result } + +///| +fn Reader::raw32(self : Reader) -> Int raise CompileError { + let mut v = 0 + for i in 0..<4 { + v = v | (self.byte() << (8 * i)) + } + v +} + +///| +fn Reader::raw64(self : Reader) -> Int64 raise CompileError { + let mut v = 0L + for i in 0..<8 { + v = v | (self.byte().to_int64() << (8 * i)) + } + v +}