|
100 | 100 | import com.oracle.truffle.api.dsl.Specialization;
|
101 | 101 | import com.oracle.truffle.api.dsl.TypeSystemReference;
|
102 | 102 | import com.oracle.truffle.api.frame.VirtualFrame;
|
| 103 | +import com.oracle.truffle.api.interop.InteropLibrary; |
| 104 | +import com.oracle.truffle.api.interop.TruffleObject; |
| 105 | +import com.oracle.truffle.api.interop.UnsupportedMessageException; |
| 106 | +import com.oracle.truffle.api.library.CachedLibrary; |
103 | 107 | import com.oracle.truffle.api.profiles.BranchProfile;
|
104 | 108 | import com.oracle.truffle.api.profiles.ConditionProfile;
|
105 | 109 |
|
@@ -1407,6 +1411,62 @@ boolean eqPiPi(PInt a, PInt b) {
|
1407 | 1411 | return a.equals(b);
|
1408 | 1412 | }
|
1409 | 1413 |
|
| 1414 | + @Specialization |
| 1415 | + boolean eqVoidPtrLong(PythonNativeVoidPtr a, long b, |
| 1416 | + @Shared("lib") @CachedLibrary(limit = "1") InteropLibrary lib) { |
| 1417 | + if (lib.isPointer(a.object)) { |
| 1418 | + try { |
| 1419 | + long ptrVal = lib.asPointer(a.object); |
| 1420 | + // pointers are considered unsigned |
| 1421 | + return ptrVal >= 0L && ptrVal == b; |
| 1422 | + } catch (UnsupportedMessageException e) { |
| 1423 | + // fall through |
| 1424 | + } |
| 1425 | + } |
| 1426 | + return doHash(a.object, b); |
| 1427 | + } |
| 1428 | + |
| 1429 | + @Specialization |
| 1430 | + boolean eqLongVoidPtr(long a, PythonNativeVoidPtr b, |
| 1431 | + @Shared("lib") @CachedLibrary(limit = "1") InteropLibrary lib) { |
| 1432 | + return eqVoidPtrLong(b, a, lib); |
| 1433 | + } |
| 1434 | + |
| 1435 | + @Specialization |
| 1436 | + @TruffleBoundary |
| 1437 | + boolean eqVoidPtrPInt(PythonNativeVoidPtr a, PInt b, |
| 1438 | + @Shared("lib") @CachedLibrary(limit = "1") InteropLibrary lib) { |
| 1439 | + if (lib.isPointer(a.object)) { |
| 1440 | + try { |
| 1441 | + long ptrVal = lib.asPointer(a.object); |
| 1442 | + if (ptrVal < 0) { |
| 1443 | + // pointers are considered unsigned |
| 1444 | + BigInteger bi = BigInteger.valueOf(ptrVal).add(BigInteger.ONE.shiftLeft(64)); |
| 1445 | + return bi.equals(b.getValue()); |
| 1446 | + } |
| 1447 | + return BigInteger.valueOf(ptrVal).equals(b.getValue()); |
| 1448 | + } catch (UnsupportedMessageException e) { |
| 1449 | + // fall through |
| 1450 | + } |
| 1451 | + } |
| 1452 | + try { |
| 1453 | + return a.object.hashCode() == b.longValueExact(); |
| 1454 | + } catch (ArithmeticException e) { |
| 1455 | + return false; |
| 1456 | + } |
| 1457 | + } |
| 1458 | + |
| 1459 | + @Specialization |
| 1460 | + boolean eqPIntVoidPtr(PInt a, PythonNativeVoidPtr b, |
| 1461 | + @Shared("lib") @CachedLibrary(limit = "1") InteropLibrary lib) { |
| 1462 | + return eqVoidPtrPInt(b, a, lib); |
| 1463 | + } |
| 1464 | + |
| 1465 | + @TruffleBoundary |
| 1466 | + private static boolean doHash(Object object, long b) { |
| 1467 | + return object.hashCode() == b; |
| 1468 | + } |
| 1469 | + |
1410 | 1470 | @SuppressWarnings("unused")
|
1411 | 1471 | @Fallback
|
1412 | 1472 | PNotImplemented eq(Object a, Object b) {
|
@@ -2184,6 +2244,24 @@ public String doL(long self) {
|
2184 | 2244 | public String doPInt(PInt self) {
|
2185 | 2245 | return self.toString();
|
2186 | 2246 | }
|
| 2247 | + |
| 2248 | + @Specialization |
| 2249 | + public String doNativeVoidPtr(PythonNativeVoidPtr self, |
| 2250 | + @CachedLibrary(limit = "1") InteropLibrary lib) { |
| 2251 | + if (lib.isPointer(self.object)) { |
| 2252 | + try { |
| 2253 | + return Long.toString(lib.asPointer(self.object)); |
| 2254 | + } catch (UnsupportedMessageException e) { |
| 2255 | + // fall through |
| 2256 | + } |
| 2257 | + } |
| 2258 | + return doHash(self.object); |
| 2259 | + } |
| 2260 | + |
| 2261 | + @TruffleBoundary |
| 2262 | + private static String doHash(Object object) { |
| 2263 | + return Integer.toString(object.hashCode()); |
| 2264 | + } |
2187 | 2265 | }
|
2188 | 2266 |
|
2189 | 2267 | @Builtin(name = SpecialMethodNames.__REPR__, minNumOfPositionalArgs = 1)
|
|
0 commit comments