Wasm backend: fix issues related to instanceof expression

This commit is contained in:
Alexey Andreev 2018-05-19 22:55:32 +03:00
parent 3ef3678ed0
commit 5ce48ce866
6 changed files with 59 additions and 6 deletions

View File

@ -23,6 +23,7 @@ import org.teavm.interop.Rename;
import org.teavm.interop.Structure; import org.teavm.interop.Structure;
import org.teavm.interop.Superclass; import org.teavm.interop.Superclass;
import org.teavm.interop.Sync; import org.teavm.interop.Sync;
import org.teavm.interop.Unmanaged;
import org.teavm.jso.browser.TimerHandler; import org.teavm.jso.browser.TimerHandler;
import org.teavm.platform.Platform; import org.teavm.platform.Platform;
import org.teavm.platform.PlatformObject; import org.teavm.platform.PlatformObject;
@ -392,6 +393,7 @@ public class TObject {
protected void finalize() throws TThrowable { protected void finalize() throws TThrowable {
} }
@Unmanaged
public static TObject wrap(Object obj) { public static TObject wrap(Object obj) {
return (TObject) obj; return (TObject) obj;
} }

View File

@ -50,6 +50,7 @@ import org.teavm.backend.wasm.intrinsics.FloatIntrinsic;
import org.teavm.backend.wasm.intrinsics.FunctionIntrinsic; import org.teavm.backend.wasm.intrinsics.FunctionIntrinsic;
import org.teavm.backend.wasm.intrinsics.GCIntrinsic; import org.teavm.backend.wasm.intrinsics.GCIntrinsic;
import org.teavm.backend.wasm.intrinsics.MutatorIntrinsic; import org.teavm.backend.wasm.intrinsics.MutatorIntrinsic;
import org.teavm.backend.wasm.intrinsics.ObjectIntrinsic;
import org.teavm.backend.wasm.intrinsics.PlatformClassIntrinsic; import org.teavm.backend.wasm.intrinsics.PlatformClassIntrinsic;
import org.teavm.backend.wasm.intrinsics.PlatformClassMetadataIntrinsic; import org.teavm.backend.wasm.intrinsics.PlatformClassMetadataIntrinsic;
import org.teavm.backend.wasm.intrinsics.PlatformIntrinsic; import org.teavm.backend.wasm.intrinsics.PlatformIntrinsic;
@ -338,6 +339,7 @@ public class WasmTarget implements TeaVMTarget, TeaVMWasmHost {
context.addIntrinsic(new RuntimeClassIntrinsic()); context.addIntrinsic(new RuntimeClassIntrinsic());
context.addIntrinsic(new FloatIntrinsic()); context.addIntrinsic(new FloatIntrinsic());
context.addIntrinsic(new DoubleIntrinsic()); context.addIntrinsic(new DoubleIntrinsic());
context.addIntrinsic(new ObjectIntrinsic());
context.addGenerator(new ArrayGenerator()); context.addGenerator(new ArrayGenerator());
IntrinsicFactoryContext intrinsicFactoryContext = new IntrinsicFactoryContext(); IntrinsicFactoryContext intrinsicFactoryContext = new IntrinsicFactoryContext();

View File

@ -120,7 +120,6 @@ import org.teavm.runtime.RuntimeClass;
import org.teavm.runtime.ShadowStack; import org.teavm.runtime.ShadowStack;
class WasmGenerationVisitor implements StatementVisitor, ExprVisitor { class WasmGenerationVisitor implements StatementVisitor, ExprVisitor {
private static FieldReference tagField = new FieldReference(RuntimeClass.class.getName(), "tag");
private static final int SWITCH_TABLE_THRESHOLD = 256; private static final int SWITCH_TABLE_THRESHOLD = 256;
private WasmGenerationContext context; private WasmGenerationContext context;
private WasmClassGenerator classGenerator; private WasmClassGenerator classGenerator;
@ -1267,6 +1266,8 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor {
@Override @Override
public void visit(InstanceOfExpr expr) { public void visit(InstanceOfExpr expr) {
classGenerator.getClassPointer(expr.getType());
accept(expr.getExpr()); accept(expr.getExpr());
WasmBlock block = new WasmBlock(false); WasmBlock block = new WasmBlock(false);
@ -1279,14 +1280,15 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor {
WasmBranch ifNull = new WasmBranch(new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.EQ, WasmBranch ifNull = new WasmBranch(new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.EQ,
new WasmGetLocal(objectVar), new WasmInt32Constant(0)), block); new WasmGetLocal(objectVar), new WasmInt32Constant(0)), block);
ifNull.setResult(new WasmInt32Constant(0)); ifNull.setResult(new WasmInt32Constant(0));
block.getBody().add(ifNull); block.getBody().add(new WasmDrop(ifNull));
WasmCall supertypeCall = new WasmCall(context.names.forSupertypeFunction(expr.getType())); WasmCall supertypeCall = new WasmCall(context.names.forSupertypeFunction(expr.getType()));
int tagOffset = classGenerator.getFieldOffset(tagField); WasmExpression classRef = new WasmLoadInt32(4, result, WasmInt32Subtype.INT32);
WasmExpression classRef = new WasmLoadInt32(4, result, WasmInt32Subtype.INT32, tagOffset);
classRef = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.SHL, classRef, new WasmInt32Constant(3)); classRef = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.SHL, classRef, new WasmInt32Constant(3));
supertypeCall.getArguments().add(classRef); supertypeCall.getArguments().add(classRef);
block.getBody().add(supertypeCall); block.getBody().add(supertypeCall);
result = block;
} }
@Override @Override

View File

@ -32,6 +32,7 @@ import org.teavm.backend.wasm.model.expression.WasmIntBinary;
import org.teavm.backend.wasm.model.expression.WasmIntBinaryOperation; import org.teavm.backend.wasm.model.expression.WasmIntBinaryOperation;
import org.teavm.backend.wasm.model.expression.WasmIntType; import org.teavm.backend.wasm.model.expression.WasmIntType;
import org.teavm.backend.wasm.model.expression.WasmLoadFloat32; import org.teavm.backend.wasm.model.expression.WasmLoadFloat32;
import org.teavm.backend.wasm.model.expression.WasmLoadFloat64;
import org.teavm.backend.wasm.model.expression.WasmLoadInt32; import org.teavm.backend.wasm.model.expression.WasmLoadInt32;
import org.teavm.backend.wasm.model.expression.WasmLoadInt64; import org.teavm.backend.wasm.model.expression.WasmLoadInt64;
import org.teavm.backend.wasm.model.expression.WasmReturn; import org.teavm.backend.wasm.model.expression.WasmReturn;
@ -147,7 +148,7 @@ public class ArrayGenerator implements WasmMethodGenerator {
call.getArguments().add(new WasmLoadFloat32(2, offset, baseAddr)); call.getArguments().add(new WasmLoadFloat32(2, offset, baseAddr));
break; break;
case DOUBLE: case DOUBLE:
call.getArguments().add(new WasmLoadFloat32(3, offset, baseAddr)); call.getArguments().add(new WasmLoadFloat64(3, offset, baseAddr));
break; break;
} }

View File

@ -0,0 +1,46 @@
/*
* Copyright 2018 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.intrinsics;
import org.teavm.ast.InvocationExpr;
import org.teavm.backend.wasm.model.expression.WasmExpression;
import org.teavm.model.MethodReference;
public class ObjectIntrinsic implements WasmIntrinsic {
@Override
public boolean isApplicable(MethodReference methodReference) {
if (!methodReference.getClassName().equals("java.lang.Object")) {
return false;
}
switch (methodReference.getName()) {
case "wrap":
return true;
default:
return false;
}
}
@Override
public WasmExpression apply(InvocationExpr invocation, WasmIntrinsicManager manager) {
switch (invocation.getMethod().getName()) {
case "wrap":
return manager.generate(invocation.getArguments().get(0));
default:
throw new AssertionError();
}
}
}

View File

@ -658,7 +658,7 @@ class WasmBinaryRenderingVisitor implements WasmExpressionVisitor {
? functionIndexes.get(expression.getFunctionName()) ? functionIndexes.get(expression.getFunctionName())
: importedIndexes.get(expression.getFunctionName()); : importedIndexes.get(expression.getFunctionName());
if (functionIndex == null) { if (functionIndex == null) {
writer.writeByte(0x0A); writer.writeByte(0x00);
return; return;
} }