Skip to content

Commit 1a9d891

Browse files
authored
gh-121404: split compile.c into compile.c and codegen.c (#123651)
1 parent 65fcaa3 commit 1a9d891

11 files changed

+6694
-6684
lines changed

Include/internal/pycore_compile.h

+120
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11+
#include "pycore_ast.h" // mod_ty
1112
#include "pycore_symtable.h" // _Py_SourceLocation
1213
#include "pycore_instruction_sequence.h"
1314

@@ -63,6 +64,120 @@ typedef struct {
6364
int u_firstlineno; /* the first lineno of the block */
6465
} _PyCompile_CodeUnitMetadata;
6566

67+
struct _PyCompiler;
68+
69+
typedef enum {
70+
COMPILE_OP_FAST,
71+
COMPILE_OP_GLOBAL,
72+
COMPILE_OP_DEREF,
73+
COMPILE_OP_NAME,
74+
} _PyCompile_optype;
75+
76+
/* _PyCompile_FBlockInfo tracks the current frame block.
77+
*
78+
* A frame block is used to handle loops, try/except, and try/finally.
79+
* It's called a frame block to distinguish it from a basic block in the
80+
* compiler IR.
81+
*/
82+
83+
enum _PyCompile_FBlockType {
84+
COMPILE_FBLOCK_WHILE_LOOP,
85+
COMPILE_FBLOCK_FOR_LOOP,
86+
COMPILE_FBLOCK_TRY_EXCEPT,
87+
COMPILE_FBLOCK_FINALLY_TRY,
88+
COMPILE_FBLOCK_FINALLY_END,
89+
COMPILE_FBLOCK_WITH,
90+
COMPILE_FBLOCK_ASYNC_WITH,
91+
COMPILE_FBLOCK_HANDLER_CLEANUP,
92+
COMPILE_FBLOCK_POP_VALUE,
93+
COMPILE_FBLOCK_EXCEPTION_HANDLER,
94+
COMPILE_FBLOCK_EXCEPTION_GROUP_HANDLER,
95+
COMPILE_FBLOCK_ASYNC_COMPREHENSION_GENERATOR,
96+
COMPILE_FBLOCK_STOP_ITERATION,
97+
};
98+
99+
typedef struct {
100+
enum _PyCompile_FBlockType fb_type;
101+
_PyJumpTargetLabel fb_block;
102+
_Py_SourceLocation fb_loc;
103+
/* (optional) type-specific exit or cleanup block */
104+
_PyJumpTargetLabel fb_exit;
105+
/* (optional) additional information required for unwinding */
106+
void *fb_datum;
107+
} _PyCompile_FBlockInfo;
108+
109+
110+
int _PyCompile_PushFBlock(struct _PyCompiler *c, _Py_SourceLocation loc,
111+
enum _PyCompile_FBlockType t,
112+
_PyJumpTargetLabel block_label,
113+
_PyJumpTargetLabel exit, void *datum);
114+
void _PyCompile_PopFBlock(struct _PyCompiler *c, enum _PyCompile_FBlockType t,
115+
_PyJumpTargetLabel block_label);
116+
_PyCompile_FBlockInfo *_PyCompile_TopFBlock(struct _PyCompiler *c);
117+
118+
int _PyCompile_EnterScope(struct _PyCompiler *c, identifier name, int scope_type,
119+
void *key, int lineno, PyObject *private,
120+
_PyCompile_CodeUnitMetadata *umd);
121+
void _PyCompile_ExitScope(struct _PyCompiler *c);
122+
Py_ssize_t _PyCompile_AddConst(struct _PyCompiler *c, PyObject *o);
123+
_PyInstructionSequence *_PyCompile_InstrSequence(struct _PyCompiler *c);
124+
int _PyCompile_FutureFeatures(struct _PyCompiler *c);
125+
PyObject *_PyCompile_DeferredAnnotations(struct _PyCompiler *c);
126+
PyObject *_PyCompile_Mangle(struct _PyCompiler *c, PyObject *name);
127+
PyObject *_PyCompile_MaybeMangle(struct _PyCompiler *c, PyObject *name);
128+
int _PyCompile_MaybeAddStaticAttributeToClass(struct _PyCompiler *c, expr_ty e);
129+
int _PyCompile_GetRefType(struct _PyCompiler *c, PyObject *name);
130+
int _PyCompile_LookupCellvar(struct _PyCompiler *c, PyObject *name);
131+
int _PyCompile_ResolveNameop(struct _PyCompiler *c, PyObject *mangled, int scope,
132+
_PyCompile_optype *optype, Py_ssize_t *arg);
133+
134+
int _PyCompile_IsInteractive(struct _PyCompiler *c);
135+
int _PyCompile_IsNestedScope(struct _PyCompiler *c);
136+
int _PyCompile_IsInInlinedComp(struct _PyCompiler *c);
137+
int _PyCompile_ScopeType(struct _PyCompiler *c);
138+
int _PyCompile_OptimizationLevel(struct _PyCompiler *c);
139+
PyArena *_PyCompile_Arena(struct _PyCompiler *c);
140+
int _PyCompile_LookupArg(struct _PyCompiler *c, PyCodeObject *co, PyObject *name);
141+
PyObject *_PyCompile_Qualname(struct _PyCompiler *c);
142+
_PyCompile_CodeUnitMetadata *_PyCompile_Metadata(struct _PyCompiler *c);
143+
PyObject *_PyCompile_StaticAttributesAsTuple(struct _PyCompiler *c);
144+
145+
#ifndef NDEBUG
146+
int _PyCompile_IsTopLevelAwait(struct _PyCompiler *c);
147+
#endif
148+
149+
struct symtable *_PyCompile_Symtable(struct _PyCompiler *c);
150+
PySTEntryObject *_PyCompile_SymtableEntry(struct _PyCompiler *c);
151+
152+
enum {
153+
COMPILE_SCOPE_MODULE,
154+
COMPILE_SCOPE_CLASS,
155+
COMPILE_SCOPE_FUNCTION,
156+
COMPILE_SCOPE_ASYNC_FUNCTION,
157+
COMPILE_SCOPE_LAMBDA,
158+
COMPILE_SCOPE_COMPREHENSION,
159+
COMPILE_SCOPE_ANNOTATIONS,
160+
};
161+
162+
163+
typedef struct {
164+
PyObject *pushed_locals;
165+
PyObject *temp_symbols;
166+
PyObject *fast_hidden;
167+
_PyJumpTargetLabel cleanup;
168+
} _PyCompile_InlinedComprehensionState;
169+
170+
int _PyCompile_TweakInlinedComprehensionScopes(struct _PyCompiler *c, _Py_SourceLocation loc,
171+
PySTEntryObject *entry,
172+
_PyCompile_InlinedComprehensionState *state);
173+
int _PyCompile_RevertInlinedComprehensionScopes(struct _PyCompiler *c, _Py_SourceLocation loc,
174+
_PyCompile_InlinedComprehensionState *state);
175+
int _PyCompile_AddDeferredAnnotaion(struct _PyCompiler *c, stmt_ty s);
176+
177+
int _PyCodegen_AddReturnAtEnd(struct _PyCompiler *c, int addNone);
178+
int _PyCodegen_EnterAnonymousScope(struct _PyCompiler* c, mod_ty mod);
179+
int _PyCodegen_Expression(struct _PyCompiler *c, expr_ty e);
180+
int _PyCodegen_Body(struct _PyCompiler *c, _Py_SourceLocation loc, asdl_stmt_seq *stmts);
66181

67182
/* Utility for a number of growing arrays used in the compiler */
68183
int _PyCompile_EnsureArrayLargeEnough(
@@ -74,6 +189,11 @@ int _PyCompile_EnsureArrayLargeEnough(
74189

75190
int _PyCompile_ConstCacheMergeOne(PyObject *const_cache, PyObject **obj);
76191

192+
PyCodeObject *_PyCompile_OptimizeAndAssemble(struct _PyCompiler *c, int addNone);
193+
194+
Py_ssize_t _PyCompile_DictAddObj(PyObject *dict, PyObject *o);
195+
int _PyCompile_Error(struct _PyCompiler *c, _Py_SourceLocation loc, const char *format, ...);
196+
int _PyCompile_Warn(struct _PyCompiler *c, _Py_SourceLocation loc, const char *format, ...);
77197

78198
// Export for '_opcode' extension module
79199
PyAPI_FUNC(PyObject*) _PyCompile_GetUnaryIntrinsicName(int index);

Include/internal/pycore_instruction_sequence.h

+5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ typedef struct {
5151
int id;
5252
} _PyJumpTargetLabel;
5353

54+
#define NO_LABEL ((const _PyJumpTargetLabel){-1})
55+
56+
#define SAME_JUMP_TARGET_LABEL(L1, L2) ((L1).id == (L2).id)
57+
#define IS_JUMP_TARGET_LABEL(L) (!SAME_JUMP_TARGET_LABEL((L), (NO_LABEL)))
58+
5459
PyAPI_FUNC(PyObject*)_PyInstructionSequence_New(void);
5560

5661
int _PyInstructionSequence_UseLabel(_PyInstructionSequence *seq, int lbl);

Makefile.pre.in

+3-2
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ PYTHON_OBJS= \
429429
Python/brc.o \
430430
Python/ceval.o \
431431
Python/codecs.o \
432+
Python/codegen.o \
432433
Python/compile.o \
433434
Python/context.o \
434435
Python/critical_section.o \
@@ -1873,7 +1874,7 @@ regen-sre:
18731874
$(srcdir)/Modules/_sre/sre_constants.h \
18741875
$(srcdir)/Modules/_sre/sre_targets.h
18751876

1876-
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
1877+
Python/compile.o Python/codegen.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
18771878

18781879
Python/getplatform.o: $(srcdir)/Python/getplatform.c
18791880
$(CC) -c $(PY_CORE_CFLAGS) -DPLATFORM='"$(MACHDEP)"' -o $@ $(srcdir)/Python/getplatform.c
@@ -2009,7 +2010,7 @@ regen-uop-metadata:
20092010
$(srcdir)/Include/internal/pycore_uop_metadata.h.new $(srcdir)/Python/bytecodes.c
20102011
$(UPDATE_FILE) $(srcdir)/Include/internal/pycore_uop_metadata.h $(srcdir)/Include/internal/pycore_uop_metadata.h.new
20112012

2012-
Python/compile.o Python/assemble.o Python/flowgraph.o Python/instruction_sequence.o: \
2013+
Python/compile.o Python/codegen.o Python/assemble.o Python/flowgraph.o Python/instruction_sequence.o: \
20132014
$(srcdir)/Include/internal/pycore_compile.h \
20142015
$(srcdir)/Include/internal/pycore_flowgraph.h \
20152016
$(srcdir)/Include/internal/pycore_instruction_sequence.h \

PCbuild/_freeze_module.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@
194194
<ClCompile Include="..\Python\bootstrap_hash.c" />
195195
<ClCompile Include="..\Python\ceval.c" />
196196
<ClCompile Include="..\Python\codecs.c" />
197+
<ClCompile Include="..\Python\codegen.c" />
197198
<ClCompile Include="..\Python\compile.c" />
198199
<ClCompile Include="..\Python\context.c" />
199200
<ClCompile Include="..\Python\critical_section.c" />

PCbuild/_freeze_module.vcxproj.filters

+3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@
9797
<ClCompile Include="..\Python\perf_jit_trampoline.c">
9898
<Filter>Source Files</Filter>
9999
</ClCompile>
100+
<ClCompile Include="..\Python\codegen.c">
101+
<Filter>Source Files</Filter>
102+
</ClCompile>
100103
<ClCompile Include="..\Python\compile.c">
101104
<Filter>Source Files</Filter>
102105
</ClCompile>

PCbuild/pythoncore.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@
581581
<ClCompile Include="..\Python\brc.c" />
582582
<ClCompile Include="..\Python\ceval.c" />
583583
<ClCompile Include="..\Python\codecs.c" />
584+
<ClCompile Include="..\Python\codegen.c" />
584585
<ClCompile Include="..\Python\compile.c" />
585586
<ClCompile Include="..\Python\context.c" />
586587
<ClCompile Include="..\Python\critical_section.c" />

PCbuild/pythoncore.vcxproj.filters

+3
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,9 @@
12981298
<ClCompile Include="..\Python\codecs.c">
12991299
<Filter>Python</Filter>
13001300
</ClCompile>
1301+
<ClCompile Include="..\Python\codegen.c">
1302+
<Filter>Python</Filter>
1303+
</ClCompile>
13011304
<ClCompile Include="..\Python\compile.c">
13021305
<Filter>Python</Filter>
13031306
</ClCompile>

0 commit comments

Comments
 (0)