@@ -197,47 +197,6 @@ _PyCompile_EnsureArrayLargeEnough(int idx, void **array, int *alloc,
197197    return  SUCCESS ;
198198}
199199
200- static  cfg_builder * 
201- instr_sequence_to_cfg (instr_sequence  * seq ) {
202-     if  (_PyInstructionSequence_ApplyLabelMap (seq ) <  0 ) {
203-         return  NULL ;
204-     }
205-     cfg_builder  * g  =  _PyCfgBuilder_New ();
206-     if  (g  ==  NULL ) {
207-         return  NULL ;
208-     }
209-     for  (int  i  =  0 ; i  <  seq -> s_used ; i ++ ) {
210-         seq -> s_instrs [i ].i_target  =  0 ;
211-     }
212-     for  (int  i  =  0 ; i  <  seq -> s_used ; i ++ ) {
213-         instruction  * instr  =  & seq -> s_instrs [i ];
214-         if  (HAS_TARGET (instr -> i_opcode )) {
215-             assert (instr -> i_oparg  >= 0  &&  instr -> i_oparg  <  seq -> s_used );
216-             seq -> s_instrs [instr -> i_oparg ].i_target  =  1 ;
217-         }
218-     }
219-     for  (int  i  =  0 ; i  <  seq -> s_used ; i ++ ) {
220-         instruction  * instr  =  & seq -> s_instrs [i ];
221-         if  (instr -> i_target ) {
222-             jump_target_label  lbl_  =  {i };
223-             if  (_PyCfgBuilder_UseLabel (g , lbl_ ) <  0 ) {
224-                 goto error ;
225-             }
226-         }
227-         int  opcode  =  instr -> i_opcode ;
228-         int  oparg  =  instr -> i_oparg ;
229-         if  (_PyCfgBuilder_Addop (g , opcode , oparg , instr -> i_loc ) <  0 ) {
230-             goto error ;
231-         }
232-     }
233-     if  (_PyCfgBuilder_CheckSize (g ) <  0 ) {
234-         goto error ;
235-     }
236-     return  g ;
237- error :
238-     _PyCfgBuilder_Free (g );
239-     return  NULL ;
240- }
241200
242201/* The following items change on entry and exit of code blocks. 
243202   They must be saved and restored when returning to a block. 
@@ -691,48 +650,6 @@ compiler_set_qualname(struct compiler *c)
691650    return  SUCCESS ;
692651}
693652
694- /* Return the stack effect of opcode with argument oparg. 
695- 
696-    Some opcodes have different stack effect when jump to the target and 
697-    when not jump. The 'jump' parameter specifies the case: 
698- 
699-    * 0 -- when not jump 
700-    * 1 -- when jump 
701-    * -1 -- maximal 
702-  */ 
703- static  int 
704- stack_effect (int  opcode , int  oparg , int  jump )
705- {
706-     if  (opcode  <  0 ) {
707-         return  PY_INVALID_STACK_EFFECT ;
708-     }
709-     if  ((opcode  <= MAX_REAL_OPCODE ) &&  (_PyOpcode_Deopt [opcode ] !=  opcode )) {
710-         // Specialized instructions are not supported. 
711-         return  PY_INVALID_STACK_EFFECT ;
712-     }
713-     int  popped  =  _PyOpcode_num_popped (opcode , oparg );
714-     int  pushed  =  _PyOpcode_num_pushed (opcode , oparg );
715-     if  (popped  <  0  ||  pushed  <  0 ) {
716-         return  PY_INVALID_STACK_EFFECT ;
717-     }
718-     if  (IS_BLOCK_PUSH_OPCODE (opcode ) &&  !jump ) {
719-         return  0 ;
720-     }
721-     return  pushed  -  popped ;
722- }
723- 
724- int 
725- PyCompile_OpcodeStackEffectWithJump (int  opcode , int  oparg , int  jump )
726- {
727-     return  stack_effect (opcode , oparg , jump );
728- }
729- 
730- int 
731- PyCompile_OpcodeStackEffect (int  opcode , int  oparg )
732- {
733-     return  stack_effect (opcode , oparg , -1 );
734- }
735- 
736653int 
737654_PyCompile_OpcodeIsValid (int  opcode )
738655{
@@ -7592,7 +7509,7 @@ optimize_and_assemble_code_unit(struct compiler_unit *u, PyObject *const_cache,
75927509    if  (consts  ==  NULL ) {
75937510        goto error ;
75947511    }
7595-     g  =  instr_sequence_to_cfg (u -> u_instr_sequence );
7512+     g  =  _PyCfg_FromInstructionSequence (u -> u_instr_sequence );
75967513    if  (g  ==  NULL ) {
75977514        goto error ;
75987515    }
@@ -7645,39 +7562,6 @@ optimize_and_assemble(struct compiler *c, int addNone)
76457562    return  optimize_and_assemble_code_unit (u , const_cache , code_flags , filename );
76467563}
76477564
7648- /* Access to compiler optimizations for unit tests. 
7649-  * 
7650-  * _PyCompile_CodeGen takes and AST, applies code-gen and 
7651-  * returns the unoptimized CFG as an instruction list. 
7652-  * 
7653-  * _PyCompile_OptimizeCfg takes an instruction list, constructs 
7654-  * a CFG, optimizes it and converts back to an instruction list. 
7655-  * 
7656-  * An instruction list is a PyList where each item is either 
7657-  * a tuple describing a single instruction: 
7658-  * (opcode, oparg, lineno, end_lineno, col, end_col), or 
7659-  * a jump target label marking the beginning of a basic block. 
7660-  */ 
7661- 
7662- 
7663- static  PyObject  * 
7664- cfg_to_instruction_sequence (cfg_builder  * g )
7665- {
7666-     instr_sequence  * seq  =  (instr_sequence  * )_PyInstructionSequence_New ();
7667-     if  (seq  !=  NULL ) {
7668-         if  (_PyCfg_ToInstructionSequence (g , seq ) <  0 ) {
7669-             goto error ;
7670-         }
7671-         if  (_PyInstructionSequence_ApplyLabelMap (seq ) <  0 ) {
7672-             goto error ;
7673-         }
7674-     }
7675-     return  (PyObject * )seq ;
7676- error :
7677-     PyInstructionSequence_Fini (seq );
7678-     return  NULL ;
7679- }
7680- 
76817565// C implementation of inspect.cleandoc() 
76827566// 
76837567// Difference from inspect.cleandoc(): 
@@ -7768,6 +7652,12 @@ _PyCompile_CleanDoc(PyObject *doc)
77687652    return  res ;
77697653}
77707654
7655+ /* Access to compiler optimizations for unit tests. 
7656+  * 
7657+  * _PyCompile_CodeGen takes an AST, applies code-gen and 
7658+  * returns the unoptimized CFG as an instruction list. 
7659+  * 
7660+  */ 
77717661
77727662PyObject  * 
77737663_PyCompile_CodeGen (PyObject  * ast , PyObject  * filename , PyCompilerFlags  * pflags ,
@@ -7859,35 +7749,6 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
78597749    return  res ;
78607750}
78617751
7862- PyObject  * 
7863- _PyCompile_OptimizeCfg (PyObject  * seq , PyObject  * consts , int  nlocals )
7864- {
7865-     if  (!_PyInstructionSequence_Check (seq )) {
7866-         PyErr_SetString (PyExc_ValueError , "expected an instruction sequence" );
7867-         return  NULL ;
7868-     }
7869-     PyObject  * const_cache  =  PyDict_New ();
7870-     if  (const_cache  ==  NULL ) {
7871-         return  NULL ;
7872-     }
7873- 
7874-     PyObject  * res  =  NULL ;
7875-     cfg_builder  * g  =  instr_sequence_to_cfg ((instr_sequence * )seq );
7876-     if  (g  ==  NULL ) {
7877-         goto error ;
7878-     }
7879-     int  nparams  =  0 , firstlineno  =  1 ;
7880-     if  (_PyCfg_OptimizeCodeUnit (g , consts , const_cache , nlocals ,
7881-                                 nparams , firstlineno ) <  0 ) {
7882-         goto error ;
7883-     }
7884-     res  =  cfg_to_instruction_sequence (g );
7885- error :
7886-     Py_DECREF (const_cache );
7887-     _PyCfgBuilder_Free (g );
7888-     return  res ;
7889- }
7890- 
78917752int  _PyCfg_JumpLabelsToTargets (cfg_builder  * g );
78927753
78937754PyCodeObject  * 
@@ -7908,7 +7769,7 @@ _PyCompile_Assemble(_PyCompile_CodeUnitMetadata *umd, PyObject *filename,
79087769        return  NULL ;
79097770    }
79107771
7911-     g  =  instr_sequence_to_cfg ((instr_sequence * )seq );
7772+     g  =  _PyCfg_FromInstructionSequence ((instr_sequence * )seq );
79127773    if  (g  ==  NULL ) {
79137774        goto error ;
79147775    }
0 commit comments