Skip to content

gh-121404: split fblock handling into compiler_* and codegen_* parts #123199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Changes from all 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
39 changes: 24 additions & 15 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,15 @@ compiler_pop_fblock(struct compiler *c, enum fblocktype t, jump_target_label blo
assert(SAME_LABEL(u->u_fblock[u->u_nfblocks].fb_block, block_label));
}

static struct fblockinfo *
compiler_top_fblock(struct compiler *c)
{
if (c->u->u_nfblocks == 0) {
return NULL;
}
return &c->u->u_fblock[c->u->u_nfblocks - 1];
}

static int
codegen_call_exit_with_nones(struct compiler *c, location loc)
{
Expand Down Expand Up @@ -1319,8 +1328,8 @@ codegen_pop_except_and_reraise(struct compiler *c, location loc)
* be popped.
*/
static int
compiler_unwind_fblock(struct compiler *c, location *ploc,
struct fblockinfo *info, int preserve_tos)
codegen_unwind_fblock(struct compiler *c, location *ploc,
struct fblockinfo *info, int preserve_tos)
{
switch (info->fb_type) {
case WHILE_LOOP:
Expand Down Expand Up @@ -1422,13 +1431,13 @@ compiler_unwind_fblock(struct compiler *c, location *ploc,

/** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
static int
compiler_unwind_fblock_stack(struct compiler *c, location *ploc,
int preserve_tos, struct fblockinfo **loop)
codegen_unwind_fblock_stack(struct compiler *c, location *ploc,
int preserve_tos, struct fblockinfo **loop)
{
if (c->u->u_nfblocks == 0) {
struct fblockinfo *top = compiler_top_fblock(c);
if (top == NULL) {
return SUCCESS;
}
struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
if (top->fb_type == EXCEPTION_GROUP_HANDLER) {
return compiler_error(
c, *ploc, "'break', 'continue' and 'return' cannot appear in an except* block");
Expand All @@ -1438,11 +1447,11 @@ compiler_unwind_fblock_stack(struct compiler *c, location *ploc,
return SUCCESS;
}
struct fblockinfo copy = *top;
c->u->u_nfblocks--;
RETURN_IF_ERROR(compiler_unwind_fblock(c, ploc, &copy, preserve_tos));
RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, ploc, preserve_tos, loop));
c->u->u_fblock[c->u->u_nfblocks] = copy;
c->u->u_nfblocks++;
compiler_pop_fblock(c, top->fb_type, top->fb_block);
RETURN_IF_ERROR(codegen_unwind_fblock(c, ploc, &copy, preserve_tos));
RETURN_IF_ERROR(codegen_unwind_fblock_stack(c, ploc, preserve_tos, loop));
compiler_push_fblock(c, copy.fb_loc, copy.fb_type, copy.fb_block,
copy.fb_exit, copy.fb_datum);
return SUCCESS;
}

Expand Down Expand Up @@ -3077,7 +3086,7 @@ codegen_return(struct compiler *c, stmt_ty s)
ADDOP(c, loc, NOP);
}

RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, preserve_tos, NULL));
RETURN_IF_ERROR(codegen_unwind_fblock_stack(c, &loc, preserve_tos, NULL));
if (s->v.Return.value == NULL) {
ADDOP_LOAD_CONST(c, loc, Py_None);
}
Expand All @@ -3096,11 +3105,11 @@ codegen_break(struct compiler *c, location loc)
location origin_loc = loc;
/* Emit instruction with line number */
ADDOP(c, loc, NOP);
RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, 0, &loop));
RETURN_IF_ERROR(codegen_unwind_fblock_stack(c, &loc, 0, &loop));
if (loop == NULL) {
return compiler_error(c, origin_loc, "'break' outside loop");
}
RETURN_IF_ERROR(compiler_unwind_fblock(c, &loc, loop, 0));
RETURN_IF_ERROR(codegen_unwind_fblock(c, &loc, loop, 0));
ADDOP_JUMP(c, loc, JUMP, loop->fb_exit);
return SUCCESS;
}
Expand All @@ -3112,7 +3121,7 @@ codegen_continue(struct compiler *c, location loc)
location origin_loc = loc;
/* Emit instruction with line number */
ADDOP(c, loc, NOP);
RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, 0, &loop));
RETURN_IF_ERROR(codegen_unwind_fblock_stack(c, &loc, 0, &loop));
if (loop == NULL) {
return compiler_error(c, origin_loc, "'continue' not properly in loop");
}
Expand Down
Loading