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.

55 lines
1.6 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. f.write("static void *opcode_targets[256] = {\n")
  32. f.write(",\n".join([" &&%s" % s for s in targets]))
  33. f.write("\n};\n")
  34. def main():
  35. if len(sys.argv) >= 3:
  36. sys.exit("Too many arguments")
  37. if len(sys.argv) == 2:
  38. target = sys.argv[1]
  39. else:
  40. target = "Python/opcode_targets.h"
  41. with open(target, "w") as f:
  42. write_contents(f)
  43. print("Jump table written into %s" % target)
  44. if __name__ == "__main__":
  45. main()