blob: d9a085552f7b257e3ff4653990d192abd8b9da9a [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():
Antoine Pitroub52ec782009-01-25 16:34:23 +000026 targets[op] = "TARGET_%s" % opname
27 f.write("static void *opcode_targets[256] = {\n")
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 f.write(",\n".join([" &&%s" % s for s in targets]))
Antoine Pitroub52ec782009-01-25 16:34:23 +000029 f.write("\n};\n")
30
31
32if __name__ == "__main__":
33 import sys
34 assert len(sys.argv) < 3, "Too many arguments"
35 if len(sys.argv) == 2:
36 target = sys.argv[1]
37 else:
38 target = "Python/opcode_targets.h"
39 f = open(target, "w")
40 try:
41 write_contents(f)
42 finally:
43 f.close()