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 | / |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 18 | * |
| 19 | jump: object = None |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 20 | |
| 21 | Compute the stack effect of the opcode. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 22 | [clinic start generated code]*/ |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 23 | |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 24 | static int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 25 | _opcode_stack_effect_impl(PyObject *module, int opcode, PyObject *oparg, |
| 26 | PyObject *jump) |
| 27 | /*[clinic end generated code: output=64a18f2ead954dbb input=461c9d4a44851898]*/ |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 28 | { |
| 29 | int effect; |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 30 | int oparg_int = 0; |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 31 | int jump_int; |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 32 | if (HAS_ARG(opcode)) { |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 33 | if (oparg == Py_None) { |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 34 | PyErr_SetString(PyExc_ValueError, |
| 35 | "stack_effect: opcode requires oparg but oparg was not specified"); |
| 36 | return -1; |
| 37 | } |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 38 | oparg_int = (int)PyLong_AsLong(oparg); |
Mohamed Koubaa | 426f2b4 | 2020-09-07 03:48:44 -0500 | [diff] [blame] | 39 | if ((oparg_int == -1) && PyErr_Occurred()) { |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 40 | return -1; |
Mohamed Koubaa | 426f2b4 | 2020-09-07 03:48:44 -0500 | [diff] [blame] | 41 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 42 | } |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 43 | else if (oparg != Py_None) { |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 44 | PyErr_SetString(PyExc_ValueError, |
| 45 | "stack_effect: opcode does not permit oparg but oparg was specified"); |
| 46 | return -1; |
| 47 | } |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 48 | if (jump == Py_None) { |
| 49 | jump_int = -1; |
| 50 | } |
| 51 | else if (jump == Py_True) { |
| 52 | jump_int = 1; |
| 53 | } |
| 54 | else if (jump == Py_False) { |
| 55 | jump_int = 0; |
| 56 | } |
| 57 | else { |
| 58 | PyErr_SetString(PyExc_ValueError, |
| 59 | "stack_effect: jump must be False, True or None"); |
| 60 | return -1; |
| 61 | } |
| 62 | effect = PyCompile_OpcodeStackEffectWithJump(opcode, oparg_int, jump_int); |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 63 | if (effect == PY_INVALID_STACK_EFFECT) { |
| 64 | PyErr_SetString(PyExc_ValueError, |
| 65 | "invalid opcode or oparg"); |
| 66 | return -1; |
| 67 | } |
| 68 | return effect; |
| 69 | } |
| 70 | |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 71 | static PyMethodDef |
| 72 | opcode_functions[] = { |
| 73 | _OPCODE_STACK_EFFECT_METHODDEF |
| 74 | {NULL, NULL, 0, NULL} |
| 75 | }; |
| 76 | |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 77 | static struct PyModuleDef opcodemodule = { |
| 78 | PyModuleDef_HEAD_INIT, |
Mohamed Koubaa | 426f2b4 | 2020-09-07 03:48:44 -0500 | [diff] [blame] | 79 | .m_name = "_opcode", |
| 80 | .m_doc = "Opcode support module.", |
| 81 | .m_size = 0, |
| 82 | .m_methods = opcode_functions |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | PyMODINIT_FUNC |
| 86 | PyInit__opcode(void) |
| 87 | { |
Mohamed Koubaa | 426f2b4 | 2020-09-07 03:48:44 -0500 | [diff] [blame] | 88 | return PyModuleDef_Init(&opcodemodule); |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 89 | } |