Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
43df9b3
compiler uses instruction stream to create codeobjects
iritkatriel Feb 25, 2023
2b43f88
remove unused code
iritkatriel Feb 26, 2023
52a1d1d
use newg all the way
iritkatriel Feb 26, 2023
9b737f1
remove double processing
iritkatriel Feb 26, 2023
083b8b1
split cfg_builder_use_label/cfg_builder_addop to separate versions fo…
iritkatriel Feb 26, 2023
a56bafe
codegen functions take an instr_stream
iritkatriel Feb 27, 2023
8be1b28
free isntr_stream. Attach it to compiluer_unit (not cfg_builder)
iritkatriel Feb 27, 2023
741f773
move things around
iritkatriel Feb 27, 2023
fb07525
labels generated by instr_stream instead of cfg_builder
iritkatriel Feb 27, 2023
1b06bf8
add implicit RETURN NONE to the stream
iritkatriel Feb 27, 2023
142a7f1
remove u_cfg_builder
iritkatriel Feb 27, 2023
f9f8443
init the cfg_builder in instr_stream_to_cfg
iritkatriel Feb 27, 2023
f6fcde9
stream->sequence
iritkatriel Feb 28, 2023
84f5a9d
instr->cfg_instr, codegen_instr->instr
iritkatriel Feb 28, 2023
cd0225d
make sure we always emit something to jump to at the end
iritkatriel Feb 28, 2023
b8f4acf
INSTR_STREAM --> INSTR_SEQUENCE
iritkatriel Feb 28, 2023
258f142
free the cfg_builder in the test harness
iritkatriel Mar 1, 2023
e5653ee
Merge branch 'main' into instruction-stream
iritkatriel Mar 1, 2023
788da74
Merge branch 'main' into instruction-stream
iritkatriel Mar 2, 2023
fd7f2b3
#ifndef NDEBUG around cfg_builder_check
iritkatriel Mar 2, 2023
57fbe36
create shared helper function for resizing arrays
iritkatriel Mar 3, 2023
dc9f1f5
remove obsolete comment
iritkatriel Mar 3, 2023
f20e480
Merge branch 'main' into instruction-stream
iritkatriel Mar 6, 2023
fa1c66a
address code review
iritkatriel Mar 7, 2023
3528176
tweak comments
iritkatriel Mar 7, 2023
fb13e36
index -> idx to avoid github's hilighting
iritkatriel Mar 7, 2023
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
Prev Previous commit
Next Next commit
codegen functions take an instr_stream
  • Loading branch information
iritkatriel committed Feb 28, 2023
commit a56bafed32cf18778bb03f6ec803a5499c7af08e
63 changes: 32 additions & 31 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ static struct jump_target_label_ NO_LABEL = {-1};
}

#define USE_LABEL(C, LBL) \
RETURN_IF_ERROR(codegen_use_label(CFG_BUILDER(C), LBL))
RETURN_IF_ERROR(codegen_use_label(INSTR_STREAM(C), LBL))

struct instr {
int i_opcode;
Expand Down Expand Up @@ -598,6 +598,7 @@ struct compiler {
};

#define CFG_BUILDER(C) (&((C)->u->u_cfg_builder))
#define INSTR_STREAM(C) (&((C)->u->u_cfg_builder.g_instr_stream))


typedef struct {
Expand Down Expand Up @@ -628,7 +629,7 @@ typedef struct {
static int basicblock_next_instr(basicblock *);

static basicblock *cfg_builder_new_block(cfg_builder *g);
static int codegen_addop_i(cfg_builder *g, int opcode, Py_ssize_t oparg, location loc);
static int codegen_addop_i(instr_stream *is, int opcode, Py_ssize_t oparg, location loc);

static void compiler_free(struct compiler *);
static int compiler_error(struct compiler *, location loc, const char *, ...);
Expand Down Expand Up @@ -1094,9 +1095,9 @@ cfg_builder_use_next_block(cfg_builder *g, basicblock *block)
}

static int
codegen_use_label(cfg_builder *g, jump_target_label lbl)
codegen_use_label(instr_stream *is, jump_target_label lbl)
{
RETURN_IF_ERROR(instr_stream_add_label(&g->g_instr_stream, lbl.id));
RETURN_IF_ERROR(instr_stream_add_label(is, lbl.id));
return SUCCESS;
}

Expand Down Expand Up @@ -1357,17 +1358,17 @@ instr_stream_insert_instruction(instr_stream *is, int pos,
}

static int
codegen_addop(cfg_builder *g, int opcode, int oparg, location loc)
codegen_addop(instr_stream *is, int opcode, int oparg, location loc)
{
RETURN_IF_ERROR(instr_stream_addop(&g->g_instr_stream, opcode, oparg, loc));
RETURN_IF_ERROR(instr_stream_addop(is, opcode, oparg, loc));
return SUCCESS;
}

static int
codegen_addop_noarg(cfg_builder *g, int opcode, location loc)
codegen_addop_noarg(instr_stream *is, int opcode, location loc)
{
assert(!HAS_ARG(opcode));
return codegen_addop(g, opcode, 0, loc);
return codegen_addop(is, opcode, 0, loc);
}

static Py_ssize_t
Expand Down Expand Up @@ -1525,7 +1526,7 @@ compiler_addop_load_const(struct compiler *c, location loc, PyObject *o)
if (arg < 0) {
return ERROR;
}
return codegen_addop_i(CFG_BUILDER(c), LOAD_CONST, arg, loc);
return codegen_addop_i(INSTR_STREAM(c), LOAD_CONST, arg, loc);
}

static int
Expand All @@ -1536,7 +1537,7 @@ compiler_addop_o(struct compiler *c, location loc,
if (arg < 0) {
return ERROR;
}
return codegen_addop_i(CFG_BUILDER(c), opcode, arg, loc);
return codegen_addop_i(INSTR_STREAM(c), opcode, arg, loc);
}

static int
Expand All @@ -1562,12 +1563,12 @@ compiler_addop_name(struct compiler *c, location loc,
arg <<= 1;
arg |= 1;
}
return codegen_addop_i(CFG_BUILDER(c), opcode, arg, loc);
return codegen_addop_i(INSTR_STREAM(c), opcode, arg, loc);
}

/* Add an opcode with an integer argument */
static int
codegen_addop_i(cfg_builder *g, int opcode, Py_ssize_t oparg, location loc)
codegen_addop_i(instr_stream *is, int opcode, Py_ssize_t oparg, location loc)
{
/* oparg value is unsigned, but a signed C int is usually used to store
it in the C code (like Python/ceval.c).
Expand All @@ -1578,23 +1579,23 @@ codegen_addop_i(cfg_builder *g, int opcode, Py_ssize_t oparg, location loc)
EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */

int oparg_ = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
return codegen_addop(g, opcode, oparg_, loc);
return codegen_addop(is, opcode, oparg_, loc);
}

static int
codegen_addop_j(cfg_builder *g, location loc,
codegen_addop_j(instr_stream *is, location loc,
int opcode, jump_target_label target)
{
assert(IS_LABEL(target));
assert(IS_JUMP_OPCODE(opcode) || IS_BLOCK_PUSH_OPCODE(opcode));
return codegen_addop(g, opcode, target.id, loc);
return codegen_addop(is, opcode, target.id, loc);
}

#define ADDOP(C, LOC, OP) \
RETURN_IF_ERROR(codegen_addop_noarg(CFG_BUILDER(C), (OP), (LOC)))
RETURN_IF_ERROR(codegen_addop_noarg(INSTR_STREAM(C), (OP), (LOC)))

#define ADDOP_IN_SCOPE(C, LOC, OP) { \
if (codegen_addop_noarg(CFG_BUILDER(C), (OP), (LOC)) < 0) { \
if (codegen_addop_noarg(INSTR_STREAM(C), (OP), (LOC)) < 0) { \
compiler_exit_scope(C); \
return ERROR; \
} \
Expand Down Expand Up @@ -1629,10 +1630,10 @@ codegen_addop_j(cfg_builder *g, location loc,
RETURN_IF_ERROR(compiler_addop_name((C), (LOC), (OP), (C)->u->u_ ## TYPE, (O)))

#define ADDOP_I(C, LOC, OP, O) \
RETURN_IF_ERROR(codegen_addop_i(CFG_BUILDER(C), (OP), (O), (LOC)))
RETURN_IF_ERROR(codegen_addop_i(INSTR_STREAM(C), (OP), (O), (LOC)))

#define ADDOP_JUMP(C, LOC, OP, O) \
RETURN_IF_ERROR(codegen_addop_j(CFG_BUILDER(C), (LOC), (OP), (O)))
RETURN_IF_ERROR(codegen_addop_j(INSTR_STREAM(C), (LOC), (OP), (O)))

#define ADDOP_COMPARE(C, LOC, CMP) \
RETURN_IF_ERROR(compiler_addcompare((C), (LOC), (cmpop_ty)(CMP)))
Expand Down Expand Up @@ -2570,7 +2571,7 @@ wrap_in_stopiteration_handler(struct compiler *c)

RETURN_IF_ERROR(
instr_stream_insert_instruction(
&c->u->u_cfg_builder.g_instr_stream, 0,
INSTR_STREAM(c), 0,
SETUP_CLEANUP, handler.id, NO_LOCATION));

ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
Expand Down Expand Up @@ -4260,7 +4261,7 @@ compiler_nameop(struct compiler *c, location loc,
if (op == LOAD_GLOBAL) {
arg <<= 1;
}
return codegen_addop_i(CFG_BUILDER(c), op, arg, loc);
return codegen_addop_i(INSTR_STREAM(c), op, arg, loc);
}

static int
Expand Down Expand Up @@ -6279,7 +6280,7 @@ emit_and_reset_fail_pop(struct compiler *c, location loc,
}
while (--pc->fail_pop_size) {
USE_LABEL(c, pc->fail_pop[pc->fail_pop_size]);
if (codegen_addop_noarg(CFG_BUILDER(c), POP_TOP, loc) < 0) {
if (codegen_addop_noarg(INSTR_STREAM(c), POP_TOP, loc) < 0) {
pc->fail_pop_size = 0;
PyObject_Free(pc->fail_pop);
pc->fail_pop = NULL;
Expand Down Expand Up @@ -6719,7 +6720,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
pc->fail_pop = NULL;
pc->fail_pop_size = 0;
pc->on_top = 0;
if (codegen_addop_i(CFG_BUILDER(c), COPY, 1, LOC(alt)) < 0 ||
if (codegen_addop_i(INSTR_STREAM(c), COPY, 1, LOC(alt)) < 0 ||
compiler_pattern(c, alt, pc) < 0) {
goto error;
}
Expand Down Expand Up @@ -6782,7 +6783,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
}
}
assert(control);
if (codegen_addop_j(CFG_BUILDER(c), LOC(alt), JUMP, end) < 0 ||
if (codegen_addop_j(INSTR_STREAM(c), LOC(alt), JUMP, end) < 0 ||
emit_and_reset_fail_pop(c, LOC(alt), pc) < 0)
{
goto error;
Expand All @@ -6794,7 +6795,7 @@ compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
// Need to NULL this for the PyObject_Free call in the error block.
old_pc.fail_pop = NULL;
// No match. Pop the remaining copy of the subject and fail:
if (codegen_addop_noarg(CFG_BUILDER(c), POP_TOP, LOC(p)) < 0 ||
if (codegen_addop_noarg(INSTR_STREAM(c), POP_TOP, LOC(p)) < 0 ||
jump_to_fail_pop(c, LOC(p), pc, JUMP) < 0) {
goto error;
}
Expand Down Expand Up @@ -8455,13 +8456,13 @@ insert_prefix_instructions(struct compiler *c, basicblock *entryblock,

RETURN_IF_ERROR(
instr_stream_insert_instruction(
&c->u->u_cfg_builder.g_instr_stream, 0,
INSTR_STREAM(c), 0,
RETURN_GENERATOR, 0,
LOCATION(c->u->u_firstlineno, c->u->u_firstlineno, -1, -1)));

RETURN_IF_ERROR(
instr_stream_insert_instruction(
&c->u->u_cfg_builder.g_instr_stream, 1,
INSTR_STREAM(c), 1,
POP_TOP, 0, NO_LOCATION));
}

Expand Down Expand Up @@ -8495,7 +8496,7 @@ insert_prefix_instructions(struct compiler *c, basicblock *entryblock,
RETURN_IF_ERROR(insert_instruction(entryblock, ncellsused, &make_cell));
RETURN_IF_ERROR(
instr_stream_insert_instruction(
&c->u->u_cfg_builder.g_instr_stream, ncellsused,
INSTR_STREAM(c), ncellsused,
MAKE_CELL, oldindex, NO_LOCATION));

ncellsused += 1;
Expand All @@ -8513,7 +8514,7 @@ insert_prefix_instructions(struct compiler *c, basicblock *entryblock,
RETURN_IF_ERROR(insert_instruction(entryblock, 0, &copy_frees));
RETURN_IF_ERROR(
instr_stream_insert_instruction(
&c->u->u_cfg_builder.g_instr_stream, 0,
INSTR_STREAM(c), 0,
COPY_FREE_VARS, nfreevars, NO_LOCATION));
}

Expand Down Expand Up @@ -8759,7 +8760,7 @@ assemble(struct compiler *c, int addNone)
if (cfg_builder_init(&newg) < 0) {
goto error;
}
if (instr_stream_to_cfg(&CFG_BUILDER(c)->g_instr_stream, &newg) < 0) {
if (instr_stream_to_cfg(INSTR_STREAM(c), &newg) < 0) {
goto error;
}

Expand Down Expand Up @@ -10046,7 +10047,7 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
if (cfg_builder_init(&g) < 0) {
goto finally;
}
if (instr_stream_to_cfg(&CFG_BUILDER(c)->g_instr_stream, &g) < 0) {
if (instr_stream_to_cfg(INSTR_STREAM(c), &g) < 0) {
goto finally;
}
if (translate_jump_labels_to_targets(g.g_entryblock) < 0) {
Expand Down