[pypy-svn] r14231 - in pypy/dist/pypy/translator/llvm2: . test

rxe at codespeak.net rxe at codespeak.net
Mon Jul 4 18:05:17 CEST 2005


Author: rxe
Date: Mon Jul  4 18:05:16 2005
New Revision: 14231

Modified:
   pypy/dist/pypy/translator/llvm2/arraynode.py
   pypy/dist/pypy/translator/llvm2/database.py
   pypy/dist/pypy/translator/llvm2/structnode.py
   pypy/dist/pypy/translator/llvm2/test/test_genllvm.py
Log:
A first iteration at getting pbc to work.  Tests pass.

Clean up time.



Modified: pypy/dist/pypy/translator/llvm2/arraynode.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/arraynode.py	(original)
+++ pypy/dist/pypy/translator/llvm2/arraynode.py	Mon Jul  4 18:05:16 2005
@@ -2,11 +2,15 @@
 from pypy.rpython import lltype
 from pypy.translator.llvm2.log import log
 from pypy.translator.llvm2.node import LLVMNode
+from pypy.objspace.flow.model import Constant
 import itertools  
 log = log.structnode
 
 count = itertools.count().next 
 
+def wrapstr(s):
+    return '"%s"' % s
+
 class ArrayTypeNode(LLVMNode):
     _issetup = False
     def __init__(self, db, array):
@@ -14,12 +18,10 @@
         assert isinstance(array, lltype.Array)
         self.array = array
         c = count()
-        ref_template = "%%array.%s." + str(c)
-        ref_template = '"%s"' % ref_template
-        arrayname = str(self.array.OF)            
-        self.ref = ref_template % arrayname
-        constructor_ref = "%%new.array.%s" % c
-        self.constructor_ref = '"%s"' % constructor_ref
+        ref_template = wrapstr("%%array.%s." + str(c))
+
+        self.ref = ref_template % array.OF
+        self.constructor_ref = wrapstr("%%new.array.%s" % c)
         self.constructor_decl = "%s * %s(int %%len)" % \
                                 (self.ref, self.constructor_ref)
 
@@ -80,9 +82,8 @@
 
     def __init__(self, db, value):
         self.db = db
-        name = "%s.%s" % (value._TYPE.OF, ArrayNode.array_counter)
-        self.ref = "%%stinstance.%s" % name
-        self.dataref = self.ref + ".tmp" 
+        name = '"%%arrayinstance.%s.%s"' % (value._TYPE.OF, ArrayNode.array_counter)
+        self.ref = name
         self.value = value
         ArrayNode.array_counter += 1
 
@@ -93,14 +94,15 @@
         T = self.value._TYPE.OF
         for item in self.value.items:
             if not isinstance(T, lltype.Primitive):
-                value = getattr(self.value, name)
                 # Create a dummy constant hack XXX
-                c = Constant(value, T)
+                c = Constant(item, T)
                 self.db.prepare_arg(c)
 
         self._issetup = True
 
-    def get_values(self):
+    def getall(self):
+        arraylen = len(self.value.items)
+
         res = []
 
         T = self.value._TYPE.OF
@@ -113,14 +115,17 @@
                 value = repr(value)
             res.append((typval, value))
 
-        return ", ".join(["%s %s" % (t, v) for t, v in res])
+        arrayvalues = ", ".join(["%s %s" % (t, v) for t, v in res])
 
+        type_ = "{ int, [%s x %s] }" % (len(self.value.items),
+                                        self.db.repr_arg_type(self.value._TYPE.OF))
+        
+        value = "int %s, [%s x %s] [ %s ]" % (arraylen,
+                                              arraylen,
+                                              typval,
+                                              arrayvalues)
+        return type_, value
+    
     def writeglobalconstants(self, codewriter):
-        lenitems = len(self.value.items)
-        lenstr = ".%s" % lenitems
-        codewriter.globalinstance(self.ref,
-                                  self.db.repr_arg_type(self.value._TYPE),
-                                  "null")
-        #codewriter.globalinstance(self.dataref,
-        #                          self.db.repr_arg_type(self.value._TYPE),
-        #                          self.get_values())
+        type_, values = self.getall()
+        codewriter.globalinstance(self.ref, type_, values)

Modified: pypy/dist/pypy/translator/llvm2/database.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/database.py	(original)
+++ pypy/dist/pypy/translator/llvm2/database.py	Mon Jul  4 18:05:16 2005
@@ -1,6 +1,7 @@
 from pypy.translator.llvm2.log import log 
 from pypy.translator.llvm2.funcnode import FuncNode, FuncTypeNode
-from pypy.translator.llvm2.structnode import StructNode, StructTypeNode, StructVarsizeTypeNode
+from pypy.translator.llvm2.structnode import StructNode, StructVarsizeNode, \
+     StructTypeNode, StructVarsizeTypeNode
 from pypy.translator.llvm2.arraynode import ArrayNode, ArrayTypeNode
 from pypy.rpython import lltype
 from pypy.objspace.flow.model import Block, Constant, Variable
@@ -82,7 +83,11 @@
                     value = value._obj
 
                 if isinstance(ct, lltype.Struct):
-                    self.addpending(const_or_var, StructNode(self, value))
+                    if ct._arrayfld:
+                        assert False, "HERE"
+                        self.addpending(const_or_var, StructVarsizeNode(self, value))
+                    else:
+                        self.addpending(const_or_var, StructNode(self, value))
 
                 elif isinstance(ct, lltype.Array):
                     self.addpending(const_or_var, ArrayNode(self, value))
@@ -141,7 +146,6 @@
             if subset_types is None or isinstance(v, subset_types):
                 res.append(v)
         return res
-
         
     # __________________________________________________________
     # Representing variables and constants in LLVM source code 

Modified: pypy/dist/pypy/translator/llvm2/structnode.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/structnode.py	(original)
+++ pypy/dist/pypy/translator/llvm2/structnode.py	Mon Jul  4 18:05:16 2005
@@ -42,6 +42,9 @@
         super(StructVarsizeTypeNode, self).__init__(db, struct)
         new_var_name = "%%new.st.var.%s" % self.name
         self.constructor_name = "%s * %s(int %%len)" % (self.ref, new_var_name)
+
+    def __str__(self):
+        return "<StructVarsizeTypeNode %r>" %(self.ref,)
         
     def writedecl(self, codewriter): 
         # declaration for constructor
@@ -81,14 +84,22 @@
         codewriter.ret(self.ref + "*", "%result")
         codewriter.closefunc()
 
+
+def cast_global(toptr, from_, name):
+    s = "cast(%s* getelementptr (%s* %s, int 0) to %s)" % (from_,
+                                                           from_,
+                                                           name,
+                                                           toptr)
+    return s
+
 class StructNode(LLVMNode):
     _issetup = False 
     struct_counter = 0
 
     def __init__(self, db, value):
         self.db = db
-        self.name = "%s.%s" % (value._TYPE._name, StructNode.struct_counter)
-        self.ref = "%%stinstance.%s" % self.name
+        name = "%s.%s" % (value._TYPE._name, StructNode.struct_counter)
+        self.ref = "%%stinstance.%s" % name
         self.value = value
         StructNode.struct_counter += 1
 
@@ -107,9 +118,40 @@
                 
         self._issetup = True
 
-    def get_values(self):
+    def getall(self):
         res = []
-        for name in self.value._TYPE._names_without_voids():
+        type_ = self.value._TYPE
+        for name in type_._names_without_voids():
+            T = type_._flds[name]
+            value = getattr(self.value, name)
+            if not isinstance(T, lltype.Primitive):
+                # Create a dummy constant hack XXX
+                c = Constant(value, T)
+                x = self.db.obj2node[c]
+                value = self.db.repr_arg(c)
+                t, v = x.getall()
+                value = cast_global(self.db.repr_arg_type(T), t, value)
+                
+            else:
+                value = str(value)
+            res.append((self.db.repr_arg_type(T), value))
+                
+        typestr = self.db.repr_arg_type(type_)
+        values = ", ".join(["%s %s" % (t, v) for t, v in res])
+        return typestr, values
+    
+    def writeglobalconstants(self, codewriter):
+        type_, values = self.getall()
+        codewriter.globalinstance(self.ref, type_, values)
+                
+class StructVarsizeNode(StructNode):
+    def __str__(self):
+        return "<StructVarsizeNode %r>" %(self.ref,)
+
+    def getall(self):
+
+        res = []
+        for name in self.value._TYPE._names_without_voids()[:-1]:
             T = self.value._TYPE._flds[name]
             value = getattr(self.value, name)
             if not isinstance(T, lltype.Primitive):
@@ -118,10 +160,18 @@
             else:
                 value = str(value)
             res.append((self.db.repr_arg_type(T), value))
-        return ", ".join(["%s %s" % (t, v) for t, v in res])
-
-    def writeglobalconstants(self, codewriter):
-        codewriter.globalinstance(self.ref,
-                                  self.db.repr_arg_type(self.value._TYPE),
-                                  self.get_values())
 
+        # Special case for varsized arrays
+        self.value._TYPE._names_without_voids()[-1]
+        x = self.db.obj2node[Constant(value, T)]
+        t, v = x.get_values() 
+        res.append((t, "{%s}" % v))
+
+        s = self.value._TYPE
+        fields = [getattr(s, name) for name in s._names_without_voids()[-1]] 
+        l = [self.db.repr_arg_type(field) for field in fields]
+        l += t
+        typestr = "{ %s }" % ", ".join(l)
+        values = ", ".join(["%s %s" % (t, v) for t, v in res])
+        return typestr, values
+    

Modified: pypy/dist/pypy/translator/llvm2/test/test_genllvm.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/test/test_genllvm.py	(original)
+++ pypy/dist/pypy/translator/llvm2/test/test_genllvm.py	Mon Jul  4 18:05:16 2005
@@ -240,7 +240,7 @@
     assert f(-1) == 3
     assert f(0) == 5
 
-def DONOT_test_simple_chars():
+def Xtest_simple_chars():
      def char_constant2(s):
          s = s + s + s
          return len(s + '.')
@@ -265,14 +265,22 @@
     f = compile_function(list_list_getitem, [])
     assert f() == 1
 
-def Xtest_list_getitem_pbc(): 
+def test_list_getitem_pbc(): 
     l = [1,2]
-    def list_getitem(i): 
+    def list_getitem_pbc(i): 
         return l[i]
-    f = compile_function(list_getitem, [int], view=True)
+    f = compile_function(list_getitem_pbc, [int])
     assert f(0) == 1
     assert f(1) == 2
 
+def test_list_list_getitem_pbc(): 
+    l = [[0, 1], [0, 1]]
+    def list_list_getitem_pbc(i): 
+        return l[i][i]
+    f = compile_function(list_list_getitem_pbc, [int])
+    assert f(0) == 0
+    assert f(1) == 1
+
 def test_list_basic_ops(): 
     def list_basic_ops(i, j): 
         l = [1,2,3]



More information about the Pypy-commit mailing list