Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update generate_opcode to generate intrinsics header file
  • Loading branch information
jkchandalia committed May 1, 2023
commit 89bfd1218228168bd6d8536e58d1c6b1b8a846c9
39 changes: 37 additions & 2 deletions Tools/build/generate_opcode_h.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
#endif // !Py_INTERNAL_OPCODE_H
"""

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

""".lstrip()

DEFINE = "#define {:<38} {:>3}\n"

UINT32_MASK = (1<<32)-1
Expand All @@ -67,7 +72,9 @@ def write_int_array_from_ops(name, ops, out):
assert bits == 0
out.write(f"}};\n")

def main(opcode_py, outfile='Include/opcode.h', internaloutfile='Include/internal/pycore_opcode.h'):
def main(opcode_py, outfile='Include/opcode.h',
internaloutfile='Include/internal/pycore_opcode.h',
intrinsicoutfile='Include/internal/pycore_intrinsics2.h'):
opcode = {}
if hasattr(tokenize, 'open'):
fp = tokenize.open(opcode_py) # Python 3.2+
Expand Down Expand Up @@ -107,9 +114,11 @@ def main(opcode_py, outfile='Include/opcode.h', internaloutfile='Include/interna
opname_including_specialized[next_op] = name
used[next_op] = True

with open(outfile, 'w') as fobj, open(internaloutfile, 'w') as iobj:
with open(outfile, 'w') as fobj, open(internaloutfile, 'w') as iobj, open(
intrinsicoutfile, "w") as nobj:
fobj.write(header)
iobj.write(internal_header)
nobj.write(intrinsic_header)

for name in opname:
if name in opmap:
Expand Down Expand Up @@ -172,6 +181,32 @@ def main(opcode_py, outfile='Include/opcode.h', internaloutfile='Include/interna
for i, (op, _) in enumerate(opcode["_nb_ops"]):
fobj.write(DEFINE.format(op, i))

nobj.write("/* Unary Functions: */")
nobj.write("\n")
for i, op in enumerate(opcode["_intrinsic_1_descs"]):
nobj.write(DEFINE.format(op, i))
nobj.write("\n")
nobj.write(DEFINE.format("MAX_INTRINSIC_1", i))

nobj.write("\n\n")
nobj.write("/* Binary Functions: */\n")
for i, op in enumerate(opcode["_intrinsic_2_descs"]):
nobj.write(DEFINE.format(op, i))
nobj.write("\n")
nobj.write(DEFINE.format("MAX_INTRINSIC_2", i))

nobj.write(
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated
"typedef PyObject *(*instrinsic_func1)(PyThreadState* tstate, PyObject *value);"
)
nobj.write("\n")
nobj.write(
"typedef PyObject *(*instrinsic_func2)(PyThreadState* tstate, PyObject *value1, PyObject *value2);"
)
nobj.write("\n")
nobj.write("extern const instrinsic_func1 _PyIntrinsics_UnaryFunctions[];")
nobj.write("\n")
nobj.write("extern const instrinsic_func2 _PyIntrinsics_BinaryFunctions[];")
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated

fobj.write("\n")
fobj.write("/* Defined in Lib/opcode.py */\n")
fobj.write(f"#define ENABLE_SPECIALIZATION {int(ENABLE_SPECIALIZATION)}")
Expand Down