Skip to content

Commit

Permalink
Block can take inputs (#57)
Browse files Browse the repository at this point in the history
* a case shows that block can have inputs

* now block instructions can accept input

* tweak

* more tests and fix If's evaluation

* resolve some TODO and more consistent naming

* block can only use the values that it declared to use

* fix tidy issues

* truncate redundant values when exiting a block

* a problematic WIP implementation

* leave usage of type use syntax as placeholder

* try loop poly

* still enable type use syntax, only ignore their index

* tweak test case to align new exporting requirement

* update test case

* remove some line wrappings

---------

Co-authored-by: ahuoguo <[email protected]>
Co-authored-by: Guannan Wei <[email protected]>
  • Loading branch information
3 people authored Oct 18, 2024
1 parent f295830 commit d13e106
Show file tree
Hide file tree
Showing 9 changed files with 1,213 additions and 1,040 deletions.
70 changes: 70 additions & 0 deletions benchmarks/wasm/block.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
(module
(func $test_block (param i32 i32 i32) (result i32)
local.get 0
local.get 1
local.get 2
block (param i32 i32 i32) (result i32 i32)
i32.add
end
i32.add
)
(func $real_main (result i32)
i32.const 1
i32.const 3
i32.const 5
call $test_block
)
;; Sum from [0, 10]
(func $test_loop_input (result i32)
(local i32)
i32.const 10
local.set 0
i32.const 0
loop (param i32) (result i32)
local.get 0
i32.add
local.get 0
i32.const 1
i32.sub
local.set 0
i32.const 0
local.get 0
i32.ne
br_if 0
end
)
(func $test_if_input (result i32)
i32.const 10
i32.const 5
i32.const 1
if (param i32 i32) (result i32 i32)
i32.const 10
i32.add
else
end
i32.add
)
(func $test_poly_br (result i32)
i32.const -30
i32.const 0 ;; unused
i32.const 0 ;; unused
i32.const 0 ;; unused
block (param i32 i32 i32) (result i32 i32)
i32.const 0 ;; truncated
i32.const 10000 ;; truncated
i32.const 10
i32.const 20
br 0
i32.add
end
i32.add
i32.add ;; add value -30 and 30
;; i32.add
;; We can't use i32.add instruction here, because the overflowed value has been truncted
;; when block exited.
)
(export "real_main" (func 1))
(export "test_loop_input" (func 2))
(export "test_if_input" (func 3))
(export "test_poly_br" (func 4))
)
20 changes: 20 additions & 0 deletions benchmarks/wasm/loop_poly.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(module
(type (;0;) (func))
(type (;1;) (func (param i32) (result i32)))
(func $test_poly_loop (;0;) (type 0)
i32.const 42
i32.const 0
block (param i32 i32) (result i32 i32)
loop (type 1) (param i32) (result i32) ;; label = @1
i32.const 1
i32.const 2
br 1 (;@1;)
end
end
;; this is not a valid wasm program due to the mismatch of stack shape,
;; we only do this for testing purposes.
;; i32.add
;; drop
)
(start 0)
)
8 changes: 5 additions & 3 deletions grammar/WatParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,13 @@ blockInstr
;

blockType
: LPAR RESULT valType RPAR
: (LPAR RESULT valType RPAR)?
| typeUse funcType
| funcType // abbreviation
;

block
: blockType? instrList
: blockType instrList
;

foldedInstr
Expand All @@ -221,7 +223,7 @@ expr
| BLOCK bindVar? block
| LOOP bindVar? block
// | IF bindVar? ifBlock
| IF bindVar? blockType? foldedInstr* LPAR THEN instrList (LPAR ELSE instrList RPAR)?
| IF bindVar? blockType foldedInstr* LPAR THEN instrList (LPAR ELSE instrList RPAR)?
;

callExprType
Expand Down
Loading

0 comments on commit d13e106

Please sign in to comment.