@@ -235,6 +235,28 @@ _PyCompile_InstructionSequence_UseLabel(instr_sequence *seq, int lbl)
235
235
return SUCCESS ;
236
236
}
237
237
238
+ int
239
+ _PyCompile_InstructionSequence_ApplyLabelMap (instr_sequence * instrs )
240
+ {
241
+ /* Replace labels by offsets in the code */
242
+ for (int i = 0 ; i < instrs -> s_used ; i ++ ) {
243
+ instruction * instr = & instrs -> s_instrs [i ];
244
+ if (HAS_TARGET (instr -> i_opcode )) {
245
+ assert (instr -> i_oparg < instrs -> s_labelmap_size );
246
+ instr -> i_oparg = instrs -> s_labelmap [instr -> i_oparg ];
247
+ }
248
+ _PyCompile_ExceptHandlerInfo * hi = & instr -> i_except_handler_info ;
249
+ if (hi -> h_label >= 0 ) {
250
+ assert (hi -> h_label < instrs -> s_labelmap_size );
251
+ hi -> h_label = instrs -> s_labelmap [hi -> h_label ];
252
+ }
253
+ }
254
+ /* Clear label map so it's never used again */
255
+ PyMem_Free (instrs -> s_labelmap );
256
+ instrs -> s_labelmap = NULL ;
257
+ instrs -> s_labelmap_size = 0 ;
258
+ return SUCCESS ;
259
+ }
238
260
239
261
#define MAX_OPCODE 511
240
262
@@ -7824,11 +7846,8 @@ instr_sequence_to_instructions(instr_sequence *seq)
7824
7846
for (int i = 0 ; i < seq -> s_used ; i ++ ) {
7825
7847
instruction * instr = & seq -> s_instrs [i ];
7826
7848
location loc = instr -> i_loc ;
7827
- int arg = HAS_TARGET (instr -> i_opcode ) ?
7828
- seq -> s_labelmap [instr -> i_oparg ] : instr -> i_oparg ;
7829
-
7830
7849
PyObject * inst_tuple = Py_BuildValue (
7831
- "(iiiiii)" , instr -> i_opcode , arg ,
7850
+ "(iiiiii)" , instr -> i_opcode , instr -> i_oparg ,
7832
7851
loc .lineno , loc .end_lineno ,
7833
7852
loc .col_offset , loc .end_col_offset );
7834
7853
if (inst_tuple == NULL ) {
@@ -7855,6 +7874,9 @@ cfg_to_instructions(cfg_builder *g)
7855
7874
if (_PyCfg_ToInstructionSequence (g , & seq ) < 0 ) {
7856
7875
return NULL ;
7857
7876
}
7877
+ if (_PyCompile_InstructionSequence_ApplyLabelMap (& seq ) < 0 ) {
7878
+ return NULL ;
7879
+ }
7858
7880
PyObject * res = instr_sequence_to_instructions (& seq );
7859
7881
instr_sequence_fini (& seq );
7860
7882
return res ;
@@ -8026,6 +8048,10 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
8026
8048
goto finally ;
8027
8049
}
8028
8050
8051
+ if (_PyCompile_InstructionSequence_ApplyLabelMap (INSTR_SEQUENCE (c )) < 0 ) {
8052
+ return NULL ;
8053
+ }
8054
+
8029
8055
PyObject * insts = instr_sequence_to_instructions (INSTR_SEQUENCE (c ));
8030
8056
if (insts == NULL ) {
8031
8057
goto finally ;
0 commit comments