question about a command like 'goto ' in Python's bytecode or it'sjust a compiler optimization?

Discussion in 'Python' started by higer, Jun 17, 2009.

  1. higer

    higer Guest

    My Python version is 2.5.2; When I reading the bytecode of some pyc
    file, I always found that there are many jump command from different
    position,but to the same position. You can see this situation in
    following code(this bytecode is just from one .pyc file and I don't
    have its source .py file):

    ......
    526 POP_TOP ''
    527 LOAD_FAST 'imeHandle'
    530 LOAD_ATTR 'isCnInput'
    533 CALL_FUNCTION_0 ''
    536 JUMP_IF_FALSE '574'
    539 POP_TOP ''
    540 LOAD_FAST 'GUIDefine'
    543 LOAD_ATTR 'CandidateIsOpen'
    546 JUMP_IF_TRUE '574'
    549 POP_TOP ''
    550 LOAD_FAST 'GUIDefine'
    553 LOAD_ATTR 'CompositionWndIsOpen'
    556 JUMP_IF_TRUE '574'
    559 POP_TOP ''
    560 LOAD_FAST 'isWanNengWB'
    563 JUMP_IF_FALSE '574'
    566 POP_TOP ''
    567 LOAD_FAST 'state'
    570 LOAD_CONST 1
    573 BINARY_AND ''
    574_0 COME_FROM ''
    574_1 COME_FROM ''
    574_2 COME_FROM ''
    574_3 COME_FROM ''
    ....

    From the above bytecode,we know that line 574 is the point that many
    position jumps to.So,it just looks like the 'goto' function in C, but
    we know that there is none such function in Python.
    One 'JUMP**' command is companied with a 'COME_FROM' command,so more
    than one 'COME_FROM' OPs are listed on line 574...

    But ,the question is, I have tried a lot of ways(e.g.for loop,while
    loop and mixed) to re-present 'goto' style bytecodes like this, but
    the result depressed me.
    So,I think maybe it is just a compiler optimization in Python2.5? I'm
    not sure,so I'm appreciated that if anyone can help me.
    higer, Jun 17, 2009
    #1
    1. Advertising

  2. Re: question about a command like 'goto ' in Python's bytecode orit's just a compiler optimization?

    higer schrieb:
    > My Python version is 2.5.2; When I reading the bytecode of some pyc
    > file, I always found that there are many jump command from different
    > position,but to the same position. You can see this situation in
    > following code(this bytecode is just from one .pyc file and I don't
    > have its source .py file):
    >
    > .....
    > 526 POP_TOP ''
    > 527 LOAD_FAST 'imeHandle'
    > 530 LOAD_ATTR 'isCnInput'
    > 533 CALL_FUNCTION_0 ''
    > 536 JUMP_IF_FALSE '574'
    > 539 POP_TOP ''
    > 540 LOAD_FAST 'GUIDefine'
    > 543 LOAD_ATTR 'CandidateIsOpen'
    > 546 JUMP_IF_TRUE '574'
    > 549 POP_TOP ''
    > 550 LOAD_FAST 'GUIDefine'
    > 553 LOAD_ATTR 'CompositionWndIsOpen'
    > 556 JUMP_IF_TRUE '574'
    > 559 POP_TOP ''
    > 560 LOAD_FAST 'isWanNengWB'
    > 563 JUMP_IF_FALSE '574'
    > 566 POP_TOP ''
    > 567 LOAD_FAST 'state'
    > 570 LOAD_CONST 1
    > 573 BINARY_AND ''
    > 574_0 COME_FROM ''
    > 574_1 COME_FROM ''
    > 574_2 COME_FROM ''
    > 574_3 COME_FROM ''
    > ...
    >
    > From the above bytecode,we know that line 574 is the point that many
    > position jumps to.So,it just looks like the 'goto' function in C, but
    > we know that there is none such function in Python.
    > One 'JUMP**' command is companied with a 'COME_FROM' command,so more
    > than one 'COME_FROM' OPs are listed on line 574...
    >
    > But ,the question is, I have tried a lot of ways(e.g.for loop,while
    > loop and mixed) to re-present 'goto' style bytecodes like this, but
    > the result depressed me.
    > So,I think maybe it is just a compiler optimization in Python2.5? I'm
    > not sure,so I'm appreciated that if anyone can help me.


    Getting a depression because of a compiler is a bit strong...

    However, yes, bytecode is similar to assembler, and in that respect
    higher-level control-structures are created using (conditional) jumps.

    The same is true for other bytecode-languages, see here for the JVM:

    http://java.sun.com/docs/books/jvms/second_edition/html/Overview.doc.html#6493

    Diez
    Diez B. Roggisch, Jun 17, 2009
    #2
    1. Advertising

  3. Re: question about a command like 'goto ' in Python's bytecode orit'sjust a compiler optimization?

    "Diez B. Roggisch" <d..@n...m.web.de> wrote:

    > Getting a depression because of a compiler is a bit strong...
    >
    > However, yes, bytecode is similar to assembler, and in that respect
    > higher-level control-structures are created using (conditional) jumps.
    >
    > The same is true for other bytecode-languages, see here for the JVM:
    >
    > http://java.sun.com/docs/books/jvms/second_edition/html/Overview.doc.html#6493


    This is right.

    It is my opinion that it is not possible to make a useful machine,
    virtual or real, which executes instructions sequentially, if the
    instruction set does not contain a conditional jump of some sort.

    I have tried doing it using conditional calls, and it fails on
    the equivalent of the first if ..., elif ... you try to write.

    - Hendrik
    Hendrik van Rooyen, Jun 17, 2009
    #3
  4. higer

    higer Guest

    Re: question about a command like 'goto ' in Python's bytecode orit'sjust a compiler optimization?

    Hi,all:

    I'm sorry that I did not make my question clear. What I mean is that
    what the souce code would look like that will be compiled to such
    bytecodes.


    Regards,
    higer
    higer, Jun 17, 2009
    #4
  5. Re: question about a command like 'goto ' in Python's bytecode orit's just a compiler optimization?

    higer wrote:

    > Hi,all:
    >
    > I'm sorry that I did not make my question clear. What I mean is that
    > what the souce code would look like that will be compiled to such
    > bytecodes.


    >>> import dis
    >>> def foo():

    .... for i in xrange(10):
    .... if i == 5:
    .... break
    .... if i == 4:
    .... continue
    .... i *= 100
    ....
    >>> dis.disassemble(foo.func_code)

    2 0 SETUP_LOOP 68 (to 71)
    3 LOAD_GLOBAL 0 (xrange)
    6 LOAD_CONST 1 (10)
    9 CALL_FUNCTION 1
    12 GET_ITER
    >> 13 FOR_ITER 54 (to 70)

    16 STORE_FAST 0 (i)

    3 19 LOAD_FAST 0 (i)
    22 LOAD_CONST 2 (5)
    25 COMPARE_OP 2 (==)
    28 JUMP_IF_FALSE 5 (to 36)
    31 POP_TOP

    4 32 BREAK_LOOP
    33 JUMP_FORWARD 1 (to 37)
    >> 36 POP_TOP


    5 >> 37 LOAD_FAST 0 (i)
    40 LOAD_CONST 3 (4)
    43 COMPARE_OP 2 (==)
    46 JUMP_IF_FALSE 7 (to 56)
    49 POP_TOP

    6 50 JUMP_ABSOLUTE 13
    53 JUMP_FORWARD 1 (to 57)
    >> 56 POP_TOP


    7 >> 57 LOAD_FAST 0 (i)
    60 LOAD_CONST 4 (100)
    63 INPLACE_MULTIPLY
    64 STORE_FAST 0 (i)
    67 JUMP_ABSOLUTE 13
    >> 70 POP_BLOCK
    >> 71 LOAD_CONST 0 (None)

    74 RETURN_VALUE
    >>>



    Pretty much everything with control-structures.

    Diez
    Diez B. Roggisch, Jun 17, 2009
    #5
  6. higer

    pdpi Guest

    Re: question about a command like 'goto ' in Python's bytecode orit'sjust a compiler optimization?

    On Jun 17, 9:01 am, "Hendrik van Rooyen" <> wrote:
    >  "Diez B. Roggisch" <d..@n...m.web.de> wrote:
    >
    > > Getting a depression because of a compiler is a bit strong...

    >
    > > However, yes, bytecode is similar to assembler, and in that respect
    > > higher-level control-structures are created using (conditional) jumps.

    >
    > > The same is true for other bytecode-languages, see here for the JVM:

    >
    > >http://java.sun.com/docs/books/jvms/second_edition/html/Overview.doc....

    >
    > This is right.
    >
    > It is my opinion that it is not possible to make a useful machine,
    > virtual or real, which executes instructions sequentially, if the
    > instruction set does not contain a conditional jump of some sort.
    >
    > I have tried doing it using conditional calls, and it fails on
    > the equivalent of the first if ..., elif ...  you try to write.
    >
    > - Hendrik


    Not a matter of opinion. One of the requisite elements of a Turing
    Machine is conditional jumping.
    pdpi, Jun 17, 2009
    #6
  7. higer

    John Machin Guest

    Re: question about a command like 'goto ' in Python's bytecode orit's just a compiler optimization?

    On Jun 17, 1:40 pm, higer <> wrote:
    > My Python version is 2.5.2; When I reading the bytecode of some pyc
    > file, I always found that there are many jump command from different
    > position,but to the same position. You can see this situation in
    > following code(this bytecode is just from one .pyc file and I don't
    > have its source .py file):
    >


    Why don't you (a) read the answers you got on stackoverflow to the
    identical question (b) WRITE some code instead of inspecting the
    entrails of the code of others?
    John Machin, Jun 17, 2009
    #7
  8. higer

    higer Guest

    Re: question about a command like 'goto ' in Python's bytecode orit's just a compiler optimization?

    On Jun 17, 8:29 pm, John Machin <> wrote:
    > On Jun 17, 1:40 pm, higer <> wrote:
    >
    > > My Python version is 2.5.2; When I reading the bytecode of some pyc
    > > file, I always found that there are many jump command from different
    > > position,but to the same position. You can see this situation in
    > > following code(this bytecode is just from one .pyc file and I don't
    > > have its source .py file):

    >
    > Why don't you (a) read the answers you got on stackoverflow to the
    > identical question (b) WRITE some code instead of inspecting the
    > entrails of the code of others?


    Thanks, I read the answer just now.
    And thank everbody for your suggestion!
    higer, Jun 17, 2009
    #8
  9. higer

    JanC Guest

    Re: question about a command like 'goto ' in Python's bytecode orit's just a compiler optimization?

    Hendrik van Rooyen wrote:

    > It is my opinion that it is not possible to make a useful machine,
    > virtual or real, which executes instructions sequentially, if the
    > instruction set does not contain a conditional jump of some sort.
    >
    > I have tried doing it using conditional calls, and it fails on
    > the equivalent of the first if ..., elif ... you try to write.


    I'm 99.99% sure you can implement that by using a decision subroutine that
    returns subroutine pointers (and maybe parameter pointers), but it certainly
    won't be efficient on current CPU designs...


    --
    JanC
    JanC, Jun 19, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonathan Neve

    Delphi to bytecode compiler

    Jonathan Neve, Aug 27, 2004, in forum: Java
    Replies:
    3
    Views:
    530
    Kai Thomsen
    Aug 28, 2004
  2. Replies:
    10
    Views:
    529
    Flash Gordon
    Aug 15, 2007
  3. Ravikiran

    Zero Optimization and Sign Optimization???

    Ravikiran, Nov 17, 2008, in forum: C Programming
    Replies:
    22
    Views:
    859
    Thad Smith
    Nov 24, 2008
  4. Rouslan Korneychuk

    a basic bytecode to machine code compiler

    Rouslan Korneychuk, Mar 31, 2011, in forum: Python
    Replies:
    10
    Views:
    432
    Robert Kern
    Apr 3, 2011
  5. Rouslan Korneychuk

    basic bytecode to machine code compiler (part 2)

    Rouslan Korneychuk, May 18, 2011, in forum: Python
    Replies:
    0
    Views:
    202
    Rouslan Korneychuk
    May 18, 2011
Loading...

Share This Page