[pypy-svn] r22823 - in pypy/dist/pypy/jit: . test

pedronis at codespeak.net pedronis at codespeak.net
Sun Jan 29 12:28:24 CET 2006


Author: pedronis
Date: Sun Jan 29 12:28:21 2006
New Revision: 22823

Modified:
   pypy/dist/pypy/jit/hintbookkeeper.py
   pypy/dist/pypy/jit/hintcontainer.py
   pypy/dist/pypy/jit/hintmodel.py
   pypy/dist/pypy/jit/test/test_hint_annotation.py
Log:
(arre, pedronis)

more ops. cast_pointer for virtual structs with simple test. added HintBookkeeper.current_op_concretetype



Modified: pypy/dist/pypy/jit/hintbookkeeper.py
==============================================================================
--- pypy/dist/pypy/jit/hintbookkeeper.py	(original)
+++ pypy/dist/pypy/jit/hintbookkeeper.py	Sun Jan 29 12:28:21 2006
@@ -39,6 +39,11 @@
         res.const = const.value
         return res
 
+    def current_op_concretetype(self):
+        _, block, i = self.position_key
+        op = block.operations[i]
+        return op.result.concretetype
+
     def getvirtualcontainerdef(self, TYPE):
         try:
             res = self.virtual_containers[self.position_key]

Modified: pypy/dist/pypy/jit/hintcontainer.py
==============================================================================
--- pypy/dist/pypy/jit/hintcontainer.py	(original)
+++ pypy/dist/pypy/jit/hintcontainer.py	Sun Jan 29 12:28:21 2006
@@ -3,21 +3,19 @@
 from pypy.jit import hintmodel
 from pypy.rpython.lltypesystem import lltype
 
-def virtualcontainerdef(bookkeeper, T):
+def virtualcontainerdef(bookkeeper, T, vparent=None):
     """Build and return a VirtualXxxDef() corresponding to a
     freshly allocated virtual container.
     """
     if isinstance(T, lltype.Struct):
-        cls = VirtualStructDef
+        return VirtualStructDef(bookkeeper, T, vparent)
     elif isinstance(T, lltype.Array):
-        cls = VirtualArrayDef
-    else:
-        raise TypeError("unsupported container type %r" % (T,))
-    return cls(bookkeeper, T)
+        return VirtualArrayDef(bookkeeper, T)
+    raise TypeError("unsupported container type %r" % (T,))
 
-def make_item_annotation(bookkeeper, TYPE):
+def make_item_annotation(bookkeeper, TYPE, vparent=None):
     if isinstance(TYPE, lltype.ContainerType):
-        vdef = virtualcontainerdef(bookkeeper, TYPE)
+        vdef = virtualcontainerdef(bookkeeper, TYPE, vparent=vparent)
         return hintmodel.SomeLLAbstractContainer(vdef)
     elif isinstance(TYPE, lltype.Ptr):
         return annmodel.s_ImpossibleValue
@@ -39,16 +37,31 @@
 
 class VirtualStructDef:
  
-    def __init__(self, bookkeeper, TYPE):
+    def __init__(self, bookkeeper, TYPE, vparent=None):
         self.T = TYPE
         self.bookkeeper = bookkeeper
         self.fields = {}
         self.names = TYPE._names
         for name in self.names:
             FIELD_TYPE = self.fieldtype(name)
-            hs = make_item_annotation(bookkeeper, FIELD_TYPE)
+            hs = make_item_annotation(bookkeeper, FIELD_TYPE, vparent=self)
             fv = self.fields[name] = FieldValue(bookkeeper, name, hs)
             fv.itemof[self] = True
+        self.vparent = vparent
+
+    def cast(self, TO):
+        down_or_up = lltype.castable(TO,
+                                     lltype.Ptr(self.T))
+        # the following works because if a structure is virtual, then
+        # all its parent and inlined substructures are also virtual
+        vstruct = self
+        if down_or_up >= 0:
+            for n in range(down_or_up):
+                vstruct = vstruct.read_field(vstruct.T._names[0]).contentdef
+        else:
+            for n in range(-down_or_up):
+                vstruct = vstruct.vparent
+        return vstruct
 
     def fieldtype(self, name):
         return getattr(self.T, name)

Modified: pypy/dist/pypy/jit/hintmodel.py
==============================================================================
--- pypy/dist/pypy/jit/hintmodel.py	(original)
+++ pypy/dist/pypy/jit/hintmodel.py	Sun Jan 29 12:28:21 2006
@@ -3,12 +3,13 @@
 from pypy.jit.hintbookkeeper import getbookkeeper
 from pypy.rpython.lltypesystem import lltype
 
-UNARY_OPERATIONS = """same_as hint getfield setfield getsubstruct getarraysize getarrayitem
+UNARY_OPERATIONS = """same_as hint getfield setfield getsubstruct getarraysize getarrayitem setarrayitem
+                      cast_pointer
                       direct_call
                       int_is_true int_neg
                       cast_char_to_int""".split()
 
-BINARY_OPERATIONS = """int_add int_sub int_mul int_and
+BINARY_OPERATIONS = """int_add int_sub int_mul int_and int_rshift
                        int_gt int_lt int_le int_ge int_eq int_ne""".split()
 
 class OriginTreeNode(object):
@@ -158,6 +159,11 @@
         origin = getbookkeeper().myorigin()
         return SomeLLAbstractConstant(lltype.Signed, {origin: True})
 
+    def cast_pointer(hs_s1):
+        TO = getbookkeeper().current_op_concretetype()
+        res_vstruct =hs_s1.contentdef.cast(TO)
+        return SomeLLAbstractContainer(res_vstruct)
+
 class __extend__(pairtype(SomeLLAbstractValue, SomeLLAbstractValue)):
 
     def int_add((hs_v1, hs_v2)):
@@ -181,7 +187,7 @@
         origin.merge(hs_c2.origins)
         return SomeLLAbstractConstant(lltype.Signed, {origin: True})
 
-    int_and = int_mul = int_sub = int_add
+    int_rshift = int_and = int_mul = int_sub = int_add
 
     def int_gt((hs_c1, hs_c2)):
         origin = getbookkeeper().myorigin()

Modified: pypy/dist/pypy/jit/test/test_hint_annotation.py
==============================================================================
--- pypy/dist/pypy/jit/test/test_hint_annotation.py	(original)
+++ pypy/dist/pypy/jit/test/test_hint_annotation.py	Sun Jan 29 12:28:21 2006
@@ -213,6 +213,25 @@
     assert hs.concretetype == lltype.Signed
     assert len(hs.origins) == 1
 
+def test_simple_cast_pointer():
+    GCS1 = lltype.GcStruct('s1', ('x', lltype.Signed))
+    GCS2 = lltype.GcStruct('s2', ('sub', GCS1), ('y', lltype.Signed))
+    PGCS1 = lltype.Ptr(GCS1)
+    PGCS2 = lltype.Ptr(GCS2)
+    def ll1():
+        s2 = lltype.malloc(GCS2)
+        return lltype.cast_pointer(PGCS1, s2)
+    hs = hannotate(ll1, [])
+    assert isinstance(hs, SomeLLAbstractContainer)
+    assert hs.concretetype == PGCS1
+    def ll1():
+        s2 = lltype.malloc(GCS2)
+        s1 = s2.sub
+        return lltype.cast_pointer(PGCS2, s1)
+    hs = hannotate(ll1, [])
+    assert isinstance(hs, SomeLLAbstractContainer)
+    assert hs.concretetype == PGCS2
+
 
 def CUR_GOAL_test_hannotate_tl():
     from pypy.jit import tl



More information about the Pypy-commit mailing list