package com.oracle.truffle.llvm.runtime.nodes.op;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.ImportStatic;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.llvm.runtime.CommonNodeFactory;
import com.oracle.truffle.llvm.runtime.interop.convert.ForeignToLLVM;
import com.oracle.truffle.llvm.runtime.interop.convert.ForeignToLLVM.ForeignToLLVMType;
import com.oracle.truffle.llvm.runtime.library.internal.LLVMNativeLibrary;
import com.oracle.truffle.llvm.runtime.nodes.api.LLVMNode;
import com.oracle.truffle.llvm.runtime.nodes.op.ToComparableValueNodeGen.ManagedToComparableValueNodeGen;
import com.oracle.truffle.llvm.runtime.pointer.LLVMManagedPointer;
public abstract class ToComparableValue extends LLVMNode {
public abstract long executeWithTarget(Object obj);
@Specialization(guards = "lib.isPointer(obj)", limit = "3", rewriteOn = UnsupportedMessageException.class)
protected long doPointer(Object obj,
@CachedLibrary("obj") LLVMNativeLibrary lib) throws UnsupportedMessageException {
return lib.asPointer(obj);
}
@Specialization(guards = "lib.isPointer(obj)", limit = "3")
protected long doPointerException(Object obj,
@CachedLibrary("obj") LLVMNativeLibrary lib,
@Cached("createUseOffset()") ManagedToComparableValue toComparable) {
try {
return lib.asPointer(obj);
} catch (UnsupportedMessageException ex) {
return doManaged(obj, lib, toComparable);
}
}
@Specialization(guards = "!lib.isPointer(obj)", limit = "3")
protected long doManaged(Object obj,
@CachedLibrary("obj") @SuppressWarnings("unused") LLVMNativeLibrary lib,
@Cached("createUseOffset()") ManagedToComparableValue toComparable) {
return toComparable.executeWithTarget(obj);
}
@TruffleBoundary(allowInlining = true)
private static long getHashCode(Object obj) {
return ((long) obj.hashCode()) << 8;
}
@ImportStatic(ForeignToLLVMType.class)
public abstract static class ManagedToComparableValue extends LLVMNode {
private final boolean includeOffset;
public ManagedToComparableValue(boolean includeOffset) {
this.includeOffset = includeOffset;
}
abstract long executeWithTarget(Object obj);
@Specialization
protected long doManaged(LLVMManagedPointer address) {
long result = getHashCode(address.getObject());
if (includeOffset) {
result += address.getOffset();
}
return result;
}
public static ManagedToComparableValue createIgnoreOffset() {
return ManagedToComparableValueNodeGen.create(false);
}
public static ManagedToComparableValue createUseOffset() {
return ManagedToComparableValueNodeGen.create(true);
}
protected ForeignToLLVM createForeignToI64() {
return CommonNodeFactory.createForeignToLLVM(ForeignToLLVMType.I64);
}
}
}