|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.graal.python.nodes.call.special; |
| 42 | + |
| 43 | +import com.oracle.graal.python.builtins.objects.PNone; |
| 44 | +import com.oracle.graal.python.builtins.objects.PNotImplemented; |
| 45 | +import com.oracle.graal.python.builtins.objects.type.TypeNodes.IsSameTypeNode; |
| 46 | +import com.oracle.graal.python.nodes.SpecialMethodNames; |
| 47 | +import com.oracle.graal.python.nodes.classes.IsSubtypeNode; |
| 48 | +import com.oracle.graal.python.nodes.object.GetClassNode; |
| 49 | +import com.oracle.graal.python.runtime.PythonOptions; |
| 50 | +import com.oracle.graal.python.util.Supplier; |
| 51 | +import com.oracle.truffle.api.CompilerDirectives; |
| 52 | +import com.oracle.truffle.api.dsl.Cached; |
| 53 | +import com.oracle.truffle.api.dsl.ImportStatic; |
| 54 | +import com.oracle.truffle.api.dsl.ReportPolymorphism.Megamorphic; |
| 55 | +import com.oracle.truffle.api.dsl.Specialization; |
| 56 | +import com.oracle.truffle.api.frame.VirtualFrame; |
| 57 | +import com.oracle.truffle.api.profiles.BranchProfile; |
| 58 | + |
| 59 | +@ImportStatic({SpecialMethodNames.class, PythonOptions.class}) |
| 60 | +public abstract class LookupAndCallReversibleTernaryNode extends LookupAndCallTernaryNode { |
| 61 | + |
| 62 | + @Child private GetClassNode thirdGetClassNode; |
| 63 | + @Child private CallTernaryMethodNode reverseDispatchNode; |
| 64 | + @Child private CallTernaryMethodNode thirdDispatchNode; |
| 65 | + @Child protected LookupSpecialMethodNode getThirdAttrNode; |
| 66 | + |
| 67 | + @Child protected NotImplementedHandler handler; |
| 68 | + protected final Supplier<NotImplementedHandler> handlerFactory; |
| 69 | + |
| 70 | + LookupAndCallReversibleTernaryNode(String name, Supplier<NotImplementedHandler> handlerFactory) { |
| 71 | + super(name); |
| 72 | + this.handlerFactory = handlerFactory; |
| 73 | + } |
| 74 | + |
| 75 | + @Specialization(guards = "v.getClass() == cachedVClass", limit = "getCallSiteInlineCacheMaxDepth()") |
| 76 | + Object callObjectR(VirtualFrame frame, Object v, Object w, Object z, |
| 77 | + @SuppressWarnings("unused") @Cached("v.getClass()") Class<?> cachedVClass, |
| 78 | + @Cached("create(name)") LookupSpecialMethodNode getattr, |
| 79 | + @Cached("create(name)") LookupSpecialMethodNode getattrR, |
| 80 | + @Cached GetClassNode getClass, |
| 81 | + @Cached GetClassNode getClassR, |
| 82 | + @Cached IsSubtypeNode isSubtype, |
| 83 | + @Cached IsSameTypeNode isSameTypeNode, |
| 84 | + @Cached BranchProfile notImplementedBranch) { |
| 85 | + return doCallObjectR(frame, v, w, z, getattr, getattrR, getClass, getClassR, isSubtype, isSameTypeNode, notImplementedBranch); |
| 86 | + } |
| 87 | + |
| 88 | + @Specialization(replaces = "callObjectR") |
| 89 | + @Megamorphic |
| 90 | + Object callObjectRMegamorphic(VirtualFrame frame, Object v, Object w, Object z, |
| 91 | + @Cached("create(name)") LookupSpecialMethodNode getattr, |
| 92 | + @Cached("create(name)") LookupSpecialMethodNode getattrR, |
| 93 | + @Cached GetClassNode getClass, |
| 94 | + @Cached GetClassNode getClassR, |
| 95 | + @Cached IsSubtypeNode isSubtype, |
| 96 | + @Cached IsSameTypeNode isSameTypeNode, |
| 97 | + @Cached BranchProfile notImplementedBranch) { |
| 98 | + return doCallObjectR(frame, v, w, z, getattr, getattrR, getClass, getClassR, isSubtype, isSameTypeNode, notImplementedBranch); |
| 99 | + } |
| 100 | + |
| 101 | + private Object doCallObjectR(VirtualFrame frame, Object v, Object w, Object z, LookupSpecialMethodNode getattr, LookupSpecialMethodNode getattrR, GetClassNode getClass, GetClassNode getClassR, |
| 102 | + IsSubtypeNode isSubtype, IsSameTypeNode isSameTypeNode, BranchProfile notImplementedBranch) { |
| 103 | + // c.f. mostly slot_nb_power and wrap_ternaryfunc_r. like |
| 104 | + // cpython://Object/abstract.c#ternary_op we try all three combinations, and the structure |
| 105 | + // of this method is modeled after this. However, this method also merges the logic from |
| 106 | + // slot_nb_power/wrap_ternaryfunc_r in that it swaps arguments around. The reversal is |
| 107 | + // undone for builtin functions in BuiltinFunctionRootNode, just like it would be undone in |
| 108 | + // CPython using its slot wrappers |
| 109 | + Object leftClass = getClass.execute(v); |
| 110 | + Object rightClass = getClassR.execute(w); |
| 111 | + |
| 112 | + Object result = PNotImplemented.NOT_IMPLEMENTED; |
| 113 | + Object leftCallable = getattr.execute(frame, leftClass, v); |
| 114 | + Object rightCallable = PNone.NO_VALUE; |
| 115 | + |
| 116 | + if (!isSameTypeNode.execute(leftClass, rightClass)) { |
| 117 | + rightCallable = getattrR.execute(frame, rightClass, w); |
| 118 | + if (rightCallable == leftCallable) { |
| 119 | + rightCallable = PNone.NO_VALUE; |
| 120 | + } |
| 121 | + } |
| 122 | + if (leftCallable != PNone.NO_VALUE) { |
| 123 | + if (rightCallable != PNone.NO_VALUE && isSubtype.execute(frame, rightClass, leftClass)) { |
| 124 | + result = ensureReverseDispatch().execute(frame, rightCallable, v, w, z); |
| 125 | + if (result != PNotImplemented.NOT_IMPLEMENTED) { |
| 126 | + return result; |
| 127 | + } |
| 128 | + rightCallable = PNone.NO_VALUE; |
| 129 | + } |
| 130 | + result = dispatchNode.execute(frame, leftCallable, v, w, z); |
| 131 | + if (result != PNotImplemented.NOT_IMPLEMENTED) { |
| 132 | + return result; |
| 133 | + } |
| 134 | + } |
| 135 | + if (rightCallable != PNone.NO_VALUE) { |
| 136 | + result = ensureReverseDispatch().execute(frame, rightCallable, v, w, z); |
| 137 | + if (result != PNotImplemented.NOT_IMPLEMENTED) { |
| 138 | + return result; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + Object zCallable = ensureGetAttrZ().execute(frame, ensureThirdGetClass().execute(z), z); |
| 143 | + if (zCallable != PNone.NO_VALUE && zCallable != leftCallable && zCallable != rightCallable) { |
| 144 | + result = ensureThirdDispatch().execute(frame, zCallable, v, w, z); |
| 145 | + if (result != PNotImplemented.NOT_IMPLEMENTED) { |
| 146 | + return result; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + notImplementedBranch.enter(); |
| 151 | + if (handlerFactory != null) { |
| 152 | + if (handler == null) { |
| 153 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 154 | + handler = insert(handlerFactory.get()); |
| 155 | + } |
| 156 | + return handler.execute(v, w, z); |
| 157 | + } |
| 158 | + return result; |
| 159 | + } |
| 160 | + |
| 161 | + protected GetClassNode ensureThirdGetClass() { |
| 162 | + if (thirdGetClassNode == null) { |
| 163 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 164 | + thirdGetClassNode = insert(GetClassNode.create()); |
| 165 | + } |
| 166 | + return thirdGetClassNode; |
| 167 | + } |
| 168 | + |
| 169 | + protected CallTernaryMethodNode ensureReverseDispatch() { |
| 170 | + // this also serves as a branch profile |
| 171 | + if (reverseDispatchNode == null) { |
| 172 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 173 | + reverseDispatchNode = insert(CallTernaryMethodNode.create()); |
| 174 | + } |
| 175 | + return reverseDispatchNode; |
| 176 | + } |
| 177 | + |
| 178 | + protected CallTernaryMethodNode ensureThirdDispatch() { |
| 179 | + // this also serves as a branch profile |
| 180 | + if (thirdDispatchNode == null) { |
| 181 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 182 | + thirdDispatchNode = insert(CallTernaryMethodNode.create()); |
| 183 | + } |
| 184 | + return thirdDispatchNode; |
| 185 | + } |
| 186 | + |
| 187 | + protected LookupSpecialMethodNode ensureGetAttrZ() { |
| 188 | + // this also serves as a branch profile |
| 189 | + if (getThirdAttrNode == null) { |
| 190 | + CompilerDirectives.transferToInterpreterAndInvalidate(); |
| 191 | + getThirdAttrNode = insert(LookupSpecialMethodNode.create(name)); |
| 192 | + } |
| 193 | + return getThirdAttrNode; |
| 194 | + } |
| 195 | +} |
0 commit comments