22
22
}
23
23
24
24
typedef _PyCompilerSrcLocation location ;
25
- typedef _PyCfgInstruction cfg_instr ;
26
- typedef _PyCfgBasicblock basicblock ;
25
+ typedef _PyCompilerInstruction instruction ;
26
+ typedef _PyCompile_InstructionSequence instr_sequence ;
27
27
28
28
static inline bool
29
29
same_location (location a , location b )
@@ -117,21 +117,22 @@ assemble_emit_exception_table_item(struct assembler *a, int value, int msb)
117
117
#define MAX_SIZE_OF_ENTRY 20
118
118
119
119
static 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
+ _PyCompilerExceptHandlerInfo * handler )
121
122
{
122
123
Py_ssize_t len = PyBytes_GET_SIZE (a -> a_except_table );
123
124
if (a -> a_except_table_off + MAX_SIZE_OF_ENTRY >= len ) {
124
125
RETURN_IF_ERROR (_PyBytes_Resize (& a -> a_except_table , len * 2 ));
125
126
}
126
127
int size = end - start ;
127
128
assert (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 ) {
131
132
depth -= 1 ;
132
133
}
133
134
assert (depth >= 0 );
134
- int depth_lasti = (depth <<1 ) | handler -> b_preserve_lasti ;
135
+ int depth_lasti = (depth <<1 ) | handler -> h_preserve_lasti ;
135
136
assemble_emit_exception_table_item (a , start , (1 <<7 ));
136
137
assemble_emit_exception_table_item (a , size , 0 );
137
138
assemble_emit_exception_table_item (a , target , 0 );
@@ -140,29 +141,26 @@ assemble_emit_exception_table_entry(struct assembler *a, int start, int end, bas
140
141
}
141
142
142
143
static int
143
- assemble_exception_table (struct assembler * a , basicblock * entryblock )
144
+ assemble_exception_table (struct assembler * a , instr_sequence * instrs )
144
145
{
145
- basicblock * b ;
146
146
int ioffset = 0 ;
147
- basicblock * handler = NULL ;
147
+ _PyCompilerExceptHandlerInfo handler ;
148
+ handler .h_offset = -1 ;
148
149
int 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 ));
160
156
}
161
- ioffset += _PyCfg_InstrSize (instr );
157
+ start = ioffset ;
158
+ handler = instr -> i_except_handler_info ;
162
159
}
160
+ ioffset += _PyCompile_InstrSize (instr -> i_opcode , instr -> i_oparg );
163
161
}
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 ));
166
164
}
167
165
return SUCCESS ;
168
166
}
@@ -316,31 +314,31 @@ assemble_emit_location(struct assembler* a, location loc, int isize)
316
314
}
317
315
318
316
static 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 )
320
319
{
321
320
a -> a_lineno = firstlineno ;
322
321
location loc = NO_LOCATION ;
323
322
int 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 )) {
327
326
RETURN_IF_ERROR (assemble_emit_location (a , loc , size ));
328
- loc = b -> b_instr [ j ]. i_loc ;
327
+ loc = instr -> i_loc ;
329
328
size = 0 ;
330
- }
331
- size += _PyCfg_InstrSize (& b -> b_instr [j ]);
332
329
}
330
+ size += _PyCompile_InstrSize (instr -> i_opcode , instr -> i_oparg );
333
331
}
334
332
RETURN_IF_ERROR (assemble_emit_location (a , loc , size ));
335
333
return SUCCESS ;
336
334
}
337
335
338
336
static void
339
- write_instr (_Py_CODEUNIT * codestr , cfg_instr * instruction , int ilen )
337
+ write_instr (_Py_CODEUNIT * codestr , instruction * instr , int ilen )
340
338
{
341
- int opcode = instruction -> i_opcode ;
339
+ int opcode = instr -> i_opcode ;
342
340
assert (!IS_PSEUDO_OPCODE (opcode ));
343
- int oparg = instruction -> i_oparg ;
341
+ int oparg = instr -> i_oparg ;
344
342
assert (HAS_ARG (opcode ) || oparg == 0 );
345
343
int caches = _PyOpcode_Caches [opcode ];
346
344
switch (ilen - caches ) {
@@ -380,12 +378,12 @@ write_instr(_Py_CODEUNIT *codestr, cfg_instr *instruction, int ilen)
380
378
*/
381
379
382
380
static int
383
- assemble_emit_instr (struct assembler * a , cfg_instr * i )
381
+ assemble_emit_instr (struct assembler * a , instruction * instr )
384
382
{
385
383
Py_ssize_t len = PyBytes_GET_SIZE (a -> a_bytecode );
386
384
_Py_CODEUNIT * code ;
387
385
388
- int size = _PyCfg_InstrSize ( i );
386
+ int size = _PyCompile_InstrSize ( instr -> i_opcode , instr -> i_oparg );
389
387
if (a -> a_offset + size >= len / (int )sizeof (_Py_CODEUNIT )) {
390
388
if (len > PY_SSIZE_T_MAX / 2 ) {
391
389
return ERROR ;
@@ -394,25 +392,24 @@ assemble_emit_instr(struct assembler *a, cfg_instr *i)
394
392
}
395
393
code = (_Py_CODEUNIT * )PyBytes_AS_STRING (a -> a_bytecode ) + a -> a_offset ;
396
394
a -> a_offset += size ;
397
- write_instr (code , i , size );
395
+ write_instr (code , instr , size );
398
396
return SUCCESS ;
399
397
}
400
398
401
399
static 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 )
404
402
{
405
403
RETURN_IF_ERROR (assemble_init (a , first_lineno ));
406
404
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 ));
411
408
}
412
409
413
- RETURN_IF_ERROR (assemble_location_info (a , entryblock , a -> a_lineno ));
410
+ RETURN_IF_ERROR (assemble_location_info (a , instrs , a -> a_lineno ));
414
411
415
- RETURN_IF_ERROR (assemble_exception_table (a , entryblock ));
412
+ RETURN_IF_ERROR (assemble_exception_table (a , instrs ));
416
413
417
414
RETURN_IF_ERROR (_PyBytes_Resize (& a -> a_except_table , a -> a_except_table_off ));
418
415
RETURN_IF_ERROR (_PyCompile_ConstCacheMergeOne (const_cache , & a -> a_except_table ));
@@ -586,13 +583,13 @@ makecode(_PyCompile_CodeUnitMetadata *umd, struct assembler *a, PyObject *const_
586
583
587
584
PyCodeObject *
588
585
_PyAssemble_MakeCodeObject (_PyCompile_CodeUnitMetadata * umd , PyObject * const_cache ,
589
- PyObject * consts , int maxdepth , basicblock * entryblock ,
586
+ PyObject * consts , int maxdepth , instr_sequence * instrs ,
590
587
int nlocalsplus , int code_flags , PyObject * filename )
591
588
{
592
589
PyCodeObject * co = NULL ;
593
590
594
591
struct 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 );
596
593
if (res == SUCCESS ) {
597
594
co = makecode (umd , & a , const_cache , consts , maxdepth , nlocalsplus ,
598
595
code_flags , filename );
0 commit comments