wasm gc: fix remaining issues that prevent running trivial example

This commit is contained in:
Alexey Andreev 2024-08-01 20:25:18 +02:00
parent 54dc7fe5f8
commit 9b5e1e7661
5 changed files with 29 additions and 5 deletions

View File

@ -24,7 +24,7 @@ public class JSStdoutPrintStream extends JsConsolePrintStream {
public void print(String s) { public void print(String s) {
if (PlatformDetector.isWebAssemblyGC()) { if (PlatformDetector.isWebAssemblyGC()) {
for (int i = 0; i < s.length(); ++i) { for (int i = 0; i < s.length(); ++i) {
WasmGCSupport.putCharStderr(s.charAt(i)); WasmGCSupport.putCharStdout(s.charAt(i));
} }
} else { } else {
writeJs(s); writeJs(s);

View File

@ -125,10 +125,11 @@ public class WasmGCDeclarationsGenerator {
} }
public void contributeToInitializer(WasmFunction function) { public void contributeToInitializer(WasmFunction function) {
classGenerator.contributeToInitializer(function);
var contributors = List.of(classGenerator, classGenerator.strings); var contributors = List.of(classGenerator, classGenerator.strings);
for (var contributor : contributors) { for (var contributor : contributors) {
contributor.contributeToInitializerDefinitions(function); contributor.contributeToInitializerDefinitions(function);
}
for (var contributor : contributors) {
contributor.contributeToInitializer(function); contributor.contributeToInitializer(function);
} }
} }

View File

@ -81,7 +81,7 @@ public class WasmGCStringPool implements WasmGCStringProvider, WasmGCInitializer
if (nextCharArrayFunction == null) { if (nextCharArrayFunction == null) {
initNextCharArrayFunction(); initNextCharArrayFunction();
} }
binaryWriter.writeInt32(string.length()); binaryWriter.writeLEB(string.length());
binaryWriter.writeBytes(string.getBytes(StandardCharsets.UTF_8)); binaryWriter.writeBytes(string.getBytes(StandardCharsets.UTF_8));
var globalName = "teavm_java_string_" + stringMap.size(); var globalName = "teavm_java_string_" + stringMap.size();
var globalType = standardClasses.stringClass().getType(); var globalType = standardClasses.stringClass().getType();

View File

@ -20,11 +20,15 @@ import org.teavm.backend.wasm.model.WasmGlobal;
import org.teavm.backend.wasm.model.WasmLocal; import org.teavm.backend.wasm.model.WasmLocal;
import org.teavm.backend.wasm.model.WasmType; import org.teavm.backend.wasm.model.WasmType;
import org.teavm.backend.wasm.model.expression.WasmGetGlobal; 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.WasmInt32Constant;
import org.teavm.backend.wasm.model.expression.WasmInt32Subtype;
import org.teavm.backend.wasm.model.expression.WasmIntBinary; 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.WasmLoadInt32;
import org.teavm.backend.wasm.model.expression.WasmReturn; 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.WasmSetLocal;
import org.teavm.model.MethodReference; import org.teavm.model.MethodReference;
@ -38,9 +42,11 @@ public class WasmGCStringPoolGenerator implements WasmGCCustomGenerator {
var resultLocal = new WasmLocal(WasmType.INT32); var resultLocal = new WasmLocal(WasmType.INT32);
function.add(resultLocal); 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, var increment = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.ADD,
new WasmGetGlobal(pointer), new WasmInt32Constant(1)); new WasmGetGlobal(pointer), new WasmInt32Constant(1));
function.getBody().add(new WasmSetLocal(resultLocal, increment)); function.getBody().add(new WasmSetGlobal(pointer, increment));
function.getBody().add(new WasmReturn(new WasmGetGlobal(pointer))); function.getBody().add(new WasmReturn(new WasmGetLocal(resultLocal)));
} }
} }

View File

@ -477,6 +477,23 @@ public class WasmBinaryRenderer {
section.writeBytes(payload); 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()); writeSection(SECTION_UNKNOWN, "name", section.getData());
} }