Skip to content

Commit 0b243c2

Browse files
authored
gh-105481: opcode.h is no longer generated during the build (#108080)
1 parent 4cb0818 commit 0b243c2

File tree

7 files changed

+99
-72
lines changed

7 files changed

+99
-72
lines changed

Include/opcode.h

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/opcode.py

+1-30
Original file line numberDiff line numberDiff line change
@@ -44,39 +44,10 @@
4444

4545
_intrinsic_1_descs = _opcode.get_intrinsic1_descs()
4646
_intrinsic_2_descs = _opcode.get_intrinsic2_descs()
47+
_nb_ops = _opcode.get_nb_ops()
4748

4849
hascompare = [opmap["COMPARE_OP"]]
4950

50-
_nb_ops = [
51-
("NB_ADD", "+"),
52-
("NB_AND", "&"),
53-
("NB_FLOOR_DIVIDE", "//"),
54-
("NB_LSHIFT", "<<"),
55-
("NB_MATRIX_MULTIPLY", "@"),
56-
("NB_MULTIPLY", "*"),
57-
("NB_REMAINDER", "%"),
58-
("NB_OR", "|"),
59-
("NB_POWER", "**"),
60-
("NB_RSHIFT", ">>"),
61-
("NB_SUBTRACT", "-"),
62-
("NB_TRUE_DIVIDE", "/"),
63-
("NB_XOR", "^"),
64-
("NB_INPLACE_ADD", "+="),
65-
("NB_INPLACE_AND", "&="),
66-
("NB_INPLACE_FLOOR_DIVIDE", "//="),
67-
("NB_INPLACE_LSHIFT", "<<="),
68-
("NB_INPLACE_MATRIX_MULTIPLY", "@="),
69-
("NB_INPLACE_MULTIPLY", "*="),
70-
("NB_INPLACE_REMAINDER", "%="),
71-
("NB_INPLACE_OR", "|="),
72-
("NB_INPLACE_POWER", "**="),
73-
("NB_INPLACE_RSHIFT", ">>="),
74-
("NB_INPLACE_SUBTRACT", "-="),
75-
("NB_INPLACE_TRUE_DIVIDE", "/="),
76-
("NB_INPLACE_XOR", "^="),
77-
]
78-
79-
8051
_cache_format = {
8152
"LOAD_GLOBAL": {
8253
"counter": 1,

Makefile.pre.in

+1-3
Original file line numberDiff line numberDiff line change
@@ -1427,13 +1427,11 @@ regen-ast:
14271427

14281428
.PHONY: regen-opcode
14291429
regen-opcode:
1430-
# Regenerate Include/opcode.h from Lib/opcode.py
1430+
# Regenerate Include/internal/pycore_opcode.h from Lib/opcode.py
14311431
# using Tools/build/generate_opcode_h.py
14321432
$(PYTHON_FOR_REGEN) $(srcdir)/Tools/build/generate_opcode_h.py \
14331433
$(srcdir)/Lib/opcode.py \
1434-
$(srcdir)/Include/opcode.h.new \
14351434
$(srcdir)/Include/internal/pycore_opcode.h.new
1436-
$(UPDATE_FILE) $(srcdir)/Include/opcode.h $(srcdir)/Include/opcode.h.new
14371435
$(UPDATE_FILE) $(srcdir)/Include/internal/pycore_opcode.h $(srcdir)/Include/internal/pycore_opcode.h.new
14381436

14391437
.PHONY: regen-token

Modules/_opcode.c

+70
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,75 @@ _opcode_get_specialization_stats_impl(PyObject *module)
223223

224224
/*[clinic input]
225225
226+
_opcode.get_nb_ops
227+
228+
Return array of symbols of binary ops.
229+
230+
Indexed by the BINARY_OP oparg value.
231+
[clinic start generated code]*/
232+
233+
static PyObject *
234+
_opcode_get_nb_ops_impl(PyObject *module)
235+
/*[clinic end generated code: output=d997d306cc15426f input=9462fc544c823176]*/
236+
{
237+
PyObject *list = PyList_New(NB_OPARG_LAST + 1);
238+
if (list == NULL) {
239+
return NULL;
240+
}
241+
#define ADD_NB_OP(NUM, STR) \
242+
do { \
243+
PyObject *pair = Py_BuildValue( \
244+
"NN", PyUnicode_FromString(#NUM), PyUnicode_FromString(STR)); \
245+
if (pair == NULL) { \
246+
Py_DECREF(list); \
247+
return NULL; \
248+
} \
249+
PyList_SET_ITEM(list, (NUM), pair); \
250+
} while(0);
251+
252+
ADD_NB_OP(NB_ADD, "+");
253+
ADD_NB_OP(NB_AND, "&");
254+
ADD_NB_OP(NB_FLOOR_DIVIDE, "//");
255+
ADD_NB_OP(NB_LSHIFT, "<<");
256+
ADD_NB_OP(NB_MATRIX_MULTIPLY, "@");
257+
ADD_NB_OP(NB_MULTIPLY, "*");
258+
ADD_NB_OP(NB_REMAINDER, "%");
259+
ADD_NB_OP(NB_OR, "|");
260+
ADD_NB_OP(NB_POWER, "**");
261+
ADD_NB_OP(NB_RSHIFT, ">>");
262+
ADD_NB_OP(NB_SUBTRACT, "-");
263+
ADD_NB_OP(NB_TRUE_DIVIDE, "/");
264+
ADD_NB_OP(NB_XOR, "^");
265+
ADD_NB_OP(NB_INPLACE_ADD, "+=");
266+
ADD_NB_OP(NB_INPLACE_AND, "&=");
267+
ADD_NB_OP(NB_INPLACE_FLOOR_DIVIDE, "//=");
268+
ADD_NB_OP(NB_INPLACE_LSHIFT, "<<=");
269+
ADD_NB_OP(NB_INPLACE_MATRIX_MULTIPLY, "@=");
270+
ADD_NB_OP(NB_INPLACE_MULTIPLY, "*=");
271+
ADD_NB_OP(NB_INPLACE_REMAINDER, "%=");
272+
ADD_NB_OP(NB_INPLACE_OR, "|=");
273+
ADD_NB_OP(NB_INPLACE_POWER, "**=");
274+
ADD_NB_OP(NB_INPLACE_RSHIFT, ">>=");
275+
ADD_NB_OP(NB_INPLACE_SUBTRACT, "-=");
276+
ADD_NB_OP(NB_INPLACE_TRUE_DIVIDE, "/=");
277+
ADD_NB_OP(NB_INPLACE_XOR, "^=");
278+
279+
#undef ADD_NB_OP
280+
281+
for(int i = 0; i <= NB_OPARG_LAST; i++) {
282+
if (PyList_GET_ITEM(list, i) == NULL) {
283+
Py_DECREF(list);
284+
PyErr_Format(PyExc_ValueError,
285+
"Missing initialization for NB_OP %d",
286+
i);
287+
return NULL;
288+
}
289+
}
290+
return list;
291+
}
292+
293+
/*[clinic input]
294+
226295
_opcode.get_intrinsic1_descs
227296
228297
Return a list of names of the unary intrinsics.
@@ -287,6 +356,7 @@ opcode_functions[] = {
287356
_OPCODE_HAS_LOCAL_METHODDEF
288357
_OPCODE_HAS_EXC_METHODDEF
289358
_OPCODE_GET_SPECIALIZATION_STATS_METHODDEF
359+
_OPCODE_GET_NB_OPS_METHODDEF
290360
_OPCODE_GET_INTRINSIC1_DESCS_METHODDEF
291361
_OPCODE_GET_INTRINSIC2_DESCS_METHODDEF
292362
{NULL, NULL, 0, NULL}

Modules/clinic/_opcode.c.h

+21-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PCbuild/regen.targets

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<Argument>-C</Argument>
1515
</_ASTOutputs>
1616
<_OpcodeSources Include="$(PySourcePath)Tools\build\generate_opcode_h.py;$(PySourcePath)Lib\opcode.py" />
17-
<_OpcodeOutputs Include="$(PySourcePath)Include\opcode.h;$(PySourcePath)Include\internal\pycore_opcode.h" />
17+
<_OpcodeOutputs Include="$(PySourcePath)Include\internal\pycore_opcode.h" />
1818
<_TokenSources Include="$(PySourcePath)Grammar\Tokens" />
1919
<_TokenOutputs Include="$(PySourcePath)Doc\library\token-list.inc">
2020
<Format>rst</Format>
@@ -59,7 +59,7 @@
5959
Inputs="@(_OpcodeSources)" Outputs="@(_OpcodeOutputs)"
6060
DependsOnTargets="FindPythonForBuild">
6161
<Message Text="Regenerate @(_OpcodeOutputs->'%(Filename)%(Extension)',' ')" Importance="high" />
62-
<Exec Command="$(PythonForBuild) Tools\build\generate_opcode_h.py Lib\opcode.py Include\opcode.h Include\internal\pycore_opcode.h Include\internal\pycore_intrinsics.h"
62+
<Exec Command="$(PythonForBuild) Tools\build\generate_opcode_h.py Lib\opcode.py Include\internal\pycore_opcode.h Include\internal\pycore_intrinsics.h"
6363
WorkingDirectory="$(PySourcePath)" />
6464
</Target>
6565

Tools/build/generate_opcode_h.py

+3-34
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,11 @@
1-
# This script generates the opcode.h header file.
1+
# This script generates the pycore_opcode.h header file.
22

33
import sys
44
import tokenize
55

66
SCRIPT_NAME = "Tools/build/generate_opcode_h.py"
77
PYTHON_OPCODE = "Lib/opcode.py"
88

9-
opcode_h_header = f"""
10-
// Auto-generated by {SCRIPT_NAME} from {PYTHON_OPCODE}
11-
12-
#ifndef Py_OPCODE_H
13-
#define Py_OPCODE_H
14-
#ifdef __cplusplus
15-
extern "C" {{
16-
#endif
17-
18-
#include "opcode_ids.h"
19-
20-
""".lstrip()
21-
22-
opcode_h_footer = """
23-
24-
#ifdef __cplusplus
25-
}
26-
#endif
27-
#endif /* !Py_OPCODE_H */
28-
"""
29-
309
internal_header = f"""
3110
// Auto-generated by {SCRIPT_NAME} from {PYTHON_OPCODE}
3211
@@ -62,20 +41,10 @@ def get_python_module_dict(filename):
6241
return mod
6342

6443
def main(opcode_py,
65-
opcode_h='Include/opcode.h',
6644
internal_opcode_h='Include/internal/pycore_opcode.h'):
6745

6846
opcode = get_python_module_dict(opcode_py)
6947

70-
with open(opcode_h, 'w') as fobj:
71-
fobj.write(opcode_h_header)
72-
73-
fobj.write("\n")
74-
for i, (op, _) in enumerate(opcode["_nb_ops"]):
75-
fobj.write(DEFINE.format(op, i))
76-
77-
fobj.write(opcode_h_footer)
78-
7948
with open(internal_opcode_h, 'w') as iobj:
8049
iobj.write(internal_header)
8150

@@ -91,8 +60,8 @@ def main(opcode_py,
9160

9261
iobj.write(internal_footer)
9362

94-
print(f"{opcode_h} regenerated from {opcode_py}")
63+
print(f"{internal_opcode_h} regenerated from {opcode_py}")
9564

9665

9766
if __name__ == '__main__':
98-
main(sys.argv[1], sys.argv[2], sys.argv[3])
67+
main(sys.argv[1], sys.argv[2])

0 commit comments

Comments
 (0)