@@ -8375,21 +8375,22 @@ fold_tuple_on_constants(struct compiler *c,
8375
8375
#define VISITED (-1)
8376
8376
8377
8377
// Replace an arbitrary run of SWAPs and NOPs with an optimal one that has the
8378
- // same effect. Return the number of instructions that were optimized.
8378
+ // same effect.
8379
8379
static int
8380
- swaptimize (basicblock * block , int ix )
8380
+ swaptimize (basicblock * block , int * ix )
8381
8381
{
8382
8382
// NOTE: "./python -m test test_patma" serves as a good, quick stress test
8383
8383
// for this function. Make sure to blow away cached *.pyc files first!
8384
- assert (ix < block -> b_iused );
8385
- struct instr * instructions = & block -> b_instr [ix ];
8384
+ assert (* ix < block -> b_iused );
8385
+ struct instr * instructions = & block -> b_instr [* ix ];
8386
8386
// Find the length of the current sequence of SWAPs and NOPs, and record the
8387
8387
// maximum depth of the stack manipulations:
8388
8388
assert (instructions [0 ].i_opcode == SWAP );
8389
8389
int depth = instructions [0 ].i_oparg ;
8390
8390
int len = 0 ;
8391
8391
int more = false;
8392
- while (++ len < block -> b_iused - ix ) {
8392
+ int limit = block -> b_iused - * ix ;
8393
+ while (++ len < limit ) {
8393
8394
int opcode = instructions [len ].i_opcode ;
8394
8395
if (opcode == SWAP ) {
8395
8396
depth = Py_MAX (depth , instructions [len ].i_oparg );
@@ -8405,6 +8406,10 @@ swaptimize(basicblock *block, int ix)
8405
8406
}
8406
8407
// Create an array with elements {0, 1, 2, ..., depth - 1}:
8407
8408
int * stack = PyMem_Malloc (depth * sizeof (int ));
8409
+ if (stack == NULL ) {
8410
+ PyErr_NoMemory ();
8411
+ return -1 ;
8412
+ }
8408
8413
for (int i = 0 ; i < depth ; i ++ ) {
8409
8414
stack [i ] = i ;
8410
8415
}
@@ -8462,9 +8467,9 @@ swaptimize(basicblock *block, int ix)
8462
8467
while (0 <= current ) {
8463
8468
instructions [current -- ].i_opcode = NOP ;
8464
8469
}
8465
- // Done! Return the number of optimized instructions:
8466
8470
PyMem_Free (stack );
8467
- return len - 1 ;
8471
+ * ix += len - 1 ;
8472
+ return
99CC
0 ;
8468
8473
}
8469
8474
8470
8475
// Attempt to eliminate jumps to jumps by updating inst to jump to
@@ -8706,7 +8711,9 @@ optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
8706
8711
inst -> i_opcode = NOP ;
8707
8712
break ;
8708
8713
}
8709
- i += swaptimize (bb , i );
8714
+ if (swaptimize (bb , & i )) {
8715
+ goto error ;
8716
+ }
8710
8717
break ;
8711
8718
case KW_NAMES :
8712
8719
break ;
0 commit comments