[pypy-commit] lang-smalltalk default: (timfel, cfbolz) change all uses of w_method to use the method shadow

timfel noreply at buildbot.pypy.org
Mon Feb 18 18:23:31 CET 2013


Author: Tim Felgentreff <timfelgentreff at gmail.com>
Branch: 
Changeset: r46:871be2673edf
Date: 2013-02-18 18:00 +0100
http://bitbucket.org/pypy/lang-smalltalk/changeset/871be2673edf/

Log:	(timfel, cfbolz) change all uses of w_method to use the method
	shadow

diff --git a/spyvm/interpreter.py b/spyvm/interpreter.py
--- a/spyvm/interpreter.py
+++ b/spyvm/interpreter.py
@@ -15,8 +15,8 @@
 class IllegalStoreError(Exception):
     """Illegal Store."""
 
-def get_printable_location(pc, self, w_method):
-    bc = ord(w_method.bytes[pc])
+def get_printable_location(pc, self, method):
+    bc = ord(method.bytecode[pc])
     return '%d: [%s]%s' % (pc, hex(bc), BYTECODE_NAMES[bc])
 
 
@@ -26,7 +26,7 @@
     cnt = 0
     _last_indent = ""
     jit_driver = jit.JitDriver(
-        greens=['pc', 'self', 'w_method'],
+        greens=['pc', 'self', 'method'],
         reds=['s_active_context'],
         get_printable_location=get_printable_location
     )
@@ -93,10 +93,10 @@
         while True:
             s_active_context = self.s_active_context()
             pc = s_active_context._pc
-            w_method = s_active_context.w_method()
+            method = s_active_context.method()
 
             self.jit_driver.jit_merge_point(
-                pc=pc, self=self, w_method=w_method,
+                pc=pc, self=self, method=method,
                 s_active_context=s_active_context)
             self.step(s_active_context)
 
@@ -141,14 +141,14 @@
 
     def pushLiteralConstantBytecode(self, interp):
         index = self.currentBytecode & 31
-        self.push(self.w_method().getliteral(index))
+        self.push(self.method().getliteral(index))
 
     def pushLiteralVariableBytecode(self, interp):
         # this bytecode assumes that literals[index] is an Association
         # which is an object with two named vars, and fetches the second
         # named var (the value).
         index = self.currentBytecode & 31
-        w_association = self.w_method().getliteral(index)
+        w_association = self.method().getliteral(index)
         association = wrapper.AssociationWrapper(self.space, w_association)
         self.push(association.value())
 
@@ -193,7 +193,7 @@
 
     # send, return bytecodes
     def sendLiteralSelectorBytecode(self, interp):
-        selector = self.w_method().getliteralsymbol(self.currentBytecode & 15)
+        selector = self.method().getliteralsymbol(self.currentBytecode & 15)
         argcount = ((self.currentBytecode >> 4) & 3) - 1
         self._sendSelfSelector(selector, argcount, interp)
 
@@ -203,7 +203,7 @@
                            receiver, receiver.shadow_of_my_class(self.space))
 
     def _sendSuperSelector(self, selector, argcount, interp):
-        w_compiledin = self.w_method().compiledin()
+        w_compiledin = self.method().w_compiledin
         assert isinstance(w_compiledin, model.W_PointersObject)
         s_compiledin = w_compiledin.as_class_get_shadow(self.space)
         self._sendSelector(selector, argcount, interp, self.w_receiver(),
@@ -289,9 +289,9 @@
         elif variableType == 1:
             self.push(self.gettemp(variableIndex))
         elif variableType == 2:
-            self.push(self.w_method().getliteral(variableIndex))
+            self.push(self.method().getliteral(variableIndex))
         elif variableType == 3:
-            w_association = self.w_method().getliteral(variableIndex)
+            w_association = self.method().getliteral(variableIndex)
             association = wrapper.AssociationWrapper(self.space, w_association)
             self.push(association.value())
         else:
@@ -306,7 +306,7 @@
         elif variableType == 2:
             raise IllegalStoreError
         elif variableType == 3:
-            w_association = self.w_method().getliteral(variableIndex)
+            w_association = self.method().getliteral(variableIndex)
             association = wrapper.AssociationWrapper(self.space, w_association)
             association.store_value(self.top())
 
@@ -316,7 +316,7 @@
 
     def getExtendedSelectorArgcount(self):
         descriptor = self.getbytecode()
-        return ((self.w_method().getliteralsymbol(descriptor & 31)),
+        return ((self.method().getliteralsymbol(descriptor & 31)),
                 (descriptor >> 5))
 
     def singleExtendedSendBytecode(self, interp):
@@ -329,21 +329,21 @@
         opType = second >> 5
         if opType == 0:
             # selfsend
-            self._sendSelfSelector(self.w_method().getliteralsymbol(third),
+            self._sendSelfSelector(self.method().getliteralsymbol(third),
                                    second & 31, interp)
         elif opType == 1:
             # supersend
-            self._sendSuperSelector(self.w_method().getliteralsymbol(third),
+            self._sendSuperSelector(self.method().getliteralsymbol(third),
                                     second & 31, interp)
         elif opType == 2:
             # pushReceiver
             self.push(self.w_receiver().fetch(self.space, third))
         elif opType == 3:
             # pushLiteralConstant
-            self.push(self.w_method().getliteral(third))
+            self.push(self.method().getliteral(third))
         elif opType == 4:
             # pushLiteralVariable
-            w_association = self.w_method().getliteral(third)
+            w_association = self.method().getliteral(third)
             association = wrapper.AssociationWrapper(self.space, w_association)
             self.push(association.value())
         elif opType == 5:
@@ -351,7 +351,7 @@
         elif opType == 6:
             self.w_receiver().store(self.space, third, self.pop())
         elif opType == 7:
-            w_association = self.w_method().getliteral(third)
+            w_association = self.method().getliteral(third)
             association = wrapper.AssociationWrapper(self.space, w_association)
             association.store_value(self.top())
 
@@ -361,7 +361,7 @@
 
     def secondExtendedSendBytecode(self, interp):
         descriptor = self.getbytecode()
-        selector = self.w_method().getliteralsymbol(descriptor & 63)
+        selector = self.method().getliteralsymbol(descriptor & 63)
         argcount = descriptor >> 6
         self._sendSelfSelector(selector, argcount, interp)
 
diff --git a/spyvm/model.py b/spyvm/model.py
--- a/spyvm/model.py
+++ b/spyvm/model.py
@@ -478,38 +478,14 @@
         self.bytes, w_other.bytes = w_other.bytes, self.bytes
         self.header, w_other.header = w_other.header, self.header
         self.literalsize, w_other.literalsize = w_other.literalsize, self.literalsize
-        self.w_compiledin, w_other.w_compiledin = w_other.w_compiledin, self.w_compiledin
         self.islarge, w_other.islarge = w_other.islarge, self.islarge
         self._shadow = w_other._shadow = None
         W_AbstractObjectWithIdentityHash._become(self, w_other)
         return True
 
-    def compiledin(self):  
-        if self.w_compiledin is None:
-            from spyvm import wrapper
-            # (Blue book, p 607) All CompiledMethods that contain
-            # extended-super bytecodes have the clain which they are found as
-            # their last literal variable.   
-            # Last of the literals is an association with compiledin
-            # as a class
-            w_association = self.literals[-1]
-            # XXX XXX XXX where to get a space from here
-            association = wrapper.AssociationWrapper(None, w_association)
-            self.w_compiledin = association.value()
-        return self.w_compiledin
-
     def getclass(self, space):
         return space.w_CompiledMethod
 
-    def getliteral(self, index):
-                                    # We changed this part
-        return self.literals[index] #+ constants.LITERAL_START]
-
-    def getliteralsymbol(self, index):
-        w_literal = self.getliteral(index)
-        assert isinstance(w_literal, W_BytesObject)
-        return w_literal.as_string()    # XXX performance issue here
-
     def create_frame(self, space, receiver, arguments, sender = None):
         from spyvm import shadow
         assert len(arguments) == self.argsize
@@ -578,9 +554,14 @@
         self.tempsize = tempsize
         assert self.tempsize >= self.argsize
         self.primitive = primitive
-        self.w_compiledin = None
         self.islarge = islarge
 
+    def setliterals(self, literals):
+        """NOT RPYTHON
+           Only for testing"""
+        self.literals = literals
+        self._shadow = None
+
     def setbytes(self, bytes):
         self.bytes = bytes
 
diff --git a/spyvm/shadow.py b/spyvm/shadow.py
--- a/spyvm/shadow.py
+++ b/spyvm/shadow.py
@@ -1,5 +1,5 @@
 import weakref
-from spyvm import model, constants, error
+from spyvm import model, constants, error, wrapper
 from rpython.tool.pairtype import extendabletype
 from rpython.rlib import rarithmetic, jit
 
@@ -249,11 +249,12 @@
             self.w_methoddict._store(1, model.W_PointersObject(None, 0))
             self.s_methoddict().invalid = False
 
-    def installmethod(self, selector, method):
+    def installmethod(self, selector, w_method):
         "NOT_RPYTHON"     # this is only for testing.
         self.initialize_methoddict()
-        self.s_methoddict().methoddict[selector] = method
-        if isinstance(method, model.W_CompiledMethod):
+        self.s_methoddict().methoddict[selector] = w_method
+        if isinstance(w_method, model.W_CompiledMethod):
+            method = w_method.as_compiledmethod_get_shadow(self.space)
             method.w_compiledin = self.w_self()
 
 class MethodDictionaryShadow(AbstractCachingShadow):
@@ -715,7 +716,8 @@
 
 
 class CompiledMethodShadow(object):
-    _immutable_fields_ = ["bytecode", "literals[*]", "bytecodeoffset", "literalsize", "tempsize"]
+    _immutable_fields_ = ["bytecode", "literals[*]", "bytecodeoffset",
+                          "literalsize", "tempsize", "w_compiledin"]
 
     def __init__(self, w_compiledmethod):
         self.bytecode = "".join(w_compiledmethod.bytes)
@@ -723,3 +725,24 @@
         self.bytecodeoffset = w_compiledmethod.bytecodeoffset()
         self.literalsize = w_compiledmethod.getliteralsize()
         self.tempsize = w_compiledmethod.gettempsize()
+
+        self.w_compiledin = None
+        if self.literals:
+            # (Blue book, p 607) All CompiledMethods that contain
+            # extended-super bytecodes have the clain which they are found as
+            # their last literal variable.   
+            # Last of the literals is an association with compiledin
+            # as a class
+            w_association = self.literals[-1]
+            if isinstance(w_association, model.W_PointersObject) and w_association.size() >= 2:
+                # XXX XXX XXX where to get a space from here
+                association = wrapper.AssociationWrapper(None, w_association)
+                self.w_compiledin = association.value()
+
+    def getliteral(self, index):
+        return self.literals[index]
+
+    def getliteralsymbol(self, index):
+        w_literal = self.getliteral(index)
+        assert isinstance(w_literal, model.W_BytesObject)
+        return w_literal.as_string()    # XXX performance issue here
diff --git a/spyvm/test/test_interpreter.py b/spyvm/test/test_interpreter.py
--- a/spyvm/test/test_interpreter.py
+++ b/spyvm/test/test_interpreter.py
@@ -32,7 +32,7 @@
         s_class = w_class.as_class_get_shadow(space)
         prim_meth = model.W_CompiledMethod(0)
         prim_meth.primitive = primnum
-        prim_meth.w_compiledin = w_class
+        # prim_meth.w_compiledin = w_class
         prim_meth.argsize = argsize
         s_class.installmethod(methname, prim_meth)
         
@@ -75,6 +75,7 @@
     w_method.bytes = bytes
     w_method.argsize=2
     w_method.tempsize=8
+    w_method.setliterals([model.W_PointersObject(None, 2)])
     w_frame = w_method.create_frame(space, receiver, ["foo", "bar"])
     interp = interpreter.Interpreter(space)
     interp.store_w_active_context(w_frame)
@@ -151,7 +152,7 @@
                                               pushLiteralConstantBytecode(1) +
                                               pushLiteralConstantBytecode(2)):
     interp = new_interpreter(bytecode)
-    interp.w_active_context().as_methodcontext_get_shadow(space).w_method().literals = fakeliterals(space, "a", "b", "c")
+    interp.w_active_context().as_methodcontext_get_shadow(space).w_method().setliterals(fakeliterals(space, "a", "b", "c"))
     interp.step(interp.s_active_context())
     interp.step(interp.s_active_context())
     interp.step(interp.s_active_context())
@@ -164,7 +165,7 @@
     w_association.store(space, 0, "mykey")
     w_association.store(space, 1, "myvalue")
     interp = new_interpreter(bytecode)
-    interp.w_active_context().as_methodcontext_get_shadow(space).w_method().literals = fakeliterals(space, w_association)
+    interp.w_active_context().as_methodcontext_get_shadow(space).w_method().setliterals( fakeliterals(space, w_association))
     interp.step(interp.s_active_context())
     assert interp.s_active_context().stack() == ["myvalue"]
 
@@ -401,7 +402,7 @@
         w_method.bytes = pushConstantOneBytecode + bytecode
         shadow.installmethod("foo", w_method)
         interp = new_interpreter(bytecodes)
-        interp.w_active_context().as_methodcontext_get_shadow(space).w_method().literals = fakeliterals(space, "foo")
+        interp.w_active_context().as_methodcontext_get_shadow(space).w_method().setliterals(fakeliterals(space, "foo"))
         interp.s_active_context().push(w_object)
         callerContext = interp.w_active_context()
         interp.step(interp.s_active_context())
@@ -428,11 +429,11 @@
     method.bytes = bytecode
     method.argsize = 1
     method.tempsize = 1
-    method.literals = fakeliterals(space, "fib:")
+    method.setliterals(fakeliterals(space, "fib:"))
     shadow.installmethod("fib:", method)
     w_object = shadow.new()
     interp = new_interpreter(sendLiteralSelectorBytecode(16) + returnTopFromMethod)
-    interp.w_active_context().as_methodcontext_get_shadow(space).w_method().literals = fakeliterals(space, "fib:")
+    interp.w_active_context().as_methodcontext_get_shadow(space).w_method().setliterals(fakeliterals(space, "fib:"))
     interp.s_active_context().push(w_object)
     interp.s_active_context().push(space.wrap_int(8))
     result = interp.interpret()
@@ -442,7 +443,7 @@
 
     def test():
         interp = new_interpreter(sendLiteralSelectorBytecode(1 + 16))
-        interp.w_active_context().as_methodcontext_get_shadow(space).w_method().literals = fakeliterals(space, "foo", "sub")
+        interp.w_active_context().as_methodcontext_get_shadow(space).w_method().setliterals(fakeliterals(space, "foo", "sub"))
         interp.s_active_context().push(space.wrap_int(50))
         interp.s_active_context().push(space.wrap_int(8))
         callerContext = interp.w_active_context()
@@ -545,7 +546,7 @@
     w_association.store(space, 0, "mykey")
     w_association.store(space, 1, "myvalue")
     interp = new_interpreter(pushConstantOneBytecode + bytecode)
-    interp.w_active_context().as_methodcontext_get_shadow(space).w_method().literals = fakeliterals(space, w_association)
+    interp.w_active_context().as_methodcontext_get_shadow(space).w_method().setliterals(fakeliterals(space, w_association))
     interp.step(interp.s_active_context())
     interp.step(interp.s_active_context())
     assert w_association.fetch(space, 1).is_same_object(space.w_one)
@@ -609,17 +610,17 @@
     # which does a call to its super
     meth1 = model.W_CompiledMethod(2)
     meth1.bytes = pushReceiverBytecode + bytecode
+    meth1.setliterals(fakeliterals(space, "foo"))
     w_class.as_class_get_shadow(space).installmethod("foo", meth1)
     # and that one again to its super
     meth2 = model.W_CompiledMethod(2)
     meth2.bytes = pushReceiverBytecode + bytecode
+    meth2.setliterals(fakeliterals(space, "foo"))
     w_super.as_class_get_shadow(space).installmethod("foo", meth2)
     meth3 = model.W_CompiledMethod(0)
     w_supersuper.as_class_get_shadow(space).installmethod("foo", meth3)
-    meth1.literals = fakeliterals(space, "foo")
-    meth2.literals = fakeliterals(space, "foo")
     interp = new_interpreter(bytecodes)
-    interp.w_active_context().as_methodcontext_get_shadow(space).w_method().literals = fakeliterals(space, "foo")
+    interp.w_active_context().as_methodcontext_get_shadow(space).w_method().setliterals(fakeliterals(space, "foo"))
     interp.s_active_context().push(w_object)
     interp.step(interp.s_active_context())
     for w_specificclass in [w_super, w_supersuper]:
@@ -665,7 +666,7 @@
 def interpret_bc(bcodes, literals, receiver=space.w_nil):
     bcode = "".join([chr(x) for x in bcodes])
     interp = new_interpreter(bcode, receiver=receiver)
-    interp.w_active_context().as_methodcontext_get_shadow(space).w_method().literals = literals
+    interp.w_active_context().as_methodcontext_get_shadow(space).w_method().setliterals(literals)
     return interp.interpret()
 
 # tests: bytecodePrimValue & bytecodePrimValueWithArg
@@ -804,7 +805,7 @@
     #   ^ self objectAt: 2 put: 3.   changes the first literal to 3
     #   ^ self objectAt: 2.          yields the new first literal (3)
     prim_meth = model.W_CompiledMethod(header=1024)
-    prim_meth.literals = fakeliterals(space, 22)
+    prim_meth.setliterals(fakeliterals(space, 22))
     oal = fakeliterals(space, "objectAt:")
     oalp = fakeliterals(space, "objectAt:put:", 3)
     def test():
@@ -839,4 +840,4 @@
     context.push(fakeliterals("bar"))
     context.push(fakeliterals("baz"))
     interp.step(interp.s_active_context())
-    assert context.pop() == fakeliterals(["egg", "bar", "baz"])
\ No newline at end of file
+    assert context.pop() == fakeliterals(["egg", "bar", "baz"])
diff --git a/spyvm/test/test_model.py b/spyvm/test/test_model.py
--- a/spyvm/test/test_model.py
+++ b/spyvm/test/test_model.py
@@ -81,7 +81,7 @@
     supershadow.installmethod("foo", model.W_CompiledMethod(0))
     classshadow = w_class.as_class_get_shadow(space)
     classshadow.initialize_methoddict()
-    assert classshadow.lookup("foo").w_compiledin is w_super
+    assert classshadow.lookup("foo").as_compiledmethod_get_shadow(space).w_compiledin is w_super
 
 def test_compiledmethod_setchar():
     w_method = model.W_CompiledMethod(3)
@@ -103,7 +103,7 @@
     w_method = model.W_CompiledMethod()
     w_method.bytes = list("abc")
     w_method.header = 100
-    w_method.literals = [ 'lit1', 'lit2' ]
+    w_method.setliterals(['lit1', 'lit2'])
     w_method.literalsize = 2
     assert space.unwrap_int(w_method.at0(space, 0)) == 100
     assert w_method.at0(space, 4) == 'lit1'


More information about the pypy-commit mailing list