|
58 | 58 | import static com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyNativeSymbols.GRAAL_HPY_MODULE_GET_LEGACY_METHODS;
|
59 | 59 | import static com.oracle.graal.python.builtins.objects.cext.hpy.GraalHPyNativeSymbols.GRAAL_HPY_WRITE_PTR;
|
60 | 60 |
|
61 |
| -import java.lang.ref.WeakReference; |
62 | 61 | import java.nio.charset.StandardCharsets;
|
63 |
| -import java.util.WeakHashMap; |
64 |
| - |
65 |
| -import org.graalvm.collections.Pair; |
66 | 62 |
|
67 | 63 | import com.oracle.graal.python.PythonLanguage;
|
68 | 64 | import com.oracle.graal.python.builtins.PythonBuiltinClassType;
|
|
103 | 99 | import com.oracle.graal.python.builtins.objects.dict.PDict;
|
104 | 100 | import com.oracle.graal.python.builtins.objects.function.PArguments;
|
105 | 101 | import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
|
106 |
| -import com.oracle.graal.python.builtins.objects.function.Signature; |
107 | 102 | import com.oracle.graal.python.builtins.objects.ints.PInt;
|
108 | 103 | import com.oracle.graal.python.builtins.objects.list.PList;
|
109 | 104 | import com.oracle.graal.python.builtins.objects.method.PBuiltinMethod;
|
|
113 | 108 | import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsTypeNode;
|
114 | 109 | import com.oracle.graal.python.nodes.PGuards;
|
115 | 110 | import com.oracle.graal.python.nodes.PRaiseNode;
|
116 |
| -import com.oracle.graal.python.nodes.PRootNode; |
117 | 111 | import com.oracle.graal.python.nodes.SpecialAttributeNames;
|
118 | 112 | import com.oracle.graal.python.nodes.SpecialMethodNames;
|
119 | 113 | import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
|
|
123 | 117 | import com.oracle.graal.python.nodes.call.GenericInvokeNode;
|
124 | 118 | import com.oracle.graal.python.nodes.call.special.CallBinaryMethodNode;
|
125 | 119 | import com.oracle.graal.python.nodes.call.special.CallTernaryMethodNode;
|
126 |
| -import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode; |
127 |
| -import com.oracle.graal.python.nodes.call.special.LookupAndCallTernaryNode; |
128 |
| -import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode; |
129 | 120 | import com.oracle.graal.python.nodes.classes.IsSubtypeNode;
|
130 | 121 | import com.oracle.graal.python.nodes.expression.BinaryArithmetic;
|
131 | 122 | import com.oracle.graal.python.nodes.expression.InplaceArithmetic;
|
132 |
| -import com.oracle.graal.python.nodes.expression.LookupAndCallInplaceNode; |
133 | 123 | import com.oracle.graal.python.nodes.expression.TernaryArithmetic;
|
134 | 124 | import com.oracle.graal.python.nodes.expression.UnaryArithmetic;
|
135 | 125 | import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
|
|
138 | 128 | import com.oracle.graal.python.nodes.util.CastToJavaIntLossyNode;
|
139 | 129 | import com.oracle.graal.python.nodes.util.CastToJavaLongExactNode;
|
140 | 130 | import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
|
141 |
| -import com.oracle.graal.python.runtime.ExecutionContext.CalleeContext; |
142 | 131 | import com.oracle.graal.python.runtime.exception.PException;
|
143 | 132 | import com.oracle.graal.python.runtime.object.PythonObjectFactory;
|
144 | 133 | import com.oracle.graal.python.runtime.sequence.PSequence;
|
|
147 | 136 | import com.oracle.truffle.api.CompilerAsserts;
|
148 | 137 | import com.oracle.truffle.api.CompilerDirectives;
|
149 | 138 | import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
|
150 |
| -import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
151 | 139 | import com.oracle.truffle.api.RootCallTarget;
|
152 | 140 | import com.oracle.truffle.api.TruffleLogger;
|
153 | 141 | import com.oracle.truffle.api.dsl.Cached;
|
154 | 142 | import com.oracle.truffle.api.dsl.CachedLanguage;
|
155 | 143 | import com.oracle.truffle.api.dsl.Specialization;
|
156 |
| -import com.oracle.truffle.api.frame.VirtualFrame; |
157 | 144 | import com.oracle.truffle.api.interop.ArityException;
|
158 | 145 | import com.oracle.truffle.api.interop.InteropException;
|
159 | 146 | import com.oracle.truffle.api.interop.InteropLibrary;
|
@@ -386,110 +373,8 @@ Object execute(Object[] arguments,
|
386 | 373 | }
|
387 | 374 | }
|
388 | 375 |
|
389 |
| - static final class CallArithmeticRootNode extends PRootNode { |
390 |
| - private static final Signature SIGNATURE_UNARY = new Signature(1, false, -1, false, new String[]{"$self"}, null); |
391 |
| - private static final Signature SIGNATURE_BINARY = new Signature(2, false, -1, false, new String[]{"$self", "other"}, null); |
392 |
| - private static final Signature SIGNATURE_TERNARY = new Signature(3, false, -1, false, new String[]{"x", "y", "z"}, null); |
393 |
| - |
394 |
| - @Child private LookupAndCallUnaryNode callUnaryNode; |
395 |
| - @Child private LookupAndCallBinaryNode callBinaryNode; |
396 |
| - @Child private LookupAndCallInplaceNode callInplaceNode; |
397 |
| - @Child private LookupAndCallTernaryNode callTernaryNode; |
398 |
| - @Child private CalleeContext calleeContext; |
399 |
| - |
400 |
| - private final UnaryArithmetic unaryOperator; |
401 |
| - private final BinaryArithmetic binaryOperator; |
402 |
| - private final InplaceArithmetic inplaceOperator; |
403 |
| - private final TernaryArithmetic ternaryOperator; |
404 |
| - |
405 |
| - @CompilationFinal private ConditionProfile customLocalsProfile; |
406 |
| - |
407 |
| - CallArithmeticRootNode(PythonLanguage language, UnaryArithmetic unaryOperator, BinaryArithmetic binaryOperator, InplaceArithmetic inplaceOperator, |
408 |
| - TernaryArithmetic ternaryOperator) { |
409 |
| - super(language); |
410 |
| - this.unaryOperator = unaryOperator; |
411 |
| - this.binaryOperator = binaryOperator; |
412 |
| - this.inplaceOperator = inplaceOperator; |
413 |
| - this.ternaryOperator = ternaryOperator; |
414 |
| - } |
415 |
| - |
416 |
| - @Override |
417 |
| - public Signature getSignature() { |
418 |
| - if (unaryOperator != null) { |
419 |
| - return SIGNATURE_UNARY; |
420 |
| - } else if (binaryOperator != null) { |
421 |
| - return SIGNATURE_BINARY; |
422 |
| - } else if (inplaceOperator != null || ternaryOperator != null) { |
423 |
| - return SIGNATURE_TERNARY; |
424 |
| - } else { |
425 |
| - throw CompilerDirectives.shouldNotReachHere(); |
426 |
| - } |
427 |
| - } |
428 |
| - |
429 |
| - @Override |
430 |
| - public boolean isPythonInternal() { |
431 |
| - return true; |
432 |
| - } |
433 |
| - |
434 |
| - @Override |
435 |
| - public Object execute(VirtualFrame frame) { |
436 |
| - ensureCallNode(); |
437 |
| - if (calleeContext == null) { |
438 |
| - CompilerDirectives.transferToInterpreterAndInvalidate(); |
439 |
| - calleeContext = insert(CalleeContext.create()); |
440 |
| - } |
441 |
| - if (customLocalsProfile == null) { |
442 |
| - CompilerDirectives.transferToInterpreterAndInvalidate(); |
443 |
| - customLocalsProfile = ConditionProfile.create(); |
444 |
| - } |
445 |
| - |
446 |
| - CalleeContext.enter(frame, customLocalsProfile); |
447 |
| - try { |
448 |
| - if (unaryOperator != null) { |
449 |
| - return callUnaryNode.executeObject(frame, PArguments.getArgument(frame, 0)); |
450 |
| - } else if (binaryOperator != null) { |
451 |
| - return callBinaryNode.executeObject(frame, PArguments.getArgument(frame, 0), PArguments.getArgument(frame, 1)); |
452 |
| - } else if (inplaceOperator != null) { |
453 |
| - // most of the in-place operators are binary but there can also be ternary |
454 |
| - if (PArguments.getUserArgumentLength(frame) == 2) { |
455 |
| - return callInplaceNode.execute(frame, PArguments.getArgument(frame, 0), PArguments.getArgument(frame, 1)); |
456 |
| - } else if (PArguments.getUserArgumentLength(frame) == 3) { |
457 |
| - return callInplaceNode.executeTernary(frame, PArguments.getArgument(frame, 0), PArguments.getArgument(frame, 1), PArguments.getArgument(frame, 2)); |
458 |
| - } |
459 |
| - throw CompilerDirectives.shouldNotReachHere(); |
460 |
| - } else if (ternaryOperator != null) { |
461 |
| - return callTernaryNode.execute(frame, PArguments.getArgument(frame, 0), PArguments.getArgument(frame, 1), PArguments.getArgument(frame, 2)); |
462 |
| - } else { |
463 |
| - throw CompilerDirectives.shouldNotReachHere(); |
464 |
| - } |
465 |
| - } finally { |
466 |
| - calleeContext.exit(frame, this); |
467 |
| - } |
468 |
| - } |
469 |
| - |
470 |
| - private void ensureCallNode() { |
471 |
| - if (callUnaryNode == null && callBinaryNode == null && callInplaceNode == null && callTernaryNode == null) { |
472 |
| - CompilerDirectives.transferToInterpreterAndInvalidate(); |
473 |
| - if (unaryOperator != null) { |
474 |
| - callUnaryNode = insert(unaryOperator.create()); |
475 |
| - } else if (binaryOperator != null) { |
476 |
| - callBinaryNode = insert(binaryOperator.create()); |
477 |
| - } else if (inplaceOperator != null) { |
478 |
| - callInplaceNode = insert(inplaceOperator.create()); |
479 |
| - } else if (ternaryOperator != null) { |
480 |
| - callTernaryNode = insert(ternaryOperator.create()); |
481 |
| - } else { |
482 |
| - throw CompilerDirectives.shouldNotReachHere(); |
483 |
| - } |
484 |
| - } |
485 |
| - } |
486 |
| - } |
487 |
| - |
488 | 376 | @ExportLibrary(InteropLibrary.class)
|
489 | 377 | public static final class GraalHPyArithmetic extends GraalHPyContextFunction {
|
490 |
| - // TODO(fa): move to PythonLanguage ? |
491 |
| - private static final WeakHashMap<Pair<PythonLanguage, Object>, WeakReference<RootCallTarget>> weakCallTargetMap = new WeakHashMap<>(); |
492 |
| - |
493 | 378 | private final UnaryArithmetic unaryOperator;
|
494 | 379 | private final BinaryArithmetic binaryOperator;
|
495 | 380 | private final InplaceArithmetic inplaceOperator;
|
@@ -535,7 +420,7 @@ Object execute(Object[] arguments,
|
535 | 420 | @Cached TransformExceptionToNativeNode transformExceptionToNativeNode) throws ArityException {
|
536 | 421 |
|
537 | 422 | // We need to do argument checking at this position because our helper root node that
|
538 |
| - // just dispatches to the appropriate 'LookupAndCallXXXNode' won't do any arguemnt |
| 423 | + // just dispatches to the appropriate 'LookupAndCallXXXNode' won't do any argument |
539 | 424 | // checking. So it would just crash if there are too few arguments or just ignore if
|
540 | 425 | // there are too many.
|
541 | 426 | checkArguments(arguments);
|
@@ -582,38 +467,18 @@ private void checkArguments(Object[] arguments) throws ArityException {
|
582 | 467 | private RootCallTarget getCallTarget(PythonLanguage language) {
|
583 | 468 | if (callTarget == null) {
|
584 | 469 | CompilerDirectives.transferToInterpreterAndInvalidate();
|
585 |
| - callTarget = createCallTarget(language); |
586 |
| - } |
587 |
| - return callTarget; |
588 |
| - } |
589 |
| - |
590 |
| - @TruffleBoundary |
591 |
| - private RootCallTarget createCallTarget(PythonLanguage language) { |
592 |
| - Pair<PythonLanguage, Object> key = Pair.create(language, getOp()); |
593 |
| - RootCallTarget cachedCallTarget; |
594 |
| - synchronized (weakCallTargetMap) { |
595 |
| - WeakReference<RootCallTarget> ctRef = weakCallTargetMap.get(key); |
596 |
| - cachedCallTarget = ctRef != null ? ctRef.get() : null; |
597 |
| - if (cachedCallTarget == null) { |
598 |
| - cachedCallTarget = PythonUtils.getOrCreateCallTarget(new CallArithmeticRootNode(language, unaryOperator, binaryOperator, inplaceOperator, ternaryOperator)); |
599 |
| - weakCallTargetMap.put(key, new WeakReference<>(cachedCallTarget)); |
| 470 | + if (unaryOperator != null) { |
| 471 | + callTarget = language.getOrCreateUnaryArithmeticCallTarget(unaryOperator); |
| 472 | + } else if (binaryOperator != null) { |
| 473 | + callTarget = language.getOrCreateBinaryArithmeticCallTarget(binaryOperator); |
| 474 | + } else if (inplaceOperator != null) { |
| 475 | + callTarget = language.getOrCreateInplaceArithmeticCallTarget(inplaceOperator); |
| 476 | + } else if (ternaryOperator != null) { |
| 477 | + callTarget = language.getOrCreateTernaryArithmeticCallTarget(ternaryOperator); |
600 | 478 | }
|
| 479 | + throw CompilerDirectives.shouldNotReachHere(); |
601 | 480 | }
|
602 |
| - assert cachedCallTarget != null; |
603 |
| - return cachedCallTarget; |
604 |
| - } |
605 |
| - |
606 |
| - private Object getOp() { |
607 |
| - if (unaryOperator != null) { |
608 |
| - return unaryOperator; |
609 |
| - } else if (binaryOperator != null) { |
610 |
| - return binaryOperator; |
611 |
| - } else if (inplaceOperator != null) { |
612 |
| - return inplaceOperator; |
613 |
| - } else if (ternaryOperator != null) { |
614 |
| - return ternaryOperator; |
615 |
| - } |
616 |
| - throw CompilerDirectives.shouldNotReachHere(); |
| 481 | + return callTarget; |
617 | 482 | }
|
618 | 483 | }
|
619 | 484 |
|
|
0 commit comments