]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-105481: combine regen-opcode-targets with regen-opcode to avoid calculating the...
authorIrit Katriel <1055913+iritkatriel@users.noreply.github.com>
Tue, 1 Aug 2023 20:05:48 +0000 (21:05 +0100)
committerGitHub <noreply@github.com>
Tue, 1 Aug 2023 20:05:48 +0000 (21:05 +0100)
Makefile.pre.in
Misc/NEWS.d/next/Build/2023-08-01-17-12-53.gh-issue-105481.42nsDE.rst [new file with mode: 0644]
PCbuild/regen.targets
Python/makeopcodetargets.py [deleted file]
Tools/build/generate_opcode_h.py

index 3725feaca66ce3cf891cbf5b018335c3e3bed34d..12409774746a308a1d24551b9adb3ab95ff61475 100644 (file)
@@ -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 (file)
index 0000000..1e61c37
--- /dev/null
@@ -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.
index 2dd786e5e82e369e1ad8b6e9a8e71295c43e7162..99cfff5acc0baf77291a3c672137a657b577424c 100644 (file)
@@ -59,9 +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 Lib\_opcode_metadata.py Include\opcode.h Include\internal\pycore_opcode.h Include\internal\pycore_intrinsics.h"
-          WorkingDirectory="$(PySourcePath)" />
-    <Exec Command="$(PythonForBuild) Python\makeopcodetargets.py Python\opcode_targets.h"
+    <Exec Command="$(PythonForBuild) Tools\build\generate_opcode_h.py Lib\opcode.py Lib\_opcode_metadata.py Include\opcode.h Python/opcode_targets.h Include\internal\pycore_opcode.h Include\internal\pycore_intrinsics.h"
           WorkingDirectory="$(PySourcePath)" />
   </Target>
 
diff --git a/Python/makeopcodetargets.py b/Python/makeopcodetargets.py
deleted file mode 100755 (executable)
index 5843079..0000000
+++ /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()
index 2259dad77869f8f1b8d90254346cccdb26aee5b1..16b028dc1205ac426f289adfb7d09ff2dea9a521 100644 (file)
@@ -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])