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