[pypy-svn] r65053 - pypy/branch/pyjitpl5/pypy/jit/backend/test

arigo at codespeak.net arigo at codespeak.net
Tue May 5 14:03:47 CEST 2009


Author: arigo
Date: Tue May  5 14:03:46 2009
New Revision: 65053

Modified:
   pypy/branch/pyjitpl5/pypy/jit/backend/test/test_ll_random.py
   pypy/branch/pyjitpl5/pypy/jit/backend/test/test_random.py
Log:
- generate rop.GETARRAYITEM_GC.
- support writing *something* to stdout, even if it's not correct,
  to get at least an idea of what is generated.


Modified: pypy/branch/pyjitpl5/pypy/jit/backend/test/test_ll_random.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/jit/backend/test/test_ll_random.py	(original)
+++ pypy/branch/pyjitpl5/pypy/jit/backend/test/test_ll_random.py	Tue May  5 14:03:46 2009
@@ -15,22 +15,52 @@
         test_random.OperationBuilder.__init__(self, *args, **kw)
         self.vtable_counter = 0
 
-    def get_structptr_var(self, r, must_have_vtable=False):
+    def get_structptr_var(self, r, must_have_vtable=False, type=lltype.Struct):
         while True:
-            if self.ptrvars and r.random() < 0.8:
-                v, S = r.choice(self.ptrvars)
-            elif self.prebuilt_ptr_consts and r.random() < 0.7:
-                v, S, _ = r.choice(self.prebuilt_ptr_consts)
+            ptrvars = [(v, S) for (v, S) in self.ptrvars
+                              if isinstance(S, type)]
+            if ptrvars and r.random() < 0.8:
+                v, S = r.choice(ptrvars)
             else:
-                must_have_vtable = must_have_vtable or r.random() < 0.5
-                p = self.get_random_structure(r, has_vtable=must_have_vtable)
-                S = lltype.typeOf(p).TO
-                v = ConstPtr(lltype.cast_opaque_ptr(llmemory.GCREF, p))
-                self.prebuilt_ptr_consts.append((v, S, self.field_values(p)))
+                prebuilt_ptr_consts = [(v, S)
+                                 for (v, S, _) in self.prebuilt_ptr_consts
+                                 if isinstance(S, type)]
+                if prebuilt_ptr_consts and r.random() < 0.7:
+                    v, S = r.choice(prebuilt_ptr_consts)
+                else:
+                    if type is lltype.Struct:
+                        # create a new constant structure
+                        must_have_vtable = must_have_vtable or r.random() < 0.5
+                        p = self.get_random_structure(r,
+                                                has_vtable=must_have_vtable)
+                    else:
+                        # create a new constant array
+                        p = self.get_random_array(r)
+                    S = lltype.typeOf(p).TO
+                    v = ConstPtr(lltype.cast_opaque_ptr(llmemory.GCREF, p))
+                    self.prebuilt_ptr_consts.append((v, S,
+                                                     self.field_values(p)))
             if not (must_have_vtable and S._names[0] != 'parent'):
                 break
         return v, S
 
+    def get_arrayptr_var(self, r):
+        return self.get_structptr_var(r, type=lltype.Array)
+
+    def get_random_primitive_type(self, r):
+        rval = r.random()
+        if rval < 0.25:
+            TYPE = lltype.Signed
+        elif rval < 0.5:
+            TYPE = lltype.Char
+        elif rval < 0.75:
+            TYPE = rffi.UCHAR
+        else:
+            TYPE = rffi.SHORT
+            if not self.HAVE_SHORT_FIELDS:
+                TYPE = lltype.Signed
+        return TYPE
+
     def get_random_structure_type(self, r, with_vtable=None):
         fields = []
         kwds = {}
@@ -38,17 +68,7 @@
             fields.append(('parent', rclass.OBJECT))
             kwds['hints'] = {'vtable': with_vtable._obj}
         for i in range(r.randrange(1, 5)):
-            rval = r.random()
-            if rval < 0.25:
-                TYPE = lltype.Signed
-            elif rval < 0.5:
-                TYPE = lltype.Char
-            elif rval < 0.75:
-                TYPE = rffi.UCHAR
-            else:
-                TYPE = rffi.SHORT
-                if not self.HAVE_SHORT_FIELDS:
-                    TYPE = lltype.Signed
+            TYPE = self.get_random_primitive_type(r)
             fields.append(('f%d' % i, TYPE))
         S = lltype.GcStruct('S%d' % self.counter, *fields, **kwds)
         self.counter += 1
@@ -82,11 +102,29 @@
                 setattr(p, fieldname, rffi.cast(TYPE, r.random_integer()))
         return p
 
+    def get_random_array_type(self, r):
+        TYPE = self.get_random_primitive_type(r)
+        return lltype.GcArray(TYPE)
+
+    def get_random_array(self, r):
+        A = self.get_random_array_type(r)
+        length = r.random_integer() % 300      # length: between 0 and 299
+        p = lltype.malloc(A, length)
+        for i in range(length):
+            p[i] = rffi.cast(A.OF, r.random_integer())
+        return p
+
     def field_values(self, p):
         dic = {}
-        for fieldname in lltype.typeOf(p).TO._names:
-            if fieldname != 'parent':
-                dic[fieldname] = getattr(p, fieldname)
+        S = lltype.typeOf(p).TO
+        if isinstance(S, lltype.Struct):
+            for fieldname in S._names:
+                if fieldname != 'parent':
+                    dic[fieldname] = getattr(p, fieldname)
+        else:
+            assert isinstance(S, lltype.Array)
+            for i in range(len(p)):
+                dic[i] = p[i]
         return dic
 
     def print_loop_prebuilt(self, names, writevar, s):
@@ -115,7 +153,8 @@
 class GuardClassOperation(test_random.GuardOperation):
     def gen_guard(self, builder, r):
         ptrvars = [(v, S) for (v, S) in builder.ptrvars
-                          if S._names[0] == 'parent']
+                          if isinstance(S, lltype.Struct) and
+                             S._names[0] == 'parent']
         if not ptrvars:
             raise test_random.CannotProduceOperation
         v, S = r.choice(ptrvars)
@@ -180,6 +219,33 @@
         v_ptr = builder.do(self.opnum, args, self.size_descr(builder, S))
         builder.ptrvars.append((v_ptr, S))
 
+class GetArrayItemOperation(test_random.AbstractOperation):
+    def field_descr(self, builder, r):
+        v, A = builder.get_arrayptr_var(r)
+        array = v.getptr(lltype.Ptr(A))
+        length = len(array)
+        if length == 0:
+            raise test_random.CannotProduceOperation
+        v_index = r.choice(builder.intvars)
+        if not (0 <= v_index.value < length):
+            v_index = ConstInt(r.random_integer() % length)
+        descr = builder.cpu.arraydescrof(A)
+        descr._random_info = 'cpu.arraydescrof(...)'
+        return v, A, v_index, descr
+
+    def produce_into(self, builder, r):
+        while True:
+            try:
+                v, _, v_index, descr = self.field_descr(builder, r)
+                self.put(builder, [v, v_index], descr)
+            except lltype.UninitializedMemoryAccess:
+                continue
+            break
+
+#class NewArrayOperation(test_random.AbstractOperation):
+#    ...
+
+# XXX why is the following here, and not in test_random?
 # there are five options in total:
 # 1. non raising call and guard_no_exception
 # 2. raising call and guard_exception
@@ -339,6 +405,8 @@
     OPERATIONS.append(NewOperation(rop.NEW))
     OPERATIONS.append(NewOperation(rop.NEW_WITH_VTABLE))
 
+    OPERATIONS.append(GetArrayItemOperation(rop.GETARRAYITEM_GC))
+
     OPERATIONS.append(GuardClassOperation(rop.GUARD_CLASS))
     OPERATIONS.append(CallOperation(rop.CALL))
     OPERATIONS.append(RaisingCallOperation(rop.CALL))

Modified: pypy/branch/pyjitpl5/pypy/jit/backend/test/test_random.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/jit/backend/test/test_random.py	(original)
+++ pypy/branch/pyjitpl5/pypy/jit/backend/test/test_random.py	Tue May  5 14:03:46 2009
@@ -62,24 +62,31 @@
             if v in names:
                 args.append(names[v])
             elif isinstance(v, ConstAddr):
-                name = ''.join([v.value.ptr.name[i]
-                                for i in range(len(v.value.ptr.name)-1)])
-                args.append(
-                    'ConstAddr(llmemory.cast_ptr_to_adr(%s_vtable), cpu)'
-                    % name)
+                try:
+                    name = ''.join([v.value.ptr.name[i]
+                                    for i in range(len(v.value.ptr.name)-1)])
+                except AttributeError:
+                    args.append('ConstAddr(...)')
+                else:
+                    args.append(
+                        'ConstAddr(llmemory.cast_ptr_to_adr(%s_vtable), cpu)'
+                        % name)
             else:
                 args.append('ConstInt(%d)' % v.value)
         if op.descr is None:
             descrstr = ''
         else:
-            descrstr = ', ' + op.descr._random_info
+            try:
+                descrstr = ', ' + op.descr._random_info
+            except AttributeError:
+                descrstr = ', descr=...'
         print >>s, '        ResOperation(rop.%s, [%s], %s%s),' % (
             opname[op.opnum], ', '.join(args), names[op.result], descrstr)
         if getattr(op, 'suboperations', None) is not None:
             subops.append(op)
 
     def print_loop(self):
-        raise PleaseRewriteMe()
+        #raise PleaseRewriteMe()
         def update_names(ops):
             for op in ops:
                 v = op.result
@@ -117,8 +124,8 @@
         #
         print >>s, '    cpu = CPU(None, None)'
         print >>s, "    loop = TreeLoop('test')"
-        print >>s, '    loop.inputargs = [%s]' % (
-            ', '.join([names[v] for v in self.loop.inputargs]))
+        print >>s, '    loop.inputargs = [...]' # % (
+            # ', '.join([names[v] for v in self.loop.inputargs]))
         print >>s, '    loop.operations = ['
         for op in self.loop.operations:
             self.process_operation(s, op, names, subops)
@@ -136,15 +143,15 @@
                 #print >>s, '        ResOperation(rop.FAIL, [%s], None)]' % (
                 #    ', '.join([names[v] for v in op.args]))
         print >>s, '    cpu.compile_operations(loop)'
-        for i, v in enumerate(self.loop.inputargs):
-            print >>s, '    cpu.set_future_value_int(%d, %d)' % (i, v.value)
+        #for i, v in enumerate(self.loop.inputargs):
+        #    print >>s, '    cpu.set_future_value_int(%d, %d)' % (i, v.value)
         print >>s, '    op = cpu.execute_operations(loop)'
         if self.should_fail_by is None:
             for i, v in enumerate(self.loop.operations[-1].args):
                 print >>s, '    assert cpu.get_latest_value_int(%d) == %d' % (
                     i, v.value)
         else:
-            print >>s, '    assert op is loop.operations[%d].suboperations[0]' % self.should_fail_by_num
+            #print >>s, '    assert op is loop.operations[%d].suboperations[0]' % self.should_fail_by_num
             for i, v in enumerate(self.should_fail_by.args):
                 print >>s, '    assert cpu.get_latest_value_int(%d) == %d' % (
                     i, v.value)
@@ -377,12 +384,16 @@
         self.expected = {}
         for v in endvars:
             self.expected[v] = v.value
+        #builder.print_loop()
 
     def clear_state(self):
         for v, S, fields in self.prebuilt_ptr_consts:
             container = v.value._obj.container
             for name, value in fields.items():
-                setattr(container, name, value)
+                if isinstance(name, str):
+                    setattr(container, name, value)
+                else:
+                    container.setitem(name, value)
 
     def run_loop(self):
         cpu = self.builder.cpu



More information about the Pypy-commit mailing list