|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, 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.builtins.modules.cext; |
| 42 | + |
| 43 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__FLOAT__; |
| 44 | +import java.util.List; |
| 45 | +import com.oracle.graal.python.builtins.Builtin; |
| 46 | +import com.oracle.graal.python.builtins.CoreFunctions; |
| 47 | +import com.oracle.graal.python.builtins.Python3Core; |
| 48 | +import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
| 49 | +import com.oracle.graal.python.builtins.PythonBuiltins; |
| 50 | +import com.oracle.graal.python.builtins.modules.BuiltinConstructors.ComplexNode; |
| 51 | +import com.oracle.graal.python.builtins.objects.PNone; |
| 52 | +import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.GetNativeNullNode; |
| 53 | +import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodes.TransformExceptionToNativeNode; |
| 54 | +import com.oracle.graal.python.builtins.objects.complex.PComplex; |
| 55 | +import com.oracle.graal.python.builtins.objects.tuple.PTuple; |
| 56 | +import com.oracle.graal.python.lib.PyObjectGetAttr; |
| 57 | +import com.oracle.graal.python.nodes.call.CallNode; |
| 58 | +import com.oracle.graal.python.nodes.classes.IsSubtypeNode; |
| 59 | +import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode; |
| 60 | +import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode; |
| 61 | +import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode; |
| 62 | +import com.oracle.graal.python.nodes.object.GetClassNode; |
| 63 | +import com.oracle.graal.python.runtime.exception.PException; |
| 64 | +import com.oracle.truffle.api.dsl.Cached; |
| 65 | +import com.oracle.truffle.api.dsl.GenerateNodeFactory; |
| 66 | +import com.oracle.truffle.api.dsl.NodeFactory; |
| 67 | +import com.oracle.truffle.api.dsl.Specialization; |
| 68 | +import com.oracle.truffle.api.frame.VirtualFrame; |
| 69 | + |
| 70 | +@CoreFunctions(defineModule = PythonCextComplexBuiltins.PYTHON_CEXT_COMPLEX) |
| 71 | +@GenerateNodeFactory |
| 72 | +public class PythonCextComplexBuiltins extends PythonBuiltins { |
| 73 | + |
| 74 | + public static final String PYTHON_CEXT_COMPLEX = "python_cext_complex"; |
| 75 | + |
| 76 | + @Override |
| 77 | + protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() { |
| 78 | + return PythonCextComplexBuiltinsFactory.getFactories(); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void initialize(Python3Core core) { |
| 83 | + super.initialize(core); |
| 84 | + } |
| 85 | + |
| 86 | + ///////////// complex ///////////// |
| 87 | + |
| 88 | + @Builtin(name = "PyComplex_AsCComplex", minNumOfPositionalArgs = 1) |
| 89 | + @GenerateNodeFactory |
| 90 | + abstract static class PyComplexAsCComplexNode extends PythonUnaryBuiltinNode { |
| 91 | + @Specialization |
| 92 | + PTuple asComplex(PComplex c) { |
| 93 | + return factory().createTuple(new Object[]{c.getReal(), c.getImag()}); |
| 94 | + } |
| 95 | + |
| 96 | + @Specialization(guards = "!isPComplex(obj)") |
| 97 | + Object asComplex(VirtualFrame frame, Object obj, |
| 98 | + @Cached ComplexNode complexNode, |
| 99 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 100 | + @Cached GetNativeNullNode getNativeNullNode) { |
| 101 | + try { |
| 102 | + PComplex c = (PComplex) complexNode.execute(frame, PythonBuiltinClassType.PComplex, obj, PNone.NO_VALUE); |
| 103 | + return factory().createTuple(new Object[]{c.getReal(), c.getImag()}); |
| 104 | + } catch (PException e) { |
| 105 | + transformExceptionToNativeNode.execute(e); |
| 106 | + return getNativeNullNode.execute(); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Builtin(name = "PyComplex_RealAsDouble", minNumOfPositionalArgs = 1) |
| 112 | + @GenerateNodeFactory |
| 113 | + abstract static class PyComplexRealAsDoubleNode extends PythonUnaryBuiltinNode { |
| 114 | + |
| 115 | + @Specialization |
| 116 | + double asDouble(PComplex d) { |
| 117 | + return d.getReal(); |
| 118 | + } |
| 119 | + |
| 120 | + @Specialization(guards = {"!isPComplex(obj)", "isComplexSubtype(frame, obj, getClassNode, isSubtypeNode)"}) |
| 121 | + public Object asDouble(VirtualFrame frame, Object obj, |
| 122 | + @Cached PyObjectGetAttr getAttr, |
| 123 | + @Cached CallNode callNode, |
| 124 | + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, |
| 125 | + @SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode, |
| 126 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode) { |
| 127 | + try { |
| 128 | + return callNode.execute(getAttr.execute(frame, obj, "real")); |
| 129 | + } catch (PException e) { |
| 130 | + transformExceptionToNativeNode.execute(e); |
| 131 | + return -1.0; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Specialization(guards = {"!isPComplex(obj)", "!isComplexSubtype(frame, obj, getClassNode, isSubtypeNode)"}) |
| 136 | + public Object asDoubleFloat(VirtualFrame frame, Object obj, |
| 137 | + @Cached PyObjectGetAttr getAttr, |
| 138 | + @Cached CallNode callNode, |
| 139 | + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, |
| 140 | + @SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode, |
| 141 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode) { |
| 142 | + try { |
| 143 | + return callNode.execute(getAttr.execute(frame, obj, __FLOAT__)); |
| 144 | + } catch (PException e) { |
| 145 | + transformExceptionToNativeNode.execute(e); |
| 146 | + return -1.0; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + protected boolean isComplexSubtype(VirtualFrame frame, Object obj, GetClassNode getClassNode, IsSubtypeNode isSubtypeNode) { |
| 151 | + return isSubtypeNode.execute(frame, getClassNode.execute(obj), PythonBuiltinClassType.PComplex); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + @Builtin(name = "PyComplex_ImagAsDouble", minNumOfPositionalArgs = 1) |
| 156 | + @GenerateNodeFactory |
| 157 | + abstract static class PyComplexImagAsDoubleNode extends PythonUnaryBuiltinNode { |
| 158 | + |
| 159 | + @Specialization |
| 160 | + double asDouble(PComplex d) { |
| 161 | + return d.getImag(); |
| 162 | + } |
| 163 | + |
| 164 | + @Specialization(guards = {"!isPComplex(obj)", "isComplexSubtype(frame, obj, getClassNode, isSubtypeNode)"}) |
| 165 | + public Object asDouble(VirtualFrame frame, Object obj, |
| 166 | + @Cached PyObjectGetAttr getAttr, |
| 167 | + @Cached CallNode callNode, |
| 168 | + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, |
| 169 | + @SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode, |
| 170 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode) { |
| 171 | + try { |
| 172 | + return callNode.execute(getAttr.execute(frame, obj, "imag")); |
| 173 | + } catch (PException e) { |
| 174 | + transformExceptionToNativeNode.execute(e); |
| 175 | + return -1; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + @SuppressWarnings("unused") |
| 180 | + @Specialization(guards = {"!isPComplex(obj)", "!isComplexSubtype(frame, obj, getClassNode, isSubtypeNode)"}) |
| 181 | + public Object asDouble(VirtualFrame frame, Object obj, |
| 182 | + @Cached GetClassNode getClassNode, |
| 183 | + @Cached IsSubtypeNode isSubtypeNode) { |
| 184 | + return 0.0; |
| 185 | + } |
| 186 | + |
| 187 | + protected boolean isComplexSubtype(VirtualFrame frame, Object obj, GetClassNode getClassNode, IsSubtypeNode isSubtypeNode) { |
| 188 | + return isSubtypeNode.execute(frame, getClassNode.execute(obj), PythonBuiltinClassType.PComplex); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + @Builtin(name = "PyComplex_FromDoubles", minNumOfPositionalArgs = 1) |
| 193 | + @GenerateNodeFactory |
| 194 | + abstract static class PyComplexFromDoublesNode extends PythonBinaryBuiltinNode { |
| 195 | + |
| 196 | + @Specialization |
| 197 | + public PComplex asDouble(double r, double i) { |
| 198 | + return factory().createComplex(r, i); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments