8000 gh-98831: Use opcode metadata for stack_effect() by gvanrossum · Pull Request #101704 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-98831: Use opcode metadata for stack_effect() #101704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
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
Declare or define opcode metadata based on NEED_OPCODE_TABLES
  • Loading branch information
gvanrossum committed Feb 8, 2023
commit 383c1fc3b0030951cadb6ed6187ea4f166bb66b9
21 changes: 16 additions & 5 deletions Python/opcode_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// from Python/bytecodes.c
// Do not edit!

#ifndef NDEBUG
static int
#ifndef NEED_OPCODE_TABLES
extern int _PyOpcode_num_popped(int opcode, int oparg, bool jump);
#else
int
_PyOpcode_num_popped(int opcode, int oparg, bool jump) {
switch(opcode) {
case NOP:
Expand Down Expand Up @@ -350,8 +352,10 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
}
#endif

#ifndef NDEBUG
static int
#ifndef NEED_OPCODE_TABLES
extern int _PyOpcode_num_pushed(int opcode, int oparg, bool jump);
#else
int
_PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
switch(opcode) {
case NOP:
Expand Down Expand Up @@ -697,6 +701,7 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
}
}
#endif

enum Direction { DIR_NONE, DIR_READ, DIR_WRITE };
enum InstructionFormat { INSTR_FMT_IB, INSTR_FMT_IBC, INSTR_FMT_IBC0, INSTR_FMT_IBC000, INSTR_FMT_IBC0000, INSTR_FMT_IBC00000000, INSTR_FMT_IBIB, INSTR_FMT_IX, INSTR_FMT_IXC, INSTR_FMT_IXC000 };
struct opcode_metadata {
Expand All @@ -705,7 +710,12 @@ struct opcode_metadata {
enum Direction dir_op3;
bool valid_entry;
enum InstructionFormat instr_format;
} _PyOpcode_opcode_metadata[256] = {
};

#ifndef NEED_OPCODE_TABLES
extern const struct opcode_metadata _PyOpcode_opcode_metadata[256];
#else
const struct opcode_metadata _PyOpcode_opcode_metadata[256] = {
[NOP] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[RESUME] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[LOAD_CLOSURE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
Expand Down Expand Up @@ -876,3 +886,4 @@ struct opcode_metadata {
[EXTENDED_ARG] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[CACHE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
};
#endif
20 changes: 16 additions & 4 deletions Tools/cases_generator/generate_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,11 @@ def write_stack_effect_functions(self) -> None:
def write_function(
direction: str, data: list[tuple[AnyInstruction, str]]
) -> None:
self.out.emit("\n#ifndef NDEBUG")
self.out.emit("static int")
self.out.emit("")
self.out.emit("#ifndef NEED_OPCODE_TABLES")
self.out.emit(f"extern int _PyOpcode_num_{direction}(int opcode, int oparg, bool jump);")
self.out.emit("#else")
self.out.emit("int")
self.out.emit(f"_PyOpcode_num_{direction}(int opcode, int oparg, bool jump) {{")
self.out.emit(" switch(opcode) {")
for instr, effect in data:
Expand All @@ -903,6 +906,7 @@ def write_function(

write_function("popped", popped_data)
write_function("pushed", pushed_data)
self.out.emit("")

def write_metadata(self) -> None:
"""Write instruction metadata to output file."""
Expand Down Expand Up @@ -934,7 +938,7 @@ def write_metadata(self) -> None:

self.write_stack_effect_functions()

# Write variable definition
# Write type definitions
self.out.emit("enum Direction { DIR_NONE, DIR_READ, DIR_WRITE };")
self.out.emit(f"enum InstructionFormat {{ {', '.join(format_enums)} }};")
self.out.emit("struct opcode_metadata {")
Expand All @@ -944,7 +948,14 @@ def write_metadata(self) -> None:
self.out.emit("enum Direction dir_op3;")
self.out.emit("bool valid_entry;")
self.out.emit("enum InstructionFormat instr_format;")
self.out.emit("} _PyOpcode_opcode_metadata[256] = {")
self.out.emit("};")
self.out.emit("")

# Write metadata array declaration
self.out.emit("#ifndef NEED_OPCODE_TABLES")
self.out.emit("extern const struct opcode_metadata _PyOpcode_opcode_metadata[256];")
self.out.emit("#else")
self.out.emit("const struct opcode_metadata _PyOpcode_opcode_metadata[256] = {")

# Write metadata for each instruction
for thing in self.everything:
Expand All @@ -961,6 +972,7 @@ def write_metadata(self) -> None:

# Write end of array
self.out.emit("};")
self.out.emit("#endif")

def write_metadata_for_inst(self, instr: Instruction) -> None:
"""Write metadata for a single instruction."""
Expand Down
0