11#include <stdbool.h>
22
33#include "Python.h"
4- #include "pycore_flowgraph .h"
4+ #include "pycore_code .h" // write_location_entry_start()
55#include "pycore_compile.h"
6+ #include "pycore_opcode.h" // _PyOpcode_Caches[] and opcode category macros
67#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
7- #include "pycore_code.h" // write_location_entry_start()
88
99
1010#define DEFAULT_CODE_SIZE 128
2222 }
2323
2424typedef _PyCompilerSrcLocation location ;
25- typedef _PyCfgInstruction cfg_instr ;
26- typedef _PyCfgBasicblock basicblock ;
25+ typedef _PyCompile_Instruction instruction ;
26+ typedef _PyCompile_InstructionSequence instr_sequence ;
2727
2828static inline bool
2929same_location (location a ,location b )
@@ -117,21 +117,22 @@ assemble_emit_exception_table_item(struct assembler *a, int value, int msb)
117117#define MAX_SIZE_OF_ENTRY 20
118118
119119static int
120- assemble_emit_exception_table_entry (struct assembler * a ,int start ,int end ,basicblock * handler )
120+ assemble_emit_exception_table_entry (struct assembler * a ,int start ,int end ,
121+ _PyCompile_ExceptHandlerInfo * handler )
121122{
122123Py_ssize_t len = PyBytes_GET_SIZE (a -> a_except_table );
123124if (a -> a_except_table_off + MAX_SIZE_OF_ENTRY >=len ) {
124125RETURN_IF_ERROR (_PyBytes_Resize (& a -> a_except_table ,len * 2 ));
125126 }
126127int size = end - start ;
127128assert (end > start );
128- int target = handler -> b_offset ;
129- int depth = handler -> b_startdepth - 1 ;
130- if (handler -> b_preserve_lasti ) {
129+ int target = handler -> h_offset ;
130+ int depth = handler -> h_startdepth - 1 ;
131+ if (handler -> h_preserve_lasti ) {
131132depth -= 1 ;
132133 }
133134assert (depth >=0 );
134- int depth_lasti = (depth <<1 ) |handler -> b_preserve_lasti ;
135+ int depth_lasti = (depth <<1 ) |handler -> h_preserve_lasti ;
135136assemble_emit_exception_table_item (a ,start , (1 <<7 ));
136137assemble_emit_exception_table_item (a ,size ,0 );
137138assemble_emit_exception_table_item (a ,target ,0 );
@@ -140,29 +141,26 @@ assemble_emit_exception_table_entry(struct assembler *a, int start, int end, bas
140141}
141142
142143static int
143- assemble_exception_table (struct assembler * a ,basicblock * entryblock )
144+ assemble_exception_table (struct assembler * a ,instr_sequence * instrs )
144145{
145- basicblock * b ;
146146int ioffset = 0 ;
147- basicblock * handler = NULL ;
147+ _PyCompile_ExceptHandlerInfo handler ;
148+ handler .h_offset = -1 ;
148149int start = -1 ;
149- for (b = entryblock ;b != NULL ;b = b -> b_next ) {
150- ioffset = b -> b_offset ;
151- for (int i = 0 ;i < b -> b_iused ;i ++ ) {
152- cfg_instr * instr = & b -> b_instr [i ];
153- if (instr -> i_except != handler ) {
154- if (handler != NULL ) {
155- RETURN_IF_ERROR (
156- assemble_emit_exception_table_entry (a ,start ,ioffset ,handler ));
157- }
158- start = ioffset ;
159- handler = instr -> i_except ;
150+ for (int i = 0 ;i < instrs -> s_used ;i ++ ) {
151+ instruction * instr = & instrs -> s_instrs [i ];
152+ if (instr -> i_except_handler_info .h_offset != handler .h_offset ) {
153+ if (handler .h_offset >=0 ) {
154+ RETURN_IF_ERROR (
155+ assemble_emit_exception_table_entry (a ,start ,ioffset ,& handler ));
160156 }
161- ioffset += _PyCfg_InstrSize (instr );
157+ start = ioffset ;
158+ handler = instr -> i_except_handler_info ;
162159 }
160+ ioffset += _PyCompile_InstrSize (instr -> i_opcode ,instr -> i_oparg );
163161 }
164- if (handler != NULL ) {
165- RETURN_IF_ERROR (assemble_emit_exception_table_entry (a ,start ,ioffset ,handler ));
162+ if (handler . h_offset >= 0 ) {
163+ RETURN_IF_ERROR (assemble_emit_exception_table_entry (a ,start ,ioffset ,& handler ));
166164 }
167165return SUCCESS ;
168166}
@@ -316,31 +314,31 @@ assemble_emit_location(struct assembler* a, location loc, int isize)
316314}
317315
318316static int
319- assemble_location_info (struct assembler * a ,basicblock * entryblock ,int firstlineno )
317+ assemble_location_info (struct assembler * a ,instr_sequence * instrs ,
318+ int firstlineno )
320319{
321320a -> a_lineno = firstlineno ;
322321location loc = NO_LOCATION ;
323322int size = 0 ;
324- for (basicblock * b = entryblock ; b != NULL ; b = b -> b_next ) {
325- for ( int j = 0 ; j < b -> b_iused ; j ++ ) {
326- if (!same_location (loc ,b -> b_instr [ j ]. i_loc )) {
323+ for (int i = 0 ; i < instrs -> s_used ; i ++ ) {
324+ instruction * instr = & instrs -> s_instrs [ i ];
325+ if (!same_location (loc ,instr -> i_loc )) {
327326RETURN_IF_ERROR (assemble_emit_location (a ,loc ,size ));
328- loc = b -> b_instr [ j ]. i_loc ;
327+ loc = instr -> i_loc ;
329328size = 0 ;
330- }
331- size += _PyCfg_InstrSize (& b -> b_instr [j ]);
332329 }
330+ size += _PyCompile_InstrSize (instr -> i_opcode ,instr -> i_oparg );
333331 }
334332RETURN_IF_ERROR (assemble_emit_location (a ,loc ,size ));
335333return SUCCESS ;
336334}
337335
338336static void
339- write_instr (_Py_CODEUNIT * codestr ,cfg_instr * instruction ,int ilen )
337+ write_instr (_Py_CODEUNIT * codestr ,instruction * instr ,int ilen )
340338{
341- int opcode = instruction -> i_opcode ;
339+ int opcode = instr -> i_opcode ;
342340assert (!IS_PSEUDO_OPCODE (opcode ));
343- int oparg = instruction -> i_oparg ;
341+ int oparg = instr -> i_oparg ;
344342assert (HAS_ARG (opcode )|| oparg == 0 );
345343int caches = _PyOpcode_Caches [opcode ];
346344switch (ilen - caches ) {
@@ -380,12 +378,12 @@ write_instr(_Py_CODEUNIT *codestr, cfg_instr *instruction, int ilen)
380378*/
381379
382380static int
383- assemble_emit_instr (struct assembler * a ,cfg_instr * i )
381+ assemble_emit_instr (struct assembler * a ,instruction * instr )
384382{
385383Py_ssize_t len = PyBytes_GET_SIZE (a -> a_bytecode );
386384_Py_CODEUNIT * code ;
387385
388- int size = _PyCfg_InstrSize ( i );
386+ int size = _PyCompile_InstrSize ( instr -> i_opcode , instr -> i_oparg );
389387if (a -> a_offset + size >=len / (int )sizeof (_Py_CODEUNIT )) {
390388if (len > PY_SSIZE_T_MAX /2 ) {
391389return ERROR ;
@@ -394,25 +392,24 @@ assemble_emit_instr(struct assembler *a, cfg_instr *i)
394392 }
395393code = (_Py_CODEUNIT * )PyBytes_AS_STRING (a -> a_bytecode )+ a -> a_offset ;
396394a -> a_offset += size ;
397- write_instr (code ,i ,size );
395+ write_instr (code ,instr ,size );
398396return SUCCESS ;
399397}
400398
401399static int
402- assemble_emit (struct assembler * a ,basicblock * entryblock , int first_lineno ,
403- PyObject * const_cache )
400+ assemble_emit (struct assembler * a ,instr_sequence * instrs ,
401+ int first_lineno , PyObject * const_cache )
404402{
405403RETURN_IF_ERROR (assemble_init (a ,first_lineno ));
406404
407- for (basicblock * b = entryblock ;b != NULL ;b = b -> b_next ) {
408- for (int j = 0 ;j < b -> b_iused ;j ++ ) {
409- RETURN_IF_ERROR (assemble_emit_instr (a ,& b -> b_instr [j ]));
410- }
405+ for (int i = 0 ;i < instrs -> s_used ;i ++ ) {
406+ instruction * instr = & instrs -> s_instrs [i ];
407+ RETURN_IF_ERROR (assemble_emit_instr (a ,instr ));
411408 }
412409
413- RETURN_IF_ERROR (assemble_location_info (a ,entryblock ,a -> a_lineno ));
410+ RETURN_IF_ERROR (assemble_location_info (a ,instrs ,a -> a_lineno ));
414411
415- RETURN_IF_ERROR (assemble_exception_table (a ,entryblock ));
412+ RETURN_IF_ERROR (assemble_exception_table (a ,instrs ));
416413
417414RETURN_IF_ERROR (_PyBytes_Resize (& a -> a_except_table ,a -> a_except_table_off ));
418415RETURN_IF_ERROR (_PyCompile_ConstCacheMergeOne (const_cache ,& a -> a_except_table ));
@@ -586,13 +583,13 @@ makecode(_PyCompile_CodeUnitMetadata *umd, struct assembler *a, PyObject *const_
586583
587584PyCodeObject *
588585_PyAssemble_MakeCodeObject (_PyCompile_CodeUnitMetadata * umd ,PyObject * const_cache ,
589- PyObject * consts ,int maxdepth ,basicblock * entryblock ,
586+ PyObject * consts ,int maxdepth ,instr_sequence * instrs ,
590587int nlocalsplus ,int code_flags ,PyObject * filename )
591588{
592589PyCodeObject * co = NULL ;
593590
594591struct assembler a ;
595- int res = assemble_emit (& a ,entryblock ,umd -> u_firstlineno ,const_cache );
592+ int res = assemble_emit (& a ,instrs ,umd -> u_firstlineno ,const_cache );
596593if (res == SUCCESS ) {
597594co = makecode (umd ,& a ,const_cache ,consts ,maxdepth ,nlocalsplus ,
598595code_flags ,filename );