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