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 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 9 | #include "clinic/_opcode.c.h" |
| 10 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 11 | /*[clinic input] |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 12 | |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 13 | _opcode.stack_effect -> int |
| 14 | |
| 15 | opcode: int |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 16 | oparg: object = None |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 17 | / |
| 18 | |
| 19 | Compute the stack effect of the opcode. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 20 | [clinic start generated code]*/ |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 21 | |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 22 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 23 | _opcode_stack_effect_impl(PyObject *module, int opcode, PyObject *oparg) |
| 24 | /*[clinic end generated code: output=ad39467fa3ad22ce input=2d0a9ee53c0418f5]*/ |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 25 | { |
| 26 | int effect; |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 27 | int oparg_int = 0; |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 28 | if (HAS_ARG(opcode)) { |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 29 | if (oparg == Py_None) { |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 30 | PyErr_SetString(PyExc_ValueError, |
| 31 | "stack_effect: opcode requires oparg but oparg was not specified"); |
| 32 | return -1; |
| 33 | } |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 34 | oparg_int = (int)PyLong_AsLong(oparg); |
| 35 | if ((oparg_int == -1) && PyErr_Occurred()) |
| 36 | return -1; |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 37 | } |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 38 | else if (oparg != Py_None) { |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 39 | PyErr_SetString(PyExc_ValueError, |
| 40 | "stack_effect: opcode does not permit oparg but oparg was specified"); |
| 41 | return -1; |
| 42 | } |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 43 | effect = PyCompile_OpcodeStackEffect(opcode, oparg_int); |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 44 | if (effect == PY_INVALID_STACK_EFFECT) { |
| 45 | PyErr_SetString(PyExc_ValueError, |
| 46 | "invalid opcode or oparg"); |
| 47 | return -1; |
| 48 | } |
| 49 | return effect; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | static PyMethodDef |
| 56 | opcode_functions[] = { |
| 57 | _OPCODE_STACK_EFFECT_METHODDEF |
| 58 | {NULL, NULL, 0, NULL} |
| 59 | }; |
| 60 | |
| 61 | |
| 62 | static struct PyModuleDef opcodemodule = { |
| 63 | PyModuleDef_HEAD_INIT, |
| 64 | "_opcode", |
| 65 | "Opcode support module.", |
| 66 | -1, |
| 67 | opcode_functions, |
| 68 | NULL, |
| 69 | NULL, |
| 70 | NULL, |
| 71 | NULL |
| 72 | }; |
| 73 | |
| 74 | PyMODINIT_FUNC |
| 75 | PyInit__opcode(void) |
| 76 | { |
| 77 | return PyModule_Create(&opcodemodule); |
| 78 | } |