summaryrefslogtreecommitdiffstats
path: root/Modules/_opcode.c
blob: cc72cb170ceaed434ebd5ace766d4367c5e72e31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#ifndef Py_BUILD_CORE_BUILTIN
#  define Py_BUILD_CORE_MODULE 1
#endif

#include "Python.h"
#include "compile.h"
#include "opcode.h"
#include "internal/pycore_code.h"
#include "internal/pycore_compile.h"
#include "internal/pycore_intrinsics.h"

/*[clinic input]
module _opcode
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=117442e66eb376e6]*/

#include "clinic/_opcode.c.h"

/*[clinic input]

_opcode.stack_effect -> int

  opcode: int
  oparg: object = None
  /
  *
  jump: object = None

Compute the stack effect of the opcode.
[clinic start generated code]*/

static int
_opcode_stack_effect_impl(PyObject *module, int opcode, PyObject *oparg,
                          PyObject *jump)
/*[clinic end generated code: output=64a18f2ead954dbb input=461c9d4a44851898]*/
{
    int oparg_int = 0;
    int jump_int;

    if (oparg != Py_None) {
        oparg_int = (int)PyLong_AsLong(oparg);
        if ((oparg_int == -1) && PyErr_Occurred()) {
            return -1;
        }
    }

    if (jump == Py_None) {
        jump_int = -1;
    }
    else if (jump == Py_True) {
        jump_int = 1;
    }
    else if (jump == Py_False) {
        jump_int = 0;
    }
    else {
        PyErr_SetString(PyExc_ValueError,
                "stack_effect: jump must be False, True or None");
        return -1;
    }
    int effect = PyCompile_OpcodeStackEffectWithJump(opcode, oparg_int, jump_int);
    if (effect == PY_INVALID_STACK_EFFECT) {
        PyErr_SetString(PyExc_ValueError, "invalid opcode or oparg");
        return -1;
    }
    return effect;
}

/*[clinic input]

_opcode.is_valid -> bool

  opcode: int

Return True if opcode is valid, False otherwise.
[clinic start generated code]*/

static int
_opcode_is_valid_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=b0d918ea1d073f65 input=fe23e0aa194ddae0]*/
{
    return _PyCompile_OpcodeIsValid(opcode);
}

/*[clinic input]

_opcode.has_arg -> bool

  opcode: int

Return True if the opcode uses its oparg, False otherwise.
[clinic start generated code]*/

static int
_opcode_has_arg_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=7a062d3b2dcc0815 input=93d878ba6361db5f]*/
{
    return _PyCompile_OpcodeIsValid(opcode) &&
           _PyCompile_OpcodeHasArg(opcode);
}

/*[clinic input]

_opcode.has_const -> bool

  opcode: int

Return True if the opcode accesses a constant, False otherwise.
[clinic start generated code]*/

static int
_opcode_has_const_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=c646d5027c634120 input=a6999e4cf13f9410]*/
{
    return _PyCompile_OpcodeIsValid(opcode) &&
           _PyCompile_OpcodeHasConst(opcode);
}

/*[clinic input]

_opcode.has_name -> bool

  opcode: int

Return True if the opcode accesses an attribute by name, False otherwise.
[clinic start generated code]*/

static int
_opcode_has_name_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=b49a83555c2fa517 input=448aa5e4bcc947ba]*/
{
    return _PyCompile_OpcodeIsValid(opcode) &&
           _PyCompile_OpcodeHasName(opcode);
}

/*[clinic input]

_opcode.has_jump -> bool

  opcode: int

Return True if the opcode has a jump target, False otherwise.
[clinic start generated code]*/

static int
_opcode_has_jump_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=e9c583c669f1c46a input=35f711274357a0c3]*/
{
    return _PyCompile_OpcodeIsValid(opcode) &&
           _PyCompile_OpcodeHasJump(opcode);

}

/*[clinic input]

_opcode.has_free -> bool

  opcode: int

Return True if the opcode accesses a free variable, False otherwise.

Note that 'free' in this context refers to names in the current scope
that are referenced by inner scopes or names in outer scopes that are
referenced from this scope. It does not include references to global
or builtin scopes.
[clinic start generated code]*/

static int
_opcode_has_free_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=d81ae4d79af0ee26 input=117dcd5c19c1139b]*/
{
    return _PyCompile_OpcodeIsValid(opcode) &&
           _PyCompile_OpcodeHasFree(opcode);

}

/*[clinic input]

_opcode.has_local -> bool

  opcode: int

Return True if the opcode accesses a local variable, False otherwise.
[clinic start generated code]*/

static int
_opcode_has_local_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=da5a8616b7a5097b input=9a798ee24aaef49d]*/
{
    return _PyCompile_OpcodeIsValid(opcode) &&
           _PyCompile_OpcodeHasLocal(opcode);
}

/*[clinic input]

_opcode.has_exc -> bool

  opcode: int

Return True if the opcode sets an exception handler, False otherwise.
[clinic start generated code]*/

static int
_opcode_has_exc_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=41b68dff0ec82a52 input=db0e4bdb9bf13fa5]*/
{
    return _PyCompile_OpcodeIsValid(opcode) &&
           _PyCompile_OpcodeHasExc(opcode);
}

/*[clinic input]

_opcode.get_specialization_stats

Return the specialization stats
[clinic start generated code]*/

static PyObject *
_opcode_get_specialization_stats_impl(PyObject *module)
/*[clinic end generated code: output=fcbc32fdfbec5c17 input=e1f60db68d8ce5f6]*/
{
#ifdef Py_STATS
    return _Py_GetSpecializationStats();
#else
    Py_RETURN_NONE;
#endif
}

/*[clinic input]

_opcode.get_nb_ops

Return array of symbols of binary ops.

Indexed by the BINARY_OP oparg value.
[clinic start generated code]*/

static PyObject *
_opcode_get_nb_ops_impl(PyObject *module)
/*[clinic end generated code: output=d997d306cc15426f input=9462fc544c823176]*/
{
    PyObject *list = PyList_New(NB_OPARG_LAST + 1);
    if (list == NULL) {
        return NULL;
    }
#define ADD_NB_OP(NUM, STR) \
    do { \
        PyObject *pair = Py_BuildValue("ss", #NUM, STR); \
        if (pair == NULL) { \
            Py_DECREF(list); \
            return NULL; \
        } \
        PyList_SET_ITEM(list, (NUM), pair); \
    } while(0);

    ADD_NB_OP(NB_ADD, "+");
    ADD_NB_OP(NB_AND, "&");
    ADD_NB_OP(NB_FLOOR_DIVIDE, "//");
    ADD_NB_OP(NB_LSHIFT, "<<");
    ADD_NB_OP(NB_MATRIX_MULTIPLY, "@");
    ADD_NB_OP(NB_MULTIPLY, "*");
    ADD_NB_OP(NB_REMAINDER, "%");
    ADD_NB_OP(NB_OR, "|");
    ADD_NB_OP(NB_POWER, "**");
    ADD_NB_OP(NB_RSHIFT, ">>");
    ADD_NB_OP(NB_SUBTRACT, "-");
    ADD_NB_OP(NB_TRUE_DIVIDE, "/");
    ADD_NB_OP(NB_XOR, "^");
    ADD_NB_OP(NB_INPLACE_ADD, "+=");
    ADD_NB_OP(NB_INPLACE_AND, "&=");
    ADD_NB_OP(NB_INPLACE_FLOOR_DIVIDE, "//=");
    ADD_NB_OP(NB_INPLACE_LSHIFT, "<<=");
    ADD_NB_OP(NB_INPLACE_MATRIX_MULTIPLY, "@=");
    ADD_NB_OP(NB_INPLACE_MULTIPLY, "*=");
    ADD_NB_OP(NB_INPLACE_REMAINDER, "%=");
    ADD_NB_OP(NB_INPLACE_OR, "|=");
    ADD_NB_OP(NB_INPLACE_POWER, "**=");
    ADD_NB_OP(NB_INPLACE_RSHIFT, ">>=");
    ADD_NB_OP(NB_INPLACE_SUBTRACT, "-=");
    ADD_NB_OP(NB_INPLACE_TRUE_DIVIDE, "/=");
    ADD_NB_OP(NB_INPLACE_XOR, "^=");

#undef ADD_NB_OP

    for(int i = 0; i <= NB_OPARG_LAST; i++) {
        if (PyList_GET_ITEM(list, i) == NULL) {
            Py_DECREF(list);
            PyErr_Format(PyExc_ValueError,
                         "Missing initialization for NB_OP %d",
                         i);
            return NULL;
        }
    }
    return list;
}

/*[clinic input]

_opcode.get_intrinsic1_descs

Return a list of names of the unary intrinsics.
[clinic start generated code]*/

static PyObject *
_opcode_get_intrinsic1_descs_impl(PyObject *module)
/*[clinic end generated code: output=bd1ddb6b4447d18b input=13b51c712618459b]*/
{
    PyObject *list = PyList_New(MAX_INTRINSIC_1 + 1);
    if (list == NULL) {
        return NULL;
    }
    for (int i=0; i <= MAX_INTRINSIC_1; i++) {
        PyObject *name = _PyCompile_GetUnaryIntrinsicName(i);
        if (name == NULL) {
            Py_DECREF(list);
            return NULL;
        }
        PyList_SET_ITEM(list, i, name);
    }
    return list;
}


/*[clinic input]

_opcode.get_intrinsic2_descs

Return a list of names of the binary intrinsics.
[clinic start generated code]*/

static PyObject *
_opcode_get_intrinsic2_descs_impl(PyObject *module)
/*[clinic end generated code: output=40e62bc27584c8a0 input=e83068f249f5471b]*/
{
    PyObject *list = PyList_New(MAX_INTRINSIC_2 + 1);
    if (list == NULL) {
        return NULL;
    }
    for (int i=0; i <= MAX_INTRINSIC_2; i++) {
        PyObject *name = _PyCompile_GetBinaryIntrinsicName(i);
        if (name == NULL) {
            Py_DECREF(list);
            return NULL;
        }
        PyList_SET_ITEM(list, i, name);
    }
    return list;
}

/*[clinic input]

_opcode.get_executor

  code: object
  offset: int

Return the executor object at offset in code if exists, None otherwise.
[clinic start generated code]*/

static PyObject *
_opcode_get_executor_impl(PyObject *module, PyObject *code, int offset)
/*[clinic end generated code: output=c035c7a47b16648f input=85eff93ea7aac282]*/
{
    if (!PyCode_Check(code)) {
        PyErr_Format(PyExc_TypeError,
                     "expected a code object, not '%.100s'",
                     Py_TYPE(code)->tp_name);
        return NULL;
    }
#ifdef _Py_TIER2
    return (PyObject *)PyUnstable_GetExecutor((PyCodeObject *)code, offset);
#else
    PyErr_Format(PyExc_RuntimeError,
                 "Executors are not available in this build");
    return NULL;
#endif
}

static PyMethodDef
opcode_functions[] =  {
    _OPCODE_STACK_EFFECT_METHODDEF
    _OPCODE_IS_VALID_METHODDEF
    _OPCODE_HAS_ARG_METHODDEF
    _OPCODE_HAS_CONST_METHODDEF
    _OPCODE_HAS_NAME_METHODDEF
    _OPCODE_HAS_JUMP_METHODDEF
    _OPCODE_HAS_FREE_METHODDEF
    _OPCODE_HAS_LOCAL_METHODDEF
    _OPCODE_HAS_EXC_METHODDEF
    _OPCODE_GET_SPECIALIZATION_STATS_METHODDEF
    _OPCODE_GET_NB_OPS_METHODDEF
    _OPCODE_GET_INTRINSIC1_DESCS_METHODDEF
    _OPCODE_GET_INTRINSIC2_DESCS_METHODDEF
    _OPCODE_GET_EXECUTOR_METHODDEF
    {NULL, NULL, 0, NULL}
};

int
_opcode_exec(PyObject *m) {
    if (PyModule_AddIntMacro(m, ENABLE_SPECIALIZATION) < 0) {
        return -1;
    }
    return 0;
}

static PyModuleDef_Slot module_slots[] = {
    {Py_mod_exec, _opcode_exec},
    {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
    {Py_mod_gil, Py_MOD_GIL_NOT_USED},
    {0, NULL}
};

static struct PyModuleDef opcodemodule = {
    PyModuleDef_HEAD_INIT,
    .m_name = "_opcode",
    .m_doc = "Opcode support module.",
    .m_size = 0,
    .m_methods = opcode_functions,
    .m_slots = module_slots,
};

PyMODINIT_FUNC
PyInit__opcode(void)
{
    return PyModuleDef_Init(&opcodemodule);
}