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.

46 lines
1.3 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. if opname == "STOP_CODE":
  22. # XXX opcode not implemented
  23. continue
  24. targets[op] = "TARGET_%s" % opname
  25. f.write("static void *opcode_targets[256] = {\n")
  26. f.write(",\n".join([" &&%s" % s for s in targets]))
  27. f.write("\n};\n")
  28. if __name__ == "__main__":
  29. import sys
  30. assert len(sys.argv) < 3, "Too many arguments"
  31. if len(sys.argv) == 2:
  32. target = sys.argv[1]
  33. else:
  34. target = "Python/opcode_targets.h"
  35. f = open(target, "w")
  36. try:
  37. write_contents(f)
  38. finally:
  39. f.close()