| 1 |
""" |
| 2 |
RTyper: converts high-level operations into low-level operations in flow graphs. |
| 3 |
|
| 4 |
The main class, with code to walk blocks and dispatch individual operations to |
| 5 |
the care of the rtype_*() methods implemented in the other r* modules. For |
| 6 |
each high-level operation 'hop', the rtype_*() methods produce low-level |
| 7 |
operations that are collected in the 'llops' list defined here. When |
| 8 |
necessary, conversions are inserted. |
| 9 |
|
| 10 |
This logic borrows a bit from rpython.annotator.annrpython, without the |
| 11 |
fixpoint computation part. |
| 12 |
""" |
| 13 |
|
| 14 |
import os |
| 15 |
|
| 16 |
import py, math |
| 17 |
|
| 18 |
from rpython.annotator import model as annmodel, unaryop, binaryop |
| 19 |
from rpython.rtyper.llannotation import lltype_to_annotation |
| 20 |
from rpython.flowspace.model import Variable, Constant, SpaceOperation |
| 21 |
from rpython.rtyper.annlowlevel import ( |
| 22 |
annotate_lowlevel_helper, LowLevelAnnotatorPolicy) |
| 23 |
from rpython.rtyper.error import TyperError |
| 24 |
from rpython.rtyper.exceptiondata import ExceptionData |
| 25 |
from rpython.rtyper.lltypesystem.lltype import (Signed, Void, LowLevelType, |
| 26 |
ContainerType, typeOf, Primitive, getfunctionptr) |
| 27 |
from rpython.rtyper.rmodel import Repr, inputconst |
| 28 |
from rpython.rtyper import rclass |
| 29 |
from rpython.rtyper.rclass import RootClassRepr |
| 30 |
from rpython.tool.pairtype import pair |
| 31 |
from rpython.translator.unsimplify import insert_empty_block |
| 32 |
from rpython.translator.sandbox.rsandbox import make_sandbox_trampoline |
| 33 |
|
| 34 |
|
| 35 |
class RTyperBackend(object): |
| 36 |
pass |
| 37 |
|
| 38 |
class GenCBackend(RTyperBackend): |
| 39 |
pass |
| 40 |
genc_backend = GenCBackend() |
| 41 |
|
| 42 |
class LLInterpBackend(RTyperBackend): |
| 43 |
pass |
| 44 |
llinterp_backend = LLInterpBackend() |
| 45 |
|
| 46 |
|
| 47 |
class RPythonTyper(object): |
| 48 |
from rpython.rtyper.rmodel import log |
| 49 |
|
| 50 |
def __init__(self, annotator, backend=genc_backend): |
| 51 |
self.annotator = annotator |
| 52 |
self.backend = backend |
| 53 |
self.lowlevel_ann_policy = LowLevelAnnotatorPolicy(self) |
| 54 |
self.reprs = {} |
| 55 |
self._reprs_must_call_setup = [] |
| 56 |
self._seen_reprs_must_call_setup = {} |
| 57 |
self.rootclass_repr = RootClassRepr(self) |
| 58 |
self.rootclass_repr.setup() |
| 59 |
self.instance_reprs = {} |
| 60 |
self.type_for_typeptr = {} |
| 61 |
self.pbc_reprs = {} |
| 62 |
self.classes_with_wrapper = {} |
| 63 |
self.wrapper_context = None |
| 64 |
self.classdef_to_pytypeobject = {} |
| 65 |
self.concrete_calltables = {} |
| 66 |
self.cache_dummy_values = {} |
| 67 |
self.lltype2vtable = {} |
| 68 |
|
| 69 |
self.primitive_to_repr = {} |
| 70 |
self.isinstance_helpers = {} |
| 71 |
self.exceptiondata = ExceptionData(self) |
| 72 |
self.custom_trace_funcs = [] |
| 73 |
self.gcrefreprcache = {} |
| 74 |
|
| 75 |
try: |
| 76 |
self.seed = int(os.getenv('RTYPERSEED')) |
| 77 |
s = 'Using %d as seed for block shuffling' % self.seed |
| 78 |
self.log.info(s) |
| 79 |
except: |
| 80 |
self.seed = 0 |
| 81 |
|
| 82 |
def getconfig(self): |
| 83 |
return self.annotator.translator.config |
| 84 |
|
| 85 |
def getprimitiverepr(self, lltype): |
| 86 |
try: |
| 87 |
return self.primitive_to_repr[lltype] |
| 88 |
except KeyError: |
| 89 |
pass |
| 90 |
if isinstance(lltype, Primitive): |
| 91 |
repr = self.primitive_to_repr[lltype] = self.getrepr(lltype_to_annotation(lltype)) |
| 92 |
return repr |
| 93 |
raise TyperError('There is no primitive repr for %r' % (lltype,)) |
| 94 |
|
| 95 |
def add_wrapper(self, clsdef): |
| 96 |
|
| 97 |
cls = clsdef.classdesc.pyobj |
| 98 |
init = getattr(cls.__init__, 'im_func', None) |
| 99 |
self.classes_with_wrapper[cls] = init |
| 100 |
|
| 101 |
def set_wrapper_context(self, obj): |
| 102 |
|
| 103 |
self.wrapper_context = obj |
| 104 |
|
| 105 |
def add_pendingsetup(self, repr): |
| 106 |
assert isinstance(repr, Repr) |
| 107 |
if repr in self._seen_reprs_must_call_setup: |
| 108 |
|
| 109 |
return |
| 110 |
self._reprs_must_call_setup.append(repr) |
| 111 |
self._seen_reprs_must_call_setup[repr] = True |
| 112 |
|
| 113 |
def lltype_to_classdef_mapping(self): |
| 114 |
result = {} |
| 115 |
for (classdef, _), repr in self.instance_reprs.iteritems(): |
| 116 |
result[repr.lowleveltype] = classdef |
| 117 |
return result |
| 118 |
|
| 119 |
def get_type_for_typeptr(self, typeptr): |
| 120 |
search = typeptr._obj |
| 121 |
try: |
| 122 |
return self.type_for_typeptr[search] |
| 123 |
except KeyError: |
| 124 |
|
| 125 |
|
| 126 |
found = None |
| 127 |
type_for_typeptr = {} |
| 128 |
for key, value in self.type_for_typeptr.items(): |
| 129 |
type_for_typeptr[key] = value |
| 130 |
if key == search: |
| 131 |
found = value |
| 132 |
self.type_for_typeptr = type_for_typeptr |
| 133 |
if found is None: |
| 134 |
raise KeyError(search) |
| 135 |
return found |
| 136 |
|
| 137 |
def set_type_for_typeptr(self, typeptr, TYPE): |
| 138 |
self.type_for_typeptr[typeptr._obj] = TYPE |
| 139 |
self.lltype2vtable[TYPE] = typeptr |
| 140 |
|
| 141 |
def get_real_typeptr_for_typeptr(self, typeptr): |
| 142 |
|
| 143 |
search = typeptr._obj |
| 144 |
for key, value in self.type_for_typeptr.items(): |
| 145 |
if key == search: |
| 146 |
return key._as_ptr() |
| 147 |
raise KeyError(search) |
| 148 |
|
| 149 |
def getrepr(self, s_obj): |
| 150 |
|
| 151 |
key = s_obj.rtyper_makekey() |
| 152 |
assert key[0] is s_obj.__class__ |
| 153 |
try: |
| 154 |
result = self.reprs[key] |
| 155 |
except KeyError: |
| 156 |
self.reprs[key] = None |
| 157 |
result = s_obj.rtyper_makerepr(self) |
| 158 |
assert not isinstance(result.lowleveltype, ContainerType), ( |
| 159 |
"missing a Ptr in the type specification " |
| 160 |
"of %s:\n%r" % (s_obj, result.lowleveltype)) |
| 161 |
self.reprs[key] = result |
| 162 |
self.add_pendingsetup(result) |
| 163 |
assert result is not None |
| 164 |
return result |
| 165 |
|
| 166 |
def annotation(self, var): |
| 167 |
s_obj = self.annotator.annotation(var) |
| 168 |
return s_obj |
| 169 |
|
| 170 |
def binding(self, var): |
| 171 |
s_obj = self.annotator.binding(var) |
| 172 |
return s_obj |
| 173 |
|
| 174 |
def bindingrepr(self, var): |
| 175 |
return self.getrepr(self.binding(var)) |
| 176 |
|
| 177 |
def specialize(self, dont_simplify_again=False): |
| 178 |
"""Main entry point: specialize all annotated blocks of the program.""" |
| 179 |
|
| 180 |
if not dont_simplify_again: |
| 181 |
self.annotator.simplify() |
| 182 |
self.exceptiondata.finish(self) |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
self.already_seen = {} |
| 187 |
self.specialize_more_blocks() |
| 188 |
self.exceptiondata.make_helpers(self) |
| 189 |
self.specialize_more_blocks() |
| 190 |
|
| 191 |
def getannmixlevel(self): |
| 192 |
if self.annmixlevel is not None: |
| 193 |
return self.annmixlevel |
| 194 |
from rpython.rtyper.annlowlevel import MixLevelHelperAnnotator |
| 195 |
self.annmixlevel = MixLevelHelperAnnotator(self) |
| 196 |
return self.annmixlevel |
| 197 |
|
| 198 |
def specialize_more_blocks(self): |
| 199 |
if self.already_seen: |
| 200 |
newtext = ' more' |
| 201 |
else: |
| 202 |
newtext = '' |
| 203 |
blockcount = 0 |
| 204 |
self.annmixlevel = None |
| 205 |
while True: |
| 206 |
|
| 207 |
self.call_all_setups() |
| 208 |
|
| 209 |
|
| 210 |
pending = [block for block in self.annotator.annotated |
| 211 |
if block not in self.already_seen] |
| 212 |
if not pending: |
| 213 |
break |
| 214 |
|
| 215 |
if self.seed: |
| 216 |
import random |
| 217 |
r = random.Random(self.seed) |
| 218 |
r.shuffle(pending) |
| 219 |
|
| 220 |
previous_percentage = 0 |
| 221 |
|
| 222 |
for block in pending: |
| 223 |
blockcount += 1 |
| 224 |
self.specialize_block(block) |
| 225 |
self.already_seen[block] = True |
| 226 |
|
| 227 |
n = len(self.already_seen) |
| 228 |
if n % 100 == 0: |
| 229 |
total = len(self.annotator.annotated) |
| 230 |
percentage = 100 * n // total |
| 231 |
if percentage >= previous_percentage + 5: |
| 232 |
previous_percentage = percentage |
| 233 |
self.log.event('specializing: %d / %d blocks (%d%%)' % |
| 234 |
(n, total, percentage)) |
| 235 |
|
| 236 |
self.log.event('-=- specialized %d%s blocks -=-' % ( |
| 237 |
blockcount, newtext)) |
| 238 |
annmixlevel = self.annmixlevel |
| 239 |
del self.annmixlevel |
| 240 |
if annmixlevel is not None: |
| 241 |
annmixlevel.finish() |
| 242 |
|
| 243 |
def call_all_setups(self): |
| 244 |
|
| 245 |
must_setup_more = [] |
| 246 |
delayed = [] |
| 247 |
while self._reprs_must_call_setup: |
| 248 |
r = self._reprs_must_call_setup.pop() |
| 249 |
if r.is_setup_delayed(): |
| 250 |
delayed.append(r) |
| 251 |
else: |
| 252 |
r.setup() |
| 253 |
must_setup_more.append(r) |
| 254 |
for r in must_setup_more: |
| 255 |
r.setup_final() |
| 256 |
self._reprs_must_call_setup.extend(delayed) |
| 257 |
|
| 258 |
def setconcretetype(self, v): |
| 259 |
assert isinstance(v, Variable) |
| 260 |
v.concretetype = self.bindingrepr(v).lowleveltype |
| 261 |
|
| 262 |
def setup_block_entry(self, block): |
| 263 |
if block.operations == () and len(block.inputargs) == 2: |
| 264 |
|
| 265 |
|
| 266 |
v1, v2 = block.inputargs |
| 267 |
v1.concretetype = self.exceptiondata.lltype_of_exception_type |
| 268 |
v2.concretetype = self.exceptiondata.lltype_of_exception_value |
| 269 |
return [self.exceptiondata.r_exception_type, |
| 270 |
self.exceptiondata.r_exception_value] |
| 271 |
else: |
| 272 |
|
| 273 |
result = [] |
| 274 |
for a in block.inputargs: |
| 275 |
r = self.bindingrepr(a) |
| 276 |
a.concretetype = r.lowleveltype |
| 277 |
result.append(r) |
| 278 |
return result |
| 279 |
|
| 280 |
def make_new_lloplist(self, block): |
| 281 |
return LowLevelOpList(self, block) |
| 282 |
|
| 283 |
def specialize_block(self, block): |
| 284 |
graph = self.annotator.annotated[block] |
| 285 |
if graph not in self.annotator.fixed_graphs: |
| 286 |
self.annotator.fixed_graphs[graph] = True |
| 287 |
|
| 288 |
|
| 289 |
self.setconcretetype(graph.getreturnvar()) |
| 290 |
|
| 291 |
|
| 292 |
try: |
| 293 |
self.setup_block_entry(block) |
| 294 |
except TyperError as e: |
| 295 |
self.gottypererror(e, block, "block-entry") |
| 296 |
raise |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
if block.operations == (): |
| 301 |
return |
| 302 |
newops = self.make_new_lloplist(block) |
| 303 |
varmapping = {} |
| 304 |
for v in block.getvariables(): |
| 305 |
varmapping[v] = v |
| 306 |
|
| 307 |
for hop in self.highlevelops(block, newops): |
| 308 |
try: |
| 309 |
hop.setup() |
| 310 |
self.translate_hl_to_ll(hop, varmapping) |
| 311 |
except TyperError as e: |
| 312 |
self.gottypererror(e, block, hop.spaceop) |
| 313 |
raise |
| 314 |
|
| 315 |
block.operations[:] = newops |
| 316 |
block.renamevariables(varmapping) |
| 317 |
|
| 318 |
extrablock = None |
| 319 |
pos = newops.llop_raising_exceptions |
| 320 |
if (pos is not None and pos != len(newops) - 1): |
| 321 |
|
| 322 |
|
| 323 |
assert block.canraise |
| 324 |
noexclink = block.exits[0] |
| 325 |
assert noexclink.exitcase is None |
| 326 |
if pos == "removed": |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
block.exitswitch = None |
| 331 |
block.exits = block.exits[:1] |
| 332 |
else: |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
assert 0 <= pos < len(newops) - 1 |
| 339 |
extraops = block.operations[pos+1:] |
| 340 |
del block.operations[pos+1:] |
| 341 |
extrablock = insert_empty_block(noexclink, newops=extraops) |
| 342 |
|
| 343 |
if extrablock is None: |
| 344 |
self.insert_link_conversions(block) |
| 345 |
else: |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
self.insert_link_conversions(block, skip=1) |
| 350 |
|
| 351 |
self.insert_link_conversions(extrablock) |
| 352 |
|
| 353 |
def _convert_link(self, block, link): |
| 354 |
if link.exitcase is not None and link.exitcase != 'default': |
| 355 |
if isinstance(block.exitswitch, Variable): |
| 356 |
r_case = self.bindingrepr(block.exitswitch) |
| 357 |
else: |
| 358 |
assert block.canraise |
| 359 |
r_case = rclass.get_type_repr(self) |
| 360 |
link.llexitcase = r_case.convert_const(link.exitcase) |
| 361 |
else: |
| 362 |
link.llexitcase = None |
| 363 |
|
| 364 |
a = link.last_exception |
| 365 |
if isinstance(a, Variable): |
| 366 |
a.concretetype = self.exceptiondata.lltype_of_exception_type |
| 367 |
elif isinstance(a, Constant): |
| 368 |
link.last_exception = inputconst( |
| 369 |
self.exceptiondata.r_exception_type, a.value) |
| 370 |
|
| 371 |
a = link.last_exc_value |
| 372 |
if isinstance(a, Variable): |
| 373 |
a.concretetype = self.exceptiondata.lltype_of_exception_value |
| 374 |
elif isinstance(a, Constant): |
| 375 |
link.last_exc_value = inputconst( |
| 376 |
self.exceptiondata.r_exception_value, a.value) |
| 377 |
|
| 378 |
def insert_link_conversions(self, block, skip=0): |
| 379 |
|
| 380 |
can_insert_here = block.exitswitch is None and len(block.exits) == 1 |
| 381 |
for link in block.exits[skip:]: |
| 382 |
self._convert_link(block, link) |
| 383 |
inputargs_reprs = self.setup_block_entry(link.target) |
| 384 |
newops = self.make_new_lloplist(block) |
| 385 |
newlinkargs = {} |
| 386 |
for i in range(len(link.args)): |
| 387 |
a1 = link.args[i] |
| 388 |
r_a2 = inputargs_reprs[i] |
| 389 |
if isinstance(a1, Constant): |
| 390 |
link.args[i] = inputconst(r_a2, a1.value) |
| 391 |
continue |
| 392 |
if a1 is link.last_exception: |
| 393 |
r_a1 = self.exceptiondata.r_exception_type |
| 394 |
elif a1 is link.last_exc_value: |
| 395 |
r_a1 = self.exceptiondata.r_exception_value |
| 396 |
else: |
| 397 |
r_a1 = self.bindingrepr(a1) |
| 398 |
if r_a1 == r_a2: |
| 399 |
continue |
| 400 |
try: |
| 401 |
new_a1 = newops.convertvar(a1, r_a1, r_a2) |
| 402 |
except TyperError as e: |
| 403 |
self.gottypererror(e, block, link) |
| 404 |
raise |
| 405 |
if new_a1 != a1: |
| 406 |
newlinkargs[i] = new_a1 |
| 407 |
|
| 408 |
if newops: |
| 409 |
if can_insert_here: |
| 410 |
block.operations.extend(newops) |
| 411 |
else: |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
newblock = insert_empty_block(link, |
| 416 |
|
| 417 |
newops=newops) |
| 418 |
link = newblock.exits[0] |
| 419 |
for i, new_a1 in newlinkargs.items(): |
| 420 |
link.args[i] = new_a1 |
| 421 |
|
| 422 |
def highlevelops(self, block, llops): |
| 423 |
|
| 424 |
if block.operations: |
| 425 |
for op in block.operations[:-1]: |
| 426 |
yield HighLevelOp(self, op, [], llops) |
| 427 |
|
| 428 |
if block.canraise: |
| 429 |
exclinks = block.exits[1:] |
| 430 |
else: |
| 431 |
exclinks = [] |
| 432 |
yield HighLevelOp(self, block.operations[-1], exclinks, llops) |
| 433 |
|
| 434 |
def translate_hl_to_ll(self, hop, varmapping): |
| 435 |
|
| 436 |
resultvar = hop.dispatch() |
| 437 |
if hop.exceptionlinks and hop.llops.llop_raising_exceptions is None: |
| 438 |
raise TyperError("the graph catches %s, but the rtyper did not " |
| 439 |
"take exceptions into account " |
| 440 |
"(exception_is_here() not called)" % ( |
| 441 |
[link.exitcase.__name__ for link in hop.exceptionlinks],)) |
| 442 |
if resultvar is None: |
| 443 |
|
| 444 |
self.translate_no_return_value(hop) |
| 445 |
else: |
| 446 |
assert isinstance(resultvar, (Variable, Constant)) |
| 447 |
op = hop.spaceop |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
if hop.s_result.is_constant(): |
| 452 |
if isinstance(resultvar, Constant) and \ |
| 453 |
isinstance(hop.r_result.lowleveltype, Primitive) and \ |
| 454 |
hop.r_result.lowleveltype is not Void: |
| 455 |
|
| 456 |
assert resultvar.value == hop.s_result.const or ( |
| 457 |
math.isnan(resultvar.value) and |
| 458 |
math.isnan(hop.s_result.const)) |
| 459 |
|
| 460 |
resulttype = resultvar.concretetype |
| 461 |
op.result.concretetype = hop.r_result.lowleveltype |
| 462 |
if op.result.concretetype != resulttype: |
| 463 |
raise TyperError("inconsistent type for the result of '%s':\n" |
| 464 |
"annotator says %s,\n" |
| 465 |
"whose repr is %r\n" |
| 466 |
"but rtype_%s returned %r" % ( |
| 467 |
op.opname, hop.s_result, |
| 468 |
hop.r_result, op.opname, resulttype)) |
| 469 |
|
| 470 |
if (isinstance(resultvar, Variable) and |
| 471 |
resultvar.annotation is None and |
| 472 |
resultvar not in varmapping): |
| 473 |
|
| 474 |
varmapping[resultvar] = op.result |
| 475 |
elif resultvar is op.result: |
| 476 |
|
| 477 |
assert varmapping[resultvar] is resultvar |
| 478 |
else: |
| 479 |
|
| 480 |
hop.llops.append(SpaceOperation('same_as', [resultvar], |
| 481 |
op.result)) |
| 482 |
|
| 483 |
def translate_no_return_value(self, hop): |
| 484 |
op = hop.spaceop |
| 485 |
if hop.s_result != annmodel.s_ImpossibleValue: |
| 486 |
raise TyperError("the annotator doesn't agree that '%s' " |
| 487 |
"has no return value" % op.opname) |
| 488 |
op.result.concretetype = Void |
| 489 |
|
| 490 |
def gottypererror(self, exc, block, position): |
| 491 |
"""Record information about the location of a TyperError""" |
| 492 |
graph = self.annotator.annotated.get(block) |
| 493 |
exc.where = (graph, block, position) |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
def _registeroperations(cls, unary_ops, binary_ops): |
| 498 |
d = {} |
| 499 |
|
| 500 |
for opname in unary_ops: |
| 501 |
fnname = 'translate_op_' + opname |
| 502 |
exec(py.code.compile(""" |
| 503 |
def translate_op_%s(self, hop): |
| 504 |
r_arg1 = hop.args_r[0] |
| 505 |
return r_arg1.rtype_%s(hop) |
| 506 |
""" % (opname, opname)), globals(), d) |
| 507 |
setattr(cls, fnname, d[fnname]) |
| 508 |
|
| 509 |
for opname in binary_ops: |
| 510 |
fnname = 'translate_op_' + opname |
| 511 |
exec(py.code.compile(""" |
| 512 |
def translate_op_%s(self, hop): |
| 513 |
r_arg1 = hop.args_r[0] |
| 514 |
r_arg2 = hop.args_r[1] |
| 515 |
return pair(r_arg1, r_arg2).rtype_%s(hop) |
| 516 |
""" % (opname, opname)), globals(), d) |
| 517 |
setattr(cls, fnname, d[fnname]) |
| 518 |
_registeroperations = classmethod(_registeroperations) |
| 519 |
|
| 520 |
|
| 521 |
def translate_op_contains(self, hop): |
| 522 |
r_arg1 = hop.args_r[0] |
| 523 |
r_arg2 = hop.args_r[1] |
| 524 |
return pair(r_arg1, r_arg2).rtype_contains(hop) |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
def translate_op_newlist(self, hop): |
| 529 |
return rlist.rtype_newlist(hop) |
| 530 |
|
| 531 |
def translate_op_newdict(self, hop): |
| 532 |
return rdict.rtype_newdict(hop) |
| 533 |
|
| 534 |
def translate_op_alloc_and_set(self, hop): |
| 535 |
return rlist.rtype_alloc_and_set(hop) |
| 536 |
|
| 537 |
def translate_op_extend_with_str_slice(self, hop): |
| 538 |
r_arg1 = hop.args_r[0] |
| 539 |
r_arg2 = hop.args_r[3] |
| 540 |
return pair(r_arg1, r_arg2).rtype_extend_with_str_slice(hop) |
| 541 |
|
| 542 |
def translate_op_extend_with_char_count(self, hop): |
| 543 |
r_arg1 = hop.args_r[0] |
| 544 |
r_arg2 = hop.args_r[1] |
| 545 |
return pair(r_arg1, r_arg2).rtype_extend_with_char_count(hop) |
| 546 |
|
| 547 |
def translate_op_newtuple(self, hop): |
| 548 |
from rpython.rtyper.rtuple import rtype_newtuple |
| 549 |
return rtype_newtuple(hop) |
| 550 |
|
| 551 |
def translate_op_instantiate1(self, hop): |
| 552 |
if not isinstance(hop.s_result, annmodel.SomeInstance): |
| 553 |
raise TyperError("instantiate1 got s_result=%r" % (hop.s_result,)) |
| 554 |
classdef = hop.s_result.classdef |
| 555 |
return rclass.rtype_new_instance(self, classdef, hop.llops) |
| 556 |
|
| 557 |
def default_translate_operation(self, hop): |
| 558 |
raise TyperError("unimplemented operation: '%s'" % hop.spaceop.opname) |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
def needs_wrapper(self, cls): |
| 563 |
return cls in self.classes_with_wrapper |
| 564 |
|
| 565 |
def get_wrapping_hint(self, clsdef): |
| 566 |
cls = clsdef.classdesc.pyobj |
| 567 |
return self.classes_with_wrapper[cls], self.wrapper_context |
| 568 |
|
| 569 |
def getcallable(self, graph): |
| 570 |
def getconcretetype(v): |
| 571 |
return self.bindingrepr(v).lowleveltype |
| 572 |
if self.annotator.translator.config.translation.sandbox: |
| 573 |
try: |
| 574 |
name = graph.func._sandbox_external_name |
| 575 |
except AttributeError: |
| 576 |
pass |
| 577 |
else: |
| 578 |
args_s = [v.annotation for v in graph.getargs()] |
| 579 |
s_result = graph.getreturnvar().annotation |
| 580 |
sandboxed = make_sandbox_trampoline(name, args_s, s_result) |
| 581 |
return self.getannmixlevel().delayedfunction( |
| 582 |
sandboxed, args_s, s_result) |
| 583 |
|
| 584 |
return getfunctionptr(graph, getconcretetype) |
| 585 |
|
| 586 |
def annotate_helper(self, ll_function, argtypes): |
| 587 |
"""Annotate the given low-level helper function and return its graph |
| 588 |
""" |
| 589 |
args_s = [] |
| 590 |
for s in argtypes: |
| 591 |
|
| 592 |
if not isinstance(s, annmodel.SomeObject): |
| 593 |
s = lltype_to_annotation(s) |
| 594 |
args_s.append(s) |
| 595 |
|
| 596 |
if hasattr(ll_function, 'im_func'): |
| 597 |
bk = self.annotator.bookkeeper |
| 598 |
args_s.insert(0, bk.immutablevalue(ll_function.im_self)) |
| 599 |
ll_function = ll_function.im_func |
| 600 |
helper_graph = annotate_lowlevel_helper(self.annotator, |
| 601 |
ll_function, args_s, |
| 602 |
policy=self.lowlevel_ann_policy) |
| 603 |
return helper_graph |
| 604 |
|
| 605 |
def annotate_helper_fn(self, ll_function, argtypes): |
| 606 |
"""Annotate the given low-level helper function |
| 607 |
and return it as a function pointer |
| 608 |
""" |
| 609 |
graph = self.annotate_helper(ll_function, argtypes) |
| 610 |
return self.getcallable(graph) |
| 611 |
|
| 612 |
|
| 613 |
RPythonTyper._registeroperations(unaryop.UNARY_OPERATIONS, binaryop.BINARY_OPERATIONS) |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
class HighLevelOp(object): |
| 619 |
def __init__(self, rtyper, spaceop, exceptionlinks, llops): |
| 620 |
self.rtyper = rtyper |
| 621 |
self.spaceop = spaceop |
| 622 |
self.exceptionlinks = exceptionlinks |
| 623 |
self.llops = llops |
| 624 |
|
| 625 |
def setup(self): |
| 626 |
rtyper = self.rtyper |
| 627 |
spaceop = self.spaceop |
| 628 |
self.args_v = list(spaceop.args) |
| 629 |
self.args_s = [rtyper.binding(a) for a in spaceop.args] |
| 630 |
self.s_result = rtyper.binding(spaceop.result) |
| 631 |
self.args_r = [rtyper.getrepr(s_a) for s_a in self.args_s] |
| 632 |
self.r_result = rtyper.getrepr(self.s_result) |
| 633 |
rtyper.call_all_setups() |
| 634 |
|
| 635 |
@property |
| 636 |
def nb_args(self): |
| 637 |
return len(self.args_v) |
| 638 |
|
| 639 |
def copy(self): |
| 640 |
result = HighLevelOp(self.rtyper, self.spaceop, |
| 641 |
self.exceptionlinks, self.llops) |
| 642 |
for key, value in self.__dict__.items(): |
| 643 |
if type(value) is list: |
| 644 |
value = value[:] |
| 645 |
setattr(result, key, value) |
| 646 |
return result |
| 647 |
|
| 648 |
def dispatch(self): |
| 649 |
rtyper = self.rtyper |
| 650 |
opname = self.spaceop.opname |
| 651 |
translate_meth = getattr(rtyper, 'translate_op_' + opname, |
| 652 |
rtyper.default_translate_operation) |
| 653 |
return translate_meth(self) |
| 654 |
|
| 655 |
def inputarg(self, converted_to, arg): |
| 656 |
"""Returns the arg'th input argument of the current operation, |
| 657 |
as a Variable or Constant converted to the requested type. |
| 658 |
'converted_to' should be a Repr instance or a Primitive low-level |
| 659 |
type. |
| 660 |
""" |
| 661 |
if not isinstance(converted_to, Repr): |
| 662 |
converted_to = self.rtyper.getprimitiverepr(converted_to) |
| 663 |
v = self.args_v[arg] |
| 664 |
if isinstance(v, Constant): |
| 665 |
return inputconst(converted_to, v.value) |
| 666 |
assert hasattr(v, 'concretetype') |
| 667 |
|
| 668 |
s_binding = self.args_s[arg] |
| 669 |
if s_binding.is_constant(): |
| 670 |
return inputconst(converted_to, s_binding.const) |
| 671 |
|
| 672 |
r_binding = self.args_r[arg] |
| 673 |
return self.llops.convertvar(v, r_binding, converted_to) |
| 674 |
|
| 675 |
inputconst = staticmethod(inputconst) |
| 676 |
|
| 677 |
def inputargs(self, *converted_to): |
| 678 |
if len(converted_to) != self.nb_args: |
| 679 |
raise TyperError("operation argument count mismatch:\n" |
| 680 |
"'%s' has %d arguments, rtyper wants %d" % ( |
| 681 |
self.spaceop.opname, self.nb_args, len(converted_to))) |
| 682 |
vars = [] |
| 683 |
for i in range(len(converted_to)): |
| 684 |
vars.append(self.inputarg(converted_to[i], i)) |
| 685 |
return vars |
| 686 |
|
| 687 |
def genop(self, opname, args_v, resulttype=None): |
| 688 |
return self.llops.genop(opname, args_v, resulttype) |
| 689 |
|
| 690 |
def gendirectcall(self, ll_function, *args_v): |
| 691 |
return self.llops.gendirectcall(ll_function, *args_v) |
| 692 |
|
| 693 |
def r_s_pop(self, index=-1): |
| 694 |
"Return and discard the argument with index position." |
| 695 |
self.args_v.pop(index) |
| 696 |
return self.args_r.pop(index), self.args_s.pop(index) |
| 697 |
|
| 698 |
def r_s_popfirstarg(self): |
| 699 |
"Return and discard the first argument." |
| 700 |
return self.r_s_pop(0) |
| 701 |
|
| 702 |
def v_s_insertfirstarg(self, v_newfirstarg, s_newfirstarg): |
| 703 |
r_newfirstarg = self.rtyper.getrepr(s_newfirstarg) |
| 704 |
self.args_v.insert(0, v_newfirstarg) |
| 705 |
self.args_r.insert(0, r_newfirstarg) |
| 706 |
self.args_s.insert(0, s_newfirstarg) |
| 707 |
|
| 708 |
def swap_fst_snd_args(self): |
| 709 |
self.args_v[0], self.args_v[1] = self.args_v[1], self.args_v[0] |
| 710 |
self.args_s[0], self.args_s[1] = self.args_s[1], self.args_s[0] |
| 711 |
self.args_r[0], self.args_r[1] = self.args_r[1], self.args_r[0] |
| 712 |
|
| 713 |
def has_implicit_exception(self, exc_cls): |
| 714 |
if self.llops.llop_raising_exceptions is not None: |
| 715 |
raise TyperError("already generated the llop that raises the " |
| 716 |
"exception") |
| 717 |
if not self.exceptionlinks: |
| 718 |
return False |
| 719 |
|
| 720 |
if self.llops.implicit_exceptions_checked is None: |
| 721 |
self.llops.implicit_exceptions_checked = [] |
| 722 |
result = False |
| 723 |
for link in self.exceptionlinks: |
| 724 |
if issubclass(exc_cls, link.exitcase): |
| 725 |
self.llops.implicit_exceptions_checked.append(link.exitcase) |
| 726 |
result = True |
| 727 |
|
| 728 |
|
| 729 |
return result |
| 730 |
|
| 731 |
def exception_is_here(self): |
| 732 |
self.llops._called_exception_is_here_or_cannot_occur = True |
| 733 |
if self.llops.llop_raising_exceptions is not None: |
| 734 |
raise TyperError("cannot catch an exception at more than one llop") |
| 735 |
if not self.exceptionlinks: |
| 736 |
return |
| 737 |
if self.llops.implicit_exceptions_checked is not None: |
| 738 |
|
| 739 |
|
| 740 |
for link in self.exceptionlinks: |
| 741 |
if link.exitcase not in self.llops.implicit_exceptions_checked: |
| 742 |
raise TyperError("the graph catches %s, but the rtyper " |
| 743 |
"did not explicitely handle it" % ( |
| 744 |
link.exitcase.__name__,)) |
| 745 |
self.llops.llop_raising_exceptions = len(self.llops) |
| 746 |
|
| 747 |
def exception_cannot_occur(self): |
| 748 |
self.llops._called_exception_is_here_or_cannot_occur = True |
| 749 |
if self.llops.llop_raising_exceptions is not None: |
| 750 |
raise TyperError("cannot catch an exception at more than one llop") |
| 751 |
if not self.exceptionlinks: |
| 752 |
return |
| 753 |
self.llops.llop_raising_exceptions = "removed" |
| 754 |
|
| 755 |
def decompose_slice_args(self): |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
s_start = self.args_s[1] |
| 761 |
s_stop = self.args_s[2] |
| 762 |
if (s_start.is_constant() and s_start.const in (None, 0) and |
| 763 |
s_stop.is_constant() and s_stop.const == -1): |
| 764 |
return "minusone", [] |
| 765 |
if isinstance(s_start, annmodel.SomeInteger): |
| 766 |
if not s_start.nonneg: |
| 767 |
raise TyperError("slice start must be proved non-negative") |
| 768 |
if isinstance(s_stop, annmodel.SomeInteger): |
| 769 |
if not s_stop.nonneg: |
| 770 |
raise TyperError("slice stop must be proved non-negative") |
| 771 |
if s_start.is_constant() and s_start.const is None: |
| 772 |
v_start = inputconst(Signed, 0) |
| 773 |
else: |
| 774 |
v_start = self.inputarg(Signed, arg=1) |
| 775 |
if s_stop.is_constant() and s_stop.const is None: |
| 776 |
return "startonly", [v_start] |
| 777 |
else: |
| 778 |
v_stop = self.inputarg(Signed, arg=2) |
| 779 |
return "startstop", [v_start, v_stop] |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
class LowLevelOpList(list): |
| 784 |
"""A list with gen*() methods to build and append low-level |
| 785 |
operations to it. |
| 786 |
""" |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
llop_raising_exceptions = None |
| 791 |
implicit_exceptions_checked = None |
| 792 |
|
| 793 |
def __init__(self, rtyper=None, originalblock=None): |
| 794 |
self.rtyper = rtyper |
| 795 |
self.originalblock = originalblock |
| 796 |
|
| 797 |
def getparentgraph(self): |
| 798 |
return self.rtyper.annotator.annotated[self.originalblock] |
| 799 |
|
| 800 |
def hasparentgraph(self): |
| 801 |
return self.originalblock is not None |
| 802 |
|
| 803 |
def record_extra_call(self, graph): |
| 804 |
if self.hasparentgraph(): |
| 805 |
self.rtyper.annotator.translator.update_call_graph( |
| 806 |
caller_graph = self.getparentgraph(), |
| 807 |
callee_graph = graph, |
| 808 |
position_tag = object()) |
| 809 |
|
| 810 |
def convertvar(self, orig_v, r_from, r_to): |
| 811 |
assert isinstance(orig_v, (Variable, Constant)) |
| 812 |
if r_from != r_to: |
| 813 |
v = pair(r_from, r_to).convert_from_to(orig_v, self) |
| 814 |
if v is NotImplemented: |
| 815 |
raise TyperError("don't know how to convert from %r to %r" % |
| 816 |
(r_from, r_to)) |
| 817 |
if v.concretetype != r_to.lowleveltype: |
| 818 |
raise TyperError("bug in conversion from %r to %r: " |
| 819 |
"returned a %r" % (r_from, r_to, |
| 820 |
v.concretetype)) |
| 821 |
else: |
| 822 |
v = orig_v |
| 823 |
return v |
| 824 |
|
| 825 |
def genop(self, opname, args_v, resulttype=None): |
| 826 |
try: |
| 827 |
for v in args_v: |
| 828 |
v.concretetype |
| 829 |
except AttributeError: |
| 830 |
raise AssertionError("wrong level! you must call hop.inputargs()" |
| 831 |
" and pass its result to genop()," |
| 832 |
" never hop.args_v directly.") |
| 833 |
vresult = Variable() |
| 834 |
self.append(SpaceOperation(opname, args_v, vresult)) |
| 835 |
if resulttype is None: |
| 836 |
vresult.concretetype = Void |
| 837 |
return None |
| 838 |
else: |
| 839 |
if isinstance(resulttype, Repr): |
| 840 |
resulttype = resulttype.lowleveltype |
| 841 |
assert isinstance(resulttype, LowLevelType) |
| 842 |
vresult.concretetype = resulttype |
| 843 |
return vresult |
| 844 |
|
| 845 |
def gendirectcall(self, ll_function, *args_v): |
| 846 |
rtyper = self.rtyper |
| 847 |
args_s = [] |
| 848 |
newargs_v = [] |
| 849 |
with rtyper.annotator.using_policy(rtyper.lowlevel_ann_policy): |
| 850 |
for v in args_v: |
| 851 |
if v.concretetype is Void: |
| 852 |
s_value = rtyper.annotation(v) |
| 853 |
if s_value is None: |
| 854 |
s_value = annmodel.s_None |
| 855 |
if not s_value.is_constant(): |
| 856 |
raise TyperError("non-constant variable of type Void") |
| 857 |
if not isinstance(s_value, (annmodel.SomePBC, annmodel.SomeNone)): |
| 858 |
raise TyperError("non-PBC Void argument: %r", (s_value,)) |
| 859 |
args_s.append(s_value) |
| 860 |
else: |
| 861 |
args_s.append(lltype_to_annotation(v.concretetype)) |
| 862 |
newargs_v.append(v) |
| 863 |
|
| 864 |
self.rtyper.call_all_setups() |
| 865 |
|
| 866 |
|
| 867 |
if hasattr(ll_function, 'im_func'): |
| 868 |
bk = rtyper.annotator.bookkeeper |
| 869 |
args_s.insert(0, bk.immutablevalue(ll_function.im_self)) |
| 870 |
newargs_v.insert(0, inputconst(Void, ll_function.im_self)) |
| 871 |
ll_function = ll_function.im_func |
| 872 |
|
| 873 |
graph = annotate_lowlevel_helper(rtyper.annotator, ll_function, args_s, |
| 874 |
rtyper.lowlevel_ann_policy) |
| 875 |
self.record_extra_call(graph) |
| 876 |
|
| 877 |
|
| 878 |
f = self.rtyper.getcallable(graph) |
| 879 |
c = inputconst(typeOf(f), f) |
| 880 |
fobj = f._obj |
| 881 |
return self.genop('direct_call', [c]+newargs_v, |
| 882 |
resulttype = typeOf(fobj).RESULT) |
| 883 |
|
| 884 |
def genconst(self, ll_value): |
| 885 |
return inputconst(typeOf(ll_value), ll_value) |
| 886 |
|
| 887 |
def genvoidconst(self, placeholder): |
| 888 |
return inputconst(Void, placeholder) |
| 889 |
|
| 890 |
def constTYPE(self, T): |
| 891 |
return T |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
from rpython.rtyper import rint, rbool, rfloat, rnone |
| 897 |
from rpython.rtyper import rrange |
| 898 |
from rpython.rtyper import rstr, rdict, rlist, rbytearray |
| 899 |
from rpython.rtyper import rbuiltin, rpbc |
| 900 |
from rpython.rtyper import rptr |
| 901 |
from rpython.rtyper import rweakref |
| 902 |
from rpython.rtyper import raddress |