Benjamin Peterson | b5cdf19 | 2010-03-11 23:39:40 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 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 | |
Mark Dickinson | 72ead17 | 2009-01-31 12:12:41 +0000 | [diff] [blame] | 6 | # 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 Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 9 | import imp |
| 10 | import os |
| 11 | |
| 12 | |
| 13 | def 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 | |
| 20 | def 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 Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 26 | targets[op] = "TARGET_%s" % opname |
| 27 | f.write("static void *opcode_targets[256] = {\n") |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 28 | f.write(",\n".join([" &&%s" % s for s in targets])) |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 29 | f.write("\n};\n") |
| 30 | |
| 31 | |
| 32 | if __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() |