Ejemplo n.º 1
0
        def method_matches(self, s_s, s_pos):
            assert model.SomeString().contains(s_s)
            assert model.SomeInteger(nonneg=True).contains(s_pos)

            bk = getbookkeeper()
            init_pbc = bk.immutablevalue(Match.__init__)
            bk.emulate_pbc_call((self, "match_init"), init_pbc, [
                model.SomeInstance(bk.getuniqueclassdef(Match)),
                model.SomeInteger(nonneg=True),
                model.SomeInteger(nonneg=True)
            ])
            init_pbc = bk.immutablevalue(rsre_core.StrMatchContext.__init__)
            bk.emulate_pbc_call((self, "str_match_context_init"), init_pbc, [
                model.SomeInstance(
                    bk.getuniqueclassdef(rsre_core.StrMatchContext)),
                bk.newlist(model.SomeInteger(nonneg=True)),
                model.SomeString(),
                model.SomeInteger(nonneg=True),
                model.SomeInteger(nonneg=True),
                model.SomeInteger(nonneg=True),
            ])
            match_context_pbc = bk.immutablevalue(rsre_core.match_context)
            bk.emulate_pbc_call((self, "match_context"), match_context_pbc, [
                model.SomeInstance(
                    bk.getuniqueclassdef(rsre_core.StrMatchContext)),
            ])

            return model.SomeInstance(getbookkeeper().getuniqueclassdef(Match),
                                      can_be_None=True)
Ejemplo n.º 2
0
def default_specialize(funcdesc, args_s):
    # first flatten the *args
    args_s, key, builder = flatten_star_args(funcdesc, args_s)
    # two versions: a regular one and one for instances with 'access_directly'
    jit_look_inside = getattr(funcdesc.pyobj, '_jit_look_inside_', True)
    # change args_s in place, "official" interface
    access_directly = False
    for i, s_obj in enumerate(args_s):
        if (isinstance(s_obj, annmodel.SomeInstance)
                and 'access_directly' in s_obj.flags):
            if jit_look_inside:
                access_directly = True
                key = (AccessDirect, key)
                break
            else:
                new_flags = s_obj.flags.copy()
                del new_flags['access_directly']
                new_s_obj = annmodel.SomeInstance(s_obj.classdef,
                                                  s_obj.can_be_None,
                                                  flags=new_flags)
                args_s[i] = new_s_obj

    # done
    graph = funcdesc.cachedgraph(key, builder=builder)
    if access_directly:
        graph.access_directly = True
    return graph
Ejemplo n.º 3
0
    def __init__(self, rtyper, s_pbc):
        self.rtyper = rtyper
        self.s_pbc = s_pbc
        mdescs = list(s_pbc.descriptions)
        methodname = mdescs[0].name
        classdef = mdescs[0].selfclassdef
        flags = mdescs[0].flags
        for mdesc in mdescs[1:]:
            if mdesc.name != methodname:
                raise TyperError("cannot find a unique name under which the "
                                 "methods can be found: %r" % (mdescs, ))
            if mdesc.flags != flags:
                raise TyperError("inconsistent 'flags': %r versus %r" %
                                 (mdesc.flags, flags))
            classdef = classdef.commonbase(mdesc.selfclassdef)
            if classdef is None:
                raise TyperError("mixing methods coming from instances of "
                                 "classes with no common base: %r" %
                                 (mdescs, ))

        self.methodname = methodname
        self.classdef = classdef.get_owner(methodname)
        # the low-level representation is just the bound 'self' argument.
        self.s_im_self = annmodel.SomeInstance(self.classdef, flags=flags)
        self.r_im_self = rclass.getinstancerepr(rtyper, self.classdef)
        self.lowleveltype = self.r_im_self.lowleveltype
Ejemplo n.º 4
0
 def compute_result_annotation(self, s_Class, s_ob):
     from rpython.annotator import model as annmodel
     from rpython.rtyper.llannotation import SomePtr
     assert isinstance(s_ob, SomePtr)
     assert s_Class.is_constant()
     classdef = self.bookkeeper.getuniqueclassdef(s_Class.const)
     return annmodel.SomeInstance(classdef, can_be_None=True)
Ejemplo n.º 5
0
def create_instantiate_function(annotator, classdef):
    # build the graph of a function that looks like
    #
    # def my_instantiate():
    #     return instantiate(cls)
    #
    if hasattr(classdef, 'my_instantiate_graph'):
        return
    v = Variable()
    block = Block([])
    block.operations.append(SpaceOperation('instantiate1', [], v))
    name = valid_identifier('instantiate_' + classdef.name)
    graph = FunctionGraph(name, block)
    block.closeblock(Link([v], graph.returnblock))
    annotator.setbinding(v, annmodel.SomeInstance(classdef))
    annotator.annotated[block] = graph
    # force the result to be converted to a generic OBJECTPTR
    generalizedresult = annmodel.SomeInstance(classdef=None)
    annotator.setbinding(graph.getreturnvar(), generalizedresult)
    classdef.my_instantiate_graph = graph
    annotator.translator.graphs.append(graph)
Ejemplo n.º 6
0
Archivo: jit.py Proyecto: weijiwei/pypy
 def compute_result_annotation(self, s_x, **kwds_s):
     from rpython.annotator import model as annmodel
     s_x = annmodel.not_const(s_x)
     access_directly = 's_access_directly' in kwds_s
     fresh_virtualizable = 's_fresh_virtualizable' in kwds_s
     if access_directly or fresh_virtualizable:
         assert access_directly, "lone fresh_virtualizable hint"
         if isinstance(s_x, annmodel.SomeInstance):
             from rpython.flowspace.model import Constant
             classdesc = s_x.classdef.classdesc
             virtualizable = classdesc.read_attribute(
                 '_virtualizable_', Constant(None)).value
             if virtualizable is not None:
                 flags = s_x.flags.copy()
                 flags['access_directly'] = True
                 if fresh_virtualizable:
                     flags['fresh_virtualizable'] = True
                 s_x = annmodel.SomeInstance(s_x.classdef, s_x.can_be_None,
                                             flags)
     return s_x
Ejemplo n.º 7
0
def cutoff_alwaysraising_block(self, block):
    "Fix a block whose end can never be reached at run-time."
    # search the operation that cannot succeed
    can_succeed = [
        op for op in block.operations if op.result.annotation is not None
    ]
    cannot_succeed = [
        op for op in block.operations if op.result.annotation is None
    ]
    n = len(can_succeed)
    # check consistency
    assert can_succeed == block.operations[:n]
    assert cannot_succeed == block.operations[n:]
    assert 0 <= n < len(block.operations)
    # chop off the unreachable end of the block
    del block.operations[n + 1:]
    self.setbinding(block.operations[n].result, annmodel.s_ImpossibleValue)
    # insert the equivalent of 'raise AssertionError'
    graph = self.annotated[block]
    msg = "Call to %r should have raised an exception" % (getattr(
        graph, 'func', None), )
    c1 = Constant(AssertionError)
    c2 = Constant(AssertionError(msg))
    errlink = Link([c1, c2], graph.exceptblock)
    block.recloseblock(errlink, *block.exits)
    # record new link to make the transformation idempotent
    self.links_followed[errlink] = True
    # fix the annotation of the exceptblock.inputargs
    etype, evalue = graph.exceptblock.inputargs
    s_type = annmodel.SomeType()
    s_type.is_type_of = [evalue]
    s_value = annmodel.SomeInstance(
        self.bookkeeper.getuniqueclassdef(Exception))
    self.setbinding(etype, s_type)
    self.setbinding(evalue, s_value)
    # make sure the bookkeeper knows about AssertionError
    self.bookkeeper.getuniqueclassdef(AssertionError)
Ejemplo n.º 8
0
def instance(cls):
    return lambda bookkeeper: model.SomeInstance(
        bookkeeper.getuniqueclassdef(cls))
Ejemplo n.º 9
0
 def compute_result_annotation(self, s_Class, s_ptr):
     assert s_Class.is_constant()
     classdef = self.bookkeeper.getuniqueclassdef(s_Class.const)
     return annmodel.SomeInstance(classdef, can_be_None=True)
Ejemplo n.º 10
0
 def s_r_instanceof(self, cls, can_be_None=True, check_never_seen=True):
     classdesc = self.rtyper.annotator.bookkeeper.getdesc(cls)
     classdef = classdesc.getuniqueclassdef()
     s_instance = annmodel.SomeInstance(classdef, can_be_None)
     r_instance = self.getdelayedrepr(s_instance, check_never_seen)
     return s_instance, r_instance
Ejemplo n.º 11
0
def instance(cls, can_be_None=False):
    return lambda bookkeeper: model.SomeInstance(
        bookkeeper.getuniqueclassdef(cls), can_be_None=can_be_None)
Ejemplo n.º 12
0
 def method_get(self, s_key):
     assert isinstance(s_key, annmodel.SomeInstance)
     assert s_key.classdef.issubclass(self.keyclassdef)
     return annmodel.SomeInstance(self.valueclassdef, can_be_None=True)
Ejemplo n.º 13
0
 def method_get(self, s_key):
     return annmodel.SomeInstance(self.valueclassdef, can_be_None=True)