Use little-endian data section, remove metainformation section

This commit is contained in:
Alexey Andreev 2016-09-03 21:31:10 +03:00
parent 09d8b581c0
commit de4128b377
3 changed files with 6 additions and 143 deletions

View File

@ -19,8 +19,6 @@ import org.teavm.interop.Address;
import org.teavm.interop.Import; import org.teavm.interop.Import;
public final class WasmRuntime { public final class WasmRuntime {
private static Address offset;
private WasmRuntime() { private WasmRuntime() {
} }
@ -64,108 +62,6 @@ public final class WasmRuntime {
private static native boolean gt(double a, double b); 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) { public static Address align(Address address, int alignment) {
int value = address.toInt(); int value = address.toInt();
if (value == 0) { if (value == 0) {
@ -175,21 +71,6 @@ public final class WasmRuntime {
return Address.fromInt(value); 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") @Import(name = "print", module = "spectest")
public static native void print(int a); public static native void print(int a);

View File

@ -160,8 +160,8 @@ public class WasmTarget implements TeaVMTarget {
dependencyChecker.linkMethod(method, null).use(); dependencyChecker.linkMethod(method, null).use();
} }
dependencyChecker.linkMethod(new MethodReference(WasmRuntime.class, "decodeData", Address.class, dependencyChecker.linkMethod(new MethodReference(WasmRuntime.class, "align", Address.class, int.class,
Address.class, void.class), null).use(); Address.class), null).use();
dependencyChecker.linkMethod(new MethodReference(WasmRuntime.class, "fillZero", Address.class, int.class, dependencyChecker.linkMethod(new MethodReference(WasmRuntime.class, "fillZero", Address.class, int.class,
void.class), null).use(); void.class), null).use();
dependencyChecker.linkMethod(new MethodReference(WasmRuntime.class, "moveMemoryBlock", Address.class, dependencyChecker.linkMethod(new MethodReference(WasmRuntime.class, "moveMemoryBlock", Address.class,
@ -227,18 +227,6 @@ public class WasmTarget implements TeaVMTarget {
dataSegment.setOffset(256); dataSegment.setOffset(256);
module.getSegments().add(dataSegment); 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()); renderAllocatorInit(module, binaryWriter.getAddress());
renderClinit(classes, classGenerator, module); renderClinit(classes, classGenerator, module);
if (controller.wasCancelled()) { if (controller.wasCancelled()) {

View File

@ -120,8 +120,8 @@ public class BinaryWriter {
} else if (type == DataPrimitives.SHORT) { } else if (type == DataPrimitives.SHORT) {
offset = align(offset, 2); offset = align(offset, 2);
short v = value.getShort(0); short v = value.getShort(0);
result[offset++] = (byte) (v >> 8);
result[offset++] = (byte) v; result[offset++] = (byte) v;
result[offset++] = (byte) (v >> 8);
} else if (type == DataPrimitives.INT) { } else if (type == DataPrimitives.INT) {
offset = writeInt(offset, result, value.getInt(0)); offset = writeInt(offset, result, value.getInt(0));
} else if (type == DataPrimitives.LONG) { } else if (type == DataPrimitives.LONG) {
@ -154,10 +154,10 @@ public class BinaryWriter {
private int writeInt(int offset, byte[] result, int v) { private int writeInt(int offset, byte[] result, int v) {
offset = align(offset, 4); 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;
result[offset++] = (byte) (v >> 8);
result[offset++] = (byte) (v >> 16);
result[offset++] = (byte) (v >> 24);
return offset; return offset;
} }
@ -173,10 +173,4 @@ public class BinaryWriter {
result[offset++] = (byte) v; result[offset++] = (byte) v;
return offset; return offset;
} }
public byte[] getMetadata() {
MetadataWriter writer = new MetadataWriter();
writer.write(values.stream().map(value -> value.getType()).toArray(DataType[]::new));
return writer.getData();
}
} }