bpo-37213: Handle negative line deltas correctly in the peephole optimizer (GH-13969)

The peephole optimizer was not optimizing correctly bytecode after negative deltas were introduced. This is due to the fact that some special values (255) were being searched for in both instruction pointer delta and line number deltas.
This commit is contained in:
Pablo Galindo 2019-06-13 19:16:22 +01:00 committed by GitHub
parent 95492032c4
commit 3498c642f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 4234 additions and 4150 deletions

View file

@ -250,12 +250,16 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
lnotab = (unsigned char*)PyBytes_AS_STRING(lnotab_obj);
tabsiz = PyBytes_GET_SIZE(lnotab_obj);
assert(tabsiz == 0 || Py_REFCNT(lnotab_obj) == 1);
if (memchr(lnotab, 255, tabsiz) != NULL) {
/* 255 value are used for multibyte bytecode instructions */
goto exitUnchanged;
/* Don't optimize if lnotab contains instruction pointer delta larger
than +255 (encoded as multiple bytes), just to keep the peephole optimizer
simple. The optimizer leaves line number deltas unchanged. */
for (j = 0; j < tabsiz; j += 2) {
if (lnotab[j] == 255) {
goto exitUnchanged;
}
}
/* Note: -128 and 127 special values for line number delta are ok,
the peephole optimizer doesn't modify line numbers. */
assert(PyBytes_Check(code));
Py_ssize_t codesize = PyBytes_GET_SIZE(code);