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
>
_______________________________________________
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/archive%40mail-archive.com

Reply via email to