McLightning
McLightning

Reputation: 1

Python virtual machine and bytecode

I am trying to fully understand the structure of PYC file. But I can't understand how FLAG_REF works and what it affects, its meaning.

https://github.com/python/cpython/blob/3.6/Python/marshal.c#L952

The code I used:

hello_world = “Hello, world!”
print(hello_world)

To create the PYC file, I used Python 3.6.8. Here is the disassembled PYC file:

330d 0d0a # magic number
7ae2 4966 # timestamp
3100 0000 # file size

e3 # TYPE_CODE = 'c'
0000 0000 # argcount
0000 0000 # kwonlyargcount
0000 0000 # nlocals 
0200 0000 # stacksize
4000 0000 # flags

73 1000 0000 # TYPE_STRING = 's' and size = 16

6400 5a00 6501 6500 
8301 0100 6401 5300 # code

 0 LOAD_CONST               0 ('Hello, world!')
 2 STORE_NAME               0 (hello_world)
 4 LOAD_NAME                1 (print)
 6 LOAD_NAME                0 (hello_world)
 8 CALL_FUNCTION            1
10 POP_TOP
12 LOAD_CONST               1 (None)
14 RETURN_VALUE

 
2901 # TYPE_TUPLE = '(' and size = 1

7a0d # TYPE_SHORT_ASCII = 'z' and size = 13

48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 - const 'Hello, world!'

4e # TYPE_NONE = 'N'

2902 # TYPE_TUPLE = '(' and size = 2

5a0b # TYPE_SHORT_ASCII_INTERNED = 'Z' and size = 11

68 65 6c 6c 6f 5f 77 6f 72 6c 64 # name 'hello_world'

da05 # TYPE_SHORT_ASCII_INTERNED da xor FLAG_REF(0x80) = 'Z' and size = 5

70 72 69 6e 74 # name 'print'

a900 # TYPE_SMALL_TUPLE = ')' and size = 0

7202 0000 00 # TYPE_REF = 'r'
7202 0000 00 # TYPE_REF = 'r'

fa0a # TYPE_SHORT_ASCII fa xor FLAG_REF(0x80) = 'z' and size = 10

65 78 61 6d 70 6c 65 2e 70 79 # filename 'example.py'

da08 # TYPE_SHORT_ASCII_INTERNED da xor FLAG_REF(0x80) = 'Z' and size = 8

3c 6d 6f 64 75 6c 65 3e # name '<module>'

0100 0000 # firstlineno = 1

7302 0000 # TYPE_STRING = 's' and size = 2

0004 01

Upvotes: 0

Views: 81

Answers (0)

Related Questions