gh-105481: split opcode_ids.h out of opcode.h so that it can be generated separately (#107866)

This commit is contained in:
Irit Katriel 2023-08-11 17:42:01 +01:00 • committed by GitHub
parent 52e0797f8e
commit caa41a4f1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 286 additions and 236 deletions

View file

@ -6,7 +6,7 @@ import tokenize
SCRIPT_NAME = "Tools/build/generate_opcode_h.py"
PYTHON_OPCODE = "Lib/opcode.py"
header = f"""
opcode_h_header = f"""
// Auto-generated by {SCRIPT_NAME} from {PYTHON_OPCODE}
#ifndef Py_OPCODE_H
@ -15,11 +15,11 @@ header = f"""
extern "C" {{
#endif
#include "opcode_ids.h"
/* Instruction opcodes for compiled code */
""".lstrip()
footer = """
opcode_h_footer = """
#ifdef __cplusplus
}
@ -27,6 +27,27 @@ footer = """
#endif /* !Py_OPCODE_H */
"""
opcode_ids_h_header = f"""
// Auto-generated by {SCRIPT_NAME} from {PYTHON_OPCODE}
#ifndef Py_OPCODE_IDS_H
#define Py_OPCODE_IDS_H
#ifdef __cplusplus
extern "C" {{
#endif
/* Instruction opcodes for compiled code */
""".lstrip()
opcode_ids_h_footer = """
#ifdef __cplusplus
}
#endif
#endif /* !Py_OPCODE_IDS_H */
"""
internal_header = f"""
// Auto-generated by {SCRIPT_NAME} from {PYTHON_OPCODE}
@ -63,9 +84,10 @@ def get_python_module_dict(filename):
def main(opcode_py,
_opcode_metadata_py='Lib/_opcode_metadata.py',
outfile='Include/opcode.h',
opcode_ids_h='Include/opcode_ids.h',
opcode_h='Include/opcode.h',
opcode_targets_h='Python/opcode_targets.h',
internaloutfile='Include/internal/pycore_opcode.h'):
internal_opcode_h='Include/internal/pycore_opcode.h'):
_opcode_metadata = get_python_module_dict(_opcode_metadata_py)
@ -91,9 +113,8 @@ def main(opcode_py,
opname_including_specialized[next_op] = name
used[next_op] = True
with open(outfile, 'w') as fobj, open(internaloutfile, 'w') as iobj:
fobj.write(header)
iobj.write(internal_header)
with open(opcode_ids_h, 'w') as fobj:
fobj.write(opcode_ids_h_header)
for name in opname:
if name in opmap:
@ -107,6 +128,20 @@ def main(opcode_py,
for name, op in specialized_opmap.items():
fobj.write(DEFINE.format(name, op))
fobj.write(opcode_ids_h_footer)
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)
iobj.write("\nextern const uint8_t _PyOpcode_Caches[256];\n")
iobj.write("\nextern const uint8_t _PyOpcode_Deopt[256];\n")
iobj.write("\n#ifdef NEED_OPCODE_TABLES\n")
@ -129,10 +164,6 @@ def main(opcode_py,
iobj.write("};\n")
iobj.write("#endif // NEED_OPCODE_TABLES\n")
fobj.write("\n")
for i, (op, _) in enumerate(opcode["_nb_ops"]):
fobj.write(DEFINE.format(op, i))
iobj.write("\n")
iobj.write(f"\nextern const char *const _PyOpcode_OpName[{NUM_OPCODES}];\n")
iobj.write("\n#ifdef NEED_OPCODE_TABLES\n")
@ -151,7 +182,6 @@ def main(opcode_py,
iobj.write(f" case {i}: \\\n")
iobj.write(" ;\n")
fobj.write(footer)
iobj.write(internal_footer)
with open(opcode_targets_h, "w") as f:
@ -164,8 +194,9 @@ def main(opcode_py,
f.write(",\n".join([f" &&{s}" for s in targets]))
f.write("\n};\n")
print(f"{outfile} regenerated from {opcode_py}")
print(f"{opcode_h} regenerated from {opcode_py}")
if __name__ == '__main__':
main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])
main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4],
sys.argv[5], sys.argv[6])