Source

pypy / pypy / rpython / lltypesystem / rtagged.py

from pypy.objspace.flow.model import Constant
from pypy.rpython.rclass import getclassrepr, getinstancerepr, get_type_repr
from pypy.rpython.lltypesystem import lltype
from pypy.rpython.lltypesystem.rclass import InstanceRepr, CLASSTYPE, ll_inst_type
from pypy.rpython.lltypesystem.rclass import MissingRTypeAttribute
from pypy.rpython.lltypesystem.rclass import ll_issubclass_const
from pypy.rpython.lltypesystem.lloperation import llop
from pypy.rpython.rmodel import TyperError, inputconst


class TaggedInstanceRepr(InstanceRepr):

    def __init__(self, rtyper, classdef, unboxedclassdef):
        InstanceRepr.__init__(self, rtyper, classdef)
        self.unboxedclassdef = unboxedclassdef
        self.is_parent = unboxedclassdef is not classdef

    def _setup_repr(self):
        InstanceRepr._setup_repr(self)
        flds = self.allinstancefields.keys()
        flds.remove('__class__')
        if self.is_parent:
            if flds:
                raise TyperError("%r is a base class of an UnboxedValue,"
                                 "so it cannot have fields: %r" % (
                    self.classdef, flds))
        else:
            if len(flds) != 1:
                raise TyperError("%r must have exactly one field: %r" % (
                    self.classdef, flds))
            self.specialfieldname = flds[0]

    def new_instance(self, llops, classcallhop=None):
        if self.is_parent:
            raise TyperError("don't instantiate %r, it is a parent of an "
                             "UnboxedValue class" % (self.classdef,))
        if classcallhop is None:
            raise TyperError("must instantiate %r by calling the class" % (
                self.classdef,))
        hop = classcallhop
        if not (hop.spaceop.opname == 'simple_call' and hop.nb_args == 2):
            raise TyperError("must instantiate %r with a simple class call" % (
                self.classdef,))
        v_value = hop.inputarg(lltype.Signed, arg=1)
        c_one = hop.inputconst(lltype.Signed, 1)
        hop.exception_is_here()
        v2 = hop.genop('int_tag_ovf', [v_value],
                       resulttype = lltype.Signed)
        v_instance =  hop.genop('cast_int_to_ptr', [v2],
                                resulttype = self.lowleveltype)
        return v_instance, False   # don't call __init__

    def convert_const_exact(self, value):
        self.setup()
        number = value.get_untagged_value()
        tagged = number * 2 + 1
        return lltype.cast_int_to_ptr(self.lowleveltype, tagged)

    def getvalue_from_unboxed(self, llops, vinst):
        assert not self.is_parent
        v2 = llops.genop('cast_ptr_to_int', [vinst],  resulttype=lltype.Signed)
        return llops.genop('int_untag', [v2], resulttype=lltype.Signed)

    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

    def getfield(self, vinst, attr, llops, force_cast=False, flags={}):
        if not self.is_parent and attr == self.specialfieldname:
            return self.getvalue_from_unboxed(llops, vinst)
        elif attr == '__class__':
            return self.gettype_from_unboxed(llops, vinst)
        else:
            raise MissingRTypeAttribute(attr)

    def rtype_type(self, hop):
        [vinst] = hop.inputargs(self)
        return self.gettype_from_unboxed(
            hop.llops, vinst, can_be_none=hop.args_s[0].can_be_none())

    def rtype_setattr(self, hop):
        # only for UnboxedValue.__init__(), which is not actually called
        hop.genop('UnboxedValue_setattr', [])

    def ll_str(self, i):
        if lltype.cast_ptr_to_int(i) & 1:
            from pypy.rpython.lltypesystem import rstr
            from pypy.rpython.rint import signed_repr
            llstr1 = signed_repr.ll_str(ll_unboxed_to_int(i))
            return rstr.ll_strconcat(rstr.unboxed_instance_str_prefix,
                      rstr.ll_strconcat(llstr1,
                                        rstr.unboxed_instance_str_suffix))
        else:
            return InstanceRepr.ll_str(self, i)

    def rtype_isinstance(self, hop):
        if not hop.args_s[1].is_constant():
            raise TyperError("isinstance() too complicated")
        [classdesc] = hop.args_s[1].descriptions
        classdef = classdesc.getuniqueclassdef()

        class_repr = get_type_repr(self.rtyper)
        instance_repr = self.common_repr()
        v_obj, v_cls = hop.inputargs(instance_repr, class_repr)
        cls = v_cls.value
        answer = self.unboxedclassdef.issubclass(classdef)
        c_answer_if_unboxed = hop.inputconst(lltype.Bool, answer)
        minid = hop.inputconst(lltype.Signed, cls.subclassrange_min)
        maxid = hop.inputconst(lltype.Signed, cls.subclassrange_max)
        return hop.gendirectcall(ll_unboxed_isinstance_const, v_obj,
                                 minid, maxid, c_answer_if_unboxed)



def ll_unboxed_to_int(p):
    return llop.int_untag(lltype.Signed, lltype.cast_ptr_to_int(p))

def ll_unboxed_getclass_canbenone(instance, class_if_unboxed):
    if instance:
        return ll_unboxed_getclass(instance, class_if_unboxed)
    return lltype.nullptr(lltype.typeOf(instance).TO.typeptr.TO)

def ll_unboxed_getclass(instance, class_if_unboxed):
    if lltype.cast_ptr_to_int(instance) & 1:
        return class_if_unboxed
    return instance.typeptr

def ll_unboxed_isinstance_const(obj, minid, maxid, answer_if_unboxed):
    if not obj:
        return False
    if lltype.cast_ptr_to_int(obj) & 1:
        return answer_if_unboxed
    else:
        return ll_issubclass_const(obj.typeptr, minid, maxid)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.