Exemplo n.º 1
0
 def checkfunction(translator):
     # make sure that there is a sensible comparison defined on the
     # symbolics
     bk = translator.annotator.bookkeeper
     rtyper = translator.rtyper
     base_classdef = bk.getuniqueclassdef(PBase)
     base_vtable = rclass.getclassrepr(rtyper, base_classdef).getruntime(CLASSTYPE)
     sub3_classdef = bk.getuniqueclassdef(Sub3)
     sub3_vtable = rclass.getclassrepr(rtyper, sub3_classdef).getruntime(CLASSTYPE)
     assert ll_issubclass(sub3_vtable, base_vtable)
     assert not ll_issubclass(base_vtable, sub3_vtable)
Exemplo n.º 2
0
 def checkfunction(translator):
     # make sure that there is a sensible comparison defined on the
     # symbolics
     bk = translator.annotator.bookkeeper
     rtyper = translator.rtyper
     base_classdef = bk.getuniqueclassdef(PBase)
     base_vtable = rclass.getclassrepr(
         rtyper, base_classdef).getruntime(CLASSTYPE)
     sub3_classdef = bk.getuniqueclassdef(Sub3)
     sub3_vtable = rclass.getclassrepr(
         rtyper, sub3_classdef).getruntime(CLASSTYPE)
     assert ll_issubclass(sub3_vtable, base_vtable)
     assert not ll_issubclass(base_vtable, sub3_vtable)
Exemplo n.º 3
0
 def initialize_prebuilt_data(self, value, classdef, result):
     # then add instance attributes from this level
     classrepr = getclassrepr(self.rtyper, self.classdef)
     for mangled, (oot, default) in self.lowleveltype._allfields().items():
         if oot is ootype.Void:
             llattrvalue = None
         elif mangled == 'meta':
             llattrvalue = classrepr.get_meta_instance()
         else:
             name = unmangle(mangled, self.rtyper.getconfig())
             try:
                 attrvalue = getattr(value, name)
             except AttributeError:
                 attrvalue = self.classdef.classdesc.read_attribute(
                     name, None)
                 if attrvalue is None:
                     warning("prebuilt instance %r has no attribute %r" %
                             (value, name))
                     continue
                 llattrvalue = self.allfields[
                     mangled].convert_desc_or_const(attrvalue)
             else:
                 llattrvalue = self.allfields[mangled].convert_const(
                     attrvalue)
         setattr(result, mangled, llattrvalue)
Exemplo n.º 4
0
Arquivo: inline.py Projeto: Mu-L/pypy
 def rewire_exceptblock_with_guard(self, afterblock, copiedexceptblock):
     # this rewiring does not always succeed. in the cases where it doesn't
     # there will be generic code inserted
     from rpython.rtyper import rclass
     excdata = self.translator.rtyper.exceptiondata
     exc_match = excdata.fn_exception_match
     for link in self.entrymap[self.graph_to_inline.exceptblock]:
         if link.prevblock.exits[0] is not link:
             continue
         copiedblock = self.copy_block(link.prevblock)
         VALUE, copiedlink = _find_exception_type(copiedblock)
         #print copiedblock.operations
         if VALUE is None or VALUE not in self.lltype_to_classdef:
             continue
         classdef = self.lltype_to_classdef[VALUE]
         rtyper = self.translator.rtyper
         classrepr = rclass.getclassrepr(rtyper, classdef)
         vtable = classrepr.getruntime(excdata.lltype_of_exception_type)
         var_etype = copiedlink.args[0]
         var_evalue = copiedlink.args[1]
         for exceptionlink in afterblock.exits[1:]:
             if exc_match(vtable, exceptionlink.llexitcase):
                 passon_vars = self.passon_vars(link.prevblock)
                 copiedlink.target = exceptionlink.target
                 linkargs = self.find_args_in_exceptional_case(
                     exceptionlink, link.prevblock, var_etype, var_evalue, afterblock, passon_vars)
                 copiedlink.args = linkargs
                 break
Exemplo n.º 5
0
 def gettype_from_unboxed(self, llops, vinst, can_be_none=False):
     unboxedclass_repr = getclassrepr(self.rtyper, self.unboxedclassdef)
     cunboxedcls = inputconst(CLASSTYPE, unboxedclass_repr.getvtable())
     if self.is_parent:
         # If the lltype of vinst shows that it cannot be a tagged value,
         # we can directly read the typeptr.  Otherwise, call a helper that
         # checks if the tag bit is set in the pointer.
         unboxedinstance_repr = getinstancerepr(self.rtyper,
                                                self.unboxedclassdef)
         try:
             lltype.castable(unboxedinstance_repr.lowleveltype,
                             vinst.concretetype)
         except lltype.InvalidCast:
             can_be_tagged = False
         else:
             can_be_tagged = True
         vinst = llops.genop('cast_pointer', [vinst],
                             resulttype=self.common_repr())
         if can_be_tagged:
             if can_be_none:
                 func = ll_unboxed_getclass_canbenone
             else:
                 func = ll_unboxed_getclass
             return llops.gendirectcall(func, vinst,
                                        cunboxedcls)
         elif can_be_none:
             return llops.gendirectcall(ll_inst_type, vinst)
         else:
             ctypeptr = inputconst(lltype.Void, 'typeptr')
             return llops.genop('getfield', [vinst, ctypeptr],
                                resulttype = CLASSTYPE)
     else:
         return cunboxedcls
Exemplo n.º 6
0
 def rewire_exceptblock_with_guard(self, afterblock, copiedexceptblock):
     # this rewiring does not always succeed. in the cases where it doesn't
     # there will be generic code inserted
     from rpython.rtyper import rclass
     excdata = self.translator.rtyper.exceptiondata
     exc_match = excdata.fn_exception_match
     for link in self.entrymap[self.graph_to_inline.exceptblock]:
         if link.prevblock.exits[0] is not link:
             continue
         copiedblock = self.copy_block(link.prevblock)
         VALUE, copiedlink = _find_exception_type(copiedblock)
         #print copiedblock.operations
         if VALUE is None or VALUE not in self.lltype_to_classdef:
             continue
         classdef = self.lltype_to_classdef[VALUE]
         rtyper = self.translator.rtyper
         classrepr = rclass.getclassrepr(rtyper, classdef)
         vtable = classrepr.getruntime(excdata.lltype_of_exception_type)
         var_etype = copiedlink.args[0]
         var_evalue = copiedlink.args[1]
         for exceptionlink in afterblock.exits[1:]:
             if exc_match(vtable, exceptionlink.llexitcase):
                 passon_vars = self.passon_vars(link.prevblock)
                 copiedlink.target = exceptionlink.target
                 linkargs = self.find_args_in_exceptional_case(
                     exceptionlink, link.prevblock, var_etype, var_evalue, afterblock, passon_vars)
                 copiedlink.args = linkargs
                 break
Exemplo n.º 7
0
Arquivo: rclass.py Projeto: sota/pypy
    def __init__(self, rtyper, classdef):
        AbstractClassRepr.__init__(self, rtyper, classdef)
        # This is the Repr for a reference to the class 'classdef' or
        # any subclass.  In the simple case, the lowleveltype is just
        # ootype.Class.  If we need to store class attributes, we use a
        # "meta" class where the attributes are defined, and the class
        # reference is a reference to an instance of this meta class.
        extra_access_sets = self.rtyper.class_pbc_attributes.get(
            classdef, {})
        has_class_attributes = bool(extra_access_sets)
        if self.classdef is not None:
            self.rbase = getclassrepr(self.rtyper, self.classdef.basedef)
            meta_base_type = self.rbase.lowleveltype
            baseclass_has_meta = meta_base_type != ootype.Class
        else:
            baseclass_has_meta = False

        if not has_class_attributes and not baseclass_has_meta:
            self.lowleveltype = ootype.Class   # simple case
        else:
            if self.classdef is None:
                raise TyperError("the root 'object' class should not have"
                                 " class attributes")
            if self.classdef.classdesc.pyobj in standardexceptions:
                raise TyperError("Standard exception class %r should not have"
                                 " class attributes" % (self.classdef.name,))
            if not baseclass_has_meta:
                meta_base_type = META
            self.lowleveltype = ootype.Instance(
                    self.classdef.name + "_meta", meta_base_type)
Exemplo n.º 8
0
 def gettype_from_unboxed(self, llops, vinst, can_be_none=False):
     unboxedclass_repr = getclassrepr(self.rtyper, self.unboxedclassdef)
     cunboxedcls = inputconst(CLASSTYPE, unboxedclass_repr.getvtable())
     if self.is_parent:
         # If the lltype of vinst shows that it cannot be a tagged value,
         # we can directly read the typeptr.  Otherwise, call a helper that
         # checks if the tag bit is set in the pointer.
         unboxedinstance_repr = getinstancerepr(self.rtyper,
                                                self.unboxedclassdef)
         try:
             lltype.castable(unboxedinstance_repr.lowleveltype,
                             vinst.concretetype)
         except lltype.InvalidCast:
             can_be_tagged = False
         else:
             can_be_tagged = True
         vinst = llops.genop('cast_pointer', [vinst],
                             resulttype=self.common_repr())
         if can_be_tagged:
             if can_be_none:
                 func = ll_unboxed_getclass_canbenone
             else:
                 func = ll_unboxed_getclass
             return llops.gendirectcall(func, vinst, cunboxedcls)
         elif can_be_none:
             return llops.gendirectcall(ll_inst_type, vinst)
         else:
             ctypeptr = inputconst(lltype.Void, 'typeptr')
             return llops.genop('getfield', [vinst, ctypeptr],
                                resulttype=CLASSTYPE)
     else:
         return cunboxedcls
Exemplo n.º 9
0
    def __init__(self, rtyper, classdef):
        AbstractClassRepr.__init__(self, rtyper, classdef)
        # This is the Repr for a reference to the class 'classdef' or
        # any subclass.  In the simple case, the lowleveltype is just
        # ootype.Class.  If we need to store class attributes, we use a
        # "meta" class where the attributes are defined, and the class
        # reference is a reference to an instance of this meta class.
        extra_access_sets = self.rtyper.class_pbc_attributes.get(classdef, {})
        has_class_attributes = bool(extra_access_sets)
        if self.classdef is not None:
            self.rbase = getclassrepr(self.rtyper, self.classdef.basedef)
            meta_base_type = self.rbase.lowleveltype
            baseclass_has_meta = meta_base_type != ootype.Class
        else:
            baseclass_has_meta = False

        if not has_class_attributes and not baseclass_has_meta:
            self.lowleveltype = ootype.Class  # simple case
        else:
            if self.classdef is None:
                raise TyperError("the root 'object' class should not have"
                                 " class attributes")
            if self.classdef.classdesc.pyobj in standardexceptions:
                raise TyperError("Standard exception class %r should not have"
                                 " class attributes" % (self.classdef.name, ))
            if not baseclass_has_meta:
                meta_base_type = META
            self.lowleveltype = ootype.Instance(self.classdef.name + "_meta",
                                                meta_base_type)
Exemplo n.º 10
0
 def initialize_prebuilt_data(self, value, classdef, result):
     if self.classdef is not None:
         # recursively build the parent part of the instance
         self.rbase.initialize_prebuilt_data(value, classdef, result.super)
         # then add instance attributes from this level
         for name, (mangled_name, r) in self.fields.items():
             if r.lowleveltype is Void:
                 llattrvalue = None
             else:
                 try:
                     attrvalue = getattr(value, name)
                 except AttributeError:
                     attrvalue = self.classdef.classdesc.read_attribute(
                         name, None)
                     if attrvalue is None:
                         # Ellipsis from get_reusable_prebuilt_instance()
                         if value is not Ellipsis:
                             warning("prebuilt instance %r has no "
                                     "attribute %r" % (value, name))
                         llattrvalue = r.lowleveltype._defl()
                     else:
                         llattrvalue = r.convert_desc_or_const(attrvalue)
                 else:
                     llattrvalue = r.convert_const(attrvalue)
             setattr(result, mangled_name, llattrvalue)
     else:
         # OBJECT part
         rclass = getclassrepr(self.rtyper, classdef)
         result.typeptr = rclass.getvtable()
Exemplo n.º 11
0
 def new_instance(self, llops, classcallhop=None):
     """Build a new instance, without calling __init__."""
     classrepr = getclassrepr(self.rtyper, self.classdef)
     v_instance = llops.genop("new",
                              [inputconst(ootype.Void, self.lowleveltype)],
                              self.lowleveltype)
     return v_instance
Exemplo n.º 12
0
Arquivo: rclass.py Projeto: sota/pypy
 def initialize_prebuilt_data(self, value, classdef, result):
     if self.classdef is not None:
         # recursively build the parent part of the instance
         self.rbase.initialize_prebuilt_data(value, classdef, result.super)
         # then add instance attributes from this level
         for name, (mangled_name, r) in self.fields.items():
             if r.lowleveltype is Void:
                 llattrvalue = None
             else:
                 try:
                     attrvalue = getattr(value, name)
                 except AttributeError:
                     attrvalue = self.classdef.classdesc.read_attribute(name, None)
                     if attrvalue is None:
                         # Ellipsis from get_reusable_prebuilt_instance()
                         if value is not Ellipsis:
                             warning("prebuilt instance %r has no "
                                     "attribute %r" % (value, name))
                         llattrvalue = r.lowleveltype._defl()
                     else:
                         llattrvalue = r.convert_desc_or_const(attrvalue)
                 else:
                     llattrvalue = r.convert_const(attrvalue)
             setattr(result, mangled_name, llattrvalue)
     else:
         # OBJECT part
         rclass = getclassrepr(self.rtyper, classdef)
         result.typeptr = rclass.getvtable()
Exemplo n.º 13
0
 def convert_desc(self, desc):
     if desc not in self.s_pbc.descriptions:
         raise TyperError("%r not in %r" % (desc, self))
     if self.lowleveltype is Void:
         return None
     subclassdef = desc.getuniqueclassdef()
     r_subclass = rclass.getclassrepr(self.rtyper, subclassdef)
     return r_subclass.getruntime(self.lowleveltype)
Exemplo n.º 14
0
 def convert_desc(self, desc):
     if desc not in self.s_pbc.descriptions:
         raise TyperError("%r not in %r" % (desc, self))
     if self.lowleveltype is Void:
         return None
     subclassdef = desc.getuniqueclassdef()
     r_subclass = rclass.getclassrepr(self.rtyper, subclassdef)
     return r_subclass.getruntime(self.lowleveltype)
Exemplo n.º 15
0
 def specialize_call(self, hop):
     from rpython.rtyper.rclass import getclassrepr, CLASSTYPE
     from rpython.flowspace.model import Constant
     Class = hop.args_s[0].const
     classdef = hop.rtyper.annotator.bookkeeper.getuniqueclassdef(Class)
     classrepr = getclassrepr(hop.rtyper, classdef)
     vtable = classrepr.getvtable()
     assert lltype.typeOf(vtable) == CLASSTYPE
     hop.exception_cannot_occur()
     return Constant(vtable, concretetype=CLASSTYPE)
Exemplo n.º 16
0
Arquivo: rgc.py Projeto: Qointum/pypy
 def specialize_call(self, hop):
     from rpython.rtyper.rclass import getclassrepr, CLASSTYPE
     from rpython.flowspace.model import Constant
     Class = hop.args_s[0].const
     classdef = hop.rtyper.annotator.bookkeeper.getuniqueclassdef(Class)
     classrepr = getclassrepr(hop.rtyper, classdef)
     vtable = classrepr.getvtable()
     assert lltype.typeOf(vtable) == CLASSTYPE
     hop.exception_cannot_occur()
     return Constant(vtable, concretetype=CLASSTYPE)
Exemplo n.º 17
0
 def getlowleveltype(self):
     classdescs = list(self.s_pbc.descriptions)
     # if any of the classdefs get the lowleveltype ootype.Class,
     # we can only pick ootype.Class for us too.  Otherwise META.
     for classdesc in classdescs:
         for classdef in classdesc.getallclassdefs():
             r_class = getclassrepr(self.rtyper, classdef)
             if r_class.lowleveltype == ootype.Class:
                 return ootype.Class
     else:
         return META
Exemplo n.º 18
0
Arquivo: rpbc.py Projeto: sota/pypy
 def getlowleveltype(self):
     classdescs = list(self.s_pbc.descriptions)
     # if any of the classdefs get the lowleveltype ootype.Class,
     # we can only pick ootype.Class for us too.  Otherwise META.
     for classdesc in classdescs:
         for classdef in classdesc.getallclassdefs():
             r_class = getclassrepr(self.rtyper, classdef)
             if r_class.lowleveltype == ootype.Class:
                 return ootype.Class
     else:
         return META
Exemplo n.º 19
0
 def __init__(self, rtyper):
     self.make_standard_exceptions(rtyper)
     # (NB. rclass identifies 'Exception' and 'object')
     r_type = rclass.getclassrepr(rtyper, None)
     r_instance = rclass.getinstancerepr(rtyper, None)
     r_type.setup()
     r_instance.setup()
     self.r_exception_type = r_type
     self.r_exception_value = r_instance
     self.lltype_of_exception_type = r_type.lowleveltype
     self.lltype_of_exception_value = r_instance.lowleveltype
     self.rtyper = rtyper
Exemplo n.º 20
0
 def get_access_set(self, attrname):
     """Return the ClassAttrFamily corresponding to accesses to 'attrname'
     and the ClassRepr of the class which stores this attribute in
     its vtable.
     """
     classdescs = list(self.s_pbc.descriptions)
     access = classdescs[0].queryattrfamily(attrname)
     for classdesc in classdescs[1:]:
         access1 = classdesc.queryattrfamily(attrname)
         assert access1 is access       # XXX not implemented
     if access is None:
         raise rclass.MissingRTypeAttribute(attrname)
     commonbase = access.commonbase
     class_repr = rclass.getclassrepr(self.rtyper, commonbase)
     return access, class_repr
Exemplo n.º 21
0
 def get_access_set(self, attrname):
     """Return the ClassAttrFamily corresponding to accesses to 'attrname'
     and the ClassRepr of the class which stores this attribute in
     its vtable.
     """
     classdescs = list(self.s_pbc.descriptions)
     access = classdescs[0].queryattrfamily(attrname)
     for classdesc in classdescs[1:]:
         access1 = classdesc.queryattrfamily(attrname)
         assert access1 is access  # XXX not implemented
     if access is None:
         raise rclass.MissingRTypeAttribute(attrname)
     commonbase = access.commonbase
     class_repr = rclass.getclassrepr(self.rtyper, commonbase)
     return access, class_repr
Exemplo n.º 22
0
 def _setup_repr(self):
     # NOTE: don't store mutable objects like the dicts below on 'self'
     #       before they are fully built, to avoid strange bugs in case
     #       of recursion where other code would uses these
     #       partially-initialized dicts.
     clsfields = {}
     pbcfields = {}
     allmethods = {}
     if self.classdef is not None:
         # class attributes
         llfields = []
         attrs = self.classdef.attrs.items()
         attrs.sort()
         for name, attrdef in attrs:
             if attrdef.readonly:
                 s_value = attrdef.s_value
                 s_unboundmethod = self.prepare_method(s_value)
                 if s_unboundmethod is not None:
                     allmethods[name] = True
                     s_value = s_unboundmethod
                 r = self.rtyper.getrepr(s_value)
                 mangled_name = 'cls_' + name
                 clsfields[name] = mangled_name, r
                 llfields.append((mangled_name, r.lowleveltype))
         # attributes showing up in getattrs done on the class as a PBC
         extra_access_sets = self.rtyper.class_pbc_attributes.get(
             self.classdef, {})
         for access_set, (attr, counter) in extra_access_sets.items():
             r = self.rtyper.getrepr(access_set.s_value)
             mangled_name = mangle('pbc%d' % counter, attr)
             pbcfields[access_set, attr] = mangled_name, r
             llfields.append((mangled_name, r.lowleveltype))
         #
         self.rbase = getclassrepr(self.rtyper, self.classdef.basedef)
         self.rbase.setup()
         kwds = {'hints': {'immutable': True}}
         vtable_type = Struct('%s_vtable' % self.classdef.name,
                              ('super', self.rbase.vtable_type), *llfields,
                              **kwds)
         self.vtable_type.become(vtable_type)
         allmethods.update(self.rbase.allmethods)
     self.clsfields = clsfields
     self.pbcfields = pbcfields
     self.allmethods = allmethods
     self.vtable = None
Exemplo n.º 23
0
Arquivo: rclass.py Projeto: sota/pypy
 def _setup_repr(self):
     # NOTE: don't store mutable objects like the dicts below on 'self'
     #       before they are fully built, to avoid strange bugs in case
     #       of recursion where other code would uses these
     #       partially-initialized dicts.
     clsfields = {}
     pbcfields = {}
     allmethods = {}
     if self.classdef is not None:
         # class attributes
         llfields = []
         attrs = self.classdef.attrs.items()
         attrs.sort()
         for name, attrdef in attrs:
             if attrdef.readonly:
                 s_value = attrdef.s_value
                 s_unboundmethod = self.prepare_method(s_value)
                 if s_unboundmethod is not None:
                     allmethods[name] = True
                     s_value = s_unboundmethod
                 r = self.rtyper.getrepr(s_value)
                 mangled_name = 'cls_' + name
                 clsfields[name] = mangled_name, r
                 llfields.append((mangled_name, r.lowleveltype))
         # attributes showing up in getattrs done on the class as a PBC
         extra_access_sets = self.rtyper.class_pbc_attributes.get(
             self.classdef, {})
         for access_set, (attr, counter) in extra_access_sets.items():
             r = self.rtyper.getrepr(access_set.s_value)
             mangled_name = mangle('pbc%d' % counter, attr)
             pbcfields[access_set, attr] = mangled_name, r
             llfields.append((mangled_name, r.lowleveltype))
         #
         self.rbase = getclassrepr(self.rtyper, self.classdef.basedef)
         self.rbase.setup()
         kwds = {'hints': {'immutable': True}}
         vtable_type = Struct('%s_vtable' % self.classdef.name,
                              ('super', self.rbase.vtable_type),
                              *llfields, **kwds)
         self.vtable_type.become(vtable_type)
         allmethods.update(self.rbase.allmethods)
     self.clsfields = clsfields
     self.pbcfields = pbcfields
     self.allmethods = allmethods
     self.vtable = None
Exemplo n.º 24
0
Arquivo: rclass.py Projeto: sota/pypy
 def initialize_prebuilt_data(self, value, classdef, result):
     # then add instance attributes from this level
     classrepr = getclassrepr(self.rtyper, self.classdef)
     for mangled, (oot, default) in self.lowleveltype._allfields().items():
         if oot is ootype.Void:
             llattrvalue = None
         elif mangled == 'meta':
             llattrvalue = classrepr.get_meta_instance()
         else:
             name = unmangle(mangled, self.rtyper.getconfig())
             try:
                 attrvalue = getattr(value, name)
             except AttributeError:
                 attrvalue = self.classdef.classdesc.read_attribute(name, None)
                 if attrvalue is None:
                     warning("prebuilt instance %r has no attribute %r" % (
                             value, name))
                     continue
                 llattrvalue = self.allfields[mangled].convert_desc_or_const(attrvalue)
             else:
                 llattrvalue = self.allfields[mangled].convert_const(attrvalue)
         setattr(result, mangled, llattrvalue)
Exemplo n.º 25
0
Arquivo: rclass.py Projeto: sota/pypy
    def _setup_repr(self, llfields=None, hints=None, adtmeths=None):
        # NOTE: don't store mutable objects like the dicts below on 'self'
        #       before they are fully built, to avoid strange bugs in case
        #       of recursion where other code would uses these
        #       partially-initialized dicts.
        AbstractInstanceRepr._setup_repr(self)
        self.rclass = getclassrepr(self.rtyper, self.classdef)
        fields = {}
        allinstancefields = {}
        if self.classdef is None:
            fields['__class__'] = 'typeptr', get_type_repr(self.rtyper)
        else:
            # instance attributes
            attrs = self.classdef.attrs.items()
            attrs.sort()
            myllfields = []
            for name, attrdef in attrs:
                if not attrdef.readonly:
                    r = self.rtyper.getrepr(attrdef.s_value)
                    mangled_name = 'inst_' + name
                    fields[name] = mangled_name, r
                    myllfields.append((mangled_name, r.lowleveltype))

            # Sort the instance attributes by decreasing "likely size",
            # as reported by rffi.sizeof(), to minimize padding holes in C.
            # Fields of the same size are sorted by name (by attrs.sort()
            # above) just to minimize randomness.
            def keysize((_, T)):
                if T is lltype.Void:
                    return None
                from rpython.rtyper.lltypesystem.rffi import sizeof
                try:
                    return -sizeof(T)
                except StandardError:
                    return None
            myllfields.sort(key = keysize)
            if llfields is None:
                llfields = myllfields
            else:
                llfields = llfields + myllfields

            self.rbase = getinstancerepr(self.rtyper, self.classdef.basedef,
                                         self.gcflavor)
            self.rbase.setup()

            MkStruct = lltype.STRUCT_BY_FLAVOR[LLFLAVOR[self.gcflavor]]
            if adtmeths is None:
                adtmeths = {}
            if hints is None:
                hints = {}
            hints = self._check_for_immutable_hints(hints)
            kwds = {}
            if self.gcflavor == 'gc':
                kwds['rtti'] = True

            for name, attrdef in attrs:
                if not attrdef.readonly and self.is_quasi_immutable(name):
                    llfields.append(('mutate_' + name, OBJECTPTR))

            object_type = MkStruct(self.classdef.name,
                                   ('super', self.rbase.object_type),
                                   hints=hints,
                                   adtmeths=adtmeths,
                                   *llfields,
                                   **kwds)
            self.object_type.become(object_type)
            allinstancefields.update(self.rbase.allinstancefields)
        allinstancefields.update(fields)
        self.fields = fields
        self.allinstancefields = allinstancefields
Exemplo n.º 26
0
 def finish(self, rtyper):
     bk = rtyper.annotator.bookkeeper
     for cls in self.standardexceptions:
         classdef = bk.getuniqueclassdef(cls)
         getclassrepr(rtyper, classdef).setup()
Exemplo n.º 27
0
def run_guards_translated(gcremovetypeptr):
    class A(object):
        pass
    class B(A):
        pass
    class C(B):
        pass
    def main(argv):
        A(); B().foo = len(argv); C()
        return 0

    t = TranslationContext()
    t.config.translation.gc = "minimark"
    t.config.translation.gcremovetypeptr = gcremovetypeptr
    ann = t.buildannotator()
    ann.build_types(main, [s_list_of_strings], main_entry_point=True)
    rtyper = t.buildrtyper()
    rtyper.specialize()

    classdef = ann.bookkeeper.getuniqueclassdef(B)
    rclass = getclassrepr(rtyper, classdef)
    rinstance = getinstancerepr(rtyper, classdef)
    LLB = rinstance.lowleveltype.TO
    ptr_vtable_B = rclass.getvtable()
    adr_vtable_B = llmemory.cast_ptr_to_adr(ptr_vtable_B)
    vtable_B = llmemory.cast_adr_to_int(adr_vtable_B, mode="symbolic")

    CPU = getcpuclass()
    cpu = CPU(rtyper, NoStats(),
              translate_support_code=True,
              gcdescr=get_description(t.config))
    execute_token = cpu.make_execute_token(llmemory.GCREF)
    finaldescr = BasicFinalDescr()
    faildescr = BasicFailDescr()

    descr_B = cpu.sizeof(LLB, ptr_vtable_B)
    typeid_B = descr_B.get_type_id()
    fielddescr_B = cpu.fielddescrof(LLB, 'inst_foo')

    LLD = lltype.GcStruct('D', ('dd', lltype.Signed))
    descr_D = cpu.sizeof(LLD)
    fielddescr_D = cpu.fielddescrof(LLD, 'dd')

    ARRAY = lltype.GcArray(lltype.Signed)
    arraydescr = cpu.arraydescrof(ARRAY)

    loop1 = parse("""
    [p0]
    guard_class(p0, ConstInt(vtable_B), descr=faildescr) []
    finish(descr=finaldescr)
    """, namespace={'finaldescr': finaldescr,
                    'faildescr': faildescr,
                    'vtable_B': vtable_B})

    loop2 = parse("""
    [p0]
    guard_gc_type(p0, ConstInt(typeid_B), descr=faildescr) []
    finish(descr=finaldescr)
    """, namespace={'finaldescr': finaldescr,
                    'faildescr': faildescr,
                    'typeid_B': typeid_B})

    loop3 = parse("""
    [p0]
    guard_is_object(p0, descr=faildescr) []
    finish(descr=finaldescr)
    """, namespace={'finaldescr': finaldescr,
                    'faildescr': faildescr})

    loop4 = parse("""
    [p0]
    guard_subclass(p0, ConstInt(vtable_B), descr=faildescr) []
    finish(descr=finaldescr)
    """, namespace={'finaldescr': finaldescr,
                    'faildescr': faildescr,
                    'vtable_B': vtable_B})

    def g():
        cpu.setup_once()
        token1 = JitCellToken()
        token2 = JitCellToken()
        token3 = JitCellToken()
        token4 = JitCellToken()
        cpu.compile_loop(loop1.inputargs, loop1.operations, token1)
        cpu.compile_loop(loop2.inputargs, loop2.operations, token2)
        cpu.compile_loop(loop3.inputargs, loop3.operations, token3)
        cpu.compile_loop(loop4.inputargs, loop4.operations, token4)

        for token, p0 in [
                (token1, rffi.cast(llmemory.GCREF, A())),
                (token1, rffi.cast(llmemory.GCREF, B())),
                (token1, rffi.cast(llmemory.GCREF, C())),

                (token2, rffi.cast(llmemory.GCREF, A())),
                (token2, rffi.cast(llmemory.GCREF, B())),
                (token2, rffi.cast(llmemory.GCREF, C())),
                (token2, rffi.cast(llmemory.GCREF, [42, 43])),

                (token3, rffi.cast(llmemory.GCREF, A())),
                (token3, rffi.cast(llmemory.GCREF, B())),
                (token3, rffi.cast(llmemory.GCREF, [44, 45])),

                (token4, rffi.cast(llmemory.GCREF, A())),
                (token4, rffi.cast(llmemory.GCREF, B())),
                (token4, rffi.cast(llmemory.GCREF, C())),
                ]:
            frame = execute_token(token, p0)
            descr = cpu.get_latest_descr(frame)
            if descr is finaldescr:
                print 'match'
            elif descr is faildescr:
                print 'fail'
            else:
                print '???'
            #
            if token is token2:    # guard_gc_type
                print int(cpu.get_actual_typeid(p0) == typeid_B)
            if token is token3:    # guard_is_object
                print int(cpu.check_is_object(p0))

        for p0 in [lltype.nullptr(llmemory.GCREF.TO),
                   rffi.cast(llmemory.GCREF, A()),
                   rffi.cast(llmemory.GCREF, B()),
                   rffi.cast(llmemory.GCREF, C()),
                   rffi.cast(llmemory.GCREF, lltype.malloc(LLD)),
                   rffi.cast(llmemory.GCREF, lltype.malloc(ARRAY, 5)),
                   rffi.cast(llmemory.GCREF, "foobar"),
                   rffi.cast(llmemory.GCREF, u"foobaz")]:
            results = ['B', 'D', 'A', 'S', 'U']
            try:
                cpu.protect_speculative_field(p0, fielddescr_B)
            except SpeculativeError:
                results[0] = '-'
            try:
                cpu.protect_speculative_field(p0, fielddescr_D)
            except SpeculativeError:
                results[1] = '-'
            try:
                cpu.protect_speculative_array(p0, arraydescr)
            except SpeculativeError:
                results[2] = '-'
            try:
                cpu.protect_speculative_string(p0)
            except SpeculativeError:
                results[3] = '-'
            try:
                cpu.protect_speculative_unicode(p0)
            except SpeculativeError:
                results[4] = '-'
            print ''.join(results)


    call_initial_function(t, g)

    cbuilder = genc.CStandaloneBuilder(t, main, t.config)
    cbuilder.generate_source(defines=cbuilder.DEBUG_DEFINES)
    cbuilder.compile()
    
    data = cbuilder.cmdexec('')
    assert data == ('fail\n'
                    'match\n'
                    'fail\n'

                    'fail\n'  '0\n'
                    'match\n' '1\n'
                    'fail\n'  '0\n'
                    'fail\n'  '0\n'

                    'match\n' '1\n'
                    'match\n' '1\n'
                    'fail\n'  '0\n'

                    'fail\n'
                    'match\n'
                    'match\n'

                    '-----\n'   # null
                    '-----\n'   # instance of A
                    'B----\n'   # instance of B
                    'B----\n'   # instance of C
                    '-D---\n'
                    '--A--\n'
                    '---S-\n'
                    '----U\n'
                    )
Exemplo n.º 28
0
    def _setup_repr(self, llfields=None, hints=None):
        if hints:
            self.lowleveltype._hints.update(hints)

        if self.classdef is None:
            self.fields = {}
            self.allfields = {}
            self.allmethods = {}
            self.allclassattributes = {}
            self.classattributes = {}
            return

        if self.baserepr is not None:
            allfields = self.baserepr.allfields.copy()
            allmethods = self.baserepr.allmethods.copy()
            allclassattributes = self.baserepr.allclassattributes.copy()
        else:
            allfields = {}
            allmethods = {}
            allclassattributes = {}

        fields = {}
        nonmangledfields = []
        fielddefaults = {}

        if llfields:
            fields.update(dict(llfields))

        selfattrs = self.classdef.attrs

        for name, attrdef in selfattrs.iteritems():
            mangled = mangle(name, self.rtyper.getconfig())
            if not attrdef.readonly:
                repr = self.rtyper.getrepr(attrdef.s_value)
                allfields[mangled] = repr
                oot = repr.lowleveltype
                fields[mangled] = oot
                nonmangledfields.append(name)
                try:
                    value = self.classdef.classdesc.read_attribute(name)
                    fielddefaults[mangled] = repr.convert_desc_or_const(value)
                except AttributeError:
                    pass
            else:
                s_value = attrdef.s_value
                if isinstance(s_value, annmodel.SomePBC):
                    if len(s_value.descriptions) > 0 and s_value.getKind(
                    ) == description.MethodDesc:
                        # attrdef is for a method
                        if mangled in allclassattributes:
                            raise TyperError(
                                "method overrides class attribute")
                        allmethods[
                            mangled] = name, self.classdef.lookup_filter(
                                s_value)
                        continue
                # class attribute
                if mangled in allmethods:
                    raise TyperError("class attribute overrides method")
                allclassattributes[mangled] = name, s_value

        special_methods = ["__init__", "__del__"]
        for meth_name in special_methods:
            if meth_name not in selfattrs and \
                    self.classdef.classdesc.find_source_for(meth_name) is not None:
                s_meth = self.classdef.classdesc.s_get_value(
                    self.classdef, meth_name)
                if isinstance(s_meth, annmodel.SomePBC):
                    mangled = mangle(meth_name, self.rtyper.getconfig())
                    allmethods[mangled] = meth_name, s_meth
                # else: it's the __init__ of a builtin exception

        ootype.addFields(self.lowleveltype, fields)

        self.rbase = getinstancerepr(self.rtyper, self.classdef.basedef)
        self.rbase.setup()

        for name, attrdef in selfattrs.iteritems():
            if not attrdef.readonly and self.is_quasi_immutable(name):
                name = mangle('mutable_' + name, self.rtyper.getconfig())
                ootype.addFields(self.lowleveltype, {name: OBJECT})

        classattributes = {}
        baseInstance = self.lowleveltype._superclass
        classrepr = getclassrepr(self.rtyper, self.classdef)

        # if this class has a corresponding metaclass, attach
        # a getmeta() method to get the corresponding meta_instance
        if classrepr.lowleveltype != ootype.Class:
            oovalue = classrepr.get_meta_instance()
            self.attach_class_attr_accessor('getmeta', oovalue)

        for classdef in self.classdef.getmro():
            for name, attrdef in classdef.attrs.iteritems():
                if not attrdef.readonly:
                    continue
                mangled = mangle(name, self.rtyper.getconfig())
                if mangled in allclassattributes:
                    selfdesc = self.classdef.classdesc
                    if name not in selfattrs:
                        # if the attr was already found in a parent class,
                        # we register it again only if it is overridden.
                        if selfdesc.find_source_for(name) is None:
                            continue
                        value = selfdesc.read_attribute(name)
                    else:
                        # otherwise, for new attrs, we look in all parent
                        # classes to see if it's defined in a parent but only
                        # actually first used in self.classdef.
                        value = selfdesc.read_attribute(name, None)

                    # a non-method class attribute
                    if not attrdef.s_value.is_constant():
                        classattributes[mangled] = attrdef.s_value, value

        self.fields = nonmangledfields
        self.allfields = allfields
        self.allmethods = allmethods
        self.allclassattributes = allclassattributes
        self.classattributes = classattributes
        # the following is done after the rest of the initialization because
        # convert_const can require 'self' to be fully initialized.

        # step 2: provide default values for fields
        for mangled, impl in fielddefaults.items():
            oot = fields[mangled]
            ootype.addFields(self.lowleveltype, {mangled: (oot, impl)},
                             with_default=True)
Exemplo n.º 29
0
def run_guards_translated(gcremovetypeptr):
    class A(object):
        pass

    class B(A):
        pass

    class C(B):
        pass

    def main(argv):
        A()
        B().foo = len(argv)
        C()
        return 0

    t = TranslationContext()
    t.config.translation.gc = "minimark"
    t.config.translation.gcremovetypeptr = gcremovetypeptr
    ann = t.buildannotator()
    ann.build_types(main, [s_list_of_strings], main_entry_point=True)
    rtyper = t.buildrtyper()
    rtyper.specialize()

    classdef = ann.bookkeeper.getuniqueclassdef(B)
    rclass = getclassrepr(rtyper, classdef)
    rinstance = getinstancerepr(rtyper, classdef)
    LLB = rinstance.lowleveltype.TO
    ptr_vtable_B = rclass.getvtable()
    adr_vtable_B = llmemory.cast_ptr_to_adr(ptr_vtable_B)
    vtable_B = llmemory.cast_adr_to_int(adr_vtable_B, mode="symbolic")

    CPU = getcpuclass()
    cpu = CPU(rtyper,
              NoStats(),
              translate_support_code=True,
              gcdescr=get_description(t.config))
    execute_token = cpu.make_execute_token(llmemory.GCREF)
    finaldescr = BasicFinalDescr()
    faildescr = BasicFailDescr()

    descr_B = cpu.sizeof(LLB, ptr_vtable_B)
    typeid_B = descr_B.get_type_id()
    fielddescr_B = cpu.fielddescrof(LLB, 'inst_foo')

    LLD = lltype.GcStruct('D', ('dd', lltype.Signed))
    descr_D = cpu.sizeof(LLD)
    fielddescr_D = cpu.fielddescrof(LLD, 'dd')

    ARRAY = lltype.GcArray(lltype.Signed)
    arraydescr = cpu.arraydescrof(ARRAY)

    loop1 = parse("""
    [p0]
    guard_class(p0, ConstInt(vtable_B), descr=faildescr) []
    finish(descr=finaldescr)
    """,
                  namespace={
                      'finaldescr': finaldescr,
                      'faildescr': faildescr,
                      'vtable_B': vtable_B
                  })

    loop1a = parse("""
    [p0]
    guard_nonnull_class(p0, ConstInt(vtable_B), descr=faildescr) []
    finish(descr=finaldescr)
    """,
                   namespace={
                       'finaldescr': finaldescr,
                       'faildescr': faildescr,
                       'vtable_B': vtable_B
                   })

    loop2 = parse("""
    [p0]
    guard_gc_type(p0, ConstInt(typeid_B), descr=faildescr) []
    finish(descr=finaldescr)
    """,
                  namespace={
                      'finaldescr': finaldescr,
                      'faildescr': faildescr,
                      'typeid_B': typeid_B
                  })

    loop3 = parse("""
    [p0]
    guard_is_object(p0, descr=faildescr) []
    finish(descr=finaldescr)
    """,
                  namespace={
                      'finaldescr': finaldescr,
                      'faildescr': faildescr
                  })

    loop4 = parse("""
    [p0]
    guard_subclass(p0, ConstInt(vtable_B), descr=faildescr) []
    finish(descr=finaldescr)
    """,
                  namespace={
                      'finaldescr': finaldescr,
                      'faildescr': faildescr,
                      'vtable_B': vtable_B
                  })

    def g():
        cpu.setup_once()
        token1 = JitCellToken()
        token1a = JitCellToken()
        token2 = JitCellToken()
        token3 = JitCellToken()
        token4 = JitCellToken()
        cpu.compile_loop(loop1.inputargs, loop1.operations, token1)
        cpu.compile_loop(loop1a.inputargs, loop1a.operations, token1a)
        cpu.compile_loop(loop2.inputargs, loop2.operations, token2)
        cpu.compile_loop(loop3.inputargs, loop3.operations, token3)
        cpu.compile_loop(loop4.inputargs, loop4.operations, token4)

        for token, p0 in [
            (token1, rffi.cast(llmemory.GCREF, A())),
            (token1, rffi.cast(llmemory.GCREF, B())),
            (token1, rffi.cast(llmemory.GCREF, C())),
            (token1a, rffi.cast(llmemory.GCREF, A())),
            (token1a, lltype.nullptr(llmemory.GCREF.TO)),
            (token1a, rffi.cast(llmemory.GCREF, B())),
            (token1a, rffi.cast(llmemory.GCREF, C())),
            (token2, rffi.cast(llmemory.GCREF, A())),
            (token2, rffi.cast(llmemory.GCREF, B())),
            (token2, rffi.cast(llmemory.GCREF, C())),
            (token2, rffi.cast(llmemory.GCREF, [42, 43])),
            (token3, rffi.cast(llmemory.GCREF, A())),
            (token3, rffi.cast(llmemory.GCREF, B())),
            (token3, rffi.cast(llmemory.GCREF, [44, 45])),
            (token4, rffi.cast(llmemory.GCREF, A())),
            (token4, rffi.cast(llmemory.GCREF, B())),
            (token4, rffi.cast(llmemory.GCREF, C())),
        ]:
            frame = execute_token(token, p0)
            descr = cpu.get_latest_descr(frame)
            if descr is finaldescr:
                print 'match'
            elif descr is faildescr:
                print 'fail'
            else:
                print '???'
            #
            if token is token2:  # guard_gc_type
                print int(cpu.get_actual_typeid(p0) == typeid_B)
            if token is token3:  # guard_is_object
                print int(cpu.check_is_object(p0))

        for p0 in [
                lltype.nullptr(llmemory.GCREF.TO),
                rffi.cast(llmemory.GCREF, A()),
                rffi.cast(llmemory.GCREF, B()),
                rffi.cast(llmemory.GCREF, C()),
                rffi.cast(llmemory.GCREF, lltype.malloc(LLD)),
                rffi.cast(llmemory.GCREF, lltype.malloc(ARRAY, 5)),
                rffi.cast(llmemory.GCREF, "foobar"),
                rffi.cast(llmemory.GCREF, u"foobaz")
        ]:
            results = ['B', 'D', 'A', 'S', 'U']
            try:
                cpu.protect_speculative_field(p0, fielddescr_B)
            except SpeculativeError:
                results[0] = '-'
            try:
                cpu.protect_speculative_field(p0, fielddescr_D)
            except SpeculativeError:
                results[1] = '-'
            try:
                cpu.protect_speculative_array(p0, arraydescr)
            except SpeculativeError:
                results[2] = '-'
            try:
                cpu.protect_speculative_string(p0)
            except SpeculativeError:
                results[3] = '-'
            try:
                cpu.protect_speculative_unicode(p0)
            except SpeculativeError:
                results[4] = '-'
            print ''.join(results)

    call_initial_function(t, g)

    cbuilder = genc.CStandaloneBuilder(t, main, t.config)
    cbuilder.generate_source(defines=cbuilder.DEBUG_DEFINES)
    cbuilder.compile()

    data = cbuilder.cmdexec('')
    assert data == (
        'fail\n'
        'match\n'
        'fail\n'
        'fail\n'
        'fail\n'
        'match\n'
        'fail\n'
        'fail\n'
        '0\n'
        'match\n'
        '1\n'
        'fail\n'
        '0\n'
        'fail\n'
        '0\n'
        'match\n'
        '1\n'
        'match\n'
        '1\n'
        'fail\n'
        '0\n'
        'fail\n'
        'match\n'
        'match\n'
        '-----\n'  # null
        '-----\n'  # instance of A
        'B----\n'  # instance of B
        'B----\n'  # instance of C
        '-D---\n'
        '--A--\n'
        '---S-\n'
        '----U\n')
Exemplo n.º 30
0
    def _setup_repr(self, llfields=None, hints=None, adtmeths=None):
        # NOTE: don't store mutable objects like the dicts below on 'self'
        #       before they are fully built, to avoid strange bugs in case
        #       of recursion where other code would uses these
        #       partially-initialized dicts.
        AbstractInstanceRepr._setup_repr(self)
        self.rclass = getclassrepr(self.rtyper, self.classdef)
        fields = {}
        allinstancefields = {}
        if self.classdef is None:
            fields['__class__'] = 'typeptr', get_type_repr(self.rtyper)
        else:
            # instance attributes
            attrs = self.classdef.attrs.items()
            attrs.sort()
            myllfields = []
            for name, attrdef in attrs:
                if not attrdef.readonly:
                    r = self.rtyper.getrepr(attrdef.s_value)
                    mangled_name = 'inst_' + name
                    fields[name] = mangled_name, r
                    myllfields.append((mangled_name, r.lowleveltype))

            # Sort the instance attributes by decreasing "likely size",
            # as reported by rffi.sizeof(), to minimize padding holes in C.
            # Fields of the same size are sorted by name (by attrs.sort()
            # above) just to minimize randomness.
            def keysize((_, T)):
                if T is lltype.Void:
                    return None
                from rpython.rtyper.lltypesystem.rffi import sizeof
                try:
                    return -sizeof(T)
                except StandardError:
                    return None

            myllfields.sort(key=keysize)
            if llfields is None:
                llfields = myllfields
            else:
                llfields = llfields + myllfields

            self.rbase = getinstancerepr(self.rtyper, self.classdef.basedef,
                                         self.gcflavor)
            self.rbase.setup()

            MkStruct = lltype.STRUCT_BY_FLAVOR[LLFLAVOR[self.gcflavor]]
            if adtmeths is None:
                adtmeths = {}
            if hints is None:
                hints = {}
            hints = self._check_for_immutable_hints(hints)
            kwds = {}
            if self.gcflavor == 'gc':
                kwds['rtti'] = True

            for name, attrdef in attrs:
                if not attrdef.readonly and self.is_quasi_immutable(name):
                    llfields.append(('mutate_' + name, OBJECTPTR))

            object_type = MkStruct(self.classdef.name,
                                   ('super', self.rbase.object_type),
                                   hints=hints,
                                   adtmeths=adtmeths,
                                   *llfields,
                                   **kwds)
            self.object_type.become(object_type)
            allinstancefields.update(self.rbase.allinstancefields)
        allinstancefields.update(fields)
        self.fields = fields
        self.allinstancefields = allinstancefields
Exemplo n.º 31
0
Arquivo: rclass.py Projeto: sota/pypy
 def new_instance(self, llops, classcallhop=None):
     """Build a new instance, without calling __init__."""
     classrepr = getclassrepr(self.rtyper, self.classdef) 
     v_instance =  llops.genop("new",
         [inputconst(ootype.Void, self.lowleveltype)], self.lowleveltype)
     return v_instance
Exemplo n.º 32
0
Arquivo: rclass.py Projeto: sota/pypy
    def _setup_repr(self, llfields=None, hints=None):
        if hints:
            self.lowleveltype._hints.update(hints)

        if self.classdef is None:
            self.fields = {}
            self.allfields = {}
            self.allmethods = {}
            self.allclassattributes = {}
            self.classattributes = {}
            return

        if self.baserepr is not None:
            allfields = self.baserepr.allfields.copy()
            allmethods = self.baserepr.allmethods.copy()
            allclassattributes = self.baserepr.allclassattributes.copy()
        else:
            allfields = {}
            allmethods = {}
            allclassattributes = {}

        fields = {}
        nonmangledfields = []
        fielddefaults = {}

        if llfields:
            fields.update(dict(llfields))
        
        selfattrs = self.classdef.attrs

        for name, attrdef in selfattrs.iteritems():
            mangled = mangle(name, self.rtyper.getconfig())
            if not attrdef.readonly:
                repr = self.rtyper.getrepr(attrdef.s_value)
                allfields[mangled] = repr
                oot = repr.lowleveltype
                fields[mangled] = oot
                nonmangledfields.append(name)
                try:
                    value = self.classdef.classdesc.read_attribute(name)
                    fielddefaults[mangled] = repr.convert_desc_or_const(value)
                except AttributeError:
                    pass
            else:
                s_value = attrdef.s_value
                if isinstance(s_value, annmodel.SomePBC):
                    if len(s_value.descriptions) > 0 and s_value.getKind() == description.MethodDesc:
                        # attrdef is for a method
                        if mangled in allclassattributes:
                            raise TyperError("method overrides class attribute")
                        allmethods[mangled] = name, self.classdef.lookup_filter(s_value)
                        continue
                # class attribute
                if mangled in allmethods:
                    raise TyperError("class attribute overrides method")
                allclassattributes[mangled] = name, s_value

        special_methods = ["__init__", "__del__"]
        for meth_name in special_methods:
            if meth_name not in selfattrs and \
                    self.classdef.classdesc.find_source_for(meth_name) is not None:
                s_meth = self.classdef.classdesc.s_get_value(self.classdef,
                        meth_name)
                if isinstance(s_meth, annmodel.SomePBC):
                    mangled = mangle(meth_name, self.rtyper.getconfig())
                    allmethods[mangled] = meth_name, s_meth
                # else: it's the __init__ of a builtin exception

        ootype.addFields(self.lowleveltype, fields)

        self.rbase = getinstancerepr(self.rtyper, self.classdef.basedef)
        self.rbase.setup()

        for name, attrdef in selfattrs.iteritems():
            if not attrdef.readonly and self.is_quasi_immutable(name):
                name = mangle('mutable_' + name, self.rtyper.getconfig())
                ootype.addFields(self.lowleveltype, {name: OBJECT})

        classattributes = {}
        baseInstance = self.lowleveltype._superclass
        classrepr = getclassrepr(self.rtyper, self.classdef)

        # if this class has a corresponding metaclass, attach
        # a getmeta() method to get the corresponding meta_instance
        if classrepr.lowleveltype != ootype.Class:
            oovalue = classrepr.get_meta_instance()
            self.attach_class_attr_accessor('getmeta', oovalue)

                                        

        for classdef in self.classdef.getmro():
            for name, attrdef in classdef.attrs.iteritems():
                if not attrdef.readonly:
                    continue
                mangled = mangle(name, self.rtyper.getconfig())
                if mangled in allclassattributes:
                    selfdesc = self.classdef.classdesc
                    if name not in selfattrs:
                        # if the attr was already found in a parent class,
                        # we register it again only if it is overridden.
                        if selfdesc.find_source_for(name) is None:
                            continue
                        value = selfdesc.read_attribute(name)
                    else:
                        # otherwise, for new attrs, we look in all parent
                        # classes to see if it's defined in a parent but only
                        # actually first used in self.classdef.
                        value = selfdesc.read_attribute(name, None)

                    # a non-method class attribute
                    if not attrdef.s_value.is_constant():
                        classattributes[mangled] = attrdef.s_value, value

        self.fields = nonmangledfields
        self.allfields = allfields
        self.allmethods = allmethods
        self.allclassattributes = allclassattributes
        self.classattributes = classattributes
        # the following is done after the rest of the initialization because
        # convert_const can require 'self' to be fully initialized.

        # step 2: provide default values for fields
        for mangled, impl in fielddefaults.items():
            oot = fields[mangled]
            ootype.addFields(self.lowleveltype, {mangled: (oot, impl)},
                             with_default=True)
Exemplo n.º 33
0
 def finish(self, rtyper):
     bk = rtyper.annotator.bookkeeper
     for cls in self.standardexceptions:
         classdef = bk.getuniqueclassdef(cls)
         getclassrepr(rtyper, classdef).setup()
def run_guards_translated(gcremovetypeptr):
    class A(object):
        pass
    class B(A):
        pass
    class C(B):
        pass
    def main(argv):
        A(); B(); C()
        return 0

    t = TranslationContext()
    t.config.translation.gc = "minimark"
    t.config.translation.gcremovetypeptr = gcremovetypeptr
    ann = t.buildannotator()
    ann.build_types(main, [s_list_of_strings], main_entry_point=True)
    rtyper = t.buildrtyper()
    rtyper.specialize()

    classdef = ann.bookkeeper.getuniqueclassdef(B)
    rclass = getclassrepr(rtyper, classdef)
    rinstance = getinstancerepr(rtyper, classdef)
    LLB = rinstance.lowleveltype.TO
    vtable_B = rclass.getvtable()
    adr_vtable_B = llmemory.cast_ptr_to_adr(vtable_B)
    vtable_B = llmemory.cast_adr_to_int(adr_vtable_B, mode="symbolic")

    CPU = getcpuclass()
    cpu = CPU(rtyper, NoStats(),
              translate_support_code=True,
              gcdescr=get_description(t.config))
    execute_token = cpu.make_execute_token(llmemory.GCREF)
    finaldescr = BasicFinalDescr()
    faildescr = BasicFailDescr()

    descr_B = cpu.sizeof(LLB, vtable_B)
    typeid_B = descr_B.get_type_id()

    loop1 = parse("""
    [p0]
    guard_class(p0, ConstInt(vtable_B), descr=faildescr) []
    finish(descr=finaldescr)
    """, namespace={'finaldescr': finaldescr,
                    'faildescr': faildescr,
                    'vtable_B': vtable_B})

    loop2 = parse("""
    [p0]
    guard_gc_type(p0, ConstInt(typeid_B), descr=faildescr) []
    finish(descr=finaldescr)
    """, namespace={'finaldescr': finaldescr,
                    'faildescr': faildescr,
                    'typeid_B': typeid_B})

    loop3 = parse("""
    [p0]
    guard_is_object(p0, descr=faildescr) []
    finish(descr=finaldescr)
    """, namespace={'finaldescr': finaldescr,
                    'faildescr': faildescr})

    loop4 = parse("""
    [p0]
    guard_subclass(p0, ConstInt(vtable_B), descr=faildescr) []
    finish(descr=finaldescr)
    """, namespace={'finaldescr': finaldescr,
                    'faildescr': faildescr,
                    'vtable_B': vtable_B})

    def g():
        cpu.setup_once()
        token1 = JitCellToken()
        token2 = JitCellToken()
        token3 = JitCellToken()
        token4 = JitCellToken()
        cpu.compile_loop(loop1.inputargs, loop1.operations, token1)
        cpu.compile_loop(loop2.inputargs, loop2.operations, token2)
        cpu.compile_loop(loop3.inputargs, loop3.operations, token3)
        cpu.compile_loop(loop4.inputargs, loop4.operations, token4)

        for token, p0 in [
                (token1, rffi.cast(llmemory.GCREF, A())),
                (token1, rffi.cast(llmemory.GCREF, B())),
                (token1, rffi.cast(llmemory.GCREF, C())),

                (token2, rffi.cast(llmemory.GCREF, A())),
                (token2, rffi.cast(llmemory.GCREF, B())),
                (token2, rffi.cast(llmemory.GCREF, C())),
                (token2, rffi.cast(llmemory.GCREF, [42, 43])),

                (token3, rffi.cast(llmemory.GCREF, A())),
                (token3, rffi.cast(llmemory.GCREF, B())),
                (token3, rffi.cast(llmemory.GCREF, [44, 45])),

                (token4, rffi.cast(llmemory.GCREF, A())),
                (token4, rffi.cast(llmemory.GCREF, B())),
                (token4, rffi.cast(llmemory.GCREF, C())),
                ]:
            frame = execute_token(token, p0)
            descr = cpu.get_latest_descr(frame)
            if descr is finaldescr:
                print 'match'
            elif descr is faildescr:
                print 'fail'
            else:
                print '???'
            #
            if token is token2:    # guard_gc_type
                print int(cpu.get_actual_typeid(p0) == typeid_B)
            if token is token3:    # guard_is_object
                print int(cpu.check_is_object(p0))

    call_initial_function(t, g)

    cbuilder = genc.CStandaloneBuilder(t, main, t.config)
    cbuilder.generate_source(defines=cbuilder.DEBUG_DEFINES)
    cbuilder.compile()
    
    data = cbuilder.cmdexec('')
    assert data == ('fail\n'
                    'match\n'
                    'fail\n'

                    'fail\n'  '0\n'
                    'match\n' '1\n'
                    'fail\n'  '0\n'
                    'fail\n'  '0\n'

                    'match\n' '1\n'
                    'match\n' '1\n'
                    'fail\n'  '0\n'

                    'fail\n'
                    'match\n'
                    'match\n')