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 | |
| 6 | import imp |
| 7 | import os |
| 8 | |
| 9 | |
| 10 | def find_module(modname): |
| 11 | """Finds and returns a module in the local dist/checkout. |
| 12 | """ |
| 13 | modpath = os.path.join( |
| 14 | os.path.dirname(os.path.dirname(__file__)), "Lib") |
| 15 | return imp.load_module(modname, *imp.find_module(modname, [modpath])) |
| 16 | |
| 17 | def write_contents(f): |
| 18 | """Write C code contents to the target file object. |
| 19 | """ |
| 20 | opcode = find_module("opcode") |
| 21 | targets = ['_unknown_opcode'] * 256 |
| 22 | for opname, op in opcode.opmap.items(): |
| 23 | if opname == "STOP_CODE": |
| 24 | # XXX opcode not implemented |
| 25 | continue |
| 26 | targets[op] = "TARGET_%s" % opname |
| 27 | f.write("static void *opcode_targets[256] = {\n") |
| 28 | f.write(",\n".join("\t&&%s" % s for s in targets)) |
| 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() |