2014-04-15 15:20:06 -03:00
|
|
|
# This script generates the opcode.h header file.
|
|
|
|
|
|
|
|
import sys
|
2016-11-28 13:13:52 -04:00
|
|
|
import tokenize
|
|
|
|
|
2022-10-17 07:01:00 -03:00
|
|
|
SCRIPT_NAME = "Tools/build/generate_opcode_h.py"
|
2022-04-25 19:14:30 -03:00
|
|
|
PYTHON_OPCODE = "Lib/opcode.py"
|
|
|
|
|
|
|
|
header = f"""
|
|
|
|
// Auto-generated by {SCRIPT_NAME} from {PYTHON_OPCODE}
|
|
|
|
|
2014-04-15 15:20:06 -03:00
|
|
|
#ifndef Py_OPCODE_H
|
|
|
|
#define Py_OPCODE_H
|
|
|
|
#ifdef __cplusplus
|
2022-04-25 19:14:30 -03:00
|
|
|
extern "C" {{
|
2014-04-15 15:20:06 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2021-11-11 02:56:22 -04:00
|
|
|
/* Instruction opcodes for compiled code */
|
2018-10-18 16:53:18 -03:00
|
|
|
""".lstrip()
|
2014-04-15 15:20:06 -03:00
|
|
|
|
|
|
|
footer = """
|
|
|
|
|
2022-07-01 11:33:35 -03:00
|
|
|
#define IS_PSEUDO_OPCODE(op) (((op) >= MIN_PSEUDO_OPCODE) && ((op) <= MAX_PSEUDO_OPCODE))
|
2021-05-07 11:19:19 -03:00
|
|
|
|
2014-04-15 15:20:06 -03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* !Py_OPCODE_H */
|
|
|
|
"""
|
|
|
|
|
2022-04-25 19:14:30 -03:00
|
|
|
internal_header = f"""
|
|
|
|
// Auto-generated by {SCRIPT_NAME} from {PYTHON_OPCODE}
|
|
|
|
|
|
|
|
#ifndef Py_INTERNAL_OPCODE_H
|
|
|
|
#define Py_INTERNAL_OPCODE_H
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {{
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef Py_BUILD_CORE
|
|
|
|
# error "this header requires Py_BUILD_CORE define"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "opcode.h"
|
|
|
|
""".lstrip()
|
|
|
|
|
|
|
|
internal_footer = """
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif // !Py_INTERNAL_OPCODE_H
|
|
|
|
"""
|
|
|
|
|
2022-03-21 08:11:17 -03:00
|
|
|
DEFINE = "#define {:<38} {:>3}\n"
|
2021-11-11 02:56:22 -04:00
|
|
|
|
2020-08-04 13:30:11 -03:00
|
|
|
UINT32_MASK = (1<<32)-1
|
|
|
|
|
|
|
|
def write_int_array_from_ops(name, ops, out):
|
|
|
|
bits = 0
|
|
|
|
for op in ops:
|
|
|
|
bits |= 1<<op
|
2022-07-01 11:33:35 -03:00
|
|
|
out.write(f"static const uint32_t {name}[9] = {{\n")
|
|
|
|
for i in range(9):
|
2020-08-04 13:30:11 -03:00
|
|
|
out.write(f" {bits & UINT32_MASK}U,\n")
|
|
|
|
bits >>= 32
|
|
|
|
assert bits == 0
|
|
|
|
out.write(f"}};\n")
|
2014-04-15 15:20:06 -03:00
|
|
|
|
2022-04-25 19:14:30 -03:00
|
|
|
def main(opcode_py, outfile='Include/opcode.h', internaloutfile='Include/internal/pycore_opcode.h'):
|
2014-04-16 20:13:29 -03:00
|
|
|
opcode = {}
|
2016-11-28 13:13:52 -04:00
|
|
|
if hasattr(tokenize, 'open'):
|
|
|
|
fp = tokenize.open(opcode_py) # Python 3.2+
|
|
|
|
else:
|
|
|
|
fp = open(opcode_py) # Python 2.7
|
|
|
|
with fp:
|
2016-11-25 06:59:52 -04:00
|
|
|
code = fp.read()
|
|
|
|
exec(code, opcode)
|
2014-04-16 20:13:29 -03:00
|
|
|
opmap = opcode['opmap']
|
2022-02-25 08:11:34 -04:00
|
|
|
opname = opcode['opname']
|
2022-07-01 11:33:35 -03:00
|
|
|
hasarg = opcode['hasarg']
|
2021-09-14 05:53:32 -03:00
|
|
|
hasconst = opcode['hasconst']
|
2020-08-04 13:30:11 -03:00
|
|
|
hasjrel = opcode['hasjrel']
|
|
|
|
hasjabs = opcode['hasjabs']
|
2022-07-01 11:33:35 -03:00
|
|
|
is_pseudo = opcode['is_pseudo']
|
|
|
|
_pseudo_ops = opcode['_pseudo_ops']
|
|
|
|
|
|
|
|
HAVE_ARGUMENT = opcode["HAVE_ARGUMENT"]
|
|
|
|
MIN_PSEUDO_OPCODE = opcode["MIN_PSEUDO_OPCODE"]
|
|
|
|
MAX_PSEUDO_OPCODE = opcode["MAX_PSEUDO_OPCODE"]
|
|
|
|
|
|
|
|
NUM_OPCODES = len(opname)
|
|
|
|
used = [ False ] * len(opname)
|
2021-06-10 04:46:01 -03:00
|
|
|
next_op = 1
|
2022-04-14 17:00:58 -03:00
|
|
|
|
2021-06-10 04:46:01 -03:00
|
|
|
for name, op in opmap.items():
|
|
|
|
used[op] = True
|
2022-04-14 17:00:58 -03:00
|
|
|
|
|
|
|
specialized_opmap = {}
|
|
|
|
opname_including_specialized = opname.copy()
|
|
|
|
for name in opcode['_specialized_instructions']:
|
|
|
|
while used[next_op]:
|
|
|
|
next_op += 1
|
|
|
|
specialized_opmap[name] = next_op
|
|
|
|
opname_including_specialized[next_op] = name
|
|
|
|
used[next_op] = True
|
|
|
|
specialized_opmap['DO_TRACING'] = 255
|
|
|
|
opname_including_specialized[255] = 'DO_TRACING'
|
|
|
|
used[255] = True
|
|
|
|
|
2022-11-02 15:30:09 -03:00
|
|
|
with open(outfile, 'w') as fobj, open(internaloutfile, 'w') as iobj:
|
2014-04-15 15:20:06 -03:00
|
|
|
fobj.write(header)
|
2022-04-25 19:14:30 -03:00
|
|
|
iobj.write(internal_header)
|
|
|
|
|
2022-02-25 08:11:34 -04:00
|
|
|
for name in opname:
|
2014-04-16 20:13:29 -03:00
|
|
|
if name in opmap:
|
2022-07-01 11:33:35 -03:00
|
|
|
op = opmap[name]
|
|
|
|
if op == HAVE_ARGUMENT:
|
|
|
|
fobj.write(DEFINE.format("HAVE_ARGUMENT", HAVE_ARGUMENT))
|
|
|
|
if op == MIN_PSEUDO_OPCODE:
|
|
|
|
fobj.write(DEFINE.format("MIN_PSEUDO_OPCODE", MIN_PSEUDO_OPCODE))
|
|
|
|
|
|
|
|
fobj.write(DEFINE.format(name, op))
|
|
|
|
|
|
|
|
if op == MAX_PSEUDO_OPCODE:
|
|
|
|
fobj.write(DEFINE.format("MAX_PSEUDO_OPCODE", MAX_PSEUDO_OPCODE))
|
|
|
|
|
2021-09-14 05:53:32 -03:00
|
|
|
|
2022-04-14 17:00:58 -03:00
|
|
|
for name, op in specialized_opmap.items():
|
|
|
|
fobj.write(DEFINE.format(name, op))
|
|
|
|
|
2022-04-25 19:14:30 -03:00
|
|
|
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")
|
|
|
|
write_int_array_from_ops("_PyOpcode_RelativeJump", opcode['hasjrel'], iobj)
|
|
|
|
write_int_array_from_ops("_PyOpcode_Jump", opcode['hasjrel'] + opcode['hasjabs'], iobj)
|
2022-02-28 08:56:29 -04:00
|
|
|
|
2022-04-25 19:14:30 -03:00
|
|
|
iobj.write("\nconst uint8_t _PyOpcode_Caches[256] = {\n")
|
2022-02-28 08:56:29 -04:00
|
|
|
for i, entries in enumerate(opcode["_inline_cache_entries"]):
|
|
|
|
if entries:
|
2022-04-25 19:14:30 -03:00
|
|
|
iobj.write(f" [{opname[i]}] = {entries},\n")
|
|
|
|
iobj.write("};\n")
|
|
|
|
|
2022-03-21 08:11:17 -03:00
|
|
|
deoptcodes = {}
|
2022-07-01 11:33:35 -03:00
|
|
|
for basic, op in opmap.items():
|
|
|
|
if not is_pseudo(op):
|
|
|
|
deoptcodes[basic] = basic
|
2022-03-21 08:11:17 -03:00
|
|
|
for basic, family in opcode["_specializations"].items():
|
|
|
|
for specialized in family:
|
|
|
|
deoptcodes[specialized] = basic
|
2022-04-25 19:14:30 -03:00
|
|
|
iobj.write("\nconst uint8_t _PyOpcode_Deopt[256] = {\n")
|
2022-03-21 08:11:17 -03:00
|
|
|
for opt, deopt in sorted(deoptcodes.items()):
|
2022-04-25 19:14:30 -03:00
|
|
|
iobj.write(f" [{opt}] = {deopt},\n")
|
|
|
|
iobj.write("};\n")
|
|
|
|
iobj.write("#endif // NEED_OPCODE_TABLES\n")
|
2021-09-14 05:53:32 -03:00
|
|
|
|
2022-07-01 11:33:35 -03:00
|
|
|
fobj.write("\n")
|
|
|
|
fobj.write("#define HAS_ARG(op) ((((op) >= HAVE_ARGUMENT) && (!IS_PSEUDO_OPCODE(op)))\\")
|
|
|
|
for op in _pseudo_ops:
|
|
|
|
if opmap[op] in hasarg:
|
|
|
|
fobj.write(f"\n || ((op) == {op}) \\")
|
|
|
|
fobj.write("\n )\n")
|
|
|
|
|
2021-09-14 05:53:32 -03:00
|
|
|
fobj.write("\n")
|
|
|
|
fobj.write("#define HAS_CONST(op) (false\\")
|
|
|
|
for op in hasconst:
|
2022-07-01 11:33:35 -03:00
|
|
|
fobj.write(f"\n || ((op) == {opname[op]}) \\")
|
2021-09-14 05:53:32 -03:00
|
|
|
fobj.write("\n )\n")
|
|
|
|
|
2021-11-11 02:56:22 -04:00
|
|
|
fobj.write("\n")
|
|
|
|
for i, (op, _) in enumerate(opcode["_nb_ops"]):
|
|
|
|
fobj.write(DEFINE.format(op, i))
|
|
|
|
|
2022-04-25 19:14:30 -03:00
|
|
|
iobj.write("\n")
|
|
|
|
iobj.write("#ifdef Py_DEBUG\n")
|
2022-07-01 11:33:35 -03:00
|
|
|
iobj.write(f"static const char *const _PyOpcode_OpName[{NUM_OPCODES}] = {{\n")
|
2022-04-14 17:00:58 -03:00
|
|
|
for op, name in enumerate(opname_including_specialized):
|
|
|
|
if name[0] != "<":
|
|
|
|
op = name
|
2022-04-25 19:14:30 -03:00
|
|
|
iobj.write(f''' [{op}] = "{name}",\n''')
|
|
|
|
iobj.write("};\n")
|
|
|
|
iobj.write("#endif\n")
|
2022-04-11 19:33:00 -03:00
|
|
|
|
2022-04-25 19:14:30 -03:00
|
|
|
iobj.write("\n")
|
|
|
|
iobj.write("#define EXTRA_CASES \\\n")
|
2022-04-21 15:53:57 -03:00
|
|
|
for i, flag in enumerate(used):
|
|
|
|
if not flag:
|
2022-04-25 19:14:30 -03:00
|
|
|
iobj.write(f" case {i}: \\\n")
|
|
|
|
iobj.write(" ;\n")
|
2022-04-21 15:53:57 -03:00
|
|
|
|
2014-04-15 15:20:06 -03:00
|
|
|
fobj.write(footer)
|
2022-04-25 19:14:30 -03:00
|
|
|
iobj.write(internal_footer)
|
2014-04-15 15:20:06 -03:00
|
|
|
|
2020-08-04 13:30:11 -03:00
|
|
|
|
2021-11-11 02:56:22 -04:00
|
|
|
print(f"{outfile} regenerated from {opcode_py}")
|
2016-11-28 13:13:52 -04:00
|
|
|
|
2014-04-15 15:20:06 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-04-25 19:14:30 -03:00
|
|
|
main(sys.argv[1], sys.argv[2], sys.argv[3])
|