|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, 2024, Oracle and/or its affiliates. |
| 3 | + * Copyright (c) 2014, Regents of the University of California |
| 4 | + * |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without modification, are |
| 8 | + * permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, this list of |
| 11 | + * conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of |
| 13 | + * conditions and the following disclaimer in the documentation and/or other materials provided |
| 14 | + * with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
| 17 | + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 21 | + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 22 | + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 23 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 24 | + * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +package com.oracle.graal.python.builtins.objects.foreign; |
| 28 | + |
| 29 | +import com.oracle.graal.python.annotations.Slot; |
| 30 | +import com.oracle.graal.python.annotations.Slot.SlotKind; |
| 31 | +import com.oracle.graal.python.builtins.Builtin; |
| 32 | +import com.oracle.graal.python.builtins.CoreFunctions; |
| 33 | +import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
| 34 | +import com.oracle.graal.python.builtins.PythonBuiltins; |
| 35 | +import com.oracle.graal.python.builtins.objects.ints.PInt; |
| 36 | +import com.oracle.graal.python.builtins.objects.type.TpSlots; |
| 37 | +import com.oracle.graal.python.builtins.objects.type.slots.TpSlotInquiry.NbBoolBuiltinNode; |
| 38 | +import com.oracle.graal.python.lib.PyObjectStrAsTruffleStringNode; |
| 39 | +import com.oracle.graal.python.nodes.expression.BinaryArithmetic.BitAndNode; |
| 40 | +import com.oracle.graal.python.nodes.expression.BinaryArithmetic.BitOrNode; |
| 41 | +import com.oracle.graal.python.nodes.expression.BinaryArithmetic.BitXorNode; |
| 42 | +import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode; |
| 43 | +import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode; |
| 44 | +import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; |
| 45 | +import com.oracle.graal.python.runtime.GilNode; |
| 46 | +import com.oracle.truffle.api.CompilerDirectives; |
| 47 | +import com.oracle.truffle.api.dsl.Bind; |
| 48 | +import com.oracle.truffle.api.dsl.Cached; |
| 49 | +import com.oracle.truffle.api.dsl.GenerateNodeFactory; |
| 50 | +import com.oracle.truffle.api.dsl.GenerateUncached; |
| 51 | +import com.oracle.truffle.api.dsl.NodeFactory; |
| 52 | +import com.oracle.truffle.api.dsl.Specialization; |
| 53 | +import com.oracle.truffle.api.frame.VirtualFrame; |
| 54 | +import com.oracle.truffle.api.interop.InteropLibrary; |
| 55 | +import com.oracle.truffle.api.interop.UnsupportedMessageException; |
| 56 | +import com.oracle.truffle.api.library.CachedLibrary; |
| 57 | +import com.oracle.truffle.api.nodes.Node; |
| 58 | + |
| 59 | +import java.util.List; |
| 60 | + |
| 61 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.J___AND__; |
| 62 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.J___INDEX__; |
| 63 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.J___OR__; |
| 64 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.J___RAND__; |
| 65 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.J___REPR__; |
| 66 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.J___ROR__; |
| 67 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.J___RXOR__; |
| 68 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.J___STR__; |
| 69 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.J___XOR__; |
| 70 | + |
| 71 | +/* |
| 72 | + * This class handles foreign booleans and reimplements the methods of Python bool. |
| 73 | + * We avoid inheriting from bool as that brings many complications, and we want to reuse the logic from ForeignNumberBuiltins. |
| 74 | + * |
| 75 | + * NOTE: We are not using IndirectCallContext here in this file |
| 76 | + * because it seems unlikely that these interop messages would call back to Python |
| 77 | + * and that we would also need precise frame info for that case. |
| 78 | + * Adding it shouldn't hurt peak, but might be a non-trivial overhead in interpreter. |
| 79 | + */ |
| 80 | +@CoreFunctions(extendClasses = PythonBuiltinClassType.ForeignBoolean) |
| 81 | +public final class ForeignBooleanBuiltins extends PythonBuiltins { |
| 82 | + public static TpSlots SLOTS = ForeignBooleanBuiltinsSlotsGen.SLOTS; |
| 83 | + |
| 84 | + @Override |
| 85 | + protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() { |
| 86 | + return ForeignBooleanBuiltinsFactory.getFactories(); |
| 87 | + } |
| 88 | + |
| 89 | + @Slot(SlotKind.nb_bool) |
| 90 | + @GenerateUncached |
| 91 | + @GenerateNodeFactory |
| 92 | + abstract static class BoolNode extends NbBoolBuiltinNode { |
| 93 | + @Specialization(limit = "getCallSiteInlineCacheMaxDepth()") |
| 94 | + static boolean bool(Object receiver, |
| 95 | + @CachedLibrary("receiver") InteropLibrary lib, |
| 96 | + @Cached GilNode gil) { |
| 97 | + gil.release(true); |
| 98 | + try { |
| 99 | + return lib.asBoolean(receiver); |
| 100 | + } catch (UnsupportedMessageException e) { |
| 101 | + throw CompilerDirectives.shouldNotReachHere(e); |
| 102 | + } finally { |
| 103 | + gil.acquire(); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Builtin(name = J___INDEX__, minNumOfPositionalArgs = 1) |
| 109 | + @GenerateNodeFactory |
| 110 | + abstract static class IndexNode extends PythonUnaryBuiltinNode { |
| 111 | + @Specialization(limit = "3") |
| 112 | + protected static Object doIt(Object object, |
| 113 | + @CachedLibrary("object") InteropLibrary lib, |
| 114 | + @Cached GilNode gil) { |
| 115 | + gil.release(true); |
| 116 | + try { |
| 117 | + try { |
| 118 | + return PInt.intValue(lib.asBoolean(object)); |
| 119 | + } catch (UnsupportedMessageException e) { |
| 120 | + throw CompilerDirectives.shouldNotReachHere("foreign value claims to be a boolean but isn't"); |
| 121 | + } |
| 122 | + } finally { |
| 123 | + gil.acquire(); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Builtin(name = J___STR__, minNumOfPositionalArgs = 1) |
| 129 | + @GenerateNodeFactory |
| 130 | + abstract static class StrNode extends PythonUnaryBuiltinNode { |
| 131 | + @Specialization |
| 132 | + Object str(VirtualFrame frame, Object object, |
| 133 | + @Bind("this") Node inliningTarget, |
| 134 | + @CachedLibrary(limit = "3") InteropLibrary lib, |
| 135 | + @Cached GilNode gil, |
| 136 | + @Cached PyObjectStrAsTruffleStringNode strNode) { |
| 137 | + final Object value; |
| 138 | + try { |
| 139 | + gil.release(true); |
| 140 | + try { |
| 141 | + value = lib.asBoolean(object); |
| 142 | + } finally { |
| 143 | + gil.acquire(); |
| 144 | + } |
| 145 | + } catch (UnsupportedMessageException e) { |
| 146 | + throw CompilerDirectives.shouldNotReachHere(e); |
| 147 | + } |
| 148 | + |
| 149 | + return strNode.execute(frame, inliningTarget, value); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Builtin(name = J___REPR__, minNumOfPositionalArgs = 1) |
| 154 | + @GenerateNodeFactory |
| 155 | + abstract static class ReprNode extends StrNode { |
| 156 | + } |
| 157 | + |
| 158 | + @Builtin(name = J___RAND__, minNumOfPositionalArgs = 2) |
| 159 | + @Builtin(name = J___AND__, minNumOfPositionalArgs = 2) |
| 160 | + @GenerateNodeFactory |
| 161 | + abstract static class AndNode extends PythonBinaryBuiltinNode { |
| 162 | + @Specialization(limit = "3") |
| 163 | + protected static Object op(VirtualFrame frame, Object left, Object right, |
| 164 | + @Cached BitAndNode andNode, |
| 165 | + @CachedLibrary("left") InteropLibrary lib, |
| 166 | + @Cached GilNode gil) { |
| 167 | + try { |
| 168 | + boolean leftBoolean; |
| 169 | + gil.release(true); |
| 170 | + try { |
| 171 | + leftBoolean = lib.asBoolean(left); |
| 172 | + } finally { |
| 173 | + gil.acquire(); |
| 174 | + } |
| 175 | + return andNode.executeObject(frame, leftBoolean, right); |
| 176 | + } catch (UnsupportedMessageException e) { |
| 177 | + throw CompilerDirectives.shouldNotReachHere(); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + @Builtin(name = J___ROR__, minNumOfPositionalArgs = 2) |
| 183 | + @Builtin(name = J___OR__, minNumOfPositionalArgs = 2) |
| 184 | + @GenerateNodeFactory |
| 185 | + abstract static class OrNode extends PythonBinaryBuiltinNode { |
| 186 | + @Specialization(limit = "3") |
| 187 | + protected static Object op(VirtualFrame frame, Object left, Object right, |
| 188 | + @Cached BitOrNode orNode, |
| 189 | + @CachedLibrary("left") InteropLibrary lib, |
| 190 | + @Cached GilNode gil) { |
| 191 | + try { |
| 192 | + boolean leftBoolean; |
| 193 | + gil.release(true); |
| 194 | + try { |
| 195 | + leftBoolean = lib.asBoolean(left); |
| 196 | + } finally { |
| 197 | + gil.acquire(); |
| 198 | + } |
| 199 | + return orNode.executeObject(frame, leftBoolean, right); |
| 200 | + } catch (UnsupportedMessageException e) { |
| 201 | + throw CompilerDirectives.shouldNotReachHere(); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + @Builtin(name = J___RXOR__, minNumOfPositionalArgs = 2) |
| 207 | + @Builtin(name = J___XOR__, minNumOfPositionalArgs = 2) |
| 208 | + @GenerateNodeFactory |
| 209 | + abstract static class XorNode extends PythonBinaryBuiltinNode { |
| 210 | + @Specialization(limit = "3") |
| 211 | + protected static Object op(VirtualFrame frame, Object left, Object right, |
| 212 | + @Cached BitXorNode xorNode, |
| 213 | + @CachedLibrary("left") InteropLibrary lib, |
| 214 | + @Cached GilNode gil) { |
| 215 | + try { |
| 216 | + boolean leftBoolean; |
| 217 | + gil.release(true); |
| 218 | + try { |
| 219 | + leftBoolean = lib.asBoolean(left); |
| 220 | + } finally { |
| 221 | + gil.acquire(); |
| 222 | + } |
| 223 | + return xorNode.executeObject(frame, leftBoolean, right); |
| 224 | + } catch (UnsupportedMessageException e) { |
| 225 | + throw CompilerDirectives.shouldNotReachHere(); |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | +} |
0 commit comments