blob: 023c9e6c9f1adc1ce9d1e4223f77f90cfde6729b [file] [log] [blame]
Benjamin Petersonb5cdf192010-03-11 23:39:40 +00001#! /usr/bin/env python
Antoine Pitroub52ec782009-01-25 16:34:23 +00002"""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
Antoine Pitroub52ec782009-01-25 16:34:23 +00006import os
Victor Stinnera9a852c2016-03-25 11:54:47 +01007import sys
Antoine Pitroub52ec782009-01-25 16:34:23 +00008
9
Victor Stinnerca9dbc72016-03-26 01:04:37 +010010try:
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
Antoine Pitroub52ec782009-01-25 16:34:23 +000030def write_contents(f):
31 """Write C code contents to the target file object.
32 """
Victor Stinnerca9dbc72016-03-26 01:04:37 +010033 opcode = find_module('opcode')
Antoine Pitroub52ec782009-01-25 16:34:23 +000034 targets = ['_unknown_opcode'] * 256
35 for opname, op in opcode.opmap.items():
Antoine Pitroub52ec782009-01-25 16:34:23 +000036 targets[op] = "TARGET_%s" % opname
37 f.write("static void *opcode_targets[256] = {\n")
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 f.write(",\n".join([" &&%s" % s for s in targets]))
Antoine Pitroub52ec782009-01-25 16:34:23 +000039 f.write("\n};\n")
40
41
Victor Stinnera9a852c2016-03-25 11:54:47 +010042def main():
43 if len(sys.argv) >= 3:
44 sys.exit("Too many arguments")
Antoine Pitroub52ec782009-01-25 16:34:23 +000045 if len(sys.argv) == 2:
46 target = sys.argv[1]
47 else:
48 target = "Python/opcode_targets.h"
Victor Stinnera9a852c2016-03-25 11:54:47 +010049 with open(target, "w") as f:
Antoine Pitroub52ec782009-01-25 16:34:23 +000050 write_contents(f)
Victor Stinnera9a852c2016-03-25 11:54:47 +010051 print("Jump table written into %s" % target)
52
53
54if __name__ == "__main__":
55 main()