Skip to content

Commit

Permalink
Init MiniWasmFX (#64)
Browse files Browse the repository at this point in the history
* check in MiniWasmFX

* Basic `(module bin ...)` support (#62)


* basic .bin.wast working

---------

Co-authored-by: butterunderflow <[email protected]>
Co-authored-by: Guannan Wei <[email protected]>

* refactor value hierarchy

* remove duplication

* prelim impl of RefFunc and ContNew

* callref

* refactor to 2cps style

* add handler

* resumable try-catch

* fix for deep handler

* mechanize test

* add test case

* missing file

* rename vars in resume, added some comments

* comment tests for now

---------

Co-authored-by: ahuoguo <[email protected]>
Co-authored-by: butterunderflow <[email protected]>
Co-authored-by: ahuoguo <[email protected]>
  • Loading branch information
4 people authored Nov 18, 2024
1 parent 2eb36f0 commit 9eaad3d
Show file tree
Hide file tree
Showing 24 changed files with 3,597 additions and 2,643 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/scala.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@ jobs:
sbt 'testOnly gensym.TestLibrary'
sbt 'testOnly gensym.wasm.TestEval'
sbt 'testOnly gensym.wasm.TestScriptRun'
sbt 'testOnly gensym.wasm.TestFx'
30 changes: 30 additions & 0 deletions benchmarks/wasm/trycatch/deep.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
(module
(type (;0;) (func (param i32)))
(type (;1;) (func))
(import "console" "log" (func (;0;) (type 0)))
(func (;1;) (type 1)
(local i32)
try ;; 1 2 3 2 4 4 5
i32.const 1
call 0
i32.const 42
;; [42]
throw
i32.const 3
call 0
i32.const -1
;; [-1]
throw
catch
;; [42, resume]
i32.const 2
call 0
drop
resume0
i32.const 4
call 0
end
i32.const 5
call 0
)
(start 1))
31 changes: 31 additions & 0 deletions benchmarks/wasm/trycatch/multishot.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(module
(type (;0;) (func (param i32)))
(type (;1;) (func))
(import "console" "log" (func (;0;) (type 0)))
(func (;1;) (type 1)
(local i32)
try
i32.const 1
call 0
i32.const 42
;; [42]
throw
i32.const 3
call 0
catch
;; [42, resume]
i32.const 2
call 0
drop
local.set 0 ;; now we are really abusing the type system ...
local.get 0
resume0
i32.const 4
call 0
local.get 0
resume0
end
i32.const 5
call 0
)
(start 1))
43 changes: 43 additions & 0 deletions benchmarks/wasm/trycatch/nested_try_catch.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
(module
(type (;0;) (func (param i32)))
(type (;1;) (func))
(import "console" "log" (func (;0;) (type 0)))
(func (;1;) (type 1)
(local i32)
try
i32.const 1
call 0
try
i32.const 42
;; [42]
throw
i32.const 3
call 0
catch
;; [42, resume]
i32.const 2
call 0
drop ;; [resume]
resume0
i32.const 4
call 0
end
i32.const 5
call 0
i32.const -1
throw
i32.const 7
call 0
catch
;; [-1, resume]
drop
i32.const 6
call 0
resume0
i32.const 8
call 0
end
i32.const 9
call 0
)
(start 1))
27 changes: 27 additions & 0 deletions benchmarks/wasm/trycatch/try_catch.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(module
(type (;0;) (func (param i32)))
(type (;1;) (func))
(import "console" "log" (func (;0;) (type 0)))
(func (;1;) (type 1)
(local i32)
try
i32.const 1
call 0
i32.const 42
;; [42]
throw
i32.const 3
call 0
catch
;; [42, resume]
i32.const 2
call 0
drop
resume0
i32.const 4
call 0
end
i32.const 5
call 0
)
(start 1))
31 changes: 31 additions & 0 deletions benchmarks/wasm/trycatch/try_catch_block.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(module
(type (;0;) (func (param i32)))
(type (;1;) (func))
(import "console" "log" (func (;0;) (type 0)))
(func (;1;) (type 1)
(local i32)
try
i32.const 1
call 0
block
block
i32.const 42
;; [42]
throw
end
end
i32.const 3
call 0
catch
;; [42, resume]
i32.const 2
call 0
drop
resume0
i32.const 4
call 0
end
i32.const 5
call 0
)
(start 1))
32 changes: 32 additions & 0 deletions benchmarks/wasm/trycatch/try_catch_br.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(module
(type (;0;) (func (param i32)))
(type (;1;) (func))
(import "console" "log" (func (;0;) (type 0)))
(func (;1;) (type 1)
(local i32)
block (result i32)
try
i32.const 1
call 0
i32.const 42
;; [42]
throw
i32.const 3
call 0
catch
;; [42, resume]
i32.const 2
call 0
;;drop
br 0
resume0
i32.const 4
call 0
end
i32.const 5
call 0
end
i32.const 6
call 0
)
(start 1))
32 changes: 32 additions & 0 deletions benchmarks/wasm/trycatch/try_catch_br2.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(module
(type (;0;) (func (param i32)))
(type (;1;) (func))
(import "console" "log" (func (;0;) (type 0)))
(func (;1;) (type 1)
(local i32)
try
i32.const 1
call 0
block
i32.const 42
;; [42]
throw
br 0
i32.const 3
call 0
end
i32.const 6
call 0
catch
;; [42, resume]
i32.const 2
call 0
drop
resume0
i32.const 4
call 0
end
i32.const 5
call 0
)
(start 1))
25 changes: 25 additions & 0 deletions benchmarks/wasm/trycatch/try_catch_discard.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(module
(type (;0;) (func (param i32)))
(type (;1;) (func))
(import "console" "log" (func (;0;) (type 0)))
(func (;1;) (type 1)
(local i32)
try
i32.const 1
call 0
i32.const 42
;; [42]
throw
i32.const 3
call 0
catch
;; [42, resume]
call 0
drop
i32.const 4
call 0
end
i32.const 5
call 0
)
(start 1))
24 changes: 24 additions & 0 deletions benchmarks/wasm/trycatch/try_catch_succ.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(module
(type (;0;) (func (param i32)))
(type (;1;) (func))
(import "console" "log" (func (;0;) (type 0)))
(func (;1;) (type 1)
(local i32)
try
i32.const 1
call 0
i32.const 3
call 0
catch
;; [42, resume]
i32.const 2
call 0
drop
resume0
i32.const 4
call 0
end
i32.const 5
call 0
)
(start 1))
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
Shorter tests: https://github.com/titzer/wizard-engine/tree/master/test/regress/ext:stack-switching

Wasm reference interpreter tests: https://github.com/WebAssembly/stack-switching/blob/wasmfx/test/core/stack-switching/cont.wast

Stack Switching proposal examples: https://github.com/WebAssembly/stack-switching/tree/main/proposals/stack-switching/examples


58 changes: 58 additions & 0 deletions benchmarks/wasm/wasmfx/callref-strip.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
(module
(type (;0;) (func (param i32)))
(type (;1;) (func (param i32 i32)))
(type (;2;) (func (param (ref 0) (ref 0))))
(type (;3;) (func))
(import "spectest" "print_i32" (func (;0;) (type 0)))
(export "_start" (func 6))
(start 6)
(elem (;0;) declare func 4)
(elem (;1;) declare func 5)
(func (;1;) (type 0) (param i32)
local.get 0
call 0
)
(func (;2;) (type 1) (param i32 i32)
(local i32)
local.get 0
local.set 2
block ;; label = @1
loop ;; label = @2
local.get 2
local.get 1
i32.gt_u
br_if 1 (;@1;)
local.get 2
call 1
local.get 2
i32.const 1
i32.add
local.set 2
br 0 (;@2;)
end
end
)
(func (;3;) (type 2) (param (ref 0) (ref 0))
i32.const 10
local.get 0
call_ref 0
i32.const 20
local.get 1
call_ref 0
)
(func (;4;) (type 0) (param i32)
local.get 0
i32.const 13
call 2
)
(func (;5;) (type 0) (param i32)
local.get 0
i32.const 23
call 2
)
(func (;6;) (type 3)
ref.func 4
ref.func 5
call 3
)
)
13 changes: 13 additions & 0 deletions benchmarks/wasm/wasmfx/cont_bind3.bin.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(module binary
"\00\61\73\6d\01\00\00\00\01\91\80\80\80\00\05\60"
"\00\00\5d\00\60\01\7f\00\5d\02\60\01\7f\01\7f\03"
"\83\80\80\80\00\02\02\04\06\86\80\80\80\00\01\7f"
"\01\41\0b\0b\07\88\80\80\80\00\01\04\6d\61\69\6e"
"\00\01\09\85\80\80\80\00\01\03\00\01\00\0a\a1\80"
"\80\80\00\02\86\80\80\80\00\00\20\00\24\00\0b\90"
"\80\80\80\00\00\20\00\d2\00\e0\03\e1\03\01\e3\01"
"\00\23\00\0b"
)
;; (module instance)
(assert_return (invoke "main" (i32.const 0x16)) (i32.const 0x16))
(assert_return (invoke "main" (i32.const 0xffff_fe44)) (i32.const 0xffff_fe44))
8 changes: 8 additions & 0 deletions grammar/WatLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,19 @@ CALL_INDIRECT: 'call_indirect' ;
RETURN_CALL: 'return_call' ;
RETURN_CALL_INDIRECT: 'return_call_indirect' ;
REFFUNC: 'ref.func' ;
CALLREF: 'call_ref' ;
RESUME: 'resume' ;
ON: 'on' ;
CONTNEW: 'cont.new' ;
CONTBIND: 'cont.bind' ;
SUSPEND: 'suspend' ;

// resumable try-catch extension:
TRY: 'try' ;
CATCH: 'catch' ;
THROW: 'throw' ;
RESUME0: 'resume0' ;

LOCAL_GET: 'local.get' ;
LOCAL_SET: 'local.set' ;
LOCAL_TEE: 'local.tee' ;
Expand Down
Loading

0 comments on commit 9eaad3d

Please sign in to comment.