Nuitka
The Python compiler
Loading...
Searching...
No Matches
operations.h
1// Copyright 2025, Kay Hayen, mailto:kay.hayen@gmail.com find license text at end of file
2
3#ifndef __NUITKA_OPERATIONS_H__
4#define __NUITKA_OPERATIONS_H__
5
6#if PYTHON_VERSION >= 0x300
7extern PyObject *UNICODE_CONCAT(PyThreadState *tstate, PyObject *left, PyObject *right);
8extern bool UNICODE_APPEND(PyThreadState *tstate, PyObject **p_left, PyObject *right);
9#else
10// TODO: Specialize for Python2 too.
11NUITKA_MAY_BE_UNUSED static PyObject *UNICODE_CONCAT(PyThreadState *tstate, PyObject *left, PyObject *right) {
12 return PyUnicode_Concat(left, right);
13}
14#endif
15
16// This macro is necessary for Python2 to determine if the "coerce" slot
17// will have to be considered or not, and if in-place operations are
18// allowed to be there or not.
19#if PYTHON_VERSION < 0x300
20#define NEW_STYLE_NUMBER(o) PyType_HasFeature(Py_TYPE(o), Py_TPFLAGS_CHECKTYPES)
21#define NEW_STYLE_NUMBER_TYPE(t) PyType_HasFeature(t, Py_TPFLAGS_CHECKTYPES)
22#define CAN_HAVE_INPLACE(t) PyType_HasFeature(t, Py_TPFLAGS_HAVE_INPLACEOPS)
23#else
24#define NEW_STYLE_NUMBER(o) (true)
25#define NEW_STYLE_NUMBER_TYPE(t) (true)
26#define CAN_HAVE_INPLACE(t) (true)
27#endif
28
29typedef PyObject *(unary_api)(PyObject *);
30
31NUITKA_MAY_BE_UNUSED static PyObject *UNARY_OPERATION(unary_api api, PyObject *operand) {
32 CHECK_OBJECT(operand);
33 PyObject *result = api(operand);
34
35 if (unlikely(result == NULL)) {
36 return NULL;
37 }
38
39 CHECK_OBJECT(result);
40
41 return result;
42}
43
44// Generated helpers to execute operations on fully or partially known types.
45#include "nuitka/helper/operations_binary_add.h"
46#include "nuitka/helper/operations_binary_bitand.h"
47#include "nuitka/helper/operations_binary_bitor.h"
48#include "nuitka/helper/operations_binary_bitxor.h"
49#include "nuitka/helper/operations_binary_divmod.h"
50#include "nuitka/helper/operations_binary_floordiv.h"
51#include "nuitka/helper/operations_binary_lshift.h"
52#include "nuitka/helper/operations_binary_mod.h"
53#include "nuitka/helper/operations_binary_mult.h"
54#include "nuitka/helper/operations_binary_pow.h"
55#include "nuitka/helper/operations_binary_rshift.h"
56#include "nuitka/helper/operations_binary_sub.h"
57#include "nuitka/helper/operations_binary_truediv.h"
58
59// Generated helpers to execute operations on dual types.
60#include "nuitka/helper/operations_binary_dual_add.h"
61
62#include "nuitka/helper/operations_inplace_add.h"
63#include "nuitka/helper/operations_inplace_bitand.h"
64#include "nuitka/helper/operations_inplace_bitor.h"
65#include "nuitka/helper/operations_inplace_bitxor.h"
66#include "nuitka/helper/operations_inplace_floordiv.h"
67#include "nuitka/helper/operations_inplace_lshift.h"
68#include "nuitka/helper/operations_inplace_mod.h"
69#include "nuitka/helper/operations_inplace_mult.h"
70#include "nuitka/helper/operations_inplace_pow.h"
71#include "nuitka/helper/operations_inplace_rshift.h"
72#include "nuitka/helper/operations_inplace_sub.h"
73#include "nuitka/helper/operations_inplace_truediv.h"
74
75#if PYTHON_VERSION < 0x300
76// Classical division is Python2 only.
77#include "nuitka/helper/operations_binary_olddiv.h"
78#include "nuitka/helper/operations_inplace_olddiv.h"
79#endif
80#if PYTHON_VERSION >= 0x350
81// Matrix multiplication is Python3.5 or higher only.
82#include "nuitka/helper/operations_binary_matmult.h"
83#include "nuitka/helper/operations_inplace_matmult.h"
84#endif
85
86// TODO: Get rid of the need for these, we should inline the abstract
87// algorithms, for now we have them for easier templating.
88#define PyNumber_InPlaceSub PyNumber_InPlaceSubtract
89#define PyNumber_InPlaceMult PyNumber_InPlaceMultiply
90#define PyNumber_InPlaceOlddiv PyNumber_InPlaceDivide
91#define PyNumber_InPlacePow(a, b) PyNumber_InPlacePower(a, b, Py_None)
92#define PyNumber_InPlaceMod PyNumber_InPlaceRemainder
93#define PyNumber_InPlaceBitor PyNumber_InPlaceOr
94#define PyNumber_InPlaceBitxor PyNumber_InPlaceXor
95#define PyNumber_InPlaceBitand PyNumber_InPlaceAnd
96#define PyNumber_InPlaceTruediv PyNumber_InPlaceTrueDivide
97#define PyNumber_InPlaceFloordiv PyNumber_InPlaceFloorDivide
98#define PyNumber_InPlaceMatmult PyNumber_InPlaceMatrixMultiply
99#endif
100
101// Part of "Nuitka", an optimizing Python compiler that is compatible and
102// integrates with CPython, but also works on its own.
103//
104// Licensed under the Apache License, Version 2.0 (the "License");
105// you may not use this file except in compliance with the License.
106// You may obtain a copy of the License at
107//
108// http://www.apache.org/licenses/LICENSE-2.0
109//
110// Unless required by applicable law or agreed to in writing, software
111// distributed under the License is distributed on an "AS IS" BASIS,
112// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
113// See the License for the specific language governing permissions and
114// limitations under the License.