[pypy-svn] r24185 - in pypy/dist/pypy/translator/squeak: . test

nik at codespeak.net nik at codespeak.net
Thu Mar 9 18:04:34 CET 2006


Author: nik
Date: Thu Mar  9 18:04:23 2006
New Revision: 24185

Modified:
   pypy/dist/pypy/translator/squeak/gensqueak.py
   pypy/dist/pypy/translator/squeak/test/test_squeaktrans.py
Log:
only use accessors when needed, ie if an instance variable is accessed
from outside its class' methods.


Modified: pypy/dist/pypy/translator/squeak/gensqueak.py
==============================================================================
--- pypy/dist/pypy/translator/squeak/gensqueak.py	(original)
+++ pypy/dist/pypy/translator/squeak/gensqueak.py	Thu Mar  9 18:04:23 2006
@@ -269,18 +269,23 @@
             receiver = args[0]
             name = op.args[1].value
             args = args[2:]
-            # XXX should only generate getter if field is set from outside
-            self.gen.schedule_node(
-                    GetterNode(self.gen, op.args[0].concretetype, name))
+            if hasattr(self, "self") and op.args[0] == self.self:
+                # Could also directly substitute op.result with name
+                # everywhere for optimization.
+                return "%s := %s." % (self.expr(op.result), name) 
+            else:
+                self.gen.schedule_node(
+                        GetterNode(self.gen, op.args[0].concretetype, name))
         elif op.opname == "oosetfield":
             receiver = args[0]
-            if hasattr(self, "self") and op.args[0] == self.self:
-                receiver = "self"
             name = op.args[1].value
             args = args[2:]
-            # XXX should only generate setter if field is set from outside
-            self.gen.schedule_node(
-                    SetterNode(self.gen, op.args[0].concretetype, name))
+            if hasattr(self, "self") and op.args[0] == self.self:
+                # Note that the receiver variable is never used
+                return "%s := %s." % (name, args[0])
+            else:
+                self.gen.schedule_node(
+                        SetterNode(self.gen, op.args[0].concretetype, name))
         elif op.opname == "direct_call":
             # XXX not sure if static methods of a specific class should
             # be treated differently.

Modified: pypy/dist/pypy/translator/squeak/test/test_squeaktrans.py
==============================================================================
--- pypy/dist/pypy/translator/squeak/test/test_squeaktrans.py	(original)
+++ pypy/dist/pypy/translator/squeak/test/test_squeaktrans.py	Thu Mar  9 18:04:23 2006
@@ -162,13 +162,18 @@
 
     def test_getfield_setfield(self):
         class A:
-            def m(self, i):
+            def set(self, i):
                 self.i = i
+            def inc(self):
+                self.i = self.i + 1
         def f(i):
             a = A()
-            a.m(i)
-            return a.i
-        assert self.run_on_squeak(f, 2) == "2"
+            a.set(i)
+            i = a.i
+            a.i = 3
+            a.inc()
+            return i + a.i
+        assert self.run_on_squeak(f, 2) == "6"
 
 
 class TestSelector:



More information about the Pypy-commit mailing list