Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def build_tuple(self, instr, container_type):
if instr.arg > len(self.const_stack):
return
next_instr = self.get_next_instr("COMPARE_OP")
if next_instr is None or next_instr.arg not in (Compare.IN, Compare.NOT_IN):
return
self.replace_container_of_consts(instr, container_type)
return True
raise ValueError(f"unknown binary instruction {b}")
c = yield from f(a1, a2)
yield am.push(c)
cmp_map = {
bc.Compare.EQ: RT.py_eq,
bc.Compare.NE: RT.py_neq,
bc.Compare.IS: RT.py_is,
bc.Compare.IS_NOT: RT.py_is_not,
bc.Compare.LT: RT.py_lt,
bc.Compare.LE: RT.py_le,
bc.Compare.GT: RT.py_gt,
bc.Compare.GE: RT.py_ge,
bc.Compare.IN: RT.py_in,
bc.Compare.NOT_IN: RT.py_not_in,
bc.Compare.EXC_MATCH: RT.py_exc_match
}
def abs_i_cmp(b: bc.Instr):
arg: bc.Compare = b.arg
f = cmp_map[arg]
a2 = yield am.pop()
a1 = yield am.pop()
a = yield from f(a1, a2)
yield am.push(a)
def abs_i_cfg(b: bc.ControlFlowGraph):
for each in b:
assert isinstance(each, bc.BasicBlock)
print(e)
return self.co_code.to_code();
for op in hasfree:
if not hasattr(Code, opname[op]):
def do_free(self, varname, op=op):
self.stackchange(stack_effects[op])
try:
arg = list(self.co_cellvars+self.co_freevars).index(varname)
except ValueError:
raise NameError("Undefined free or cell var", varname)
self.emit_arg(op, arg)
setattr(Code, opname[op], with_name(do_free, opname[op]))
compares = { '==' : Compare.EQ, '!=' : Compare.NE, '>' : Compare.GT, '>=' : Compare.GE,
'<' : Compare.LT, '<=' : Compare.LE, "in" : Compare.IN}
for op in hasname:
if not hasattr(Code, opname[op]):
def do_name(self, name, op=op):
self.stackchange(stack_effects[op])
self.emit_arg(opname[op], name)
if op in (LOAD_NAME, STORE_NAME, DELETE_NAME):
# Can't use optimized local vars, so reset flags
self.co_flags &= ~CO_OPTIMIZED
setattr(Code, opname[op], with_name(do_name, opname[op]))
for op in haslocal:
if not hasattr(Code, opname[op]):
def abs_i_inplace_binary(b: bc.Instr):
a2 = yield am.pop()
a1 = yield am.pop()
f = ibin_map.get(b.name, None)
if f is None:
raise ValueError(f"unknown binary instruction {b}")
c = yield from f(a1, a2)
yield am.push(c)
cmp_map = {
bc.Compare.EQ: RT.py_eq,
bc.Compare.NE: RT.py_neq,
bc.Compare.IS: RT.py_is,
bc.Compare.IS_NOT: RT.py_is_not,
bc.Compare.LT: RT.py_lt,
bc.Compare.LE: RT.py_le,
bc.Compare.GT: RT.py_gt,
bc.Compare.GE: RT.py_ge,
bc.Compare.IN: RT.py_in,
bc.Compare.NOT_IN: RT.py_not_in,
bc.Compare.EXC_MATCH: RT.py_exc_match
}
def abs_i_cmp(b: bc.Instr):
arg: bc.Compare = b.arg
f = cmp_map[arg]
a2 = yield am.pop()
a1 = yield am.pop()
def abs_i_inplace_binary(b: bc.Instr):
a2 = yield am.pop()
a1 = yield am.pop()
f = ibin_map.get(b.name, None)
if f is None:
raise ValueError(f"unknown binary instruction {b}")
c = yield from f(a1, a2)
yield am.push(c)
cmp_map = {
bc.Compare.EQ: RT.py_eq,
bc.Compare.NE: RT.py_neq,
bc.Compare.IS: RT.py_is,
bc.Compare.IS_NOT: RT.py_is_not,
bc.Compare.LT: RT.py_lt,
bc.Compare.LE: RT.py_le,
bc.Compare.GT: RT.py_gt,
bc.Compare.GE: RT.py_ge,
bc.Compare.IN: RT.py_in,
bc.Compare.NOT_IN: RT.py_not_in,
bc.Compare.EXC_MATCH: RT.py_exc_match
}
def abs_i_cmp(b: bc.Instr):
arg: bc.Compare = b.arg
f = cmp_map[arg]
a2 = yield am.pop()
a1 = yield am.pop()
a = yield from f(a1, a2)
yield am.push(a)
instructions = [
# Instr("SETUP_LOOP",loop_exit,lineno=ln),
Instr("LOAD_CONST", 0, lineno=ln),
Instr("STORE_FAST", arg=loopcounter_name, lineno=ln),
Instr("LOAD_FAST", arg=iterable, lineno=ln),
Instr("LOAD_GLOBAL", arg="len", lineno=ln),
Instr("CALL_FUNCTION", arg=1, lineno=ln),
Instr("STORE_FAST", arg=looplength_name, lineno=ln),
loop_start,
# Instr("FOR_ITER",loop_done,lineno=ln),
Instr("LOAD_FAST", arg=loopcounter_name, lineno=ln),
Instr("LOAD_FAST", arg=looplength_name, lineno=ln),
Instr("COMPARE_OP", arg=Compare.LT, lineno=ln),
Instr("POP_JUMP_IF_FALSE", arg=loop_done, lineno=ln),
Instr("LOAD_FAST", arg=iterable, lineno=ln),
Instr("LOAD_FAST", arg=loopcounter_name, lineno=ln),
Instr("BINARY_SUBSCR", lineno=ln),
Instr("STORE_FAST", arg=iterable_name, lineno=ln),
Instr("LOAD_FAST", loopcounter_name, lineno=ln),
Instr("LOAD_CONST", 1, lineno=ln),
Instr("INPLACE_ADD", lineno=ln),
Instr("STORE_FAST", loopcounter_name, lineno=ln),
]
self.container_method.add_to_scope(loopcounter_name)
self.container_method.add_to_scope(looplength_name)
"""
Peephole optimizer of CPython 3.6 reimplemented in pure Python using
the bytecode module.
"""
import opcode
import operator
import sys
from bytecode import Instr, Bytecode, ControlFlowGraph, BasicBlock, Compare
JUMPS_ON_TRUE = frozenset(("POP_JUMP_IF_TRUE", "JUMP_IF_TRUE_OR_POP",))
NOT_COMPARE = {
Compare.IN: Compare.NOT_IN,
Compare.NOT_IN: Compare.IN,
Compare.IS: Compare.IS_NOT,
Compare.IS_NOT: Compare.IS,
}
MAX_SIZE = 20
class ExitUnchanged(Exception):
"""Exception used to skip the peephole optimizer"""
pass
class PeepholeOptimizer:
"""Python reimplementation of the peephole optimizer.
a2 = yield am.pop()
a1 = yield am.pop()
f = ibin_map.get(b.name, None)
if f is None:
raise ValueError(f"unknown binary instruction {b}")
c = yield from f(a1, a2)
yield am.push(c)
cmp_map = {
bc.Compare.EQ: RT.py_eq,
bc.Compare.NE: RT.py_neq,
bc.Compare.IS: RT.py_is,
bc.Compare.IS_NOT: RT.py_is_not,
bc.Compare.LT: RT.py_lt,
bc.Compare.LE: RT.py_le,
bc.Compare.GT: RT.py_gt,
bc.Compare.GE: RT.py_ge,
bc.Compare.IN: RT.py_in,
bc.Compare.NOT_IN: RT.py_not_in,
bc.Compare.EXC_MATCH: RT.py_exc_match
}
def abs_i_cmp(b: bc.Instr):
arg: bc.Compare = b.arg
f = cmp_map[arg]
a2 = yield am.pop()
a1 = yield am.pop()
a = yield from f(a1, a2)
yield am.push(a)