def test_cast_adr_to_int_and_back(): X = lltype.Struct('X', ('foo', lltype.Signed)) x = lltype.malloc(X, immortal=True) x.foo = 42 a = llmemory.cast_ptr_to_adr(x) i = adr2int(a) assert lltype.typeOf(i) is lltype.Signed a2 = int2adr(i) assert llmemory.cast_adr_to_ptr(a2, lltype.Ptr(X)) == x assert adr2int(llmemory.NULL) == 0 assert int2adr(0) == llmemory.NULL
def emit_const(self, const, kind, allow_short=False): value = const.value value_key = value if kind == 'int': TYPE = const.concretetype if isinstance(TYPE, lltype.Ptr): assert TYPE.TO._gckind == 'raw' self.see_raw_object(value) value = llmemory.cast_ptr_to_adr(value) TYPE = llmemory.Address if TYPE == llmemory.Address: value = adr2int(value) if TYPE is lltype.SingleFloat: value = longlong.singlefloat2int(value) if not isinstance(value, (llmemory.AddressAsInt, ComputedIntSymbolic)): value = lltype.cast_primitive(lltype.Signed, value) if type(value) is r_int: value = int(value) if allow_short: try: short_num = -128 <= value <= 127 except TypeError: # "Symbolics cannot be compared!" short_num = False if short_num: # emit the constant as a small integer self.code.append(chr(value & 0xFF)) return True constants = self.constants_i elif kind == 'ref': value = lltype.cast_opaque_ptr(llmemory.GCREF, value) constants = self.constants_r if not value: # nullptr value_key = None else: value_key = value._obj.container elif kind == 'float': if const.concretetype == lltype.Float: value = longlong.getfloatstorage(value) else: assert longlong.is_longlong(const.concretetype) value = rffi.cast(lltype.SignedLongLong, value) constants = self.constants_f else: raise AssemblerError('unimplemented %r in %r' % (const, self.ssareprname)) key = (kind, Constant(value_key)) try: val = self.constants_dict[key] except KeyError: constants.append(value) val = 256 - len(constants) assert val >= 0, "too many constants" self.constants_dict[key] = val # emit the constant normally, as one byte that is an index in the # list of constants self.code.append(chr(val)) return False
def cast_to_int(x): TP = lltype.typeOf(x) if isinstance(TP, lltype.Ptr): return ptr2int(x) if TP == llmemory.Address: return adr2int(x) if TP is lltype.SingleFloat: return longlong.singlefloat2int(x) return lltype.cast_primitive(lltype.Signed, x)
def compile_tmp_callback(cpu, jitdriver_sd, greenboxes, redargtypes, memory_manager=None): """Make a LoopToken that corresponds to assembler code that just calls back the interpreter. Used temporarily: a fully compiled version of the code may end up replacing it. """ jitcell_token = make_jitcell_token(jitdriver_sd) # # record the target of a temporary callback to the interpreter jl.tmp_callback(jitcell_token) # nb_red_args = jitdriver_sd.num_red_args assert len(redargtypes) == nb_red_args inputargs = [] for kind in redargtypes: if kind == history.INT: box = InputArgInt() elif kind == history.REF: box = InputArgRef() elif kind == history.FLOAT: box = InputArgFloat() else: raise AssertionError inputargs.append(box) k = jitdriver_sd.portal_runner_adr funcbox = history.ConstInt(adr2int(k)) callargs = [funcbox] + greenboxes + inputargs # jd = jitdriver_sd opnum = OpHelpers.call_for_descr(jd.portal_calldescr) call_op = ResOperation(opnum, callargs, descr=jd.portal_calldescr) if call_op.type != 'v' is not None: finishargs = [call_op] else: finishargs = [] # faildescr = jitdriver_sd.propagate_exc_descr operations = [ call_op, ResOperation(rop.GUARD_NO_EXCEPTION, [], descr=faildescr), ResOperation(rop.FINISH, finishargs, descr=jd.portal_finishtoken) ] operations[1].setfailargs([]) operations = get_deep_immutable_oplist(operations) cpu.compile_loop(inputargs, operations, jitcell_token, log=False) if memory_manager is not None: # for tests memory_manager.keep_loop_alive(jitcell_token) return jitcell_token
def test_guard_class(self): loop = """ [p0] guard_class(p0, ConstClass(node_vtable)) [] jump(p0) """ es, loop, preamble = self.optimize(loop) p0 = preamble.inputargs[0] expected_class = adr2int(self.node_vtable_adr) assert expected_class == es.exported_infos[p0]._known_class.getint() vs = es.virtual_state assert vs.state[0].level == LEVEL_KNOWNCLASS assert vs.state[0].known_class.getint() == expected_class
def cast_from_int(TYPE, x): if isinstance(TYPE, lltype.Ptr): if isinstance(x, (int, long, llmemory.AddressAsInt)): x = llmemory.cast_int_to_adr(x) try: # pom pom pom return llmemory.cast_adr_to_ptr(x, TYPE) except Exception: # assume that we want a "C-style" cast, without typechecking the value return rffi.cast(TYPE, x) elif TYPE == llmemory.Address: if isinstance(x, (int, long, llmemory.AddressAsInt)): x = llmemory.cast_int_to_adr(x) assert lltype.typeOf(x) == llmemory.Address return x elif TYPE is lltype.SingleFloat: assert lltype.typeOf(x) is lltype.Signed return longlong.int2singlefloat(x) else: if lltype.typeOf(x) == llmemory.Address: x = adr2int(x) return lltype.cast_primitive(TYPE, x)
def pos_exc_value(): addr = llop.get_exc_value_addr(llmemory.Address) return adr2int(addr)
def pos_exception(): addr = llop.get_exception_addr(llmemory.Address) return adr2int(addr)
def get_fnaddr_as_int(self): return adr2int(self.fnaddr)