diff --git a/core/src/main/java/org/teavm/backend/wasm/WasmRuntime.java b/core/src/main/java/org/teavm/backend/wasm/WasmRuntime.java index f3f1526e2..69e20f493 100644 --- a/core/src/main/java/org/teavm/backend/wasm/WasmRuntime.java +++ b/core/src/main/java/org/teavm/backend/wasm/WasmRuntime.java @@ -19,8 +19,6 @@ import org.teavm.interop.Address; import org.teavm.interop.Import; public final class WasmRuntime { - private static Address offset; - private WasmRuntime() { } @@ -64,108 +62,6 @@ public final class WasmRuntime { private static native boolean gt(double a, double b); - public static void decodeData(Address data, Address metadata) { - offset = metadata; - int count = decodeLeb(); - - while (count-- > 0) { - data = decodeDataRec(data); - } - } - - private static Address decodeDataRec(Address data) { - Address metadata = offset; - int type = metadata.getByte(); - metadata = metadata.add(1); - - switch (type) { - case 0: - data = data.add(1); - break; - case 1: { - data = align(data, 2); - byte a = data.getByte(); - byte b = data.add(1).getByte(); - int value = (a << 8) | b; - data.putShort((short) value); - data = data.add(2); - break; - } - case 2: - case 4: - case 6: { - data = align(data, 4); - int value = getInt(data); - data.putInt(value); - data = data.add(4); - break; - } - case 3: - case 5: { - data = align(data, 8); - data.putLong(getLong(data)); - data = data.add(8); - break; - } - case 7: { - offset = metadata; - int size = decodeLeb(); - metadata = offset; - while (size-- > 0) { - offset = metadata; - data = decodeDataRec(data); - } - metadata = offset; - break; - } - case 8: { - int alignment = metadata.getByte(); - offset = metadata.add(1); - int size = decodeLeb(); - if (alignment > 0) { - data = align(data, alignment); - } - while (size-- > 0) { - data = decodeDataRec(data); - } - metadata = offset; - break; - } - case 9: { - Address start = metadata.add(-1); - offset = metadata; - int back = decodeLeb(); - metadata = offset; - offset = start.add(-back); - data = decodeDataRec(data); - break; - } - } - - offset = metadata; - return data; - } - - private static int getInt(Address data) { - int a = data.getByte() & 0xFF; - int b = data.add(1).getByte() & 0xFF; - int c = data.add(2).getByte() & 0xFF; - int d = data.add(3).getByte() & 0xFF; - return (a << 24) | (b << 16) | (c << 8) | d; - } - - private static long getLong(Address data) { - long a = data.getByte() & 0xFF; - long b = data.add(1).getByte() & 0xFF; - long c = data.add(2).getByte() & 0xFF; - long d = data.add(3).getByte() & 0xFF; - long e = data.add(4).getByte() & 0xFF; - long f = data.add(5).getByte() & 0xFF; - long g = data.add(6).getByte() & 0xFF; - long h = data.add(7).getByte() & 0xFF; - return (a << 56) | (b << 48) | (c << 40) | (d << 32) | (e << 24) | (f << 16) | (g << 8) | h; - } - public static Address align(Address address, int alignment) { int value = address.toInt(); if (value == 0) { @@ -175,21 +71,6 @@ public final class WasmRuntime { return Address.fromInt(value); } - private static int decodeLeb() { - Address index = offset; - int result = 0; - int shift = 0; - int value; - do { - value = index.getByte(); - index = index.add(1); - result |= (value & 0x7F) << shift; - shift += 7; - } while ((value & 0x80) != 0); - offset = index; - return result; - } - @Import(name = "print", module = "spectest") public static native void print(int a); diff --git a/core/src/main/java/org/teavm/backend/wasm/WasmTarget.java b/core/src/main/java/org/teavm/backend/wasm/WasmTarget.java index b688f15f7..3676b8161 100644 --- a/core/src/main/java/org/teavm/backend/wasm/WasmTarget.java +++ b/core/src/main/java/org/teavm/backend/wasm/WasmTarget.java @@ -160,8 +160,8 @@ public class WasmTarget implements TeaVMTarget { dependencyChecker.linkMethod(method, null).use(); } - dependencyChecker.linkMethod(new MethodReference(WasmRuntime.class, "decodeData", Address.class, - Address.class, void.class), null).use(); + dependencyChecker.linkMethod(new MethodReference(WasmRuntime.class, "align", Address.class, int.class, + Address.class), null).use(); dependencyChecker.linkMethod(new MethodReference(WasmRuntime.class, "fillZero", Address.class, int.class, void.class), null).use(); dependencyChecker.linkMethod(new MethodReference(WasmRuntime.class, "moveMemoryBlock", Address.class, @@ -227,18 +227,6 @@ public class WasmTarget implements TeaVMTarget { dataSegment.setOffset(256); module.getSegments().add(dataSegment); - WasmMemorySegment metadataSegment = new WasmMemorySegment(); - metadataSegment.setData(binaryWriter.getMetadata()); - metadataSegment.setOffset(binaryWriter.getAddress()); - module.getSegments().add(metadataSegment); - - MethodReference initData = new MethodReference(WasmRuntime.class, "decodeData", Address.class, - Address.class, void.class); - WasmCall initDataCall = new WasmCall(WasmMangling.mangleMethod(initData)); - initDataCall.getArguments().add(new WasmInt32Constant(256)); - initDataCall.getArguments().add(new WasmInt32Constant(binaryWriter.getAddress())); - initFunction.getBody().add(initDataCall); - renderAllocatorInit(module, binaryWriter.getAddress()); renderClinit(classes, classGenerator, module); if (controller.wasCancelled()) { diff --git a/core/src/main/java/org/teavm/backend/wasm/binary/BinaryWriter.java b/core/src/main/java/org/teavm/backend/wasm/binary/BinaryWriter.java index 7b9c18be6..47a8a5055 100644 --- a/core/src/main/java/org/teavm/backend/wasm/binary/BinaryWriter.java +++ b/core/src/main/java/org/teavm/backend/wasm/binary/BinaryWriter.java @@ -120,8 +120,8 @@ public class BinaryWriter { } else if (type == DataPrimitives.SHORT) { offset = align(offset, 2); short v = value.getShort(0); - result[offset++] = (byte) (v >> 8); result[offset++] = (byte) v; + result[offset++] = (byte) (v >> 8); } else if (type == DataPrimitives.INT) { offset = writeInt(offset, result, value.getInt(0)); } else if (type == DataPrimitives.LONG) { @@ -154,10 +154,10 @@ public class BinaryWriter { private int writeInt(int offset, byte[] result, int v) { offset = align(offset, 4); - result[offset++] = (byte) (v >> 24); - result[offset++] = (byte) (v >> 16); - result[offset++] = (byte) (v >> 8); result[offset++] = (byte) v; + result[offset++] = (byte) (v >> 8); + result[offset++] = (byte) (v >> 16); + result[offset++] = (byte) (v >> 24); return offset; } @@ -173,10 +173,4 @@ public class BinaryWriter { result[offset++] = (byte) v; return offset; } - - public byte[] getMetadata() { - MetadataWriter writer = new MetadataWriter(); - writer.write(values.stream().map(value -> value.getType()).toArray(DataType[]::new)); - return writer.getData(); - } }