Skip to content

Commit 4b7c488

Browse files
authored
gh-121404: split fblock handling into compiler_* and codegen_* parts (#123199)
1 parent b1d3bd2 commit 4b7c488

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

Python/compile.c

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,15 @@ compiler_pop_fblock(struct compiler *c, enum fblocktype t, jump_target_label blo
12621262
assert(SAME_LABEL(u->u_fblock[u->u_nfblocks].fb_block, block_label));
12631263
}
12641264

1265+
static struct fblockinfo *
1266+
compiler_top_fblock(struct compiler *c)
1267+
{
1268+
if (c->u->u_nfblocks == 0) {
1269+
return NULL;
1270+
}
1271+
return &c->u->u_fblock[c->u->u_nfblocks - 1];
1272+
}
1273+
12651274
static int
12661275
codegen_call_exit_with_nones(struct compiler *c, location loc)
12671276
{
@@ -1319,8 +1328,8 @@ codegen_pop_except_and_reraise(struct compiler *c, location loc)
13191328
* be popped.
13201329
*/
13211330
static int
1322-
compiler_unwind_fblock(struct compiler *c, location *ploc,
1323-
struct fblockinfo *info, int preserve_tos)
1331+
codegen_unwind_fblock(struct compiler *c, location *ploc,
1332+
struct fblockinfo *info, int preserve_tos)
13241333
{
13251334
switch (info->fb_type) {
13261335
case WHILE_LOOP:
@@ -1422,13 +1431,13 @@ compiler_unwind_fblock(struct compiler *c, location *ploc,
14221431

14231432
/** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
14241433
static int
1425-
compiler_unwind_fblock_stack(struct compiler *c, location *ploc,
1426-
int preserve_tos, struct fblockinfo **loop)
1434+
codegen_unwind_fblock_stack(struct compiler *c, location *ploc,
1435+
int preserve_tos, struct fblockinfo **loop)
14271436
{
1428-
if (c->u->u_nfblocks == 0) {
1437+
struct fblockinfo *top = compiler_top_fblock(c);
1438+
if (top == NULL) {
14291439
return SUCCESS;
14301440
}
1431-
struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
14321441
if (top->fb_type == EXCEPTION_GROUP_HANDLER) {
14331442
return compiler_error(
14341443
c, *ploc, "'break', 'continue' and 'return' cannot appear in an except* block");
@@ -1438,11 +1447,11 @@ compiler_unwind_fblock_stack(struct compiler *c, location *ploc,
14381447
return SUCCESS;
14391448
}
14401449
struct fblockinfo copy = *top;
1441-
c->u->u_nfblocks--;
1442-
RETURN_IF_ERROR(compiler_unwind_fblock(c, ploc, &copy, preserve_tos));
1443-
RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, ploc, preserve_tos, loop));
1444-
c->u->u_fblock[c->u->u_nfblocks] = copy;
1445-
c->u->u_nfblocks++;
1450+
compiler_pop_fblock(c, top->fb_type, top->fb_block);
1451+
RETURN_IF_ERROR(codegen_unwind_fblock(c, ploc, &copy, preserve_tos));
1452+
RETURN_IF_ERROR(codegen_unwind_fblock_stack(c, ploc, preserve_tos, loop));
1453+
compiler_push_fblock(c, copy.fb_loc, copy.fb_type, copy.fb_block,
1454+
copy.fb_exit, copy.fb_datum);
14461455
return SUCCESS;
14471456
}
14481457

@@ -3077,7 +3086,7 @@ codegen_return(struct compiler *c, stmt_ty s)
30773086
ADDOP(c, loc, NOP);
30783087
}
30793088

3080-
RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, preserve_tos, NULL));
3089+
RETURN_IF_ERROR(codegen_unwind_fblock_stack(c, &loc, preserve_tos, NULL));
30813090
if (s->v.Return.value == NULL) {
30823091
ADDOP_LOAD_CONST(c, loc, Py_None);
30833092
}
@@ -3096,11 +3105,11 @@ codegen_break(struct compiler *c, location loc)
30963105
location origin_loc = loc;
30973106
/* Emit instruction with line number */
30983107
ADDOP(c, loc, NOP);
3099-
RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, 0, &loop));
3108+
RETURN_IF_ERROR(codegen_unwind_fblock_stack(c, &loc, 0, &loop));
31003109
if (loop == NULL) {
31013110
return compiler_error(c, origin_loc, "'break' outside loop");
31023111
}
3103-
RETURN_IF_ERROR(compiler_unwind_fblock(c, &loc, loop, 0));
3112+
RETURN_IF_ERROR(codegen_unwind_fblock(c, &loc, loop, 0));
31043113
ADDOP_JUMP(c, loc, JUMP, loop->fb_exit);
31053114
return SUCCESS;
31063115
}
@@ -3112,7 +3121,7 @@ codegen_continue(struct compiler *c, location loc)
31123121
location origin_loc = loc;
31133122
/* Emit instruction with line number */
31143123
ADDOP(c, loc, NOP);
3115-
RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, 0, &loop));
3124+
RETURN_IF_ERROR(codegen_unwind_fblock_stack(c, &loc, 0, &loop));
31163125
if (loop == NULL) {
31173126
return compiler_error(c, origin_loc, "'continue' not properly in loop");
31183127
}

0 commit comments

Comments
 (0)