Add i64 support #1
No files matched your search
@@ -0,0 +1,15 @@
|
||||
# Sweet repository configuration
|
||||
types:
|
||||
fix:
|
||||
bump: patch
|
||||
section: Fixes
|
||||
feature:
|
||||
bump: minor
|
||||
section: Features
|
||||
chore:
|
||||
bump: none
|
||||
refactor:
|
||||
bump: none
|
||||
performance:
|
||||
bump: patch
|
||||
section: Performance
|
||||
@@ -0,0 +1,5 @@
|
||||
name: wasm2elf
|
||||
version: 0.1.0
|
||||
version-files:
|
||||
- path: moon.mod
|
||||
format: annotated
|
||||
@@ -51,3 +51,11 @@ You can browse and install extra skills here:
|
||||
scientific computations), prefer assertion tests. You can use
|
||||
`moon coverage analyze > uncovered.log` to see which parts of your code are
|
||||
not covered by tests.
|
||||
|
||||
## Releases
|
||||
|
||||
Releases are managed with `sweet`
|
||||
(config in `.sweet.config.yml` and `.sweet.yml`). For any user-visible change,
|
||||
add a release note with `sweet change`. `sweet bump` updates the version in
|
||||
`moon.mod` (the line tagged `// sweet-version`; keep that comment). Run
|
||||
`sweet doctor` to validate.
|
||||
@@ -6,6 +6,7 @@ runtime: the output is hand-encoded machine code in one or two `PT_LOAD` segment
|
||||
```
|
||||
just run sum # core module -> ELF, runs it, prints the exit code
|
||||
just run-component hello # wasi:cli component -> ELF, prints "Hello from wasi:cli!"
|
||||
just run-component fact # wasi:cli component -> ELF, prints "fact(5) = 120"
|
||||
just examples # everything in examples/
|
||||
```
|
||||
|
||||
|
||||
+95
-9
@@ -355,6 +355,15 @@ fn compile_body(
|
||||
0x43 => ignore(r.raw32())
|
||||
0x44 => ignore(r.raw64())
|
||||
0x3F | 0x40 => ignore(r.byte())
|
||||
0xFC =>
|
||||
match r.u32() {
|
||||
0x0A => {
|
||||
ignore(r.byte())
|
||||
ignore(r.byte())
|
||||
}
|
||||
0x0B => ignore(r.byte())
|
||||
_ => ()
|
||||
}
|
||||
n if n >= 0x28 && n <= 0x3E => {
|
||||
ignore(r.u32())
|
||||
ignore(r.u32())
|
||||
@@ -487,6 +496,7 @@ fn compile_body(
|
||||
f.code.bs([0x6A, 0xFF])
|
||||
f.height += 1
|
||||
}
|
||||
0xFC => f.bulk_mem(r.u32(), r)
|
||||
0x41 => {
|
||||
f.code.b(0x68)
|
||||
f.code.i32(r.s32())
|
||||
@@ -614,7 +624,7 @@ fn Fn::fp_store(self : Fn, wide : Bool) -> Unit {
|
||||
|
||||
///|
|
||||
/// Scalar-op prefix (F2 for f64, F3 for f32).
|
||||
fn sp_of(wide : Bool) -> Int {
|
||||
fn sp_of(wide : Bool) -> Int32 {
|
||||
if wide {
|
||||
0xF2
|
||||
} else {
|
||||
@@ -632,7 +642,7 @@ fn Fn::pu(self : Fn, wide : Bool) -> Unit {
|
||||
|
||||
///|
|
||||
/// idx: eq, ne, lt, gt, le, ge. NaN compares false (true for ne).
|
||||
fn Fn::fp_cmp(self : Fn, idx : Int, wide : Bool) -> Unit {
|
||||
fn Fn::fp_cmp(self : Fn, idx : Int32, wide : Bool) -> Unit {
|
||||
self.fp_load(wide, true)
|
||||
self.pu(wide)
|
||||
// lt/le compare (b, a) so that "above" is false on unordered
|
||||
@@ -649,7 +659,7 @@ fn Fn::fp_cmp(self : Fn, idx : Int, wide : Bool) -> Unit {
|
||||
|
||||
///|
|
||||
/// op: abs, neg, ceil, floor, trunc, nearest, sqrt.
|
||||
fn Fn::fp_unop(self : Fn, op : Int, wide : Bool) -> Unit {
|
||||
fn Fn::fp_unop(self : Fn, op : Int32, wide : Bool) -> Unit {
|
||||
match op {
|
||||
0 | 1 => {
|
||||
self.pop(0)
|
||||
@@ -675,7 +685,18 @@ fn Fn::fp_unop(self : Fn, op : Int, wide : Bool) -> Unit {
|
||||
4 => 0x0B
|
||||
_ => 0x08
|
||||
}
|
||||
self.code.bs([0x66, 0x0F, 0x3A, if wide { 0x0B } else { 0x0A }, 0xC0, mode])
|
||||
self.code.bs([
|
||||
0x66,
|
||||
0x0F,
|
||||
0x3A,
|
||||
if wide {
|
||||
0x0B
|
||||
} else {
|
||||
0x0A
|
||||
},
|
||||
0xC0,
|
||||
mode,
|
||||
])
|
||||
self.fp_store(wide)
|
||||
}
|
||||
}
|
||||
@@ -683,13 +704,15 @@ fn Fn::fp_unop(self : Fn, op : Int, wide : Bool) -> Unit {
|
||||
|
||||
///|
|
||||
/// op: add, sub, mul, div, min, max, copysign.
|
||||
fn Fn::fp_binop(self : Fn, op : Int, wide : Bool) -> Unit {
|
||||
fn Fn::fp_binop(self : Fn, op : Int32, 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])
|
||||
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])
|
||||
@@ -750,7 +773,9 @@ fn Fn::fp_convert(
|
||||
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])
|
||||
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)
|
||||
@@ -768,14 +793,19 @@ fn Fn::fp_resize(self : Fn, src_wide : Bool, dst_wide : Bool) -> Unit {
|
||||
|
||||
///|
|
||||
/// Trapping float -> int truncation (NaN and out-of-range trap).
|
||||
fn Fn::fp_trunc(self : Fn, src_wide : Bool, dst64 : Bool, signed : Bool) -> Unit {
|
||||
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) {
|
||||
let load_const = fn(hi_f64 : Int32, f32_bits : Int32) {
|
||||
if src_wide {
|
||||
self.code.bs([0x48, 0xB9]) // mov rcx, imm64
|
||||
self.code.i32(0)
|
||||
@@ -831,3 +861,59 @@ fn Fn::fp_trunc(self : Fn, src_wide : Bool, dst64 : Bool, signed : Bool) -> Unit
|
||||
}
|
||||
self.push_rax()
|
||||
}
|
||||
|
||||
///|
|
||||
/// Bulk memory ops: memory.copy (0xFC 0x0A, overlap-safe) and memory.fill (0xFC 0x0B),
|
||||
/// with bounds checks, via rep movsb / rep stosb.
|
||||
fn Fn::bulk_mem(self : Fn, sub : Int32, r : Reader) -> Unit raise CompileError {
|
||||
if self.mem_bytes == 0 {
|
||||
raise CompileError("memory access but the module defines no memory")
|
||||
}
|
||||
match sub {
|
||||
0x0A => {
|
||||
ignore(r.byte())
|
||||
ignore(r.byte())
|
||||
self.pop(1) // rcx = n
|
||||
self.pop(6) // rsi = src
|
||||
self.pop(7) // rdi = dst
|
||||
self.code.bs([0x89, 0xC9, 0x89, 0xF6, 0x89, 0xFF]) // zero-extend ecx,esi,edi
|
||||
self.code.bs([0x48, 0x8D, 0x04, 0x0E]) // lea rax,[rsi+rcx]
|
||||
self.code.bs([0x48, 0x3D])
|
||||
self.code.i32(self.mem_bytes)
|
||||
self.trap_jmp(TRAP_OOB, [0x0F, 0x87])
|
||||
self.code.bs([0x48, 0x8D, 0x04, 0x0F]) // lea rax,[rdi+rcx]
|
||||
self.code.bs([0x48, 0x3D])
|
||||
self.code.i32(self.mem_bytes)
|
||||
self.trap_jmp(TRAP_OOB, [0x0F, 0x87])
|
||||
self.code.bs([0x48, 0x81, 0xC6])
|
||||
self.code.i32(MEM_BASE)
|
||||
self.code.bs([0x48, 0x81, 0xC7])
|
||||
self.code.i32(MEM_BASE)
|
||||
self.code.bs([0x48, 0x39, 0xF7]) // cmp rdi,rsi
|
||||
let fwd = self.code.rel8([0x76]) // jbe forward
|
||||
self.code.bs([0xFD]) // std
|
||||
self.code.bs([0x48, 0x8D, 0x74, 0x0E, 0xFF]) // lea rsi,[rsi+rcx-1]
|
||||
self.code.bs([0x48, 0x8D, 0x7C, 0x0F, 0xFF]) // lea rdi,[rdi+rcx-1]
|
||||
self.code.bs([0xF3, 0xA4, 0xFC]) // rep movsb; cld
|
||||
let done = self.code.rel8([0xEB])
|
||||
self.code.patch_here8(fwd)
|
||||
self.code.bs([0xF3, 0xA4])
|
||||
self.code.patch_here8(done)
|
||||
}
|
||||
0x0B => {
|
||||
ignore(r.byte())
|
||||
self.pop(1) // rcx = n
|
||||
self.pop(0) // rax = value
|
||||
self.pop(7) // rdi = dst
|
||||
self.code.bs([0x89, 0xC9, 0x89, 0xFF])
|
||||
self.code.bs([0x48, 0x8D, 0x14, 0x0F]) // lea rdx,[rdi+rcx]
|
||||
self.code.bs([0x48, 0x81, 0xFA])
|
||||
self.code.i32(self.mem_bytes)
|
||||
self.trap_jmp(TRAP_OOB, [0x0F, 0x87])
|
||||
self.code.bs([0x48, 0x81, 0xC7])
|
||||
self.code.i32(MEM_BASE)
|
||||
self.code.bs([0xF3, 0xAA])
|
||||
}
|
||||
_ => raise CompileError("unsupported 0xFC opcode \{sub}")
|
||||
}
|
||||
}
|
||||
+33
-3
@@ -1,8 +1,38 @@
|
||||
;; recursive factorial(5) = 120, plus if/else with result
|
||||
;; wasi:cli command: computes factorial(5) recursively and prints "fact(5) = 120".
|
||||
;; Build with: just run-component fact
|
||||
(module
|
||||
(import "wasi:cli/stdout@0.2.0" "get-stdout" (func $get_stdout (result i32)))
|
||||
(import "wasi:io/streams@0.2.0" "[method]output-stream.blocking-write-and-flush"
|
||||
(func $write (param i32 i32 i32 i32)))
|
||||
(memory (export "memory") 1)
|
||||
(data (i32.const 16) "fact(5) = ")
|
||||
|
||||
(func $fact (param $n i32) (result i32)
|
||||
(if (result i32) (i32.le_s (local.get $n) (i32.const 1))
|
||||
(then (i32.const 1))
|
||||
(else (i32.mul (local.get $n) (call $fact (i32.sub (local.get $n) (i32.const 1)))))))
|
||||
(func (export "main") (result i32)
|
||||
(call $fact (i32.const 5))))
|
||||
|
||||
(func (export "wasi:cli/run@0.2.0#run") (result i32)
|
||||
(local $v i32) (local $p i32)
|
||||
(local.set $v (call $fact (i32.const 5)))
|
||||
;; write decimal digits backwards, ending just before a trailing newline at 63
|
||||
(local.set $p (i32.const 63))
|
||||
(i32.store8 (i32.const 63) (i32.const 10))
|
||||
(loop $digits
|
||||
(local.set $p (i32.sub (local.get $p) (i32.const 1)))
|
||||
(i32.store8 (local.get $p)
|
||||
(i32.add (i32.const 48) (i32.rem_u (local.get $v) (i32.const 10))))
|
||||
(local.set $v (i32.div_u (local.get $v) (i32.const 10)))
|
||||
(br_if $digits (local.get $v)))
|
||||
;; prefix "fact(5) = " (10 bytes at 16..26); copy digits right after it
|
||||
(local.set $v (i32.const 26))
|
||||
(block $done
|
||||
(loop $copy
|
||||
(br_if $done (i32.gt_u (local.get $p) (i32.const 63)))
|
||||
(i32.store8 (local.get $v) (i32.load8_u (local.get $p)))
|
||||
(local.set $v (i32.add (local.get $v) (i32.const 1)))
|
||||
(local.set $p (i32.add (local.get $p) (i32.const 1)))
|
||||
(br $copy)))
|
||||
(call $write (call $get_stdout) (i32.const 16)
|
||||
(i32.sub (local.get $v) (i32.const 16)) (i32.const 128))
|
||||
(i32.load8_u (i32.const 128))))
|
||||
@@ -3,3 +3,7 @@ package local:examples;
|
||||
world hello {
|
||||
include wasi:cli/command@0.2.0;
|
||||
}
|
||||
|
||||
world fact {
|
||||
include wasi:cli/command@0.2.0;
|
||||
}
|
||||
+46
-1
@@ -72,6 +72,46 @@ fn import_stub(m : String, name : String) -> Asm? {
|
||||
s.i32(MEM_BASE)
|
||||
s.b(0)
|
||||
}
|
||||
// --- raw host intrinsics for hand-written wasm shims (module "sys") ---
|
||||
"sys::syscall" => {
|
||||
// (nr, a1..a6 : i64) -> i64
|
||||
s.bs([0x48, 0x8B, 0x45, 0x40]) // mov rax,[rbp+64]
|
||||
s.bs([0x48, 0x8B, 0x7D, 0x38]) // mov rdi,[rbp+56]
|
||||
s.bs([0x48, 0x8B, 0x75, 0x30]) // mov rsi,[rbp+48]
|
||||
s.bs([0x48, 0x8B, 0x55, 0x28]) // mov rdx,[rbp+40]
|
||||
s.bs([0x4C, 0x8B, 0x55, 0x20]) // mov r10,[rbp+32]
|
||||
s.bs([0x4C, 0x8B, 0x45, 0x18]) // mov r8,[rbp+24]
|
||||
s.bs([0x4C, 0x8B, 0x4D, 0x10]) // mov r9,[rbp+16]
|
||||
s.bs([0x0F, 0x05])
|
||||
}
|
||||
"sys::membase" => {
|
||||
s.b(0xB8)
|
||||
s.i32(MEM_BASE)
|
||||
}
|
||||
"sys::argv_base" => {
|
||||
// initial rsp saved by _start (points at argc)
|
||||
s.bs([0x48, 0x8B, 0x04, 0x25])
|
||||
s.i32(GLOB_BASE + 0xFF8)
|
||||
}
|
||||
"sys::peek64" =>
|
||||
// (host addr : i64) -> i64
|
||||
s.bs([0x48, 0x8B, 0x45, 0x10, 0x48, 0x8B, 0x00])
|
||||
"sys::strlen" => {
|
||||
// (host addr : i64) -> i64
|
||||
s.bs([
|
||||
0x48, 0x8B, 0x7D, 0x10, 0x31, 0xC0, 0x48, 0xC7, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
])
|
||||
s.bs([0xF2, 0xAE, 0x48, 0xF7, 0xD1, 0x48, 0xFF, 0xC9, 0x48, 0x89, 0xC8])
|
||||
}
|
||||
"sys::copy_in" => {
|
||||
// (dst guest i32, src host i64, n i32) -> ()
|
||||
s.bs([0x8B, 0x7D, 0x20]) // mov edi,[rbp+32]
|
||||
s.bs([0x48, 0x81, 0xC7])
|
||||
s.i32(MEM_BASE)
|
||||
s.bs([0x48, 0x8B, 0x75, 0x18]) // mov rsi,[rbp+24]
|
||||
s.bs([0x8B, 0x4D, 0x10]) // mov ecx,[rbp+16]
|
||||
s.bs([0xF3, 0xA4])
|
||||
}
|
||||
"wasi:cli/exit::exit" =>
|
||||
// (status: result) -> () ; 0 = ok, 1 = err
|
||||
s.bs([0x8B, 0x7D, 0x10, 0xB8, 0xE7, 0x00, 0x00, 0x00, 0x0F, 0x05])
|
||||
@@ -216,7 +256,8 @@ fn compile_core(wasm : Bytes, entry : String?) -> Bytes raise CompileError {
|
||||
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")
|
||||
_ =>
|
||||
raise CompileError("global initializers must be a numeric const")
|
||||
}
|
||||
if r.byte() != 0x0B {
|
||||
raise CompileError("bad global initializer")
|
||||
@@ -335,6 +376,10 @@ fn compile_core(wasm : Bytes, entry : String?) -> Bytes raise CompileError {
|
||||
let phnum = if has_data { 2 } else { 1 }
|
||||
let headers = 64 + 56 * phnum
|
||||
let a = Asm::new()
|
||||
if has_data {
|
||||
a.bs([0x48, 0x89, 0x24, 0x25]) // mov [GLOB_BASE+0xFF8], rsp (argc/argv)
|
||||
a.i32(GLOB_BASE + 0xFF8)
|
||||
}
|
||||
let start_call = a.rel([0xE8])
|
||||
a.bs([0x89, 0xC7]) // mov edi,eax
|
||||
if types[func_types[entry_idx]].nresults == 0 {
|
||||
|
||||
Reference in new issue
Block a user