opcode_14.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # (C) Copyright 2018-2021, 2023 by Rocky Bernstein
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # of the License, or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. """
  17. CPython 1.4 bytecode opcodes
  18. This is used in bytecode disassembly. This is similar to the
  19. opcodes in Python's dis.py library.
  20. """
  21. import xdis.opcodes.opcode_15 as opcode_15
  22. # This is used from outside this module
  23. from xdis.cross_dis import findlabels # noqa
  24. from xdis.opcodes.base import ( # Although these aren't used here, they are exported
  25. def_op,
  26. finalize_opcodes,
  27. init_opdata,
  28. name_op,
  29. update_pj2,
  30. varargs_op,
  31. )
  32. from xdis.opcodes.opcode_1x import opcode_extended_fmt_base1x, update_arg_fmt_base1x
  33. version_tuple = (1, 4)
  34. python_implementation = "CPython"
  35. loc = locals()
  36. init_opdata(loc, opcode_15, version_tuple)
  37. # fmt: off
  38. # 1.4 Bytecodes not in 1.5
  39. def_op(loc, "UNARY_CALL", 14)
  40. def_op(loc, "BINARY_CALL", 26)
  41. def_op(loc, "RAISE_EXCEPTION", 81)
  42. def_op(loc, "BUILD_FUNCTION", 86)
  43. varargs_op(loc, "UNPACK_ARG", 94) # Number of arguments expected
  44. varargs_op(loc, "UNPACK_VARARG", 99) # Minimal number of arguments
  45. name_op(loc, "LOAD_LOCAL", 115)
  46. loc["nullaryloadop"].add(115)
  47. varargs_op(loc, "SET_FUNC_ARGS", 117) # Argcount
  48. varargs_op(loc, "RESERVE_FAST", 123) # Number of local variables
  49. # fmt: on
  50. def findlinestarts(co, dup_lines: bool=False):
  51. code = co.co_code
  52. n = len(code)
  53. offset = 0
  54. while offset < n:
  55. op = code[offset]
  56. offset += 1
  57. if op == loc["opmap"]["SET_LINENO"] and offset > 0:
  58. lineno = code[offset] + code[offset + 1] * 256
  59. yield (offset + 2, lineno)
  60. pass
  61. if op >= loc["HAVE_ARGUMENT"]:
  62. offset += 2
  63. pass
  64. pass
  65. opcode_arg_fmt = update_arg_fmt_base1x.copy()
  66. opcode_extended_fmt = opcode_extended_fmt_base1x.copy()
  67. update_pj2(globals(), loc)
  68. finalize_opcodes(loc)