| 1 |
""" |
| 2 |
Code for annotating low-level thingies. |
| 3 |
""" |
| 4 |
from rpython.tool.pairtype import pair, pairtype |
| 5 |
from rpython.annotator.model import ( |
| 6 |
SomeObject, SomeSingleFloat, SomeFloat, SomeLongFloat, SomeChar, |
| 7 |
SomeUnicodeCodePoint, SomeInteger, SomeImpossibleValue, |
| 8 |
s_None, s_Bool, UnionError, AnnotatorError) |
| 9 |
from rpython.rtyper.lltypesystem import lltype, llmemory |
| 10 |
from rpython.rtyper.lltypesystem.lltype import SomePtr |
| 11 |
from rpython.rtyper.lltypesystem.llmemory import ( |
| 12 |
SomeAddress, SomeTypedAddressAccess) |
| 13 |
|
| 14 |
|
| 15 |
class __extend__(pairtype(SomeAddress, SomeAddress)): |
| 16 |
def union((s_addr1, s_addr2)): |
| 17 |
return SomeAddress() |
| 18 |
|
| 19 |
def sub((s_addr1, s_addr2)): |
| 20 |
from rpython.annotator.bookkeeper import getbookkeeper |
| 21 |
if s_addr1.is_null_address() and s_addr2.is_null_address(): |
| 22 |
return getbookkeeper().immutablevalue(0) |
| 23 |
return SomeInteger() |
| 24 |
|
| 25 |
def is_((s_addr1, s_addr2)): |
| 26 |
assert False, "comparisons with is not supported by addresses" |
| 27 |
|
| 28 |
class __extend__(pairtype(SomeTypedAddressAccess, SomeTypedAddressAccess)): |
| 29 |
def union((s_taa1, s_taa2)): |
| 30 |
assert s_taa1.type == s_taa2.type |
| 31 |
return s_taa1 |
| 32 |
|
| 33 |
class __extend__(pairtype(SomeTypedAddressAccess, SomeInteger)): |
| 34 |
def getitem((s_taa, s_int)): |
| 35 |
return lltype_to_annotation(s_taa.type) |
| 36 |
getitem.can_only_throw = [] |
| 37 |
|
| 38 |
def setitem((s_taa, s_int), s_value): |
| 39 |
assert annotation_to_lltype(s_value) is s_taa.type |
| 40 |
setitem.can_only_throw = [] |
| 41 |
|
| 42 |
|
| 43 |
class __extend__(pairtype(SomeAddress, SomeInteger)): |
| 44 |
def add((s_addr, s_int)): |
| 45 |
return SomeAddress() |
| 46 |
|
| 47 |
def sub((s_addr, s_int)): |
| 48 |
return SomeAddress() |
| 49 |
|
| 50 |
class __extend__(pairtype(SomeAddress, SomeImpossibleValue)): |
| 51 |
|
| 52 |
|
| 53 |
def union((s_addr, s_imp)): |
| 54 |
return s_addr |
| 55 |
|
| 56 |
class __extend__(pairtype(SomeImpossibleValue, SomeAddress)): |
| 57 |
|
| 58 |
|
| 59 |
def union((s_imp, s_addr)): |
| 60 |
return s_addr |
| 61 |
|
| 62 |
class __extend__(pairtype(SomeAddress, SomeObject)): |
| 63 |
def union((s_addr, s_obj)): |
| 64 |
raise UnionError(s_addr, s_obj) |
| 65 |
|
| 66 |
class __extend__(pairtype(SomeObject, SomeAddress)): |
| 67 |
def union((s_obj, s_addr)): |
| 68 |
raise UnionError(s_obj, s_addr) |
| 69 |
|
| 70 |
|
| 71 |
class SomeInteriorPtr(SomePtr): |
| 72 |
def __init__(self, ll_ptrtype): |
| 73 |
assert isinstance(ll_ptrtype, lltype.InteriorPtr) |
| 74 |
self.ll_ptrtype = ll_ptrtype |
| 75 |
|
| 76 |
|
| 77 |
class SomeLLADTMeth(SomeObject): |
| 78 |
immutable = True |
| 79 |
|
| 80 |
def __init__(self, ll_ptrtype, func): |
| 81 |
self.ll_ptrtype = ll_ptrtype |
| 82 |
self.func = func |
| 83 |
|
| 84 |
def can_be_none(self): |
| 85 |
return False |
| 86 |
|
| 87 |
def call(self, args): |
| 88 |
from rpython.annotator.bookkeeper import getbookkeeper |
| 89 |
bookkeeper = getbookkeeper() |
| 90 |
s_func = bookkeeper.immutablevalue(self.func) |
| 91 |
return s_func.call(args.prepend(lltype_to_annotation(self.ll_ptrtype))) |
| 92 |
|
| 93 |
|
| 94 |
class __extend__(pairtype(SomePtr, SomePtr)): |
| 95 |
def union((p1, p2)): |
| 96 |
if p1.ll_ptrtype != p2.ll_ptrtype: |
| 97 |
raise UnionError(p1, p2) |
| 98 |
return SomePtr(p1.ll_ptrtype) |
| 99 |
|
| 100 |
class __extend__(pairtype(SomePtr, SomeInteger)): |
| 101 |
|
| 102 |
def getitem((p, int1)): |
| 103 |
example = p.ll_ptrtype._example() |
| 104 |
try: |
| 105 |
v = example[0] |
| 106 |
except IndexError: |
| 107 |
return None |
| 108 |
return ll_to_annotation(v) |
| 109 |
getitem.can_only_throw = [] |
| 110 |
|
| 111 |
def setitem((p, int1), s_value): |
| 112 |
example = p.ll_ptrtype._example() |
| 113 |
if example[0] is not None: |
| 114 |
v_lltype = annotation_to_lltype(s_value) |
| 115 |
example[0] = v_lltype._defl() |
| 116 |
setitem.can_only_throw = [] |
| 117 |
|
| 118 |
class __extend__(pairtype(SomePtr, SomeObject)): |
| 119 |
def union((p, obj)): |
| 120 |
raise UnionError(p, obj) |
| 121 |
|
| 122 |
def getitem((p, obj)): |
| 123 |
raise AnnotatorError("ptr %r getitem index not an int: %r" % |
| 124 |
(p.ll_ptrtype, obj)) |
| 125 |
|
| 126 |
def setitem((p, obj), s_value): |
| 127 |
raise AnnotatorError("ptr %r setitem index not an int: %r" % |
| 128 |
(p.ll_ptrtype, obj)) |
| 129 |
|
| 130 |
class __extend__(pairtype(SomeObject, SomePtr)): |
| 131 |
def union((obj, p2)): |
| 132 |
return pair(p2, obj).union() |
| 133 |
|
| 134 |
|
| 135 |
annotation_to_ll_map = [ |
| 136 |
(SomeSingleFloat(), lltype.SingleFloat), |
| 137 |
(s_None, lltype.Void), |
| 138 |
(s_Bool, lltype.Bool), |
| 139 |
(SomeFloat(), lltype.Float), |
| 140 |
(SomeLongFloat(), lltype.LongFloat), |
| 141 |
(SomeChar(), lltype.Char), |
| 142 |
(SomeUnicodeCodePoint(), lltype.UniChar), |
| 143 |
(SomeAddress(), llmemory.Address), |
| 144 |
] |
| 145 |
|
| 146 |
|
| 147 |
def annotation_to_lltype(s_val, info=None): |
| 148 |
if isinstance(s_val, SomeInteriorPtr): |
| 149 |
p = s_val.ll_ptrtype |
| 150 |
if 0 in p.offsets: |
| 151 |
assert list(p.offsets).count(0) == 1 |
| 152 |
return lltype.Ptr(lltype.Ptr(p.PARENTTYPE)._interior_ptr_type_with_index(p.TO)) |
| 153 |
else: |
| 154 |
return lltype.Ptr(p.PARENTTYPE) |
| 155 |
if isinstance(s_val, SomePtr): |
| 156 |
return s_val.ll_ptrtype |
| 157 |
if type(s_val) is SomeInteger: |
| 158 |
return lltype.build_number(None, s_val.knowntype) |
| 159 |
|
| 160 |
for witness, T in annotation_to_ll_map: |
| 161 |
if witness.contains(s_val): |
| 162 |
return T |
| 163 |
if info is None: |
| 164 |
info = '' |
| 165 |
else: |
| 166 |
info = '%s: ' % info |
| 167 |
raise ValueError("%sshould return a low-level type,\ngot instead %r" % ( |
| 168 |
info, s_val)) |
| 169 |
|
| 170 |
ll_to_annotation_map = dict([(ll, ann) for ann, ll in annotation_to_ll_map]) |
| 171 |
|
| 172 |
def lltype_to_annotation(T): |
| 173 |
try: |
| 174 |
s = ll_to_annotation_map.get(T) |
| 175 |
except TypeError: |
| 176 |
s = None |
| 177 |
if s is None: |
| 178 |
if isinstance(T, lltype.Typedef): |
| 179 |
return lltype_to_annotation(T.OF) |
| 180 |
if isinstance(T, lltype.Number): |
| 181 |
return SomeInteger(knowntype=T._type) |
| 182 |
elif isinstance(T, lltype.InteriorPtr): |
| 183 |
return SomeInteriorPtr(T) |
| 184 |
else: |
| 185 |
return SomePtr(T) |
| 186 |
else: |
| 187 |
return s |
| 188 |
|
| 189 |
|
| 190 |
def ll_to_annotation(v): |
| 191 |
if v is None: |
| 192 |
|
| 193 |
|
| 194 |
return s_None |
| 195 |
if isinstance(v, lltype._interior_ptr): |
| 196 |
ob = v._parent |
| 197 |
if ob is None: |
| 198 |
raise RuntimeError |
| 199 |
T = lltype.InteriorPtr(lltype.typeOf(ob), v._T, v._offsets) |
| 200 |
return SomeInteriorPtr(T) |
| 201 |
return lltype_to_annotation(lltype.typeOf(v)) |