Files
wasm2elf-mb/reader.mbt
T
SpriteandClaude Sonnet 5.5 4c4fa91dc9 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>
2026-11-21 09:49:28 +00:00

98 lines
1.8 KiB
MoonBit

///|
pub suberror CompileError {
CompileError(String)
}
///|
priv struct Reader {
data : Bytes
mut pos : Int32
}
///|
fn Reader::new(data : Bytes, pos : Int32) -> Reader {
{ data, pos, }
}
///|
fn Reader::byte(self : Reader) -> Int32 raise CompileError {
if self.pos >= self.data.length() {
raise CompileError("unexpected end of input")
}
let b = self.data[self.pos].to_int()
self.pos += 1
b
}
///|
fn Reader::u32(self : Reader) -> Int32 raise CompileError {
let mut result = 0
let mut shift = 0
while true {
let b = self.byte()
result = result | ((b & 0x7F) << shift)
if (b & 0x80) == 0 {
break
}
shift += 7
if shift > 35 {
raise CompileError("LEB128 too long")
}
}
result
}
///|
fn Reader::s32(self : Reader) -> Int32 raise CompileError {
let mut result = 0
let mut shift = 0
let mut b = 0
while true {
b = self.byte()
result = result | ((b & 0x7F) << shift)
shift += 7
if (b & 0x80) == 0 {
break
}
if shift > 35 {
raise CompileError("LEB128 too long")
}
}
if shift < 32 && (b & 0x40) != 0 {
result = result | (-1 << shift)
}
result
}
///|
fn Reader::name(self : Reader) -> String raise CompileError {
let len = self.u32()
let sb = StringBuilder()
for _ in 0..<len {
sb.write_char(self.byte().unsafe_to_char())
}
sb.to_string()
}
///|
fn Reader::s64(self : Reader) -> Int64 raise CompileError {
let mut result = 0L
let mut shift = 0
let mut b = 0
while true {
b = self.byte()
result = result | ((b & 0x7F).to_int64() << shift)
shift += 7
if (b & 0x80) == 0 {
break
}
if shift > 63 {
raise CompileError("LEB128 too long")
}
}
if shift < 64 && (b & 0x40) != 0 {
result = result | (-1L << shift)
}
result
}