Python Virtual Machine Reference

Dave Kuhlman dkuhlman at rexx.com
Wed Feb 18 17:20:37 EST 2004


Kevin Albrecht wrote:

> Fellow Pythonites,
> 
> I am trying to find a reference on the design and structure
> of the Python Virtual Machine, but I can't seem to find one
> anywhere.  In particular, I am looking for a list of all the
> instructions in the Python VM's "assembly language".

The byte codes are defined in the Python Library Reference, sect.
"18.10.1 Python Byte Code Instructions":

    http://www.python.org/doc/current/lib/bytecodes.html

The dis module lets you see what byte codes are generated for a
given piece of code.  Example:

    >>> import tmp
    >>> tmp.test
    <function test at 0x402e35a4>
    >>> import dis
    >>> dir(dis)
    ['EXTENDED_ARG', 'HAVE_ARGUMENT', '__all__', '__builtins__',
'__doc__', '__file__', '__name__', '_test', 'cmp_op', 'dis',
'disassemble', 'disassemble_string', 'disco', 'distb',
'findlabels', 'hascompare', 'hasconst', 'hasfree', 'hasjabs',
'hasjrel', 'haslocal', 'hasname', 'opmap', 'opname', 'sys',
'types']
    >>> dis.dis(tmp.test)
      5           0 LOAD_CONST               1 (2)
                  3 STORE_FAST               0 (count)

      6           6 SETUP_LOOP              22 (to 31)
                  9 LOAD_GLOBAL              1 (sys)
                 12 LOAD_ATTR                2 (path)
                 15 GET_ITER
            >>   16 FOR_ITER                11 (to 30)
                 19 STORE_FAST               1 (path)

      7          22 LOAD_FAST                1 (path)
                 25 PRINT_ITEM
                 26 PRINT_NEWLINE
                 27 JUMP_ABSOLUTE           16
            >>   30 POP_BLOCK
            >>   31 LOAD_CONST               0 (None)
                 34 RETURN_VALUE
    >>>


The interpreter itself is implemented in
Python-2.3.3/Python/ceval.c in the Python source code distribution.
Search for "Interpreter main loop" and "Main switch on opcode".  I
suppose you could use the source as a definition.

The byte code is generated in Python-2.3.3/Python/compile.c

You may also want to look at the Python Library Reference, Sect.
"19. Python compiler package":

    http://www.python.org/doc/current/lib/lib.html

But, I don't understand what the connection is between the AST
(abstract syntax tree) and the byte code interpreter's op-codes.
Maybe someone else can explain that.  Perhaps the real Python
compiler does not use the AST.

Dave

-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
dkuhlman at rexx.com



More information about the Python-list mailing list