|
213 | 213 | import com.oracle.graal.python.builtins.objects.dict.PDict;
|
214 | 214 | import com.oracle.graal.python.builtins.objects.ellipsis.PEllipsis;
|
215 | 215 | import com.oracle.graal.python.builtins.objects.exception.PBaseException;
|
| 216 | +import com.oracle.graal.python.builtins.objects.floats.FloatBuiltins.IntNode; |
216 | 217 | import com.oracle.graal.python.builtins.objects.frame.PFrame;
|
217 | 218 | import com.oracle.graal.python.builtins.objects.frame.PFrame.Reference;
|
218 | 219 | import com.oracle.graal.python.builtins.objects.function.PArguments;
|
|
221 | 222 | import com.oracle.graal.python.builtins.objects.function.PKeyword;
|
222 | 223 | import com.oracle.graal.python.builtins.objects.function.Signature;
|
223 | 224 | import com.oracle.graal.python.builtins.objects.getsetdescriptor.GetSetDescriptor;
|
| 225 | +import com.oracle.graal.python.builtins.objects.ints.IntBuiltins.GtNode; |
| 226 | +import com.oracle.graal.python.builtins.objects.ints.IntBuiltins.LtNode; |
| 227 | +import com.oracle.graal.python.builtins.objects.ints.IntBuiltins.NegNode; |
224 | 228 | import com.oracle.graal.python.builtins.objects.ints.PInt;
|
225 | 229 | import com.oracle.graal.python.builtins.objects.iterator.PSequenceIterator;
|
226 | 230 | import com.oracle.graal.python.builtins.objects.list.ListBuiltins;
|
@@ -2215,7 +2219,151 @@ protected boolean isListSubtype(VirtualFrame frame, Object obj, GetClassNode get
|
2215 | 2219 | return isSubtypeNode.execute(frame, getClassNode.execute(obj), PythonBuiltinClassType.PList);
|
2216 | 2220 | }
|
2217 | 2221 | }
|
| 2222 | + |
| 2223 | + ///////////// long ///////////// |
2218 | 2224 |
|
| 2225 | + @Builtin(name = "_PyLong_Sign", minNumOfPositionalArgs = 1) |
| 2226 | + @GenerateNodeFactory |
| 2227 | + abstract static class PyLongSignNode extends PythonUnaryBuiltinNode { |
| 2228 | + |
| 2229 | + @SuppressWarnings("unused") |
| 2230 | + @Specialization(guards = "n == 0") |
| 2231 | + int sign(int n) { |
| 2232 | + return 0; |
| 2233 | + } |
| 2234 | + |
| 2235 | + @SuppressWarnings("unused") |
| 2236 | + @Specialization(guards = "n < 0") |
| 2237 | + int signNeg(int n) { |
| 2238 | + return -1; |
| 2239 | + } |
| 2240 | + |
| 2241 | + @SuppressWarnings("unused") |
| 2242 | + @Specialization(guards = "n > 0") |
| 2243 | + int signPos(int n) { |
| 2244 | + return 1; |
| 2245 | + } |
| 2246 | + |
| 2247 | + @SuppressWarnings("unused") |
| 2248 | + @Specialization(guards = "n == 0") |
| 2249 | + int sign(long n) { |
| 2250 | + return 0; |
| 2251 | + } |
| 2252 | + |
| 2253 | + @SuppressWarnings("unused") |
| 2254 | + @Specialization(guards = "n < 0") |
| 2255 | + int signNeg(long n) { |
| 2256 | + return -1; |
| 2257 | + } |
| 2258 | + |
| 2259 | + @SuppressWarnings("unused") |
| 2260 | + @Specialization(guards = "n > 0") |
| 2261 | + int signPos(long n) { |
| 2262 | + return 1; |
| 2263 | + } |
| 2264 | + |
| 2265 | + @SuppressWarnings("unused") |
| 2266 | + @Specialization(guards = "b") |
| 2267 | + int signTrue(boolean b) { |
| 2268 | + return 1; |
| 2269 | + } |
| 2270 | + |
| 2271 | + @SuppressWarnings("unused") |
| 2272 | + @Specialization(guards = "!b") |
| 2273 | + int signFalse(boolean b) { |
| 2274 | + return 0; |
| 2275 | + } |
| 2276 | + |
| 2277 | + @Specialization |
| 2278 | + int sign(VirtualFrame frame, PInt n, |
| 2279 | + @Cached LtNode ltNode, |
| 2280 | + @Cached GtNode gtNode, |
| 2281 | + @Cached BranchProfile gtProfile, |
| 2282 | + @Cached BranchProfile ltProfile) { |
| 2283 | + if ((boolean) gtNode.execute(frame, n, 0)) { |
| 2284 | + gtProfile.enter(); |
| 2285 | + return 1; |
| 2286 | + } else if ((boolean) ltNode.execute(frame, n, 0)) { |
| 2287 | + ltProfile.enter(); |
| 2288 | + return -1; |
| 2289 | + } else { |
| 2290 | + return 0; |
| 2291 | + } |
| 2292 | + } |
| 2293 | + |
| 2294 | + @SuppressWarnings("unused") |
| 2295 | + @Specialization(guards = {"!canBeInteger(obj)", "isPIntSubtype(frame, obj, getClassNode, isSubtypeNode)"}) |
| 2296 | + public Object signNative(VirtualFrame frame, Object obj, |
| 2297 | + @Cached GetClassNode getClassNode, |
| 2298 | + @Cached IsSubtypeNode isSubtypeNode) { |
| 2299 | + // function returns int, but -1 is expected result for 'n < 0' |
| 2300 | + throw CompilerDirectives.shouldNotReachHere("not yet implemented"); |
| 2301 | + } |
| 2302 | + |
| 2303 | + @Specialization(guards = {"!isInteger(obj)", "!isPInt(obj)", "!isPIntSubtype(frame, obj,getClassNode,isSubtypeNode)"}) |
| 2304 | + public Object sign(VirtualFrame frame, Object obj, |
| 2305 | + @SuppressWarnings("unused") @Cached GetClassNode getClassNode, |
| 2306 | + @SuppressWarnings("unused") @Cached IsSubtypeNode isSubtypeNode) { |
| 2307 | + // assert(PyLong_Check(v)); |
| 2308 | + throw CompilerDirectives.shouldNotReachHere(); |
| 2309 | + } |
| 2310 | + |
| 2311 | + protected boolean isPIntSubtype(VirtualFrame frame, Object obj, GetClassNode getClassNode, IsSubtypeNode isSubtypeNode) { |
| 2312 | + return isSubtypeNode.execute(frame, getClassNode.execute(obj), PythonBuiltinClassType.PInt); |
| 2313 | + } |
| 2314 | + } |
| 2315 | + |
| 2316 | + @Builtin(name = "PyLong_FromDouble", minNumOfPositionalArgs = 1) |
| 2317 | + @GenerateNodeFactory |
| 2318 | + abstract static class PyLongFromDoubleNode extends PythonUnaryBuiltinNode { |
| 2319 | + |
| 2320 | + @Specialization |
| 2321 | + Object fromDouble(VirtualFrame frame, double d, |
| 2322 | + @Cached IntNode intNode, |
| 2323 | + @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 2324 | + @Cached GetNativeNullNode getNativeNullNode) { |
| 2325 | + try { |
| 2326 | + return intNode.execute(frame, d); |
| 2327 | + } catch (PException e) { |
| 2328 | + transformExceptionToNativeNode.execute(e); |
| 2329 | + return getNativeNullNode.execute(); |
| 2330 | + } |
| 2331 | + } |
| 2332 | + } |
| 2333 | + |
| 2334 | + @Builtin(name = "PyLong_FromString", minNumOfPositionalArgs = 3) |
| 2335 | + @TypeSystemReference(PythonTypes.class) |
| 2336 | + @GenerateNodeFactory |
| 2337 | + abstract static class PyLongFromStringNode extends PythonTernaryBuiltinNode { |
| 2338 | + |
| 2339 | + @Specialization(guards = "negative == 0") |
| 2340 | + Object fromString(VirtualFrame frame, String s, long base, @SuppressWarnings("unused") long negative, |
| 2341 | + @Cached com.oracle.graal.python.builtins.modules.BuiltinConstructors.IntNode intNode, |
| 2342 | + @Shared("transforEx") @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 2343 | + @Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) { |
| 2344 | + try { |
| 2345 | + return intNode.executeWith(frame, s, base); |
| 2346 | + } catch (PException e) { |
| 2347 | + transformExceptionToNativeNode.execute(e); |
| 2348 | + return getNativeNullNode.execute(); |
| 2349 | + } |
| 2350 | + } |
| 2351 | + |
| 2352 | + @Specialization(guards = "negative != 0") |
| 2353 | + Object fromString(VirtualFrame frame, String s, long base, @SuppressWarnings("unused") long negative, |
| 2354 | + @Cached com.oracle.graal.python.builtins.modules.BuiltinConstructors.IntNode intNode, |
| 2355 | + @Cached NegNode negNode, |
| 2356 | + @Shared("transforEx") @Cached TransformExceptionToNativeNode transformExceptionToNativeNode, |
| 2357 | + @Shared("nativeNull") @Cached GetNativeNullNode getNativeNullNode) { |
| 2358 | + try { |
| 2359 | + return negNode.execute(frame, intNode.executeWith(frame, s, base)); |
| 2360 | + } catch (PException e) { |
| 2361 | + transformExceptionToNativeNode.execute(e); |
| 2362 | + return getNativeNullNode.execute(); |
| 2363 | + } |
| 2364 | + } |
| 2365 | + } |
| 2366 | + |
2219 | 2367 | /**
|
2220 | 2368 | * This is used in the ExternalFunctionNode below, so all arguments passed from Python code into
|
2221 | 2369 | * a C function are automatically unwrapped if they are wrapped. This function is also called
|
|
0 commit comments