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