Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-105481: opcode.h is no longer generated during the build #108080

Merged
merged 3 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions Include/opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 1 addition & 30 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,39 +44,10 @@

_intrinsic_1_descs = _opcode.get_intrinsic1_descs()
_intrinsic_2_descs = _opcode.get_intrinsic2_descs()
_nb_ops = _opcode.get_nb_ops()

hascompare = [opmap["COMPARE_OP"]]

_nb_ops = [
("NB_ADD", "+"),
("NB_AND", "&"),
("NB_FLOOR_DIVIDE", "//"),
("NB_LSHIFT", "<<"),
("NB_MATRIX_MULTIPLY", "@"),
("NB_MULTIPLY", "*"),
("NB_REMAINDER", "%"),
("NB_OR", "|"),
("NB_POWER", "**"),
("NB_RSHIFT", ">>"),
("NB_SUBTRACT", "-"),
("NB_TRUE_DIVIDE", "/"),
("NB_XOR", "^"),
("NB_INPLACE_ADD", "+="),
("NB_INPLACE_AND", "&="),
("NB_INPLACE_FLOOR_DIVIDE", "//="),
("NB_INPLACE_LSHIFT", "<<="),
("NB_INPLACE_MATRIX_MULTIPLY", "@="),
("NB_INPLACE_MULTIPLY", "*="),
("NB_INPLACE_REMAINDER", "%="),
("NB_INPLACE_OR", "|="),
("NB_INPLACE_POWER", "**="),
("NB_INPLACE_RSHIFT", ">>="),
("NB_INPLACE_SUBTRACT", "-="),
("NB_INPLACE_TRUE_DIVIDE", "/="),
("NB_INPLACE_XOR", "^="),
]


_cache_format = {
"LOAD_GLOBAL": {
"counter": 1,
Expand Down
4 changes: 1 addition & 3 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -1427,13 +1427,11 @@ regen-ast:

.PHONY: regen-opcode
regen-opcode:
# Regenerate Include/opcode.h from Lib/opcode.py
# Regenerate Include/internal/pycore_opcode.h from Lib/opcode.py
# using Tools/build/generate_opcode_h.py
$(PYTHON_FOR_REGEN) $(srcdir)/Tools/build/generate_opcode_h.py \
$(srcdir)/Lib/opcode.py \
$(srcdir)/Include/opcode.h.new \
$(srcdir)/Include/internal/pycore_opcode.h.new
$(UPDATE_FILE) $(srcdir)/Include/opcode.h $(srcdir)/Include/opcode.h.new
$(UPDATE_FILE) $(srcdir)/Include/internal/pycore_opcode.h $(srcdir)/Include/internal/pycore_opcode.h.new

.PHONY: regen-token
Expand Down
70 changes: 70 additions & 0 deletions Modules/_opcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,75 @@ _opcode_get_specialization_stats_impl(PyObject *module)

/*[clinic input]

_opcode.get_nb_ops

Return array of symbols of binary ops.

Indexed by the BINARY_OP oparg value.
[clinic start generated code]*/

static PyObject *
_opcode_get_nb_ops_impl(PyObject *module)
/*[clinic end generated code: output=d997d306cc15426f input=9462fc544c823176]*/
{
PyObject *list = PyList_New(NB_OPARG_LAST + 1);
if (list == NULL) {
return NULL;
}
#define ADD_NB_OP(NUM, STR) \
do { \
PyObject *pair = Py_BuildValue( \
"NN", PyUnicode_FromString(#NUM), PyUnicode_FromString(STR)); \
if (pair == NULL) { \
Py_DECREF(list); \
return NULL; \
} \
PyList_SET_ITEM(list, (NUM), pair); \
} while(0);

ADD_NB_OP(NB_ADD, "+");
ADD_NB_OP(NB_AND, "&");
ADD_NB_OP(NB_FLOOR_DIVIDE, "//");
ADD_NB_OP(NB_LSHIFT, "<<");
ADD_NB_OP(NB_MATRIX_MULTIPLY, "@");
ADD_NB_OP(NB_MULTIPLY, "*");
ADD_NB_OP(NB_REMAINDER, "%");
ADD_NB_OP(NB_OR, "|");
ADD_NB_OP(NB_POWER, "**");
ADD_NB_OP(NB_RSHIFT, ">>");
ADD_NB_OP(NB_SUBTRACT, "-");
ADD_NB_OP(NB_TRUE_DIVIDE, "/");
ADD_NB_OP(NB_XOR, "^");
ADD_NB_OP(NB_INPLACE_ADD, "+=");
ADD_NB_OP(NB_INPLACE_AND, "&=");
ADD_NB_OP(NB_INPLACE_FLOOR_DIVIDE, "//=");
ADD_NB_OP(NB_INPLACE_LSHIFT, "<<=");
ADD_NB_OP(NB_INPLACE_MATRIX_MULTIPLY, "@=");
ADD_NB_OP(NB_INPLACE_MULTIPLY, "*=");
ADD_NB_OP(NB_INPLACE_REMAINDER, "%=");
ADD_NB_OP(NB_INPLACE_OR, "|=");
ADD_NB_OP(NB_INPLACE_POWER, "**=");
ADD_NB_OP(NB_INPLACE_RSHIFT, ">>=");
ADD_NB_OP(NB_INPLACE_SUBTRACT, "-=");
ADD_NB_OP(NB_INPLACE_TRUE_DIVIDE, "/=");
ADD_NB_OP(NB_INPLACE_XOR, "^=");

#undef ADD_NB_OP

for(int i = 0; i <= NB_OPARG_LAST; i++) {
if (PyList_GET_ITEM(list, i) == NULL) {
Py_DECREF(list);
PyErr_Format(PyExc_ValueError,
"Missing initialization for NB_OP %d",
i);
return NULL;
}
}
return list;
}

/*[clinic input]

_opcode.get_intrinsic1_descs

Return a list of names of the unary intrinsics.
Expand Down Expand Up @@ -287,6 +356,7 @@ opcode_functions[] = {
_OPCODE_HAS_LOCAL_METHODDEF
_OPCODE_HAS_EXC_METHODDEF
_OPCODE_GET_SPECIALIZATION_STATS_METHODDEF
_OPCODE_GET_NB_OPS_METHODDEF
_OPCODE_GET_INTRINSIC1_DESCS_METHODDEF
_OPCODE_GET_INTRINSIC2_DESCS_METHODDEF
{NULL, NULL, 0, NULL}
Expand Down
22 changes: 21 additions & 1 deletion Modules/clinic/_opcode.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions PCbuild/regen.targets
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Argument>-C</Argument>
</_ASTOutputs>
<_OpcodeSources Include="$(PySourcePath)Tools\build\generate_opcode_h.py;$(PySourcePath)Lib\opcode.py" />
<_OpcodeOutputs Include="$(PySourcePath)Include\opcode.h;$(PySourcePath)Include\internal\pycore_opcode.h" />
<_OpcodeOutputs Include="$(PySourcePath)Include\internal\pycore_opcode.h" />
<_TokenSources Include="$(PySourcePath)Grammar\Tokens" />
<_TokenOutputs Include="$(PySourcePath)Doc\library\token-list.inc">
<Format>rst</Format>
Expand Down Expand Up @@ -59,7 +59,7 @@
Inputs="@(_OpcodeSources)" Outputs="@(_OpcodeOutputs)"
DependsOnTargets="FindPythonForBuild">
<Message Text="Regenerate @(_OpcodeOutputs->'%(Filename)%(Extension)',' ')" Importance="high" />
<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"
<Exec Command="$(PythonForBuild) Tools\build\generate_opcode_h.py Lib\opcode.py Include\internal\pycore_opcode.h Include\internal\pycore_intrinsics.h"
WorkingDirectory="$(PySourcePath)" />
</Target>

Expand Down
37 changes: 3 additions & 34 deletions Tools/build/generate_opcode_h.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
# This script generates the opcode.h header file.
# This script generates the pycore_opcode.h header file.

import sys
import tokenize

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

opcode_h_header = f"""
// Auto-generated by {SCRIPT_NAME} from {PYTHON_OPCODE}

#ifndef Py_OPCODE_H
#define Py_OPCODE_H
#ifdef __cplusplus
extern "C" {{
#endif

#include "opcode_ids.h"

""".lstrip()

opcode_h_footer = """

#ifdef __cplusplus
}
#endif
#endif /* !Py_OPCODE_H */
"""

internal_header = f"""
// Auto-generated by {SCRIPT_NAME} from {PYTHON_OPCODE}

Expand Down Expand Up @@ -62,20 +41,10 @@ def get_python_module_dict(filename):
return mod

def main(opcode_py,
opcode_h='Include/opcode.h',
internal_opcode_h='Include/internal/pycore_opcode.h'):

opcode = get_python_module_dict(opcode_py)

with open(opcode_h, 'w') as fobj:
fobj.write(opcode_h_header)

fobj.write("\n")
for i, (op, _) in enumerate(opcode["_nb_ops"]):
fobj.write(DEFINE.format(op, i))

fobj.write(opcode_h_footer)

with open(internal_opcode_h, 'w') as iobj:
iobj.write(internal_header)

Expand All @@ -91,8 +60,8 @@ def main(opcode_py,

iobj.write(internal_footer)

print(f"{opcode_h} regenerated from {opcode_py}")
print(f"{internal_opcode_h} regenerated from {opcode_py}")


if __name__ == '__main__':
main(sys.argv[1], sys.argv[2], sys.argv[3])
main(sys.argv[1], sys.argv[2])