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;
} else if (type == DataPrimitives.INT) {
offset = align(offset, 4);
int v = value.getInt(0);
result[offset++] = (byte) (v >> 24);
result[offset++] = (byte) (v >> 16);
result[offset++] = (byte) (v >> 8);
result[offset++] = (byte) v;
offset = writeInt(offset, result, value.getInt(0));
} else if (type == DataPrimitives.LONG) {
offset = align(offset, 8);
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;
offset = writeLong(offset, result, value.getLong(0));
} else if (type == DataPrimitives.FLOAT) {
offset = align(offset, 4);
int v = Float.floatToRawIntBits(value.getInt(0));
result[offset++] = (byte) (v >> 24);
result[offset++] = (byte) (v >> 16);
result[offset++] = (byte) (v >> 8);
result[offset++] = (byte) v;
int v = Float.floatToRawIntBits(value.getFloat(0));
offset = writeInt(offset, result, v);
} else if (type == DataPrimitives.DOUBLE) {
offset = align(offset, 8);
long v = Double.doubleToRawLongBits(value.getDouble(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;
offset = writeLong(offset, result, v);
} else if (type == DataPrimitives.ADDRESS) {
offset = writeInt(offset, result, (int) value.getAddress(0));
} else if (type instanceof DataArray) {
DataArray array = (DataArray) type;
for (int i = 0; i < array.getSize(); ++i) {
@ -176,6 +152,28 @@ public class BinaryWriter {
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() {
MetadataWriter writer = new MetadataWriter();
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()) {
int desiredAlignment = getDesiredAlignment(field.getType());
if (field.hasModifier(ElementModifier.STATIC)) {
/*int offset = align(address, desiredAlignment);
data.fieldLayout.put(field.getName(), offset);
address = offset + desiredAlignment;*/
DataType type = asDataType(field.getType());
data.fieldLayout.put(field.getName(), binaryWriter.append(type.createValue()));
} else {
int offset = align(data.size, desiredAlignment);
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) {
if (base == 0) {
return 0;