Skip to content

Commit a819499

Browse files
committed
support for loop
Co-authored-by: Jiaaaaatai <[email protected]
1 parent 080483d commit a819499

File tree

13 files changed

+3256
-3038
lines changed

13 files changed

+3256
-3038
lines changed

benchmarks/wasm/for_loop.wat

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
(module
2+
(func $for_loop (result i32)
3+
(local i32)
4+
(local i32)
5+
6+
for
7+
(
8+
;; init
9+
i32.const 0
10+
local.set 0
11+
i32.const 0
12+
local.set 1
13+
|
14+
;; cond
15+
local.get 1
16+
i32.const 10
17+
i32.gt_s
18+
i32.eqz
19+
|
20+
;; post
21+
local.get 1
22+
i32.const 1
23+
i32.add
24+
local.set 1
25+
)
26+
27+
;; es
28+
local.get 0
29+
local.get 1
30+
i32.add
31+
local.set 0
32+
33+
local.get 0
34+
35+
36+
)
37+
38+
(export "for_loop" (func 0))
39+
40+
)

grammar/WatLexer.g4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ UNREACHABLE: 'unreachable' ;
6060
DROP: 'drop' ;
6161
BLOCK: 'block' ;
6262
LOOP: 'loop' ;
63+
FOR : 'for';
64+
VBAR: '|';
6365
END: 'end' ;
6466
BR: 'br' ;
6567
BR_IF: 'br_if' ;

grammar/WatParser.g4

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ instr
130130
| blockInstr
131131
| foldedInstr
132132
| resumeInstr
133+
| forLoop
134+
;
135+
136+
forLoop
137+
: 'for' '(' instrList '|' instrList '|' instrList ')' instrList
133138
;
134139

135140
plainInstr

src/main/java/wasm/WatLexer.java

Lines changed: 1456 additions & 1437 deletions
Large diffs are not rendered by default.

src/main/java/wasm/WatParser.java

Lines changed: 1689 additions & 1601 deletions
Large diffs are not rendered by default.

src/main/java/wasm/WatParserBaseListener.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,18 @@ public class WatParserBaseListener implements WatParserListener {
240240
* <p>The default implementation does nothing.</p>
241241
*/
242242
@Override public void exitInstr(WatParser.InstrContext ctx) { }
243+
/**
244+
* {@inheritDoc}
245+
*
246+
* <p>The default implementation does nothing.</p>
247+
*/
248+
@Override public void enterForLoop(WatParser.ForLoopContext ctx) { }
249+
/**
250+
* {@inheritDoc}
251+
*
252+
* <p>The default implementation does nothing.</p>
253+
*/
254+
@Override public void exitForLoop(WatParser.ForLoopContext ctx) { }
243255
/**
244256
* {@inheritDoc}
245257
*

src/main/java/wasm/WatParserBaseVisitor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ public class WatParserBaseVisitor<T> extends AbstractParseTreeVisitor<T> impleme
145145
* {@link #visitChildren} on {@code ctx}.</p>
146146
*/
147147
@Override public T visitInstr(WatParser.InstrContext ctx) { return visitChildren(ctx); }
148+
/**
149+
* {@inheritDoc}
150+
*
151+
* <p>The default implementation returns the result of calling
152+
* {@link #visitChildren} on {@code ctx}.</p>
153+
*/
154+
@Override public T visitForLoop(WatParser.ForLoopContext ctx) { return visitChildren(ctx); }
148155
/**
149156
* {@inheritDoc}
150157
*

src/main/java/wasm/WatParserListener.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ public interface WatParserListener extends ParseTreeListener {
197197
* @param ctx the parse tree
198198
*/
199199
void exitInstr(WatParser.InstrContext ctx);
200+
/**
201+
* Enter a parse tree produced by {@link WatParser#forLoop}.
202+
* @param ctx the parse tree
203+
*/
204+
void enterForLoop(WatParser.ForLoopContext ctx);
205+
/**
206+
* Exit a parse tree produced by {@link WatParser#forLoop}.
207+
* @param ctx the parse tree
208+
*/
209+
void exitForLoop(WatParser.ForLoopContext ctx);
200210
/**
201211
* Enter a parse tree produced by {@link WatParser#plainInstr}.
202212
* @param ctx the parse tree

src/main/java/wasm/WatParserVisitor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ public interface WatParserVisitor<T> extends ParseTreeVisitor<T> {
124124
* @return the visitor result
125125
*/
126126
T visitInstr(WatParser.InstrContext ctx);
127+
/**
128+
* Visit a parse tree produced by {@link WatParser#forLoop}.
129+
* @param ctx the parse tree
130+
* @return the visitor result
131+
*/
132+
T visitForLoop(WatParser.ForLoopContext ctx);
127133
/**
128134
* Visit a parse tree produced by {@link WatParser#plainInstr}.
129135
* @param ctx the parse tree

src/main/scala/wasm/AST.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ case class Select(ty: Option[List[ValueType]]) extends Instr
6565
case class Block(ty: BlockType, instrs: List[Instr]) extends Instr
6666
case class IdBlock(id: Int, ty: BlockType, instrs: List[Instr]) extends Instr
6767
case class Loop(ty: BlockType, instrs: List[Instr]) extends Instr
68+
case class ForLoop(init:List[Instr], cond: List[Instr], post: List[Instr], body: List[Instr]) extends Instr
6869
case class IdLoop(id: Int, ty: BlockType, instrs: List[Instr]) extends Instr
6970
case class If(ty: BlockType, thenInstrs: List[Instr], elseInstrs: List[Instr]) extends Instr
7071
case class IdIf(ty: BlockType, thenInstrs: IdBlock, elseInstrs: IdBlock) extends Instr

0 commit comments

Comments
 (0)