blob: 7a57a0631bad5a8f2317d7c4f545839e0f1b9123 [file] [log] [blame]
Benjamin Petersonb5cdf192010-03-11 23:39:40 +00001#! /usr/bin/env python
Antoine Pitroub52ec782009-01-25 16:34:23 +00002"""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 Stinnera9a852c2016-03-25 11:54:47 +01006import opcode
Antoine Pitroub52ec782009-01-25 16:34:23 +00007import os
Victor Stinnera9a852c2016-03-25 11:54:47 +01008import sys
Antoine Pitroub52ec782009-01-25 16:34:23 +00009
10
Antoine Pitroub52ec782009-01-25 16:34:23 +000011def write_contents(f):
12 """Write C code contents to the target file object.
13 """
Antoine Pitroub52ec782009-01-25 16:34:23 +000014 targets = ['_unknown_opcode'] * 256
15 for opname, op in opcode.opmap.items():
Antoine Pitroub52ec782009-01-25 16:34:23 +000016 targets[op] = "TARGET_%s" % opname
17 f.write("static void *opcode_targets[256] = {\n")
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000018 f.write(",\n".join([" &&%s" % s for s in targets]))
Antoine Pitroub52ec782009-01-25 16:34:23 +000019 f.write("\n};\n")
20
21
Victor Stinnera9a852c2016-03-25 11:54:47 +010022def main():
23 if len(sys.argv) >= 3:
24 sys.exit("Too many arguments")
Antoine Pitroub52ec782009-01-25 16:34:23 +000025 if len(sys.argv) == 2:
26 target = sys.argv[1]
27 else:
28 target = "Python/opcode_targets.h"
Victor Stinnera9a852c2016-03-25 11:54:47 +010029 with open(target, "w") as f:
Antoine Pitroub52ec782009-01-25 16:34:23 +000030 write_contents(f)
Victor Stinnera9a852c2016-03-25 11:54:47 +010031 print("Jump table written into %s" % target)
32
33
34if __name__ == "__main__":
35 main()