def test_execute_nonspec(): cpu = FakeCPU() descr = FakeDescr() # cases with a descr # arity == -1 argboxes = [BoxInt(321), ConstInt(123)] box = execute_nonspec(cpu, rop.CALL, argboxes, descr) assert box.args == ('call', argboxes, descr) # arity == 0 box = execute_nonspec(cpu, rop.NEW, [], descr) assert box.args == ('new', descr) # arity == 1 box1 = BoxInt(515) box = execute_nonspec(cpu, rop.ARRAYLEN_GC, [box1], descr) assert box.args == ('arraylen_gc', box1, descr) # arity == 2 box2 = BoxInt(222) box = execute_nonspec(cpu, rop.SETFIELD_GC, [box1, box2], descr) assert box.args == ('setfield_gc', box1, box2, descr) # arity == 3 box3 = BoxInt(-33) box = execute_nonspec(cpu, rop.SETARRAYITEM_GC, [box1, box2, box3], descr) assert box.args == ('setarrayitem_gc', box1, box2, box3, descr) # cases without descr # arity == 1 box = execute_nonspec(cpu, rop.INT_INVERT, [box1]) assert box.value == ~515 # arity == 2 box = execute_nonspec(cpu, rop.INT_LSHIFT, [box1, BoxInt(3)]) assert box.value == 515 << 3 # arity == 3 box = execute_nonspec(cpu, rop.STRSETITEM, [box1, box2, box3]) assert box.args == ('strsetitem', box1, box2, box3)
def optimize_default(self, op): if op.is_always_pure(): for arg in op.args: if self.get_constant_box(arg) is None: break else: # all constant arguments: constant-fold away argboxes = [self.get_constant_box(arg) for arg in op.args] resbox = execute_nonspec(self.cpu, op.opnum, argboxes, op.descr) self.make_constant(op.result, resbox.constbox()) return # did we do the exact same operation already? args = op.args[:] for i in range(len(args)): arg = args[i] if arg in self.values: args[i] = self.values[arg].get_key_box() args.append(ConstInt(op.opnum)) oldop = self.pure_operations.get(args, None) if oldop is not None and oldop.descr is op.descr: assert oldop.opnum == op.opnum self.make_equal_to(op.result, self.getvalue(oldop.result)) return else: self.pure_operations[args] = op # otherwise, the operation remains self.emit_operation(op)
def do(self, opnum, argboxes, descr=None): v_result = execute_nonspec(self.cpu, opnum, argboxes, descr) if isinstance(v_result, Const): v_result = v_result.clonebox() self.loop.operations.append(ResOperation(opnum, argboxes, v_result, descr)) return v_result
def do(self, opnum, argboxes, descr=None): v_result = execute_nonspec(self.cpu, opnum, argboxes, descr) if isinstance(v_result, Const): v_result = v_result.clonebox() self.loop.operations.append( ResOperation(opnum, argboxes, v_result, descr)) return v_result
def constant_fold(self, op): argboxes = [ self.get_constant_box(op.getarg(i)) for i in range(op.numargs()) ] resbox = execute_nonspec(self.cpu, None, op.getopnum(), argboxes, op.getdescr()) return resbox.constbox()
def test_float_ops(): cpu = FakeCPU() for opnum, boxargs, rettype, retvalue in get_float_tests(cpu): box = execute_nonspec(cpu, opnum, boxargs) if rettype == 'float': assert box.getfloat() == retvalue elif rettype == 'int': assert box.getint() == retvalue else: assert retvalue is None
def test_float_ops(): cpu = FakeCPU() for opnum, boxargs, rettype, retvalue in get_float_tests(cpu): box = execute_nonspec(cpu, None, opnum, boxargs) if rettype == "float": assert box.getfloat() == retvalue elif rettype == "int": assert box.getint() == retvalue else: assert 0, "rettype is %r" % (rettype,)
def find_nodes_default(self, op): if op.is_always_pure(): for arg in op.args: if self.get_constant_box(arg) is None: break else: # all constant arguments: we can constant-fold argboxes = [self.get_constant_box(arg) for arg in op.args] resbox = execute_nonspec(self.cpu, op.opnum, argboxes, op.descr) self.set_constant_node(op.result, resbox.constbox()) # default case: mark the arguments as escaping for box in op.args: self.getnode(box).mark_escaped()
def optimize_default(self, op): if op.is_always_pure(): for arg in op.args: if self.get_constant_box(arg) is None: break else: # all constant arguments: constant-fold away argboxes = [self.get_constant_box(arg) for arg in op.args] resbox = execute_nonspec(self.cpu, op.opnum, argboxes, op.descr) self.make_constant(op.result, resbox.constbox()) return # otherwise, the operation remains self.emit_operation(op)
def constant_fold(self, op): argboxes = [self.get_constant_box(op.getarg(i)) for i in range(op.numargs())] resbox = execute_nonspec(self.cpu, None, op.getopnum(), argboxes, op.getdescr()) return resbox.constbox()
def test_int_ops(): cpu = FakeCPU() for opnum, boxargs, retvalue in get_int_tests(): box = execute_nonspec(cpu, opnum, boxargs) assert box.getint() == retvalue
def test_execute_nonspec(): cpu = FakeCPU() descr = FakeDescr() # cases with a descr # arity == -1 argboxes = [BoxInt(321), ConstInt(123)] box = execute_nonspec(cpu, FakeMetaInterp(), rop.CALL, argboxes, FakeCallDescr()) assert box.getfloat() == 42.5 # arity == 0 box = execute_nonspec(cpu, None, rop.NEW, [], descr) assert box.value.fakeargs == ('new', descr) # arity == 1 box1 = BoxPtr() box = execute_nonspec(cpu, None, rop.ARRAYLEN_GC, [box1], descr) assert box.value == 55 # arity == 2 box2 = boxfloat(222.2) fielddescr = FakeFieldDescr() execute_nonspec(cpu, None, rop.SETFIELD_GC, [box1, box2], fielddescr) assert cpu.fakesetfield == (box1.value, box2.value, fielddescr) # arity == 3 box3 = BoxInt(33) arraydescr = FakeArrayDescr() execute_nonspec(cpu, None, rop.SETARRAYITEM_GC, [box1, box3, box2], arraydescr) assert cpu.fakesetarrayitem == (box1.value, box3.value, box2.value, arraydescr) # cases without descr # arity == 1 box = execute_nonspec(cpu, None, rop.INT_INVERT, [box3]) assert box.value == ~33 # arity == 2 box = execute_nonspec(cpu, None, rop.INT_LSHIFT, [box3, BoxInt(3)]) assert box.value == 33 << 3 # arity == 3 execute_nonspec(cpu, None, rop.STRSETITEM, [box1, BoxInt(3), box3]) assert cpu.fakestrsetitem == (box1.value, 3, box3.value)