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 | |
Victor Stinner | a9a852c | 2016-03-25 11:54:47 +0100 | [diff] [blame^] | 6 | import opcode |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 7 | import os |
Victor Stinner | a9a852c | 2016-03-25 11:54:47 +0100 | [diff] [blame^] | 8 | import sys |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 9 | |
| 10 | |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 11 | def write_contents(f): |
| 12 | """Write C code contents to the target file object. |
| 13 | """ |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 14 | targets = ['_unknown_opcode'] * 256 |
| 15 | for opname, op in opcode.opmap.items(): |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 16 | targets[op] = "TARGET_%s" % opname |
| 17 | f.write("static void *opcode_targets[256] = {\n") |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 18 | f.write(",\n".join([" &&%s" % s for s in targets])) |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 19 | f.write("\n};\n") |
| 20 | |
| 21 | |
Victor Stinner | a9a852c | 2016-03-25 11:54:47 +0100 | [diff] [blame^] | 22 | def main(): |
| 23 | if len(sys.argv) >= 3: |
| 24 | sys.exit("Too many arguments") |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 25 | if len(sys.argv) == 2: |
| 26 | target = sys.argv[1] |
| 27 | else: |
| 28 | target = "Python/opcode_targets.h" |
Victor Stinner | a9a852c | 2016-03-25 11:54:47 +0100 | [diff] [blame^] | 29 | with open(target, "w") as f: |
Antoine Pitrou | b52ec78 | 2009-01-25 16:34:23 +0000 | [diff] [blame] | 30 | write_contents(f) |
Victor Stinner | a9a852c | 2016-03-25 11:54:47 +0100 | [diff] [blame^] | 31 | print("Jump table written into %s" % target) |
| 32 | |
| 33 | |
| 34 | if __name__ == "__main__": |
| 35 | main() |