Skip to content

Commit 0b8075e

Browse files
committed
make jump_target_label have a different type than basicblock*, so we can see it all works with the split between frontend and backend responsibilities
1 parent ac8b7b3 commit 0b8075e

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

Python/compile.c

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,22 @@ struct location {
139139

140140
static struct location NO_LOCATION = {-1, -1, -1, -1};
141141

142-
typedef struct basicblock_* jump_target_label;
142+
typedef struct jump_target_label_ {
143+
struct basicblock_ *block;
144+
} jump_target_label;
145+
146+
static struct jump_target_label_ NO_LABEL = {NULL};
147+
148+
#define SAME_LABEL(L1, L2) ((L1).block == (L2).block)
149+
#define IS_LABEL(L) (!SAME_LABEL((L), (NO_LABEL)))
143150

144151
#define NEW_JUMP_TARGET_LABEL(C, NAME) \
145-
jump_target_label NAME = compiler_new_block(C); \
146-
if (NAME == NULL) { \
152+
jump_target_label NAME = {compiler_new_block(C)}; \
153+
if (!IS_LABEL(NAME)) { \
147154
return 0; \
148155
}
149156

150-
#define USE_LABEL(C, LBL) compiler_use_next_block(C, LBL)
151-
152-
#define NO_LABEL NULL
157+
#define USE_LABEL(C, LBL) compiler_use_next_block(C, (LBL).block)
153158

154159
struct instr {
155160
int i_opcode;
@@ -1275,10 +1280,10 @@ basicblock_addop(basicblock *b, int opcode, int oparg,
12751280
assert(!IS_ASSEMBLER_OPCODE(opcode));
12761281
assert(HAS_ARG(opcode) || oparg == 0);
12771282
assert(0 <= oparg && oparg < (1 << 30));
1278-
assert((target == NULL) ||
1283+
assert(!IS_LABEL(target) ||
12791284
IS_JUMP_OPCODE(opcode) ||
12801285
IS_BLOCK_PUSH_OPCODE(opcode));
1281-
assert(oparg == 0 || target == NULL);
1286+
assert(oparg == 0 || !IS_LABEL(target));
12821287

12831288
int off = basicblock_next_instr(b);
12841289
if (off < 0) {
@@ -1529,9 +1534,9 @@ cfg_builder_addop_i(cfg_builder *g, int opcode, Py_ssize_t oparg, struct locatio
15291534
}
15301535

15311536
static int
1532-
cfg_builder_addop_j(cfg_builder *g, int opcode, basicblock *target, struct location loc)
1537+
cfg_builder_addop_j(cfg_builder *g, int opcode, jump_target_label target, struct location loc)
15331538
{
1534-
assert(target != NULL);
1539+
assert(IS_LABEL(target));
15351540
assert(IS_JUMP_OPCODE(opcode) || IS_BLOCK_PUSH_OPCODE(opcode));
15361541
return cfg_builder_addop(g, opcode, 0, target, loc);
15371542
}
@@ -1879,7 +1884,7 @@ find_ann(asdl_stmt_seq *stmts)
18791884
*/
18801885

18811886
static int
1882-
compiler_push_fblock(struct compiler *c, enum fblocktype t, jump_target_label b,
1887+
compiler_push_fblock(struct compiler *c, enum fblocktype t, jump_target_label block_label,
18831888
jump_target_label exit, void *datum)
18841889
{
18851890
struct fblockinfo *f;
@@ -1888,20 +1893,20 @@ compiler_push_fblock(struct compiler *c, enum fblocktype t, jump_target_label b,
18881893
}
18891894
f = &c->u->u_fblock[c->u->u_nfblocks++];
18901895
f->fb_type = t;
1891-
f->fb_block = b;
1896+
f->fb_block = block_label;
18921897
f->fb_exit = exit;
18931898
f->fb_datum = datum;
18941899
return 1;
18951900
}
18961901

18971902
static void
1898-
compiler_pop_fblock(struct compiler *c, enum fblocktype t, jump_target_label b)
1903+
compiler_pop_fblock(struct compiler *c, enum fblocktype t, jump_target_label block_label)
18991904
{
19001905
struct compiler_unit *u = c->u;
19011906
assert(u->u_nfblocks > 0);
19021907
u->u_nfblocks--;
19031908
assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1904-
assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1909+
assert(SAME_LABEL(u->u_fblock[u->u_nfblocks].fb_block, block_label));
19051910
}
19061911

19071912
static int
@@ -2855,7 +2860,7 @@ compiler_jump_if(struct compiler *c, expr_ty e, jump_target_label next, int cond
28552860
}
28562861
if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
28572862
return 0;
2858-
if (next2 != next) {
2863+
if (!SAME_LABEL(next2, next)) {
28592864
USE_LABEL(c, next2);
28602865
}
28612866
return 1;
@@ -5123,12 +5128,12 @@ compiler_sync_comprehension_generator(struct compiler *c,
51235128
start = NO_LABEL;
51245129
}
51255130
}
5126-
if (start != NO_LABEL) {
5131+
if (IS_LABEL(start)) {
51275132
VISIT(c, expr, gen->iter);
51285133
ADDOP(c, GET_ITER);
51295134
}
51305135
}
5131-
if (start != NO_LABEL) {
5136+
if (IS_LABEL(start)) {
51325137
depth++;
51335138
USE_LABEL(c, start);
51345139
ADDOP_JUMP(c, FOR_ITER, anchor);
@@ -5179,7 +5184,7 @@ compiler_sync_comprehension_generator(struct compiler *c,
51795184
}
51805185

51815186
USE_LABEL(c, if_cleanup);
5182-
if (start != NO_LABEL) {
5187+
if (IS_LABEL(start)) {
51835188
ADDOP_JUMP(c, JUMP, start);
51845189

51855190
USE_LABEL(c, anchor);
@@ -7369,16 +7374,17 @@ push_cold_blocks_to_end(cfg_builder *g, int code_flags) {
73697374
if (explicit_jump == NULL) {
73707375
return -1;
73717376
}
7372-
basicblock_addop(explicit_jump, JUMP, 0, b->b_next, NO_LOCATION);
7377+
jump_target_label next_label = {b->b_next};
7378+
basicblock_addop(explicit_jump, JUMP, 0, next_label, NO_LOCATION);
73737379
explicit_jump->b_cold = 1;
73747380
explicit_jump->b_next = b->b_next;
73757381
b->b_next = explicit_jump;
73767382

73777383
/* calculate target from target_label */
73787384
/* TODO: formalize an API for adding jumps in the backend */
73797385
struct instr *last = basicblock_last_instr(explicit_jump);
7380-
last->i_target = last->i_target_label;
7381-
last->i_target_label = NULL;
7386+
last->i_target = last->i_target_label.block;
7387+
last->i_target_label = NO_LABEL;
73827388
}
73837389
}
73847390

@@ -9398,8 +9404,8 @@ calculate_jump_targets(basicblock *entryblock)
93989404
for (int i = 0; i < b->b_iused; i++) {
93999405
struct instr *instr = &b->b_instr[i];
94009406
assert(instr->i_target == NULL);
9401-
instr->i_target = instr->i_target_label;
9402-
instr->i_target_label = NULL;
9407+
instr->i_target = instr->i_target_label.block;
9408+
instr->i_target_label = NO_LABEL;
94039409
if (is_jump(instr) || is_block_push(instr)) {
94049410
assert(instr->i_target != NULL);
94059411
}

0 commit comments

Comments
 (0)