/Python/makeopcodetargets.py

http://unladen-swallow.googlecode.com/ · Python · 43 lines · 30 code · 6 blank · 7 comment · 8 complexity · e0f912c24faa0fca0c422a32834fdeee MD5 · raw file

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