| 1 |
import types |
| 2 |
|
| 3 |
from rpython.flowspace.model import FunctionGraph, Link, Block, SpaceOperation |
| 4 |
from rpython.annotator import model as annmodel |
| 5 |
from rpython.annotator.description import ( |
| 6 |
FunctionDesc, MethodDesc, FrozenDesc, MethodOfFrozenDesc) |
| 7 |
from rpython.annotator.classdesc import ClassDesc |
| 8 |
from rpython.flowspace.model import Constant |
| 9 |
from rpython.annotator.argument import simple_args |
| 10 |
from rpython.rtyper.debug import ll_assert |
| 11 |
from rpython.rlib.unroll import unrolling_iterable |
| 12 |
from rpython.rtyper import rclass, callparse |
| 13 |
from rpython.rtyper.rclass import CLASSTYPE, OBJECT_VTABLE, OBJECTPTR |
| 14 |
from rpython.rtyper.error import TyperError |
| 15 |
from rpython.rtyper.lltypesystem import rffi |
| 16 |
from rpython.rtyper.lltypesystem import llmemory |
| 17 |
from rpython.rtyper.lltypesystem.lltype import ( |
| 18 |
typeOf, Void, ForwardReference, Struct, Bool, Char, Ptr, malloc, nullptr, |
| 19 |
Array, Signed, cast_pointer, getfunctionptr, cast_ptr_to_int) |
| 20 |
from rpython.rtyper.rmodel import (Repr, inputconst, CanBeNull, mangle, |
| 21 |
warning, impossible_repr) |
| 22 |
from rpython.tool.pairtype import pair, pairtype |
| 23 |
from rpython.translator.unsimplify import varoftype |
| 24 |
|
| 25 |
|
| 26 |
def small_cand(rtyper, s_pbc): |
| 27 |
if 1 < len(s_pbc.descriptions) < rtyper.getconfig().translation.withsmallfuncsets: |
| 28 |
callfamily = s_pbc.any_description().getcallfamily() |
| 29 |
llct = get_concrete_calltable(rtyper, callfamily) |
| 30 |
if (len(llct.uniquerows) == 1 and |
| 31 |
(not s_pbc.subset_of or small_cand(rtyper, s_pbc.subset_of))): |
| 32 |
return True |
| 33 |
return False |
| 34 |
|
| 35 |
class __extend__(annmodel.SomePBC): |
| 36 |
def rtyper_makerepr(self, rtyper): |
| 37 |
kind = self.getKind() |
| 38 |
if issubclass(kind, FunctionDesc): |
| 39 |
if len(self.descriptions) == 1 and not self.can_be_None: |
| 40 |
getRepr = FunctionRepr |
| 41 |
else: |
| 42 |
sample = self.any_description() |
| 43 |
callfamily = sample.querycallfamily() |
| 44 |
if callfamily and callfamily.total_calltable_size > 0: |
| 45 |
getRepr = FunctionsPBCRepr |
| 46 |
if small_cand(rtyper, self): |
| 47 |
getRepr = SmallFunctionSetPBCRepr |
| 48 |
else: |
| 49 |
getRepr = getFrozenPBCRepr |
| 50 |
elif issubclass(kind, ClassDesc): |
| 51 |
|
| 52 |
getRepr = ClassesPBCRepr |
| 53 |
elif issubclass(kind, MethodDesc): |
| 54 |
getRepr = MethodsPBCRepr |
| 55 |
elif issubclass(kind, FrozenDesc): |
| 56 |
getRepr = getFrozenPBCRepr |
| 57 |
elif issubclass(kind, MethodOfFrozenDesc): |
| 58 |
getRepr = MethodOfFrozenPBCRepr |
| 59 |
else: |
| 60 |
raise TyperError("unexpected PBC kind %r" % (kind,)) |
| 61 |
|
| 62 |
return getRepr(rtyper, self) |
| 63 |
|
| 64 |
def rtyper_makekey(self): |
| 65 |
lst = list(self.descriptions) |
| 66 |
lst.sort() |
| 67 |
if self.subset_of: |
| 68 |
t = self.subset_of.rtyper_makekey() |
| 69 |
else: |
| 70 |
t = () |
| 71 |
return tuple([self.__class__, self.can_be_None] + lst) + t |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
class ConcreteCallTableRow(dict): |
| 76 |
"""A row in a concrete call table.""" |
| 77 |
@classmethod |
| 78 |
def from_row(cls, rtyper, row): |
| 79 |
concreterow = cls() |
| 80 |
for funcdesc, graph in row.items(): |
| 81 |
llfn = rtyper.getcallable(graph) |
| 82 |
concreterow[funcdesc] = llfn |
| 83 |
assert len(concreterow) > 0 |
| 84 |
|
| 85 |
concreterow.fntype = typeOf(llfn) |
| 86 |
return concreterow |
| 87 |
|
| 88 |
|
| 89 |
class LLCallTable(object): |
| 90 |
"""A call table of a call family with low-level functions.""" |
| 91 |
def __init__(self, table, uniquerows): |
| 92 |
self.table = table |
| 93 |
self.uniquerows = uniquerows |
| 94 |
|
| 95 |
def lookup(self, row): |
| 96 |
"""A 'matching' row is one that has the same llfn, except |
| 97 |
that it may have more or less 'holes'.""" |
| 98 |
for existingindex, existingrow in enumerate(self.uniquerows): |
| 99 |
if row.fntype != existingrow.fntype: |
| 100 |
continue |
| 101 |
for funcdesc, llfn in row.items(): |
| 102 |
if funcdesc in existingrow: |
| 103 |
if llfn != existingrow[funcdesc]: |
| 104 |
break |
| 105 |
else: |
| 106 |
|
| 107 |
merged = ConcreteCallTableRow(row) |
| 108 |
merged.update(existingrow) |
| 109 |
merged.fntype = row.fntype |
| 110 |
if len(merged) == len(row) + len(existingrow): |
| 111 |
pass |
| 112 |
else: |
| 113 |
return existingindex, merged |
| 114 |
raise LookupError |
| 115 |
|
| 116 |
def add(self, row): |
| 117 |
"""Add a row to the table, potentially merging it with an existing row |
| 118 |
""" |
| 119 |
try: |
| 120 |
index, merged = self.lookup(row) |
| 121 |
except LookupError: |
| 122 |
self.uniquerows.append(row) |
| 123 |
else: |
| 124 |
if merged == self.uniquerows[index]: |
| 125 |
pass |
| 126 |
else: |
| 127 |
del self.uniquerows[index] |
| 128 |
self.add(merged) |
| 129 |
|
| 130 |
|
| 131 |
def build_concrete_calltable(rtyper, callfamily): |
| 132 |
"""Build a complete call table of a call family |
| 133 |
with concrete low-level function objs. |
| 134 |
""" |
| 135 |
concretetable = {} |
| 136 |
uniquerows = [] |
| 137 |
llct = LLCallTable(concretetable, uniquerows) |
| 138 |
|
| 139 |
concreterows = {} |
| 140 |
for shape, rows in callfamily.calltables.items(): |
| 141 |
for index, row in enumerate(rows): |
| 142 |
concreterow = ConcreteCallTableRow.from_row(rtyper, row) |
| 143 |
concreterows[shape, index] = concreterow |
| 144 |
llct.add(concreterow) |
| 145 |
|
| 146 |
for (shape, index), row in concreterows.items(): |
| 147 |
existingindex, biggerrow = llct.lookup(row) |
| 148 |
row = llct.uniquerows[existingindex] |
| 149 |
assert biggerrow == row |
| 150 |
llct.table[shape, index] = row |
| 151 |
|
| 152 |
if len(llct.uniquerows) == 1: |
| 153 |
llct.uniquerows[0].attrname = None |
| 154 |
else: |
| 155 |
for finalindex, row in enumerate(llct.uniquerows): |
| 156 |
row.attrname = 'variant%d' % finalindex |
| 157 |
|
| 158 |
return llct |
| 159 |
|
| 160 |
def get_concrete_calltable(rtyper, callfamily): |
| 161 |
"""Get a complete call table of a call family |
| 162 |
with concrete low-level function objs. |
| 163 |
""" |
| 164 |
|
| 165 |
try: |
| 166 |
cached = rtyper.concrete_calltables[callfamily] |
| 167 |
except KeyError: |
| 168 |
llct = build_concrete_calltable(rtyper, callfamily) |
| 169 |
cached = llct, callfamily.total_calltable_size |
| 170 |
rtyper.concrete_calltables[callfamily] = cached |
| 171 |
else: |
| 172 |
llct, oldsize = cached |
| 173 |
if oldsize != callfamily.total_calltable_size: |
| 174 |
raise TyperError("call table was unexpectedly extended") |
| 175 |
return llct |
| 176 |
|
| 177 |
class FunctionReprBase(Repr): |
| 178 |
def __init__(self, rtyper, s_pbc): |
| 179 |
self.rtyper = rtyper |
| 180 |
self.s_pbc = s_pbc |
| 181 |
self.callfamily = s_pbc.any_description().getcallfamily() |
| 182 |
|
| 183 |
def get_s_callable(self): |
| 184 |
return self.s_pbc |
| 185 |
|
| 186 |
def get_r_implfunc(self): |
| 187 |
return self, 0 |
| 188 |
|
| 189 |
def get_s_signatures(self, shape): |
| 190 |
funcdesc = self.s_pbc.any_description() |
| 191 |
return funcdesc.get_s_signatures(shape) |
| 192 |
|
| 193 |
def rtype_simple_call(self, hop): |
| 194 |
return self.call(hop) |
| 195 |
|
| 196 |
def rtype_call_args(self, hop): |
| 197 |
return self.call(hop) |
| 198 |
|
| 199 |
def call(self, hop): |
| 200 |
bk = self.rtyper.annotator.bookkeeper |
| 201 |
args = hop.spaceop.build_args(hop.args_s[1:]) |
| 202 |
s_pbc = hop.args_s[0] |
| 203 |
descs = list(s_pbc.descriptions) |
| 204 |
shape, index = self.callfamily.find_row(bk, descs, args, hop.spaceop) |
| 205 |
row_of_graphs = self.callfamily.calltables[shape][index] |
| 206 |
anygraph = row_of_graphs.itervalues().next() |
| 207 |
vfn = hop.inputarg(self, arg=0) |
| 208 |
vlist = [self.convert_to_concrete_llfn(vfn, shape, index, |
| 209 |
hop.llops)] |
| 210 |
vlist += callparse.callparse(self.rtyper, anygraph, hop) |
| 211 |
rresult = callparse.getrresult(self.rtyper, anygraph) |
| 212 |
hop.exception_is_here() |
| 213 |
if isinstance(vlist[0], Constant): |
| 214 |
v = hop.genop('direct_call', vlist, resulttype=rresult) |
| 215 |
else: |
| 216 |
vlist.append(hop.inputconst(Void, row_of_graphs.values())) |
| 217 |
v = hop.genop('indirect_call', vlist, resulttype=rresult) |
| 218 |
if hop.r_result is impossible_repr: |
| 219 |
return None |
| 220 |
else: |
| 221 |
return hop.llops.convertvar(v, rresult, hop.r_result) |
| 222 |
|
| 223 |
|
| 224 |
class FunctionsPBCRepr(CanBeNull, FunctionReprBase): |
| 225 |
"""Representation selected for a PBC of functions.""" |
| 226 |
|
| 227 |
def __init__(self, rtyper, s_pbc): |
| 228 |
FunctionReprBase.__init__(self, rtyper, s_pbc) |
| 229 |
llct = get_concrete_calltable(self.rtyper, self.callfamily) |
| 230 |
self.concretetable = llct.table |
| 231 |
self.uniquerows = llct.uniquerows |
| 232 |
if len(llct.uniquerows) == 1: |
| 233 |
row = llct.uniquerows[0] |
| 234 |
self.lowleveltype = row.fntype |
| 235 |
else: |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
self.lowleveltype = self.setup_specfunc() |
| 240 |
self.funccache = {} |
| 241 |
|
| 242 |
def setup_specfunc(self): |
| 243 |
fields = [] |
| 244 |
for row in self.uniquerows: |
| 245 |
fields.append((row.attrname, row.fntype)) |
| 246 |
kwds = {'hints': {'immutable': True, 'static_immutable': True}} |
| 247 |
return Ptr(Struct('specfunc', *fields, **kwds)) |
| 248 |
|
| 249 |
def create_specfunc(self): |
| 250 |
return malloc(self.lowleveltype.TO, immortal=True) |
| 251 |
|
| 252 |
def get_specfunc_row(self, llop, v, c_rowname, resulttype): |
| 253 |
return llop.genop('getfield', [v, c_rowname], resulttype=resulttype) |
| 254 |
|
| 255 |
def convert_desc(self, funcdesc): |
| 256 |
|
| 257 |
try: |
| 258 |
return self.funccache[funcdesc] |
| 259 |
except KeyError: |
| 260 |
pass |
| 261 |
llfns = {} |
| 262 |
found_anything = False |
| 263 |
for row in self.uniquerows: |
| 264 |
if funcdesc in row: |
| 265 |
llfn = row[funcdesc] |
| 266 |
found_anything = True |
| 267 |
else: |
| 268 |
|
| 269 |
|
| 270 |
llfn = nullptr(row.fntype.TO) |
| 271 |
llfns[row.attrname] = llfn |
| 272 |
if len(self.uniquerows) == 1: |
| 273 |
if found_anything: |
| 274 |
result = llfn |
| 275 |
else: |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
result = rffi.cast(self.lowleveltype, ~len(self.funccache)) |
| 281 |
else: |
| 282 |
|
| 283 |
result = self.create_specfunc() |
| 284 |
for attrname, llfn in llfns.items(): |
| 285 |
setattr(result, attrname, llfn) |
| 286 |
self.funccache[funcdesc] = result |
| 287 |
return result |
| 288 |
|
| 289 |
def convert_const(self, value): |
| 290 |
if isinstance(value, types.MethodType) and value.im_self is None: |
| 291 |
value = value.im_func |
| 292 |
elif isinstance(value, staticmethod): |
| 293 |
value = value.__get__(42) |
| 294 |
if value is None: |
| 295 |
null = nullptr(self.lowleveltype.TO) |
| 296 |
return null |
| 297 |
funcdesc = self.rtyper.annotator.bookkeeper.getdesc(value) |
| 298 |
return self.convert_desc(funcdesc) |
| 299 |
|
| 300 |
def convert_to_concrete_llfn(self, v, shape, index, llop): |
| 301 |
"""Convert the variable 'v' to a variable referring to a concrete |
| 302 |
low-level function. In case the call table contains multiple rows, |
| 303 |
'index' and 'shape' tells which of its items we are interested in. |
| 304 |
""" |
| 305 |
assert v.concretetype == self.lowleveltype |
| 306 |
if len(self.uniquerows) == 1: |
| 307 |
return v |
| 308 |
else: |
| 309 |
|
| 310 |
row = self.concretetable[shape, index] |
| 311 |
cname = inputconst(Void, row.attrname) |
| 312 |
return self.get_specfunc_row(llop, v, cname, row.fntype) |
| 313 |
|
| 314 |
|
| 315 |
class FunctionRepr(FunctionReprBase): |
| 316 |
"""Repr for a constant function""" |
| 317 |
|
| 318 |
lowleveltype = Void |
| 319 |
|
| 320 |
def convert_desc(self, funcdesc): |
| 321 |
return None |
| 322 |
|
| 323 |
def convert_const(self, value): |
| 324 |
return None |
| 325 |
|
| 326 |
def convert_to_concrete_llfn(self, v, shape, index, llop): |
| 327 |
"""Convert the variable 'v' to a variable referring to a concrete |
| 328 |
low-level function. In case the call table contains multiple rows, |
| 329 |
'index' and 'shape' tells which of its items we are interested in. |
| 330 |
""" |
| 331 |
assert v.concretetype == Void |
| 332 |
funcdesc, = self.s_pbc.descriptions |
| 333 |
row_of_one_graph = self.callfamily.calltables[shape][index] |
| 334 |
graph = row_of_one_graph[funcdesc] |
| 335 |
llfn = self.rtyper.getcallable(graph) |
| 336 |
return inputconst(typeOf(llfn), llfn) |
| 337 |
|
| 338 |
def get_unique_llfn(self): |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
funcdesc, = self.s_pbc.descriptions |
| 343 |
tables = [] |
| 344 |
for shape, table in self.callfamily.calltables.items(): |
| 345 |
if not shape[1] and not shape[2]: |
| 346 |
tables.append(table) |
| 347 |
if len(tables) != 1: |
| 348 |
raise TyperError("cannot pass a function with various call shapes") |
| 349 |
table, = tables |
| 350 |
graphs = [] |
| 351 |
for row in table: |
| 352 |
if funcdesc in row: |
| 353 |
graphs.append(row[funcdesc]) |
| 354 |
if not graphs: |
| 355 |
raise TyperError("cannot pass here a function that is not called") |
| 356 |
graph = graphs[0] |
| 357 |
if graphs != [graph] * len(graphs): |
| 358 |
raise TyperError("cannot pass a specialized function here") |
| 359 |
llfn = self.rtyper.getcallable(graph) |
| 360 |
return inputconst(typeOf(llfn), llfn) |
| 361 |
|
| 362 |
def get_concrete_llfn(self, s_pbc, args_s, op): |
| 363 |
bk = self.rtyper.annotator.bookkeeper |
| 364 |
funcdesc, = s_pbc.descriptions |
| 365 |
with bk.at_position(None): |
| 366 |
argspec = simple_args(args_s) |
| 367 |
graph = funcdesc.get_graph(argspec, op) |
| 368 |
llfn = self.rtyper.getcallable(graph) |
| 369 |
return inputconst(typeOf(llfn), llfn) |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
class __extend__(pairtype(FunctionRepr, FunctionRepr)): |
| 374 |
def convert_from_to((r_fpbc1, r_fpbc2), v, llops): |
| 375 |
return v |
| 376 |
|
| 377 |
class __extend__(pairtype(FunctionRepr, FunctionsPBCRepr)): |
| 378 |
def convert_from_to((r_fpbc1, r_fpbc2), v, llops): |
| 379 |
return inputconst(r_fpbc2, r_fpbc1.s_pbc.const) |
| 380 |
|
| 381 |
class __extend__(pairtype(FunctionsPBCRepr, FunctionRepr)): |
| 382 |
def convert_from_to((r_fpbc1, r_fpbc2), v, llops): |
| 383 |
return inputconst(Void, None) |
| 384 |
|
| 385 |
class __extend__(pairtype(FunctionsPBCRepr, FunctionsPBCRepr)): |
| 386 |
def convert_from_to((r_fpbc1, r_fpbc2), v, llops): |
| 387 |
|
| 388 |
if r_fpbc1.lowleveltype == r_fpbc2.lowleveltype: |
| 389 |
return v |
| 390 |
return NotImplemented |
| 391 |
|
| 392 |
|
| 393 |
class SmallFunctionSetPBCRepr(FunctionReprBase): |
| 394 |
def __init__(self, rtyper, s_pbc): |
| 395 |
FunctionReprBase.__init__(self, rtyper, s_pbc) |
| 396 |
llct = get_concrete_calltable(self.rtyper, self.callfamily) |
| 397 |
assert len(llct.uniquerows) == 1 |
| 398 |
self.lowleveltype = Char |
| 399 |
self.pointer_repr = FunctionsPBCRepr(rtyper, s_pbc) |
| 400 |
self._conversion_tables = {} |
| 401 |
self._compression_function = None |
| 402 |
self._dispatch_cache = {} |
| 403 |
|
| 404 |
def _setup_repr(self): |
| 405 |
if self.s_pbc.subset_of: |
| 406 |
assert self.s_pbc.can_be_None == self.s_pbc.subset_of.can_be_None |
| 407 |
r = self.rtyper.getrepr(self.s_pbc.subset_of) |
| 408 |
if r is not self: |
| 409 |
r.setup() |
| 410 |
self.descriptions = r.descriptions |
| 411 |
self.c_pointer_table = r.c_pointer_table |
| 412 |
return |
| 413 |
self.descriptions = list(self.s_pbc.descriptions) |
| 414 |
if self.s_pbc.can_be_None: |
| 415 |
self.descriptions.insert(0, None) |
| 416 |
POINTER_TABLE = Array(self.pointer_repr.lowleveltype, |
| 417 |
hints={'nolength': True, 'immutable': True, |
| 418 |
'static_immutable': True}) |
| 419 |
pointer_table = malloc(POINTER_TABLE, len(self.descriptions), |
| 420 |
immortal=True) |
| 421 |
for i, desc in enumerate(self.descriptions): |
| 422 |
if desc is not None: |
| 423 |
pointer_table[i] = self.pointer_repr.convert_desc(desc) |
| 424 |
else: |
| 425 |
pointer_table[i] = self.pointer_repr.convert_const(None) |
| 426 |
self.c_pointer_table = inputconst(Ptr(POINTER_TABLE), pointer_table) |
| 427 |
|
| 428 |
def convert_desc(self, funcdesc): |
| 429 |
return chr(self.descriptions.index(funcdesc)) |
| 430 |
|
| 431 |
def convert_const(self, value): |
| 432 |
if isinstance(value, types.MethodType) and value.im_self is None: |
| 433 |
value = value.im_func |
| 434 |
if value is None: |
| 435 |
assert self.descriptions[0] is None |
| 436 |
return chr(0) |
| 437 |
funcdesc = self.rtyper.annotator.bookkeeper.getdesc(value) |
| 438 |
return self.convert_desc(funcdesc) |
| 439 |
|
| 440 |
def special_uninitialized_value(self): |
| 441 |
return chr(0xFF) |
| 442 |
|
| 443 |
def dispatcher(self, shape, index, argtypes, resulttype): |
| 444 |
key = shape, index, tuple(argtypes), resulttype |
| 445 |
if key in self._dispatch_cache: |
| 446 |
return self._dispatch_cache[key] |
| 447 |
graph = self.make_dispatcher(shape, index, argtypes, resulttype) |
| 448 |
self.rtyper.annotator.translator.graphs.append(graph) |
| 449 |
ll_ret = getfunctionptr(graph) |
| 450 |
c_ret = self._dispatch_cache[key] = inputconst(typeOf(ll_ret), ll_ret) |
| 451 |
return c_ret |
| 452 |
|
| 453 |
def make_dispatcher(self, shape, index, argtypes, resulttype): |
| 454 |
inputargs = [varoftype(t) for t in [Char] + argtypes] |
| 455 |
startblock = Block(inputargs) |
| 456 |
startblock.exitswitch = inputargs[0] |
| 457 |
graph = FunctionGraph("dispatcher", startblock, varoftype(resulttype)) |
| 458 |
row_of_graphs = self.callfamily.calltables[shape][index] |
| 459 |
links = [] |
| 460 |
descs = list(self.s_pbc.descriptions) |
| 461 |
if self.s_pbc.can_be_None: |
| 462 |
descs.insert(0, None) |
| 463 |
for desc in descs: |
| 464 |
if desc is None: |
| 465 |
continue |
| 466 |
args_v = [varoftype(t) for t in argtypes] |
| 467 |
b = Block(args_v) |
| 468 |
llfn = self.rtyper.getcallable(row_of_graphs[desc]) |
| 469 |
v_fn = inputconst(typeOf(llfn), llfn) |
| 470 |
v_result = varoftype(resulttype) |
| 471 |
b.operations.append( |
| 472 |
SpaceOperation("direct_call", [v_fn] + args_v, v_result)) |
| 473 |
b.closeblock(Link([v_result], graph.returnblock)) |
| 474 |
i = self.descriptions.index(desc) |
| 475 |
links.append(Link(inputargs[1:], b, chr(i))) |
| 476 |
links[-1].llexitcase = chr(i) |
| 477 |
startblock.closeblock(*links) |
| 478 |
graph.name = self._invent_dispatcher_name(row_of_graphs) |
| 479 |
return graph |
| 480 |
|
| 481 |
def _invent_dispatcher_name(self, row): |
| 482 |
import os |
| 483 |
names = [value.name.rsplit(".", 1)[-1] for value in row.itervalues()] |
| 484 |
commonprefix = os.path.commonprefix(names) |
| 485 |
|
| 486 |
if not commonprefix: |
| 487 |
commonprefix = sorted(names)[0] + "_etc" |
| 488 |
return "dispatcher_" + commonprefix |
| 489 |
|
| 490 |
def call(self, hop): |
| 491 |
bk = self.rtyper.annotator.bookkeeper |
| 492 |
args = hop.spaceop.build_args(hop.args_s[1:]) |
| 493 |
s_pbc = hop.args_s[0] |
| 494 |
descs = list(s_pbc.descriptions) |
| 495 |
shape, index = self.callfamily.find_row(bk, descs, args, hop.spaceop) |
| 496 |
row_of_graphs = self.callfamily.calltables[shape][index] |
| 497 |
anygraph = row_of_graphs.itervalues().next() |
| 498 |
vlist = [hop.inputarg(self, arg=0)] |
| 499 |
vlist += callparse.callparse(self.rtyper, anygraph, hop) |
| 500 |
rresult = callparse.getrresult(self.rtyper, anygraph) |
| 501 |
hop.exception_is_here() |
| 502 |
v_dispatcher = self.dispatcher(shape, index, |
| 503 |
[v.concretetype for v in vlist[1:]], rresult.lowleveltype) |
| 504 |
v_result = hop.genop('direct_call', [v_dispatcher] + vlist, |
| 505 |
resulttype=rresult) |
| 506 |
return hop.llops.convertvar(v_result, rresult, hop.r_result) |
| 507 |
|
| 508 |
def rtype_bool(self, hop): |
| 509 |
if not self.s_pbc.can_be_None: |
| 510 |
return inputconst(Bool, True) |
| 511 |
else: |
| 512 |
v1, = hop.inputargs(self) |
| 513 |
return hop.genop('char_ne', [v1, inputconst(Char, '\000')], |
| 514 |
resulttype=Bool) |
| 515 |
|
| 516 |
|
| 517 |
class __extend__(pairtype(SmallFunctionSetPBCRepr, FunctionRepr)): |
| 518 |
def convert_from_to((r_set, r_ptr), v, llops): |
| 519 |
return inputconst(Void, None) |
| 520 |
|
| 521 |
class __extend__(pairtype(SmallFunctionSetPBCRepr, FunctionsPBCRepr)): |
| 522 |
def convert_from_to((r_set, r_ptr), v, llops): |
| 523 |
assert v.concretetype is Char |
| 524 |
v_int = llops.genop('cast_char_to_int', [v], resulttype=Signed) |
| 525 |
return llops.genop('getarrayitem', [r_set.c_pointer_table, v_int], |
| 526 |
resulttype=r_ptr.lowleveltype) |
| 527 |
|
| 528 |
|
| 529 |
def compression_function(r_set): |
| 530 |
if r_set._compression_function is None: |
| 531 |
table = [] |
| 532 |
for i, p in enumerate(r_set.c_pointer_table.value): |
| 533 |
table.append((chr(i), p)) |
| 534 |
last_c, last_p = table[-1] |
| 535 |
unroll_table = unrolling_iterable(table[:-1]) |
| 536 |
|
| 537 |
def ll_compress(fnptr): |
| 538 |
for c, p in unroll_table: |
| 539 |
if fnptr == p: |
| 540 |
return c |
| 541 |
else: |
| 542 |
ll_assert(fnptr == last_p, "unexpected function pointer") |
| 543 |
return last_c |
| 544 |
r_set._compression_function = ll_compress |
| 545 |
return r_set._compression_function |
| 546 |
|
| 547 |
|
| 548 |
class __extend__(pairtype(FunctionRepr, SmallFunctionSetPBCRepr)): |
| 549 |
def convert_from_to((r_ptr, r_set), v, llops): |
| 550 |
desc, = r_ptr.s_pbc.descriptions |
| 551 |
return inputconst(Char, r_set.convert_desc(desc)) |
| 552 |
|
| 553 |
class __extend__(pairtype(FunctionsPBCRepr, SmallFunctionSetPBCRepr)): |
| 554 |
def convert_from_to((r_ptr, r_set), v, llops): |
| 555 |
ll_compress = compression_function(r_set) |
| 556 |
return llops.gendirectcall(ll_compress, v) |
| 557 |
|
| 558 |
class __extend__(pairtype(FunctionReprBase, FunctionReprBase)): |
| 559 |
def rtype_is_((robj1, robj2), hop): |
| 560 |
if hop.s_result.is_constant(): |
| 561 |
return inputconst(Bool, hop.s_result.const) |
| 562 |
s_pbc = annmodel.unionof(robj1.s_pbc, robj2.s_pbc) |
| 563 |
r_pbc = hop.rtyper.getrepr(s_pbc) |
| 564 |
v1, v2 = hop.inputargs(r_pbc, r_pbc) |
| 565 |
assert v1.concretetype == v2.concretetype |
| 566 |
if v1.concretetype == Char: |
| 567 |
return hop.genop('char_eq', [v1, v2], resulttype=Bool) |
| 568 |
elif isinstance(v1.concretetype, Ptr): |
| 569 |
return hop.genop('ptr_eq', [v1, v2], resulttype=Bool) |
| 570 |
else: |
| 571 |
raise TyperError("unknown type %r" % (v1.concretetype,)) |
| 572 |
|
| 573 |
|
| 574 |
def conversion_table(r_from, r_to): |
| 575 |
if r_to in r_from._conversion_tables: |
| 576 |
return r_from._conversion_tables[r_to] |
| 577 |
else: |
| 578 |
t = malloc(Array(Char, hints={'nolength': True, 'immutable': True, |
| 579 |
'static_immutable': True}), |
| 580 |
len(r_from.descriptions), immortal=True) |
| 581 |
l = [] |
| 582 |
for i, d in enumerate(r_from.descriptions): |
| 583 |
if d in r_to.descriptions: |
| 584 |
j = r_to.descriptions.index(d) |
| 585 |
l.append(j) |
| 586 |
t[i] = chr(j) |
| 587 |
else: |
| 588 |
l.append(None) |
| 589 |
if l == range(len(r_from.descriptions)): |
| 590 |
r = None |
| 591 |
else: |
| 592 |
r = inputconst(typeOf(t), t) |
| 593 |
r_from._conversion_tables[r_to] = r |
| 594 |
return r |
| 595 |
|
| 596 |
|
| 597 |
class __extend__(pairtype(SmallFunctionSetPBCRepr, SmallFunctionSetPBCRepr)): |
| 598 |
def convert_from_to((r_from, r_to), v, llops): |
| 599 |
c_table = conversion_table(r_from, r_to) |
| 600 |
if c_table: |
| 601 |
assert v.concretetype is Char |
| 602 |
v_int = llops.genop('cast_char_to_int', [v], |
| 603 |
resulttype=Signed) |
| 604 |
return llops.genop('getarrayitem', [c_table, v_int], |
| 605 |
resulttype=Char) |
| 606 |
else: |
| 607 |
return v |
| 608 |
|
| 609 |
|
| 610 |
def getFrozenPBCRepr(rtyper, s_pbc): |
| 611 |
descs = list(s_pbc.descriptions) |
| 612 |
assert len(descs) >= 1 |
| 613 |
if len(descs) == 1 and not s_pbc.can_be_None: |
| 614 |
return SingleFrozenPBCRepr(descs[0]) |
| 615 |
else: |
| 616 |
access = descs[0].queryattrfamily() |
| 617 |
for desc in descs[1:]: |
| 618 |
access1 = desc.queryattrfamily() |
| 619 |
if access1 is not access: |
| 620 |
try: |
| 621 |
return rtyper.pbc_reprs['unrelated'] |
| 622 |
except KeyError: |
| 623 |
result = MultipleUnrelatedFrozenPBCRepr(rtyper) |
| 624 |
rtyper.pbc_reprs['unrelated'] = result |
| 625 |
return result |
| 626 |
try: |
| 627 |
return rtyper.pbc_reprs[access] |
| 628 |
except KeyError: |
| 629 |
result = MultipleFrozenPBCRepr(rtyper, access) |
| 630 |
rtyper.pbc_reprs[access] = result |
| 631 |
rtyper.add_pendingsetup(result) |
| 632 |
return result |
| 633 |
|
| 634 |
|
| 635 |
class SingleFrozenPBCRepr(Repr): |
| 636 |
"""Representation selected for a single non-callable pre-built constant.""" |
| 637 |
lowleveltype = Void |
| 638 |
|
| 639 |
def __init__(self, frozendesc): |
| 640 |
self.frozendesc = frozendesc |
| 641 |
|
| 642 |
def rtype_getattr(_, hop): |
| 643 |
if not hop.s_result.is_constant(): |
| 644 |
raise TyperError("getattr on a constant PBC returns a non-constant") |
| 645 |
return hop.inputconst(hop.r_result, hop.s_result.const) |
| 646 |
|
| 647 |
def convert_desc(self, frozendesc): |
| 648 |
assert frozendesc is self.frozendesc |
| 649 |
return object() |
| 650 |
|
| 651 |
def convert_const(self, value): |
| 652 |
return None |
| 653 |
|
| 654 |
def getstr(self): |
| 655 |
return str(self.frozendesc) |
| 656 |
getstr._annspecialcase_ = 'specialize:memo' |
| 657 |
|
| 658 |
def ll_str(self, x): |
| 659 |
return self.getstr() |
| 660 |
|
| 661 |
def get_ll_eq_function(self): |
| 662 |
return None |
| 663 |
|
| 664 |
|
| 665 |
class MultipleFrozenPBCReprBase(CanBeNull, Repr): |
| 666 |
def convert_const(self, pbc): |
| 667 |
if pbc is None: |
| 668 |
return self.null_instance() |
| 669 |
frozendesc = self.rtyper.annotator.bookkeeper.getdesc(pbc) |
| 670 |
return self.convert_desc(frozendesc) |
| 671 |
|
| 672 |
def get_ll_eq_function(self): |
| 673 |
return None |
| 674 |
|
| 675 |
class MultipleUnrelatedFrozenPBCRepr(MultipleFrozenPBCReprBase): |
| 676 |
"""For a SomePBC of frozen PBCs that have no common access set. |
| 677 |
The only possible operation on such a thing is comparison with 'is'.""" |
| 678 |
lowleveltype = llmemory.Address |
| 679 |
EMPTY = Struct('pbc', hints={'immutable': True, 'static_immutable': True}) |
| 680 |
|
| 681 |
def __init__(self, rtyper): |
| 682 |
self.rtyper = rtyper |
| 683 |
self.converted_pbc_cache = {} |
| 684 |
|
| 685 |
def convert_desc(self, frozendesc): |
| 686 |
try: |
| 687 |
return self.converted_pbc_cache[frozendesc] |
| 688 |
except KeyError: |
| 689 |
r = self.rtyper.getrepr(annmodel.SomePBC([frozendesc])) |
| 690 |
if r.lowleveltype is Void: |
| 691 |
|
| 692 |
pbc = self.create_instance() |
| 693 |
else: |
| 694 |
pbc = r.convert_desc(frozendesc) |
| 695 |
convpbc = self.convert_pbc(pbc) |
| 696 |
self.converted_pbc_cache[frozendesc] = convpbc |
| 697 |
return convpbc |
| 698 |
|
| 699 |
def convert_pbc(self, pbcptr): |
| 700 |
return llmemory.fakeaddress(pbcptr) |
| 701 |
|
| 702 |
def create_instance(self): |
| 703 |
return malloc(self.EMPTY, immortal=True) |
| 704 |
|
| 705 |
def null_instance(self): |
| 706 |
return llmemory.Address._defl() |
| 707 |
|
| 708 |
def rtype_getattr(_, hop): |
| 709 |
if not hop.s_result.is_constant(): |
| 710 |
raise TyperError("getattr on a constant PBC returns a non-constant") |
| 711 |
return hop.inputconst(hop.r_result, hop.s_result.const) |
| 712 |
|
| 713 |
class __extend__(pairtype(MultipleUnrelatedFrozenPBCRepr, |
| 714 |
MultipleUnrelatedFrozenPBCRepr), |
| 715 |
pairtype(MultipleUnrelatedFrozenPBCRepr, |
| 716 |
SingleFrozenPBCRepr), |
| 717 |
pairtype(SingleFrozenPBCRepr, |
| 718 |
MultipleUnrelatedFrozenPBCRepr)): |
| 719 |
def rtype_is_((robj1, robj2), hop): |
| 720 |
if isinstance(robj1, MultipleUnrelatedFrozenPBCRepr): |
| 721 |
r = robj1 |
| 722 |
else: |
| 723 |
r = robj2 |
| 724 |
vlist = hop.inputargs(r, r) |
| 725 |
return hop.genop('adr_eq', vlist, resulttype=Bool) |
| 726 |
|
| 727 |
|
| 728 |
class MultipleFrozenPBCRepr(MultipleFrozenPBCReprBase): |
| 729 |
"""For a SomePBC of frozen PBCs with a common attribute access set.""" |
| 730 |
|
| 731 |
def __init__(self, rtyper, access_set): |
| 732 |
self.rtyper = rtyper |
| 733 |
self.access_set = access_set |
| 734 |
self.pbc_type = ForwardReference() |
| 735 |
self.lowleveltype = Ptr(self.pbc_type) |
| 736 |
self.pbc_cache = {} |
| 737 |
|
| 738 |
def _setup_repr(self): |
| 739 |
llfields = self._setup_repr_fields() |
| 740 |
kwds = {'hints': {'immutable': True, 'static_immutable': True}} |
| 741 |
self.pbc_type.become(Struct('pbc', *llfields, **kwds)) |
| 742 |
|
| 743 |
def create_instance(self): |
| 744 |
return malloc(self.pbc_type, immortal=True) |
| 745 |
|
| 746 |
def null_instance(self): |
| 747 |
return nullptr(self.pbc_type) |
| 748 |
|
| 749 |
def getfield(self, vpbc, attr, llops): |
| 750 |
mangled_name, r_value = self.fieldmap[attr] |
| 751 |
cmangledname = inputconst(Void, mangled_name) |
| 752 |
return llops.genop('getfield', [vpbc, cmangledname], |
| 753 |
resulttype=r_value) |
| 754 |
|
| 755 |
def _setup_repr_fields(self): |
| 756 |
fields = [] |
| 757 |
self.fieldmap = {} |
| 758 |
if self.access_set is not None: |
| 759 |
attrlist = self.access_set.attrs.keys() |
| 760 |
attrlist.sort() |
| 761 |
for attr in attrlist: |
| 762 |
s_value = self.access_set.attrs[attr] |
| 763 |
r_value = self.rtyper.getrepr(s_value) |
| 764 |
mangled_name = mangle('pbc', attr) |
| 765 |
fields.append((mangled_name, r_value.lowleveltype)) |
| 766 |
self.fieldmap[attr] = mangled_name, r_value |
| 767 |
return fields |
| 768 |
|
| 769 |
def convert_desc(self, frozendesc): |
| 770 |
if (self.access_set is not None and |
| 771 |
frozendesc not in self.access_set.descs): |
| 772 |
raise TyperError("not found in PBC access set: %r" % (frozendesc,)) |
| 773 |
try: |
| 774 |
return self.pbc_cache[frozendesc] |
| 775 |
except KeyError: |
| 776 |
self.setup() |
| 777 |
result = self.create_instance() |
| 778 |
self.pbc_cache[frozendesc] = result |
| 779 |
for attr, (mangled_name, r_value) in self.fieldmap.items(): |
| 780 |
if r_value.lowleveltype is Void: |
| 781 |
continue |
| 782 |
try: |
| 783 |
thisattrvalue = frozendesc.attrcache[attr] |
| 784 |
except KeyError: |
| 785 |
if frozendesc.warn_missing_attribute(attr): |
| 786 |
warning("Desc %r has no attribute %r" % (frozendesc, attr)) |
| 787 |
continue |
| 788 |
llvalue = r_value.convert_const(thisattrvalue) |
| 789 |
setattr(result, mangled_name, llvalue) |
| 790 |
return result |
| 791 |
|
| 792 |
def rtype_getattr(self, hop): |
| 793 |
if hop.s_result.is_constant(): |
| 794 |
return hop.inputconst(hop.r_result, hop.s_result.const) |
| 795 |
|
| 796 |
attr = hop.args_s[1].const |
| 797 |
vpbc, vattr = hop.inputargs(self, Void) |
| 798 |
v_res = self.getfield(vpbc, attr, hop.llops) |
| 799 |
mangled_name, r_res = self.fieldmap[attr] |
| 800 |
return hop.llops.convertvar(v_res, r_res, hop.r_result) |
| 801 |
|
| 802 |
class __extend__(pairtype(MultipleFrozenPBCRepr, |
| 803 |
MultipleUnrelatedFrozenPBCRepr)): |
| 804 |
def convert_from_to((robj1, robj2), v, llops): |
| 805 |
return llops.genop('cast_ptr_to_adr', [v], resulttype=llmemory.Address) |
| 806 |
|
| 807 |
class __extend__(pairtype(MultipleFrozenPBCRepr, MultipleFrozenPBCRepr)): |
| 808 |
def convert_from_to((r_pbc1, r_pbc2), v, llops): |
| 809 |
if r_pbc1.access_set == r_pbc2.access_set: |
| 810 |
return v |
| 811 |
return NotImplemented |
| 812 |
|
| 813 |
class __extend__(pairtype(SingleFrozenPBCRepr, MultipleFrozenPBCRepr)): |
| 814 |
def convert_from_to((r_pbc1, r_pbc2), v, llops): |
| 815 |
frozendesc1 = r_pbc1.frozendesc |
| 816 |
access = frozendesc1.queryattrfamily() |
| 817 |
if access is r_pbc2.access_set: |
| 818 |
value = r_pbc2.convert_desc(frozendesc1) |
| 819 |
lltype = r_pbc2.lowleveltype |
| 820 |
return Constant(value, lltype) |
| 821 |
return NotImplemented |
| 822 |
|
| 823 |
class __extend__(pairtype(MultipleFrozenPBCReprBase, |
| 824 |
SingleFrozenPBCRepr)): |
| 825 |
def convert_from_to((r_pbc1, r_pbc2), v, llops): |
| 826 |
return inputconst(Void, r_pbc2.frozendesc) |
| 827 |
|
| 828 |
class __extend__(pairtype(FunctionRepr, MultipleFrozenPBCRepr)): |
| 829 |
def convert_from_to((r_fn1, r_frozen2), v, llops): |
| 830 |
if r_fn1.s_pbc.is_constant(): |
| 831 |
value = r_frozen2.convert_const(r_fn1.s_pbc.const) |
| 832 |
lltype = r_frozen2.lowleveltype |
| 833 |
return Constant(value, lltype) |
| 834 |
return NotImplemented |
| 835 |
|
| 836 |
class __extend__(pairtype(MultipleFrozenPBCRepr, FunctionRepr)): |
| 837 |
def convert_from_to((r_frozen1, r_fn2), v, llops): |
| 838 |
if r_fn2.lowleveltype is Void: |
| 839 |
value = r_fn2.s_pbc.const |
| 840 |
return Constant(value, Void) |
| 841 |
return NotImplemented |
| 842 |
|
| 843 |
|
| 844 |
class MethodOfFrozenPBCRepr(Repr): |
| 845 |
"""Representation selected for a PBC of method object(s) of frozen PBCs. |
| 846 |
It assumes that all methods are the same function bound to different PBCs. |
| 847 |
The low-level representation can then be a pointer to that PBC.""" |
| 848 |
|
| 849 |
def __init__(self, rtyper, s_pbc): |
| 850 |
self.rtyper = rtyper |
| 851 |
funcdescs = set([desc.funcdesc for desc in s_pbc.descriptions]) |
| 852 |
assert len(funcdescs) == 1 |
| 853 |
self.funcdesc = funcdescs.pop() |
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
if s_pbc.can_be_none(): |
| 863 |
raise TyperError("unsupported: variable of type " |
| 864 |
"method-of-frozen-PBC or None") |
| 865 |
|
| 866 |
im_selves = [desc.frozendesc for desc in s_pbc.descriptions] |
| 867 |
self.s_im_self = annmodel.SomePBC(im_selves) |
| 868 |
self.r_im_self = rtyper.getrepr(self.s_im_self) |
| 869 |
self.lowleveltype = self.r_im_self.lowleveltype |
| 870 |
|
| 871 |
def get_s_callable(self): |
| 872 |
return annmodel.SomePBC([self.funcdesc]) |
| 873 |
|
| 874 |
def get_r_implfunc(self): |
| 875 |
r_func = self.rtyper.getrepr(self.get_s_callable()) |
| 876 |
return r_func, 1 |
| 877 |
|
| 878 |
def convert_desc(self, mdesc): |
| 879 |
if mdesc.funcdesc is not self.funcdesc: |
| 880 |
raise TyperError("not a method bound on %r: %r" % (self.funcdesc, |
| 881 |
mdesc)) |
| 882 |
return self.r_im_self.convert_desc(mdesc.frozendesc) |
| 883 |
|
| 884 |
def convert_const(self, method): |
| 885 |
mdesc = self.rtyper.annotator.bookkeeper.getdesc(method) |
| 886 |
return self.convert_desc(mdesc) |
| 887 |
|
| 888 |
def rtype_simple_call(self, hop): |
| 889 |
return self.redispatch_call(hop, call_args=False) |
| 890 |
|
| 891 |
def rtype_call_args(self, hop): |
| 892 |
return self.redispatch_call(hop, call_args=True) |
| 893 |
|
| 894 |
def redispatch_call(self, hop, call_args): |
| 895 |
|
| 896 |
s_function = annmodel.SomePBC([self.funcdesc]) |
| 897 |
hop2 = hop.copy() |
| 898 |
hop2.args_s[0] = self.s_im_self |
| 899 |
hop2.args_r[0] = self.r_im_self |
| 900 |
if isinstance(hop2.args_v[0], Constant): |
| 901 |
boundmethod = hop2.args_v[0].value |
| 902 |
hop2.args_v[0] = Constant(boundmethod.im_self) |
| 903 |
if call_args: |
| 904 |
hop2.swap_fst_snd_args() |
| 905 |
_, s_shape = hop2.r_s_popfirstarg() |
| 906 |
adjust_shape(hop2, s_shape) |
| 907 |
|
| 908 |
c = Constant("obscure-don't-use-me") |
| 909 |
hop2.v_s_insertfirstarg(c, s_function) |
| 910 |
|
| 911 |
return hop2.dispatch() |
| 912 |
|
| 913 |
class __extend__(pairtype(MethodOfFrozenPBCRepr, MethodOfFrozenPBCRepr)): |
| 914 |
|
| 915 |
def convert_from_to((r_from, r_to), v, llops): |
| 916 |
return pair(r_from.r_im_self, r_to.r_im_self).convert_from_to(v, llops) |
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
class ClassesPBCRepr(Repr): |
| 921 |
"""Representation selected for a PBC of class(es).""" |
| 922 |
|
| 923 |
def __init__(self, rtyper, s_pbc): |
| 924 |
self.rtyper = rtyper |
| 925 |
self.s_pbc = s_pbc |
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
if s_pbc.is_constant(): |
| 930 |
self.lowleveltype = Void |
| 931 |
else: |
| 932 |
self.lowleveltype = self.getlowleveltype() |
| 933 |
|
| 934 |
def get_access_set(self, attrname): |
| 935 |
"""Return the ClassAttrFamily corresponding to accesses to 'attrname' |
| 936 |
and the ClassRepr of the class which stores this attribute in |
| 937 |
its vtable. |
| 938 |
""" |
| 939 |
classdescs = list(self.s_pbc.descriptions) |
| 940 |
access = classdescs[0].queryattrfamily(attrname) |
| 941 |
for classdesc in classdescs[1:]: |
| 942 |
access1 = classdesc.queryattrfamily(attrname) |
| 943 |
assert access1 is access |
| 944 |
if access is None: |
| 945 |
raise rclass.MissingRTypeAttribute(attrname) |
| 946 |
commonbase = access.commonbase |
| 947 |
class_repr = rclass.getclassrepr(self.rtyper, commonbase) |
| 948 |
return access, class_repr |
| 949 |
|
| 950 |
def convert_desc(self, desc): |
| 951 |
if desc not in self.s_pbc.descriptions: |
| 952 |
raise TyperError("%r not in %r" % (desc, self)) |
| 953 |
if self.lowleveltype is Void: |
| 954 |
return None |
| 955 |
subclassdef = desc.getuniqueclassdef() |
| 956 |
r_subclass = rclass.getclassrepr(self.rtyper, subclassdef) |
| 957 |
return r_subclass.getruntime(self.lowleveltype) |
| 958 |
|
| 959 |
def convert_const(self, cls): |
| 960 |
if cls is None: |
| 961 |
if self.lowleveltype is Void: |
| 962 |
return None |
| 963 |
else: |
| 964 |
T = self.lowleveltype |
| 965 |
return nullptr(T.TO) |
| 966 |
bk = self.rtyper.annotator.bookkeeper |
| 967 |
classdesc = bk.getdesc(cls) |
| 968 |
return self.convert_desc(classdesc) |
| 969 |
|
| 970 |
def rtype_getattr(self, hop): |
| 971 |
if hop.s_result.is_constant(): |
| 972 |
return hop.inputconst(hop.r_result, hop.s_result.const) |
| 973 |
else: |
| 974 |
attr = hop.args_s[1].const |
| 975 |
if attr == '__name__': |
| 976 |
from rpython.rtyper.lltypesystem import rstr |
| 977 |
class_repr = self.rtyper.rootclass_repr |
| 978 |
vcls, vattr = hop.inputargs(class_repr, Void) |
| 979 |
cname = inputconst(Void, 'name') |
| 980 |
return hop.genop('getfield', [vcls, cname], |
| 981 |
resulttype = Ptr(rstr.STR)) |
| 982 |
access_set, class_repr = self.get_access_set(attr) |
| 983 |
vcls, vattr = hop.inputargs(class_repr, Void) |
| 984 |
v_res = class_repr.getpbcfield(vcls, access_set, attr, hop.llops) |
| 985 |
s_res = access_set.s_value |
| 986 |
r_res = self.rtyper.getrepr(s_res) |
| 987 |
return hop.llops.convertvar(v_res, r_res, hop.r_result) |
| 988 |
|
| 989 |
def replace_class_with_inst_arg(self, hop, v_inst, s_inst, call_args): |
| 990 |
hop2 = hop.copy() |
| 991 |
hop2.r_s_popfirstarg() |
| 992 |
if call_args: |
| 993 |
_, s_shape = hop2.r_s_popfirstarg() |
| 994 |
hop2.v_s_insertfirstarg(v_inst, s_inst) |
| 995 |
adjust_shape(hop2, s_shape) |
| 996 |
else: |
| 997 |
hop2.v_s_insertfirstarg(v_inst, s_inst) |
| 998 |
return hop2 |
| 999 |
|
| 1000 |
def rtype_simple_call(self, hop): |
| 1001 |
return self.redispatch_call(hop, call_args=False) |
| 1002 |
|
| 1003 |
def rtype_call_args(self, hop): |
| 1004 |
return self.redispatch_call(hop, call_args=True) |
| 1005 |
|
| 1006 |
def redispatch_call(self, hop, call_args): |
| 1007 |
s_instance = hop.s_result |
| 1008 |
r_instance = hop.r_result |
| 1009 |
|
| 1010 |
if len(self.s_pbc.descriptions) == 1: |
| 1011 |
|
| 1012 |
if self.lowleveltype is not Void: |
| 1013 |
assert 0, "XXX None-or-1-class instantation not implemented" |
| 1014 |
assert isinstance(s_instance, annmodel.SomeInstance) |
| 1015 |
classdef = s_instance.classdef |
| 1016 |
s_init = classdef.classdesc.s_read_attribute('__init__') |
| 1017 |
v_init = Constant("init-func-dummy") |
| 1018 |
|
| 1019 |
if (isinstance(s_init, annmodel.SomeImpossibleValue) and |
| 1020 |
classdef.classdesc.is_exception_class() and |
| 1021 |
classdef.has_no_attrs()): |
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
r_instance = rclass.getinstancerepr(hop.rtyper, classdef) |
| 1026 |
example = r_instance.get_reusable_prebuilt_instance() |
| 1027 |
hop.exception_cannot_occur() |
| 1028 |
return hop.inputconst(r_instance.lowleveltype, example) |
| 1029 |
|
| 1030 |
v_instance = rclass.rtype_new_instance(hop.rtyper, classdef, |
| 1031 |
hop.llops, hop) |
| 1032 |
if isinstance(v_instance, tuple): |
| 1033 |
v_instance, must_call_init = v_instance |
| 1034 |
if not must_call_init: |
| 1035 |
return v_instance |
| 1036 |
else: |
| 1037 |
|
| 1038 |
vtypeptr = hop.inputarg(self, arg=0) |
| 1039 |
try: |
| 1040 |
access_set, r_class = self.get_access_set('__init__') |
| 1041 |
except rclass.MissingRTypeAttribute: |
| 1042 |
s_init = annmodel.s_ImpossibleValue |
| 1043 |
else: |
| 1044 |
s_init = access_set.s_value |
| 1045 |
v_init = r_class.getpbcfield(vtypeptr, access_set, '__init__', |
| 1046 |
hop.llops) |
| 1047 |
v_instance = self._instantiate_runtime_class(hop, vtypeptr, r_instance) |
| 1048 |
|
| 1049 |
if isinstance(s_init, annmodel.SomeImpossibleValue): |
| 1050 |
assert hop.nb_args == 1, ("arguments passed to __init__, " |
| 1051 |
"but no __init__!") |
| 1052 |
hop.exception_cannot_occur() |
| 1053 |
else: |
| 1054 |
hop2 = self.replace_class_with_inst_arg( |
| 1055 |
hop, v_instance, s_instance, call_args) |
| 1056 |
hop2.v_s_insertfirstarg(v_init, s_init) |
| 1057 |
hop2.s_result = annmodel.s_None |
| 1058 |
hop2.r_result = self.rtyper.getrepr(hop2.s_result) |
| 1059 |
|
| 1060 |
hop2.dispatch() |
| 1061 |
return v_instance |
| 1062 |
|
| 1063 |
def _instantiate_runtime_class(self, hop, vtypeptr, r_instance): |
| 1064 |
graphs = [] |
| 1065 |
for desc in self.s_pbc.descriptions: |
| 1066 |
classdef = desc.getclassdef(None) |
| 1067 |
assert hasattr(classdef, 'my_instantiate_graph') |
| 1068 |
graphs.append(classdef.my_instantiate_graph) |
| 1069 |
c_graphs = hop.inputconst(Void, graphs) |
| 1070 |
|
| 1071 |
|
| 1072 |
c_name = hop.inputconst(Void, 'instantiate') |
| 1073 |
v_instantiate = hop.genop('getfield', [vtypeptr, c_name], |
| 1074 |
resulttype=OBJECT_VTABLE.instantiate) |
| 1075 |
|
| 1076 |
v_inst = hop.genop('indirect_call', [v_instantiate, c_graphs], |
| 1077 |
resulttype=OBJECTPTR) |
| 1078 |
return hop.genop('cast_pointer', [v_inst], resulttype=r_instance) |
| 1079 |
|
| 1080 |
def getlowleveltype(self): |
| 1081 |
return CLASSTYPE |
| 1082 |
|
| 1083 |
def get_ll_hash_function(self): |
| 1084 |
return ll_cls_hash |
| 1085 |
|
| 1086 |
get_ll_fasthash_function = get_ll_hash_function |
| 1087 |
|
| 1088 |
def get_ll_eq_function(self): |
| 1089 |
return None |
| 1090 |
|
| 1091 |
def ll_str(self, ptr): |
| 1092 |
cls = cast_pointer(CLASSTYPE, ptr) |
| 1093 |
return cls.name |
| 1094 |
|
| 1095 |
|
| 1096 |
def ll_cls_hash(cls): |
| 1097 |
return cast_ptr_to_int(cls) |
| 1098 |
|
| 1099 |
class __extend__(pairtype(ClassesPBCRepr, rclass.ClassRepr)): |
| 1100 |
def convert_from_to((r_clspbc, r_cls), v, llops): |
| 1101 |
|
| 1102 |
if r_clspbc.lowleveltype == r_cls.lowleveltype: |
| 1103 |
return v |
| 1104 |
if r_clspbc.lowleveltype is Void: |
| 1105 |
return inputconst(r_cls, r_clspbc.s_pbc.const) |
| 1106 |
|
| 1107 |
return r_cls.fromclasstype(v, llops) |
| 1108 |
|
| 1109 |
class __extend__(pairtype(ClassesPBCRepr, ClassesPBCRepr)): |
| 1110 |
def convert_from_to((r_clspbc1, r_clspbc2), v, llops): |
| 1111 |
|
| 1112 |
if r_clspbc1.lowleveltype == r_clspbc2.lowleveltype: |
| 1113 |
return v |
| 1114 |
if r_clspbc1.lowleveltype is Void: |
| 1115 |
return inputconst(r_clspbc2, r_clspbc1.s_pbc.const) |
| 1116 |
if r_clspbc2.lowleveltype is Void: |
| 1117 |
return inputconst(Void, r_clspbc2.s_pbc.const) |
| 1118 |
return NotImplemented |
| 1119 |
|
| 1120 |
def adjust_shape(hop2, s_shape): |
| 1121 |
new_shape = (s_shape.const[0]+1,) + s_shape.const[1:] |
| 1122 |
c_shape = Constant(new_shape) |
| 1123 |
s_shape = hop2.rtyper.annotator.bookkeeper.immutablevalue(new_shape) |
| 1124 |
hop2.v_s_insertfirstarg(c_shape, s_shape) |
| 1125 |
|
| 1126 |
class MethodsPBCRepr(Repr): |
| 1127 |
"""Representation selected for a PBC of MethodDescs. |
| 1128 |
It assumes that all the methods come from the same name and have |
| 1129 |
been read from instances with a common base.""" |
| 1130 |
|
| 1131 |
def __init__(self, rtyper, s_pbc): |
| 1132 |
self.rtyper = rtyper |
| 1133 |
self.s_pbc = s_pbc |
| 1134 |
mdescs = list(s_pbc.descriptions) |
| 1135 |
methodname = mdescs[0].name |
| 1136 |
classdef = mdescs[0].selfclassdef |
| 1137 |
flags = mdescs[0].flags |
| 1138 |
for mdesc in mdescs[1:]: |
| 1139 |
if mdesc.name != methodname: |
| 1140 |
raise TyperError("cannot find a unique name under which the " |
| 1141 |
"methods can be found: %r" % ( |
| 1142 |
mdescs,)) |
| 1143 |
if mdesc.flags != flags: |
| 1144 |
raise TyperError("inconsistent 'flags': %r versus %r" % ( |
| 1145 |
mdesc.flags, flags)) |
| 1146 |
classdef = classdef.commonbase(mdesc.selfclassdef) |
| 1147 |
if classdef is None: |
| 1148 |
raise TyperError("mixing methods coming from instances of " |
| 1149 |
"classes with no common base: %r" % (mdescs,)) |
| 1150 |
|
| 1151 |
self.methodname = methodname |
| 1152 |
self.classdef = classdef.get_owner(methodname) |
| 1153 |
|
| 1154 |
self.s_im_self = annmodel.SomeInstance(self.classdef, flags=flags) |
| 1155 |
self.r_im_self = rclass.getinstancerepr(rtyper, self.classdef) |
| 1156 |
self.lowleveltype = self.r_im_self.lowleveltype |
| 1157 |
|
| 1158 |
def convert_const(self, method): |
| 1159 |
if method is None: |
| 1160 |
return nullptr(self.lowleveltype.TO) |
| 1161 |
if getattr(method, 'im_func', None) is None: |
| 1162 |
raise TyperError("not a bound method: %r" % method) |
| 1163 |
return self.r_im_self.convert_const(method.im_self) |
| 1164 |
|
| 1165 |
def get_r_implfunc(self): |
| 1166 |
r_class = self.r_im_self.rclass |
| 1167 |
mangled_name, r_func = r_class.clsfields[self.methodname] |
| 1168 |
return r_func, 1 |
| 1169 |
|
| 1170 |
def get_s_callable(self): |
| 1171 |
return self.s_pbc |
| 1172 |
|
| 1173 |
def get_method_from_instance(self, r_inst, v_inst, llops): |
| 1174 |
|
| 1175 |
|
| 1176 |
return llops.convertvar(v_inst, r_inst, self.r_im_self) |
| 1177 |
|
| 1178 |
def add_instance_arg_to_hop(self, hop, call_args): |
| 1179 |
hop2 = hop.copy() |
| 1180 |
hop2.args_s[0] = self.s_im_self |
| 1181 |
hop2.args_r[0] = self.r_im_self |
| 1182 |
|
| 1183 |
if call_args: |
| 1184 |
hop2.swap_fst_snd_args() |
| 1185 |
_, s_shape = hop2.r_s_popfirstarg() |
| 1186 |
adjust_shape(hop2, s_shape) |
| 1187 |
return hop2 |
| 1188 |
|
| 1189 |
def rtype_simple_call(self, hop): |
| 1190 |
return self.redispatch_call(hop, call_args=False) |
| 1191 |
|
| 1192 |
def rtype_call_args(self, hop): |
| 1193 |
return self.redispatch_call(hop, call_args=True) |
| 1194 |
|
| 1195 |
def redispatch_call(self, hop, call_args): |
| 1196 |
r_class = self.r_im_self.rclass |
| 1197 |
mangled_name, r_func = r_class.clsfields[self.methodname] |
| 1198 |
assert isinstance(r_func, FunctionReprBase) |
| 1199 |
|
| 1200 |
|
| 1201 |
funcdescs = [desc.funcdesc for desc in hop.args_s[0].descriptions] |
| 1202 |
s_func = annmodel.SomePBC(funcdescs, subset_of=r_func.s_pbc) |
| 1203 |
v_im_self = hop.inputarg(self, arg=0) |
| 1204 |
v_cls = self.r_im_self.getfield(v_im_self, '__class__', hop.llops) |
| 1205 |
v_func = r_class.getclsfield(v_cls, self.methodname, hop.llops) |
| 1206 |
|
| 1207 |
hop2 = self.add_instance_arg_to_hop(hop, call_args) |
| 1208 |
hop2.v_s_insertfirstarg(v_func, s_func) |
| 1209 |
|
| 1210 |
if (type(hop2.args_r[0]) is SmallFunctionSetPBCRepr and |
| 1211 |
type(r_func) is FunctionsPBCRepr): |
| 1212 |
hop2.args_r[0] = FunctionsPBCRepr(self.rtyper, s_func) |
| 1213 |
else: |
| 1214 |
hop2.args_v[0] = hop2.llops.convertvar( |
| 1215 |
hop2.args_v[0], r_func, hop2.args_r[0]) |
| 1216 |
|
| 1217 |
|
| 1218 |
return hop2.dispatch() |
| 1219 |
|
| 1220 |
class __extend__(pairtype(MethodsPBCRepr, MethodsPBCRepr)): |
| 1221 |
def convert_from_to((r_mpbc1, r_mpbc2), v, llops): |
| 1222 |
if r_mpbc1.lowleveltype == r_mpbc2.lowleveltype: |
| 1223 |
return v |
| 1224 |
return NotImplemented |