(original) (raw)

Python has a peephole optimizer which does not remove dead code that it just created.

Victor

Le 18 juin 2016 23:14, "Obiesie ike-nwosu via Python-Dev" <python-dev@python.org> a écrit :
Hi,

Could some one give a hand with explaining to me why we have a JUMP\_ABSOLUTE followed by a JUMP\_FORWARD op code when this function is disassembled.

\>>> def f1():
... a, b = 10, 11
... if a >= 10:
... if b >= 11:
... print("hello world")


The disassembled function is shown below.
\>>> dis(f1)
2 0 LOAD\_CONST 4 ((10, 11))
3 UNPACK\_SEQUENCE 2
6 STORE\_FAST 0 (a)
9 STORE\_FAST 1 (b)

3 12 LOAD\_FAST 0 (a)
15 LOAD\_CONST 1 (10)
18 COMPARE\_OP 5 (>=)
21 POP\_JUMP\_IF\_FALSE 47

4 24 LOAD\_FAST 1 (b)
27 LOAD\_CONST 2 (11)
30 COMPARE\_OP 5 (>=)
33 POP\_JUMP\_IF\_FALSE 47

5 36 LOAD\_CONST 3 ('hello world')
39 PRINT\_ITEM
40 PRINT\_NEWLINE
41 JUMP\_ABSOLUTE 47
44 JUMP\_FORWARD 0 (to 47)
\>> 47 LOAD\_CONST 0 (None)
50 RETURN\_VALUE

\>From my understanding, once JUMP\_ABSOLUTE is executed, then JUMP\_FORWARD is never gotten to so must be dead code so why is it being generated?
Furthermore why is JUMP\_ABSOLUTE rather than JUMP\_FORWARD used in this particular case of nested if statements? I have tried other types of nested if statements and it has always been JUMP\_FORWARD that
is generated.
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: https://mail.python.org/mailman/options/python-dev/victor.stinner%40gmail.com