From 2bd04d423404e5ea862c858f62af76feade4d831 Mon Sep 17 00:00:00 2001
From: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Date: Tue, 1 Aug 2023 21:05:48 +0100
Subject: [PATCH] gh-105481: combine regen-opcode-targets with regen-opcode to
avoid calculating the specialized opcodes in two places (#107540)
---
Makefile.pre.in | 11 +---
...-08-01-17-12-53.gh-issue-105481.42nsDE.rst | 1 +
PCbuild/regen.targets | 4 +-
Python/makeopcodetargets.py | 56 -------------------
Tools/build/generate_opcode_h.py | 12 +++-
5 files changed, 16 insertions(+), 68 deletions(-)
create mode 100644 Misc/NEWS.d/next/Build/2023-08-01-17-12-53.gh-issue-105481.42nsDE.rst
delete mode 100755 Python/makeopcodetargets.py
diff --git a/Makefile.pre.in b/Makefile.pre.in
index 3725feaca66..12409774746 100644
--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -1316,7 +1316,7 @@ regen-limited-abi: all
# Regenerate all generated files
.PHONY: regen-all
-regen-all: regen-cases regen-opcode regen-opcode-targets regen-typeslots \
+regen-all: regen-cases regen-opcode regen-typeslots \
regen-token regen-ast regen-keyword regen-sre regen-frozen clinic \
regen-pegen-metaparser regen-pegen regen-test-frozenmain \
regen-test-levenshtein regen-global-objects
@@ -1428,8 +1428,10 @@ regen-opcode:
$(srcdir)/Lib/opcode.py \
$(srcdir)/Lib/_opcode_metadata.py \
$(srcdir)/Include/opcode.h.new \
+ $(srcdir)/Python/opcode_targets.h.new \
$(srcdir)/Include/internal/pycore_opcode.h.new
$(UPDATE_FILE) $(srcdir)/Include/opcode.h $(srcdir)/Include/opcode.h.new
+ $(UPDATE_FILE) $(srcdir)/Python/opcode_targets.h $(srcdir)/Python/opcode_targets.h.new
$(UPDATE_FILE) $(srcdir)/Include/internal/pycore_opcode.h $(srcdir)/Include/internal/pycore_opcode.h.new
.PHONY: regen-token
@@ -1531,13 +1533,6 @@ Objects/unicodeobject.o: $(srcdir)/Objects/unicodeobject.c $(UNICODE_DEPS)
Objects/dictobject.o: $(srcdir)/Objects/stringlib/eq.h
Objects/setobject.o: $(srcdir)/Objects/stringlib/eq.h
-.PHONY: regen-opcode-targets
-regen-opcode-targets:
- # Regenerate Python/opcode_targets.h from Lib/opcode.py
- # using Python/makeopcodetargets.py
- $(PYTHON_FOR_REGEN) $(srcdir)/Python/makeopcodetargets.py \
- $(srcdir)/Python/opcode_targets.h.new
- $(UPDATE_FILE) $(srcdir)/Python/opcode_targets.h $(srcdir)/Python/opcode_targets.h.new
.PHONY: regen-cases
regen-cases:
diff --git a/Misc/NEWS.d/next/Build/2023-08-01-17-12-53.gh-issue-105481.42nsDE.rst b/Misc/NEWS.d/next/Build/2023-08-01-17-12-53.gh-issue-105481.42nsDE.rst
new file mode 100644
index 00000000000..1e61c37b609
--- /dev/null
+++ b/Misc/NEWS.d/next/Build/2023-08-01-17-12-53.gh-issue-105481.42nsDE.rst
@@ -0,0 +1 @@
+Remove the make target ``regen-opcode-targets``, merge its work into ``regen-opcode`` which repeats most of the calculation. This simplifies the code for the build and reduces code duplication.
diff --git a/PCbuild/regen.targets b/PCbuild/regen.targets
index 2dd786e5e82..99cfff5acc0 100644
--- a/PCbuild/regen.targets
+++ b/PCbuild/regen.targets
@@ -59,9 +59,7 @@
Inputs="@(_OpcodeSources)" Outputs="@(_OpcodeOutputs)"
DependsOnTargets="FindPythonForBuild">
-
-
diff --git a/Python/makeopcodetargets.py b/Python/makeopcodetargets.py
deleted file mode 100755
index 5843079b729..00000000000
--- a/Python/makeopcodetargets.py
+++ /dev/null
@@ -1,56 +0,0 @@
-#! /usr/bin/env python
-"""Generate C code for the jump table of the threaded code interpreter
-(for compilers supporting computed gotos or "labels-as-values", such as gcc).
-"""
-
-import os
-import sys
-
-
-# 2023-04-27(warsaw): Pre-Python 3.12, this would catch ImportErrors and try to
-# import imp, and then use imp.load_module(). The imp module was removed in
-# Python 3.12 (and long deprecated before that), and it's unclear under what
-# conditions this import will now fail, so the fallback was simply removed.
-from importlib.machinery import SourceFileLoader
-
-def find_module(modname):
- """Finds and returns a module in the local dist/checkout.
- """
- modpath = os.path.join(
- os.path.dirname(os.path.dirname(__file__)), "Lib", modname + ".py")
- return SourceFileLoader(modname, modpath).load_module()
-
-
-def write_contents(f):
- """Write C code contents to the target file object.
- """
- opcode = find_module('opcode')
- _opcode_metadata = find_module('_opcode_metadata')
- targets = ['_unknown_opcode'] * 256
- for opname, op in opcode.opmap.items():
- if not opcode.is_pseudo(op):
- targets[op] = "TARGET_%s" % opname
- next_op = 1
- for opname in _opcode_metadata._specialized_instructions:
- while targets[next_op] != '_unknown_opcode':
- next_op += 1
- targets[next_op] = "TARGET_%s" % opname
- f.write("static void *opcode_targets[256] = {\n")
- f.write(",\n".join([" &&%s" % s for s in targets]))
- f.write("\n};\n")
-
-
-def main():
- if len(sys.argv) >= 3:
- sys.exit("Too many arguments")
- if len(sys.argv) == 2:
- target = sys.argv[1]
- else:
- target = "Python/opcode_targets.h"
- with open(target, "w") as f:
- write_contents(f)
- print("Jump table written into %s" % target)
-
-
-if __name__ == "__main__":
- main()
diff --git a/Tools/build/generate_opcode_h.py b/Tools/build/generate_opcode_h.py
index 2259dad7786..16b028dc120 100644
--- a/Tools/build/generate_opcode_h.py
+++ b/Tools/build/generate_opcode_h.py
@@ -64,6 +64,7 @@ def get_python_module_dict(filename):
def main(opcode_py,
_opcode_metadata_py='Lib/_opcode_metadata.py',
outfile='Include/opcode.h',
+ opcode_targets_h='Python/opcode_targets.h',
internaloutfile='Include/internal/pycore_opcode.h'):
_opcode_metadata = get_python_module_dict(_opcode_metadata_py)
@@ -161,9 +162,18 @@ def main(opcode_py,
fobj.write(footer)
iobj.write(internal_footer)
+ with open(opcode_targets_h, "w") as f:
+ targets = ["_unknown_opcode"] * 256
+ for op, name in enumerate(opname_including_specialized):
+ if op < 256 and not name.startswith("<"):
+ targets[op] = f"TARGET_{name}"
+
+ f.write("static void *opcode_targets[256] = {\n")
+ f.write(",\n".join([f" &&{s}" for s in targets]))
+ f.write("\n};\n")
print(f"{outfile} regenerated from {opcode_py}")
if __name__ == '__main__':
- main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
+ main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])