Skip to content

Commit

Permalink
#154 Use F64/I64 Datatypes for Double and Long values in WASM Backend (
Browse files Browse the repository at this point in the history
…#394)

* #154 Use F64/I64 Datatypes for Double and Long values in WASM Backend
  • Loading branch information
mirkosertic authored May 4, 2020
1 parent 2f48759 commit 485e38e
Show file tree
Hide file tree
Showing 29 changed files with 1,005 additions and 365 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public static int getGCEpoch() {
}

@Export("freeMem")
public static long freeMem() {
long theResult = 0;
public static int freeMem() {
int theResult = 0;

final int heapBase = Address.getHeapBase();

Expand All @@ -77,8 +77,8 @@ public static long freeMem() {
}

@Export("usedMem")
public static long usedMem() {
long theResult = 0;
public static int usedMem() {
int theResult = 0;

final int heapBase = Address.getHeapBase();

Expand Down Expand Up @@ -337,7 +337,7 @@ public static int IncrementalGC(final int blockLimit) {
public static int newArray(final int aSize, final int aType, final int aVTableIndex) {

// Arrays are normal objects. Their data are a length field plus n * data
final int theObject = newObject(16 + 4 + 4 * aSize, aType, aVTableIndex);
final int theObject = newObject(16 + 4 + 8 * aSize, aType, aVTableIndex);

Address.setIntValue(theObject, 16, aSize);
return theObject;
Expand All @@ -347,7 +347,7 @@ public static int newArray(final int aSize, final int aType, final int aVTableIn
public static int newArray(final int aSize1, final int aSize2, final int aType, final int aVTableIndex) {
final int theResult = newArray(aSize1, aType, aVTableIndex);
for (int i=0;i<aSize1;i++) {
final int theOffset = 16 + 4 + 4 * i;
final int theOffset = 16 + 4 + 8 * i;
final int theSubArray = newArray(aSize2, aType, aVTableIndex);
Address.setIntValue(theResult, theOffset, theSubArray);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,20 +274,32 @@ public static Locale defaultLocale() {
public static native boolean isLong(final MethodType aType, final int aParamIndex);

@EmulatedByRuntime
public static native int reinterpretAsInt(final Object o);
public static native boolean isShort(final MethodType aType, final int aParamIndex);

@EmulatedByRuntime
public static native long reinterpretAsLong(final Object o);
public static native boolean isByte(final MethodType aType, final int aParamIndex);

@EmulatedByRuntime
public static native float reinterpretAsFloat(final Object o);
public static native long arrayEntryAsLong(final Object[] aObject, final int index);

@EmulatedByRuntime
public static native double reinterpretAsDouble(final Object o);
public static native int arrayEntryAsInt(final Object[] aObject, final int index);

@EmulatedByRuntime
public static native char reinterpretAsChar(final Object o);
public static native float arrayEntryAsFloat(final Object[] aObject, final int index);

@EmulatedByRuntime
public static native boolean reinterpretAsBoolean(final Object o);
public static native double arrayEntryAsDouble(final Object[] aObject, final int index);

@EmulatedByRuntime
public static native char arrayEntryAsChar(final Object[] aObject, final int index);

@EmulatedByRuntime
public static native char arrayEntryAsBoolean(final Object[] aObject, final int index);

@EmulatedByRuntime
public static native short arrayEntryAsShort(final Object[] aObject, final int index);

@EmulatedByRuntime
public static native byte arrayEntryAsByte(final Object[] aObject, final int index);
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public boolean valid() {
}

@Export("initDefaultFileHandles")
public static void initDefaultFileHandles(final long stdIn, final long stdOut, final long stdErr) {
public static void initDefaultFileHandles(final int stdIn, final int stdOut, final int stdErr) {
((TFileDescriptor) (Object) FileDescriptor.in).setHandle(stdIn);
((TFileDescriptor) (Object) FileDescriptor.out).setHandle(stdOut);
((TFileDescriptor) (Object) FileDescriptor.err).setHandle(stdErr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public TFileInputStream(final FileDescriptor fdObj) {
* Opens the specified file for reading.
* @param name the name of the file
*/
private native long open0(String name) throws FileNotFoundException;
private native int open0(String name) throws FileNotFoundException;

// wrap native call to allow instrumentation
/**
Expand All @@ -79,37 +79,37 @@ private void open(final String name) throws FileNotFoundException {
}

public int read() throws IOException {
final long handle = ((TFileDescriptor) (Object) fd).getHandle();
final int handle = (int) ((TFileDescriptor) (Object) fd).getHandle();
return read0(handle);
}

private native int read0(long handle) throws IOException;
private native int read0(int handle) throws IOException;

private native int readBytes(long handle, byte b[], int off, int len) throws IOException;
private native int readBytes(int handle, byte b[], int off, int len) throws IOException;

public int read(final byte[] b) throws IOException {
final long handle = ((TFileDescriptor) (Object) fd).getHandle();
final int handle = (int) ((TFileDescriptor) (Object) fd).getHandle();
return readBytes(handle, b, 0, b.length);
}

public int read(final byte[] b, final int off, final int len) throws IOException {
final long handle = ((TFileDescriptor) (Object) fd).getHandle();
final int handle = (int) ((TFileDescriptor) (Object) fd).getHandle();
return readBytes(handle, b, off, len);
}

public long skip(final long n) throws IOException {
final long handle = ((TFileDescriptor) (Object) fd).getHandle();
return skip0(handle, n);
final int handle = (int) ((TFileDescriptor) (Object) fd).getHandle();
return skip0(handle, (int) n);
}

private native long skip0(long handle, long n) throws IOException;
private native long skip0(int handle, int n) throws IOException;

public int available() throws IOException {
final long handle = ((TFileDescriptor) (Object) fd).getHandle();
final int handle = (int) ((TFileDescriptor) (Object) fd).getHandle();
return available0(handle);
}

private native int available0(long handle) throws IOException;
private native int available0(int handle) throws IOException;

public void close() {
if (closed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,38 +60,38 @@ public TFileOutputStream(final FileDescriptor fdObj) {
this.path = null;
}

private native long open0(String name, boolean append) throws FileNotFoundException;
private native int open0(String name, boolean append) throws FileNotFoundException;

private native void close0(long handle);
private native void close0(int handle);

private void open(final String name, final boolean append) throws FileNotFoundException {
((TFileDescriptor) (Object) fd).setHandle(open0(name, append));
}

public void write(final int b) throws IOException {
final long handle = ((TFileDescriptor) (Object) fd).getHandle();
final int handle = (int) ((TFileDescriptor) (Object) fd).getHandle();
writeInt(handle, b);
}

public native void writeInt(long handle, int b) throws IOException;
public native void writeInt(int handle, int b) throws IOException;

private native void writeBytes(long handle, byte b[], int off, int len);
private native void writeBytes(int handle, byte b[], int off, int len);

public void write(final byte[] b) {
final long handle = ((TFileDescriptor) (Object) fd).getHandle();
final int handle = (int) ((TFileDescriptor) (Object) fd).getHandle();
writeBytes(handle, b, 0, b.length);
}

public void write(final byte[] b, final int off, final int len) {
final long handle = ((TFileDescriptor) (Object) fd).getHandle();
final int handle = (int) ((TFileDescriptor) (Object) fd).getHandle();
writeBytes(handle, b, off, len);
}

public void close() {
if (closed) {
return;
}
final long handle = ((TFileDescriptor) (Object) fd).getHandle();
final int handle = (int) ((TFileDescriptor) (Object) fd).getHandle();
close0(handle);
closed = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package de.mirkosertic.bytecoder.classlib.java.lang;

import de.mirkosertic.bytecoder.api.EmulatedByRuntime;
import de.mirkosertic.bytecoder.api.SubstitutesInClass;

@SubstitutesInClass(completeReplace = true)
Expand Down Expand Up @@ -52,12 +53,15 @@ public static int abs(final int a) {
return a;
}

@EmulatedByRuntime
public static native double sqrt(double aValue);

public static native double cbrt(double aValue);

@EmulatedByRuntime
public static native double ceil(double aValue);

@EmulatedByRuntime
public static native double floor(double aValue);

public static native double sin(double aValue);
Expand All @@ -72,20 +76,28 @@ public static int abs(final int a) {

public static native double tan(double aValue);

@EmulatedByRuntime
public static native long max(long aValue1, long aValue2);

@EmulatedByRuntime
public static native int max(int aValue1, int aValue2);

@EmulatedByRuntime
public static native float max(float aValue1, float aValue2);

@EmulatedByRuntime
public static native double max(double aValue1, double aValue2);

@EmulatedByRuntime
public static native int min(int aValue1, int aValue2);

@EmulatedByRuntime
public static native long min(long aValue1, long aValue2);

@EmulatedByRuntime
public static native float min(float aValue1, float aValue2);

@EmulatedByRuntime
public static native double min(double aValue1, double aValue2);

public static int getExponent(float f) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
*/
package de.mirkosertic.bytecoder.classlib.java.lang;

import de.mirkosertic.bytecoder.api.EmulatedByRuntime;
import de.mirkosertic.bytecoder.api.SubstitutesInClass;

@SubstitutesInClass(completeReplace = true)
public class TStrictMath {

@EmulatedByRuntime
public static native double sqrt(double aValue);

public static native double sin(double aValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ public StringBuilder append(long aValue) {
}
final char[] theCharacters = new char[20];
int theOffset = 0;

do {
final int theRemainder = (int) aValue % 10;
final int theRemainder = (int) (aValue % 10);
theCharacters[theOffset++] = Character.forDigit(theRemainder, 10);
aValue = aValue / 10;
} while (aValue > 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,33 @@ public Object invokeExact(final Object... args) throws Throwable {
final StringBuilder theResult = new StringBuilder();
if (args != null) {
for (int i=0;i<args.length;i++) {
appendTo(theResult, args[i], aConcatType, i);
appendTo(theResult, args, i, aConcatType, i);
}
}
return theResult.toString();
}
};
}

private static void appendTo(final StringBuilder aTarget, final Object aValue, final MethodType aType, final int aIndex) {
private static void appendTo(final StringBuilder aTarget, final Object[] aArray, final int aArrayIndex, final MethodType aType, final int aIndex) {
if (VM.isInteger(aType, aIndex)) {
aTarget.append(VM.reinterpretAsInt(aValue));
aTarget.append(VM.arrayEntryAsInt(aArray, aArrayIndex));
} else if (VM.isLong(aType, aIndex)) {
aTarget.append(VM.reinterpretAsLong(aValue));
aTarget.append(VM.arrayEntryAsLong(aArray, aArrayIndex));
} else if (VM.isFloat(aType, aIndex)) {
aTarget.append(VM.reinterpretAsFloat(aValue));
aTarget.append(VM.arrayEntryAsFloat(aArray, aArrayIndex));
} else if (VM.isDouble(aType, aIndex)) {
aTarget.append(VM.reinterpretAsDouble(aValue));
aTarget.append(VM.arrayEntryAsDouble(aArray, aArrayIndex));
} else if (VM.isBoolean(aType, aIndex)) {
aTarget.append(VM.reinterpretAsBoolean(aValue));
aTarget.append(VM.arrayEntryAsBoolean(aArray, aArrayIndex));
} else if (VM.isChar(aType, aIndex)) {
aTarget.append(VM.reinterpretAsChar(aValue));
aTarget.append(VM.arrayEntryAsChar(aArray, aArrayIndex));
} else if (VM.isShort(aType, aIndex)) {
aTarget.append(VM.arrayEntryAsShort(aArray, aArrayIndex));
} else if (VM.isByte(aType, aIndex)) {
aTarget.append(VM.arrayEntryAsByte(aArray, aArrayIndex));
} else {
aTarget.append(aValue);
aTarget.append(aArray[aArrayIndex]);
}
}

Expand All @@ -71,9 +75,9 @@ public Object invokeExact(final Object... args) throws Throwable {
for (int i=0;i<aRecipe.length();i++) {
final char theChar = aRecipe.charAt(i);
if (theChar == 1) {
appendTo(theResult, args[theDynIndex++], aConcatType, totalIndex++);
appendTo(theResult, args, theDynIndex++, aConcatType, totalIndex++);
} else if (theChar == 2) {
appendTo(theResult, aConstants[theConstIndex++], aConcatType, totalIndex++);
appendTo(theResult, aConstants, theConstIndex++, aConcatType, totalIndex++);
} else {
theResult.append(theChar);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class NativeMemoryLayouter {

public static final int CLASS_HEADER_SIZE = 24; // Object header plus initialization status + enum values offset + classname + typeid
public static final int OBJECT_HEADER_SIZE = 8;
public static final int OBJECT_FIELDSIZE = 4;

public interface MemoryLayout {

Expand All @@ -44,9 +43,11 @@ public interface MemoryLayout {
}

private final Map<BytecodeObjectTypeRef, BytecodeResolvedFields> fields;
private final int fieldSize;

public NativeMemoryLayouter(final BytecodeLinkerContext aLinkerContext) {
public NativeMemoryLayouter(final BytecodeLinkerContext aLinkerContext, final int aFieldSize) {
fields = new HashMap<>();
fieldSize = aFieldSize;
aLinkerContext.linkedClasses().forEach(aEntry -> registerClass(aEntry.targetNode()));
}

Expand All @@ -62,13 +63,13 @@ public MemoryLayout layoutFor(final BytecodeObjectTypeRef aType) {
@Override
public int instanceSize() {
final BytecodeResolvedFields theInstanceFields = fields.get(aType);
return OBJECT_HEADER_SIZE + OBJECT_FIELDSIZE * (int) theInstanceFields.streamForInstanceFields().count();
return OBJECT_HEADER_SIZE + fieldSize * (int) theInstanceFields.streamForInstanceFields().count();
}

@Override
public int classSize() {
final BytecodeResolvedFields theClassFields = fields.get(aType);
return CLASS_HEADER_SIZE + OBJECT_FIELDSIZE * (int) theClassFields.streamForStaticFields().count();
return CLASS_HEADER_SIZE + fieldSize * (int) theClassFields.streamForStaticFields().count();
}

@Override
Expand All @@ -80,7 +81,7 @@ public int offsetForInstanceMember(final String aName) {
if (Objects.equals(aName, theField.getValue().getName().stringValue())) {
return theOffset;
}
theOffset+= OBJECT_FIELDSIZE;
theOffset+= fieldSize;
}
throw new IllegalArgumentException("Member field " + aName + " not found for type " + aType.name());
}
Expand All @@ -94,7 +95,7 @@ public int offsetForClassMember(final String aName) {
if (Objects.equals(aName, theField.getValue().getName().stringValue())) {
return theOffset;
}
theOffset+= OBJECT_FIELDSIZE;
theOffset+= fieldSize;
}
throw new IllegalArgumentException("Static field " + aName + " not found for type " + aType.name());
}
Expand Down
Loading

0 comments on commit 485e38e

Please sign in to comment.