Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block can take inputs #57

Merged
merged 15 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading