[pypy-svn] r6632 - pypy/trunk/src/pypy/translator

arigo at codespeak.net arigo at codespeak.net
Tue Sep 21 20:56:02 CEST 2004


Author: arigo
Date: Tue Sep 21 20:56:02 2004
New Revision: 6632

Modified:
   pypy/trunk/src/pypy/translator/genc.h
   pypy/trunk/src/pypy/translator/genc_op.py
   pypy/trunk/src/pypy/translator/genc_typeset.py
Log:
SETARRAYITEM support.


Modified: pypy/trunk/src/pypy/translator/genc.h
==============================================================================
--- pypy/trunk/src/pypy/translator/genc.h	(original)
+++ pypy/trunk/src/pypy/translator/genc.h	Tue Sep 21 20:56:02 2004
@@ -143,11 +143,17 @@
 
 #define OP_NEWARRAY(name,len,r,err)   if (!(r=alloclist_##name(len))) goto err;
 #define OP_NEWARRAY_SET(name,a,i,f,v)   ((PyList_##name*) a)->ob_item[i].f=v;
-#define OP_NEWARRAY_SET_o(name,a,i,f,o) ((PyList_##name*) a)->ob_item[i].f=o; \
-								  Py_INCREF(o);
+#define OP_NEWARRAY_SET_o(name,a,i,f,v) ((PyList_##name*) a)->ob_item[i].f=v; \
+								  Py_INCREF(v);
 #define OP_GETARRAYITEM(name,a,i,f,r)   r=((PyList_##name*) a)->ob_item[i].f;
 #define OP_GETARRAYITEM_o(name,a,i,f,r) r=((PyList_##name*) a)->ob_item[i].f; \
 								  Py_INCREF(r);
+#define OP_SETARRAYITEM(name,a,i,f,v)   ((PyList_##name*) a)->ob_item[i].f=v;
+#define OP_SETARRAYITEM_o(name,a,i,f,v) { PyObject* tmp;                    \
+					  OP_GETARRAYITEM(name,a,i,f,tmp)   \
+					  OP_SETARRAYITEM(name,a,i,f,v)     \
+					  Py_INCREF(v); Py_DECREF(tmp);     \
+					}
 
 /************************************************************/
  /***  The rest is produced by genc.py                     ***/

Modified: pypy/trunk/src/pypy/translator/genc_op.py
==============================================================================
--- pypy/trunk/src/pypy/translator/genc_op.py	(original)
+++ pypy/trunk/src/pypy/translator/genc_op.py	Tue Sep 21 20:56:02 2004
@@ -211,6 +211,7 @@
 class LoGetArrayItem(LoC):
     typename = PARAMETER   # the name of the PyList_Xxx type in the C source
     lltypes  = PARAMETER   # the C types needed to represent each array item
+    macro    = 'OP_GETARRAYITEM'
     # self.args: [PyObject, int_index, output_item..]
     def writestr(self, array, index, *output):
         assert len(output) == len(self.lltypes)
@@ -220,10 +221,15 @@
                 typecode = '_o'
             else:
                 typecode = ''
-            ls.append('OP_GETARRAYITEM%s(%s, %s, %s, a%d, %s)' % (
-                typecode, self.typename, array, index, j, output[j]))
+            ls.append('%s%s(%s, %s, %s, a%d, %s)' % (
+                self.macro, typecode, self.typename,
+                array, index, j, output[j]))
         return '\n'.join(ls)
 
+class LoSetArrayItem(LoGetArrayItem):
+    macro = 'OP_SETARRAYITEM'
+    # self.args: [PyObject, int_index, input_item..]
+
 class LoGetAttr(LoC):
     cost = 1
     fld  = PARAMETER

Modified: pypy/trunk/src/pypy/translator/genc_typeset.py
==============================================================================
--- pypy/trunk/src/pypy/translator/genc_typeset.py	(original)
+++ pypy/trunk/src/pypy/translator/genc_typeset.py	Tue Sep 21 20:56:02 2004
@@ -73,7 +73,7 @@
                     return r_func
                 else:
                     return method_representation(r_func)
-            if 0: # still working on it -- isinstance(var, annmodel.SomeList):
+            if isinstance(var, annmodel.SomeList):
                 r_item = self.gethltype(var.s_item)
                 typename = self.genc.declare_list(r_item.impl)
                 r = list_representation(r_item)
@@ -154,6 +154,23 @@
         return result
 
     # ____________________________________________________________
+    #
+    # Each extend_OPNAME() method below is called when there is no direct
+    # match for the given hltypes for the operation OPNAME.
+    # The extend_XXX() methods should produce (yield) as many low-level
+    # operation signatures as they like, using the hltypes as a guide;
+    # once all these have been generated, the algorithms in typer.py will
+    # look for a best approximate match, doing conversions if needed.
+    #
+    # For example, extend_OP_GETITEM() is called with
+    #    hltypes == (repr_of_obj, repr_of_index, repr_of_result).
+    # When it is called with an instance of CList in hltypes[0], it
+    # generates the operation that reads an item out of this kind of array.
+    # This operation takes exactly an R_INT index (which may differ from
+    # hltypes[1]) and produce a result of exactly the CList's r_item type
+    # (which may differ from hltypes[2]).  Then typer.py will try to
+    # convert the index from hltypes[1] to R_INT and convert the result back
+    # to hltypes[2].
 
     def extend_OP_NEWLIST(self, hltypes):
         if not hltypes:
@@ -350,6 +367,18 @@
                 lltypes  = r.r_item.impl,
                 )
 
+    def extend_OP_SETITEM(self, hltypes):
+        if len(hltypes) != 4:
+            return
+        r, r_index, r_value, r_void = hltypes
+        # writing into a CList
+        if isinstance(r, CList):
+            sig = (r, R_INT, r.r_item, R_VOID)
+            yield sig, genc_op.LoSetArrayItem.With(
+                typename = r.typename,
+                lltypes  = r.r_item.impl,
+                )
+
     # ____________________________________________________________
 
     def parse_operation_templates(self):



More information about the Pypy-commit mailing list