From 019eb4500cf427de6f09525153947a6924e0bcbb Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 2 Sep 2024 16:02:58 +0200 Subject: [PATCH] wasm gc: fix issues, make more test passing --- .../wasm/disasm/BaseDisassemblyListener.java | 2 +- .../gc/classes/WasmGCClassGenerator.java | 5 +++++ .../gc/methods/WasmGCMethodGenerator.java | 4 +++- .../org/teavm/model/optimization/Inlining.java | 16 +++++++++++++++- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/teavm/backend/wasm/disasm/BaseDisassemblyListener.java b/core/src/main/java/org/teavm/backend/wasm/disasm/BaseDisassemblyListener.java index 74514d49e..21c0a31cd 100644 --- a/core/src/main/java/org/teavm/backend/wasm/disasm/BaseDisassemblyListener.java +++ b/core/src/main/java/org/teavm/backend/wasm/disasm/BaseDisassemblyListener.java @@ -122,7 +122,7 @@ public abstract class BaseDisassemblyListener { var refType = (WasmHollowType.CompositeReference) type; writer.write("(ref "); if (refType.isNullable()) { - writer.write("null"); + writer.write("null "); } writeTypeRef(refType.index); writer.write(")"); diff --git a/core/src/main/java/org/teavm/backend/wasm/generate/gc/classes/WasmGCClassGenerator.java b/core/src/main/java/org/teavm/backend/wasm/generate/gc/classes/WasmGCClassGenerator.java index adcd15c17..6150eae8d 100644 --- a/core/src/main/java/org/teavm/backend/wasm/generate/gc/classes/WasmGCClassGenerator.java +++ b/core/src/main/java/org/teavm/backend/wasm/generate/gc/classes/WasmGCClassGenerator.java @@ -64,6 +64,7 @@ import org.teavm.backend.wasm.model.expression.WasmNullConstant; import org.teavm.backend.wasm.model.expression.WasmReturn; import org.teavm.backend.wasm.model.expression.WasmSetGlobal; import org.teavm.backend.wasm.model.expression.WasmSetLocal; +import org.teavm.backend.wasm.model.expression.WasmSignedType; import org.teavm.backend.wasm.model.expression.WasmStructGet; import org.teavm.backend.wasm.model.expression.WasmStructNewDefault; import org.teavm.backend.wasm.model.expression.WasmStructSet; @@ -558,18 +559,22 @@ public class WasmGCClassGenerator implements WasmGCClassInfoProvider, WasmGCInit case BOOLEAN: primitiveType = boolean.class; wrapperType = Boolean.class; + result.setSignedType(WasmSignedType.UNSIGNED); break; case BYTE: primitiveType = byte.class; wrapperType = Byte.class; + result.setSignedType(WasmSignedType.SIGNED); break; case SHORT: primitiveType = short.class; wrapperType = Short.class; + result.setSignedType(WasmSignedType.SIGNED); break; case CHARACTER: primitiveType = char.class; wrapperType = Character.class; + result.setSignedType(WasmSignedType.UNSIGNED); break; case INTEGER: primitiveType = int.class; diff --git a/core/src/main/java/org/teavm/backend/wasm/generate/gc/methods/WasmGCMethodGenerator.java b/core/src/main/java/org/teavm/backend/wasm/generate/gc/methods/WasmGCMethodGenerator.java index c0f558cbc..fa806ea16 100644 --- a/core/src/main/java/org/teavm/backend/wasm/generate/gc/methods/WasmGCMethodGenerator.java +++ b/core/src/main/java/org/teavm/backend/wasm/generate/gc/methods/WasmGCMethodGenerator.java @@ -238,7 +238,9 @@ public class WasmGCMethodGenerator implements BaseWasmFunctionRepository { var variable = method.getProgram().variableAt(i); var varNodeIndex = variable.getRegister() >= 0 ? originalIndexToIndex[variable.getRegister()] : -1; if (varNodeIndex >= 0 && variableRepresentatives[varNodeIndex] < 0) { - variableRepresentatives[varNodeIndex] = variable.getIndex(); + if (typeInference.typeOf(variable) != null) { + variableRepresentatives[varNodeIndex] = variable.getIndex(); + } } } diff --git a/core/src/main/java/org/teavm/model/optimization/Inlining.java b/core/src/main/java/org/teavm/model/optimization/Inlining.java index 6f5cd6eb1..95c35bf99 100644 --- a/core/src/main/java/org/teavm/model/optimization/Inlining.java +++ b/core/src/main/java/org/teavm/model/optimization/Inlining.java @@ -47,10 +47,12 @@ import org.teavm.model.Program; import org.teavm.model.ProgramReader; import org.teavm.model.TextLocation; import org.teavm.model.TryCatchBlock; +import org.teavm.model.ValueType; import org.teavm.model.VariableReader; import org.teavm.model.analysis.ClassInference; import org.teavm.model.instructions.AbstractInstructionReader; import org.teavm.model.instructions.AssignInstruction; +import org.teavm.model.instructions.CastInstruction; import org.teavm.model.instructions.ExitInstruction; import org.teavm.model.instructions.InvocationType; import org.teavm.model.instructions.InvokeInstruction; @@ -456,8 +458,20 @@ public class Inlining { } if (implementations.size() == 1) { + var implementation = implementations.iterator().next(); invoke.setType(InvocationType.SPECIAL); - invoke.setMethod(implementations.iterator().next()); + invoke.setMethod(implementation); + + if (!implementation.getName().equals(invoke.getMethod().getClassName())) { + var cast = new CastInstruction(); + cast.setWeak(true); + cast.setValue(invoke.getInstance()); + cast.setReceiver(program.createVariable()); + cast.setLocation(invoke.getLocation()); + cast.setTargetType(ValueType.object(implementation.getClassName())); + invoke.insertPrevious(cast); + invoke.setInstance(cast.getReceiver()); + } } } }