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

H

higer

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.
 
D

Diez B. Roggisch

higer said:
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
 
H

Hendrik van Rooyen

Diez B. Roggisch said:
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
 
H

higer

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
 
D

Diez B. Roggisch

higer said:
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.
.... for i in xrange(10):
.... if i == 5:
.... break
.... if i == 4:
.... continue
.... i *= 100
.... 2 0 SETUP_LOOP 68 (to 71)
3 LOAD_GLOBAL 0 (xrange)
6 LOAD_CONST 1 (10)
9 CALL_FUNCTION 1
12 GET_ITER 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)
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)
7 >> 57 LOAD_FAST 0 (i)
60 LOAD_CONST 4 (100)
63 INPLACE_MULTIPLY
64 STORE_FAST 0 (i)
67 JUMP_ABSOLUTE 13

Pretty much everything with control-structures.

Diez
 
P

pdpi

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.
 
J

John Machin

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?
 
H

higer

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!
 
J

JanC

Hendrik said:
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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,039
Messages
2,570,375
Members
47,021
Latest member
AleciaMcMa

Latest Threads

Top