module documentation

Functions for computing the execution order of bytecode.

Class Block A block is a node in a directed graph.
Class CollectAnnotationTargetsVisitor Collect opcodes that might have annotations attached.
Class DisCodeVisitor Visitor for disassembling code into Opcode objects.
Class OrderCodeVisitor Visitor for recursively changing all CodeType to OrderedCode.
Class OrderedCode Code object which knows about instruction ordering.
Function add_pop_block_targets Modifies bytecode so that each POP_BLOCK has a block_target.
Function compute_order Split bytecode into blocks and order the blocks.
Function merge_annotations Merges type comments into their associated opcodes.
Function order_code Split a CodeType object into ordered blocks.
Function process_code Undocumented
Constant CODE_LOADING_OPCODES Undocumented
Constant STORE_OPCODES Undocumented
Function _is_function_def Helper function for CollectFunctionTypeCommentTargetsVisitor.
Function _split_bytecode Given a sequence of bytecodes, return basic blocks.
def add_pop_block_targets(bytecode, python_version): (source)

Modifies bytecode so that each POP_BLOCK has a block_target. This is to achieve better initial ordering of try/except and try/finally code. try: i = 1 a[i] except IndexError: return i By connecting a CFG edge from the end of the block (after the "a[i]") to the except handler, our basic block ordering algorithm knows that the except block needs to be scheduled last, whereas if there only was an edge before the "i = 1", it would be able to schedule it too early and thus encounter an undefined variable. This is only for ordering. The actual analysis of the code happens later, in vm.py. Args: bytecode: An array of bytecodes. python_version: The target python version.

def compute_order(bytecode): (source)

Split bytecode into blocks and order the blocks. This builds an "ancestor first" ordering of the basic blocks of the bytecode. Args: bytecode: A list of instances of opcodes.Opcode. (E.g. returned from opcodes.dis()) Returns: A list of Block instances.

def merge_annotations(code, annotations): (source)

Merges type comments into their associated opcodes. Modifies code in place. Args: code: An OrderedCode object. annotations: A map of lines to annotations. Returns: The code with annotations added to the relevant opcodes.

def order_code(code, python_version): (source)

Split a CodeType object into ordered blocks. This takes a CodeType object (i.e., a piece of compiled Python code) and splits it into ordered basic blocks. Args: code: A loadmarshal.CodeType object. python_version: The target python version. Returns: A CodeBlocks instance.

def process_code(code, python_version): (source)

Undocumented

CODE_LOADING_OPCODES = (source)

Undocumented

Value
(opcodes.LOAD_CONST)
def _is_function_def(fn_code): (source)

Helper function for CollectFunctionTypeCommentTargetsVisitor.

def _split_bytecode(bytecode): (source)

Given a sequence of bytecodes, return basic blocks. This will split the code at "basic block boundaries". These occur at every instruction that is jumped to, and after every instruction that jumps somewhere else (or returns / aborts). Args: bytecode: A list of instances of opcodes.Opcode. (E.g. returned from opcodes.dis()) Returns: A list of _Block instances.