Add i64 and f32/f64 support (README updated to match) (#3)
This commit was merged in pull request #3.
This commit is contained in:
1 parent
05488c9783
commit
987cdf256b
8 files changed
+690
-128
No files matched your search
+36
-33
@@ -1,15 +1,15 @@
|
||||
///|
|
||||
fn expect_i32(r : Reader) -> Unit raise CompileError {
|
||||
fn expect_num(r : Reader) -> Unit raise CompileError {
|
||||
let t = r.byte()
|
||||
if t != 0x7F {
|
||||
if t < 0x7C || t > 0x7F {
|
||||
raise CompileError(
|
||||
"only i32 value types are supported (got 0x\{t.to_string(radix=16)})",
|
||||
"only numeric 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,22 +17,22 @@ 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] = [
|
||||
"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",
|
||||
]
|
||||
|
||||
///|
|
||||
@@ -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/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.
|
||||
@@ -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_num(r)
|
||||
}
|
||||
let nr = r.u32()
|
||||
if nr > 1 {
|
||||
raise CompileError("multi-value results are not supported")
|
||||
}
|
||||
for _ in 0..<nr {
|
||||
expect_i32(r)
|
||||
expect_num(r)
|
||||
}
|
||||
types.push({ nparams: np, nresults: nr })
|
||||
types.push({ nparams: np, nresults: nr, })
|
||||
}
|
||||
2 =>
|
||||
for _ in 0..<r.u32() {
|
||||
@@ -212,12 +209,15 @@ fn compile_core(wasm : Bytes, entry : String?) -> Bytes raise CompileError {
|
||||
}
|
||||
6 =>
|
||||
for _ in 0..<r.u32() {
|
||||
expect_i32(r)
|
||||
expect_num(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())
|
||||
0x43 => globals.push(r.raw32().to_int64() & 0xFFFFFFFFL)
|
||||
0x44 => globals.push(r.raw64())
|
||||
_ => raise CompileError("global initializers must be a numeric const")
|
||||
}
|
||||
globals.push(r.s32())
|
||||
if r.byte() != 0x0B {
|
||||
raise CompileError("bad global initializer")
|
||||
}
|
||||
@@ -240,7 +240,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 +288,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 +314,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_num(br)
|
||||
nlocals += count
|
||||
}
|
||||
fns.push(
|
||||
@@ -338,8 +341,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 +353,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 +431,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 {
|
||||
|
||||
Reference in new issue
Block a user