|
| 1 | +#include "Python.h" |
| 2 | +#include "opcode.h" |
| 3 | + |
| 4 | + |
| 5 | +/*[clinic] |
| 6 | +
|
| 7 | +module _opcode |
| 8 | +
|
| 9 | +_opcode.stack_effect -> int |
| 10 | +
|
| 11 | + opcode: int |
| 12 | +
|
| 13 | + [ |
| 14 | + oparg: int |
| 15 | + ] |
| 16 | + / |
| 17 | +
|
| 18 | +Compute the stack effect of the opcode. |
| 19 | +[clinic]*/ |
| 20 | + |
| 21 | +PyDoc_STRVAR(_opcode_stack_effect__doc__, |
| 22 | +"Compute the stack effect of the opcode.\n" |
| 23 | +"\n" |
| 24 | +"_opcode.stack_effect(opcode, [oparg])"); |
| 25 | + |
| 26 | +#define _OPCODE_STACK_EFFECT_METHODDEF \ |
| 27 | + {"stack_effect", (PyCFunction)_opcode_stack_effect, METH_VARARGS, _opcode_stack_effect__doc__}, |
| 28 | + |
| 29 | +static int |
| 30 | +_opcode_stack_effect_impl(PyObject *module, int opcode, int group_right_1, int oparg); |
| 31 | + |
| 32 | +static PyObject * |
| 33 | +_opcode_stack_effect(PyObject *module, PyObject *args) |
| 34 | +{ |
| 35 | + PyObject *return_value = NULL; |
| 36 | + int opcode; |
| 37 | + int group_right_1 = 0; |
| 38 | + int oparg = 0; |
| 39 | + int _return_value; |
| 40 | + |
| 41 | + switch (PyTuple_Size(args)) { |
| 42 | + case 1: |
| 43 | + if (!PyArg_ParseTuple(args, "i:stack_effect", &opcode)) |
| 44 | + return NULL; |
| 45 | + break; |
| 46 | + case 2: |
| 47 | + if (!PyArg_ParseTuple(args, "ii:stack_effect", &opcode, &oparg)) |
| 48 | + return NULL; |
| 49 | + group_right_1 = 1; |
| 50 | + break; |
| 51 | + default: |
| 52 | + PyErr_SetString(PyExc_TypeError, "_opcode.stack_effect requires 1 to 2 arguments"); |
| 53 | + return NULL; |
| 54 | + } |
| 55 | + _return_value = _opcode_stack_effect_impl(module, opcode, group_right_1, oparg); |
| 56 | + if ((_return_value == -1) && PyErr_Occurred()) |
| 57 | + goto exit; |
| 58 | + return_value = PyLong_FromLong((long)_return_value); |
| 59 | + |
| 60 | +exit: |
| 61 | + return return_value; |
| 62 | +} |
| 63 | + |
| 64 | +static int |
| 65 | +_opcode_stack_effect_impl(PyObject *module, int opcode, int group_right_1, int oparg) |
| 66 | +/*[clinic checksum: 2312ded40abc9bcbce718942de21f53e61a2dfd3]*/ |
| 67 | +{ |
| 68 | + int effect; |
| 69 | + if (HAS_ARG(opcode)) { |
| 70 | + if (!group_right_1) { |
| 71 | + PyErr_SetString(PyExc_ValueError, |
| 72 | + "stack_effect: opcode requires oparg but oparg was not specified"); |
| 73 | + return -1; |
| 74 | + } |
| 75 | + } |
| 76 | + else if (group_right_1) { |
| 77 | + PyErr_SetString(PyExc_ValueError, |
| 78 | + "stack_effect: opcode does not permit oparg but oparg was specified"); |
| 79 | + return -1; |
| 80 | + } |
| 81 | + effect = PyCompile_OpcodeStackEffect(opcode, oparg); |
| 82 | + if (effect == PY_INVALID_STACK_EFFECT) { |
| 83 | + PyErr_SetString(PyExc_ValueError, |
| 84 | + "invalid opcode or oparg"); |
| 85 | + return -1; |
| 86 | + } |
| 87 | + return effect; |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +static PyMethodDef |
| 94 | +opcode_functions[] = { |
| 95 | + _OPCODE_STACK_EFFECT_METHODDEF |
| 96 | + {NULL, NULL, 0, NULL} |
| 97 | +}; |
| 98 | + |
| 99 | + |
| 100 | +static struct PyModuleDef opcodemodule = { |
| 101 | + PyModuleDef_HEAD_INIT, |
| 102 | + "_opcode", |
| 103 | + "Opcode support module.", |
| 104 | + -1, |
| 105 | + opcode_functions, |
| 106 | + NULL, |
| 107 | + NULL, |
| 108 | + NULL, |
| 109 | + NULL |
| 110 | +}; |
| 111 | + |
| 112 | +PyMODINIT_FUNC |
| 113 | +PyInit__opcode(void) |
| 114 | +{ |
| 115 | + return PyModule_Create(&opcodemodule); |
| 116 | +} |
0 commit comments