Skip to content

Commit

Permalink
return j.l.String as managed pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasstadler committed Oct 30, 2019
1 parent f426cf3 commit e48249e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,33 @@
*/
package com.oracle.truffle.llvm.runtime.nodes.intrinsics.interop;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.interop.LLVMPolyglotFromString.ReadBytesNode;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.interop.LLVMReadCharsetNode.LLVMCharset;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.llvm.LLVMIntrinsic;
import com.oracle.truffle.llvm.runtime.nodes.memory.LLVMGetElementPtrNode.LLVMIncrementPointerNode;
import com.oracle.truffle.llvm.runtime.nodes.api.LLVMLoadNode;
import com.oracle.truffle.llvm.runtime.nodes.api.LLVMExpressionNode;
import com.oracle.truffle.llvm.runtime.nodes.api.LLVMLoadNode;
import com.oracle.truffle.llvm.runtime.nodes.api.LLVMNode;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.interop.LLVMPolyglotFromString.ReadBytesNode;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.interop.LLVMPolyglotFromStringNodeGen.PutCharNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.interop.LLVMPolyglotFromStringNodeGen.ReadBytesWithLengthNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.interop.LLVMPolyglotFromStringNodeGen.ReadZeroTerminatedBytesNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.interop.LLVMReadCharsetNode.LLVMCharset;
import com.oracle.truffle.llvm.runtime.nodes.intrinsics.llvm.LLVMIntrinsic;
import com.oracle.truffle.llvm.runtime.nodes.memory.LLVMGetElementPtrNode.LLVMIncrementPointerNode;
import com.oracle.truffle.llvm.runtime.nodes.memory.LLVMGetElementPtrNodeGen.LLVMIncrementPointerNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.memory.load.LLVMI16LoadNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.memory.load.LLVMI32LoadNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.memory.load.LLVMI64LoadNodeGen;
import com.oracle.truffle.llvm.runtime.nodes.memory.load.LLVMI8LoadNodeGen;
import com.oracle.truffle.llvm.runtime.pointer.LLVMManagedPointer;
import com.oracle.truffle.llvm.runtime.pointer.LLVMPointer;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

@NodeChild(value = "charset", type = LLVMReadCharsetNode.class)
@NodeChild(value = "rawString", type = ReadBytesNode.class, executeWith = "charset")
public abstract class LLVMPolyglotFromString extends LLVMIntrinsic {
Expand All @@ -72,8 +73,8 @@ public static LLVMPolyglotFromString createN(LLVMExpressionNode string, LLVMExpr
}

@Specialization
String doFromString(LLVMCharset charset, ByteBuffer rawString) {
return charset.decode(rawString);
LLVMManagedPointer doFromString(LLVMCharset charset, ByteBuffer rawString) {
return LLVMManagedPointer.create(charset.decode(rawString));
}

abstract static class ReadBytesNode extends LLVMNode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ String readString(String address) {
return address;
}

@Specialization(guards = "isString(foreign)")
String readString(LLVMManagedPointer foreign) {
return (String) foreign.getObject();
}

@Specialization(guards = "isForeign(foreign)")
String readForeign(LLVMManagedPointer foreign,
@Cached("create()") ForeignReadStringNode read) {
Expand All @@ -79,6 +84,10 @@ String readOther(Object address) {
return readOther.execute(address);
}

protected static boolean isString(LLVMManagedPointer pointer) {
return pointer.getOffset() == 0 && pointer.getObject() instanceof String;
}

protected static boolean isForeign(LLVMManagedPointer pointer) {
return pointer.getOffset() == 0 && pointer.getObject() instanceof LLVMTypedForeignObject;
}
Expand Down

0 comments on commit e48249e

Please sign in to comment.