Skip to content

Commit

Permalink
gh-87092: move assembler related code from compile.c to assemble.c (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
iritkatriel authored Apr 11, 2023
1 parent 78b763f commit 33822d0
Show file tree
Hide file tree
Showing 10 changed files with 851 additions and 749 deletions.
39 changes: 39 additions & 0 deletions Include/internal/pycore_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,45 @@ extern int _PyAST_Optimize(
struct _arena *arena,
_PyASTOptimizeState *state);


typedef struct {
int i_opcode;
int i_oparg;
_PyCompilerSrcLocation i_loc;
} _PyCompilerInstruction;

typedef struct {
_PyCompilerInstruction *s_instrs;
int s_allocated;
int s_used;

int *s_labelmap; /* label id --> instr offset */
int s_labelmap_size;
int s_next_free_label; /* next free label id */
} _PyCompile_InstructionSequence;

typedef struct {
PyObject *u_name;
PyObject *u_qualname; /* dot-separated qualified name (lazy) */

/* The following fields are dicts that map objects to
the index of them in co_XXX. The index is used as
the argument for opcodes that refer to those collections.
*/
PyObject *u_consts; /* all constants */
PyObject *u_names; /* all names */
PyObject *u_varnames; /* local variables */
PyObject *u_cellvars; /* cell variables */
PyObject *u_freevars; /* free variables */

Py_ssize_t u_argcount; /* number of arguments for block */
Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */
Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */

int u_firstlineno; /* the first lineno of the block */
} _PyCompile_CodeUnitMetadata;


/* Utility for a number of growing arrays used in the compiler */
int _PyCompile_EnsureArrayLargeEnough(
int idx,
Expand Down
11 changes: 8 additions & 3 deletions Include/internal/pycore_flowgraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern "C" {
#endif

#include "pycore_opcode_utils.h"
#include "pycore_compile.h"

static const _PyCompilerSrcLocation NO_LOCATION = {-1, -1, -1, -1};

Expand All @@ -33,7 +34,8 @@ typedef struct {
typedef struct _PyCfgBasicblock_ {
/* Each basicblock in a compilation unit is linked via b_list in the
reverse order that the block are allocated. b_list points to the next
block, not to be confused with b_next, which is next by control flow. */
block in this list, not to be confused with b_next, which is next by
control flow. */
struct _PyCfgBasicblock_ *b_list;
/* The label of this block if it is a jump target, -1 otherwise */
_PyCfgJumpTargetLabel b_label;
Expand Down Expand Up @@ -91,10 +93,9 @@ void _PyCfgBuilder_Fini(_PyCfgBuilder *g);

_PyCfgInstruction* _PyCfg_BasicblockLastInstr(const _PyCfgBasicblock *b);
int _PyCfg_OptimizeCodeUnit(_PyCfgBuilder *g, PyObject *consts, PyObject *const_cache,
int code_flags, int nlocals, int nparams);
int code_flags, int nlocals, int nparams, int firstlineno);
int _PyCfg_Stackdepth(_PyCfgBasicblock *entryblock, int code_flags);
void _PyCfg_ConvertExceptionHandlersToNops(_PyCfgBasicblock *entryblock);
int _PyCfg_ResolveLineNumbers(_PyCfgBuilder *g, int firstlineno);
int _PyCfg_ResolveJumps(_PyCfgBuilder *g);
int _PyCfg_InstrSize(_PyCfgInstruction *instruction);

Expand All @@ -110,6 +111,10 @@ basicblock_nofallthrough(const _PyCfgBasicblock *b) {
#define BB_NO_FALLTHROUGH(B) (basicblock_nofallthrough(B))
#define BB_HAS_FALLTHROUGH(B) (!basicblock_nofallthrough(B))

PyCodeObject *
_PyAssemble_MakeCodeObject(_PyCompile_CodeUnitMetadata *u, PyObject *const_cache,
PyObject *consts, int maxdepth, _PyCfgBasicblock *entryblock,
int nlocalsplus, int code_flags, PyObject *filename);

#ifdef __cplusplus
}
Expand Down
3 changes: 2 additions & 1 deletion Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -369,17 +369,18 @@ PYTHON_OBJS= \
Python/Python-ast.o \
Python/Python-tokenize.o \
Python/asdl.o \
Python/assemble.o \
Python/ast.o \
Python/ast_opt.o \
Python/ast_unparse.o \
Python/bltinmodule.o \
Python/ceval.o \
Python/flowgraph.o \
Python/codecs.o \
Python/compile.o \
Python/context.o \
Python/dynamic_annotations.o \
Python/errors.o \
Python/flowgraph.o \
Python/frame.o \
Python/frozenmain.o \
Python/future.o \
Expand Down
3 changes: 2 additions & 1 deletion PCbuild/_freeze_module.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@
<ClCompile Include="..\PC\winreg.c" />
<ClCompile Include="..\Python\_warnings.c" />
<ClCompile Include="..\Python\asdl.c" />
<ClCompile Include="..\Python\assemble.c" />
<ClCompile Include="..\Python\ast.c" />
<ClCompile Include="..\Python\ast_opt.c" />
<ClCompile Include="..\Python\ast_unparse.c" />
<ClCompile Include="..\Python\bltinmodule.c" />
<ClCompile Include="..\Python\bootstrap_hash.c" />
<ClCompile Include="..\Python\ceval.c" />
<ClCompile Include="..\Python\flowgraph.c" />
<ClCompile Include="..\Python\codecs.c" />
<ClCompile Include="..\Python\compile.c" />
<ClCompile Include="..\Python\context.c" />
Expand All @@ -192,6 +192,7 @@
<ClCompile Include="..\Python\dynload_win.c" />
<ClCompile Include="..\Python\errors.c" />
<ClCompile Include="..\Python\fileutils.c" />
<ClCompile Include="..\Python\flowgraph.c" />
<ClCompile Include="..\Python\formatter_unicode.c" />
<ClCompile Include="..\Python\frame.c" />
<ClCompile Include="..\Python\future.c" />
Expand Down
9 changes: 6 additions & 3 deletions PCbuild/_freeze_module.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
<ClCompile Include="..\Python\asdl.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Python\assemble.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Python\ast.c">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down Expand Up @@ -76,9 +79,6 @@
<ClCompile Include="..\Python\ceval.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Python\flowgraph.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Objects\classobject.c">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down Expand Up @@ -142,6 +142,9 @@
<ClCompile Include="..\Objects\floatobject.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Python\flowgraph.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Python\formatter_unicode.c">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down
3 changes: 2 additions & 1 deletion PCbuild/pythoncore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -500,20 +500,21 @@
<ClCompile Include="..\Python\pyhash.c" />
<ClCompile Include="..\Python\_warnings.c" />
<ClCompile Include="..\Python\asdl.c" />
<ClCompile Include="..\Python\assemble.c" />
<ClCompile Include="..\Python\ast.c" />
<ClCompile Include="..\Python\ast_opt.c" />
<ClCompile Include="..\Python\ast_unparse.c" />
<ClCompile Include="..\Python\bltinmodule.c" />
<ClCompile Include="..\Python\bootstrap_hash.c" />
<ClCompile Include="..\Python\ceval.c" />
<ClCompile Include="..\Python\flowgraph.c" />
<ClCompile Include="..\Python\codecs.c" />
<ClCompile Include="..\Python\compile.c" />
<ClCompile Include="..\Python\context.c" />
<ClCompile Include="..\Python\dynamic_annotations.c" />
<ClCompile Include="..\Python\dynload_win.c" />
<ClCompile Include="..\Python\errors.c" />
<ClCompile Include="..\Python\fileutils.c" />
<ClCompile Include="..\Python\flowgraph.c" />
<ClCompile Include="..\Python\formatter_unicode.c" />
<ClCompile Include="..\Python\frame.c" />
<ClCompile Include="..\Python\frozen.c" />
Expand Down
9 changes: 6 additions & 3 deletions PCbuild/pythoncore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,9 @@
<ClCompile Include="..\Python\asdl.c">
<Filter>Python</Filter>
</ClCompile>
<ClCompile Include="..\Python\assemble.c">
<Filter>Python</Filter>
</ClCompile>
<ClCompile Include="..\Python\ast.c">
<Filter>Python</Filter>
</ClCompile>
Expand All @@ -1109,9 +1112,6 @@
<ClCompile Include="..\Python\ceval.c">
<Filter>Python</Filter>
</ClCompile>
<ClCompile Include="..\Python\flowgraph.c">
<Filter>Python</Filter>
</ClCompile>
<ClCompile Include="..\Python\codecs.c">
<Filter>Python</Filter>
</ClCompile>
Expand All @@ -1130,6 +1130,9 @@
<ClCompile Include="..\Python\fileutils.c">
<Filter>Python</Filter>
</ClCompile>
<ClCompile Include="..\Python\flowgraph.c">
<Filter>Python</Filter>
</ClCompile>
<ClCompile Include="..\Python\formatter_unicode.c">
<Filter>Python</Filter>
</ClCompile>
Expand Down
Loading

0 comments on commit 33822d0

Please sign in to comment.