[pypy-svn] r65664 - in pypy/branch/pyjitpl5-experiments/pypy/jit/backend: llvm test

arigo at codespeak.net arigo at codespeak.net
Mon Jun 8 18:00:14 CEST 2009


Author: arigo
Date: Mon Jun  8 18:00:12 2009
New Revision: 65664

Modified:
   pypy/branch/pyjitpl5-experiments/pypy/jit/backend/llvm/compile.py
   pypy/branch/pyjitpl5-experiments/pypy/jit/backend/llvm/runner.py
   pypy/branch/pyjitpl5-experiments/pypy/jit/backend/test/runner_test.py
Log:
Strings.


Modified: pypy/branch/pyjitpl5-experiments/pypy/jit/backend/llvm/compile.py
==============================================================================
--- pypy/branch/pyjitpl5-experiments/pypy/jit/backend/llvm/compile.py	(original)
+++ pypy/branch/pyjitpl5-experiments/pypy/jit/backend/llvm/compile.py	Mon Jun  8 18:00:12 2009
@@ -608,6 +608,40 @@
             value_ref = self.getintarg(op.args[2])
         llvm_rffi.LLVMBuildStore(self.builder, value_ref, loc, "")
 
+    def generate_STRLEN(self, op):
+        array = llvm_rffi.LLVMBuildBitCast(self.builder,
+                                           self.getptrarg(op.args[0]),
+                                           self.cpu.ty_string_ptr, "")
+        indices = lltype.malloc(rffi.CArray(llvm_rffi.LLVMValueRef), 2,
+                                flavor='raw')
+        indices[0] = self.cpu.const_zero
+        indices[1] = self.cpu.const_string_index_length
+        loc = llvm_rffi.LLVMBuildGEP(self.builder, array, indices, 2, "")
+        lltype.free(indices, flavor='raw')
+        self.vars[op.result] = llvm_rffi.LLVMBuildLoad(self.builder, loc, "")
+
+    def _generate_string_gep(self, v_string, v_index):
+        array = llvm_rffi.LLVMBuildBitCast(self.builder,
+                                           self.getptrarg(v_string),
+                                           self.cpu.ty_string_ptr, "")
+        indices = lltype.malloc(rffi.CArray(llvm_rffi.LLVMValueRef), 3,
+                                flavor='raw')
+        indices[0] = self.cpu.const_zero
+        indices[1] = self.cpu.const_string_index_array
+        indices[2] = self.getintarg(v_index)
+        location = llvm_rffi.LLVMBuildGEP(self.builder, array, indices, 3, "")
+        lltype.free(indices, flavor='raw')
+        return location
+
+    def generate_STRGETITEM(self, op):
+        loc = self._generate_string_gep(op.args[0], op.args[1])
+        self.vars[op.result] = llvm_rffi.LLVMBuildLoad(self.builder, loc, "")
+
+    def generate_STRSETITEM(self, op):
+        loc = self._generate_string_gep(op.args[0], op.args[1])
+        value_ref = self.getchararg(op.args[2])
+        llvm_rffi.LLVMBuildStore(self.builder, value_ref, loc, "")
+
 # ____________________________________________________________
 
 class MissingOperation(Exception):

Modified: pypy/branch/pyjitpl5-experiments/pypy/jit/backend/llvm/runner.py
==============================================================================
--- pypy/branch/pyjitpl5-experiments/pypy/jit/backend/llvm/runner.py	(original)
+++ pypy/branch/pyjitpl5-experiments/pypy/jit/backend/llvm/runner.py	Mon Jun  8 18:00:12 2009
@@ -1,5 +1,5 @@
 import sys
-from pypy.rpython.lltypesystem import lltype, llmemory, rffi, rclass
+from pypy.rpython.lltypesystem import lltype, llmemory, rffi, rclass, rstr
 from pypy.rlib.objectmodel import we_are_translated
 from pypy.jit.metainterp.history import AbstractDescr, INT
 from pypy.jit.metainterp.history import BoxInt, BoxPtr
@@ -42,6 +42,9 @@
         basesize, _, ofs_length = symbolic.get_array_token(
             lltype.GcArray(lltype.Signed), self.translate_support_code)
         self._fixed_array_shape = basesize, ofs_length
+        basesize, _, ofs_length = symbolic.get_array_token(
+            rstr.STR, self.translate_support_code)
+        self._string_shape = basesize, ofs_length
 
     def setup_once(self):
         if not we_are_translated():
@@ -70,34 +73,22 @@
         self.types_ptr_by_index = [self.ty_char_ptr_ptr,   # SIZE_GCPTR
                                    self.ty_int_ptr,        # SIZE_INT
                                    self.ty_char_ptr]       # SIZE_CHAR
-        pad1 = self._fixed_array_shape[1]
-        pad2 = (self._fixed_array_shape[0] - self._fixed_array_shape[1]
-                - self.size_of_int)
-        self.const_array_index_length = self._make_const_int(pad1)
-        self.const_array_index_array = self._make_const_int(pad1 + 1 + pad2)
+        (shape_basesize, shape_length) = self._fixed_array_shape
         for i in range(len(self.types_by_index)):
-            # build the type "struct{pad1.., length, pad2.., array{type}}"
-            typeslist = lltype.malloc(rffi.CArray(llvm_rffi.LLVMTypeRef),
-                                      pad1+pad2+2, flavor='raw')
-            # add the first padding
-            for n in range(pad1):
-                typeslist[n] = self.ty_char
-            # add the length field
-            typeslist[pad1] = self.ty_int
-            # add the second padding
-            for n in range(pad1+1, pad1+1+pad2):
-                typeslist[n] = self.ty_char
-            # add the array field
-            typeslist[pad1+1+pad2] = llvm_rffi.LLVMArrayType(
-                self.types_by_index[i], 0)
-            # done
-            ty_array = llvm_rffi.LLVMStructType(typeslist,
-                                                pad1+pad2+2,
-                                                1)
-            lltype.free(typeslist, flavor='raw')
-            ty_array_ptr = llvm_rffi.LLVMPointerType(ty_array, 0)
             arraydescr = self._descr_caches['array', i]
-            arraydescr.ty_array_ptr = ty_array_ptr
+            (arraydescr.ty_array_ptr,
+             self.const_array_index_length,
+             self.const_array_index_array) = \
+                    self._build_ty_array_ptr(shape_basesize,
+                                             self.types_by_index[i],
+                                             shape_length)
+        (shape_basesize, shape_length) = self._string_shape
+        (self.ty_string_ptr,
+         self.const_string_index_length,
+         self.const_string_index_array) = \
+                 self._build_ty_array_ptr(shape_basesize,
+                                          self.ty_char,
+                                          shape_length)
         #
         arglist = lltype.malloc(rffi.CArray(llvm_rffi.LLVMTypeRef), 0,
                                 flavor='raw')
@@ -161,6 +152,33 @@
         setattr(self, 'const_%s_error_value' % prefix,
                 self._make_const(ll_inst, self.ty_char_ptr))
 
+    def _build_ty_array_ptr(self, basesize, ty_item, ofs_length):
+        pad1 = ofs_length
+        pad2 = basesize - ofs_length - self.size_of_int
+        assert pad1 >= 0 and pad2 >= 0
+        index_length = self._make_const_int(pad1)
+        index_array = self._make_const_int(pad1 + 1 + pad2)
+        # build the type "struct{pad1.., length, pad2.., array{type}}"
+        typeslist = lltype.malloc(rffi.CArray(llvm_rffi.LLVMTypeRef),
+                                  pad1+pad2+2, flavor='raw')
+        # add the first padding
+        for n in range(pad1):
+            typeslist[n] = self.ty_char
+        # add the length field
+        typeslist[pad1] = self.ty_int
+        # add the second padding
+        for n in range(pad1+1, pad1+1+pad2):
+            typeslist[n] = self.ty_char
+        # add the array field
+        typeslist[pad1+1+pad2] = llvm_rffi.LLVMArrayType(ty_item, 0)
+        # done
+        ty_array = llvm_rffi.LLVMStructType(typeslist,
+                                            pad1+pad2+2,
+                                            1)
+        lltype.free(typeslist, flavor='raw')
+        ty_array_ptr = llvm_rffi.LLVMPointerType(ty_array, 0)
+        return (ty_array_ptr, index_length, index_array)
+
     # ------------------------------
     # Compilation
 

Modified: pypy/branch/pyjitpl5-experiments/pypy/jit/backend/test/runner_test.py
==============================================================================
--- pypy/branch/pyjitpl5-experiments/pypy/jit/backend/test/runner_test.py	(original)
+++ pypy/branch/pyjitpl5-experiments/pypy/jit/backend/test/runner_test.py	Mon Jun  8 18:00:12 2009
@@ -281,7 +281,7 @@
     def test_ovf_operations_reversed(self):
         self.test_ovf_operations(reversed=True)
 
-    def test_field(self):
+    def test_field_basic(self):
         t_box, T_box = self.alloc_instance(self.T)
         fielddescr = self.cpu.fielddescrof(self.S, 'value')
         res = self.execute_operation(rop.SETFIELD_GC, [t_box, BoxInt(39082)],
@@ -384,7 +384,7 @@
         r = self.execute_operation(rop.OONONNULL, [null_box], 'int')
         assert r.value == 0
 
-    def test_array(self):
+    def test_array_basic(self):
         a_box, A = self.alloc_array_of(lltype.Signed, 342)
         arraydescr = self.cpu.arraydescrof(A)
         r = self.execute_operation(rop.ARRAYLEN_GC, [a_box],
@@ -433,6 +433,13 @@
                                    'ptr', descr=arraydescr)
         assert r.value == a_box.value
 
+    def test_string_basic(self):
+        s_box = self.alloc_string("hello\xfe")
+        r = self.execute_operation(rop.STRLEN, [s_box], 'int')
+        assert r.value == 6
+        r = self.execute_operation(rop.STRGETITEM, [s_box, BoxInt(5)], 'int')
+        assert r.value == 254
+
 
 class LLtypeBackendTest(BaseBackendTest):
 
@@ -488,6 +495,13 @@
         a_box = BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, a))
         return a_box, A
 
+    def alloc_string(self, string):
+        s = rstr.mallocstr(len(string))
+        for i in range(len(string)):
+            s.chars[i] = string[i]
+        s_box = BoxPtr(lltype.cast_opaque_ptr(llmemory.GCREF, s))
+        return s_box
+
 
     def test_casts(self):
         from pypy.rpython.lltypesystem import lltype, llmemory
@@ -545,3 +559,6 @@
 
     def alloc_array_of(self, ITEM, length):
         py.test.skip("implement me")
+
+    def alloc_string(self, string):
+        py.test.skip("implement me")



More information about the Pypy-commit mailing list