Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 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(): |
| 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") |
Mark Dickinson | 72ead17 | 2009-01-31 12:12:41 +0000 | [diff] [blame] | 31 | f.write(",\n".join(["\t&&%s" % s for s in targets])) |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 32 | f.write("\n};\n") |
| 33 | |
| 34 | |
| 35 | if __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() |