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.

43 lines
1.2 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. # This code should stay compatible with Python 2.3, at least while
  6. # some of the buildbots have Python 2.3 as their system Python.
  7. import imp
  8. import os
  9. def find_module(modname):
  10. """Finds and returns a module in the local dist/checkout.
  11. """
  12. modpath = os.path.join(
  13. os.path.dirname(os.path.dirname(__file__)), "Lib")
  14. return imp.load_module(modname, *imp.find_module(modname, [modpath]))
  15. def write_contents(f):
  16. """Write C code contents to the target file object.
  17. """
  18. opcode = find_module("opcode")
  19. targets = ['_unknown_opcode'] * 256
  20. for opname, op in opcode.opmap.items():
  21. targets[op] = "TARGET_%s" % opname
  22. f.write("static void *opcode_targets[256] = {\n")
  23. f.write(",\n".join([" &&%s" % s for s in targets]))
  24. f.write("\n};\n")
  25. if __name__ == "__main__":
  26. import sys
  27. assert len(sys.argv) < 3, "Too many arguments"
  28. if len(sys.argv) == 2:
  29. target = sys.argv[1]
  30. else:
  31. target = "Python/opcode_targets.h"
  32. f = open(target, "w")
  33. try:
  34. write_contents(f)
  35. finally:
  36. f.close()