"dis" — Python バイトコードの逆アセンブラ
*****************************************

**ソースコード:** Lib/dis.py

======================================================================

"dis" モジュールは CPython バイトコード (*bytecode*) を逆アセンブルす
ることでバイトコードの解析をサポートします。 このモジュールが入力とし
て受け取る CPython バイトコードはファイル "Include/opcode.h" に定義さ
れており、 コンパイラとインタプリタが使用しています。

**CPython implementation detail:** Bytecode is an implementation
detail of the CPython interpreter!  No guarantees are made that
bytecode will not be added, removed, or changed between versions of
Python.  Use of this module should not be considered to work across
Python VMs or Python releases.

例: 以下の関数 "myfunc()" を考えると

   def myfunc(alist):
       return len(alist)

the following command can be used to get the disassembly of
"myfunc()":

   >>> dis.dis(myfunc)
     2           0 LOAD_GLOBAL              0 (len)
                 3 LOAD_FAST                0 (alist)
                 6 CALL_FUNCTION            1
                 9 RETURN_VALUE

(「2」 は行番号です)。

The "dis" module defines the following functions and constants:

dis.dis([bytesource])

   Disassemble the *bytesource* object. *bytesource* can denote either
   a module, a class, a method, a function, or a code object.  For a
   module, it disassembles all functions.  For a class, it
   disassembles all methods.  For a single code sequence, it prints
   one line per bytecode instruction.  If no object is provided, it
   disassembles the last traceback.

dis.distb([tb])

   Disassembles the top-of-stack function of a traceback, using the
   last traceback if none was passed.  The instruction causing the
   exception is indicated.

dis.disassemble(code[, lasti])

   Disassembles a code object, indicating the last instruction if
   *lasti* was provided.  The output is divided in the following
   columns:

   1. 各行の最初の命令に対する行番号。

   2. 現在の命令。 "-->" として示されます。

   3. ラベル付けされた命令。 ">>" とともに表示されます。

   4. 命令のアドレス。

   5. 命令コード名。

   6. 命令パラメタ。

   7. パラメタの解釈を括弧で囲んだもの。

   パラメタの解釈は、ローカル変数とグローバル変数の名前、定数の値、 分
   岐先、比較命令を認識します。

dis.disco(code[, lasti])

   A synonym for "disassemble()".  It is more convenient to type, and
   kept for compatibility with earlier Python releases.

dis.findlinestarts(code)

   This generator function uses the "co_firstlineno" and "co_lnotab"
   attributes of the code object *code* to find the offsets which are
   starts of lines in the source code.  They are generated as
   "(offset, lineno)" pairs.

dis.findlabels(code)

   ジャンプ先であるコードオブジェクト *code* のすべてのオフセットを 求
   め、これらのオフセットのリストを返します。

dis.opname

   命令コード名のリスト。 バイトコードをインデックスに使って参照できま
   す。

dis.opmap

   命令コード名をバイトコードに対応づける辞書。

dis.cmp_op

   すべての比較命令の名前のリスト。

dis.hasconst

   定数パラメタを持つバイトコードのリスト。

dis.hasfree

   Sequence of bytecodes that access a free variable.

dis.hasname

   名前によって属性にアクセスするバイトコードのリスト。

dis.hasjrel

   相対ジャンプ先を持つバイトコードのリスト。

dis.hasjabs

   絶対ジャンプ先を持つバイトコードのリスト。

dis.haslocal

   ローカル変数にアクセスするバイトコードのリスト。

dis.hascompare

   ブール命令のバイトコードのリスト。


Python バイトコード命令
=======================

現在 Python コンパイラは次のバイトコード命令を生成します。

STOP_CODE()

   Indicates end-of-code to the compiler, not used by the interpreter.

NOP()

   なにもしないコード。バイトコードオプティマイザでプレースホルダとし
   て使われます。

POP_TOP()

   スタックの先頭 (TOS) の要素を取り除きます。

ROT_TWO()

   スタックの先頭の 2 つの要素を入れ替えます。

ROT_THREE()

   スタックの二番目と三番目の要素の位置を 1 つ上げ、先頭を三番目へ下げ
   ます。

ROT_FOUR()

   Lifts second, third and forth stack item one position up, moves top
   down to position four.

DUP_TOP()

   スタックの先頭にある参照の複製を作ります。

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

UNARY_POSITIVE()

   "TOS = +TOS" を実行します。

UNARY_NEGATIVE()

   "TOS = -TOS" を実行します。

UNARY_NOT()

   "TOS = not TOS" を実行します。

UNARY_CONVERT()

   Implements "TOS = `TOS`".

UNARY_INVERT()

   "TOS = ~TOS" を実行します。

GET_ITER()

   "TOS = iter(TOS)" を実行します。

二項命令はスタックの先頭 (TOS) と先頭から二番目の要素をスタックから取
り除きます。 命令を実行し、スタックへ結果をプッシュし戻します。

BINARY_POWER()

   "TOS = TOS1 ** TOS" を実行します。

BINARY_MULTIPLY()

   "TOS = TOS1 * TOS" を実行します。

BINARY_DIVIDE()

   Implements "TOS = TOS1 / TOS" when "from __future__ import
   division" is not in effect.

BINARY_FLOOR_DIVIDE()

   "TOS = TOS1 // TOS" を実行します。

BINARY_TRUE_DIVIDE()

   Implements "TOS = TOS1 / TOS" when "from __future__ import
   division" is in effect.

BINARY_MODULO()

   "TOS = TOS1 % TOS" を実行します。

BINARY_ADD()

   "TOS = TOS1 + TOS" を実行します。

BINARY_SUBTRACT()

   "TOS = TOS1 - TOS" を実行します。

BINARY_SUBSCR()

   "TOS = TOS1[TOS]" を実行します。

BINARY_LSHIFT()

   "TOS = TOS1 << TOS" を実行します。

BINARY_RSHIFT()

   "TOS = TOS1 >> TOS" を実行します。

BINARY_AND()

   "TOS = TOS1 & TOS" を実行します。

BINARY_XOR()

   "TOS = TOS1 ^ TOS" を実行します。

BINARY_OR()

   "TOS = TOS1 | TOS" を実行します。

インプレース命令は TOS と TOS1 を取り除いて結果をスタックへプッシュす
るという点で二項命令と似ています。 しかし、TOS1 がインプレース命令をサ
ポートしている場合には操作が直接 TOS1 に行われます。 また、操作結果の
TOS は (常に同じというわけではありませんが) 元の TOS1 と同じオブジェク
トになることが多いです。

INPLACE_POWER()

   インプレースの "TOS = TOS1 ** TOS" を実行します。

INPLACE_MULTIPLY()

   インプレースの "TOS = TOS1 * TOS" を実行します。

INPLACE_DIVIDE()

   Implements in-place "TOS = TOS1 / TOS" when "from __future__ import
   division" is not in effect.

INPLACE_FLOOR_DIVIDE()

   インプレースの "TOS = TOS1 // TOS" を実行します。

INPLACE_TRUE_DIVIDE()

   Implements in-place "TOS = TOS1 / TOS" when "from __future__ import
   division" is in effect.

INPLACE_MODULO()

   インプレースの "TOS = TOS1 % TOS" を実行します。

INPLACE_ADD()

   インプレースの "TOS = TOS1 + TOS" を実行します。

INPLACE_SUBTRACT()

   インプレースの "TOS = TOS1 - TOS" を実行します。

INPLACE_LSHIFT()

   インプレースの "TOS = TOS1 << TOS" を実行します。

INPLACE_RSHIFT()

   インプレースの "TOS = TOS1 >> TOS" を実行します。

INPLACE_AND()

   インプレースの "TOS = TOS1 & TOS" を実行します。

INPLACE_XOR()

   インプレースの "TOS = TOS1 ^ TOS" を実行します。

INPLACE_OR()

   インプレースの "TOS = TOS1 | TOS" を実行します。

The slice opcodes take up to three parameters.

SLICE+0()

   Implements "TOS = TOS[:]".

SLICE+1()

   Implements "TOS = TOS1[TOS:]".

SLICE+2()

   Implements "TOS = TOS1[:TOS]".

SLICE+3()

   Implements "TOS = TOS2[TOS1:TOS]".

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

STORE_SLICE+0()

   Implements "TOS[:] = TOS1".

STORE_SLICE+1()

   Implements "TOS1[TOS:] = TOS2".

STORE_SLICE+2()

   Implements "TOS1[:TOS] = TOS2".

STORE_SLICE+3()

   Implements "TOS2[TOS1:TOS] = TOS3".

DELETE_SLICE+0()

   Implements "del TOS[:]".

DELETE_SLICE+1()

   Implements "del TOS1[TOS:]".

DELETE_SLICE+2()

   Implements "del TOS1[:TOS]".

DELETE_SLICE+3()

   Implements "del TOS2[TOS1:TOS]".

STORE_SUBSCR()

   "TOS1[TOS] = TOS2" を実行します。

DELETE_SUBSCR()

   "del TOS1[TOS]" を実行します。

Miscellaneous opcodes.

PRINT_EXPR()

   対話モードのための式文を実行します。TOS はスタックから取り除かれ表
   示されます。 非対話モードにおいては、式文は "POP_TOP" で終了してい
   ます。

PRINT_ITEM()

   Prints TOS to the file-like object bound to "sys.stdout".  There is
   one such instruction for each item in the "print" statement.

PRINT_ITEM_TO()

   Like "PRINT_ITEM", but prints the item second from TOS to the file-
   like object at TOS.  This is used by the extended print statement.

PRINT_NEWLINE()

   Prints a new line on "sys.stdout".  This is generated as the last
   operation of a "print" statement, unless the statement ends with a
   comma.

PRINT_NEWLINE_TO()

   Like "PRINT_NEWLINE", but prints the new line on the file-like
   object on the TOS.  This is used by the extended print statement.

BREAK_LOOP()

   "break" 文によってループを終了します。

CONTINUE_LOOP(target)

   "continue" 文によってループを継続します。 *target* はジャンプするア
   ドレスです (アドレスは "FOR_ITER" 命令でなければなりません)。

LIST_APPEND(i)

   Calls "list.append(TOS[-i], TOS)".  Used to implement list
   comprehensions. While the appended value is popped off, the list
   object remains on the stack so that it is available for further
   iterations of the loop.

LOAD_LOCALS()

   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.

RETURN_VALUE()

   関数の呼び出し元へ TOS を返します。

YIELD_VALUE()

   Pops "TOS" and yields it from a *generator*.

IMPORT_STAR()

   "'_'" で始まっていないすべてのシンボルをモジュール TOS から直接ロー
   カル名前空間へロードします。 モジュールはすべての名前をロードした後
   にポップされます。 この命令コードは "from module import *" を実行し
   ます。

EXEC_STMT()

   Implements "exec TOS2,TOS1,TOS".  The compiler fills missing
   optional parameters with "None".

POP_BLOCK()

   ブロックスタックからブロックを一つ取り除きます。 フレームごとにブロ
   ックのスタックがあり、ネストしたループや try 文などを表しています。

END_FINALLY()

   "finally" 節を終了します。 インタプリタは例外を再送出しなければなら
   ないかどうか、あるいは、 関数から return して外側の次のブロックに続
   くかどうかを再度判断します。

BUILD_CLASS()

   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.

SETUP_WITH(delta)

   この命令コードは、with ブロックが開始する前にいくつかの命令を行いま
   す。 まず、コンテキストマネージャから "__exit__()" をロードし、 後
   から "WITH_CLEANUP" で使うためにスタックにプッシュします。 そして、
   "__enter__()" が呼び出され、 *delta* を指す finally ブロックがプッ
   シュされます。最後に、enter メソッドを呼び出した 結果がスタックにプ
   ッシュされます。次の命令コードはこれを無視 ("POP_TOP") するか、変数
   に保存 ("STORE_FAST", "STORE_NAME", または "UNPACK_SEQUENCE") しま
   す。

WITH_CLEANUP()

   Cleans up the stack when a "with" statement block exits.  On top of
   the stack are 1–3 values indicating how/why the finally clause was
   entered:

   * TOP = "None"

   * (TOP, SECOND) = ("WHY_{RETURN,CONTINUE}"), retval

   * TOP = "WHY_*"; no retval below it

   * (TOP, SECOND, THIRD) = exc_info()

   Under them is EXIT, the context manager’s "__exit__()" bound
   method.

   In the last case, "EXIT(TOP, SECOND, THIRD)" is called, otherwise
   "EXIT(None, None, None)".

   EXIT is removed from the stack, leaving the values above it in the
   same order. In addition, if the stack represents an exception,
   *and* the function call returns a 『true』 value, this information
   is 「zapped」, to prevent "END_FINALLY" from re-raising the
   exception.  (But non-local gotos should still be resumed.)

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

STORE_NAME(namei)

   Implements "name = TOS". *namei* is the index of *name* in the
   attribute "co_names" of the code object. The compiler tries to use
   "STORE_FAST" or "STORE_GLOBAL" if possible.

DELETE_NAME(namei)

   "del name" を実行します。 *namei* はコードオブジェクトの "co_names"
   属性へのインデックスです。

UNPACK_SEQUENCE(count)

   TOS を *count* 個の個別の値にアンパックして、右から左の順にスタック
   に積みます。

DUP_TOPX(count)

   Duplicate *count* items, keeping them in the same order. Due to
   implementation limits, *count* should be between 1 and 5 inclusive.

STORE_ATTR(namei)

   "TOS.name = TOS1" を実行します。 *namei* は "co_names" における名前
   のインデックスです。

DELETE_ATTR(namei)

   "del TOS.name" を実行します。 "co_names" へのインデックスとして
   *namei* を使います。

STORE_GLOBAL(namei)

   Works as "STORE_NAME", but stores the name as a global.

DELETE_GLOBAL(namei)

   Works as "DELETE_NAME", but deletes a global name.

LOAD_CONST(consti)

   "co_consts[consti]" をスタックにプッシュします。

LOAD_NAME(namei)

   "co_names[namei]" に関連付けられた値をスタックにプッシュします。

BUILD_TUPLE(count)

   スタックから *count* 個の要素を消費してタプルを作り出し、できたタプ
   ルをスタックにプッシュします。

BUILD_LIST(count)

   Works as "BUILD_TUPLE", but creates a list.

BUILD_SET(count)

   Works as "BUILD_TUPLE", but creates a set.

   バージョン 2.7 で追加.

BUILD_MAP(count)

   Pushes a new dictionary object onto the stack.  The dictionary is
   pre-sized to hold *count* entries.

LOAD_ATTR(namei)

   TOS を "getattr(TOS, co_names[namei])" と入れ替えます。

COMPARE_OP(opname)

   ブール命令を実行します。命令名は "cmp_op[opname]" にあります。

IMPORT_NAME(namei)

   Imports the module "co_names[namei]".  TOS and TOS1 are popped and
   provide the *fromlist* and *level* arguments of "__import__()".
   The module object is pushed onto the stack.  The current namespace
   is not affected: for a proper import statement, a subsequent
   "STORE_FAST" instruction modifies the namespace.

IMPORT_FROM(namei)

   Loads the attribute "co_names[namei]" from the module found in TOS.
   The resulting object is pushed onto the stack, to be subsequently
   stored by a "STORE_FAST" instruction.

JUMP_FORWARD(delta)

   バイトコードカウンタを *delta* だけ増加させます。

POP_JUMP_IF_TRUE(target)

   TOS が真ならば、バイトコードカウンタを *target* に設定します。 TOS
   はポップされます。

POP_JUMP_IF_FALSE(target)

   TOS が偽ならば、バイトコードカウンタを *target* に設定します。 TOS
   はポップされます。

JUMP_IF_TRUE_OR_POP(target)

   TOS が真ならば、バイトコードカウンタを *target* に設定し、TOS は ス
   タックに残されます。そうでない (TOS が偽) なら、TOS はポップされま
   す。

JUMP_IF_FALSE_OR_POP(target)

   TOS が偽ならば、バイトコードカウンタを *target* に設定し、TOS は ス
   タックに残されます。そうでない (TOS が真) なら、TOS はポップされま
   す。

JUMP_ABSOLUTE(target)

   バイトコードカウンタを *target* に設定します。

FOR_ITER(delta)

   "TOS" is an *iterator*.  Call its "next()" method.  If this yields
   a new value, push it on the stack (leaving the iterator below it).
   If the iterator indicates it is exhausted "TOS" is popped, and the
   bytecode counter is incremented by *delta*.

LOAD_GLOBAL(namei)

   "co_names[namei]" という名前のグローバルをスタック上にロードします
   。

SETUP_LOOP(delta)

   ループのためのブロックをブロックスタックにプッシュします。 ブロック
   は現在の命令から *delta* バイトの大きさを占めます。

SETUP_EXCEPT(delta)

   try-except 節から try ブロックをブロックスタックにプッシュします。
   *delta* は最初の except ブロックを指します。

SETUP_FINALLY(delta)

   try-except 節から try ブロックをブロックスタックにプッシュします。
   *delta* は finally ブロックを指します。

STORE_MAP()

   Store a key and value pair in a dictionary.  Pops the key and value
   while leaving the dictionary on the stack.

LOAD_FAST(var_num)

   ローカルな "co_varnames[var_num]" への参照をスタックにプッシュしま
   す。

STORE_FAST(var_num)

   TOS をローカルな "co_varnames[var_num]" の中に保存します。

DELETE_FAST(var_num)

   ローカルな "co_varnames[var_num]" を削除します。

LOAD_CLOSURE(i)

   セルと自由変数の記憶領域のスロット *i* に含まれるセルへの参照をプッ
   シュします。 *i* が *co_cellvars* の長さより小さければ、変数の名前
   は "co_cellvars[i]" です。 そうでなければ "co_freevars[i -
   len(co_cellvars)]" です。

LOAD_DEREF(i)

   セルと自由変数の記憶領域のスロット *i* に含まれるセルをロードします
   。 セルが持つオブジェクトへの参照をスタックにプッシュします。

STORE_DEREF(i)

   セルと自由変数の記憶領域のスロット *i* に含まれるセルへTOSを保存し
   ます。

SET_LINENO(lineno)

   This opcode is obsolete.

RAISE_VARARGS(argc)

   例外を発生させます。 *argc* は raise 文へ与えるパラメタの数を 0 か
   ら 3 の 範囲で示します。 ハンドラは TOS2 をトレースバック、TOS1 を
   パラメタ、TOS を例外として探します。

CALL_FUNCTION(argc)

   Calls a function.  The low byte of *argc* 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.  Pops all function
   arguments, and the function itself off the stack, and pushes the
   return value.

MAKE_FUNCTION(argc)

   Pushes a new function object on the stack.  TOS is the code
   associated with the function.  The function object is defined to
   have *argc* default parameters, which are found below TOS.

MAKE_CLOSURE(argc)

   Creates a new function object, sets its *func_closure* slot, and
   pushes it on the stack.  TOS is the code associated with the
   function, TOS1 the tuple containing cells for the closure’s free
   variables.  The function also has *argc* default parameters, which
   are found below the cells.

BUILD_SLICE(argc)

   スライスオブジェクトをスタックにプッシュします。 *argc* は2あるいは
   3でなければなりません。 2 ならば "slice(TOS1, TOS)" がプッシュされ
   ます。 3 ならば "slice(TOS2, TOS1, TOS)" がプッシュされます。 これ
   以上の情報については、 "slice()" 組み込み関数を参照してください。

EXTENDED_ARG(ext)

   デフォルトの 2 バイトに収まりきらない大きな引数を持つあらゆる命令コ
   ードの前に置かれます。 *ext* は追加の 2 バイトを保持し、後続の命令
   コードの引数と組み合わされます。 それらは 4 バイト引数を構成し、
   *ext* はその最上位バイトです。

CALL_FUNCTION_VAR(argc)

   Calls a function. *argc* is interpreted as in "CALL_FUNCTION". The
   top element on the stack contains the variable argument list,
   followed by keyword and positional arguments.

CALL_FUNCTION_KW(argc)

   Calls a function. *argc* is interpreted as in "CALL_FUNCTION". The
   top element on the stack contains the keyword arguments dictionary,
   followed by explicit keyword and positional arguments.

CALL_FUNCTION_VAR_KW(argc)

   Calls a function. *argc* is interpreted as in "CALL_FUNCTION".  The
   top element on the stack contains the keyword arguments dictionary,
   followed by the variable-arguments tuple, followed by explicit
   keyword and positional arguments.

HAVE_ARGUMENT()

   This is not really an opcode.  It identifies the dividing line
   between opcodes which don’t take arguments "< HAVE_ARGUMENT" and
   those which do ">= HAVE_ARGUMENT".
