8000 gh-117494: extract the Instruction Sequence data structure into a sep… · python/cpython@04697bc · GitHub
[go: up one dir, main page]

Skip to content

Commit 04697bc

Browse files
authored
gh-117494: extract the Instruction Sequence data structure into a separate file (#117496)
1 parent 060a96f commit 04697bc

13 files changed

+283
-224
lines changed

Include/internal/pycore_compile.h

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ extern "C" {
99
#endif
1010

1111
#include "pycore_symtable.h" // _Py_SourceLocation
12+
#include "pycore_instruction_sequence.h"
1213

1314
struct _arena; // Type defined in pycore_pyarena.h
1415
struct _mod; // Type defined in pycore_ast.h
@@ -37,38 +38,6 @@ extern int _PyAST_Optimize(
3738
int optimize,
3839
int ff_features);
3940

40-
typedef struct {
41-
int h_label;
42-
int h_startdepth;
43-
int h_preserve_lasti;
44-
} _PyCompile_ExceptHandlerInfo;
45-
46-
typedef struct {
47-
int i_opcode;
48-
int i_oparg;
49-
_Py_SourceLocation i_loc;
50-
_PyCompile_ExceptHandlerInfo i_except_handler_info;
51-
52-
/* Used by the assembler */
53-
int i_target;
54-
int i_offset;
55-
} _PyCompile_Instruction;
56-
57-
typedef struct {
58-
_PyCompile_Instruction *s_instrs;
59-
int s_allocated;
60-
int s_used;
61-
62-
int *s_labelmap; /* label id --> instr offset */
63-
int s_labelmap_size;
64-
int s_next_free_label; /* next free label id */
65-
} _PyCompile_InstructionSequence;
66-
67-
int _PyCompile_InstructionSequence_UseLabel(_PyCompile_InstructionSequence *seq, int lbl);
68-
int _PyCompile_InstructionSequence_Addop(_PyCompile_InstructionSequence *seq,
69-
int opcode, int oparg,
70-
_Py_SourceLocation loc);
71-
int _PyCompile_InstructionSequence_ApplyLabelMap(_PyCompile_InstructionSequence *seq);
7241

7342
typedef struct {
7443
PyObject *u_name;

Include/internal/pycore_flowgraph.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11-
#include "pycore_opcode_utils.h"
1211
#include "pycore_compile.h"
13-
14-
typedef struct {
15-
int id;
16-
} _PyCfgJumpTargetLabel;
12+
#include "pycore_instruction_sequence.h"
13+
#include "pycore_opcode_utils.h"
1714

1815
struct _PyCfgBuilder;
1916

20-
int _PyCfgBuilder_UseLabel(struct _PyCfgBuilder *g, _PyCfgJumpTargetLabel lbl);
17+
int _PyCfgBuilder_UseLabel(struct _PyCfgBuilder *g, _PyJumpTargetLabel lbl);
2118
int _PyCfgBuilder_Addop(struct _PyCfgBuilder *g, int opcode, int oparg, _Py_SourceLocation loc);
2219

2320
struct _PyCfgBuilder* _PyCfgBuilder_New(void);
@@ -27,14 +24,14 @@ int _PyCfgBuilder_CheckSize(struct _PyCfgBuilder* g);
2724
int _PyCfg_OptimizeCodeUnit(struct _PyCfgBuilder *g, PyObject *consts, PyObject *const_cache,
2825
int nlocals, int nparams, int firstlineno);
2926

30-
int _PyCfg_ToInstructionSequence(struct _PyCfgBuilder *g, _PyCompile_InstructionSequence *seq);
27+
int _PyCfg_ToInstructionSequence(struct _PyCfgBuilder *g, _PyInstructionSequence *seq);
3128
int _PyCfg_OptimizedCfgToInstructionSequence(struct _PyCfgBuilder *g, _PyCompile_CodeUnitMetadata *umd,
3229
int code_flags, int *stackdepth, int *nlocalsplus,
33-
_PyCompile_InstructionSequence *seq);
30+
_PyInstructionSequence *seq);
3431

3532
PyCodeObject *
3633
_PyAssemble_MakeCodeObject(_PyCompile_CodeUnitMetadata *u, PyObject *const_cache,
37-
PyObject *consts, int maxdepth, _PyCompile_InstructionSequence *instrs,
34+
PyObject *consts, int maxdepth, _PyInstructionSequence *instrs,
3835
int nlocalsplus, int code_flags, PyObject *filename);
3936

4037
#ifdef __cplusplus
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#ifndef Py_INTERNAL_INSTRUCTION_SEQUENCE_H
2+
#define Py_INTERNAL_INSTRUCTION_SEQUENCE_H
3+
4+
#ifndef Py_BUILD_CORE
5+
# error "this header requires Py_BUILD_CORE define"
6+
#endif
7+
8+
#ifdef __cplusplus
9+
extern "C" {
10+
#endif
11+
12+
typedef struct {
13+
int h_label;
14+
int h_startdepth;
15+
int h_preserve_lasti;
16+
} _PyExceptHandlerInfo;
17+
18+
typedef struct {
19+
int i_opcode;
20+
int i_oparg;
21+
_Py_SourceLocation i_loc;
22+
_PyExceptHandlerInfo i_except_handler_info;
23+
24+
/* Temporary fields, used by the assembler and in instr_sequence_to_cfg */
25+
int i_target;
26+
int i_offset;
27+
} _PyInstruction;
28+
29+
typedef struct {
30+
_PyInstruction *s_instrs;
31+
int s_allocated;
32+
int s_used;
33+
34+
int s_next_free_label; /* next free label id */
35+
/* Map of a label id to instruction offset (index into s_instrs).
36+
* If s_labelmap is NULL, then each label id is the offset itself.
37+
*/
38+
int *s_labelmap; /* label id --> instr offset */
39+
int s_labelmap_size;
40+
} _PyInstructionSequence;
41+
42+
typedef struct {
43+
int id;
44+
} _PyJumpTargetLabel;
45+
46+
int _PyInstructionSequence_UseLabel(_PyInstructionSequence *seq, int lbl);
47+
int _PyInstructionSequence_Addop(_PyInstructionSequence *seq,
48+
int opcode, int oparg,
49+
_Py_SourceLocation loc);
50+
_PyJumpTargetLabel _PyInstructionSequence_NewLabel(_PyInstructionSequence *seq);
51+
int _PyInstructionSequence_ApplyLabelMap(_PyInstructionSequence *seq);
52+
int _PyInstructionSequence_InsertInstruction(_PyInstructionSequence *seq, int pos,
53+
int opcode, int oparg, _Py_SourceLocation loc);
54+
void PyInstructionSequence_Fini(_PyInstructionSequence *seq);
55+
56+
57+
#ifdef __cplusplus
58+
}
59+
#endif
60+
#endif /* !Py_INTERNAL_INSTRUCTION_SEQUENCE_H */

Makefile.pre.in

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ PYTHON_OBJS= \
442442
Python/initconfig.o \
443443
Python/interpconfig.o \
444444
Python/instrumentation.o \
445+
Python/instruction_sequence.o \
445446
Python/intrinsics.o \
446447
Python/jit.o \
447448
Python/legacy_tracing.o \
@@ -1170,6 +1171,7 @@ PYTHON_HEADERS= \
11701171
$(srcdir)/Include/internal/pycore_importdl.h \
11711172
$(srcdir)/Include/internal/pycore_initconfig.h \
11721173
$(srcdir)/Include/internal/pycore_instruments.h \
1174+
$(srcdir)/Include/internal/pycore_instruction_sequence.h \
11731175
$(srcdir)/Include/internal/pycore_interp.h \
11741176
$(srcdir)/Include/internal/pycore_intrinsics.h \
11751177
$(srcdir)/Include/internal/pycore_jit.h \
@@ -1800,7 +1802,7 @@ regen-sre:
18001802
$(srcdir)/Modules/_sre/sre_constants.h \
18011803
$(srcdir)/Modules/_sre/sre_targets.h
18021804

1803-
Python/compile.o Python/symtable.o Python/ast_unparse.o Python/ast.o Python/future.o: $(srcdir)/Include/internal/pycore_ast.h
1805+
Python/compile.o Python/symtable.o Python/ast_unparse.o Python/ast.o Python/future.o: $(srcdir)/Include/internal/pycore_ast.h $(srcdir)/Include/internal/pycore_ast.h
18041806

18051807
Python/getplatform.o: $(srcdir)/Python/getplatform.c
18061808
$(CC) -c $(PY_CORE_CFLAGS) -DPLATFORM='"$(MACHDEP)"' -o $@ $(srcdir)/Python/getplatform.c
@@ -1935,8 +1937,12 @@ regen-uop-metadata:
19351937
$(srcdir)/Include/internal/pycore_uop_metadata.h.new $(srcdir)/Python/bytecodes.c
19361938
$(UPDATE_FILE) $(srcdir)/Include/internal/pycore_uop_metadata.h $(srcdir)/Include/internal/pycore_uop_metadata.h.new
19371939

1938-
1939-
Python/compile.o: $(srcdir)/Include/internal/pycore_opcode_metadata.h
1940+
Python/compile.o Python/assemble.o Python/flowgraph.o Python/instruction_sequence.o: \
1941+
$(srcdir)/Include/internal/pycore_compile.h \
1942+
$(srcdir)/Include/internal/pycore_flowgraph.h \
1943+
$(srcdir)/Include/internal/pycore_instruction_sequence.h \
1944+
$(srcdir)/Include/internal/pycore_opcode_metadata.h \
1945+
$(srcdir)/Include/internal/pycore_opcode_utils.h
19401946

19411947
Python/ceval.o: \
19421948
$(srcdir)/Python/ceval_macros.h \
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactored the instruction sequence data structure out of compile.c into instruction_sequence.c.

PCbuild/_freeze_module.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
<ClCompile Include="..\Python\import.c" />
223223
<ClCompile Include="..\Python\importdl.c" />
224224
<ClCompile Include="..\Python\initconfig.c" />
225+
<ClCompile Include="..\Python\instruction_sequence.c" />
225226
<ClCompile Include="..\Python\interpconfig.c" />
226227
<ClCompile Include="..\Python\intrinsics.c" />
227228
<ClCompile Include="..\Python\instrumentation.c" />

PCbuild/_freeze_module.vcxproj.filters

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@
229229
<ClCompile Include="..\Python\initconfig.c">
230230
<Filter>Source Files</Filter>
231231
</ClCompile>
232+
<ClCompile Include="..\Python\instruction_sequence.c">
232233
<ClCompile Include="..\Python\interpconfig.c">
233234
<Filter>Source Files</Filter>
234235
</ClCompile>

PCbuild/pythoncore.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@
248248
<ClInclude Include="..\Include\internal\pycore_import.h" />
249249
<ClInclude Include="..\Include\internal\pycore_importdl.h" />
250250
<ClInclude Include="..\Include\internal\pycore_initconfig.h" />
251+
<ClInclude Include="..\Include\internal\pycore_instruction_sequence.h" />
251252
<ClInclude Include="..\Include\internal\pycore_interp.h" />
252253
<ClInclude Include="..\Include\internal\pycore_intrinsics.h" />
253254
<ClInclude Include="..\Include\internal\pycore_jit.h" />
@@ -590,6 +591,7 @@
590591
<ClCompile Include="..\Python\initconfig.c" />
591592
<ClCompile Include="..\Python\interpconfig.c" />
592593
<ClCompile Include="..\Python\intrinsics.c" />
594+
<ClCompile Include="..\Python\instruction_sequence.c" />
593595
<ClCompile Include="..\Python\instrumentation.c" />
594596
<ClCompile Include="..\Python\jit.c" />
595597
<ClCompile Include="..\Python\legacy_tracing.c" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,9 @@
663663
<ClInclude Include="..\Include\internal\pycore_initconfig.h">
664664
<Filter>Include\internal</Filter>
665665
</ClInclude>
666+
<ClInclude Include="..\Include\internal\pycore_instruction_sequence.h">
667+
<Filter>Include\internal</Filter>
668+
</ClInclude>
666669
<ClInclude Include="..\Include\internal\pycore_interp.h">
667670
<Filter>Include\internal</Filter>
668671
</ClInclude>
@@ -1349,6 +1352,9 @@
13491352
<ClCompile Include="..\Python\intrinsics.c">
13501353
<Filter>Source Files</Filter>
13511354
</ClCompile>
1355+
<ClCompile Include="..\Python\instruction_sequence.c">
1356+
<Filter>Source Files</Filter>
1357+
</ClCompile>
13521358
<ClCompile Include="..\Python\instrumentation.c">
13531359
<Filter>Source Files</Filter>
13541360
</ClCompile>

Python/assemble.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "Python.h"
44
#include "pycore_code.h" // write_location_entry_start()
55
#include "pycore_compile.h"
6+
#include "pycore_instruction_sequence.h"
67
#include "pycore_opcode_utils.h" // IS_BACKWARDS_JUMP_OPCODE
78
#include "pycore_opcode_metadata.h" // is_pseudo_target, _PyOpcode_Caches
89
#include "pycore_symtable.h" // _Py_SourceLocation
@@ -23,8 +24,8 @@
2324
}
2425

2526
typedef _Py_SourceLocation location;
26-
typedef _PyCompile_Instruction instruction;
27-
typedef _PyCompile_InstructionSequence instr_sequence;
27+
typedef _PyInstruction instruction;
28+
typedef _PyInstructionSequence instr_sequence;
2829

2930
static inline bool
3031
same_location(location a, location b)
@@ -132,7 +133,7 @@ assemble_emit_exception_table_item(struct assembler *a, int value, int msb)
132133
static int
133134
assemble_emit_exception_table_entry(struct assembler *a, int start, int end,
134135
int handler_offset,
135-
_PyCompile_ExceptHandlerInfo *handler)
136+
_PyExceptHandlerInfo *handler)
136137
{
137138
Py_ssize_t len = PyBytes_GET_SIZE(a->a_except_table);
138139
if (a->a_except_table_off + MAX_SIZE_OF_ENTRY >= len) {
@@ -158,7 +159,7 @@ static int
158159
assemble_exception_table(struct assembler *a, instr_sequence *instrs)
159160
{
160161
int ioffset = 0;
161-
_PyCompile_ExceptHandlerInfo handler;
162+
_PyExceptHandlerInfo handler;
162163
handler.h_label = -1;
163164
handler.h_startdepth = -1;
164165
handler.h_preserve_lasti = -1;
@@ -736,8 +737,7 @@ _PyAssemble_MakeCodeObject(_PyCompile_CodeUnitMetadata *umd, PyObject *const_cac
736737
PyObject *consts, int maxdepth, instr_sequence *instrs,
737738
int nlocalsplus, int code_flags, PyObject *filename)
738739
{
739-
740-
if (_PyCompile_InstructionSequence_ApplyLabelMap(instrs) < 0) {
740+
if (_PyInstructionSequence_ApplyLabelMap(instrs) < 0) {
741741
return NULL;
742742
}
743743
if (resolve_unconditional_jumps(instrs) < 0) {

0 commit comments

Comments
 (0)
0