Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
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
115 changes: 115 additions & 0 deletions Include/internal/pycore_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif

#include "pycore_ast.h" // mod_ty
#include "pycore_symtable.h" // _Py_SourceLocation
#include "pycore_instruction_sequence.h"

Expand Down Expand Up @@ -63,6 +64,115 @@ typedef struct {
int u_firstlineno; /* the first lineno of the block */
} _PyCompile_CodeUnitMetadata;

struct _PyCompiler;

typedef enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } _PyCompiler_optype;

/* _PyCompile_FBlockInfo tracks the current frame block.
*
* A frame block is used to handle loops, try/except, and try/finally.
* It's called a frame block to distinguish it from a basic block in the
* compiler IR.
*/

enum _PyCompile_FBlockType {
COMPILER_FBLOCK_WHILE_LOOP,
COMPILER_FBLOCK_FOR_LOOP,
COMPILER_FBLOCK_TRY_EXCEPT,
COMPILER_FBLOCK_FINALLY_TRY,
COMPILER_FBLOCK_FINALLY_END,
COMPILER_FBLOCK_WITH,
COMPILER_FBLOCK_ASYNC_WITH,
COMPILER_FBLOCK_HANDLER_CLEANUP,
COMPILER_FBLOCK_POP_VALUE,
COMPILER_FBLOCK_EXCEPTION_HANDLER,
COMPILER_FBLOCK_EXCEPTION_GROUP_HANDLER,
COMPILER_FBLOCK_ASYNC_COMPREHENSION_GENERATOR,
COMPILER_FBLOCK_STOP_ITERATION,
};

typedef struct {
enum _PyCompile_FBlockType fb_type;
_PyJumpTargetLabel fb_block;
_Py_SourceLocation fb_loc;
/* (optional) type-specific exit or cleanup block */
_PyJumpTargetLabel fb_exit;
/* (optional) additional information required for unwinding */
void *fb_datum;
} _PyCompile_FBlockInfo;


int _PyCompiler_PushFBlock(struct _PyCompiler *c, _Py_SourceLocation loc,
enum _PyCompile_FBlockType t,
_PyJumpTargetLabel block_label,
_PyJumpTargetLabel exit, void *datum);
void _PyCompiler_PopFBlock(struct _PyCompiler *c, enum _PyCompile_FBlockType t,
_PyJumpTargetLabel block_label);
_PyCompile_FBlockInfo *_PyCompiler_TopFBlock(struct _PyCompiler *c);

int _PyCompiler_EnterScope(struct _PyCompiler *c, identifier name, int scope_type,
void *key, int lineno, PyObject *private,
_PyCompile_CodeUnitMetadata *umd);
void _PyCompiler_ExitScope(struct _PyCompiler *c);
Py_ssize_t _PyCompiler_AddConst(struct _PyCompiler *c, PyObject *o);
_PyInstructionSequence *_PyCompiler_InstrSequence(struct _PyCompiler *c);
int _PyCompiler_FutureFeatures(struct _PyCompiler *c);
PyObject *_PyCompiler_DeferredAnnotations(struct _PyCompiler *c);
PyObject *_PyCompiler_Mangle(struct _PyCompiler *c, PyObject *name);
PyObject *_PyCompiler_MaybeMangle(struct _PyCompiler *c, PyObject *name);
int _PyCompiler_MaybeAddStaticAttributeToClass(struct _PyCompiler *c, expr_ty e);
int _PyCompiler_GetRefType(struct _PyCompiler *c, PyObject *name);
int _PyCompiler_LookupCellvar(struct _PyCompiler *c, PyObject *name);
int _PyCompiler_ResolveNameop(struct _PyCompiler *c, PyObject *mangled, int scope,
_PyCompiler_optype *optype, Py_ssize_t *arg);

int _PyCompiler_IsInteractive(struct _PyCompiler *c);
int _PyCompiler_IsNestedScope(struct _PyCompiler *c);
int _PyCompiler_IsInInlinedComp(struct _PyCompiler *c);
int _PyCompiler_ScopeType(struct _PyCompiler *c);
int _PyCompiler_OptimizationLevel(struct _PyCompiler *c);
PyArena *_PyCompiler_Arena(struct _PyCompiler *c);
int _PyCompiler_LookupArg(struct _PyCompiler *c, PyCodeObject *co, PyObject *name);
PyObject *_PyCompiler_Qualname(struct _PyCompiler *c);
_PyCompile_CodeUnitMetadata *_PyCompiler_Metadata(struct _PyCompiler *c);
PyObject *_PyCompiler_StaticAttributesAsTuple(struct _PyCompiler *c);

#ifndef NDEBUG
int _PyCompiler_IsTopLevelAwait(struct _PyCompiler *c);
#endif

struct symtable *_PyCompiler_Symtable(struct _PyCompiler *c);
PySTEntryObject *_PyCompiler_SymtableEntry(struct _PyCompiler *c);

enum {
_PY_COMPILER_SCOPE_MODULE,
_PY_COMPILER_SCOPE_CLASS,
_PY_COMPILER_SCOPE_FUNCTION,
_PY_COMPILER_SCOPE_ASYNC_FUNCTION,
_PY_COMPILER_SCOPE_LAMBDA,
_PY_COMPILER_SCOPE_COMPREHENSION,
_PY_COMPILER_SCOPE_ANNOTATIONS,
};


typedef struct {
PyObject *pushed_locals;
PyObject *temp_symbols;
PyObject *fast_hidden;
_PyJumpTargetLabel cleanup;
} _PyCompile_InlinedComprehensionState;

int _PyCompiler_TweakInlinedComprehensionScopes(struct _PyCompiler *c, _Py_SourceLocation loc,
PySTEntryObject *entry,
_PyCompile_InlinedComprehensionState *state);
int _PyCompiler_RevertInlinedComprehensionScopes(struct _PyCompiler *c, _Py_SourceLocation loc,
_PyCompile_InlinedComprehensionState *state);
int _PyCompiler_AddDeferredAnnotaion(struct _PyCompiler *c, stmt_ty s);

int _PyCodegen_AddReturnAtEnd(struct _PyCompiler *c, int addNone);
int _PyCodegen_EnterAnonymousScope(struct _PyCompiler* c, mod_ty mod);
int _PyCodegen_Expression(struct _PyCompiler *c, expr_ty e);
int _PyCodegen_Body(struct _PyCompiler *c, _Py_SourceLocation loc, asdl_stmt_seq *stmts);

/* Utility for a number of growing arrays used in the compiler */
int _PyCompile_EnsureArrayLargeEnough(
Expand All @@ -74,6 +184,11 @@ int _PyCompile_EnsureArrayLargeEnough(

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

PyCodeObject *_PyCompile_OptimizeAndAssemble(struct _PyCompiler *c, int addNone);

Py_ssize_t _PyCompile_DictAddObj(PyObject *dict, PyObject *o);
int _PyCompiler_Error(struct _PyCompiler *c, _Py_SourceLocation loc, const char *format, ...);
int _PyCompiler_Warn(struct _PyCompiler *c, _Py_SourceLocation loc, const char *format, ...);

// Export for '_opcode' extension module
PyAPI_FUNC(PyObject*) _PyCompile_GetUnaryIntrinsicName(int index);
Expand Down
5 changes: 5 additions & 0 deletions Include/internal/pycore_instruction_sequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ typedef struct {
int id;
} _PyJumpTargetLabel;

#define NO_LABEL ((const _PyJumpTargetLabel){-1})

#define SAME_JUMP_TARGET_LABEL(L1, L2) ((L1).id == (L2).id)
#define IS_JUMP_TARGET_LABEL(L) (!SAME_JUMP_TARGET_LABEL((L), (NO_LABEL)))

PyAPI_FUNC(PyObject*)_PyInstructionSequence_New(void);

int _PyInstructionSequence_UseLabel(_PyInstructionSequence *seq, int lbl);
Expand Down
5 changes: 3 additions & 2 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ PYTHON_OBJS= \
Python/brc.o \
Python/ceval.o \
Python/codecs.o \
Python/codegen.o \
Python/compile.o \
Python/context.o \
Python/critical_section.o \
Expand Down Expand Up @@ -1873,7 +1874,7 @@ regen-sre:
$(srcdir)/Modules/_sre/sre_constants.h \
$(srcdir)/Modules/_sre/sre_targets.h

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
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

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

Python/compile.o Python/assemble.o Python/flowgraph.o Python/instruction_sequence.o: \
Python/compile.o Python/codegen.o Python/assemble.o Python/flowgraph.o Python/instruction_sequence.o: \
$(srcdir)/Include/internal/pycore_compile.h \
$(srcdir)/Include/internal/pycore_flowgraph.h \
$(srcdir)/Include/internal/pycore_instruction_sequence.h \
Expand Down
1 change: 1 addition & 0 deletions PCbuild/_freeze_module.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
<ClCompile Include="..\Python\bootstrap_hash.c" />
<ClCompile Include="..\Python\ceval.c" />
<ClCompile Include="..\Python\codecs.c" />
<ClCompile Include="..\Python\codegen.c" />
<ClCompile Include="..\Python\compile.c" />
<ClCompile Include="..\Python\context.c" />
<ClCompile Include="..\Python\critical_section.c" />
Expand Down
3 changes: 3 additions & 0 deletions PCbuild/_freeze_module.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@
<ClCompile Include="..\Python\perf_jit_trampoline.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Python\codegen.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Python\compile.c">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down
1 change: 1 addition & 0 deletions PCbuild/pythoncore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@
<ClCompile Include="..\Python\brc.c" />
<ClCompile Include="..\Python\ceval.c" />
<ClCompile Include="..\Python\codecs.c" />
<ClCompile Include="..\Python\codegen.c" />
<ClCompile Include="..\Python\compile.c" />
<ClCompile Include="..\Python\context.c" />
<ClCompile Include="..\Python\critical_section.c" />
Expand Down
3 changes: 3 additions & 0 deletions PCbuild/pythoncore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,9 @@
<ClCompile Include="..\Python\codecs.c">
<Filter>Python</Filter>
</ClCompile>
<ClCompile Include="..\Python\codegen.c">
<Filter>Python</Filter>
</ClCompile>
<ClCompile Include="..\Python\compile.c">
<Filter>Python</Filter>
</ClCompile>
Expand Down
Loading
Loading