Author: Philip Jenvey <[email protected]>
Branch:
Changeset: r74722:290d6ee4dc2d
Date: 2014-11-25 18:22 -0800
http://bitbucket.org/pypy/pypy/changeset/290d6ee4dc2d/
Log: cleanup/arrange imports
diff --git a/pypy/interpreter/astcompiler/assemble.py
b/pypy/interpreter/astcompiler/assemble.py
--- a/pypy/interpreter/astcompiler/assemble.py
+++ b/pypy/interpreter/astcompiler/assemble.py
@@ -1,15 +1,13 @@
-"""
-Python control flow graph generation and bytecode assembly.
-"""
+"""Python control flow graph generation and bytecode assembly."""
-from pypy.interpreter.astcompiler import ast, symtable, misc
-from pypy.interpreter import pycode
+from rpython.rlib import rfloat
+from rpython.rlib.objectmodel import we_are_translated
+
+from pypy.interpreter.astcompiler import ast, misc, symtable
+from pypy.interpreter.error import OperationError
+from pypy.interpreter.pycode import PyCode
from pypy.tool import stdlib_opcode as ops
-from pypy.interpreter.error import OperationError
-from rpython.rlib.objectmodel import we_are_translated
-from rpython.rlib import rfloat
-
class Instruction(object):
"""Represents a single opcode."""
@@ -21,14 +19,12 @@
self.has_jump = False
def size(self):
- """Return the size of bytes of this instruction when it is encoded."""
+ """Return the size of bytes of this instruction when it is
+ encoded.
+ """
if self.opcode >= ops.HAVE_ARGUMENT:
- if self.arg > 0xFFFF:
- return 6
- else:
- return 3
- else:
- return 1
+ return (6 if self.arg > 0xFFFF else 3)
+ return 1
def jump_to(self, target, absolute=False):
"""Indicate the target this jump instruction.
@@ -54,9 +50,9 @@
class Block(object):
"""A basic control flow block.
- It has one entry point and several possible exit points. Its instructions
- may be jumps to other blocks, or if control flow reaches the end of the
- block, it continues to next_block.
+ It has one entry point and several possible exit points. Its
+ instructions may be jumps to other blocks, or if control flow
+ reaches the end of the block, it continues to next_block.
"""
def __init__(self):
@@ -71,10 +67,10 @@
stack.append(nextblock)
def post_order(self):
- """Return this block and its children in post order.
- This means that the graph of blocks is first cleaned up to
- ignore back-edges, thus turning it into a DAG. Then the DAG
- is linearized. For example:
+ """Return this block and its children in post order. This means
+ that the graph of blocks is first cleaned up to ignore
+ back-edges, thus turning it into a DAG. Then the DAG is
+ linearized. For example:
A --> B -\ => [A, D, B, C]
\-> D ---> C
@@ -105,7 +101,9 @@
return resultblocks
def code_size(self):
- """Return the encoded size of all the instructions in this block."""
+ """Return the encoded size of all the instructions in this
+ block.
+ """
i = 0
for instr in self.instructions:
i += instr.size()
@@ -141,6 +139,7 @@
i += 1
return result
+
def _list_to_dict(l, offset=0):
result = {}
index = offset
@@ -300,11 +299,11 @@
def _resolve_block_targets(self, blocks):
"""Compute the arguments of jump instructions."""
last_extended_arg_count = 0
- # The reason for this loop is extended jumps. EXTENDED_ARG extends the
- # bytecode size, so it might invalidate the offsets we've already
given.
- # Thus we have to loop until the number of extended args is stable.
Any
- # extended jump at all is extremely rare, so performance is not too
- # concerning.
+ # The reason for this loop is extended jumps. EXTENDED_ARG
+ # extends the bytecode size, so it might invalidate the offsets
+ # we've already given. Thus we have to loop until the number of
+ # extended args is stable. Any extended jump at all is
+ # extremely rare, so performance is not too concerning.
while True:
extended_arg_count = 0
offset = 0
@@ -330,7 +329,8 @@
instr.opcode = ops.JUMP_ABSOLUTE
absolute = True
elif target_op == ops.RETURN_VALUE:
- # Replace JUMP_* to a RETURN into just a
RETURN
+ # Replace JUMP_* to a RETURN into
+ # just a RETURN
instr.opcode = ops.RETURN_VALUE
instr.arg = 0
instr.has_jump = False
@@ -345,7 +345,8 @@
instr.arg = jump_arg
if jump_arg > 0xFFFF:
extended_arg_count += 1
- if extended_arg_count == last_extended_arg_count and not
force_redo:
+ if (extended_arg_count == last_extended_arg_count and
+ not force_redo):
break
else:
last_extended_arg_count = extended_arg_count
@@ -360,7 +361,7 @@
while True:
try:
w_key = space.next(w_iter)
- except OperationError, e:
+ except OperationError as e:
if not e.match(space, space.w_StopIteration):
raise
break
@@ -435,15 +436,16 @@
continue
addr = offset - current_off
# Python assumes that lineno always increases with
- # increasing bytecode address (lnotab is unsigned char).
- # Depending on when SET_LINENO instructions are emitted
this
- # is not always true. Consider the code:
+ # increasing bytecode address (lnotab is unsigned
+ # char). Depending on when SET_LINENO instructions
+ # are emitted this is not always true. Consider the
+ # code:
# a = (1,
# b)
- # In the bytecode stream, the assignment to "a" occurs
after
- # the loading of "b". This works with the C Python
compiler
- # because it only generates a SET_LINENO instruction for
the
- # assignment.
+ # In the bytecode stream, the assignment to "a"
+ # occurs after the loading of "b". This works with
+ # the C Python compiler because it only generates a
+ # SET_LINENO instruction for the assignment.
if line or addr:
while addr > 255:
push(chr(255))
@@ -486,22 +488,22 @@
free_names = _list_from_dict(self.free_vars, len(cell_names))
flags = self._get_code_flags() | self.compile_info.flags
bytecode = ''.join([block.get_code() for block in blocks])
- return pycode.PyCode(self.space,
- self.argcount,
- len(self.var_names),
- stack_depth,
- flags,
- bytecode,
- list(consts_w),
- names,
- var_names,
- self.compile_info.filename,
- self.name,
- self.first_lineno,
- lnotab,
- free_names,
- cell_names,
- self.compile_info.hidden_applevel)
+ return PyCode(self.space,
+ self.argcount,
+ len(self.var_names),
+ stack_depth,
+ flags,
+ bytecode,
+ list(consts_w),
+ names,
+ var_names,
+ self.compile_info.filename,
+ self.name,
+ self.first_lineno,
+ lnotab,
+ free_names,
+ cell_names,
+ self.compile_info.hidden_applevel)
def _list_from_dict(d, offset=0):
@@ -512,134 +514,134 @@
_static_opcode_stack_effects = {
- ops.NOP : 0,
- ops.STOP_CODE : 0,
+ ops.NOP: 0,
+ ops.STOP_CODE: 0,
- ops.POP_TOP : -1,
- ops.ROT_TWO : 0,
- ops.ROT_THREE : 0,
- ops.ROT_FOUR : 0,
- ops.DUP_TOP : 1,
+ ops.POP_TOP: -1,
+ ops.ROT_TWO: 0,
+ ops.ROT_THREE: 0,
+ ops.ROT_FOUR: 0,
+ ops.DUP_TOP: 1,
- ops.UNARY_POSITIVE : 0,
- ops.UNARY_NEGATIVE : 0,
- ops.UNARY_NOT : 0,
- ops.UNARY_CONVERT : 0,
- ops.UNARY_INVERT : 0,
+ ops.UNARY_POSITIVE: 0,
+ ops.UNARY_NEGATIVE: 0,
+ ops.UNARY_NOT: 0,
+ ops.UNARY_CONVERT: 0,
+ ops.UNARY_INVERT: 0,
- ops.LIST_APPEND : -1,
- ops.SET_ADD : -1,
- ops.MAP_ADD : -2,
- ops.STORE_MAP : -2,
+ ops.LIST_APPEND: -1,
+ ops.SET_ADD: -1,
+ ops.MAP_ADD: -2,
+ ops.STORE_MAP: -2,
- ops.BINARY_POWER : -1,
- ops.BINARY_MULTIPLY : -1,
- ops.BINARY_DIVIDE : -1,
- ops.BINARY_MODULO : -1,
- ops.BINARY_ADD : -1,
- ops.BINARY_SUBTRACT : -1,
- ops.BINARY_SUBSCR : -1,
- ops.BINARY_FLOOR_DIVIDE : -1,
- ops.BINARY_TRUE_DIVIDE : -1,
- ops.BINARY_LSHIFT : -1,
- ops.BINARY_RSHIFT : -1,
- ops.BINARY_AND : -1,
- ops.BINARY_OR : -1,
- ops.BINARY_XOR : -1,
+ ops.BINARY_POWER: -1,
+ ops.BINARY_MULTIPLY: -1,
+ ops.BINARY_DIVIDE: -1,
+ ops.BINARY_MODULO: -1,
+ ops.BINARY_ADD: -1,
+ ops.BINARY_SUBTRACT: -1,
+ ops.BINARY_SUBSCR: -1,
+ ops.BINARY_FLOOR_DIVIDE: -1,
+ ops.BINARY_TRUE_DIVIDE: -1,
+ ops.BINARY_LSHIFT: -1,
+ ops.BINARY_RSHIFT: -1,
+ ops.BINARY_AND: -1,
+ ops.BINARY_OR: -1,
+ ops.BINARY_XOR: -1,
- ops.INPLACE_FLOOR_DIVIDE : -1,
- ops.INPLACE_TRUE_DIVIDE : -1,
- ops.INPLACE_ADD : -1,
- ops.INPLACE_SUBTRACT : -1,
- ops.INPLACE_MULTIPLY : -1,
- ops.INPLACE_DIVIDE : -1,
- ops.INPLACE_MODULO : -1,
- ops.INPLACE_POWER : -1,
- ops.INPLACE_LSHIFT : -1,
- ops.INPLACE_RSHIFT : -1,
- ops.INPLACE_AND : -1,
- ops.INPLACE_OR : -1,
- ops.INPLACE_XOR : -1,
+ ops.INPLACE_FLOOR_DIVIDE: -1,
+ ops.INPLACE_TRUE_DIVIDE: -1,
+ ops.INPLACE_ADD: -1,
+ ops.INPLACE_SUBTRACT: -1,
+ ops.INPLACE_MULTIPLY: -1,
+ ops.INPLACE_DIVIDE: -1,
+ ops.INPLACE_MODULO: -1,
+ ops.INPLACE_POWER: -1,
+ ops.INPLACE_LSHIFT: -1,
+ ops.INPLACE_RSHIFT: -1,
+ ops.INPLACE_AND: -1,
+ ops.INPLACE_OR: -1,
+ ops.INPLACE_XOR: -1,
- ops.SLICE+0 : 1,
- ops.SLICE+1 : 0,
- ops.SLICE+2 : 0,
- ops.SLICE+3 : -1,
- ops.STORE_SLICE+0 : -2,
- ops.STORE_SLICE+1 : -3,
- ops.STORE_SLICE+2 : -3,
- ops.STORE_SLICE+3 : -4,
- ops.DELETE_SLICE+0 : -1,
- ops.DELETE_SLICE+1 : -2,
- ops.DELETE_SLICE+2 : -2,
- ops.DELETE_SLICE+3 : -3,
+ ops.SLICE+0: 1,
+ ops.SLICE+1: 0,
+ ops.SLICE+2: 0,
+ ops.SLICE+3: -1,
+ ops.STORE_SLICE+0: -2,
+ ops.STORE_SLICE+1: -3,
+ ops.STORE_SLICE+2: -3,
+ ops.STORE_SLICE+3: -4,
+ ops.DELETE_SLICE+0: -1,
+ ops.DELETE_SLICE+1: -2,
+ ops.DELETE_SLICE+2: -2,
+ ops.DELETE_SLICE+3: -3,
- ops.STORE_SUBSCR : -2,
- ops.DELETE_SUBSCR : -2,
+ ops.STORE_SUBSCR: -2,
+ ops.DELETE_SUBSCR: -2,
- ops.GET_ITER : 0,
- ops.FOR_ITER : 1,
- ops.BREAK_LOOP : 0,
- ops.CONTINUE_LOOP : 0,
- ops.SETUP_LOOP : 0,
+ ops.GET_ITER: 0,
+ ops.FOR_ITER: 1,
+ ops.BREAK_LOOP: 0,
+ ops.CONTINUE_LOOP: 0,
+ ops.SETUP_LOOP: 0,
- ops.PRINT_EXPR : -1,
- ops.PRINT_ITEM : -1,
- ops.PRINT_NEWLINE : 0,
- ops.PRINT_ITEM_TO : -2,
- ops.PRINT_NEWLINE_TO : -1,
+ ops.PRINT_EXPR: -1,
+ ops.PRINT_ITEM: -1,
+ ops.PRINT_NEWLINE: 0,
+ ops.PRINT_ITEM_TO: -2,
+ ops.PRINT_NEWLINE_TO: -1,
- ops.WITH_CLEANUP : -1,
- ops.POP_BLOCK : 0,
- ops.END_FINALLY : -1,
- ops.SETUP_WITH : 1,
- ops.SETUP_FINALLY : 0,
- ops.SETUP_EXCEPT : 0,
+ ops.WITH_CLEANUP: -1,
+ ops.POP_BLOCK: 0,
+ ops.END_FINALLY: -1,
+ ops.SETUP_WITH: 1,
+ ops.SETUP_FINALLY: 0,
+ ops.SETUP_EXCEPT: 0,
- ops.LOAD_LOCALS : 1,
- ops.RETURN_VALUE : -1,
- ops.EXEC_STMT : -3,
- ops.YIELD_VALUE : 0,
- ops.BUILD_CLASS : -2,
- ops.BUILD_MAP : 1,
- ops.BUILD_SET : 1,
- ops.COMPARE_OP : -1,
+ ops.LOAD_LOCALS: 1,
+ ops.RETURN_VALUE: -1,
+ ops.EXEC_STMT: -3,
+ ops.YIELD_VALUE: 0,
+ ops.BUILD_CLASS: -2,
+ ops.BUILD_MAP: 1,
+ ops.BUILD_SET: 1,
+ ops.COMPARE_OP: -1,
- ops.LOOKUP_METHOD : 1,
+ ops.LOOKUP_METHOD: 1,
- ops.LOAD_NAME : 1,
- ops.STORE_NAME : -1,
- ops.DELETE_NAME : 0,
+ ops.LOAD_NAME: 1,
+ ops.STORE_NAME: -1,
+ ops.DELETE_NAME: 0,
- ops.LOAD_FAST : 1,
- ops.STORE_FAST : -1,
- ops.DELETE_FAST : 0,
+ ops.LOAD_FAST: 1,
+ ops.STORE_FAST: -1,
+ ops.DELETE_FAST: 0,
- ops.LOAD_ATTR : 0,
- ops.STORE_ATTR : -2,
- ops.DELETE_ATTR : -1,
+ ops.LOAD_ATTR: 0,
+ ops.STORE_ATTR: -2,
+ ops.DELETE_ATTR: -1,
- ops.LOAD_GLOBAL : 1,
- ops.STORE_GLOBAL : -1,
- ops.DELETE_GLOBAL : 0,
+ ops.LOAD_GLOBAL: 1,
+ ops.STORE_GLOBAL: -1,
+ ops.DELETE_GLOBAL: 0,
- ops.LOAD_CLOSURE : 1,
- ops.LOAD_DEREF : 1,
- ops.STORE_DEREF : -1,
+ ops.LOAD_CLOSURE: 1,
+ ops.LOAD_DEREF: 1,
+ ops.STORE_DEREF: -1,
- ops.LOAD_CONST : 1,
+ ops.LOAD_CONST: 1,
- ops.IMPORT_STAR : -1,
- ops.IMPORT_NAME : -1,
- ops.IMPORT_FROM : 1,
+ ops.IMPORT_STAR: -1,
+ ops.IMPORT_NAME: -1,
+ ops.IMPORT_FROM: 1,
- ops.JUMP_FORWARD : 0,
- ops.JUMP_ABSOLUTE : 0,
- ops.JUMP_IF_TRUE_OR_POP : 0,
- ops.JUMP_IF_FALSE_OR_POP : 0,
- ops.POP_JUMP_IF_TRUE : -1,
- ops.POP_JUMP_IF_FALSE : -1,
- ops.JUMP_IF_NOT_DEBUG : 0,
+ ops.JUMP_FORWARD: 0,
+ ops.JUMP_ABSOLUTE: 0,
+ ops.JUMP_IF_TRUE_OR_POP: 0,
+ ops.JUMP_IF_FALSE_OR_POP: 0,
+ ops.POP_JUMP_IF_TRUE: -1,
+ ops.POP_JUMP_IF_FALSE: -1,
+ ops.JUMP_IF_NOT_DEBUG: 0,
ops.BUILD_LIST_FROM_ARG: 1,
}
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit