Skip to content

Commit b3a245b

Browse files
committed
wasm gc: fix case when non-split block breaks from split block
1 parent 9a85f02 commit b3a245b

15 files changed

Lines changed: 1020 additions & 521 deletions

File tree

core/src/main/java/org/teavm/ast/Expr.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ public static Expr binary(BinaryOperation op, OperationType type, Expr first, Ex
6363
return expr;
6464
}
6565

66+
public static Expr and(Expr first, Expr second) {
67+
return binary(BinaryOperation.AND, null, first, second);
68+
}
69+
70+
public static Expr or(Expr first, Expr second) {
71+
return binary(BinaryOperation.OR, null, first, second);
72+
}
73+
74+
public static Expr addInt(Expr first, Expr second) {
75+
return binary(BinaryOperation.ADD, OperationType.INT, first, second);
76+
}
77+
78+
public static Expr divInt(Expr first, Expr second) {
79+
return binary(BinaryOperation.DIVIDE, OperationType.INT, first, second);
80+
}
81+
82+
public static Expr less(Expr first, Expr second) {
83+
return binary(BinaryOperation.LESS, OperationType.INT, first, second);
84+
}
85+
6686
public static Expr binary(BinaryOperation op, OperationType type, Expr first, Expr second, TextLocation loc) {
6787
Expr expr = binary(op, type, first, second);
6888
expr.setLocation(loc);
@@ -146,7 +166,7 @@ public static InvocationExpr invokeSpecial(MethodReference method, Expr target,
146166
return expr;
147167
}
148168

149-
public static InvocationExpr invokeStatic(MethodReference method, Expr[] arguments) {
169+
public static InvocationExpr invokeStatic(MethodReference method, Expr... arguments) {
150170
InvocationExpr expr = new InvocationExpr();
151171
expr.setMethod(method);
152172
expr.setType(InvocationType.STATIC);

core/src/main/java/org/teavm/ast/Statement.java

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
*/
1616
package org.teavm.ast;
1717

18+
import java.util.Arrays;
19+
import java.util.Collection;
1820
import java.util.Collections;
1921
import java.util.List;
22+
import java.util.function.Function;
2023

2124
public abstract class Statement {
2225
public abstract void acceptVisitor(StatementVisitor visitor);
@@ -26,26 +29,61 @@ public static Statement empty() {
2629
}
2730

2831
public static AssignmentStatement assign(Expr left, Expr right) {
29-
AssignmentStatement stmt = new AssignmentStatement();
32+
var stmt = new AssignmentStatement();
3033
stmt.setLeftValue(left);
3134
stmt.setRightValue(right);
3235
return stmt;
3336
}
3437

38+
public static AssignmentStatement statementExpr(Expr expr) {
39+
var stmt = new AssignmentStatement();
40+
stmt.setRightValue(expr);
41+
return stmt;
42+
}
43+
44+
public static ReturnStatement exitFunction() {
45+
return exitFunction(null);
46+
}
47+
3548
public static ReturnStatement exitFunction(Expr result) {
36-
ReturnStatement stmt = new ReturnStatement();
49+
var stmt = new ReturnStatement();
3750
stmt.setResult(result);
3851
return stmt;
3952
}
4053

54+
public static SequentialStatement sequence(Statement... statements) {
55+
var seq = new SequentialStatement();
56+
seq.getSequence().addAll(Arrays.asList(statements));
57+
return seq;
58+
}
59+
60+
public static BlockStatement block(Function<BlockStatement, Collection<Statement>> body) {
61+
var statement = new BlockStatement();
62+
statement.getBody().addAll(body.apply(statement));
63+
return statement;
64+
}
65+
66+
public static BreakStatement exitBlock(IdentifiedStatement identifiedStatement) {
67+
var statement = new BreakStatement();
68+
statement.setTarget(identifiedStatement);
69+
return statement;
70+
}
71+
72+
public static WhileStatement loopWhile(Expr condition, Function<WhileStatement, Collection<Statement>> body) {
73+
var statement = new WhileStatement();
74+
statement.setCondition(condition);
75+
statement.getBody().addAll(body.apply(statement));
76+
return statement;
77+
}
78+
4179
public static ThrowStatement raiseException(Expr exception) {
42-
ThrowStatement stmt = new ThrowStatement();
80+
var stmt = new ThrowStatement();
4381
stmt.setException(exception);
4482
return stmt;
4583
}
4684

4785
public static Statement cond(Expr predicate, List<Statement> consequent, List<Statement> alternative) {
48-
ConditionalStatement statement = new ConditionalStatement();
86+
var statement = new ConditionalStatement();
4987
statement.setCondition(predicate);
5088
statement.getConsequent().addAll(consequent);
5189
statement.getAlternative().addAll(alternative);
@@ -57,8 +95,61 @@ public static Statement cond(Expr predicate, List<Statement> consequent) {
5795
}
5896

5997
public static InitClassStatement initClass(String className) {
60-
InitClassStatement stmt = new InitClassStatement();
98+
var stmt = new InitClassStatement();
6199
stmt.setClassName(className);
62100
return stmt;
63101
}
102+
103+
public static SwitchStatement switchStatement(Expr condition, List<Statement> defaultClause,
104+
SwitchClause... clauses) {
105+
var statement = new SwitchStatement();
106+
statement.setValue(condition);
107+
statement.getDefaultClause().addAll(defaultClause);
108+
statement.getClauses().addAll(List.of(clauses));
109+
return statement;
110+
}
111+
112+
public static SwitchClause switchClause(int[] conditions, Statement... statements) {
113+
var clause = new SwitchClause();
114+
clause.setConditions(conditions);
115+
clause.getBody().addAll(List.of(statements));
116+
return clause;
117+
}
118+
119+
public static SwitchClause switchClause(int condition, Statement... statements) {
120+
return switchClause(new int[] { condition }, statements);
121+
}
122+
123+
public static TryCatchStatementBuilder doTry(Statement... statements) {
124+
return new TryCatchStatementBuilder(statements);
125+
}
126+
127+
public static class TryCatchStatementBuilder {
128+
Statement[] protectedBody;
129+
130+
TryCatchStatementBuilder(Statement[] protectedBody) {
131+
this.protectedBody = protectedBody;
132+
}
133+
134+
public TryCatchStatementBuilder2 doCatch(String type, Integer variable) {
135+
var result = new TryCatchStatement();
136+
result.getProtectedBody().addAll(Arrays.asList(protectedBody));
137+
result.setExceptionType(type);
138+
result.setExceptionVariable(variable);
139+
return new TryCatchStatementBuilder2(result);
140+
}
141+
}
142+
143+
public static class TryCatchStatementBuilder2 {
144+
TryCatchStatement statement;
145+
146+
TryCatchStatementBuilder2(TryCatchStatement statement) {
147+
this.statement = statement;
148+
}
149+
150+
public TryCatchStatement with(Statement... statements) {
151+
statement.getHandler().addAll(Arrays.asList(statements));
152+
return statement;
153+
}
154+
}
64155
}

core/src/main/java/org/teavm/backend/wasm/generate/gc/methods/WasmGCMethodGenerator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,13 @@ private void generateMethodBody(MethodHolder method, WasmFunction function) {
289289
private void generateCustomMethodBody(WasmGCCustomGenerator customGenerator, MethodReference method,
290290
WasmFunction function) {
291291
customGenerator.apply(method, function, customGeneratorContext);
292+
var isSuspend = asyncMethods.contains(method);
293+
if (isSuspend) {
294+
if (coroutineTransformation == null) {
295+
coroutineTransformation = new CoroutineTransformation(functionTypes, this, classInfoProvider);
296+
}
297+
coroutineTransformation.transform(function);
298+
}
292299
}
293300

294301
private void generateRegularMethodBody(MethodHolder method, WasmFunction function) {

0 commit comments

Comments
 (0)