|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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.builtins.objects.cext; |
| 42 | + |
| 43 | +import static com.oracle.graal.python.builtins.objects.cext.NativeCAPISymbols.FUN_GET_UINT32_ARRAY_TYPE_ID; |
| 44 | + |
| 45 | +import com.oracle.graal.python.PythonLanguage; |
| 46 | +import com.oracle.graal.python.builtins.objects.cext.CExtNodes.PCallCapiFunction; |
| 47 | +import com.oracle.graal.python.builtins.objects.ints.PInt; |
| 48 | +import com.oracle.graal.python.runtime.PythonOptions; |
| 49 | +import com.oracle.truffle.api.Assumption; |
| 50 | +import com.oracle.truffle.api.CompilerAsserts; |
| 51 | +import com.oracle.truffle.api.dsl.Cached; |
| 52 | +import com.oracle.truffle.api.dsl.Cached.Exclusive; |
| 53 | +import com.oracle.truffle.api.dsl.Specialization; |
| 54 | +import com.oracle.truffle.api.interop.InteropLibrary; |
| 55 | +import com.oracle.truffle.api.interop.InvalidArrayIndexException; |
| 56 | +import com.oracle.truffle.api.interop.UnsupportedMessageException; |
| 57 | +import com.oracle.truffle.api.library.CachedLibrary; |
| 58 | +import com.oracle.truffle.api.library.ExportLibrary; |
| 59 | +import com.oracle.truffle.api.library.ExportMessage; |
| 60 | +import com.oracle.truffle.llvm.spi.NativeTypeLibrary; |
| 61 | + |
| 62 | +/** |
| 63 | + * Emulates {@code ob_digit} of {@code struct _longobject} for Python integers. |
| 64 | + */ |
| 65 | +@ExportLibrary(InteropLibrary.class) |
| 66 | +@ExportLibrary(NativeTypeLibrary.class) |
| 67 | +public final class PyLongDigitsWrapper extends PythonNativeWrapper { |
| 68 | + |
| 69 | + public PyLongDigitsWrapper(int delegate) { |
| 70 | + super(delegate); |
| 71 | + } |
| 72 | + |
| 73 | + public PyLongDigitsWrapper(long delegate) { |
| 74 | + super(delegate); |
| 75 | + } |
| 76 | + |
| 77 | + public PyLongDigitsWrapper(PInt delegate) { |
| 78 | + super(delegate); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public int hashCode() { |
| 83 | + CompilerAsserts.neverPartOfCompilation(); |
| 84 | + final int prime = 31; |
| 85 | + int result = 1; |
| 86 | + result = prime * result + PythonNativeWrapperLibrary.getUncached().getDelegate(this).hashCode(); |
| 87 | + return result; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean equals(Object obj) { |
| 92 | + if (this == obj) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + if (obj == null) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + if (getClass() != obj.getClass()) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + // n.b.: (tfel) This is hopefully fine here, since if we get to this |
| 102 | + // code path, we don't speculate that either of those objects is |
| 103 | + // constant anymore, so any caching on them won't happen anyway |
| 104 | + PythonNativeWrapperLibrary lib = PythonNativeWrapperLibrary.getUncached(); |
| 105 | + return lib.getDelegate(this) == lib.getDelegate((PyLongDigitsWrapper) obj); |
| 106 | + } |
| 107 | + |
| 108 | + @ExportMessage |
| 109 | + final long getArraySize( |
| 110 | + @CachedLibrary("this") PythonNativeWrapperLibrary lib) { |
| 111 | + Object delegate = lib.getDelegate(this); |
| 112 | + if (delegate instanceof Integer) { |
| 113 | + return Integer.BYTES / 4; |
| 114 | + } else if (delegate instanceof Long) { |
| 115 | + return Long.BYTES / 4; |
| 116 | + } |
| 117 | + assert delegate instanceof PInt; |
| 118 | + return ((PInt) delegate).bitCount(); |
| 119 | + } |
| 120 | + |
| 121 | + @ExportMessage |
| 122 | + @SuppressWarnings("static-method") |
| 123 | + final boolean hasArrayElements() { |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + @ExportMessage |
| 128 | + final Object readArrayElement(long index, |
| 129 | + @CachedLibrary("this") PythonNativeWrapperLibrary lib) throws InvalidArrayIndexException { |
| 130 | + Object delegate = lib.getDelegate(this); |
| 131 | + if (delegate instanceof Integer) { |
| 132 | + if (index == 0) { |
| 133 | + return ((Number) delegate).intValue(); |
| 134 | + } |
| 135 | + } else if (delegate instanceof Long) { |
| 136 | + if (index >= 0 && index < 2) { |
| 137 | + return ((Number) delegate).intValue(); |
| 138 | + } |
| 139 | + } else { |
| 140 | + byte[] bytes = ((PInt) delegate).toByteArray(); |
| 141 | + if (index >= 0 && index < bytes.length / 4) { |
| 142 | + // the cast to int is safe since the length check already succeeded |
| 143 | + return getUInt32(bytes, (int) index); |
| 144 | + } |
| 145 | + } |
| 146 | + throw InvalidArrayIndexException.create(index); |
| 147 | + } |
| 148 | + |
| 149 | + private static long byteAsULong(byte b) { |
| 150 | + return ((long) b) & 0xFFL; |
| 151 | + } |
| 152 | + |
| 153 | + public static long getUInt32(byte[] bytes, int index) { |
| 154 | + return byteAsULong(bytes[index]) | (byteAsULong(bytes[index + 1]) << 8) | (byteAsULong(bytes[index + 2]) << 16) | (byteAsULong(bytes[index + 3]) << 24); |
| 155 | + } |
| 156 | + |
| 157 | + static int getCallSiteInlineCacheMaxDepth() { |
| 158 | + return PythonOptions.getCallSiteInlineCacheMaxDepth(); |
| 159 | + } |
| 160 | + |
| 161 | + @ExportMessage |
| 162 | + final boolean isArrayElementReadable(long identifier, |
| 163 | + @CachedLibrary("this") PythonNativeWrapperLibrary lib) { |
| 164 | + // also include the implicit null-terminator |
| 165 | + return 0 <= identifier && identifier <= getArraySize(lib); |
| 166 | + } |
| 167 | + |
| 168 | + @ExportMessage |
| 169 | + public void toNative( |
| 170 | + @CachedLibrary("this") PythonNativeWrapperLibrary lib, |
| 171 | + @Cached InvalidateNativeObjectsAllManagedNode invalidateNode, |
| 172 | + @Cached PCallCapiFunction callToNativeNode) { |
| 173 | + invalidateNode.execute(); |
| 174 | + if (!lib.isNative(this)) { |
| 175 | + Object ptr = callToNativeNode.call(NativeCAPISymbols.FUN_PY_TRUFFLE_INT_ARRAY_TO_NATIVE, this, getArraySize(lib)); |
| 176 | + setNativePointer(ptr); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + @ExportMessage |
| 181 | + public boolean isPointer( |
| 182 | + @Cached CExtNodes.IsPointerNode pIsPointerNode) { |
| 183 | + return pIsPointerNode.execute(this); |
| 184 | + } |
| 185 | + |
| 186 | + @ExportMessage |
| 187 | + public long asPointer( |
| 188 | + @CachedLibrary(limit = "1") InteropLibrary interopLibrary, |
| 189 | + @CachedLibrary("this") PythonNativeWrapperLibrary lib) throws UnsupportedMessageException { |
| 190 | + Object nativePointer = lib.getNativePointer(this); |
| 191 | + if (nativePointer instanceof Long) { |
| 192 | + return (long) nativePointer; |
| 193 | + } |
| 194 | + return interopLibrary.asPointer(nativePointer); |
| 195 | + } |
| 196 | + |
| 197 | + @ExportMessage |
| 198 | + @SuppressWarnings("static-method") |
| 199 | + protected boolean hasNativeType() { |
| 200 | + return true; |
| 201 | + } |
| 202 | + |
| 203 | + @ExportMessage |
| 204 | + abstract static class GetNativeType { |
| 205 | + |
| 206 | + static Object callGetUInt32ArrayTypeIDUncached(PyLongDigitsWrapper digitsWrapper) { |
| 207 | + return PCallCapiFunction.getUncached().call(FUN_GET_UINT32_ARRAY_TYPE_ID, 0); |
| 208 | + } |
| 209 | + |
| 210 | + @Specialization(assumptions = "singleContextAssumption()") |
| 211 | + static Object doByteArray(@SuppressWarnings("unused") PyLongDigitsWrapper object, |
| 212 | + @Exclusive @Cached("callGetUInt32ArrayTypeIDUncached(object)") Object nativeType) { |
| 213 | + return nativeType; |
| 214 | + } |
| 215 | + |
| 216 | + @Specialization(replaces = "doByteArray") |
| 217 | + static Object doByteArrayMultiCtx(@SuppressWarnings("unused") PyLongDigitsWrapper object, |
| 218 | + @Cached PCallCapiFunction callGetTypeIDNode) { |
| 219 | + return callGetTypeIDNode.call(FUN_GET_UINT32_ARRAY_TYPE_ID, 0); |
| 220 | + } |
| 221 | + |
| 222 | + protected static Assumption singleContextAssumption() { |
| 223 | + return PythonLanguage.getCurrent().singleContextAssumption; |
| 224 | + } |
| 225 | + } |
| 226 | +} |
0 commit comments