Skip to content

Commit c91c00d

Browse files
committed
Declare or define opcode metadata based on NEED_OPCODE_TABLES
1 parent 306c366 commit c91c00d

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

Python/opcode_metadata.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// from Python/bytecodes.c
33
// Do not edit!
44

5-
#ifndef NDEBUG
6-
static int
5+
#ifndef NEED_OPCODE_TABLES
6+
extern int _PyOpcode_num_popped(int opcode, int oparg, bool jump);
7+
#else
8+
int
79
_PyOpcode_num_popped(int opcode, int oparg, bool jump) {
810
switch(opcode) {
911
case NOP:
@@ -350,8 +352,10 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
350352
}
351353
#endif
352354

353-
#ifndef NDEBUG
354-
static int
355+
#ifndef NEED_OPCODE_TABLES
356+
extern int _PyOpcode_num_pushed(int opcode, int oparg, bool jump);
357+
#else
358+
int
355359
_PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
356360
switch(opcode) {
357361
case NOP:
@@ -697,6 +701,7 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
697701
}
698702
}
699703
#endif
704+
700705
enum Direction { DIR_NONE, DIR_READ, DIR_WRITE };
701706
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 };
702707
struct opcode_metadata {
@@ -705,7 +710,12 @@ struct opcode_metadata {
705710
enum Direction dir_op3;
706711
bool valid_entry;
707712
enum InstructionFormat instr_format;
708-
} _PyOpcode_opcode_metadata[256] = {
713+
};
714+
715+
#ifndef NEED_OPCODE_TABLES
716+
extern const struct opcode_metadata _PyOpcode_opcode_metadata[256];
717+
#else
718+
const struct opcode_metadata _PyOpcode_opcode_metadata[256] = {
709719
[NOP] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
710720
[RESUME] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
711721
[LOAD_CLOSURE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
@@ -876,3 +886,4 @@ struct opcode_metadata {
876886
[EXTENDED_ARG] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
877887
[CACHE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
878888
};
889+
#endif

Tools/cases_generator/generate_cases.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -888,8 +888,11 @@ def write_stack_effect_functions(self) -> None:
888888
def write_function(
889889
direction: str, data: list[tuple[AnyInstruction, str]]
890890
) -> None:
891-
self.out.emit("\n#ifndef NDEBUG")
892-
self.out.emit("static int")
891+
self.out.emit("")
892+
self.out.emit("#ifndef NEED_OPCODE_TABLES")
893+
self.out.emit(f"extern int _PyOpcode_num_{direction}(int opcode, int oparg, bool jump);")
894+
self.out.emit("#else")
895+
self.out.emit("int")
893896
self.out.emit(f"_PyOpcode_num_{direction}(int opcode, int oparg, bool jump) {{")
894897
self.out.emit(" switch(opcode) {")
895898
for instr, effect in data:
@@ -903,6 +906,7 @@ def write_function(
903906

904907
write_function("popped", popped_data)
905908
write_function("pushed", pushed_data)
909+
self.out.emit("")
906910

907911
def write_metadata(self) -> None:
908912
"""Write instruction metadata to output file."""
@@ -934,7 +938,7 @@ def write_metadata(self) -> None:
934938

935939
self.write_stack_effect_functions()
936940

937-
# Write variable definition
941+
# Write type definitions
938942
self.out.emit("enum Direction { DIR_NONE, DIR_READ, DIR_WRITE };")
939943
self.out.emit(f"enum InstructionFormat {{ {', '.join(format_enums)} }};")
940944
self.out.emit("struct opcode_metadata {")
@@ -944,7 +948,14 @@ def write_metadata(self) -> None:
944948
self.out.emit("enum Direction dir_op3;")
945949
self.out.emit("bool valid_entry;")
946950
self.out.emit("enum InstructionFormat instr_format;")
947-
self.out.emit("} _PyOpcode_opcode_metadata[256] = {")
951+
self.out.emit("};")
952+
self.out.emit("")
953+
954+
# Write metadata array declaration
955+
self.out.emit("#ifndef NEED_OPCODE_TABLES")
956+
self.out.emit("extern const struct opcode_metadata _PyOpcode_opcode_metadata[256];")
957+
self.out.emit("#else")
958+
self.out.emit("const struct opcode_metadata _PyOpcode_opcode_metadata[256] = {")
948959

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

962973
# Write end of array
963974
self.out.emit("};")
975+
self.out.emit("#endif")
964976

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

0 commit comments

Comments
 (0)