Exemple #1
0
    def test_subclassof(self):
        A = ootype.Instance("A", ootype.ROOT)
        B = ootype.Instance("B", A)
        clsA = ootype.runtimeClass(A)
        clsB = ootype.runtimeClass(B)
        myjitdriver = JitDriver(greens=[], reds=['n', 'flag', 'res'])

        def getcls(flag):
            if flag:
                return clsA
            else:
                return clsB

        def f(flag, n):
            res = True
            while n > -100:
                myjitdriver.can_enter_jit(n=n, flag=flag, res=res)
                myjitdriver.jit_merge_point(n=n, flag=flag, res=res)
                cls = getcls(flag)
                n -= 1
                res = ootype.subclassof(cls, clsB)
            return res

        res = self.meta_interp(f, [1, 100],
                               policy=StopAtXPolicy(getcls),
                               optimizer=OPTIMIZER_SIMPLE)
        assert not res

        res = self.meta_interp(f, [0, 100],
                               policy=StopAtXPolicy(getcls),
                               optimizer=OPTIMIZER_SIMPLE)
        assert res
Exemple #2
0
def test_fielddescr_ootype():
    from pypy.rpython.ootypesystem import ootype
    from pypy.jit.backend.cli.runner import CliCPU
    A = ootype.Instance("A", ootype.ROOT, {"foo": ootype.Signed})
    B = ootype.Instance("B", A)
    descr1 = CliCPU.fielddescrof(A, "foo")
    descr2 = CliCPU.fielddescrof(B, "foo")
    assert descr1 is descr2
Exemple #3
0
def test_fielddescr_ootype():
    from pypy.rpython.ootypesystem import ootype
    from pypy.jit.backend.llgraph.runner import OOtypeCPU
    A = ootype.Instance("A", ootype.ROOT, {"foo": ootype.Signed})
    B = ootype.Instance("B", A)
    cpu = OOtypeCPU(None)
    descr1 = cpu.fielddescrof(A, "foo")
    descr2 = cpu.fielddescrof(B, "foo")
    assert descr1 is descr2
Exemple #4
0
    def test_oostring_instance(self):
        A = ootype.Instance("A", ootype.ROOT)
        B = ootype.Instance("B", ootype.ROOT)

        def f(n):
            obj1 = ootype.new(A)
            obj2 = ootype.new(B)
            s1 = ootype.oostring(obj1, -1)
            s2 = ootype.oostring(obj2, -1)
            ch1 = s1.ll_stritem_nonneg(1)
            ch2 = s2.ll_stritem_nonneg(1)
            return ord(ch1) + ord(ch2)

        res = self.interp_operations(f, [0])
        assert res == ord('A') + ord('B')
Exemple #5
0
    def __init__(self, rtyper, classdef, gcflavor='ignored'):
        AbstractInstanceRepr.__init__(self, rtyper, classdef)

        self.baserepr = None
        if self.classdef is None:
            self.lowleveltype = OBJECT
        else:
            b = self.classdef.basedef
            if b is not None:
                self.baserepr = getinstancerepr(rtyper, b)
                b = self.baserepr.lowleveltype
            else:
                b = OBJECT

            if hasattr(classdef.classdesc.pyobj, '_rpython_hints'):
                hints = classdef.classdesc.pyobj._rpython_hints
            else:
                hints = {}
            hints = self._check_for_immutable_hints(hints)
            self.lowleveltype = ootype.Instance(classdef.name,
                                                b, {}, {},
                                                _hints=hints)
        self.iprebuiltinstances = identity_dict()
        self.object_type = self.lowleveltype
        self.gcflavor = gcflavor
Exemple #6
0
 def __init__(self, rtyper, access_set):
     self.rtyper = rtyper
     self.access_set = access_set
     self.lowleveltype = ootype.Instance('pbc',
                                         PBCROOT,
                                         _hints={'immutable': True})
     self.pbc_cache = {}
Exemple #7
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)
Exemple #8
0
def test_oohelper():
    S = ootype.Instance('S', ootype.ROOT, {'x': Signed, 'y': Signed})

    def f(s, z):
        #assert we_are_translated()
        return s.x * s.y + z

    def g(s):
        #assert we_are_translated()
        return s.x + s.y

    F = ootype.StaticMethod([S, Signed], Signed)
    G = ootype.StaticMethod([S], Signed)

    def h(x, y, z):
        s = ootype.new(S)
        s.x = x
        s.y = y
        fsm = llhelper(F, f)
        gsm = llhelper(G, g)
        assert typeOf(fsm) == F
        return fsm(s, z) + fsm(s, z * 2) + gsm(s)

    res = h(8, 5, 2)
    assert res == 99
    res = interpret(h, [8, 5, 2], type_system='ootype')
    assert res == 99
Exemple #9
0
    def test_oononnull(self):
        FOO = ootype.Instance('Foo', ootype.ROOT)

        def fn():
            s = ootype.new(FOO)
            return bool(s)

        self.check(fn, [], [], True)
Exemple #10
0
def test_compare_classes():
    A = ootype.Instance("A", ootype.ROOT)
    B = ootype.Instance("B", ootype.ROOT)

    cls1 = ootype.runtimeClass(A)

    def fn(n):
        if n:
            cls2 = ootype.runtimeClass(A)
        else:
            cls2 = ootype.runtimeClass(B)

        assert (cls1 == cls2) == (not (cls1 != cls2))
        return cls1 == cls2

    res = interpret(fn, [1], type_system='ootype')
    assert res
Exemple #11
0
 def test_box_unbox_ooinstance_fail(self):
     A = ootype.Instance('A', ootype.ROOT, {'xx': ootype.Signed})
     def fn(flag):
         b_obj = System.Object()
         a2 = unbox(b_obj, A)
         return a2
     res = self.interpret(fn, [True])
     assert res is None
Exemple #12
0
def test_filter_out_instance_with_void():
    effects = frozenset([("struct",
                          ootype.Instance("x", ootype.ROOT,
                                          {"a": ootype.Void}), "a")])
    effectinfo = effectinfo_from_writeanalyze(effects, None)
    assert not effectinfo.readonly_descrs_fields
    assert not effectinfo.write_descrs_fields
    assert not effectinfo.write_descrs_arrays
Exemple #13
0
 def test_box_unbox_ooinstance(self):
     A = ootype.Instance('A', ootype.ROOT, {'xx': ootype.Signed})
     def fn(flag):
         a = ootype.new(A)
         a.xx = 42
         b_obj = box(a)
         a2 = unbox(b_obj, A)
         return a2.xx
     res = self.interpret(fn, [True])
     assert res == 42
Exemple #14
0
    def test_identityhash(self):
        A = ootype.Instance("A", ootype.ROOT)

        def f():
            obj1 = ootype.new(A)
            obj2 = ootype.new(A)
            return ootype.identityhash(obj1) == ootype.identityhash(obj2)

        assert not f()
        res = self.interp_operations(f, [])
        assert not res
Exemple #15
0
    def __init__(self, rtyper, classdef):
        AbstractClassRepr.__init__(self, rtyper, classdef)

        if self.classdef is not None:
            self.rbase = getclassrepr(self.rtyper, self.classdef.basedef)
            base_type = self.rbase.lowleveltype
            self.lowleveltype = ootype.Instance(self.classdef.name + "_meta",
                                                base_type)
        else:
            # we are ROOT
            self.lowleveltype = CLASSTYPE
Exemple #16
0
    def test_cast_object_instance(self):
        A = ootype.Instance("Foo", ootype.ROOT)

        def fn_instance():
            a = ootype.new(A)
            obj = ootype.cast_to_object(a)
            a2 = ootype.cast_from_object(A, obj)
            a3 = ootype.cast_from_object(ootype.ROOT, obj)
            assert a is a2
            assert a is a3

        self.interpret(fn_instance, [])
Exemple #17
0
    def test_cast_object_mix_null(self):
        A = ootype.Instance("Foo", ootype.ROOT)

        def fn_mix_null(flag):
            a = ootype.new(A)
            obj = ootype.cast_to_object(a)
            if flag:
                return obj
            else:
                return ootype.NULL

        res = self.interpret(fn_mix_null, [False])
        assert res is ootype.NULL
Exemple #18
0
    def test_cast_object_is_true(self):
        A = ootype.Instance("Foo", ootype.ROOT)

        def fn_is_true(flag):
            if flag:
                a = ootype.new(A)
            else:
                a = ootype.null(A)
            obj = ootype.cast_to_object(a)
            return bool(obj)

        assert self.interpret(fn_is_true, [True]) is True
        assert self.interpret(fn_is_true, [False]) is False
Exemple #19
0
    def test_cast_object_null(self):
        A = ootype.Instance("Foo", ootype.ROOT)
        B = ootype.Record({'x': ootype.Signed})

        def fn_null():
            a = ootype.null(A)
            b = ootype.null(B)
            obj1 = ootype.cast_to_object(a)
            obj2 = ootype.cast_to_object(b)
            assert obj1 == obj2
            assert ootype.cast_from_object(A, obj1) == a
            assert ootype.cast_from_object(B, obj2) == b

        self.interpret(fn_null, [])
Exemple #20
0
 def test_fieldinfo_for_const_pbc(self):
     A = ootype.Instance('A', ootype.ROOT, {'xx': ootype.Signed})
     const = ootype.new(A)
     fieldinfo = fieldinfo_for_const(const)
     def fn():
         const.xx = 42
         obj = fieldinfo.GetValue(None)
         # get the 'xx' field by using reflection
         t = obj.GetType()
         x_info = t.GetField('xx')
         x_value = x_info.GetValue(obj)
         return unbox(x_value, ootype.Signed)
     res = self.interpret(fn, [])
     assert res == 42
Exemple #21
0
 def test_cast_native_object(self):
     A = ootype.Instance("A", ootype.ROOT, {})
     def fn():
         a = ootype.new(A)
         ahash = ootype.identityhash(a)
         obj = ootype.cast_to_object(a)
         native = cast_to_native_object(obj)
         name = native.GetType().get_Name()
         obj2 = cast_from_native_object(native)
         a2 = ootype.cast_from_object(A, obj2)
         a2hash = ootype.identityhash(a2)
         return name, ahash == a2hash
     res = self.ll_to_tuple(self.interpret(fn, []))
     assert res == ('A', True)
Exemple #22
0
    def test_oois(self):
        A = ootype.Instance("A", ootype.ROOT)

        def f(n):
            obj1 = ootype.new(A)
            if n:
                obj2 = obj1
            else:
                obj2 = ootype.new(A)
            return obj1 is obj2

        res = self.interp_operations(f, [0])
        assert not res
        res = self.interp_operations(f, [1])
        assert res
Exemple #23
0
    def test_unwrap_object(self):
        A = ootype.Instance("A", ootype.ROOT, {})
        a1 = ootype.new(A)
        a2 = ootype.new(A)
        obj1 = ootype.cast_to_object(a1)
        obj2 = ootype.cast_to_object(a2)

        def fn(flag):
            if flag:
                obj = obj1
            else:
                obj = obj2
            a3 = ootype.cast_from_object(A, obj)
            return a3 is a1

        res = self.interpret(fn, [True], backendopt=False)
        assert res is True
Exemple #24
0
    def __init__(self, rtyper, classdef, gcflavor='ignored'):
        AbstractInstanceRepr.__init__(self, rtyper, classdef)

        self.baserepr = None
        if self.classdef is None:
            self.lowleveltype = OBJECT
        else:
            b = self.classdef.basedef
            if b is not None:
                self.baserepr = getinstancerepr(rtyper, b)
                b = self.baserepr.lowleveltype
            else:
                b = OBJECT

            if hasattr(classdef.classdesc.pyobj, '_rpython_hints'):
                hints = classdef.classdesc.pyobj._rpython_hints
            else:
                hints = {}
            self.lowleveltype = ootype.Instance(classdef.name,
                                                b, {}, {},
                                                _hints=hints)
        self.prebuiltinstances = {}  # { id(x): (x, _ptr) }
        self.object_type = self.lowleveltype
        self.gcflavor = gcflavor
Exemple #25
0
class OOtypeMixin(object):
    type_system = 'ootype'

    def get_class_of_box(self, box):
        root = box.getref(ootype.ROOT)
        return ootype.classof(root)
    
    cpu = runner.OOtypeCPU(None)
    NODE = ootype.Instance('NODE', ootype.ROOT, {})
    NODE._add_fields({'value': ootype.Signed,
                      'floatval' : ootype.Float,
                      'next': NODE})
    NODE2 = ootype.Instance('NODE2', NODE, {'other': NODE})

    node_vtable = ootype.runtimeClass(NODE)
    node_vtable_adr = ootype.cast_to_object(node_vtable)
    node_vtable2 = ootype.runtimeClass(NODE2)
    node_vtable_adr2 = ootype.cast_to_object(node_vtable2)

    node = ootype.new(NODE)
    nodebox = BoxObj(ootype.cast_to_object(node))
    myptr = nodebox.value
    myptr2 = ootype.cast_to_object(ootype.new(NODE))
    nodebox2 = BoxObj(ootype.cast_to_object(node))
    valuedescr = cpu.fielddescrof(NODE, 'value')
    floatdescr = cpu.fielddescrof(NODE, 'floatval')
    nextdescr = cpu.fielddescrof(NODE, 'next')
    otherdescr = cpu.fielddescrof(NODE2, 'other')
    nodesize = cpu.typedescrof(NODE)
    nodesize2 = cpu.typedescrof(NODE2)

    arraydescr = cpu.arraydescrof(ootype.Array(ootype.Signed))
    floatarraydescr = cpu.arraydescrof(ootype.Array(ootype.Float))

    # a plain Record
    S = ootype.Record({'a': ootype.Signed, 'b': NODE})
    ssize = cpu.typedescrof(S)
    adescr = cpu.fielddescrof(S, 'a')
    bdescr = cpu.fielddescrof(S, 'b')
    sbox = BoxObj(ootype.cast_to_object(ootype.new(S)))
    arraydescr2 = cpu.arraydescrof(ootype.Array(S))

    T = ootype.Record({'c': ootype.Signed,
                       'd': ootype.Array(NODE)})
    tsize = cpu.typedescrof(T)
    cdescr = cpu.fielddescrof(T, 'c')
    ddescr = cpu.fielddescrof(T, 'd')
    arraydescr3 = cpu.arraydescrof(ootype.Array(NODE))

    U = ootype.Instance('U', ootype.ROOT, {'one': ootype.Array(NODE)})
    usize = cpu.typedescrof(U)
    onedescr = cpu.fielddescrof(U, 'one')
    u_vtable = ootype.runtimeClass(U)
    u_vtable_adr = ootype.cast_to_object(u_vtable)

    # force a consistent order
    valuedescr.sort_key()
    nextdescr.sort_key()
    adescr.sort_key()
    bdescr.sort_key()

    FUNC = lltype.FuncType([lltype.Signed], lltype.Signed)
    nonwritedescr = cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT) # XXX fix ootype

    cpu.class_sizes = {node_vtable_adr: cpu.typedescrof(NODE),
                       node_vtable_adr2: cpu.typedescrof(NODE2),
                       u_vtable_adr: cpu.typedescrof(U)}
    namespace = locals()
Exemple #26
0
from pypy.rpython.rslice import AbstractSliceRepr
from pypy.rpython.lltypesystem.lltype import Void, Signed
from pypy.rpython.ootypesystem import ootype

SLICE = ootype.Instance('Slice', ootype.ROOT, {
    'start': Signed,
    'stop': Signed
})


class SliceRepr(AbstractSliceRepr):
    pass


startstop_slice_repr = SliceRepr()
startstop_slice_repr.lowleveltype = SLICE
startonly_slice_repr = SliceRepr()
startonly_slice_repr.lowleveltype = Signed
minusone_slice_repr = SliceRepr()
minusone_slice_repr.lowleveltype = Void  # only for [:-1]

# ____________________________________________________________


def ll_newslice(start, stop):
    s = ootype.new(SLICE)
    s.start = start
    s.stop = stop
    return s
Exemple #27
0
 def _get_NODE(self):
     NODE = ootype.Instance('NODE', ootype.ROOT, {})
     NODE._add_fields({'value': ootype.Signed, 'next': NODE})
     return NODE
Exemple #28
0
 def GcStruct(name, *fields, **kwds):
     if 'hints' in kwds:
         kwds['_hints'] = kwds['hints']
         del kwds['hints']
     I = ootype.Instance(name, ootype.ROOT, dict(fields), **kwds)
     return I
Exemple #29
0
    def _get_method_name(self, opname, s_pbc, args_s, hop):
        shape, index, callfamily = self._get_shape_index_callfamily(opname, s_pbc, args_s, hop)
        mangled = mangle(self.methodname, self.rtyper.getconfig())
        row = self.concretetable[shape, index]
        derived_mangled = row_method_name(mangled, row.attrname)
        return derived_mangled

class __extend__(pairtype(InstanceRepr, MethodsPBCRepr)):

    def convert_from_to(_, v, llops):
        return v

# ____________________________________________________________

PBCROOT = ootype.Instance('pbcroot', ootype.ROOT)

class MultipleFrozenPBCRepr(AbstractMultipleFrozenPBCRepr):
    """Representation selected for multiple non-callable pre-built constants."""
    def __init__(self, rtyper, access_set):
        self.rtyper = rtyper
        self.access_set = access_set
        self.lowleveltype = ootype.Instance('pbc', PBCROOT, _hints={'immutable': True})
        self.pbc_cache = {}

    def _setup_repr(self):
        fields_list = self._setup_repr_fields()
        ootype.addFields(self.lowleveltype, dict(fields_list))

    def create_instance(self):
        return ootype.new(self.lowleveltype)
Exemple #30
0
 def setup_specfunc(self):
     fields = {}
     for row in self.uniquerows:
         fields[row.attrname] = row.fntype
     return ootype.Instance('specfunc', ootype.ROOT, fields)