Wasm backend: fix many tests

This commit is contained in:
Alexey Andreev 2018-05-12 23:43:53 +03:00
parent 5e3514e5dd
commit f23128bb13
14 changed files with 86 additions and 25 deletions

View File

@ -234,8 +234,7 @@ public class TStreamTokenizer {
} }
} }
peekChar = currentChar; peekChar = currentChar;
sval = forceLowercase ? word.toString().toLowerCase() : word sval = forceLowercase ? word.toString().toLowerCase() : word.toString();
.toString();
ttype = TT_WORD; ttype = TT_WORD;
return ttype; return ttype;
} }

View File

@ -40,7 +40,11 @@ public class TRandom extends TObject implements TSerializable {
} }
protected int next(int bits) { protected int next(int bits) {
return (int) (nextDouble() * (1L << TMath.min(32, bits))); if (bits == 32) {
return (int) (nextDouble() * ((1L << 32) - 1) + Integer.MIN_VALUE);
} else {
return (int) (nextDouble() * (1L << TMath.min(32, bits)));
}
} }
public void nextBytes(byte[] bytes) { public void nextBytes(byte[] bytes) {

View File

@ -129,6 +129,7 @@ public final class WasmRuntime {
public static void moveMemoryBlock(Address source, Address target, int count) { public static void moveMemoryBlock(Address source, Address target, int count) {
if (count < 8) { if (count < 8) {
slowMemoryMove(source, target, count); slowMemoryMove(source, target, count);
return;
} }
int diff = source.toInt() - target.toInt(); int diff = source.toInt() - target.toInt();
if (diff == 0) { if (diff == 0) {

View File

@ -94,6 +94,7 @@ import org.teavm.model.CallLocation;
import org.teavm.model.ClassHolder; import org.teavm.model.ClassHolder;
import org.teavm.model.ClassHolderTransformer; import org.teavm.model.ClassHolderTransformer;
import org.teavm.model.ClassReader; import org.teavm.model.ClassReader;
import org.teavm.model.ClassReaderSource;
import org.teavm.model.ElementModifier; import org.teavm.model.ElementModifier;
import org.teavm.model.FieldReader; import org.teavm.model.FieldReader;
import org.teavm.model.FieldReference; import org.teavm.model.FieldReference;
@ -325,7 +326,7 @@ public class WasmTarget implements TeaVMTarget, TeaVMWasmHost {
context.addIntrinsic(new PlatformClassMetadataIntrinsic()); context.addIntrinsic(new PlatformClassMetadataIntrinsic());
context.addIntrinsic(new ClassIntrinsic()); context.addIntrinsic(new ClassIntrinsic());
IntrinsicFactoryContext intrinsicFactoryContext = new IntrinsicFactoryContext(classes); IntrinsicFactoryContext intrinsicFactoryContext = new IntrinsicFactoryContext();
for (WasmIntrinsicFactory additionalIntrinsicFactory : additionalIntrinsics) { for (WasmIntrinsicFactory additionalIntrinsicFactory : additionalIntrinsics) {
context.addIntrinsic(additionalIntrinsicFactory.create(intrinsicFactoryContext)); context.addIntrinsic(additionalIntrinsicFactory.create(intrinsicFactoryContext));
} }
@ -416,15 +417,9 @@ public class WasmTarget implements TeaVMTarget, TeaVMWasmHost {
} }
private class IntrinsicFactoryContext implements WasmIntrinsicFactoryContext { private class IntrinsicFactoryContext implements WasmIntrinsicFactoryContext {
private ListableClassReaderSource classSource;
IntrinsicFactoryContext(ListableClassReaderSource classSource) {
this.classSource = classSource;
}
@Override @Override
public ListableClassReaderSource getClassSource() { public ClassReaderSource getClassSource() {
return classSource; return controller.getUnprocessedClassSource();
} }
@Override @Override

View File

@ -175,8 +175,10 @@ public class WasmClassGenerator {
binaryData.data.setInt(CLASS_IS_INSTANCE, functionTable.size()); binaryData.data.setInt(CLASS_IS_INSTANCE, functionTable.size());
binaryData.data.setInt(CLASS_CANARY, RuntimeClass.computeCanary(4, 0)); binaryData.data.setInt(CLASS_CANARY, RuntimeClass.computeCanary(4, 0));
functionTable.add(names.forSupertypeFunction(type)); functionTable.add(names.forSupertypeFunction(type));
binaryData.data.setAddress(CLASS_NAME, stringPool.getStringPointer(type.toString()));
binaryData.data.setAddress(CLASS_SIMPLE_NAME, 0); binaryData.data.setAddress(CLASS_SIMPLE_NAME, 0);
binaryData.data.setInt(CLASS_INIT, -1); binaryData.data.setInt(CLASS_INIT, -1);
binaryData.data.setAddress(CLASS_PARENT, getClassPointer(ValueType.object("java.lang.Object")));
binaryData.start = binaryWriter.append(vtableSize > 0 ? wrapper : binaryData.data); binaryData.start = binaryWriter.append(vtableSize > 0 ? wrapper : binaryData.data);
itemBinaryData.data.setAddress(CLASS_ARRAY_TYPE, binaryData.start); itemBinaryData.data.setAddress(CLASS_ARRAY_TYPE, binaryData.start);
@ -428,7 +430,11 @@ public class WasmClassGenerator {
int desiredAlignment = getTypeSize(field.getType()); int desiredAlignment = getTypeSize(field.getType());
if (field.hasModifier(ElementModifier.STATIC)) { if (field.hasModifier(ElementModifier.STATIC)) {
DataType type = asDataType(field.getType()); DataType type = asDataType(field.getType());
data.fieldLayout.put(field.getName(), binaryWriter.append(type.createValue())); DataValue value = type.createValue();
if (field.getInitialValue() != null) {
setInitialValue(field.getType(), value, field.getInitialValue());
}
data.fieldLayout.put(field.getName(), binaryWriter.append(value));
} 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);
@ -440,6 +446,41 @@ public class WasmClassGenerator {
} }
} }
private void setInitialValue(ValueType type, DataValue data, Object value) {
if (value instanceof Number) {
switch (((ValueType.Primitive) type).getKind()) {
case BYTE:
data.setByte(0, ((Number) value).byteValue());
break;
case SHORT:
data.setShort(0, ((Number) value).shortValue());
break;
case CHARACTER:
data.setShort(0, ((Number) value).shortValue());
break;
case INTEGER:
data.setInt(0, ((Number) value).intValue());
break;
case LONG:
data.setLong(0, ((Number) value).longValue());
break;
case FLOAT:
data.setFloat(0, ((Number) value).floatValue());
break;
case DOUBLE:
data.setDouble(0, ((Number) value).doubleValue());
break;
case BOOLEAN:
data.setByte(0, ((Number) value).byteValue());
break;
}
} else if (value instanceof Boolean) {
data.setByte(0, (Boolean) value ? (byte) 1 : 0);
} else if (value instanceof String) {
data.setAddress(0, stringPool.getStringPointer((String) value));
}
}
private static DataType asDataType(ValueType type) { private static DataType asDataType(ValueType type) {
if (type instanceof ValueType.Primitive) { if (type instanceof ValueType.Primitive) {
switch (((ValueType.Primitive) type).getKind()) { switch (((ValueType.Primitive) type).getKind()) {

View File

@ -813,7 +813,7 @@ class WasmGenerationVisitor implements StatementVisitor, ExprVisitor {
private void translateSwitchToWasmSwitch(SwitchStatement statement, WasmExpression condition, private void translateSwitchToWasmSwitch(SwitchStatement statement, WasmExpression condition,
WasmBlock initialWrapper, WasmBlock defaultTarget, WasmBlock[] targets, int min, int max) { WasmBlock initialWrapper, WasmBlock defaultTarget, WasmBlock[] targets, int min, int max) {
if (min > 0) { if (min != 0) {
condition = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.SUB, condition, condition = new WasmIntBinary(WasmIntType.INT32, WasmIntBinaryOperation.SUB, condition,
new WasmInt32Constant(min)); new WasmInt32Constant(min));
} }

View File

@ -34,7 +34,6 @@ public class PlatformIntrinsic implements WasmIntrinsic {
switch (methodDescriptor.getName()) { switch (methodDescriptor.getName()) {
case "getPlatformObject": case "getPlatformObject":
case "asJavaClass": case "asJavaClass":
case "getName":
case "createQueue": case "createQueue":
return true; return true;
default: default:
@ -48,7 +47,6 @@ public class PlatformIntrinsic implements WasmIntrinsic {
case "getPlatformObject": case "getPlatformObject":
case "asJavaClass": case "asJavaClass":
return manager.generate(invocation.getArguments().get(0)); return manager.generate(invocation.getArguments().get(0));
case "getName":
case "createQueue": case "createQueue":
return new WasmInt32Constant(0); return new WasmInt32Constant(0);
default: default:

View File

@ -17,10 +17,10 @@ package org.teavm.backend.wasm.intrinsics;
import java.util.Properties; import java.util.Properties;
import org.teavm.common.ServiceRepository; import org.teavm.common.ServiceRepository;
import org.teavm.model.ListableClassReaderSource; import org.teavm.model.ClassReaderSource;
public interface WasmIntrinsicFactoryContext { public interface WasmIntrinsicFactoryContext {
ListableClassReaderSource getClassSource(); ClassReaderSource getClassSource();
ClassLoader getClassLoader(); ClassLoader getClassLoader();

View File

@ -308,12 +308,24 @@ class WasmCRenderingVisitor implements WasmExpressionVisitor {
@Override @Override
public void visit(WasmFloat32Constant expression) { public void visit(WasmFloat32Constant expression) {
value = CExpression.relocatable(Float.toHexString(expression.getValue()) + "F"); if (Float.isInfinite(expression.getValue())) {
value = CExpression.relocatable(expression.getValue() < 0 ? "-INFINITY" : "INFINITY");
} else if (Float.isNaN(expression.getValue())) {
value = CExpression.relocatable("NAN");
} else {
value = CExpression.relocatable(Float.toHexString(expression.getValue()) + "F");
}
} }
@Override @Override
public void visit(WasmFloat64Constant expression) { public void visit(WasmFloat64Constant expression) {
value = CExpression.relocatable(Double.toHexString(expression.getValue())); if (Double.isInfinite(expression.getValue())) {
value = CExpression.relocatable(expression.getValue() < 0 ? "-INFINITY" : "INFINITY");
} else if (Double.isNaN(expression.getValue())) {
value = CExpression.relocatable("NAN");
} else {
value = CExpression.relocatable(Double.toHexString(expression.getValue()));
}
} }
@Override @Override

View File

@ -78,7 +78,9 @@ public class VirtualTableProvider {
Set<MethodDescriptor> newDescriptors = virtualMethodMap.get(className); Set<MethodDescriptor> newDescriptors = virtualMethodMap.get(className);
if (newDescriptors != null) { if (newDescriptors != null) {
for (MethodDescriptor method : newDescriptors) { for (MethodDescriptor method : newDescriptors) {
table.entries.put(method, new VirtualTableEntry(table, method, null, table.entries.size())); if (!table.entries.containsKey(method)) {
table.entries.put(method, new VirtualTableEntry(table, method, null, table.entries.size()));
}
} }
} }

View File

@ -6,7 +6,15 @@
#include <math.h> #include <math.h>
#include <wchar.h> #include <wchar.h>
#include <wctype.h> #include <wctype.h>
#include <time.h>
static inline float TeaVM_getNaN() { static inline float TeaVM_getNaN() {
return NAN; return NAN;
} }
static int64_t currentTimeMillis() {
struct timespec time;
clock_gettime(CLOCK_REALTIME, &time);
return time.tv_sec * 1000 + (int64_t) round(time.tv_nsec / 1000000);
}

View File

@ -227,6 +227,7 @@ public final class Platform {
return cls.getMetadata().getArrayItem(); return cls.getMetadata().getArrayItem();
} }
@Unmanaged
public static String getName(PlatformClass cls) { public static String getName(PlatformClass cls) {
return cls.getMetadata().getName(); return cls.getMetadata().getName();
} }

View File

@ -33,7 +33,7 @@ import org.teavm.backend.wasm.model.expression.WasmExpression;
import org.teavm.backend.wasm.model.expression.WasmInt32Constant; import org.teavm.backend.wasm.model.expression.WasmInt32Constant;
import org.teavm.common.ServiceRepository; import org.teavm.common.ServiceRepository;
import org.teavm.model.CallLocation; import org.teavm.model.CallLocation;
import org.teavm.model.ListableClassReaderSource; import org.teavm.model.ClassReaderSource;
import org.teavm.model.MethodReader; import org.teavm.model.MethodReader;
import org.teavm.model.MethodReference; import org.teavm.model.MethodReference;
import org.teavm.platform.metadata.MetadataGenerator; import org.teavm.platform.metadata.MetadataGenerator;
@ -41,13 +41,13 @@ import org.teavm.platform.metadata.MetadataProvider;
import org.teavm.platform.metadata.Resource; import org.teavm.platform.metadata.Resource;
public class MetadataIntrinsic implements WasmIntrinsic { public class MetadataIntrinsic implements WasmIntrinsic {
private ListableClassReaderSource classSource; private ClassReaderSource classSource;
private ClassLoader classLoader; private ClassLoader classLoader;
private ServiceRepository services; private ServiceRepository services;
private Properties properties; private Properties properties;
private Map<ResourceTypeDescriptor, DataStructure> resourceTypeCache = new HashMap<>(); private Map<ResourceTypeDescriptor, DataStructure> resourceTypeCache = new HashMap<>();
public MetadataIntrinsic(ListableClassReaderSource classSource, ClassLoader classLoader, public MetadataIntrinsic(ClassReaderSource classSource, ClassLoader classLoader,
ServiceRepository services, Properties properties) { ServiceRepository services, Properties properties) {
this.classSource = classSource; this.classSource = classSource;
this.classLoader = classLoader; this.classLoader = classLoader;

View File

@ -287,7 +287,7 @@ public class StreamTokenizerTest {
public void test_toString() throws IOException { public void test_toString() throws IOException {
setTest("ABC Hello World"); setTest("ABC Hello World");
st.nextToken(); st.nextToken();
assertTrue("toString failed." + st.toString(), st.toString().equals("Token[ABC], line 1")); assertEquals("toString failed.", "Token[ABC], line 1", st.toString());
// Regression test for HARMONY-4070 // Regression test for HARMONY-4070
byte[] data = new byte[] { (byte) '-' }; byte[] data = new byte[] { (byte) '-' };