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