def annotation_to_lltype(s_val, info=None): if isinstance(s_val, SomeOOInstance): return s_val.ootype if isinstance(s_val, SomeOOStaticMeth): return s_val.method if isinstance(s_val, SomeOOClass): return ootype.Class if isinstance(s_val, SomeOOObject): return s_val.ootype if isinstance(s_val, SomeInteriorPtr): p = s_val.ll_ptrtype if 0 in p.offsets: assert list(p.offsets).count(0) == 1 return lltype.Ptr( lltype.Ptr(p.PARENTTYPE)._interior_ptr_type_with_index(p.TO)) else: return lltype.Ptr(p.PARENTTYPE) if isinstance(s_val, SomePtr): return s_val.ll_ptrtype for witness, T in annotation_to_ll_map: if witness.contains(s_val): if T is NUMBER: return lltype.build_number(None, s_val.knowntype) return T if info is None: info = '' else: info = '%s: ' % info raise ValueError("%sshould return a low-level type,\ngot instead %r" % (info, s_val))
def annotation_to_lltype(s_val, info=None): if isinstance(s_val, SomeOOInstance): return s_val.ootype if isinstance(s_val, SomeOOStaticMeth): return s_val.method if isinstance(s_val, SomeOOClass): return ootype.Class if isinstance(s_val, SomeOOObject): return s_val.ootype if isinstance(s_val, SomeInteriorPtr): p = s_val.ll_ptrtype if 0 in p.offsets: assert list(p.offsets).count(0) == 1 return lltype.Ptr(lltype.Ptr(p.PARENTTYPE)._interior_ptr_type_with_index(p.TO)) else: return lltype.Ptr(p.PARENTTYPE) if isinstance(s_val, SomePtr): return s_val.ll_ptrtype for witness, T in annotation_to_ll_map: if witness.contains(s_val): if T is NUMBER: return lltype.build_number(None, s_val.knowntype) return T if info is None: info = '' else: info = '%s: ' % info raise ValueError("%sshould return a low-level type,\ngot instead %r" % ( info, s_val))
def inttype(self, name, c_name, signed, **kwds): try: return self.types[name] except KeyError: bits = sizeof_c_type(c_name, **kwds) * 8 inttype = rarithmetic.build_int('r_' + name, signed, bits) tp = lltype.build_number(name, inttype) self.numbertype_to_rclass[tp] = inttype self.types[name] = tp return tp
def inttype(self, name, c_name, signed, **kwds): try: return self.types[name] except KeyError: bits = sizeof_c_type(c_name, **kwds) * 8 inttype = rarithmetic.build_int('r_' + name, signed, bits) tp = lltype.build_number(name, inttype) self.numbertype_to_rclass[tp] = inttype self.types[name] = tp return tp
def setup(): for _name in 'byte short int long longlong'.split(): for name in (_name, 'u' + _name): c_type = getattr(ctypes, 'c_' + name) sign, bits = c_type_size(c_type) inttype = rarithmetic.build_int('rc' + name, sign, bits) globals()['rc'+name] = inttype if name[0] == 'u': llname = 'CU' + name[1:].title() else: llname = 'C' + name.title() globals()[llname] = lltype.build_number(llname, inttype)
def test_specialize(): for inttype in inttypes: c = inttype() def f(): return c res = interpret(f, []) assert res == f() assert lltype.typeOf(res) == lltype.build_number(None, inttype) for inttype in inttypes: def f(): return inttype(0) res = interpret(f, []) assert res == f() assert lltype.typeOf(res) == lltype.build_number(None, inttype) for inttype in inttypes: def f(x): return x res = interpret(f, [inttype(0)]) assert res == f(inttype(0)) assert lltype.typeOf(res) == lltype.build_number(None, inttype)
def setup(): """ creates necessary c-level types """ from pypy.rpython.lltypesystem.rfficache import platform for name, bits in platform.items(): if name.startswith('unsigned'): name = 'u' + name[9:] signed = False else: signed = True name = name.replace(' ', '') llname = name.upper() inttype = rarithmetic.build_int('r_' + name, signed, bits) globals()['r_' + name] = inttype globals()[llname] = lltype.build_number(llname, inttype)
def annotation_to_lltype(s_val, info=None): if isinstance(s_val, SomeOOInstance): return s_val.ootype if isinstance(s_val, SomeOOStaticMeth): return s_val.method if isinstance(s_val, SomePtr): return s_val.ll_ptrtype for witness, T in annotation_to_ll_map: if witness.contains(s_val): if T is NUMBER: return lltype.build_number(None, s_val.knowntype) return T if info is None: info = '' else: info = '%s: ' % info raise ValueError("%sshould return a low-level type,\ngot instead %r" % ( info, s_val))
""" names = populate_inttypes() result = [] for name in names: tp = platform.types[name.upper()] globals()['r_' + name] = platform.numbertype_to_rclass[tp] globals()[name.upper()] = tp tpp = lltype.Ptr(lltype.Array(tp, hints={'nolength': True})) globals()[name.upper()+'P'] = tpp result.append(tp) return result NUMBER_TYPES = setup() platform.numbertype_to_rclass[lltype.Signed] = int # avoid "r_long" for common cases r_int_real = rarithmetic.build_int("r_int_real", r_int.SIGN, r_int.BITS, True) INT_real = lltype.build_number("INT", r_int_real) platform.numbertype_to_rclass[INT_real] = r_int_real NUMBER_TYPES.append(INT_real) # ^^^ this creates at least the following names: # -------------------------------------------------------------------- # Type RPython integer class doing wrap-around # -------------------------------------------------------------------- # SIGNEDCHAR r_signedchar # UCHAR r_uchar # SHORT r_short # USHORT r_ushort # INT r_int # UINT r_uint # LONG r_long # ULONG r_ulong
def rtyper_makerepr(self, rtyper): lltype = build_number(None, self.knowntype) return getintegerrepr(lltype)
def _make_type(self, name, signed, size): inttype = rarithmetic.build_int('r_' + name, signed, size * 8) tp = lltype.build_number(name, inttype) self.numbertype_to_rclass[tp] = inttype self.types[name] = tp return tp
def rtyper_makerepr(self, rtyper): lltype = build_number(None, self.knowntype) return getintegerrepr(lltype)
def _make_type(self, name, signed, size): inttype = rarithmetic.build_int('r_' + name, signed, size*8) tp = lltype.build_number(name, inttype) self.numbertype_to_rclass[tp] = inttype self.types[name] = tp return tp