Skip to content

Commit 75e17c4

Browse files
committed
Closes Issue 17861: Autogenerate Include/opcode.h from opcode.py.
It includes required changes in Makefile.pre.in and configure.ac among other files.
1 parent 47e9a60 commit 75e17c4

7 files changed

Lines changed: 239 additions & 141 deletions

File tree

.hgtouch

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
Python/importlib.h: Lib/importlib/_bootstrap.py Modules/_freeze_importlib.c
66

7+
Include/opcode.h: Lib/opcode.py Tools/scripts/generate_opcode_h.py
8+
79
Include/Python-ast.h: Parser/Python.asdl Parser/asdl.py Parser/asdl_c.py
810
Python/Python-ast.c: Include/Python-ast.h
911

Include/opcode.h

Lines changed: 109 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,116 @@
1+
/* Auto-generated by Tools/scripts/generate_opcode_h.py */
12
#ifndef Py_OPCODE_H
23
#define Py_OPCODE_H
34
#ifdef __cplusplus
45
extern "C" {
56
#endif
67

78

8-
/* Instruction opcodes for compiled code */
9-
10-
#define POP_TOP 1
11-
#define ROT_TWO 2
12-
#define ROT_THREE 3
13-
#define DUP_TOP 4
14-
#define DUP_TOP_TWO 5
15-
#define NOP 9
16-
17-
#define UNARY_POSITIVE 10
18-
#define UNARY_NEGATIVE 11
19-
#define UNARY_NOT 12
20-
21-
#define UNARY_INVERT 15
22-
23-
#define BINARY_MATRIX_MULTIPLY 16
24-
#define INPLACE_MATRIX_MULTIPLY 17
25-
26-
#define BINARY_POWER 19
27-
28-
#define BINARY_MULTIPLY 20
29-
30-
#define BINARY_MODULO 22
31-
#define BINARY_ADD 23
32-
#define BINARY_SUBTRACT 24
33-
#define BINARY_SUBSCR 25
34-
#define BINARY_FLOOR_DIVIDE 26
35-
#define BINARY_TRUE_DIVIDE 27
36-
#define INPLACE_FLOOR_DIVIDE 28
37-
#define INPLACE_TRUE_DIVIDE 29
38-
39-
#define STORE_MAP 54
40-
#define INPLACE_ADD 55
41-
#define INPLACE_SUBTRACT 56
42-
#define INPLACE_MULTIPLY 57
43-
44-
#define INPLACE_MODULO 59
45-
#define STORE_SUBSCR 60
46-
#define DELETE_SUBSCR 61
47-
48-
#define BINARY_LSHIFT 62
49-
#define BINARY_RSHIFT 63
50-
#define BINARY_AND 64
51-
#define BINARY_XOR 65
52-
#define BINARY_OR 66
53-
#define INPLACE_POWER 67
54-
#define GET_ITER 68
55-
#define PRINT_EXPR 70
56-
#define LOAD_BUILD_CLASS 71
57-
#define YIELD_FROM 72
58-
59-
#define INPLACE_LSHIFT 75
60-
#define INPLACE_RSHIFT 76
61-
#define INPLACE_AND 77
62-
#define INPLACE_XOR 78
63-
#define INPLACE_OR 79
64-
#define BREAK_LOOP 80
65-
#define WITH_CLEANUP 81
66-
67-
#define RETURN_VALUE 83
68-
#define IMPORT_STAR 84
69-
70-
#define YIELD_VALUE 86
71-
#define POP_BLOCK 87
72-
#define END_FINALLY 88
73-
#define POP_EXCEPT 89
74-
75-
#define HAVE_ARGUMENT 90 /* Opcodes from here have an argument: */
76-
77-
#define STORE_NAME 90 /* Index in name list */
78-
#define DELETE_NAME 91 /* "" */
79-
#define UNPACK_SEQUENCE 92 /* Number of sequence items */
80-
#define FOR_ITER 93
81-
#define UNPACK_EX 94 /* Num items before variable part +
82-
(Num items after variable part << 8) */
83-
84-
#define STORE_ATTR 95 /* Index in name list */
85-
#define DELETE_ATTR 96 /* "" */
86-
#define STORE_GLOBAL 97 /* "" */
87-
#define DELETE_GLOBAL 98 /* "" */
88-
89-
#define LOAD_CONST 100 /* Index in const list */
90-
#define LOAD_NAME 101 /* Index in name list */
91-
#define BUILD_TUPLE 102 /* Number of tuple items */
92-
#define BUILD_LIST 103 /* Number of list items */
93-
#define BUILD_SET 104 /* Number of set items */
94-
#define BUILD_MAP 105 /* Always zero for now */
95-
#define LOAD_ATTR 106 /* Index in name list */
96-
#define COMPARE_OP 107 /* Comparison operator */
97-
#define IMPORT_NAME 108 /* Index in name list */
98-
#define IMPORT_FROM 109 /* Index in name list */
99-
100-
#define JUMP_FORWARD 110 /* Number of bytes to skip */
101-
#define JUMP_IF_FALSE_OR_POP 111 /* Target byte offset from beginning of code */
102-
#define JUMP_IF_TRUE_OR_POP 112 /* "" */
103-
#define JUMP_ABSOLUTE 113 /* "" */
104-
#define POP_JUMP_IF_FALSE 114 /* "" */
105-
#define POP_JUMP_IF_TRUE 115 /* "" */
106-
107-
#define LOAD_GLOBAL 116 /* Index in name list */
108-
109-
#define CONTINUE_LOOP 119 /* Start of loop (absolute) */
110-
#define SETUP_LOOP 120 /* Target address (relative) */
111-
#define SETUP_EXCEPT 121 /* "" */
112-
#define SETUP_FINALLY 122 /* "" */
113-
114-
#define LOAD_FAST 124 /* Local variable number */
115-
#define STORE_FAST 125 /* Local variable number */
116-
#define DELETE_FAST 126 /* Local variable number */
117-
118-
#define RAISE_VARARGS 130 /* Number of raise arguments (1, 2 or 3) */
119-
/* CALL_FUNCTION_XXX opcodes defined below depend on this definition */
120-
#define CALL_FUNCTION 131 /* #args + (#kwargs<<8) */
121-
#define MAKE_FUNCTION 132 /* #defaults + #kwdefaults<<8 + #annotations<<16 */
122-
#define BUILD_SLICE 133 /* Number of items */
123-
124-
#define MAKE_CLOSURE 134 /* same as MAKE_FUNCTION */
125-
#define LOAD_CLOSURE 135 /* Load free variable from closure */
126-
#define LOAD_DEREF 136 /* Load and dereference from closure cell */
127-
#define STORE_DEREF 137 /* Store into cell */
128-
#define DELETE_DEREF 138 /* Delete closure cell */
129-
130-
/* The next 3 opcodes must be contiguous and satisfy
131-
(CALL_FUNCTION_VAR - CALL_FUNCTION) & 3 == 1 */
132-
#define CALL_FUNCTION_VAR 140 /* #args + (#kwargs<<8) */
133-
#define CALL_FUNCTION_KW 141 /* #args + (#kwargs<<8) */
134-
#define CALL_FUNCTION_VAR_KW 142 /* #args + (#kwargs<<8) */
135-
136-
#define SETUP_WITH 143
137-
138-
/* Support for opargs more than 16 bits long */
139-
#define EXTENDED_ARG 144
140-
141-
#define LIST_APPEND 145
142-
#define SET_ADD 146
143-
#define MAP_ADD 147
144-
145-
#define LOAD_CLASSDEREF 148
9+
/* Instruction opcodes for compiled code */
10+
#define POP_TOP 1
11+
#define ROT_TWO 2
12+
#define ROT_THREE 3
13+
#define DUP_TOP 4
14+
#define DUP_TOP_TWO 5
15+
#define NOP 9
16+
#define UNARY_POSITIVE 10
17+
#define UNARY_NEGATIVE 11
18+
#define UNARY_NOT 12
19+
#define UNARY_INVERT 15
20+
#define BINARY_MATRIX_MULTIPLY 16
21+
#define INPLACE_MATRIX_MULTIPLY 17
22+
#define BINARY_POWER 19
23+
#define BINARY_MULTIPLY 20
24+
#define BINARY_MODULO 22
25+
#define BINARY_ADD 23
26+
#define BINARY_SUBTRACT 24
27+
#define BINARY_SUBSCR 25
28+
#define BINARY_FLOOR_DIVIDE 26
29+
#define BINARY_TRUE_DIVIDE 27
30+
#define INPLACE_FLOOR_DIVIDE 28
31+
#define INPLACE_TRUE_DIVIDE 29
32+
#define STORE_MAP 54
33+
#define INPLACE_ADD 55
34+
#define INPLACE_SUBTRACT 56
35+
#define INPLACE_MULTIPLY 57
36+
#define INPLACE_MODULO 59
37+
#define STORE_SUBSCR 60
38+
#define DELETE_SUBSCR 61
39+
#define BINARY_LSHIFT 62
40+
#define BINARY_RSHIFT 63
41+
#define BINARY_AND 64
42+
#define BINARY_XOR 65
43+
#define BINARY_OR 66
44+
#define INPLACE_POWER 67
45+
#define GET_ITER 68
46+
#define PRINT_EXPR 70
47+
#define LOAD_BUILD_CLASS 71
48+
#define YIELD_FROM 72
49+
#define INPLACE_LSHIFT 75
50+
#define INPLACE_RSHIFT 76
51+
#define INPLACE_AND 77
52+
#define INPLACE_XOR 78
53+
#define INPLACE_OR 79
54+
#define BREAK_LOOP 80
55+
#define WITH_CLEANUP 81
56+
#define RETURN_VALUE 83
57+
#define IMPORT_STAR 84
58+
#define YIELD_VALUE 86
59+
#define POP_BLOCK 87
60+
#define END_FINALLY 88
61+
#define POP_EXCEPT 89
62+
#define HAVE_ARGUMENT 90
63+
#define STORE_NAME 90
64+
#define DELETE_NAME 91
65+
#define UNPACK_SEQUENCE 92
66+
#define FOR_ITER 93
67+
#define UNPACK_EX 94
68+
#define STORE_ATTR 95
69+
#define DELETE_ATTR 96
70+
#define STORE_GLOBAL 97
71+
#define DELETE_GLOBAL 98
72+
#define LOAD_CONST 100
73+
#define LOAD_NAME 101
74+
#define BUILD_TUPLE 102
75+
#define BUILD_LIST 103
76+
#define BUILD_SET 104
77+
#define BUILD_MAP 105
78+
#define LOAD_ATTR 106
79+
#define COMPARE_OP 107
80+
#define IMPORT_NAME 108
81+
#define IMPORT_FROM 109
82+
#define JUMP_FORWARD 110
83+
#define JUMP_IF_FALSE_OR_POP 111
84+
#define JUMP_IF_TRUE_OR_POP 112
85+
#define JUMP_ABSOLUTE 113
86+
#define POP_JUMP_IF_FALSE 114
87+
#define POP_JUMP_IF_TRUE 115
88+
#define LOAD_GLOBAL 116
89+
#define CONTINUE_LOOP 119
90+
#define SETUP_LOOP 120
91+
#define SETUP_EXCEPT 121
92+
#define SETUP_FINALLY 122
93+
#define LOAD_FAST 124
94+
#define STORE_FAST 125
95+
#define DELETE_FAST 126
96+
#define RAISE_VARARGS 130
97+
#define CALL_FUNCTION 131
98+
#define MAKE_FUNCTION 132
99+
#define BUILD_SLICE 133
100+
#define MAKE_CLOSURE 134
101+
#define LOAD_CLOSURE 135
102+
#define LOAD_DEREF 136
103+
#define STORE_DEREF 137
104+
#define DELETE_DEREF 138
105+
#define CALL_FUNCTION_VAR 140
106+
#define CALL_FUNCTION_KW 141
107+
#define CALL_FUNCTION_VAR_KW 142
108+
#define SETUP_WITH 143
109+
#define EXTENDED_ARG 144
110+
#define LIST_APPEND 145
111+
#define SET_ADD 146
112+
#define MAP_ADD 147
113+
#define LOAD_CLASSDEREF 148
146114

147115
/* EXCEPT_HANDLER is a special, implicit block type which is created when
148116
entering an except handler. It is not an opcode but we define it here
@@ -151,8 +119,9 @@ extern "C" {
151119
#define EXCEPT_HANDLER 257
152120

153121

154-
enum cmp_op {PyCmp_LT=Py_LT, PyCmp_LE=Py_LE, PyCmp_EQ=Py_EQ, PyCmp_NE=Py_NE, PyCmp_GT=Py_GT, PyCmp_GE=Py_GE,
155-
PyCmp_IN, PyCmp_NOT_IN, PyCmp_IS, PyCmp_IS_NOT, PyCmp_EXC_MATCH, PyCmp_BAD};
122+
enum cmp_op {PyCmp_LT=Py_LT, PyCmp_LE=Py_LE, PyCmp_EQ=Py_EQ, PyCmp_NE=Py_NE,
123+
PyCmp_GT=Py_GT, PyCmp_GE=Py_GE, PyCmp_IN, PyCmp_NOT_IN,
124+
PyCmp_IS, PyCmp_IS_NOT, PyCmp_EXC_MATCH, PyCmp_BAD};
156125

157126
#define HAS_ARG(op) ((op) >= HAVE_ARGUMENT)
158127

Makefile.pre.in

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,13 @@ PARSER_HEADERS= \
322322
PGENSRCS= $(PSRCS) $(PGSRCS)
323323
PGENOBJS= $(POBJS) $(PGOBJS)
324324

325+
##########################################################################
326+
# opcode.h generation
327+
OPCODE_H_DIR= Include
328+
OPCODE_H_SCRIPT= Tools/scripts/generate_opcode_h.py
329+
OPCODE_H= $(srcdir)/$(OPCODE_H_DIR)/opcode.h
330+
OPCODE_H_GEN= @OPCODEHGEN@ $(OPCODE_H_SCRIPT) Lib/ $(OPCODE_H)
331+
#
325332
##########################################################################
326333
# AST
327334
AST_H_DIR= Include
@@ -760,6 +767,9 @@ $(AST_C): $(AST_H) $(AST_ASDL) $(ASDLGEN_FILES)
760767
$(MKDIR_P) $(AST_C_DIR)
761768
$(ASDLGEN) -c $(AST_C_DIR) $(AST_ASDL)
762769

770+
$(OPCODE_H): $(srcdir)/Lib/opcode.py $(OPCODE_H_SCRIPT)
771+
$(OPCODE_H_GEN)
772+
763773
Python/compile.o Python/symtable.o Python/ast.o: $(GRAMMAR_H) $(AST_H)
764774

765775
Python/getplatform.o: $(srcdir)/Python/getplatform.c
@@ -871,7 +881,7 @@ PYTHON_HEADERS= \
871881
$(srcdir)/Include/node.h \
872882
$(srcdir)/Include/object.h \
873883
$(srcdir)/Include/objimpl.h \
874-
$(srcdir)/Include/opcode.h \
884+
$(OPCODE_H) \
875885
$(srcdir)/Include/osdefs.h \
876886
$(srcdir)/Include/patchlevel.h \
877887
$(srcdir)/Include/pgen.h \

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ IDLE
223223
Build
224224
-----
225225

226+
- Issue #17861: Tools/scripts/generate_opcode_h.py automatically regenerates
227+
Include/opcode.h from Lib/opcode.py if the later gets any change.
228+
226229
- Issue #20644: OS X installer build support for documentation build changes
227230
in 3.4.1: assume externally supplied sphinx-build is available in /usr/bin.
228231

Tools/scripts/generate_opcode_h.py

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

0 commit comments

Comments
 (0)