|
53 | 53 | import com.oracle.graal.python.builtins.objects.cext.PySequenceArrayWrapperMRFactory.ToNativeArrayNodeGen; |
54 | 54 | import com.oracle.graal.python.builtins.objects.cext.PySequenceArrayWrapperMRFactory.ToNativeStorageNodeGen; |
55 | 55 | import com.oracle.graal.python.builtins.objects.cext.PySequenceArrayWrapperMRFactory.WriteArrayItemNodeGen; |
56 | | -import com.oracle.graal.python.builtins.objects.common.SequenceNodes; |
57 | 56 | import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes; |
58 | 57 | import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.ListGeneralizationNode; |
59 | 58 | import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes.NormalizeIndexNode; |
|
68 | 67 | import com.oracle.graal.python.nodes.SpecialMethodNames; |
69 | 68 | import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode; |
70 | 69 | import com.oracle.graal.python.nodes.call.special.LookupAndCallTernaryNode; |
71 | | -import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode; |
72 | 70 | import com.oracle.graal.python.nodes.truffle.PythonTypes; |
73 | 71 | import com.oracle.graal.python.runtime.sequence.PSequence; |
74 | 72 | import com.oracle.graal.python.runtime.sequence.storage.EmptySequenceStorage; |
@@ -411,82 +409,68 @@ long access(PySequenceArrayWrapper obj) { |
411 | 409 | abstract static class GetTypeIDNode extends CExtBaseNode { |
412 | 410 |
|
413 | 411 | @Child private PCallNativeNode callUnaryNode = PCallNativeNode.create(); |
414 | | - @Child private SequenceNodes.LenNode lenNode; |
415 | 412 |
|
416 | | - @CompilationFinal TruffleObject funGetByteArrayTypeID; |
417 | | - @CompilationFinal TruffleObject funGetPtrArrayTypeID; |
| 413 | + @CompilationFinal private TruffleObject funGetByteArrayTypeID; |
| 414 | + @CompilationFinal private TruffleObject funGetPtrArrayTypeID; |
418 | 415 |
|
419 | 416 | public abstract Object execute(Object delegate); |
420 | 417 |
|
421 | | - private Object callGetByteArrayTypeID(long len) { |
| 418 | + protected Object callGetByteArrayTypeID() { |
| 419 | + return callGetArrayTypeID(importCAPISymbol(NativeCAPISymbols.FUN_GET_BYTE_ARRAY_TYPE_ID)); |
| 420 | + } |
| 421 | + |
| 422 | + protected Object callGetPtrArrayTypeID() { |
| 423 | + return callGetArrayTypeID(importCAPISymbol(NativeCAPISymbols.FUN_GET_PTR_ARRAY_TYPE_ID)); |
| 424 | + } |
| 425 | + |
| 426 | + private Object callGetByteArrayTypeIDCached() { |
422 | 427 | if (funGetByteArrayTypeID == null) { |
423 | 428 | CompilerDirectives.transferToInterpreterAndInvalidate(); |
424 | 429 | funGetByteArrayTypeID = importCAPISymbol(NativeCAPISymbols.FUN_GET_BYTE_ARRAY_TYPE_ID); |
425 | 430 | } |
426 | | - return callUnaryNode.execute(funGetByteArrayTypeID, new Object[]{len}); |
| 431 | + return callGetArrayTypeID(funGetByteArrayTypeID); |
427 | 432 | } |
428 | 433 |
|
429 | | - private Object callGetPtrArrayTypeID(long len) { |
| 434 | + private Object callGetPtrArrayTypeIDCached() { |
430 | 435 | if (funGetPtrArrayTypeID == null) { |
431 | 436 | CompilerDirectives.transferToInterpreterAndInvalidate(); |
432 | 437 | funGetPtrArrayTypeID = importCAPISymbol(NativeCAPISymbols.FUN_GET_PTR_ARRAY_TYPE_ID); |
433 | 438 | } |
434 | | - return callUnaryNode.execute(funGetPtrArrayTypeID, new Object[]{len}); |
435 | | - } |
436 | | - |
437 | | - @Specialization |
438 | | - Object doTuple(PTuple tuple) { |
439 | | - return callGetPtrArrayTypeID(getLength(tuple)); |
440 | | - } |
441 | | - |
442 | | - @Specialization |
443 | | - Object doList(PList list) { |
444 | | - return callGetPtrArrayTypeID(getLength(list)); |
445 | | - } |
446 | | - |
447 | | - @Specialization |
448 | | - Object doBytes(PBytes bytes) { |
449 | | - return callGetByteArrayTypeID(getLength(bytes)); |
450 | | - } |
451 | | - |
452 | | - @Specialization |
453 | | - Object doByteArray(PByteArray bytes) { |
454 | | - return callGetByteArrayTypeID(getLength(bytes)); |
| 439 | + return callGetArrayTypeID(funGetPtrArrayTypeID); |
455 | 440 | } |
456 | 441 |
|
457 | | - @Specialization(guards = {"!isTuple(object)", "!isList(object)"}) |
458 | | - Object doGeneric(Object object, |
459 | | - @Cached("create(__LEN__)") LookupAndCallUnaryNode getLenNode) { |
460 | | - try { |
461 | | - return callGetPtrArrayTypeID(getLenNode.executeInt(object)); |
462 | | - } catch (UnexpectedResultException e) { |
463 | | - // TODO do something useful |
464 | | - throw new AssertionError(); |
465 | | - } |
| 442 | + private Object callGetArrayTypeID(TruffleObject fun) { |
| 443 | + // We use length=0 indicating an unknown length. This allows us to reuse the type but |
| 444 | + // Sulong will report the wrong length via interop for a pointer to this object. |
| 445 | + return callUnaryNode.execute(fun, new Object[]{0}); |
466 | 446 | } |
467 | 447 |
|
468 | | - protected static ListBuiltins.GetItemNode createListGetItem() { |
469 | | - return ListBuiltinsFactory.GetItemNodeFactory.create(); |
| 448 | + @Specialization(assumptions = "singleContextAssumption()", guards = "hasByteArrayContent(object)") |
| 449 | + Object doByteArray(@SuppressWarnings("unused") PSequence object, |
| 450 | + @Cached("callGetByteArrayTypeID()") Object nativeType) { |
| 451 | + // TODO(fa): use weak reference ? |
| 452 | + return nativeType; |
470 | 453 | } |
471 | 454 |
|
472 | | - protected static TupleBuiltins.GetItemNode createTupleGetItem() { |
473 | | - return TupleBuiltinsFactory.GetItemNodeFactory.create(); |
| 455 | + @Specialization(guards = "hasByteArrayContent(object)", replaces = "doByteArray") |
| 456 | + Object doByteArrayMultiCtx(@SuppressWarnings("unused") PSequence object) { |
| 457 | + return callGetByteArrayTypeIDCached(); |
474 | 458 | } |
475 | 459 |
|
476 | | - protected boolean isTuple(Object object) { |
477 | | - return object instanceof PTuple; |
| 460 | + @Specialization(assumptions = "singleContextAssumption()", guards = "!hasByteArrayContent(object)") |
| 461 | + Object doPtrArray(@SuppressWarnings("unused") PSequence object, |
| 462 | + @Cached("callGetPtrArrayTypeID()") Object nativeType) { |
| 463 | + // TODO(fa): use weak reference ? |
| 464 | + return nativeType; |
478 | 465 | } |
479 | 466 |
|
480 | | - protected boolean isList(Object object) { |
481 | | - return object instanceof PList; |
| 467 | + @Specialization(guards = "!hasByteArrayContent(object)", replaces = "doPtrArray") |
| 468 | + Object doPtrArrayMultiCtx(@SuppressWarnings("unused") PSequence object) { |
| 469 | + return callGetPtrArrayTypeIDCached(); |
482 | 470 | } |
483 | 471 |
|
484 | | - private int getLength(PSequence s) { |
485 | | - if (lenNode == null) { |
486 | | - CompilerDirectives.transferToInterpreterAndInvalidate(); |
487 | | - lenNode = insert(SequenceNodes.LenNode.create()); |
488 | | - } |
489 | | - return lenNode.execute(s); |
| 472 | + protected static boolean hasByteArrayContent(Object object) { |
| 473 | + return object instanceof PBytes || object instanceof PByteArray; |
490 | 474 | } |
491 | 475 |
|
492 | 476 | public static GetTypeIDNode create() { |
|
0 commit comments