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