From 9b5e1e7661934ba183b6bb270c8251e084b61c41 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Thu, 1 Aug 2024 20:25:18 +0200 Subject: [PATCH] wasm gc: fix remaining issues that prevent running trivial example --- .../impl/console/JSStdoutPrintStream.java | 2 +- .../gc/WasmGCDeclarationsGenerator.java | 3 ++- .../generate/gc/strings/WasmGCStringPool.java | 2 +- .../gc/WasmGCStringPoolGenerator.java | 10 ++++++++-- .../backend/wasm/render/WasmBinaryRenderer.java | 17 +++++++++++++++++ 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/impl/console/JSStdoutPrintStream.java b/classlib/src/main/java/org/teavm/classlib/impl/console/JSStdoutPrintStream.java index 9707e83f6..76f2de9eb 100644 --- a/classlib/src/main/java/org/teavm/classlib/impl/console/JSStdoutPrintStream.java +++ b/classlib/src/main/java/org/teavm/classlib/impl/console/JSStdoutPrintStream.java @@ -24,7 +24,7 @@ public class JSStdoutPrintStream extends JsConsolePrintStream { public void print(String s) { if (PlatformDetector.isWebAssemblyGC()) { for (int i = 0; i < s.length(); ++i) { - WasmGCSupport.putCharStderr(s.charAt(i)); + WasmGCSupport.putCharStdout(s.charAt(i)); } } else { writeJs(s); diff --git a/core/src/main/java/org/teavm/backend/wasm/generate/gc/WasmGCDeclarationsGenerator.java b/core/src/main/java/org/teavm/backend/wasm/generate/gc/WasmGCDeclarationsGenerator.java index 489ecfa1f..391409805 100644 --- a/core/src/main/java/org/teavm/backend/wasm/generate/gc/WasmGCDeclarationsGenerator.java +++ b/core/src/main/java/org/teavm/backend/wasm/generate/gc/WasmGCDeclarationsGenerator.java @@ -125,10 +125,11 @@ public class WasmGCDeclarationsGenerator { } public void contributeToInitializer(WasmFunction function) { - classGenerator.contributeToInitializer(function); var contributors = List.of(classGenerator, classGenerator.strings); for (var contributor : contributors) { contributor.contributeToInitializerDefinitions(function); + } + for (var contributor : contributors) { contributor.contributeToInitializer(function); } } diff --git a/core/src/main/java/org/teavm/backend/wasm/generate/gc/strings/WasmGCStringPool.java b/core/src/main/java/org/teavm/backend/wasm/generate/gc/strings/WasmGCStringPool.java index 49091e65a..dea33a1a8 100644 --- a/core/src/main/java/org/teavm/backend/wasm/generate/gc/strings/WasmGCStringPool.java +++ b/core/src/main/java/org/teavm/backend/wasm/generate/gc/strings/WasmGCStringPool.java @@ -81,7 +81,7 @@ public class WasmGCStringPool implements WasmGCStringProvider, WasmGCInitializer if (nextCharArrayFunction == null) { initNextCharArrayFunction(); } - binaryWriter.writeInt32(string.length()); + binaryWriter.writeLEB(string.length()); binaryWriter.writeBytes(string.getBytes(StandardCharsets.UTF_8)); var globalName = "teavm_java_string_" + stringMap.size(); var globalType = standardClasses.stringClass().getType(); diff --git a/core/src/main/java/org/teavm/backend/wasm/generators/gc/WasmGCStringPoolGenerator.java b/core/src/main/java/org/teavm/backend/wasm/generators/gc/WasmGCStringPoolGenerator.java index 86eccd6ba..d4512ca8a 100644 --- a/core/src/main/java/org/teavm/backend/wasm/generators/gc/WasmGCStringPoolGenerator.java +++ b/core/src/main/java/org/teavm/backend/wasm/generators/gc/WasmGCStringPoolGenerator.java @@ -20,11 +20,15 @@ import org.teavm.backend.wasm.model.WasmGlobal; import org.teavm.backend.wasm.model.WasmLocal; import org.teavm.backend.wasm.model.WasmType; import org.teavm.backend.wasm.model.expression.WasmGetGlobal; +import org.teavm.backend.wasm.model.expression.WasmGetLocal; import org.teavm.backend.wasm.model.expression.WasmInt32Constant; +import org.teavm.backend.wasm.model.expression.WasmInt32Subtype; 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.WasmLoadInt32; 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.model.MethodReference; @@ -38,9 +42,11 @@ public class WasmGCStringPoolGenerator implements WasmGCCustomGenerator { var resultLocal = new WasmLocal(WasmType.INT32); function.add(resultLocal); + function.getBody().add(new WasmSetLocal(resultLocal, new WasmLoadInt32(1, new WasmGetGlobal(pointer), + WasmInt32Subtype.UINT8))); var increment = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.ADD, new WasmGetGlobal(pointer), new WasmInt32Constant(1)); - function.getBody().add(new WasmSetLocal(resultLocal, increment)); - function.getBody().add(new WasmReturn(new WasmGetGlobal(pointer))); + function.getBody().add(new WasmSetGlobal(pointer, increment)); + function.getBody().add(new WasmReturn(new WasmGetLocal(resultLocal))); } } diff --git a/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderer.java b/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderer.java index 39647bf3e..605296bf0 100644 --- a/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderer.java +++ b/core/src/main/java/org/teavm/backend/wasm/render/WasmBinaryRenderer.java @@ -477,6 +477,23 @@ public class WasmBinaryRenderer { section.writeBytes(payload); } + var globals = module.globals.stream() + .filter(g -> g.getName() != null) + .collect(Collectors.toList()); + if (!globals.isEmpty()) { + var globalsSubsection = new WasmBinaryWriter(); + globalsSubsection.writeLEB(globals.size()); + for (var global : globals) { + globalsSubsection.writeLEB(module.globals.indexOf(global)); + globalsSubsection.writeAsciiString(global.getName()); + } + + payload = globalsSubsection.getData(); + section.writeLEB(7); + section.writeLEB(payload.length); + section.writeBytes(payload); + } + writeSection(SECTION_UNKNOWN, "name", section.getData()); }