[pypy-svn] r29660 - in pypy/dist/pypy: rpython/rctypes/test translator/llvm

rxe at codespeak.net rxe at codespeak.net
Thu Jul 6 01:20:35 CEST 2006


Author: rxe
Date: Thu Jul  6 01:20:30 2006
New Revision: 29660

Modified:
   pypy/dist/pypy/rpython/rctypes/test/test_rarray.py
   pypy/dist/pypy/translator/llvm/codewriter.py
   pypy/dist/pypy/translator/llvm/database.py
   pypy/dist/pypy/translator/llvm/opwriter.py
   pypy/dist/pypy/translator/llvm/structnode.py
Log:
Actually create some (struct!) nodes for fixed size arrays.  Pass some more
tests.  Gave up with direct_arrayitems() operation.  Would probably save some
time if I stopped trying to guess what everything did and asked.
   


Modified: pypy/dist/pypy/rpython/rctypes/test/test_rarray.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/test/test_rarray.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/test/test_rarray.py	Thu Jul  6 01:20:30 2006
@@ -8,7 +8,6 @@
 from pypy.annotation.annrpython import RPythonAnnotator
 from pypy.translator.translator import TranslationContext
 from pypy import conftest
-from pypy.translator.c.test.test_genc import compile
 import sys
 from pypy.rpython.test.test_llinterp import interpret
 
@@ -17,6 +16,9 @@
 
 c_int_10 = ARRAY(c_int,10)
 
+test_c_compile = True
+test_llvm_compile = False
+
 def maketest():
     A1 = c_int * 10
     A2 = POINTER(c_int) * 10
@@ -281,6 +283,13 @@
         assert res.c_data[4] == 0
 
 class Test_compilation:
+    def setup_class(self):
+        if not test_c_compile:
+            py.test.skip("c compilation disabled")
+
+        from pypy.translator.c.test.test_genc import compile
+        self.compile = lambda s, x, y : compile(x, y)
+
     def test_compile_array_access(self):
         def access_array():
             my_array = c_int_10()
@@ -289,7 +298,7 @@
 
             return my_array[1]
 
-        fn = compile(access_array, [])
+        fn = self.compile(access_array, [])
         
         assert fn() == 2
 
@@ -303,13 +312,13 @@
                 array = my_array_3
             return array[n]
 
-        fn = compile(func, [int, int])
+        fn = self.compile(func, [int, int])
         assert fn(2, 7) == 49
         assert fn(3, 6) == 216
 
     def test_compile_variants(self):
         func, expected = maketest()
-        fn = compile(func, [])
+        fn = self.compile(func, [])
         assert fn() == expected
 
     def test_compile_char_array_value(self):
@@ -319,5 +328,12 @@
             a[0] = 'x'
             a[1] = 'y'
             return a.value
-        fn = compile(func, [])
+        fn = self.compile(func, [])
         assert fn() == "xy"
+
+class Test_compilation_llvm(Test_compilation):
+    def setup_class(self):
+        if not test_llvm_compile:
+            py.test.skip("llvm tests disabled")
+        from pypy.translator.llvm.test.runtest import compile_function
+        self.compile = lambda s, x, y : compile_function(x, y)

Modified: pypy/dist/pypy/translator/llvm/codewriter.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/codewriter.py	(original)
+++ pypy/dist/pypy/translator/llvm/codewriter.py	Thu Jul  6 01:20:30 2006
@@ -78,6 +78,9 @@
     def arraydef(self, name, lentype, typerepr):
         self.typedef(name, "{ %s, [0 x %s] }" % (lentype, typerepr))
 
+    def fixedarraydef(self, name, arraylen, typerepr):
+        self.typedef(name, "[%s x %s]" % (arraylen, typerepr))
+
     def funcdef(self, name, rettyperepr, argtypereprs):
         self.typedef(name, "%s (%s)" % (rettyperepr,
                                         ", ".join(argtypereprs)))

Modified: pypy/dist/pypy/translator/llvm/database.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/database.py	(original)
+++ pypy/dist/pypy/translator/llvm/database.py	Thu Jul  6 01:20:30 2006
@@ -5,7 +5,8 @@
 from pypy.translator.llvm.funcnode import FuncNode, FuncTypeNode
 from pypy.translator.llvm.extfuncnode import ExternalFuncNode
 from pypy.translator.llvm.structnode import StructNode, StructVarsizeNode, \
-     StructTypeNode, StructVarsizeTypeNode, getindexhelper
+     StructTypeNode, StructVarsizeTypeNode, getindexhelper, \
+     FixedSizeArrayTypeNode, FixedSizeArrayNode
 from pypy.translator.llvm.arraynode import ArrayNode, StrArrayNode, \
      VoidArrayNode, ArrayTypeNode, VoidArrayTypeNode
 from pypy.translator.llvm.opaquenode import OpaqueNode, ExtOpaqueNode, \
@@ -107,6 +108,9 @@
             else:
                 node = FuncNode(self, value)
 
+        elif isinstance(type_, lltype.FixedSizeArray):
+            node = FixedSizeArrayNode(self, value)
+
         elif isinstance(type_, lltype.Struct):
             if type_._arrayfld:
                 node = StructVarsizeNode(self, value)
@@ -151,11 +155,15 @@
         elif isinstance(type_, lltype.Ptr): 
             self.prepare_type(type_.TO)
 
+        elif isinstance(type_, lltype.FixedSizeArray):
+            self.addpending(type_, FixedSizeArrayTypeNode(self, type_))
+            
         elif isinstance(type_, lltype.Struct):
             if type_._arrayfld:
                 self.addpending(type_, StructVarsizeTypeNode(self, type_))
             else:
                 self.addpending(type_, StructTypeNode(self, type_))                
+
         elif isinstance(type_, lltype.FuncType): 
             self.addpending(type_, FuncTypeNode(self, type_))
 

Modified: pypy/dist/pypy/translator/llvm/opwriter.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/opwriter.py	(original)
+++ pypy/dist/pypy/translator/llvm/opwriter.py	Thu Jul  6 01:20:30 2006
@@ -373,7 +373,7 @@
         else:
             assert isinstance(ARRAYTYPE, lltype.FixedSizeArray)
             self.codewriter.getelementptr(tmpvar, arraytype, array,
-                                          [("uint", index)])
+                                          [(indextype, index)])
 
         self.codewriter.load(opr.retref, opr.rettype, tmpvar)
 
@@ -402,7 +402,7 @@
         else:
             assert isinstance(ARRAYTYPE, lltype.FixedSizeArray)
             self.codewriter.getelementptr(tmpvar, arraytype, array,
-                                          [("uint", index)])
+                                          [(indextype, index)])
             
         self.codewriter.store(valuetype, valuevar, tmpvar) 
             

Modified: pypy/dist/pypy/translator/llvm/structnode.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/structnode.py	(original)
+++ pypy/dist/pypy/translator/llvm/structnode.py	Thu Jul  6 01:20:30 2006
@@ -16,15 +16,15 @@
 
 class StructTypeNode(LLVMNode):
     __slots__ = "db struct ref name".split()
+    prefix = '%structtype_'
 
     def __init__(self, db, struct): 
         assert isinstance(struct, lltype.Struct)
         self.db = db
         self.struct = struct
-        prefix = '%structtype_'
         name = self.struct._name
-        self.ref = self.make_ref(prefix, name)
-        self.name = self.ref[len(prefix):]
+        self.ref = self.make_ref(self.prefix, name)
+        self.name = self.ref[len(self.prefix):]
         
     def __str__(self):
         return "<StructTypeNode %r>" %(self.ref,)
@@ -45,6 +45,18 @@
         fields_types = [self.db.repr_type(f) for f in self._fields()]
         codewriter.structdef(self.ref, fields_types)
 
+class FixedSizeArrayTypeNode(StructTypeNode):
+    prefix = '%fixarray_'
+
+    def __str__(self):
+        return "<FixedArrayTypeNode %r>" % self.ref
+
+    def writedatatypedecl(self, codewriter):
+        codewriter.fixedarraydef(self.ref,
+                                 self.struct.length,
+                                 self.db.repr_type(self.struct.OF))
+
+
 class StructVarsizeTypeNode(StructTypeNode):
     __slots__ = "constructor_ref constructor_decl".split()
 
@@ -86,13 +98,14 @@
     """
     __slots__ = "db value structtype ref _get_ref_cache _get_types".split()
 
+    prefix = '%structinstance_'
+
     def __init__(self, db, value):
         self.db = db
         self.value = value
         self.structtype = self.value._TYPE
-        prefix = '%structinstance_'
         name = str(value).split()[1]
-        self.ref = self.make_ref(prefix, name)
+        self.ref = self.make_ref(self.prefix, name)
         self._get_ref_cache = None
         self._get_types = self._compute_types()
 
@@ -160,6 +173,33 @@
         return "%s {\n  %s\n  }\n" % (self.get_typerepr(), all_values)
                 
                 
+class FixedSizeArrayNode(StructNode):
+    prefix = '%fixarrayinstance_'
+
+    def __str__(self):
+        return "<FixedSizeArrayNode %r>" % (self.ref,)
+
+    def constantvalue(self):
+        """ Returns the constant representation for this node. """
+        values = self._getvalues()
+        all_values = ",\n  ".join(values)
+        return "%s [\n  %s\n  ]\n" % (self.get_typerepr(), all_values)
+
+    def get_childref(self, index):
+        pos = 0
+        found = False
+        for name in self.structtype._names_without_voids():
+            if name == index:
+                found = True
+                break
+            pos += 1
+
+        return "getelementptr(%s* %s, int 0, int %s)" %(
+            self.get_typerepr(),
+            self.get_ref(),
+            pos)
+
+
 class StructVarsizeNode(StructNode):
     """ A varsize struct constant.  Can simply contain
     a primitive,



More information about the Pypy-commit mailing list