-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create InterpreterProxy and extract inner classes
- Loading branch information
Showing
4 changed files
with
225 additions
and
145 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
...pi.swa.trufflesqueak/src/de/hpi/swa/trufflesqueak/nodes/plugins/ffi/InterpreterProxy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package de.hpi.swa.trufflesqueak.nodes.plugins.ffi; | ||
|
||
import com.oracle.truffle.api.frame.VirtualFrame; | ||
import com.oracle.truffle.api.interop.ArityException; | ||
import com.oracle.truffle.api.interop.InteropLibrary; | ||
import com.oracle.truffle.api.interop.UnknownIdentifierException; | ||
import com.oracle.truffle.api.interop.UnsupportedMessageException; | ||
import com.oracle.truffle.api.interop.UnsupportedTypeException; | ||
import de.hpi.swa.trufflesqueak.exceptions.PrimitiveFailed; | ||
import de.hpi.swa.trufflesqueak.image.SqueakImageContext; | ||
import de.hpi.swa.trufflesqueak.model.NativeObject; | ||
import de.hpi.swa.trufflesqueak.nodes.plugins.ffi.wrappers.ByteStorage; | ||
import de.hpi.swa.trufflesqueak.util.FrameAccess; | ||
import de.hpi.swa.trufflesqueak.util.NFIUtils; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class InterpreterProxy { | ||
|
||
private final VirtualFrame frame; | ||
private final ArrayList<Object> objectRegistry = new ArrayList<>(); | ||
private final ArrayList<de.hpi.swa.trufflesqueak.nodes.plugins.ffi.InterpreterProxy.PostPrimitiveCleanup> postPrimitiveCleanups = new ArrayList<>(); | ||
|
||
private static Object interpreterProxyPointer = null; | ||
|
||
public InterpreterProxy(SqueakImageContext context, VirtualFrame frame) throws UnsupportedMessageException, UnknownIdentifierException, UnsupportedTypeException, ArityException { | ||
this.frame = frame; | ||
if (interpreterProxyPointer == null) { | ||
final Object interpreterProxy = NFIUtils.loadLibrary( | ||
context, | ||
"InterpreterProxy.so", | ||
"{ createInterpreterProxy((SINT64):SINT64,(SINT64):POINTER,(SINT64):SINT64,():SINT64,():SINT64,():SINT64,():SINT64,(SINT64):SINT64):POINTER; }" | ||
); | ||
|
||
final InteropLibrary interpreterProxyLibrary = NFIUtils.getInteropLibrary(interpreterProxy); | ||
interpreterProxyPointer = interpreterProxyLibrary.invokeMember( | ||
interpreterProxy,"createInterpreterProxy", (Object[]) getExecutables()); | ||
} | ||
} | ||
public Object getPointer() { | ||
return interpreterProxyPointer; | ||
} | ||
|
||
public NFIUtils.TruffleExecutable[] getExecutables() { | ||
return new NFIUtils.TruffleExecutable[] { | ||
NFIUtils.TruffleExecutable.wrap(this::byteSizeOf), | ||
NFIUtils.TruffleExecutable.wrap(this::firstIndexableField), | ||
NFIUtils.TruffleExecutable.wrap(this::isBytes), | ||
NFIUtils.TruffleExecutable.wrap(this::majorVersion), | ||
NFIUtils.TruffleExecutable.wrap(this::methodArgumentCount), | ||
NFIUtils.TruffleExecutable.wrap(this::minorVersion), | ||
NFIUtils.TruffleExecutable.wrap(this::primitiveFail), | ||
NFIUtils.TruffleExecutable.wrap(this::stackValue), | ||
}; | ||
} | ||
public void postPrimitiveCleanups() { | ||
postPrimitiveCleanups.forEach(de.hpi.swa.trufflesqueak.nodes.plugins.ffi.InterpreterProxy.PostPrimitiveCleanup::cleanup); | ||
} | ||
|
||
private int byteSizeOf() { | ||
return 16; | ||
} | ||
private ByteStorage firstIndexableField(long index) { | ||
byte[] storage = ((NativeObject) objectRegistry.get((int) index)).getByteStorage(); | ||
ByteStorage byteStorage = new ByteStorage(storage); | ||
postPrimitiveCleanups.add(byteStorage); | ||
return byteStorage; | ||
} | ||
private int isBytes(long oop) { | ||
return 1; | ||
} | ||
private int majorVersion() { | ||
return 1; | ||
} | ||
private int methodArgumentCount() { | ||
return FrameAccess.getNumArguments(frame); | ||
} | ||
private int minorVersion() { | ||
return 17; | ||
} | ||
private int primitiveFail() { | ||
throw PrimitiveFailed.GENERIC_ERROR; | ||
} | ||
private int stackValue(long stackIndex) { | ||
Object objectOnStack = FrameAccess.getStackValue(frame, (int) stackIndex, FrameAccess.getNumArguments(frame)); | ||
int objectIndex = objectRegistry.size(); | ||
objectRegistry.add(objectOnStack); | ||
return objectIndex; | ||
} | ||
|
||
public interface PostPrimitiveCleanup { | ||
void cleanup(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...wa.trufflesqueak/src/de/hpi/swa/trufflesqueak/nodes/plugins/ffi/wrappers/ByteStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package de.hpi.swa.trufflesqueak.nodes.plugins.ffi.wrappers; | ||
|
||
import com.oracle.truffle.api.interop.InteropLibrary; | ||
import com.oracle.truffle.api.interop.TruffleObject; | ||
import com.oracle.truffle.api.library.ExportLibrary; | ||
import com.oracle.truffle.api.library.ExportMessage; | ||
import de.hpi.swa.trufflesqueak.nodes.plugins.ffi.InterpreterProxy.PostPrimitiveCleanup; | ||
import de.hpi.swa.trufflesqueak.util.UnsafeUtils; | ||
|
||
@ExportLibrary(InteropLibrary.class) | ||
public class ByteStorage implements PostPrimitiveCleanup, TruffleObject { | ||
byte[] storage; | ||
public long nativeAddress; | ||
|
||
public ByteStorage(byte[] storage) { | ||
this.storage = storage; | ||
nativeAddress = UnsafeUtils.allocateNativeBytes(storage); | ||
} | ||
|
||
@ExportMessage | ||
public boolean isPointer() { | ||
return true; | ||
} | ||
|
||
@ExportMessage | ||
public long asPointer() { | ||
return nativeAddress; | ||
} | ||
|
||
@Override | ||
public void cleanup() { | ||
UnsafeUtils.copyNativeBytesBackAndFree(nativeAddress, storage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.