<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>3.25.1 Python Byte Code Instructions</TITLE>
<META NAME="description" CONTENT="3.25.1 Python Byte Code Instructions">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="lib.css">
<LINK REL="previous" HREF="module-dis.html">
<LINK REL="up" HREF="module-dis.html">
<LINK REL="next" HREF="module-site.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="module-site.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="module-dis.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-dis.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="module-site.html">3.26 site  </A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="module-dis.html">3.25 dis  </A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-dis.html">3.25 dis  </A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H2><A NAME="SECTION0052510000000000000000"></A>
<BR>
3.25.1 Python Byte Code Instructions
</H2>

<P>
The Python compiler currently generates the following byte code
instructions.

<P>

<P>
<dl><dt><b><tt class=opcode>STOP_CODE</tt></b>
<dd>
Indicates end-of-code to the compiler, not used by the interpreter.
</dl>

<P>
<dl><dt><b><tt class=opcode>POP_TOP</tt></b>
<dd>
Removes the top-of-stack (TOS) item.
</dl>

<P>
<dl><dt><b><tt class=opcode>ROT_TWO</tt></b>
<dd>
Swaps the two top-most stack items.
</dl>

<P>
<dl><dt><b><tt class=opcode>ROT_THREE</tt></b>
<dd>
Lifts second and third stack item one position up, moves top down
to position three.
</dl>

<P>
<dl><dt><b><tt class=opcode>DUP_TOP</tt></b>
<dd>
Duplicates the reference on top of the stack.
</dl>

<P>
Unary Operations take the top of the stack, apply the operation, and
push the result back on the stack.

<P>
<dl><dt><b><tt class=opcode>UNARY_POSITIVE</tt></b>
<dd>
Implements <tt>TOS = +TOS</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>UNARY_NEG</tt></b>
<dd>
Implements <tt>TOS = -TOS</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>UNARY_NOT</tt></b>
<dd>
Implements <tt>TOS = not TOS</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>UNARY_CONVERT</tt></b>
<dd>
Implements <tt>TOS = `TOS`</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>UNARY_INVERT</tt></b>
<dd>
Implements <tt>TOS = TOS</tt>.
</dl>

<P>
Binary operations remove the top of the stack (TOS) and the second top-most
stack item (TOS1) from the stack.  They perform the operation, and put the
result back on the stack.

<P>
<dl><dt><b><tt class=opcode>BINARY_POWER</tt></b>
<dd>
Implements <tt>TOS = TOS1 ** TOS</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>BINARY_MULTIPLY</tt></b>
<dd>
Implements <tt>TOS = TOS1 * TOS</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>BINARY_DIVIDE</tt></b>
<dd>
Implements <tt>TOS = TOS1 / TOS</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>BINARY_MODULO</tt></b>
<dd>
Implements <tt>TOS = TOS1 % TOS</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>BINARY_ADD</tt></b>
<dd>
Implements <tt>TOS = TOS1 + TOS</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>BINARY_SUBTRACT</tt></b>
<dd>
Implements <tt>TOS = TOS1 - TOS</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>BINARY_SUBSCR</tt></b>
<dd>
Implements <tt>TOS = TOS1[TOS]</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>BINARY_LSHIFT</tt></b>
<dd>
Implements <tt>TOS = TOS1 &lt;&lt; TOS</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>BINARY_RSHIFT</tt></b>
<dd>
Implements <tt>TOS = TOS1 &gt;&gt; TOS</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>BINARY_AND</tt></b>
<dd>
Implements <tt>TOS = TOS1 and TOS</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>BINARY_XOR</tt></b>
<dd>
Implements <tt>TOS = TOS1 ^ TOS</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>BINARY_OR</tt></b>
<dd>
Implements <tt>TOS = TOS1 or TOS</tt>.
</dl>

<P>
The slice opcodes take up to three parameters.

<P>
<dl><dt><b><tt class=opcode>SLICE+0</tt></b>
<dd>
Implements <tt>TOS = TOS[:]</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>SLICE+1</tt></b>
<dd>
Implements <tt>TOS = TOS1[TOS:]</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>SLICE+2</tt></b>
<dd>
Implements <tt>TOS = TOS1[:TOS1]</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>SLICE+3</tt></b>
<dd>
Implements <tt>TOS = TOS2[TOS1:TOS]</tt>.
</dl>

<P>
Slice assignment needs even an additional parameter.  As any statement,
they put nothing on the stack.

<P>
<dl><dt><b><tt class=opcode>STORE_SLICE+0</tt></b>
<dd>
Implements <tt>TOS[:] = TOS1</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>STORE_SLICE+1</tt></b>
<dd>
Implements <tt>TOS1[TOS:] = TOS2</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>STORE_SLICE+2</tt></b>
<dd>
Implements <tt>TOS1[:TOS] = TOS2</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>STORE_SLICE+3</tt></b>
<dd>
Implements <tt>TOS2[TOS1:TOS] = TOS3</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>DELETE_SLICE+0</tt></b>
<dd>
Implements <tt>del TOS[:]</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>DELETE_SLICE+1</tt></b>
<dd>
Implements <tt>del TOS1[TOS:]</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>DELETE_SLICE+2</tt></b>
<dd>
Implements <tt>del TOS1[:TOS]</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>DELETE_SLICE+3</tt></b>
<dd>
Implements <tt>del TOS2[TOS1:TOS]</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>STORE_SUBSCR</tt></b>
<dd>
Implements <tt>TOS1[TOS] = TOS2</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>DELETE_SUBSCR</tt></b>
<dd>
Implements <tt>del TOS1[TOS]</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>PRINT_EXPR</tt></b>
<dd>
Implements the expression statement for the interactive mode.  TOS is
removed from the stack and printed.  In non-interactive mode, an
expression statement is terminated with <tt>POP_STACK</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>PRINT_ITEM</tt></b>
<dd>
Prints TOS.  There is one such instruction for
each item in the print statement.
</dl>

<P>
<dl><dt><b><tt class=opcode>PRINT_NEWLINE</tt></b>
<dd>
Prints a new line on <tt>sys.stdout</tt>.  This is generated as the
last operation of a print statement, unless the statement ends
with a comma.
</dl>

<P>
<dl><dt><b><tt class=opcode>BREAK_LOOP</tt></b>
<dd>
Terminates a loop due to a break statement.
</dl>

<P>
<dl><dt><b><tt class=opcode>LOAD_LOCALS</tt></b>
<dd>
Pushes a reference to the locals of the current scope on the stack.
This is used in the code for a class definition: After the class body
is evaluated, the locals are passed to the class definition.
</dl>

<P>
<dl><dt><b><tt class=opcode>RETURN_VALUE</tt></b>
<dd>
Returns with TOS to the caller of the function.
</dl>

<P>
<dl><dt><b><tt class=opcode>EXEC_STMT</tt></b>
<dd>
Implements <tt>exec TOS2,TOS1,TOS</tt>.  The compiler fills
missing optional parameters with None.
</dl>

<P>
<dl><dt><b><tt class=opcode>POP_BLOCK</tt></b>
<dd>
Removes one block from the block stack.  Per frame, there is a 
stack of blocks, denoting nested loops, try statements, and such.
</dl>

<P>
<dl><dt><b><tt class=opcode>END_FINALLY</tt></b>
<dd>
Terminates a finally-block.  The interpreter recalls whether the
exception has to be re-raised, or whether the function returns,
and continues with the outer-next block.
</dl>

<P>
<dl><dt><b><tt class=opcode>BUILD_CLASS</tt></b>
<dd>
Creates a new class object.  TOS is the methods dictionary, TOS1
the tuple of the names of the base classes, and TOS2 the class name.
</dl>

<P>
All of the following opcodes expect arguments.  An argument is two
bytes, with the more significant byte last.

<P>
<dl><dt><b><tt class=opcode>STORE_NAME</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>namei</var>
<dd>
Implements <tt>name = TOS</tt>. <var>namei</var> is the index of <var>name</var>
in the attribute <tt class=member>co_names</tt> of the code object.
The compiler tries to use <tt>STORE_LOCAL</tt> or <tt>STORE_GLOBAL</tt>if possible.
</dl>

<P>
<dl><dt><b><tt class=opcode>DELETE_NAME</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>namei</var>
<dd>
Implements <tt>del name</tt>, where <var>namei</var> is the index into
<tt class=member>co_names</tt> attribute of the code object.
</dl>

<P>
<dl><dt><b><tt class=opcode>UNPACK_TUPLE</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>count</var>
<dd>
Unpacks TOS into <var>count</var> individual values, which are put onto
the stack right-to-left.
</dl>

<P>
<dl><dt><b><tt class=opcode>UNPACK_LIST</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>count</var>
<dd>
Unpacks TOS into <var>count</var> individual values.
</dl>

<P>
<dl><dt><b><tt class=opcode>STORE_ATTR</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>namei</var>
<dd>
Implements <tt>TOS.name = TOS1</tt>, where <var>namei</var> is the index
of name in <tt class=member>co_names</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>DELETE_ATTR</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>namei</var>
<dd>
Implements <tt>del TOS.name</tt>, using <var>namei</var> as index into
<tt class=member>co_names</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>STORE_GLOBAL</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>namei</var>
<dd>
Works as <tt>STORE_NAME</tt>, but stores the name as a global.
</dl>

<P>
<dl><dt><b><tt class=opcode>DELETE_GLOBAL</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>namei</var>
<dd>
Works as <tt>DELETE_NAME</tt>, but deletes a global name.
</dl>

<P>
<dl><dt><b><tt class=opcode>LOAD_CONST</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>consti</var>
<dd>
Pushes "<tt class=samp>co_consts[<var>consti</var>]</tt>" onto the stack.
</dl>

<P>
<dl><dt><b><tt class=opcode>LOAD_NAME</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>namei</var>
<dd>
Pushes the value associated with "<tt class=samp>co_names[<var>namei</var>]</tt>" onto the stack.
</dl>

<P>
<dl><dt><b><tt class=opcode>BUILD_TUPLE</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>count</var>
<dd>
Creates a tuple consuming <var>count</var> items from the stack, and pushes
the resulting tuple onto the stack.
</dl>

<P>
<dl><dt><b><tt class=opcode>BUILD_LIST</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>count</var>
<dd>
Works as <tt>BUILD_TUPLE</tt>, but creates a list.
</dl>

<P>
<dl><dt><b><tt class=opcode>BUILD_MAP</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>zero</var>
<dd>
Pushes an empty dictionary object onto the stack.  The argument is ignored
and set to zero by the compiler.
</dl>

<P>
<dl><dt><b><tt class=opcode>LOAD_ATTR</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>namei</var>
<dd>
Replaces TOS with <tt>getattr(TOS,co_names[<var>namei</var>]</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>COMPARE_OP</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>opname</var>
<dd>
Performs a boolean operation.  The operation name can be found
in <tt>cmp_op[<var>opname</var>]</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>IMPORT_NAME</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>namei</var>
<dd>
Imports the module <tt>co_names[<var>namei</var>]</tt>.  The module object is
pushed onto the stack.  The current name space is not affected: for a
proper import statement, a subsequent <tt>STORE_FAST</tt> instruction
modifies the name space.
</dl>

<P>
<dl><dt><b><tt class=opcode>IMPORT_FROM</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>namei</var>
<dd>
Imports the attribute <tt>co_names[<var>namei</var>]</tt>.  The module to import
from is found in TOS and left there.
</dl>

<P>
<dl><dt><b><tt class=opcode>JUMP_FORWARD</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>delta</var>
<dd>
Increments byte code counter by <var>delta</var>.
</dl>

<P>
<dl><dt><b><tt class=opcode>JUMP_IF_TRUE</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>delta</var>
<dd>
If TOS is true, increment the byte code counter by <var>delta</var>.  TOS is
left on the stack.
</dl>

<P>
<dl><dt><b><tt class=opcode>JUMP_IF_FALSE</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>delta</var>
<dd>
If TOS is false, increment the byte code counter by <var>delta</var>.  TOS
is not changed. 
</dl>

<P>
<dl><dt><b><tt class=opcode>JUMP_ABSOLUTE</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>target</var>
<dd>
Set byte code counter to <var>target</var>.
</dl>

<P>
<dl><dt><b><tt class=opcode>FOR_LOOP</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>delta</var>
<dd>
Iterate over a sequence.  TOS is the current index, TOS1 the sequence.
First, the next element is computed.  If the sequence is exhausted,
increment byte code counter by <var>delta</var>.  Otherwise, push the
sequence, the incremented counter, and the current item onto the stack.
</dl>

<P>
<dl><dt><b><tt class=opcode>LOAD_GLOBAL</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>namei</var>
<dd>
Loads the global named <tt>co_names[<var>namei</var>]</tt> onto the stack.
</dl>

<P>
<dl><dt><b><tt class=opcode>SETUP_LOOP</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>delta</var>
<dd>
Pushes a block for a loop onto the block stack.  The block spans
from the current instruction with a size of <var>delta</var> bytes.
</dl>

<P>
<dl><dt><b><tt class=opcode>SETUP_EXCEPT</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>delta</var>
<dd>
Pushes a try block from a try-except clause onto the block stack.
<var>delta</var> points to the first except block.
</dl>

<P>
<dl><dt><b><tt class=opcode>SETUP_FINALLY</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>delta</var>
<dd>
Pushes a try block from a try-except clause onto the block stack.
<var>delta</var> points to the finally block.
</dl>

<P>
<dl><dt><b><tt class=opcode>LOAD_FAST</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>var_num</var>
<dd>
Pushes a reference to the local <tt>co_varnames[<var>var_num</var>]</tt> onto
the stack.
</dl>

<P>
<dl><dt><b><tt class=opcode>STORE_FAST</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>var_num</var>
<dd>
Stores TOS into the local <tt>co_varnames[<var>var_num</var>]</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>DELETE_FAST</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>var_num</var>
<dd>
Deletes local <tt>co_varnames[<var>var_num</var>]</tt>.
</dl>

<P>
<dl><dt><b><tt class=opcode>SET_LINE_NO</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>lineno</var>
<dd>
Sets the current line number to <var>lineno</var>.
</dl>

<P>
<dl><dt><b><tt class=opcode>RAISE_VARARGS</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>argc</var>
<dd>
Raises an exception. <var>argc</var> indicates the number of parameters
to the raise statement, ranging from 1 to 3.  The handler will find
the traceback as TOS2, the parameter as TOS1, and the exception
as TOS.
</dl>

<P>
<dl><dt><b><tt class=opcode>CALL_FUNCTION</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>argc</var>
<dd>
Calls a function.  The low byte of <var>argc</var> indicates the number of
positional parameters, the high byte the number of keyword parameters.
On the stack, the opcode finds the keyword parameters first.  For each
keyword argument, the value is on top of the key.  Below the keyword
parameters, the positional parameters are on the stack, with the
right-most parameter on top.  Below the parameters, the function object
to call is on the stack.
</dl>

<P>
<dl><dt><b><tt class=opcode>MAKE_FUNCTION</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>argc</var>
<dd>
Pushes a new function object on the stack.  TOS is the code associated
with the function.  The function object is defined to have <var>argc</var>
default parameters, which are found below TOS.
</dl>

<P>
<dl><dt><b><tt class=opcode>BUILD_SLICE</tt></b>&nbsp;&nbsp;&nbsp;&nbsp;<var>argc</var>
<dd>
Pushes a slice object on the stack.  <var>argc</var> must be 2 or 3.  If it
is 2, <tt>slice(TOS1, TOS)</tt> is pushed; if it is 3,
<tt>slice(TOS2, TOS1, TOS)</tt> is pushed.
See the <tt>slice()</tt><a name="l2h-566"></a> built-in function.
</dl><p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="module-site.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="module-dis.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-dis.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="module-site.html">3.26 site  </A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="module-dis.html">3.25 dis  </A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-dis.html">3.25 dis  </A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
