• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# This script generates the opcode.h header file.
2
3import sys
4import tokenize
5
6header = """
7/* Auto-generated by Tools/scripts/generate_opcode_h.py from Lib/opcode.py */
8#ifndef Py_OPCODE_H
9#define Py_OPCODE_H
10#ifdef __cplusplus
11extern "C" {
12#endif
13
14
15    /* Instruction opcodes for compiled code */
16""".lstrip()
17
18footer = """
19/* EXCEPT_HANDLER is a special, implicit block type which is created when
20   entering an except handler. It is not an opcode but we define it here
21   as we want it to be available to both frameobject.c and ceval.c, while
22   remaining private.*/
23#define EXCEPT_HANDLER 257
24
25
26enum cmp_op {PyCmp_LT=Py_LT, PyCmp_LE=Py_LE, PyCmp_EQ=Py_EQ, PyCmp_NE=Py_NE,
27                PyCmp_GT=Py_GT, PyCmp_GE=Py_GE, PyCmp_IN, PyCmp_NOT_IN,
28                PyCmp_IS, PyCmp_IS_NOT, PyCmp_EXC_MATCH, PyCmp_BAD};
29
30#define HAS_ARG(op) ((op) >= HAVE_ARGUMENT)
31
32#ifdef __cplusplus
33}
34#endif
35#endif /* !Py_OPCODE_H */
36"""
37
38
39def main(opcode_py, outfile='Include/opcode.h'):
40    opcode = {}
41    if hasattr(tokenize, 'open'):
42        fp = tokenize.open(opcode_py)   # Python 3.2+
43    else:
44        fp = open(opcode_py)            # Python 2.7
45    with fp:
46        code = fp.read()
47    exec(code, opcode)
48    opmap = opcode['opmap']
49    with open(outfile, 'w') as fobj:
50        fobj.write(header)
51        for name in opcode['opname']:
52            if name in opmap:
53                fobj.write("#define %-23s %3s\n" % (name, opmap[name]))
54            if name == 'POP_EXCEPT': # Special entry for HAVE_ARGUMENT
55                fobj.write("#define %-23s %3d\n" %
56                            ('HAVE_ARGUMENT', opcode['HAVE_ARGUMENT']))
57        fobj.write(footer)
58
59    print("%s regenerated from %s" % (outfile, opcode_py))
60
61
62if __name__ == '__main__':
63    main(sys.argv[1], sys.argv[2])
64