Add i64 support

Stack slots, locals and globals were already 8 bytes wide, so i64 is mostly
64-bit encodings of the existing i32 ops: constants, comparisons, arithmetic,
bitwise, shifts/rotates, div/rem (with zero and overflow traps), wrap/extend,
sign-extension ops, i64 loads/stores (incl. narrow forms) and 64-bit globals.
Adds examples/i64.wat, which self-checks and exits 0 on success.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
SpriteandClaude Sonnet 5.5 committed 2026-11-21 09:49:28 +00:00
1 parent 05488c9783
commit 4c4fa91dc9
6 files changed
+294 -126

No files matched your search

+36 -32
View File
@@ -1,15 +1,15 @@
///|
fn expect_i32(r : Reader) -> Unit raise CompileError {
fn expect_int(r : Reader) -> Unit raise CompileError {
let t = r.byte()
if t != 0x7F {
if t != 0x7F && t != 0x7E {
raise CompileError(
"only i32 value types are supported (got 0x\{t.to_string(radix=16)})",
"only i32/i64 value types are supported (got 0x\{t.to_string(radix=16)})",
)
}
}
///|
fn le(out : Array[Byte], v : Int, n : Int) -> Unit {
fn le(out : Array[Byte], v : Int32, n : Int32) -> Unit {
// little-endian, zero-extended past 32 bits (Int shifts wrap mod 32)
for i in 0..<n {
out.push(if i < 4 { ((v >> (8 * i)) & 0xFF).to_byte() } else { b'\x00' })
@@ -17,17 +17,17 @@ fn le(out : Array[Byte], v : Int, n : Int) -> Unit {
}
///|
const BASE : Int = 0x400000
const BASE : Int32 = 0x400000
///|
/// Read/write data segment: one page of globals, then linear memory.
const SEG_BASE : Int = 0x10000000
const SEG_BASE : Int32 = 0x10000000
///|
const GLOB_BASE : Int = 0x10000000
const GLOB_BASE : Int32 = 0x10000000
///|
const MEM_BASE : Int = 0x10001000
const MEM_BASE : Int32 = 0x10001000
///|
let messages : Array[String] = [
@@ -75,10 +75,7 @@ fn import_stub(m : String, name : String) -> Asm? {
"wasi:cli/exit::exit" =>
// (status: result) -> () ; 0 = ok, 1 = err
s.bs([0x8B, 0x7D, 0x10, 0xB8, 0xE7, 0x00, 0x00, 0x00, 0x0F, 0x05])
_ =>
if !name.has_prefix("[resource-drop]") {
return None
}
_ => if !name.has_prefix("[resource-drop]") { return None }
}
s.bs([0x5D, 0xC3]) // pop rbp ; ret
Some(s)
@@ -107,7 +104,7 @@ fn module_exports_prefix(m : Bytes, prefix : String) -> Bool raise CompileError
}
///|
/// Compile a WebAssembly core module (i32-only subset) or a component whose
/// Compile a WebAssembly core module (i32/i64 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.
@@ -149,11 +146,11 @@ fn compile_core(wasm : Bytes, entry : String?) -> Bytes raise CompileError {
}
let r = Reader::new(wasm, 8)
let types : Array[FuncType] = []
let func_types : Array[Int] = [] // function index space: imports first
let func_types : Array[Int32] = [] // function index space: imports first
let imports : Array[(String, String)] = []
let exports : Array[(String, Int)] = []
let bodies : Array[(Int, Int)] = [] // (start of locals, end)
let globals : Array[Int] = []
let exports : Array[(String, Int32)] = []
let bodies : Array[(Int32, Int32)] = [] // (start of locals, end)
let globals : Array[Int64] = []
let mut mem_bytes = 0
let image : Array[Byte] = [] // initial contents of the RW segment (after globals page)
while r.pos < wasm.length() {
@@ -169,16 +166,16 @@ fn compile_core(wasm : Bytes, entry : String?) -> Bytes raise CompileError {
}
let np = r.u32()
for _ in 0..<np {
expect_i32(r)
expect_int(r)
}
let nr = r.u32()
if nr > 1 {
raise CompileError("multi-value results are not supported")
}
for _ in 0..<nr {
expect_i32(r)
expect_int(r)
}
types.push({ nparams: np, nresults: nr })
types.push({ nparams: np, nresults: nr, })
}
2 =>
for _ in 0..<r.u32() {
@@ -212,12 +209,16 @@ fn compile_core(wasm : Bytes, entry : String?) -> Bytes raise CompileError {
}
6 =>
for _ in 0..<r.u32() {
expect_i32(r)
expect_int(r)
ignore(r.byte()) // mutability
if r.byte() != 0x41 {
raise CompileError("global initializers must be i32.const")
match r.byte() {
0x41 => globals.push(r.s32().to_int64())
0x42 => globals.push(r.s64())
_ =>
raise CompileError(
"global initializers must be i32.const or i64.const",
)
}
globals.push(r.s32())
if r.byte() != 0x0B {
raise CompileError("bad global initializer")
}
@@ -240,7 +241,9 @@ fn compile_core(wasm : Bytes, entry : String?) -> Bytes raise CompileError {
11 =>
for _ in 0..<r.u32() {
if r.u32() != 0 {
raise CompileError("only active data segments on memory 0 are supported")
raise CompileError(
"only active data segments on memory 0 are supported",
)
}
if r.byte() != 0x41 {
raise CompileError("data offsets must be i32.const")
@@ -286,7 +289,8 @@ fn compile_core(wasm : Bytes, entry : String?) -> Bytes raise CompileError {
match find("main") {
Some(i) => exports[i].1
None =>
match exports.search_by(fn(e) { e.0.has_prefix("wasi:cli/run@") }) {
match
exports.search_by(fn(e) { e.0.has_prefix("wasi:cli/run@") }) {
Some(i) => exports[i].1
None =>
if exports.length() > 0 {
@@ -311,7 +315,7 @@ fn compile_core(wasm : Bytes, entry : String?) -> Bytes raise CompileError {
let mut nlocals = ft.nparams
for _ in 0..<br.u32() {
let count = br.u32()
expect_i32(br)
expect_int(br)
nlocals += count
}
fns.push(
@@ -338,8 +342,8 @@ fn compile_core(wasm : Bytes, entry : String?) -> Bytes raise CompileError {
a.bs([0x31, 0xFF]) // xor edi,edi
}
a.bs([0xB8, 0xE7, 0x00, 0x00, 0x00, 0x0F, 0x05]) // exit_group
let stub_at : Array[Int] = []
let lea_at : Array[Int] = []
let stub_at : Array[Int32] = []
let lea_at : Array[Int32] = []
for _ in 0..<messages.length() {
stub_at.push(a.here())
lea_at.push(a.rel([0x48, 0x8D, 0x35])) // lea rsi,[rip+msg]
@@ -350,7 +354,7 @@ fn compile_core(wasm : Bytes, entry : String?) -> Bytes raise CompileError {
a.bs([0xBF, 0x86, 0x00, 0x00, 0x00]) // mov edi,134
a.bs([0xB8, 0xE7, 0x00, 0x00, 0x00, 0x0F, 0x05]) // exit_group
}
let fn_at : Array[Int] = []
let fn_at : Array[Int32] = []
for (m, n) in imports {
fn_at.push(a.here())
match import_stub(m, n) {
@@ -428,8 +432,8 @@ fn compile_core(wasm : Bytes, entry : String?) -> Bytes raise CompileError {
seg.push(b'\x00')
}
for i in 0..<globals.length() {
for k in 0..<4 {
seg[8 * i + k] = ((globals[i] >> (8 * k)) & 0xFF).to_byte()
for k in 0..<8 {
seg[8 * i + k] = ((globals[i] >> (8 * k)) & 0xFFL).to_int().to_byte()
}
}
for c in image {