| 1 |
|
| 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 |
|
| 7 |
|
| 8 |
|
| 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 |
continue |
| 28 |
targets[op] = "TARGET_%s" % opname.replace("+0", " ").replace("+", "_") |
| 29 |
f.write("static void *opcode_targets[256] = {\n") |
| 30 |
f.write(",\n".join([" &&%s" % s for s in targets])) |
| 31 |
f.write("\n};\n") |
| 32 |
|
| 33 |
|
| 34 |
if __name__ == "__main__": |
| 35 |
import sys |
| 36 |
assert len(sys.argv) < 3, "Too many arguments" |
| 37 |
if len(sys.argv) == 2: |
| 38 |
target = sys.argv[1] |
| 39 |
else: |
| 40 |
target = "Python/opcode_targets.h" |
| 41 |
f = open(target, "w") |
| 42 |
try: |
| 43 |
write_contents(f) |
| 44 |
finally: |
| 45 |
f.close() |