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.

116 lines
2.7 KiB

  1. #include "Python.h"
  2. #include "opcode.h"
  3. /*[clinic]
  4. module _opcode
  5. _opcode.stack_effect -> int
  6. opcode: int
  7. [
  8. oparg: int
  9. ]
  10. /
  11. Compute the stack effect of the opcode.
  12. [clinic]*/
  13. PyDoc_STRVAR(_opcode_stack_effect__doc__,
  14. "Compute the stack effect of the opcode.\n"
  15. "\n"
  16. "_opcode.stack_effect(opcode, [oparg])");
  17. #define _OPCODE_STACK_EFFECT_METHODDEF \
  18. {"stack_effect", (PyCFunction)_opcode_stack_effect, METH_VARARGS, _opcode_stack_effect__doc__},
  19. static int
  20. _opcode_stack_effect_impl(PyObject *module, int opcode, int group_right_1, int oparg);
  21. static PyObject *
  22. _opcode_stack_effect(PyObject *module, PyObject *args)
  23. {
  24. PyObject *return_value = NULL;
  25. int opcode;
  26. int group_right_1 = 0;
  27. int oparg = 0;
  28. int _return_value;
  29. switch (PyTuple_Size(args)) {
  30. case 1:
  31. if (!PyArg_ParseTuple(args, "i:stack_effect", &opcode))
  32. return NULL;
  33. break;
  34. case 2:
  35. if (!PyArg_ParseTuple(args, "ii:stack_effect", &opcode, &oparg))
  36. return NULL;
  37. group_right_1 = 1;
  38. break;
  39. default:
  40. PyErr_SetString(PyExc_TypeError, "_opcode.stack_effect requires 1 to 2 arguments");
  41. return NULL;
  42. }
  43. _return_value = _opcode_stack_effect_impl(module, opcode, group_right_1, oparg);
  44. if ((_return_value == -1) && PyErr_Occurred())
  45. goto exit;
  46. return_value = PyLong_FromLong((long)_return_value);
  47. exit:
  48. return return_value;
  49. }
  50. static int
  51. _opcode_stack_effect_impl(PyObject *module, int opcode, int group_right_1, int oparg)
  52. /*[clinic checksum: 2312ded40abc9bcbce718942de21f53e61a2dfd3]*/
  53. {
  54. int effect;
  55. if (HAS_ARG(opcode)) {
  56. if (!group_right_1) {
  57. PyErr_SetString(PyExc_ValueError,
  58. "stack_effect: opcode requires oparg but oparg was not specified");
  59. return -1;
  60. }
  61. }
  62. else if (group_right_1) {
  63. PyErr_SetString(PyExc_ValueError,
  64. "stack_effect: opcode does not permit oparg but oparg was specified");
  65. return -1;
  66. }
  67. effect = PyCompile_OpcodeStackEffect(opcode, oparg);
  68. if (effect == PY_INVALID_STACK_EFFECT) {
  69. PyErr_SetString(PyExc_ValueError,
  70. "invalid opcode or oparg");
  71. return -1;
  72. }
  73. return effect;
  74. }
  75. static PyMethodDef
  76. opcode_functions[] = {
  77. _OPCODE_STACK_EFFECT_METHODDEF
  78. {NULL, NULL, 0, NULL}
  79. };
  80. static struct PyModuleDef opcodemodule = {
  81. PyModuleDef_HEAD_INIT,
  82. "_opcode",
  83. "Opcode support module.",
  84. -1,
  85. opcode_functions,
  86. NULL,
  87. NULL,
  88. NULL,
  89. NULL
  90. };
  91. PyMODINIT_FUNC
  92. PyInit__opcode(void)
  93. {
  94. return PyModule_Create(&opcodemodule);
  95. }