Skip to content

Commit d13e106

Browse files
butterunderflowahuoguoGuannan Wei
authored
Block can take inputs (#57)
* 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 <ahuoguo@gmail.com> Co-authored-by: Guannan Wei <wei220@purdue.edu>
1 parent f295830 commit d13e106

File tree

9 files changed

+1213
-1040
lines changed

9 files changed

+1213
-1040
lines changed

benchmarks/wasm/block.wat

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
(module
2+
(func $test_block (param i32 i32 i32) (result i32)
3+
local.get 0
4+
local.get 1
5+
local.get 2
6+
block (param i32 i32 i32) (result i32 i32)
7+
i32.add
8+
end
9+
i32.add
10+
)
11+
(func $real_main (result i32)
12+
i32.const 1
13+
i32.const 3
14+
i32.const 5
15+
call $test_block
16+
)
17+
;; Sum from [0, 10]
18+
(func $test_loop_input (result i32)
19+
(local i32)
20+
i32.const 10
21+
local.set 0
22+
i32.const 0
23+
loop (param i32) (result i32)
24+
local.get 0
25+
i32.add
26+
local.get 0
27+
i32.const 1
28+
i32.sub
29+
local.set 0
30+
i32.const 0
31+
local.get 0
32+
i32.ne
33+
br_if 0
34+
end
35+
)
36+
(func $test_if_input (result i32)
37+
i32.const 10
38+
i32.const 5
39+
i32.const 1
40+
if (param i32 i32) (result i32 i32)
41+
i32.const 10
42+
i32.add
43+
else
44+
end
45+
i32.add
46+
)
47+
(func $test_poly_br (result i32)
48+
i32.const -30
49+
i32.const 0 ;; unused
50+
i32.const 0 ;; unused
51+
i32.const 0 ;; unused
52+
block (param i32 i32 i32) (result i32 i32)
53+
i32.const 0 ;; truncated
54+
i32.const 10000 ;; truncated
55+
i32.const 10
56+
i32.const 20
57+
br 0
58+
i32.add
59+
end
60+
i32.add
61+
i32.add ;; add value -30 and 30
62+
;; i32.add
63+
;; We can't use i32.add instruction here, because the overflowed value has been truncted
64+
;; when block exited.
65+
)
66+
(export "real_main" (func 1))
67+
(export "test_loop_input" (func 2))
68+
(export "test_if_input" (func 3))
69+
(export "test_poly_br" (func 4))
70+
)

benchmarks/wasm/loop_poly.wat

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(module
2+
(type (;0;) (func))
3+
(type (;1;) (func (param i32) (result i32)))
4+
(func $test_poly_loop (;0;) (type 0)
5+
i32.const 42
6+
i32.const 0
7+
block (param i32 i32) (result i32 i32)
8+
loop (type 1) (param i32) (result i32) ;; label = @1
9+
i32.const 1
10+
i32.const 2
11+
br 1 (;@1;)
12+
end
13+
end
14+
;; this is not a valid wasm program due to the mismatch of stack shape,
15+
;; we only do this for testing purposes.
16+
;; i32.add
17+
;; drop
18+
)
19+
(start 0)
20+
)

grammar/WatParser.g4

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,13 @@ blockInstr
204204
;
205205

206206
blockType
207-
: LPAR RESULT valType RPAR
207+
: (LPAR RESULT valType RPAR)?
208+
| typeUse funcType
209+
| funcType // abbreviation
208210
;
209211

210212
block
211-
: blockType? instrList
213+
: blockType instrList
212214
;
213215

214216
foldedInstr
@@ -221,7 +223,7 @@ expr
221223
| BLOCK bindVar? block
222224
| LOOP bindVar? block
223225
// | IF bindVar? ifBlock
224-
| IF bindVar? blockType? foldedInstr* LPAR THEN instrList (LPAR ELSE instrList RPAR)?
226+
| IF bindVar? blockType foldedInstr* LPAR THEN instrList (LPAR ELSE instrList RPAR)?
225227
;
226228

227229
callExprType

0 commit comments

Comments
 (0)