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 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 6 | import os |
Victor Stinner | a9a852c | 2016-03-25 11:54:47 +0100 | [diff] [blame] | 7 | import sys |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 8 | |
| 9 | |
Victor Stinner | ca9dbc7 | 2016-03-26 01:04:37 +0100 | [diff] [blame] | 10 | try: |
| 11 | from importlib.machinery import SourceFileLoader |
| 12 | except ImportError: |
| 13 | import imp |
| 14 | |
| 15 | def find_module(modname): |
| 16 | """Finds and returns a module in the local dist/checkout. |
| 17 | """ |
| 18 | modpath = os.path.join( |
| 19 | os.path.dirname(os.path.dirname(__file__)), "Lib") |
| 20 | return imp.load_module(modname, *imp.find_module(modname, [modpath])) |
| 21 | else: |
| 22 | def find_module(modname): |
| 23 | """Finds and returns a module in the local dist/checkout. |
| 24 | """ |
| 25 | modpath = os.path.join( |
| 26 | os.path.dirname(os.path.dirname(__file__)), "Lib", modname + ".py") |
| 27 | return SourceFileLoader(modname, modpath).load_module() |
| 28 | |
| 29 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 30 | def write_contents(f): |
| 31 | """Write C code contents to the target file object. |
| 32 | """ |
Victor Stinner | ca9dbc7 | 2016-03-26 01:04:37 +0100 | [diff] [blame] | 33 | opcode = find_module('opcode') |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 34 | targets = ['_unknown_opcode'] * 256 |
| 35 | for opname, op in opcode.opmap.items(): |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 36 | targets[op] = "TARGET_%s" % opname |
| 37 | f.write("static void *opcode_targets[256] = {\n") |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 38 | f.write(",\n".join([" &&%s" % s for s in targets])) |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 39 | f.write("\n};\n") |
| 40 | |
| 41 | |
Victor Stinner | a9a852c | 2016-03-25 11:54:47 +0100 | [diff] [blame] | 42 | def main(): |
| 43 | if len(sys.argv) >= 3: |
| 44 | sys.exit("Too many arguments") |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 45 | if len(sys.argv) == 2: |
| 46 | target = sys.argv[1] |
| 47 | else: |
| 48 | target = "Python/opcode_targets.h" |
Victor Stinner | a9a852c | 2016-03-25 11:54:47 +0100 | [diff] [blame] | 49 | with open(target, "w") as f: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 50 | write_contents(f) |
Victor Stinner | a9a852c | 2016-03-25 11:54:47 +0100 | [diff] [blame] | 51 | print("Jump table written into %s" % target) |
| 52 | |
| 53 | |
| 54 | if __name__ == "__main__": |
| 55 | main() |