@@ -291,7 +291,9 @@ typedef struct basicblock_ {
291
291
292
292
static struct instr *
293
293
basicblock_last_instr (const basicblock * b ) {
294
- if (b -> b_iused ) {
294
+ assert (b -> b_iused >= 0 );
295
+ if (b -> b_iused > 0 ) {
296
+ assert (b -> b_instr != NULL );
295
297
return & b -> b_instr [b -> b_iused - 1 ];
296
298
}
297
299
return NULL ;
@@ -7168,10 +7170,8 @@ assemble_free(struct assembler *a)
7168
7170
static int
7169
7171
blocksize (basicblock * b )
7170
7172
{
7171
- int i ;
7172
7173
int size = 0 ;
7173
-
7174
- for (i = 0 ; i < b -> b_iused ; i ++ ) {
7174
+ for (int i = 0 ; i < b -> b_iused ; i ++ ) {
7175
7175
size += instr_size (& b -> b_instr [i ]);
7176
7176
}
7177
7177
return size ;
@@ -7746,10 +7746,10 @@ normalize_jumps(basicblock *entryblock)
7746
7746
}
7747
7747
for (basicblock * b = entryblock ; b != NULL ; b = b -> b_next ) {
7748
7748
b -> b_visited = 1 ;
7749
- if (b -> b_iused == 0 ) {
7749
+ struct instr * last = basicblock_last_instr (b );
7750
+ if (last == NULL ) {
7750
7751
continue ;
7751
7752
}
7752
- struct instr * last = & b -> b_instr [b -> b_iused - 1 ];
7753
7753
assert (!IS_ASSEMBLER_OPCODE (last -> i_opcode ));
7754
7754
if (is_jump (last )) {
7755
7755
bool is_forward = last -> i_target -> b_visited == 0 ;
@@ -7915,8 +7915,8 @@ scan_block_for_local(int target, basicblock *b, bool unsafe_to_start,
7915
7915
if (b -> b_next && BB_HAS_FALLTHROUGH (b )) {
7916
7916
MAYBE_PUSH (b -> b_next );
7917
7917
}
7918
- if ( b -> b_iused > 0 ) {
7919
- struct instr * last = & b -> b_instr [ b -> b_iused - 1 ];
7918
+ struct instr * last = basicblock_last_instr ( b );
7919
+ if ( last != NULL ) {
7920
7920
if (is_jump (last )) {
7921
7921
assert (last -> i_target != NULL );
7922
7922
MAYBE_PUSH (last -> i_target );
@@ -8405,10 +8405,10 @@ guarantee_lineno_for_exits(basicblock *entryblock, int firstlineno) {
8405
8405
int lineno = firstlineno ;
8406
8406
assert (lineno > 0 );
8407
8407
for (basicblock * b = entryblock ; b != NULL ; b = b -> b_next ) {
8408
- if (b -> b_iused == 0 ) {
8408
+ struct instr * last = basicblock_last_instr (b );
8409
+ if (last == NULL ) {
8409
8410
continue ;
8410
8411
}
8411
- struct instr * last = & b -> b_instr [b -> b_iused - 1 ];
8412
8412
if (last -> i_loc .lineno < 0 ) {
8413
8413
if (last -> i_opcode == RETURN_VALUE ) {
8414
8414
for (int i = 0 ; i < b -> b_iused ; i ++ ) {
@@ -8486,18 +8486,18 @@ remove_redundant_jumps(cfg_builder *g) {
8486
8486
*/
8487
8487
int removed = 0 ;
8488
8488
for (basicblock * b = g -> g_entryblock ; b != NULL ; b = b -> b_next ) {
8489
- if ( b -> b_iused > 0 ) {
8490
- struct instr * b_last_instr = & b -> b_instr [ b -> b_iused - 1 ];
8491
- assert (!IS_ASSEMBLER_OPCODE (b_last_instr -> i_opcode ));
8492
- if (b_last_instr -> i_opcode == JUMP ||
8493
- b_last_instr -> i_opcode == JUMP_NO_INTERRUPT ) {
8494
- if (b_last_instr -> i_target == NULL ) {
8489
+ struct instr * last = basicblock_last_instr ( b );
8490
+ if ( last != NULL ) {
8491
+ assert (!IS_ASSEMBLER_OPCODE (last -> i_opcode ));
8492
+ if (last -> i_opcode == JUMP ||
8493
+ last -> i_opcode == JUMP_NO_INTERRUPT ) {
8494
+ if (last -> i_target == NULL ) {
8495
8495
PyErr_SetString (PyExc_SystemError , "jump with NULL target" );
8496
8496
return -1 ;
8497
8497
}
8498
- if (b_last_instr -> i_target == b -> b_next ) {
8498
+ if (last -> i_target == b -> b_next ) {
8499
8499
assert (b -> b_next -> b_iused );
8500
- b_last_instr -> i_opcode = NOP ;
8500
+ last -> i_opcode = NOP ;
8501
8501
removed ++ ;
8502
8502
}
8503
8503
}
@@ -9215,10 +9215,10 @@ basicblock_has_lineno(const basicblock *bb) {
9215
9215
*/
9216
9216
static int
9217
9217
extend_block (basicblock * bb ) {
9218
- if (bb -> b_iused == 0 ) {
9218
+ struct instr * last = basicblock_last_instr (bb );
9219
+ if (last == NULL ) {
9219
9220
return 0 ;
9220
9221
}
9221
- struct instr * last = & bb -> b_instr [bb -> b_iused - 1 ];
9222
9222
if (last -> i_opcode != JUMP &&
9223
9223
last -> i_opcode != JUMP_FORWARD &&
9224
9224
last -> i_opcode != JUMP_BACKWARD ) {
@@ -9399,7 +9399,8 @@ eliminate_empty_basic_blocks(cfg_builder *g) {
9399
9399
static void
9400
9400
propagate_line_numbers (basicblock * entryblock ) {
9401
9401
for (basicblock * b = entryblock ; b != NULL ; b = b -> b_next ) {
9402
- if (b -> b_iused == 0 ) {
9402
+ struct instr * last = basicblock_last_instr (b );
9403
+ if (last == NULL ) {
9403
9404
continue ;
9404
9405
}
9405
9406
@@ -9418,8 +9419,8 @@ propagate_line_numbers(basicblock *entryblock) {
9418
9419
b -> b_next -> b_instr [0 ].i_loc = prev_location ;
9419
9420
}
9420
9421
}
9421
- if (is_jump (& b -> b_instr [ b -> b_iused - 1 ] )) {
9422
- basicblock * target = b -> b_instr [ b -> b_iused - 1 ]. i_target ;
9422
+ if (is_jump (last )) {
9423
+ basicblock * target = last -> i_target ;
9423
9424
if (target -> b_predecessors == 1 ) {
9424
9425
if (target -> b_instr [0 ].i_loc .lineno < 0 ) {
9425
9426
target -> b_instr [0 ].i_loc = prev_location ;
@@ -9576,15 +9577,16 @@ duplicate_exits_without_lineno(cfg_builder *g)
9576
9577
*/
9577
9578
basicblock * entryblock = g -> g_entryblock ;
9578
9579
for (basicblock * b = entryblock ; b != NULL ; b = b -> b_next ) {
9579
- if (b -> b_iused > 0 && is_jump (& b -> b_instr [b -> b_iused - 1 ])) {
9580
- basicblock * target = b -> b_instr [b -> b_iused - 1 ].i_target ;
9580
+ struct instr * last = basicblock_last_instr (b );
9581
+ if (last != NULL && is_jump (last )) {
9582
+ basicblock * target = last -> i_target ;
9581
9583
if (is_exit_without_lineno (target ) && target -> b_predecessors > 1 ) {
9582
9584
basicblock * new_target = copy_basicblock (g , target );
9583
9585
if (new_target == NULL ) {
9584
9586
return -1 ;
9585
9587
}
9586
- new_target -> b_instr [0 ].i_loc = b -> b_instr [ b -> b_iused - 1 ]. i_loc ;
9587
- b -> b_instr [ b -> b_iused - 1 ]. i_target = new_target ;
9588
+ new_target -> b_instr [0 ].i_loc = last -> i_loc ;
9589
+ last -> i_target = new_target ;
9588
9590
target -> b_predecessors -- ;
9589
9591
new_target -> b_predecessors = 1 ;
9590
9592
new_target -> b_next = target -> b_next ;
@@ -9603,8 +9605,9 @@ duplicate_exits_without_lineno(cfg_builder *g)
9603
9605
for (basicblock * b = entryblock ; b != NULL ; b = b -> b_next ) {
9604
9606
if (BB_HAS_FALLTHROUGH (b ) && b -> b_next && b -> b_iused > 0 ) {
9605
9607
if (is_exit_without_lineno (b -> b_next )) {
9606
- assert (b -> b_next -> b_iused > 0 );
9607
- b -> b_next -> b_instr [0 ].i_loc = b -> b_instr [b -> b_iused - 1 ].i_loc ;
9608
+ struct instr * last = basicblock_last_instr (b );
9609
+ assert (last != NULL );
9610
+ b -> b_next -> b_instr [0 ].i_loc = last -> i_loc ;
9608
9611
}
9609
9612
}
9610
9613
}
0 commit comments