Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "opcode.h" |
| 3 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4 | /*[clinic input] |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 5 | module _opcode |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6 | [clinic start generated code]*/ |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 7 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=117442e66eb376e6]*/ |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 8 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 9 | /*[clinic input] |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 10 | |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 11 | _opcode.stack_effect -> int |
| 12 | |
| 13 | opcode: int |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 14 | oparg: object = None |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 15 | / |
| 16 | |
| 17 | Compute the stack effect of the opcode. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 18 | [clinic start generated code]*/ |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 19 | |
| 20 | PyDoc_STRVAR(_opcode_stack_effect__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 21 | "stack_effect($module, opcode, oparg=None, /)\n" |
| 22 | "--\n" |
| 23 | "\n" |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 24 | "Compute the stack effect of the opcode."); |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 25 | |
| 26 | #define _OPCODE_STACK_EFFECT_METHODDEF \ |
| 27 | {"stack_effect", (PyCFunction)_opcode_stack_effect, METH_VARARGS, _opcode_stack_effect__doc__}, |
| 28 | |
| 29 | static int |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 30 | _opcode_stack_effect_impl(PyModuleDef *module, int opcode, PyObject *oparg); |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 31 | |
| 32 | static PyObject * |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 33 | _opcode_stack_effect(PyModuleDef *module, PyObject *args) |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 34 | { |
| 35 | PyObject *return_value = NULL; |
| 36 | int opcode; |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 37 | PyObject *oparg = Py_None; |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 38 | int _return_value; |
| 39 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 40 | if (!PyArg_ParseTuple(args, |
| 41 | "i|O:stack_effect", |
| 42 | &opcode, &oparg)) |
| 43 | goto exit; |
| 44 | _return_value = _opcode_stack_effect_impl(module, opcode, oparg); |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 45 | if ((_return_value == -1) && PyErr_Occurred()) |
| 46 | goto exit; |
| 47 | return_value = PyLong_FromLong((long)_return_value); |
| 48 | |
| 49 | exit: |
| 50 | return return_value; |
| 51 | } |
| 52 | |
| 53 | static int |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 54 | _opcode_stack_effect_impl(PyModuleDef *module, int opcode, PyObject *oparg) |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 55 | /*[clinic end generated code: output=9e1133f8d587bc67 input=2d0a9ee53c0418f5]*/ |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 56 | { |
| 57 | int effect; |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 58 | int oparg_int = 0; |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 59 | if (HAS_ARG(opcode)) { |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 60 | if (oparg == Py_None) { |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 61 | PyErr_SetString(PyExc_ValueError, |
| 62 | "stack_effect: opcode requires oparg but oparg was not specified"); |
| 63 | return -1; |
| 64 | } |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 65 | oparg_int = (int)PyLong_AsLong(oparg); |
| 66 | if ((oparg_int == -1) && PyErr_Occurred()) |
| 67 | return -1; |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 68 | } |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 69 | else if (oparg != Py_None) { |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 70 | PyErr_SetString(PyExc_ValueError, |
| 71 | "stack_effect: opcode does not permit oparg but oparg was specified"); |
| 72 | return -1; |
| 73 | } |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 74 | effect = PyCompile_OpcodeStackEffect(opcode, oparg_int); |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 75 | if (effect == PY_INVALID_STACK_EFFECT) { |
| 76 | PyErr_SetString(PyExc_ValueError, |
| 77 | "invalid opcode or oparg"); |
| 78 | return -1; |
| 79 | } |
| 80 | return effect; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | static PyMethodDef |
| 87 | opcode_functions[] = { |
| 88 | _OPCODE_STACK_EFFECT_METHODDEF |
| 89 | {NULL, NULL, 0, NULL} |
| 90 | }; |
| 91 | |
| 92 | |
| 93 | static struct PyModuleDef opcodemodule = { |
| 94 | PyModuleDef_HEAD_INIT, |
| 95 | "_opcode", |
| 96 | "Opcode support module.", |
| 97 | -1, |
| 98 | opcode_functions, |
| 99 | NULL, |
| 100 | NULL, |
| 101 | NULL, |
| 102 | NULL |
| 103 | }; |
| 104 | |
| 105 | PyMODINIT_FUNC |
| 106 | PyInit__opcode(void) |
| 107 | { |
| 108 | return PyModule_Create(&opcodemodule); |
| 109 | } |