PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/Python/makeopcodetargets.py

http://unladen-swallow.googlecode.com/
Python | 43 lines | 41 code | 0 blank | 2 comment | 0 complexity | e0f912c24faa0fca0c422a32834fdeee MD5 | raw file
 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
 6import imp
 7import os
 8
 9
10def 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
17def 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
32if __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()