basic.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # (C) Copyright 2023, 2025 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. Routines for formatting opcodes.
  18. """
  19. def format_extended_arg(arg) -> str:
  20. return str(arg * (1 << 16))
  21. def format_CALL_FUNCTION_pos_name_encoded(argc) -> str:
  22. """Encoded positional and named args. Used to
  23. up to about 3.6 where wordcodes are used and
  24. a different encoding occurs. Pypy36 though
  25. sticks to this encoded version though."""
  26. name_default, pos_args = divmod(argc, 256)
  27. return "%d positional, %d named" % (pos_args, name_default)
  28. def format_IS_OP(arg: int) -> str:
  29. return "is" if arg == 0 else "is not"
  30. def format_MAKE_FUNCTION_10_27(argc: int) -> str:
  31. """
  32. ``argc`` is the operand of a "MAKE_FUNCTION" or "MAKE_CLOSURE" instruction.
  33. This code works for Python versions up to and including 2.7.
  34. Python docs for MAKE_FUNCTION and MAKE_CLOSURE the was changed in 33, but testing
  35. shows that the change was really made in Python 3.0 or so.
  36. """
  37. return f"{argc} default parameters"
  38. # Up until 3.7
  39. def format_RAISE_VARARGS_older(argc) -> str:
  40. assert 0 <= argc <= 3
  41. if argc == 0:
  42. return "reraise"
  43. elif argc == 1:
  44. return "exception"
  45. elif argc == 2:
  46. return "exception, parameter"
  47. elif argc == 3:
  48. return "exception, parameter, traceback"
  49. return ""
  50. def format_ROT_FOUR(_: int) -> str:
  51. return "TOS, TOS1, TOS2, TOS3 = TOS1, TOS2, TOS3, TOS"
  52. def format_ROT_THREE(_: int) -> str:
  53. return "TOS, TOS1, TOS2 = TOS1, TOS2, TOS"
  54. def format_ROT_TWO(_: int) -> str:
  55. # We add a space at the end as a sentinal to use in get_instruction_tos_str()
  56. return "TOS, TOS1 = TOS1, TOS"
  57. opcode_arg_fmt_base = opcode_arg_fmt34 = {
  58. "CALL_FUNCTION": format_CALL_FUNCTION_pos_name_encoded,
  59. "CALL_FUNCTION_KW": format_CALL_FUNCTION_pos_name_encoded,
  60. "CALL_FUNCTION_VAR_KW": format_CALL_FUNCTION_pos_name_encoded,
  61. "EXTENDED_ARG": format_extended_arg,
  62. "RAISE_VARARGS": format_RAISE_VARARGS_older,
  63. "ROT_FOUR": format_ROT_FOUR,
  64. "ROT_THREE": format_ROT_THREE,
  65. "ROT_TWO": format_ROT_TWO,
  66. }