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