blob: 5d8e5a9a461c3d9006734af9fbc0bc14cc204c52 [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
Mark Dickinson72ead172009-01-31 12:12:41 +00006# This code should stay compatible with Python 2.3, at least while
7# some of the buildbots have Python 2.3 as their system Python.
8
Antoine Pitroub52ec782009-01-25 16:34:23 +00009import imp
10import os
11
12
13def find_module(modname):
14 """Finds and returns a module in the local dist/checkout.
15 """
16 modpath = os.path.join(
17 os.path.dirname(os.path.dirname(__file__)), "Lib")
18 return imp.load_module(modname, *imp.find_module(modname, [modpath]))
19
20def write_contents(f):
21 """Write C code contents to the target file object.
22 """
23 opcode = find_module("opcode")
24 targets = ['_unknown_opcode'] * 256
25 for opname, op in opcode.opmap.items():
26 if opname == "STOP_CODE":
27 # XXX opcode not implemented
28 continue
29 targets[op] = "TARGET_%s" % opname
30 f.write("static void *opcode_targets[256] = {\n")
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 f.write(",\n".join([" &&%s" % s for s in targets]))
Antoine Pitroub52ec782009-01-25 16:34:23 +000032 f.write("\n};\n")
33
34
35if __name__ == "__main__":
36 import sys
37 assert len(sys.argv) < 3, "Too many arguments"
38 if len(sys.argv) == 2:
39 target = sys.argv[1]
40 else:
41 target = "Python/opcode_targets.h"
42 f = open(target, "w")
43 try:
44 write_contents(f)
45 finally:
46 f.close()