You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.6 KiB

  1. #include "Python.h"
  2. #include "opcode.h"
  3. /*[clinic input]
  4. module _opcode
  5. [clinic start generated code]*/
  6. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=117442e66eb376e6]*/
  7. /*[clinic input]
  8. _opcode.stack_effect -> int
  9. opcode: int
  10. oparg: object = None
  11. /
  12. Compute the stack effect of the opcode.
  13. [clinic start generated code]*/
  14. PyDoc_STRVAR(_opcode_stack_effect__doc__,
  15. "stack_effect($module, opcode, oparg=None, /)\n"
  16. "--\n"
  17. "\n"
  18. "Compute the stack effect of the opcode.");
  19. #define _OPCODE_STACK_EFFECT_METHODDEF \
  20. {"stack_effect", (PyCFunction)_opcode_stack_effect, METH_VARARGS, _opcode_stack_effect__doc__},
  21. static int
  22. _opcode_stack_effect_impl(PyModuleDef *module, int opcode, PyObject *oparg);
  23. static PyObject *
  24. _opcode_stack_effect(PyModuleDef *module, PyObject *args)
  25. {
  26. PyObject *return_value = NULL;
  27. int opcode;
  28. PyObject *oparg = Py_None;
  29. int _return_value;
  30. if (!PyArg_ParseTuple(args,
  31. "i|O:stack_effect",
  32. &opcode, &oparg))
  33. goto exit;
  34. _return_value = _opcode_stack_effect_impl(module, opcode, oparg);
  35. if ((_return_value == -1) && PyErr_Occurred())
  36. goto exit;
  37. return_value = PyLong_FromLong((long)_return_value);
  38. exit:
  39. return return_value;
  40. }
  41. static int
  42. _opcode_stack_effect_impl(PyModuleDef *module, int opcode, PyObject *oparg)
  43. /*[clinic end generated code: output=9e1133f8d587bc67 input=2d0a9ee53c0418f5]*/
  44. {
  45. int effect;
  46. int oparg_int = 0;
  47. if (HAS_ARG(opcode)) {
  48. if (oparg == Py_None) {
  49. PyErr_SetString(PyExc_ValueError,
  50. "stack_effect: opcode requires oparg but oparg was not specified");
  51. return -1;
  52. }
  53. oparg_int = (int)PyLong_AsLong(oparg);
  54. if ((oparg_int == -1) && PyErr_Occurred())
  55. return -1;
  56. }
  57. else if (oparg != Py_None) {
  58. PyErr_SetString(PyExc_ValueError,
  59. "stack_effect: opcode does not permit oparg but oparg was specified");
  60. return -1;
  61. }
  62. effect = PyCompile_OpcodeStackEffect(opcode, oparg_int);
  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. static PyMethodDef
  71. opcode_functions[] = {
  72. _OPCODE_STACK_EFFECT_METHODDEF
  73. {NULL, NULL, 0, NULL}
  74. };
  75. static struct PyModuleDef opcodemodule = {
  76. PyModuleDef_HEAD_INIT,
  77. "_opcode",
  78. "Opcode support module.",
  79. -1,
  80. opcode_functions,
  81. NULL,
  82. NULL,
  83. NULL,
  84. NULL
  85. };
  86. PyMODINIT_FUNC
  87. PyInit__opcode(void)
  88. {
  89. return PyModule_Create(&opcodemodule);
  90. }