@@ -967,6 +967,19 @@ compiler_next_instr(basicblock *b)
967
967
(c)->u->u_end_lineno = (x)->end_lineno; \
968
968
(c)->u->u_end_col_offset = (x)->end_col_offset;
969
969
970
+ // Artificial instructions
971
+ #define UNSET_LOC (c ) \
972
+ (c)->u->u_lineno = -1; \
973
+ (c)->u->u_col_offset = -1; \
974
+ (c)->u->u_end_lineno = -1; \
975
+ (c)->u->u_end_col_offset = -1;
976
+
977
+ #define COPY_INSTR_LOC (old , new ) \
978
+ (new).i_lineno = (old).i_lineno; \
979
+ (new).i_col_offset = (old).i_col_offset; \
980
+ (new).i_end_lineno = (old).i_end_lineno; \
981
+ (new).i_end_col_offset = (old).i_end_col_offset;
982
+
970
983
/* Return the stack effect of opcode with argument oparg.
971
984
972
985
Some opcodes have different stack effect when jump to the target and
@@ -1858,8 +1871,7 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1858
1871
/* The finally block should appear to execute after the
1859
1872
* statement causing the unwinding, so make the unwinding
1860
1873
* instruction artificial */
1861
- c -> u -> u_lineno = -1 ;
1862
- c -> u -> u_end_lineno = -1 ;
1874
+ UNSET_LOC (c );
1863
1875
return 1 ;
1864
1876
1865
1877
case FINALLY_END :
@@ -1895,7 +1907,7 @@ compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1895
1907
/* The exit block should appear to execute after the
1896
1908
* statement causing the unwinding, so make the unwinding
1897
1909
* instruction artificial */
1898
- c -> u -> u_lineno = -1 ;
1910
+ UNSET_LOC ( c ) ;
1899
1911
return 1 ;
1900
1912
1901
1913
case HANDLER_CLEANUP :
@@ -2547,7 +2559,7 @@ compiler_class(struct compiler *c, stmt_ty s)
2547
2559
return 0 ;
2548
2560
}
2549
2561
/* The following code is artificial */
2550
- c -> u -> u_lineno = -1 ;
2562
+ UNSET_LOC ( c ) ;
2551
2563
/* Return __classcell__ if it is referenced, otherwise return None */
2552
2564
if (c -> u -> u_ste -> ste_needs_class_closure ) {
2553
2565
/* Store __classcell__ into class namespace & return it */
@@ -2938,7 +2950,7 @@ compiler_for(struct compiler *c, stmt_ty s)
2938
2950
VISIT (c , expr , s -> v .For .target );
2939
2951
VISIT_SEQ (c , stmt , s -> v .For .body );
2940
2952
/* Mark jump as artificial */
2941
- c -> u -> u_lineno = -1 ;
2953
+ UNSET_LOC ( c ) ;
2942
2954
ADDOP_JUMP (c , JUMP_ABSOLUTE , start );
2943
2955
compiler_use_next_block (c , cleanup );
2944
2956
@@ -2991,7 +3003,7 @@ compiler_async_for(struct compiler *c, stmt_ty s)
2991
3003
/* Except block for __anext__ */
2992
3004
compiler_use_next_block (c , except );
2993
3005
2994
- c -> u -> u_lineno = -1 ;
3006
+ UNSET_LOC ( c ) ;
2995
3007
ADDOP (c , END_ASYNC_FOR );
2996
3008
2997
3009
/* `else` block */
@@ -3178,7 +3190,7 @@ compiler_try_finally(struct compiler *c, stmt_ty s)
3178
3190
/* `finally` block */
3179
3191
compiler_use_next_block (c , end );
3180
3192
3181
- c -> u -> u_lineno = -1 ;
3193
+ UNSET_LOC ( c ) ;
3182
3194
ADDOP_JUMP (c , SETUP_CLEANUP , cleanup );
3183
3195
ADDOP (c , PUSH_EXC_INFO );
3184
3196
if (!compiler_push_fblock (c , FINALLY_END , end , NULL , NULL ))
@@ -3246,7 +3258,7 @@ compiler_try_except(struct compiler *c, stmt_ty s)
3246
3258
n = asdl_seq_LEN (s -> v .Try .handlers );
3247
3259
compiler_use_next_block (c , except );
3248
3260
3249
- c -> u -> u_lineno = -1 ;
3261
+ UNSET_LOC ( c ) ;
3250
3262
ADDOP_JUMP (c , SETUP_CLEANUP , cleanup );
3251
3263
ADDOP (c , PUSH_EXC_INFO );
3252
3264
/* Runtime will push a block here, so we need to account for that */
@@ -3305,7 +3317,7 @@ compiler_try_except(struct compiler *c, stmt_ty s)
3305
3317
ADDOP (c , POP_BLOCK );
3306
3318
ADDOP (c , POP_EXCEPT );
3307
3319
/* name = None; del name; # Mark as artificial */
3308
- c -> u -> u_lineno = -1 ;
3320
+ UNSET_LOC ( c ) ;
3309
3321
ADDOP_LOAD_CONST (c , Py_None );
3310
3322
compiler_nameop (c , handler -> v .ExceptHandler .name , Store );
3311
3323
compiler_nameop (c , handler -> v .ExceptHandler .name , Del );
@@ -3315,7 +3327,7 @@ compiler_try_except(struct compiler *c, stmt_ty s)
3315
3327
compiler_use_next_block (c , cleanup_end );
3316
3328
3317
3329
/* name = None; del name; # Mark as artificial */
3318
- c -> u -> u_lineno = -1 ;
3330
+ UNSET_LOC ( c ) ;
3319
3331
3320
3332
ADDOP_LOAD_CONST (c , Py_None );
3321
3333
compiler_nameop (c , handler -> v .ExceptHandler .name , Store );
@@ -3338,15 +3350,15 @@ compiler_try_except(struct compiler *c, stmt_ty s)
3338
3350
VISIT_SEQ (c , stmt , handler -> v .ExceptHandler .body );
3339
3351
compiler_pop_fblock (c , HANDLER_CLEANUP , cleanup_body );
3340
3352
/* name = None; del name; # Mark as artificial */
3341
- c -> u -> u_lineno = -1 ;
3353
+ UNSET_LOC ( c ) ;
3342
3354
ADDOP (c , POP_BLOCK );
3343
3355
ADDOP (c , POP_EXCEPT );
3344
3356
ADDOP_JUMP (c , JUMP_FORWARD , end );
3345
3357
}
3346
3358
compiler_use_next_block (c , except );
3347
3359
}
3348
3360
/* Mark as artificial */
3349
- c -> u -> u_lineno = -1 ;
3361
+ UNSET_LOC ( c ) ;
3350
3362
compiler_pop_fblock (c , EXCEPTION_HANDLER , NULL );
3351
3363
ADDOP_I (c , RERAISE , 0 );
3352
3364
compiler_use_next_block (c , cleanup );
@@ -3567,7 +3579,7 @@ compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3567
3579
3568
3580
VISIT (c , expr , value );
3569
3581
/* Mark POP_TOP as artificial */
3570
- c -> u -> u_lineno = -1 ;
3582
+ UNSET_LOC ( c ) ;
3571
3583
ADDOP (c , POP_TOP );
3572
3584
return 1 ;
3573
3585
}
@@ -4921,7 +4933,7 @@ compiler_async_comprehension_generator(struct compiler *c,
4921
4933
compiler_pop_fblock (c , ASYNC_COMPREHENSION_GENERATOR , start );
4922
4934
4923
4935
compiler_use_next_block (c , except );
4924
- //c->u->u_lineno = -1 ;
4936
+ //UNSET_LOC(c) ;
4925
4937
4926
4938
ADDOP (c , END_ASYNC_FOR );
4927
4939
@@ -5101,7 +5113,7 @@ compiler_visit_keyword(struct compiler *c, keyword_ty k)
5101
5113
5102
5114
static int
5103
5115
compiler_with_except_finish (struct compiler * c , basicblock * cleanup ) {
5104
- c -> u -> u_lineno = -1 ;
5116
+ UNSET_LOC ( c ) ;
5105
5117
basicblock * exit ;
5106
5118
exit = compiler_new_block (c );
5107
5119
if (exit == NULL )
@@ -5296,7 +5308,7 @@ compiler_with(struct compiler *c, stmt_ty s, int pos)
5296
5308
5297
5309
5298
5310
/* Mark all following code as artificial */
5299
- c -> u -> u_lineno = -1 ;
5311
+ UNSET_LOC ( c ) ;
5300
5312
ADDOP (c , POP_BLOCK );
5301
5313
compiler_pop_fblock (c , WITH , block );
5302
5314
@@ -7587,6 +7599,9 @@ insert_prefix_instructions(struct compiler *c, basicblock *entryblock,
7587
7599
// This will get fixed in offset_derefs().
7588
7600
.i_oparg = oldindex ,
7589
7601
.i_lineno = -1 ,
7602
+ .i_col_offset = -1 ,
7603
+ .i_end_lineno = -1 ,
7604
+ .i_end_col_offset = -1 ,
7590
7605
.i_target = NULL ,
7591
7606
};
7592
7607
if (insert_instruction (entryblock , ncellsused , & make_cell ) < 0 ) {
@@ -7614,6 +7629,9 @@ insert_prefix_instructions(struct compiler *c, basicblock *entryblock,
7614
7629
.i_opcode = GEN_START ,
7615
7630
.i_oparg = kind ,
7616
7631
.i_lineno = -1 ,
7632
+ .i_col_offset = -1 ,
7633
+ .i_end_lineno = -1 ,
7634
+ .i_end_col_offset = -1 ,
7617
7635
.i_target = NULL ,
7618
7636
};
7619
7637
if (insert_instruction (entryblock , 0 , & gen_start ) < 0 ) {
@@ -7711,7 +7729,7 @@ assemble(struct compiler *c, int addNone)
7711
7729
block ends with a jump or return b_next shouldn't set.
7712
7730
*/
7713
7731
if (!c -> u -> u_curblock -> b_return ) {
7714
- c -> u -> u_lineno = -1 ;
7732
+ UNSET_LOC ( c ) ;
7715
7733
if (addNone )
7716
7734
ADDOP_LOAD_CONST (c , Py_None );
7717
7735
ADDOP (c , RETURN_VALUE );
@@ -8246,7 +8264,7 @@ clean_basic_block(basicblock *bb) {
8246
8264
if (src < bb -> b_iused - 1 ) {
8247
8265
int next_lineno = bb -> b_instr [src + 1 ].i_lineno ;
8248
8266
if (next_lineno < 0 || next_lineno == lineno ) {
8249
- bb -> b_instr [src + 1 ]. i_lineno = lineno ;
8267
+ COPY_INSTR_LOC ( bb -> b_instr [src ], bb -> b_instr [ src + 1 ]) ;
8250
8268
continue ;
8251
8269
}
8252
8270
}
@@ -8381,19 +8399,27 @@ propogate_line_numbers(struct assembler *a) {
8381
8399
if (b -> b_iused == 0 ) {
8382
8400
continue ;
8383
8401
}
8384
- int prev_lineno = -1 ;
8402
+
8403
+ // Not a real instruction, only to store positions
8404
+ // from previous instructions and propagate them.
8405
+ struct instr prev_instr = {
8406
+ .i_lineno = -1 ,
8407
+ .i_col_offset = -1 ,
8408
+ .i_end_lineno = -1 ,
8409
+ .i_end_col_offset = -1 ,
8410
+ };
8385
8411
for (int i = 0 ; i < b -> b_iused ; i ++ ) {
8386
8412
if (b -> b_instr [i ].i_lineno < 0 ) {
8387
- b -> b_instr [i ]. i_lineno = prev_lineno ;
8413
+ COPY_INSTR_LOC ( prev_instr , b -> b_instr [i ]) ;
8388
8414
}
8389
8415
else {
8390
- prev_lineno = b -> b_instr [i ]. i_lineno ;
8416
+ COPY_INSTR_LOC ( b -> b_instr [i ], prev_instr ) ;
8391
8417
}
8392
8418
}
8393
8419
if (!b -> b_nofallthrough && b -> b_next -> b_predecessors == 1 ) {
8394
8420
assert (b -> b_next -> b_iused );
8395
8421
if (b -> b_next -> b_instr [0 ].i_lineno < 0 ) {
8396
- b -> b_next -> b_instr [0 ]. i_lineno = prev_lineno ;
8422
+ COPY_INSTR_LOC ( prev_instr , b -> b_next -> b_instr [0 ]) ;
8397
8423
}
8398
8424
}
8399
8425
if (is_jump (& b -> b_instr [b -> b_iused - 1 ])) {
@@ -8407,7 +8433,7 @@ propogate_line_numbers(struct assembler *a) {
8407
8433
basicblock * target = b -> b_instr [b -> b_iused - 1 ].i_target ;
8408
8434
if (target -> b_predecessors == 1 ) {
8409
8435
if (target -> b_instr [0 ].i_lineno < 0 ) {
8410
- target -> b_instr [0 ]. i_lineno = prev_lineno ;
8436
+ COPY_INSTR_LOC ( prev_instr , target -> b_instr [0 ]) ;
8411
8437
}
8412
8438
}
8413
8439
}
@@ -8510,7 +8536,7 @@ ensure_exits_have_lineno(struct compiler *c)
8510
8536
if (new_target == NULL ) {
8511
8537
return -1 ;
8512
8538
}
8513
- new_target -> b_instr [ 0 ]. i_lineno = b -> b_instr [b -> b_iused - 1 ]. i_lineno ;
8539
+ COPY_INSTR_LOC ( b -> b_instr [b -> b_iused - 1 ], new_target -> b_instr [ 0 ]) ;
8514
8540
b -> b_instr [b -> b_iused - 1 ].i_target = new_target ;
8515
8541
}
8516
8542
}
@@ -8531,7 +8557,7 @@ ensure_exits_have_lineno(struct compiler *c)
8531
8557
if (!b -> b_nofallthrough && b -> b_next && b -> b_iused > 0 ) {
8532
8558
if (is_exit_without_lineno (b -> b_next )) {
8533
8559
assert (b -> b_next -> b_iused > 0 );
8534
- b -> b_next -> b_instr [0 ]. i_lineno = b -> b_instr [ b -> b_iused - 1 ]. i_lineno ;
8560
+ COPY_INSTR_LOC ( b -> b_instr [b -> b_iused - 1 ], b -> b_next -> b_instr [ 0 ]) ;
8535
8561
}
8536
8562
}
8537
8563
}
0 commit comments