def test_execute(): cpu = FakeCPU() descr = FakeDescr() box = execute(cpu, None, rop.INT_ADD, None, InputArgInt(40), ConstInt(2)) assert box == 42 box = execute(cpu, None, rop.NEW, descr) assert box.fakeargs == ('new', descr)
def test_nullity_with_guard(self): allops = [rop.INT_IS_TRUE] guards = [rop.GUARD_TRUE, rop.GUARD_FALSE] p = lltype.cast_opaque_ptr(llmemory.GCREF, lltype.malloc(lltype.GcStruct('x'))) nullptr = lltype.nullptr(llmemory.GCREF.TO) f = InputArgInt() for op in allops: for guard in guards: if op == rop.INT_IS_TRUE: bp = InputArgInt(1) n = InputArgInt(0) else: bp = InputArgRef(p) n = InputArgRef(nullptr) for b in (bp, n): i1 = ResOperation(rop.SAME_AS_I, [ConstInt(1)]) f = ResOperation(op, [b]) ops = [ i1, f, ResOperation(guard, [f], descr=BasicFailDescr()), ResOperation(rop.FINISH, [ConstInt(0)], descr=BasicFinalDescr()), ] ops[-2].setfailargs([i1]) looptoken = JitCellToken() self.cpu.compile_loop([b], ops, looptoken) deadframe = self.cpu.execute_token(looptoken, b.getint()) result = self.cpu.get_int_value(deadframe, 0) if guard == rop.GUARD_FALSE: assert result == execute(self.cpu, None, op, None, b) else: assert result != execute(self.cpu, None, op, None, b)
def _really_force(self, optforce): op = self.source_op assert op is not None # ^^^ This case should not occur any more (see test_bug_3). # if not we_are_translated(): op.name = 'FORCE ' + self.source_op.name if self._is_immutable_and_filled_with_constants(): box = optforce.optimizer.constant_fold(op) self.make_constant(box) for ofs, value in self._fields.iteritems(): subbox = value.force_box(optforce) assert isinstance(subbox, Const) execute(optforce.optimizer.cpu, None, rop.SETFIELD_GC, ofs, box, subbox) # keep self._fields, because it's all immutable anyway else: optforce.emit_operation(op) self.box = box = op.result # iteritems = self._fields.iteritems() if not we_are_translated(): #random order is fine, except for tests iteritems = list(iteritems) iteritems.sort(key=lambda (x, y): x.sort_key()) for ofs, value in iteritems: if value.is_null(): continue subbox = value.force_box(optforce) op = ResOperation(rop.SETFIELD_GC, [box, subbox], None, descr=ofs) optforce.emit_operation(op)
def test_execute(): cpu = FakeCPU() descr = FakeDescr() box = execute(cpu, None, rop.INT_ADD, None, BoxInt(40), ConstInt(2)) assert box.value == 42 box = execute(cpu, None, rop.NEW, descr) assert box.value.fakeargs == ('new', descr)
def _force_elements_immutable(self, descr, constptr, optforce): for i, fielddescr in enumerate(descr.get_all_fielddescrs()): fld = self._fields[i] subbox = optforce.force_box(fld) assert isinstance(subbox, Const) execute(optforce.optimizer.cpu, None, rop.SETFIELD_GC, fielddescr, constptr, subbox)
def _really_force(self, optforce): op = self.source_op assert op is not None # ^^^ This case should not occur any more (see test_bug_3). # if not we_are_translated(): op.name = 'FORCE ' + self.source_op.name if self._is_immutable_and_filled_with_constants(): box = optforce.optimizer.constant_fold(op) self.make_constant(box) for ofs, value in self._fields.iteritems(): subbox = value.force_box(optforce) assert isinstance(subbox, Const) execute(optforce.optimizer.cpu, None, rop.SETFIELD_GC, ofs, box, subbox) # keep self._fields, because it's all immutable anyway else: optforce.emit_operation(op) self.box = box = op.result # iteritems = self._fields.iteritems() if not we_are_translated( ): #random order is fine, except for tests iteritems = list(iteritems) iteritems.sort(key=lambda (x, y): x.sort_key()) for ofs, value in iteritems: subbox = value.force_box(optforce) op = ResOperation(rop.SETFIELD_GC, [box, subbox], None, descr=ofs) optforce.emit_operation(op)
def test_opboolreflex(): cpu = FakeCPU() for op1, op2 in opboolreflex.items(): for a in (1,2,3): for b in (1,2,3): arg1, arg2 = make_args_for_op(op1, a, b) box1 = execute(cpu, None, op1, None, arg1, arg2) box2 = execute(cpu, None, op2, None, arg2, arg1) assert box1.value == box2.value
def test_opboolinvers(): cpu = FakeCPU() for op1 in opclasses: if op1 is None or op1.boolinverse == -1: continue op2 = opclasses[op1.boolinverse].opnum op1 = op1.opnum for a in (1,2,3): for b in (1,2,3): arg1, arg2 = make_args_for_op(op1, a, b) box1 = execute(cpu, None, op1, None, arg1, arg2) box2 = execute(cpu, None, op2, None, arg1, arg2) assert box1 == (not box2)
def test_opboolinvers(): cpu = FakeCPU() for op1 in opclasses: if op1 is None or op1.boolinverse == -1: continue op2 = opclasses[op1.boolinverse].opnum op1 = op1.opnum for a in (1, 2, 3): for b in (1, 2, 3): arg1, arg2 = make_args_for_op(op1, a, b) box1 = execute(cpu, None, op1, None, arg1, arg2) box2 = execute(cpu, None, op2, None, arg1, arg2) assert box1.value == (not box2.value)
def test_opboolreflex(): cpu = FakeCPU() for op1 in opclasses: if op1 is None or op1.boolreflex == -1: continue op2 = opclasses[op1.boolreflex].opnum op1 = op1.opnum for a in (1,2,3): for b in (1,2,3): arg1, arg2 = make_args_for_op(op1, a, b) box1 = execute(cpu, None, op1, None, arg1, arg2) box2 = execute(cpu, None, op2, None, arg2, arg1) assert box1.value == box2.value
def test_opboolreflex(): cpu = FakeCPU() for op1 in opclasses: if op1 is None or op1.boolreflex == -1: continue op2 = opclasses[op1.boolreflex].opnum op1 = op1.opnum for a in (1, 2, 3): for b in (1, 2, 3): arg1, arg2 = make_args_for_op(op1, a, b) box1 = execute(cpu, None, op1, None, arg1, arg2) box2 = execute(cpu, None, op2, None, arg2, arg1) assert box1 == box2
def execute_and_record(self, opnum, descr, *argboxes): resvalue = executor.execute(self.cpu, None, opnum, descr, *argboxes) if isinstance(resvalue, int): op = IntFrontendOp(0) else: op = RefFrontendOp(0) setvalue(op, resvalue) self.trace.append((opnum, list(argboxes), resvalue, descr)) return op
def test_modulo_operations(n, m, known_nonneg): if n < 0: known_nonneg = False n_box = InputArgInt() ops = modulo_operations(n_box, m, known_nonneg) constants = {n_box: ConstInt(n)} for op in ops: argboxes = op.getarglist() constantboxes = [constants.get(box, box) for box in argboxes] res = execute(None, None, op.getopnum(), None, *constantboxes) constants[op] = ConstInt(res) assert constants[op].getint() == n % m
def test_stuff_followed_by_guard(self): boxes = [(BoxInt(1), BoxInt(0)), (BoxInt(0), BoxInt(1)), (BoxInt(1), BoxInt(1)), (BoxInt(-1), BoxInt(1)), (BoxInt(1), BoxInt(-1)), (ConstInt(1), BoxInt(0)), (ConstInt(0), BoxInt(1)), (ConstInt(1), BoxInt(1)), (ConstInt(-1), BoxInt(1)), (ConstInt(1), BoxInt(-1)), (BoxInt(1), ConstInt(0)), (BoxInt(0), ConstInt(1)), (BoxInt(1), ConstInt(1)), (BoxInt(-1), ConstInt(1)), (BoxInt(1), ConstInt(-1))] guards = [rop.GUARD_FALSE, rop.GUARD_TRUE] all = [rop.INT_EQ, rop.INT_NE, rop.INT_LE, rop.INT_LT, rop.INT_GT, rop.INT_GE, rop.UINT_GT, rop.UINT_LT, rop.UINT_LE, rop.UINT_GE] for a, b in boxes: for guard in guards: for op in all: res = BoxInt() i1 = BoxInt(1) ops = [ ResOperation(rop.SAME_AS, [ConstInt(1)], i1), ResOperation(op, [a, b], res), ResOperation(guard, [res], None, descr=BasicFailDescr()), ResOperation(rop.FINISH, [ConstInt(0)], None, descr=BasicFinalDescr()), ] ops[-2].setfailargs([i1]) inputargs = [i for i in (a, b) if isinstance(i, Box)] looptoken = JitCellToken() self.cpu.compile_loop(inputargs, ops, looptoken) inputvalues = [box.value for box in inputargs] deadframe = self.cpu.execute_token(looptoken, *inputvalues) result = self.cpu.get_int_value(deadframe, 0) expected = execute(self.cpu, None, op, None, a, b).value if guard == rop.GUARD_FALSE: assert result == expected else: assert result != expected
def test_stuff_followed_by_guard(self): boxes = [(InputArgInt(1), InputArgInt(0)), (InputArgInt(0), InputArgInt(1)), (InputArgInt(1), InputArgInt(1)), (InputArgInt(-1), InputArgInt(1)), (InputArgInt(1), InputArgInt(-1)), (ConstInt(1), InputArgInt(0)), (ConstInt(0), InputArgInt(1)), (ConstInt(1), InputArgInt(1)), (ConstInt(-1), InputArgInt(1)), (ConstInt(1), InputArgInt(-1)), (InputArgInt(1), ConstInt(0)), (InputArgInt(0), ConstInt(1)), (InputArgInt(1), ConstInt(1)), (InputArgInt(-1), ConstInt(1)), (InputArgInt(1), ConstInt(-1))] guards = [rop.GUARD_FALSE, rop.GUARD_TRUE] all = [rop.INT_EQ, rop.INT_NE, rop.INT_LE, rop.INT_LT, rop.INT_GT, rop.INT_GE, rop.UINT_GT, rop.UINT_LT, rop.UINT_LE, rop.UINT_GE] for a, b in boxes: for guard in guards: for op in all: i1 = ResOperation(rop.SAME_AS_I, [ConstInt(1)]) res = ResOperation(op, [a, b]) ops = [ i1, res, ResOperation(guard, [res], descr=BasicFailDescr()), ResOperation(rop.FINISH, [ConstInt(0)], descr=BasicFinalDescr()), ] ops[-2].setfailargs([i1]) inputargs = [i for i in (a, b) if not isinstance(i, Const)] looptoken = JitCellToken() self.cpu.compile_loop(inputargs, ops, looptoken) inputvalues = [box.getint() for box in inputargs] deadframe = self.cpu.execute_token(looptoken, *inputvalues) result = self.cpu.get_int_value(deadframe, 0) expected = execute(self.cpu, None, op, None, a, b) if guard == rop.GUARD_FALSE: assert result == expected else: assert result != expected
def get_current_constant_fieldvalue(self): from rpython.jit.metainterp import executor from rpython.jit.metainterp.resoperation import rop fieldbox = executor.execute(self.cpu, None, rop.GETFIELD_GC, self.fielddescr, self.structbox) return fieldbox.constbox()