Skip to content

Commit 7450b32

Browse files
committed
Cleanup CExtNodes.PointerCompareNode
1 parent 99fc72d commit 7450b32

File tree

1 file changed

+32
-25
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi

1 file changed

+32
-25
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CExtNodes.java

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@
142142
import com.oracle.graal.python.nodes.SpecialAttributeNames;
143143
import com.oracle.graal.python.nodes.SpecialMethodNames;
144144
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
145-
import com.oracle.graal.python.nodes.attributes.WriteAttributeToPythonObjectNode;
146145
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
146+
import com.oracle.graal.python.nodes.attributes.WriteAttributeToPythonObjectNode;
147147
import com.oracle.graal.python.nodes.call.CallNode;
148148
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode.LookupAndCallUnaryDynamicNode;
149149
import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
@@ -582,42 +582,49 @@ public abstract static class PointerCompareNode extends Node {
582582

583583
public abstract boolean execute(Node inliningTarget, ComparisonOp op, Object a, Object b);
584584

585-
private static boolean executeCFunction(Node inliningTarget, int op, Object a, Object b, InteropLibrary interopLibrary) {
586-
try {
587-
Object sym = CApiContext.getNativeSymbol(inliningTarget, FUN_PTR_COMPARE);
588-
return (int) interopLibrary.execute(sym, a, b, op) != 0;
589-
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e) {
590-
CompilerDirectives.transferToInterpreterAndInvalidate();
591-
throw new IllegalStateException(FUN_PTR_COMPARE + " didn't work!");
592-
}
593-
}
594-
595585
@Specialization(guards = "op.isEqualityOp()", limit = "2")
596586
static boolean doEqNe(ComparisonOp op, PythonAbstractNativeObject a, PythonAbstractNativeObject b,
597587
@CachedLibrary("a") InteropLibrary aLib,
598588
@CachedLibrary(limit = "3") InteropLibrary bLib) {
599589
return aLib.isIdentical(a, b, bLib) == (op == ComparisonOp.EQ);
600590
}
601591

602-
@Specialization
603-
static boolean doPythonNativeObject(Node inliningTarget, ComparisonOp op, PythonNativeObject a, PythonNativeObject b,
604-
@Shared @CachedLibrary(limit = "1") InteropLibrary interopLibrary) {
592+
@Specialization(guards = {"isNativeObjectOrVoidPointer(a)", "isNativeObjectOrLong(b)"})
593+
static boolean doGeneric(Node inliningTarget, ComparisonOp op, Object a, Object b,
594+
@CachedLibrary(limit = "1") InteropLibrary interopLibrary,
595+
@Cached InlinedConditionProfile aProfile,
596+
@Cached InlinedConditionProfile bProfile) {
605597
CompilerAsserts.partialEvaluationConstant(op);
606-
return executeCFunction(inliningTarget, op.opCode, a.getPtr(), b.getPtr(), interopLibrary);
598+
Object ptrA;
599+
if (aProfile.profile(inliningTarget, a instanceof PythonNativeObject)) {
600+
ptrA = ((PythonNativeObject) a).getPtr();
601+
} else {
602+
// guaranteed by the guard
603+
assert a instanceof PythonNativeVoidPtr;
604+
ptrA = ((PythonNativeVoidPtr) a).getNativePointer();
605+
}
606+
Object ptrB;
607+
if (bProfile.profile(inliningTarget, b instanceof PythonNativeObject)) {
608+
ptrB = ((PythonNativeObject) b).getPtr();
609+
} else {
610+
// guaranteed by the guard
611+
assert b instanceof Long;
612+
ptrB = b;
613+
}
614+
try {
615+
Object sym = CApiContext.getNativeSymbol(inliningTarget, FUN_PTR_COMPARE);
616+
return (int) interopLibrary.execute(sym, ptrA, ptrB, op.opCode) != 0;
617+
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e) {
618+
throw CompilerDirectives.shouldNotReachHere(e);
619+
}
607620
}
608621

609-
@Specialization
610-
static boolean doPythonNativeObjectLong(Node inliningTarget, ComparisonOp op, PythonNativeObject a, long b,
611-
@Shared @CachedLibrary(limit = "1") InteropLibrary interopLibrary) {
612-
CompilerAsserts.partialEvaluationConstant(op);
613-
return executeCFunction(inliningTarget, op.opCode, a.getPtr(), b, interopLibrary);
622+
static boolean isNativeObjectOrVoidPointer(Object object) {
623+
return object instanceof PythonNativeObject || object instanceof PythonNativeVoidPtr;
614624
}
615625

616-
@Specialization
617-
static boolean doNativeVoidPtrLong(Node inliningTarget, ComparisonOp op, PythonNativeVoidPtr a, long b,
618-
@Shared @CachedLibrary(limit = "1") InteropLibrary interopLibrary) {
619-
CompilerAsserts.partialEvaluationConstant(op);
620-
return executeCFunction(inliningTarget, op.opCode, a.getPointerObject(), b, interopLibrary);
626+
static boolean isNativeObjectOrLong(Object object) {
627+
return object instanceof PythonNativeObject || object instanceof Long;
621628
}
622629
}
623630

0 commit comments

Comments
 (0)