Fix bugs in GC and runtime

This commit is contained in:
Alexey Andreev 2016-09-15 16:34:06 +03:00
parent de299dda48
commit 67098a60ef
5 changed files with 58 additions and 13 deletions

View File

@ -23,6 +23,7 @@ import org.teavm.dependency.PluggableDependency;
import org.teavm.backend.javascript.spi.GeneratedBy;
import org.teavm.interop.Address;
import org.teavm.interop.DelegateTo;
import org.teavm.interop.Import;
import org.teavm.interop.NoGC;
import org.teavm.interop.Structure;
import org.teavm.runtime.Allocator;
@ -98,8 +99,16 @@ public final class TSystem extends TObject {
}
@GeneratedBy(SystemNativeGenerator.class)
@DelegateTo(("currentTimeMillisLowLevel"))
public static native long currentTimeMillis();
private static long currentTimeMillisLowLevel() {
return (long) currentTimeMillisImpl();
}
@Import(name = "currentTimeMillis", module = "runtime")
private static native double currentTimeMillisImpl();
public static TString getProperty(@SuppressWarnings("unused") TString key) {
// TODO: make implementation
return null;

View File

@ -15,6 +15,7 @@
*/
package org.teavm.backend.wasm;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -36,9 +37,33 @@ public final class Example {
testArrayCopy();
testArrayIsObject();
testIsAssignableFrom();
testBigInteger();
testGC();
}
private static void testBigInteger() {
BigInteger result = BigInteger.ONE;
for (int j = 0; j < 100; ++j) {
long start = System.currentTimeMillis();
for (int k = 0; k < 5000; ++k) {
BigInteger a = BigInteger.ZERO;
BigInteger b = BigInteger.ONE;
for (int i = 0; i < 1000; ++i) {
BigInteger c = a.add(b);
a = b;
b = c;
}
result = a;
}
long end = System.currentTimeMillis();
println("Operation took " + (end - start) + " milliseconds");
}
println(result.toString());
}
private static void testFibonacci() {
int a = 0;
int b = 1;

View File

@ -163,14 +163,14 @@ public class BinaryWriter {
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;
result[offset++] = (byte) (v >> 8);
result[offset++] = (byte) (v >> 16);
result[offset++] = (byte) (v >> 24);
result[offset++] = (byte) (v >> 32);
result[offset++] = (byte) (v >> 40);
result[offset++] = (byte) (v >> 48);
result[offset++] = (byte) (v >> 56);
return offset;
}
}

View File

@ -78,6 +78,7 @@ public class WasmGenerator {
WasmType paramType = WasmGeneratorUtil.mapType(methodReference.parameterType(i));
function.getParameters().add(paramType);
}
function.setResult(WasmGeneratorUtil.mapType(methodReference.getReturnType()));
WasmGenerationContext.ImportedMethod importedMethod = context.getImportedMethod(methodReference);
if (importedMethod != null) {

View File

@ -59,15 +59,18 @@ public final class GC {
public static RuntimeObject alloc(int size) {
FreeChunk current = currentChunk;
Address next = currentChunk.toAddress().add(size);
if (!next.add(Structure.sizeOf(FreeChunk.class) + 1).isLessThan(currentChunkLimit)) {
if (!next.add(Structure.sizeOf(FreeChunk.class)).isLessThan(currentChunkLimit)) {
getAvailableChunk(size);
current = currentChunk;
next = currentChunk.toAddress().add(size);
}
int oldSize = current.size;
int freeSize = current.size;
currentChunk = next.toStructure();
freeSize -= size;
if (freeSize > 0) {
currentChunk.classReference = 0;
currentChunk.size = oldSize - size;
currentChunk.size = freeSize;
}
return current;
}
@ -80,7 +83,13 @@ public final class GC {
}
private static boolean getAvailableChunkIfPossible(int size) {
while (!currentChunk.toAddress().add(size).isLessThan(currentChunkLimit)) {
while (true) {
if (currentChunk.toAddress().add(size) == currentChunkLimit) {
break;
}
if (currentChunk.toAddress().add(size + Structure.sizeOf(FreeChunk.class)).isLessThan(currentChunkLimit)) {
break;
}
if (--freeChunks == 0) {
return false;
}
@ -104,10 +113,11 @@ public final class GC {
int staticCount = staticRoots.getInt();
staticRoots.add(8);
while (staticCount-- > 0) {
RuntimeObject object = staticRoots.getAddress().toStructure();
RuntimeObject object = staticRoots.getAddress().getAddress().toStructure();
if (object != null) {
mark(object);
}
staticRoots = staticRoots.add(Address.sizeOf());
}
for (Address stackRoots = Mutator.getStackGcRoots(); stackRoots != null;