Fix static fields

This commit is contained in:
Alexey Andreev 2016-08-15 17:13:29 +03:00
parent 38638bc560
commit 73d4a389b7
2 changed files with 53 additions and 34 deletions

View File

@ -123,41 +123,17 @@ public class BinaryWriter {
result[offset++] = (byte) (v >> 8); result[offset++] = (byte) (v >> 8);
result[offset++] = (byte) v; result[offset++] = (byte) v;
} else if (type == DataPrimitives.INT) { } else if (type == DataPrimitives.INT) {
offset = align(offset, 4); offset = writeInt(offset, result, value.getInt(0));
int v = value.getInt(0);
result[offset++] = (byte) (v >> 24);
result[offset++] = (byte) (v >> 16);
result[offset++] = (byte) (v >> 8);
result[offset++] = (byte) v;
} else if (type == DataPrimitives.LONG) { } else if (type == DataPrimitives.LONG) {
offset = align(offset, 8); offset = writeLong(offset, result, value.getLong(0));
long v = value.getInt(0);
result[offset++] = (byte) (v >> 56);
result[offset++] = (byte) (v >> 48);
result[offset++] = (byte) (v >> 40);
result[offset++] = (byte) (v >> 32);
result[offset++] = (byte) (v >> 24);
result[offset++] = (byte) (v >> 16);
result[offset++] = (byte) (v >> 8);
result[offset++] = (byte) v;
} else if (type == DataPrimitives.FLOAT) { } else if (type == DataPrimitives.FLOAT) {
offset = align(offset, 4); int v = Float.floatToRawIntBits(value.getFloat(0));
int v = Float.floatToRawIntBits(value.getInt(0)); offset = writeInt(offset, result, v);
result[offset++] = (byte) (v >> 24);
result[offset++] = (byte) (v >> 16);
result[offset++] = (byte) (v >> 8);
result[offset++] = (byte) v;
} else if (type == DataPrimitives.DOUBLE) { } else if (type == DataPrimitives.DOUBLE) {
offset = align(offset, 8);
long v = Double.doubleToRawLongBits(value.getDouble(0)); long v = Double.doubleToRawLongBits(value.getDouble(0));
result[offset++] = (byte) (v >> 56); offset = writeLong(offset, result, v);
result[offset++] = (byte) (v >> 48); } else if (type == DataPrimitives.ADDRESS) {
result[offset++] = (byte) (v >> 40); offset = writeInt(offset, result, (int) value.getAddress(0));
result[offset++] = (byte) (v >> 32);
result[offset++] = (byte) (v >> 24);
result[offset++] = (byte) (v >> 16);
result[offset++] = (byte) (v >> 8);
result[offset++] = (byte) v;
} else if (type instanceof DataArray) { } else if (type instanceof DataArray) {
DataArray array = (DataArray) type; DataArray array = (DataArray) type;
for (int i = 0; i < array.getSize(); ++i) { for (int i = 0; i < array.getSize(); ++i) {
@ -176,6 +152,28 @@ public class BinaryWriter {
return offset; return offset;
} }
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;
return offset;
}
private int writeLong(int offset, byte[] result, long v) {
offset = align(offset, 8);
result[offset++] = (byte) (v >> 56);
result[offset++] = (byte) (v >> 48);
result[offset++] = (byte) (v >> 40);
result[offset++] = (byte) (v >> 32);
result[offset++] = (byte) (v >> 24);
result[offset++] = (byte) (v >> 16);
result[offset++] = (byte) (v >> 8);
result[offset++] = (byte) v;
return offset;
}
public byte[] getMetadata() { public byte[] getMetadata() {
MetadataWriter writer = new MetadataWriter(); MetadataWriter writer = new MetadataWriter();
writer.write(values.stream().map(value -> value.getType()).toArray(DataType[]::new)); writer.write(values.stream().map(value -> value.getType()).toArray(DataType[]::new));

View File

@ -177,9 +177,8 @@ public class WasmClassGenerator {
for (FieldReader field : cls.getFields()) { for (FieldReader field : cls.getFields()) {
int desiredAlignment = getDesiredAlignment(field.getType()); int desiredAlignment = getDesiredAlignment(field.getType());
if (field.hasModifier(ElementModifier.STATIC)) { if (field.hasModifier(ElementModifier.STATIC)) {
/*int offset = align(address, desiredAlignment); DataType type = asDataType(field.getType());
data.fieldLayout.put(field.getName(), offset); data.fieldLayout.put(field.getName(), binaryWriter.append(type.createValue()));
address = offset + desiredAlignment;*/
} else { } else {
int offset = align(data.size, desiredAlignment); int offset = align(data.size, desiredAlignment);
data.fieldLayout.put(field.getName(), offset); data.fieldLayout.put(field.getName(), offset);
@ -188,6 +187,28 @@ public class WasmClassGenerator {
} }
} }
private static DataType asDataType(ValueType type) {
if (type instanceof ValueType.Primitive) {
switch (((ValueType.Primitive) type).getKind()) {
case BOOLEAN:
case BYTE:
return DataPrimitives.BYTE;
case SHORT:
case CHARACTER:
return DataPrimitives.SHORT;
case INTEGER:
return DataPrimitives.INT;
case LONG:
return DataPrimitives.LONG;
case FLOAT:
return DataPrimitives.FLOAT;
case DOUBLE:
return DataPrimitives.DOUBLE;
}
}
return DataPrimitives.ADDRESS;
}
private static int align(int base, int alignment) { private static int align(int base, int alignment) {
if (base == 0) { if (base == 0) {
return 0; return 0;