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.

60 lines
1.7 KiB

  1. #! /usr/bin/env python
  2. """Generate C code for the jump table of the threaded code interpreter
  3. (for compilers supporting computed gotos or "labels-as-values", such as gcc).
  4. """
  5. import os
  6. import sys
  7. try:
  8. from importlib.machinery import SourceFileLoader
  9. except ImportError:
  10. import imp
  11. def find_module(modname):
  12. """Finds and returns a module in the local dist/checkout.
  13. """
  14. modpath = os.path.join(
  15. os.path.dirname(os.path.dirname(__file__)), "Lib")
  16. return imp.load_module(modname, *imp.find_module(modname, [modpath]))
  17. else:
  18. def find_module(modname):
  19. """Finds and returns a module in the local dist/checkout.
  20. """
  21. modpath = os.path.join(
  22. os.path.dirname(os.path.dirname(__file__)), "Lib", modname + ".py")
  23. return SourceFileLoader(modname, modpath).load_module()
  24. def write_contents(f):
  25. """Write C code contents to the target file object.
  26. """
  27. opcode = find_module('opcode')
  28. targets = ['_unknown_opcode'] * 256
  29. for opname, op in opcode.opmap.items():
  30. targets[op] = "TARGET_%s" % opname
  31. next_op = 1
  32. for opname in opcode._specialized_instructions:
  33. while targets[next_op] != '_unknown_opcode':
  34. next_op += 1
  35. targets[next_op] = "TARGET_%s" % opname
  36. f.write("static void *opcode_targets[256] = {\n")
  37. f.write(",\n".join([" &&%s" % s for s in targets]))
  38. f.write("\n};\n")
  39. def main():
  40. if len(sys.argv) >= 3:
  41. sys.exit("Too many arguments")
  42. if len(sys.argv) == 2:
  43. target = sys.argv[1]
  44. else:
  45. target = "Python/opcode_targets.h"
  46. with open(target, "w") as f:
  47. write_contents(f)
  48. print("Jump table written into %s" % target)
  49. if __name__ == "__main__":
  50. main()