From ba08fb395cf7bf5c542bfe2631882516be0a95e0 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sun, 25 Aug 2024 20:38:02 +0200 Subject: [PATCH] wasm gc: always emit java.lang.Class, fix issue with recursive types --- .../org/teavm/backend/wasm/gc/WasmGCDependencies.java | 1 + .../wasm/generate/gc/classes/WasmGCClassGenerator.java | 9 +++++++++ .../org/teavm/backend/wasm/model/WasmCompositeType.java | 5 ----- .../java/org/teavm/backend/wasm/model/WasmModule.java | 7 ++----- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/org/teavm/backend/wasm/gc/WasmGCDependencies.java b/core/src/main/java/org/teavm/backend/wasm/gc/WasmGCDependencies.java index bcd779a87..cd4c17e02 100644 --- a/core/src/main/java/org/teavm/backend/wasm/gc/WasmGCDependencies.java +++ b/core/src/main/java/org/teavm/backend/wasm/gc/WasmGCDependencies.java @@ -30,6 +30,7 @@ public class WasmGCDependencies { } public void contribute() { + analyzer.linkClass("java.lang.Class"); contributeWasmRuntime(); contributeMathUtils(); contributeExceptionUtils(); 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 98a14150f..48f57d310 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 @@ -296,31 +296,37 @@ public class WasmGCClassGenerator implements WasmGCClassInfoProvider, WasmGCInit } public int getClassTagOffset() { + standardClasses.classClass().getStructure().init(); return classTagOffset; } @Override public int getClassArrayItemOffset() { + standardClasses.classClass().getStructure().init(); return classArrayItemOffset; } @Override public int getClassSupertypeFunctionOffset() { + standardClasses.classClass().getStructure().init(); return classSupertypeFunctionOffset; } @Override public int getClassNameOffset() { + standardClasses.classClass().getStructure().init(); return classNameOffset; } @Override public int getNewArrayFunctionOffset() { + standardClasses.classClass().getStructure().init(); return classNewArrayOffset; } @Override public int getVirtualMethodsOffset() { + standardClasses.classClass().getStructure().init(); return virtualTableFieldOffset; } @@ -387,6 +393,7 @@ public class WasmGCClassGenerator implements WasmGCClassInfoProvider, WasmGCInit } } classInfo.initializer = target -> { + standardClasses.classClass().getStructure().init(); var ranges = tagRegistry.getRanges(name); int tag = ranges.stream().mapToInt(range -> range.lower).min().orElse(0); target.add(setClassField(classInfo, classTagOffset, new WasmInt32Constant(tag))); @@ -770,6 +777,7 @@ public class WasmGCClassGenerator implements WasmGCClassInfoProvider, WasmGCInit function.add(nameVar); function.add(kindVar); + standardClasses.classClass().getStructure().getFields().size(); var flagsExpr = new WasmIntBinary( WasmIntType.INT32, WasmIntBinaryOperation.SHL, @@ -824,6 +832,7 @@ public class WasmGCClassGenerator implements WasmGCClassInfoProvider, WasmGCInit var itemVar = new WasmLocal(standardClasses.classClass().getType(), "item"); function.add(targetVar); function.add(itemVar); + standardClasses.classClass().getStructure().init(); function.getBody().add(new WasmStructSet( standardClasses.classClass().getStructure(), diff --git a/core/src/main/java/org/teavm/backend/wasm/model/WasmCompositeType.java b/core/src/main/java/org/teavm/backend/wasm/model/WasmCompositeType.java index e9923e0f1..c2a369fa6 100644 --- a/core/src/main/java/org/teavm/backend/wasm/model/WasmCompositeType.java +++ b/core/src/main/java/org/teavm/backend/wasm/model/WasmCompositeType.java @@ -18,7 +18,6 @@ package org.teavm.backend.wasm.model; public abstract class WasmCompositeType extends WasmEntity { private String name; private WasmType.CompositeReference reference; - int indexInRecursiveType = -1; int recursiveTypeCount = -1; WasmCompositeType(String name) { @@ -36,10 +35,6 @@ public abstract class WasmCompositeType extends WasmEntity { return reference; } - public int getIndexInRecursiveType() { - return indexInRecursiveType; - } - public int getRecursiveTypeCount() { return recursiveTypeCount; } diff --git a/core/src/main/java/org/teavm/backend/wasm/model/WasmModule.java b/core/src/main/java/org/teavm/backend/wasm/model/WasmModule.java index 7e7f9eb7a..f299b29a7 100644 --- a/core/src/main/java/org/teavm/backend/wasm/model/WasmModule.java +++ b/core/src/main/java/org/teavm/backend/wasm/model/WasmModule.java @@ -107,12 +107,7 @@ public class WasmModule { var sccsByIndex = new int[types.size()][]; for (var scc : sccs) { sccsByIndex[scc[0]] = scc; - var firstType = types.get(scc[0]); - firstType.recursiveTypeCount = scc.length; for (var i = 0; i < scc.length; i++) { - var index = scc[i]; - var type = types.get(index); - type.indexInRecursiveType = i; sccStartNode[scc[i]] = sccStartNode[scc[0]]; } } @@ -162,9 +157,11 @@ public class WasmModule { visit(outgoing); } } + var indexOfFirst = sorted.size(); for (var index : scc) { visitScc(index, typeIndex); } + sorted.get(indexOfFirst).recursiveTypeCount = scc.length; } }