Python Bytecode

Terry Reedy tjreedy at udel.edu
Fri Aug 2 12:53:27 EDT 2002


<emailed and posted>
"Michael Hudson" <mwh at python.net> wrote
...
 http://www.python.org/doc/current/lib/bytecodes.html

Thanks.  I read this, found a couple of typos, and sent note to
python-doc (don't have Source Forge login).

Can you or someone answer a question regarding the following from the
above:
"
JUMP_IF_TRUE    delta
If TOS is true, increment the byte code counter by delta. TOS is left
on the stack.

JUMP_IF_FALSE    delta
If TOS is false, increment the byte code counter by delta. TOS is not
changed.
"

Why isn't TOS removed?  Or, at least, why isn't there alternative
bytecode that does so?  Seems like most (all?) usages remove it anyway
on both branches.  For example (from your python-dev post)

>>> def f():
...     if a:
...         print 1
...
>>> import dis
>>> dis.dis(f)
  2           0 LOAD_GLOBAL            0 (a)
              3 JUMP_IF_FALSE            9 (to 15)
              6 POP_TOP

  3           7 LOAD_CONST               1 (1)
             10 PRINT_ITEM
             11 PRINT_NEWLINE
             12 JUMP_FORWARD         1 (to 16)
             15 POP_TOP
             16 LOAD_CONST               0 (None)
             19 RETURN_VALUE

Folding the two POP_TOPs into the jump would shorten both code length
and execution time.  For an 'if' without 'elif' or 'else', it would
also eliminate the need to JUMP_FORWARD over the now not-there second
POP_TOP, giving even more saving.

Terry J. Reedy






More information about the Python-list mailing list