简体   繁体   中英

python bytecode instruction study

I am written some programs containing loop function used the dis module to disassemble

import dis
def f():
    a = 10
    while a < 50:
         print a
         a+=1
    while a < 100:
         print a
         a+=1
dis.dis(f)

0 load_const  1 (10)
3 store_fast  0 (a)
6 setup_loop  31 (to 40)
9 load_fast   0 (a)
12 load_const  2(50)
15 compare_op  0(<)
18 pop_jump_if_false 39
21 laod_fast  0(a)
24 print_item 
25 print_newline
26 load_fast 0(a)
29 load_const 3(1)
32 inplace_add 
33store_fast  0(a)
36 jump_absolute 9
39 pop_block 
40 setup_loop  31 (to 74)
43 load_fast 0(a)
46 load_const 4(10)
49 compare_op 0 (<)
52 pop_jump_if_false 73
55 load_fast 0(a)
58 print_item
59 print_newline
60 load_fast 0 (a)
63 load_const 3(1)
66 inplace_add
67 store_fast 0(a)
70 jump_absolute 43
73 pop_block
74 load_const 0(none)
77 return_value

and i see some instruction like load_const, load_name etc.... but i didn't how actually SETUP_LOOP and Function call instructions..... please anyone tell how those are actually work in virtual machine... Is there any link for read?

Assuming you are only interested in how CPython does it' magic, look at Python/ceval.c , more specifically the _PyEval_EvalFrameDefault function. All the opcodes are right there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM