Skip to content

Commit

Permalink
wasm gc: implement branching instructions and null check instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
konsoletyper committed Sep 13, 2024
1 parent 5acb9a4 commit 7029d3c
Show file tree
Hide file tree
Showing 21 changed files with 567 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,33 @@ public void branch(BranchOpcode opcode, int depth, int target) {
case BR_IF:
writer.write("br_if");
break;
case BR_ON_NULL:
writer.write("br_on_null");
break;
case BR_ON_NON_NULL:
writer.write("br_on_non_null");
break;
}
writer.endLink().write(" $label_" + target).eol();
}

@Override
public void castBranch(boolean success, int depth, int target, WasmHollowType.Reference sourceType,
WasmHollowType.Reference targetType) {
writer.address().startLink("start" + target);
if (success) {
writer.write("br_if_cast");
} else {
writer.write("br_if_cast_fail");
}
writer.endLink().write(" $label_" + target);
writer.write(" ");
writeType(sourceType);
writer.write(" ");
writeType(sourceType);
writer.eol();
}

@Override
public void tableBranch(int[] depths, int[] targets, int defaultDepth, int defaultTarget) {
writer.address();
Expand Down Expand Up @@ -162,6 +185,9 @@ public void opcode(Opcode opcode) {
case ARRAY_LENGTH:
writer.write("array.length");
break;
case IS_NULL:
writer.write("ref.is_null");
break;
}
writer.eol();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ public void visit(UnaryExpr expr) {

protected abstract boolean isManagedCall(MethodReference method);

private WasmExpression nullCheck(Expr value, TextLocation location) {
protected WasmExpression nullCheck(Expr value, TextLocation location) {
var block = new WasmBlock(false);
block.setLocation(location);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.teavm.backend.wasm.model.expression.WasmIntBinary;
import org.teavm.backend.wasm.model.expression.WasmIntBinaryOperation;
import org.teavm.backend.wasm.model.expression.WasmIntType;
import org.teavm.backend.wasm.model.expression.WasmNullConstant;
import org.teavm.backend.wasm.model.expression.WasmIsNull;
import org.teavm.backend.wasm.model.expression.WasmReferencesEqual;
import org.teavm.backend.wasm.model.expression.WasmReturn;
import org.teavm.backend.wasm.model.expression.WasmSetLocal;
Expand Down Expand Up @@ -156,8 +156,7 @@ private void generateIsArray(WasmLocal subtypeVar, ValueType itemType, List<Wasm
var itemExpression = getClassField(new WasmGetLocal(subtypeVar), itemOffset);
body.add(new WasmSetLocal(subtypeVar, itemExpression));

var itemTest = new WasmConditional(new WasmReferencesEqual(new WasmGetLocal(subtypeVar),
new WasmNullConstant(WasmType.Reference.STRUCT)));
var itemTest = new WasmConditional(new WasmIsNull(new WasmGetLocal(subtypeVar)));
itemTest.setType(WasmType.INT32);
itemTest.getThenBlock().getBody().add(new WasmInt32Constant(0));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
import org.teavm.backend.wasm.model.expression.WasmIntType;
import org.teavm.backend.wasm.model.expression.WasmIntUnary;
import org.teavm.backend.wasm.model.expression.WasmIntUnaryOperation;
import org.teavm.backend.wasm.model.expression.WasmIsNull;
import org.teavm.backend.wasm.model.expression.WasmNullBranch;
import org.teavm.backend.wasm.model.expression.WasmNullCondition;
import org.teavm.backend.wasm.model.expression.WasmNullConstant;
import org.teavm.backend.wasm.model.expression.WasmReferencesEqual;
import org.teavm.backend.wasm.model.expression.WasmSetGlobal;
Expand Down Expand Up @@ -232,7 +235,32 @@ protected WasmExpression nullLiteral(WasmType type) {

@Override
protected WasmExpression genIsNull(WasmExpression value) {
return new WasmReferencesEqual(value, new WasmNullConstant(WasmType.Reference.STRUCT));
return new WasmIsNull(value);
}

protected WasmExpression nullCheck(Expr value, TextLocation location) {
var block = new WasmBlock(false);
block.setLocation(location);

accept(value);
if (result instanceof WasmUnreachable) {
return result;
}
result.acceptVisitor(typeInference);
block.setType(typeInference.getResult());
var cachedValue = exprCache.create(result, typeInference.getResult(), location, block.getBody());

var check = new WasmNullBranch(WasmNullCondition.NOT_NULL, cachedValue.expr(), block);
check.setResult(cachedValue.expr());
block.getBody().add(new WasmDrop(check));

var callSiteId = generateCallSiteId(location);
callSiteId.generateRegister(block.getBody(), location);
generateThrowNPE(location, block.getBody());
callSiteId.generateThrow(block.getBody(), location);

cachedValue.release();
return block;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
import org.teavm.backend.wasm.model.expression.WasmCallReference;
import org.teavm.backend.wasm.model.expression.WasmConditional;
import org.teavm.backend.wasm.model.expression.WasmGetLocal;
import org.teavm.backend.wasm.model.expression.WasmNullConstant;
import org.teavm.backend.wasm.model.expression.WasmReferencesEqual;
import org.teavm.backend.wasm.model.expression.WasmIsNull;
import org.teavm.backend.wasm.model.expression.WasmReturn;
import org.teavm.backend.wasm.model.expression.WasmSetGlobal;
import org.teavm.backend.wasm.model.expression.WasmStructGet;
Expand All @@ -50,8 +49,7 @@ private void generateIsAssignable(WasmFunction function, WasmGCCustomGeneratorCo
function.add(thisVar);
function.add(otherClassVar);

var conditional = new WasmConditional(new WasmReferencesEqual(new WasmGetLocal(otherClassVar),
new WasmNullConstant(WasmType.Reference.STRUCT)));
var conditional = new WasmConditional(new WasmIsNull(new WasmGetLocal(otherClassVar)));
function.getBody().add(conditional);
var npe = new WasmCall(context.functions().forStaticMethod(new MethodReference(WasmGCSupport.class, "npe",
NullPointerException.class)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2024 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.backend.wasm.model.expression;

import java.util.Objects;
import org.teavm.backend.wasm.model.WasmType;

public class WasmCastBranch extends WasmExpression {
private WasmCastCondition condition;
private WasmExpression value;
private WasmType.Reference sourceType;
private WasmType.Reference type;
private WasmBlock target;
private WasmExpression result;

public WasmCastBranch(WasmCastCondition condition, WasmExpression value, WasmType.Reference sourceType,
WasmType.Reference type, WasmBlock target) {
this.condition = Objects.requireNonNull(condition);
this.value = Objects.requireNonNull(value);
this.type = Objects.requireNonNull(type);
this.target = Objects.requireNonNull(target);
}

public WasmCastCondition getCondition() {
return condition;
}

public void setCondition(WasmCastCondition condition) {
this.condition = Objects.requireNonNull(condition);
}

public WasmExpression getValue() {
return value;
}

public void setValue(WasmExpression value) {
this.value = Objects.requireNonNull(value);
}

public WasmType.Reference getSourceType() {
return sourceType;
}

public void setSourceType(WasmType.Reference sourceType) {
this.sourceType = Objects.requireNonNull(sourceType);
}

public WasmType.Reference getType() {
return type;
}

public void setType(WasmType.Reference type) {
this.type = Objects.requireNonNull(type);
}

public WasmBlock getTarget() {
return target;
}

public void setTarget(WasmBlock target) {
this.target = Objects.requireNonNull(target);
}

public WasmExpression getResult() {
return result;
}

public void setResult(WasmExpression result) {
this.result = result;
}

@Override
public void acceptVisitor(WasmExpressionVisitor visitor) {
visitor.visit(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2024 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.backend.wasm.model.expression;

public enum WasmCastCondition {
SUCCESS,
FAILURE
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ public void visit(WasmBranch expression) {
}
}

@Override
public void visit(WasmNullBranch expression) {
expression.getValue().acceptVisitor(this);
if (expression.getResult() != null) {
expression.getResult().acceptVisitor(this);
}
}

@Override
public void visit(WasmCastBranch expression) {
expression.getValue().acceptVisitor(this);
if (expression.getResult() != null) {
expression.getResult().acceptVisitor(this);
}
}

@Override
public void visit(WasmBreak expression) {
if (expression.getResult() != null) {
Expand Down Expand Up @@ -85,6 +101,11 @@ public void visit(WasmFloat64Constant expression) {
public void visit(WasmNullConstant expression) {
}

@Override
public void visit(WasmIsNull expression) {
expression.getValue().acceptVisitor(this);
}

@Override
public void visit(WasmGetLocal expression) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public interface WasmExpressionVisitor {

void visit(WasmBranch expression);

void visit(WasmNullBranch expression);

void visit(WasmCastBranch expression);

void visit(WasmBreak expression);

void visit(WasmSwitch expression);
Expand All @@ -40,6 +44,8 @@ public interface WasmExpressionVisitor {

void visit(WasmNullConstant expression);

void visit(WasmIsNull expression);

void visit(WasmGetLocal expression);

void visit(WasmSetLocal expression);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2024 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.backend.wasm.model.expression;

import java.util.Objects;

public class WasmIsNull extends WasmExpression {
private WasmExpression value;

public WasmIsNull(WasmExpression value) {
this.value = Objects.requireNonNull(value);
}

public WasmExpression getValue() {
return value;
}

public void setValue(WasmExpression value) {
this.value = value;
}

@Override
public void acceptVisitor(WasmExpressionVisitor visitor) {
visitor.visit(this);
}
}
Loading

0 comments on commit 7029d3c

Please sign in to comment.