Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 099ecfb

Browse files
committed
Simplify and future proof NOP counting in the peepholer.
No longer assumes that the input is NOP free.
1 parent 4a8d851 commit 099ecfb

1 file changed

Lines changed: 6 additions & 13 deletions

File tree

Python/compile.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,7 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
536536
goto exitUnchanged;
537537
assert(PyList_Check(consts));
538538

539-
for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
540-
addrmap[i] = i - nops;
539+
for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
541540
opcode = codestr[i];
542541

543542
lastlc = cumlc;
@@ -560,7 +559,6 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
560559
SETARG(codestr, i, j);
561560
codestr[i+3] = POP_TOP;
562561
codestr[i+4] = NOP;
563-
nops++;
564562
break;
565563

566564
/* not a is b --> a is not b
@@ -575,7 +573,6 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
575573
continue;
576574
SETARG(codestr, i, (j^1));
577575
codestr[i+3] = NOP;
578-
nops++;
579576
break;
580577

581578
/* Replace LOAD_GLOBAL/LOAD_NAME None with LOAD_CONST None */
@@ -604,7 +601,6 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
604601
!PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
605602
continue;
606603
memset(codestr+i, NOP, 7);
607-
nops += 7;
608604
break;
609605

610606
/* Try to fold tuples of constants.
@@ -619,7 +615,6 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
619615
codestr[h] == LOAD_CONST &&
620616
ISBASICBLOCK(blocks, h, 3*(j+1)) &&
621617
tuple_of_constants(&codestr[h], j, consts)) {
622-
nops += 3 * j;
623618
break;
624619
}
625620
/* Intentional fallthrough */
@@ -631,16 +626,13 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
631626
continue;
632627
if (j == 1) {
633628
memset(codestr+i, NOP, 6);
634-
nops += 6;
635629
} else if (j == 2) {
636630
codestr[i] = ROT_TWO;
637631
memset(codestr+i+1, NOP, 5);
638-
nops += 5;
639632
} else if (j == 3) {
640633
codestr[i] = ROT_THREE;
641634
codestr[i+1] = ROT_TWO;
642635
memset(codestr+i+2, NOP, 4);
643-
nops += 4;
644636
}
645637
break;
646638

@@ -704,12 +696,16 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
704696
!ISBASICBLOCK(blocks,i,5))
705697
continue;
706698
memset(codestr+i+1, NOP, 4);
707-
nops += 4;
708699
break;
709700
}
710701
}
711702

712703
/* Fixup linenotab */
704+
for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
705+
addrmap[i] = i - nops;
706+
if (codestr[i] == NOP)
707+
nops++;
708+
}
713709
cum_orig_line = 0;
714710
last_line = 0;
715711
for (i=0 ; i < tabsiz ; i+=2) {
@@ -749,9 +745,6 @@ optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *linen
749745
while (adj--)
750746
codestr[h++] = codestr[i++];
751747
}
752-
/* The following assertion detects the presence of NOPs in the input
753-
bytecode. The compiler never produces NOPs so far; if one day it
754-
does, the way 'nops' is counted above must be changed. */
755748
assert(h + nops == codelen);
756749

757750
code = PyString_FromStringAndSize((char *)codestr, h);

0 commit comments

Comments
 (0)