[pypy-svn] r54773 - in pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk: . test

tverwaes at codespeak.net tverwaes at codespeak.net
Fri May 16 09:26:22 CEST 2008


Author: tverwaes
Date: Fri May 16 09:26:21 2008
New Revision: 54773

Modified:
   pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/classtable.py
   pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/constants.py
   pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/interpreter.py
   pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/model.py
   pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/objtable.py
   pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/primitives.py
   pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/shadow.py
   pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/squeakimage.py
   pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/test/test_interpreter.py
   pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/test/test_miniimage.py
   pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/test/test_shadow.py
Log:
bugfixes


Modified: pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/classtable.py
==============================================================================
--- pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/classtable.py	(original)
+++ pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/classtable.py	Fri May 16 09:26:21 2008
@@ -129,6 +129,6 @@
 define_cls("w_LinkedList", "w_SequenceableCollection")
 define_cls("w_Semaphore", "w_LinkedList")
 define_cls("w_BlockContext", "w_ContextPart",
-           instvarsize=constants.BLKCTX_TEMP_FRAME_START)
+           instvarsize=constants.BLKCTX_STACK_START)
 
 copy_in_globals_classes_known_to_the_vm()

Modified: pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/constants.py
==============================================================================
--- pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/constants.py	(original)
+++ pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/constants.py	Fri May 16 09:26:21 2008
@@ -59,7 +59,7 @@
 BLKCTX_BLOCK_ARGUMENT_COUNT_INDEX = 3
 BLKCTX_INITIAL_IP_INDEX = 4
 BLKCTX_HOME_INDEX = 5
-BLKCTX_TEMP_FRAME_START = 6
+BLKCTX_STACK_START = 6
 
 # MethodContext < ContextPart
 MTHDCTX_METHOD = 3

Modified: pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/interpreter.py
==============================================================================
--- pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/interpreter.py	(original)
+++ pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/interpreter.py	Fri May 16 09:26:21 2008
@@ -1,6 +1,6 @@
 import py
 from pypy.lang.smalltalk.shadow import Invalidateable
-from pypy.lang.smalltalk.shadow import ContextPartShadow
+from pypy.lang.smalltalk.shadow import ContextPartShadow, MethodContextShadow, BlockContextShadow
 from pypy.lang.smalltalk import model, constants, primitives
 from pypy.lang.smalltalk import objtable
 from pypy.lang.smalltalk.shadow import ContextPartShadow
@@ -38,6 +38,10 @@
         return self._w_active_context
 
     def store_w_active_context(self, w_context):
+        # We can only interpret contexts of which we know the type already
+        s_ctx = w_context.as_context_get_shadow()
+        assert (isinstance(s_ctx, MethodContextShadow) or 
+                isinstance(s_ctx, BlockContextShadow))
         if self._s_active_context is not None:
             self._s_active_context.unnotify(self)
             self._s_active_context = None
@@ -69,6 +73,7 @@
         # translating the interpreter
         if not objectmodel.we_are_translated():
             bytecodeimpl = BYTECODE_TABLE[next]
+
             if self.should_trace():
                 if self._w_last_active_context != self.w_active_context():
                     cnt = 0
@@ -76,7 +81,9 @@
                     # AK make method
                     while p is not objtable.w_nil:
                         cnt += 1
-                        p = p.as_context_get_shadow().w_sender()
+                                                  # Do not update the context
+                                                  # for this action.
+                        p = p.as_context_get_shadow(False).w_sender()
                     self._last_indent = "  " * cnt
                     self._w_last_active_context = self.w_active_context()
 
@@ -87,7 +94,9 @@
                     self._last_indent,
                     self.s_active_context().pc(),
                     next, bytecodeimpl.__name__,)
+
             bytecodeimpl(self.s_active_context(), self)
+
         else:
             # this is a performance optimization: when translating the
             # interpreter, the bytecode dispatching is not implemented as a

Modified: pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/model.py
==============================================================================
--- pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/model.py	(original)
+++ pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/model.py	Fri May 16 09:26:21 2008
@@ -310,9 +310,9 @@
         from pypy.lang.smalltalk.shadow import MethodContextShadow
         return self.as_special_get_shadow(MethodContextShadow, invalid)
 
-    def as_context_get_shadow(self):
+    def as_context_get_shadow(self, invalid=True):
         from pypy.lang.smalltalk.shadow import ContextPartShadow
-        return self.as_special_get_shadow(ContextPartShadow)
+        return self.as_special_get_shadow(ContextPartShadow, invalid)
 
     def as_methoddict_get_shadow(self):
         from pypy.lang.smalltalk.shadow import MethodDictionaryShadow
@@ -458,9 +458,12 @@
 
     def __str__(self):
         from pypy.lang.smalltalk.interpreter import BYTECODE_TABLE
-        return ("\n\nBytecode:\n---------------------\n" +
-                "\n".join([BYTECODE_TABLE[ord(i)].__name__ + " " + str(ord(i)) for i in self.bytes]) +
-                "\n---------------------\n")
+        j = 1
+        retval = "\n\nBytecode:\n---------------------\n"
+        for i in self.bytes:
+            retval += str(j) + ": " + BYTECODE_TABLE[ord(i)].__name__ + " " + str(ord(i)) + "\n"
+            j += 1
+        return retval + "\n---------------------\n"
 
     def invariant(self):
         return (W_Object.invariant(self) and
@@ -481,6 +484,9 @@
     def getliteralsize(self):
         return self.literalsize * constants.BYTES_PER_WORD
 
+    def bytecodeoffset(self):
+        return self.getliteralsize() + self.headersize()
+
     def headersize(self):
         return constants.BYTES_PER_WORD
 
@@ -510,6 +516,9 @@
         self.w_compiledin = None
         self.islarge = islarge
 
+    def tempframesize(self):
+        return self.argsize + self.tempsize
+
     def literalat0(self, index0):
         if index0 == 0:
             from pypy.lang.smalltalk import utility

Modified: pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/objtable.py
==============================================================================
--- pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/objtable.py	(original)
+++ pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/objtable.py	Fri May 16 09:26:21 2008
@@ -33,7 +33,7 @@
 
 objtable = {}
 # XXX Used for non-PyPy way of doing become.
-#objects = []
+objects = []
 
 for name in constants.objects_in_special_object_table:
     name = "w_" + name

Modified: pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/primitives.py
==============================================================================
--- pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/primitives.py	(original)
+++ pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/primitives.py	Fri May 16 09:26:21 2008
@@ -502,9 +502,11 @@
 
 @expose_primitive(BECOME, unwrap_spec=[object, object])
 def func(interp, w_rcvr, w_new):
-#    for w_object in objtable.objects:
-#        w_object.become(w_rcvr, w_new)
-    raise PrimitiveNotYetWrittenError
+# Non-PyPy way of doing become
+    for w_object in objtable.objects:
+        w_object.become(w_rcvr, w_new)
+    return w_rcvr
+    #raise PrimitiveNotYetWrittenError
 
 def fake_bytes_left():
     return utility.wrap_int(2**20) # XXX we don't know how to do this :-(

Modified: pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/shadow.py
==============================================================================
--- pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/shadow.py	(original)
+++ pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/shadow.py	Fri May 16 09:26:21 2008
@@ -188,9 +188,9 @@
         else:
             raise NotImplementedError(self.instance_kind)
         # XXX Used for non-PyPy way of doing become.
-        #if store:
-        #    from pypy.lang.smalltalk import objtable
-        #    objtable.objects.extend([w_new])
+        if store:
+            from pypy.lang.smalltalk import objtable
+            objtable.objects.extend([w_new])
         return w_new
 
     # _______________________________________________________________
@@ -242,8 +242,8 @@
             try:
                 w_method = look_in_shadow.methoddict[selector]
                 # We locally cache the method we found.
-                if look_in_shadow is not self:
-                    self.methoddict[selector] = w_method
+                #if look_in_shadow is not self:
+                #    self.methoddict[selector] = w_method
                 return w_method
             except KeyError, e:
                 look_in_shadow = look_in_shadow.s_superclass
@@ -329,7 +329,7 @@
 class SemaphoreShadow(LinkedListShadow):
     """A shadow for Smalltalk objects that are semaphores
     """
-    def __init__(self, w_self, invalid):
+    def __init__(self, w_self, invalid=False):
         LinkedListShadow.__init__(self, w_self, invalid)
 
     def put_to_sleep(self, s_process):
@@ -450,20 +450,29 @@
     def update_shadow(self):
         AbstractShadow.update_shadow(self)
         self._stack = [self.w_self()._vars[i]
-                        for i in range(self.stackstart() + 1,
-                                       self.stackpointer() + 1)]
-        self._pc = utility.unwrap_int(self.w_self()._vars[constants.CTXPART_PC_INDEX])
-        self._pc -= 1 + self.w_method().getliteralsize()
+                        for i in range(self.stackstart(),
+                                       self.stackpointer())]
+        self.init_pc()
         # Using a shadow most likely results in an invalid state for w_self
         self.invalidate_w_self()
 
+    def init_pc(self):
+        self._pc = utility.unwrap_int(self.w_self()._vars[constants.CTXPART_PC_INDEX])
+        self._pc -= self.w_method().bytecodeoffset()
+        self._pc -= 1
+
+    def save_back_pc(self):
+        pc = self._pc
+        pc += 1
+        pc += self.w_method().bytecodeoffset()
+        self.w_self()._vars[constants.CTXPART_PC_INDEX] = utility.wrap_int(pc)
+
     def update_w_self(self):
         AbstractShadow.update_w_self(self)
         for i in range(len(self._stack)):
-            self.w_self()._vars[self.stackstart() + 1 + i] = self._stack[i]
-        self.store_stackpointer(len(self._stack) + self.stackstart())
-        self.w_self()._vars[constants.CTXPART_PC_INDEX] = utility.wrap_int(self._pc + 1 +
-                                                                           self.w_method().getliteralsize())
+            self.w_self()._vars[self.stackstart() + i] = self._stack[i]
+        self.store_stackpointer(len(self._stack))
+        self.save_back_pc()
 
     def __init__(self, w_self, invalid):
         AbstractShadow.__init__(self, w_self, invalid)
@@ -477,6 +486,9 @@
     def stackstart(self):
         raise NotImplementedError()
 
+    def stackpointer_offset(self):
+        raise NotImplementedError()
+
     def w_receiver(self):
         " Return self of the method, or the method that contains the block "
         return self.s_home().w_receiver()
@@ -504,7 +516,11 @@
         self._pc = newpc
 
     def stackpointer(self):
-        return utility.unwrap_int(self.w_self()._vars[constants.CTXPART_STACKP_INDEX])
+        return (utility.unwrap_int(self.w_self()._vars[constants.CTXPART_STACKP_INDEX]) +
+                self.stackpointer_offset())
+
+    def stackpointer_offset(self):
+        raise NotImplementedError()
 
     def store_stackpointer(self, pointer):
         self.w_self()._vars[constants.CTXPART_STACKP_INDEX] = utility.wrap_int(pointer)
@@ -630,10 +646,12 @@
 
     def reset_stack(self):
         self._stack = []
-        
+
     def stackstart(self):
-        return (constants.BLKCTX_TEMP_FRAME_START +
-                self.expected_argument_count())
+        return constants.BLKCTX_STACK_START
+
+    def stackpointer_offset(self):
+        return constants.BLKCTX_STACK_START
 
 class MethodContextShadow(ContextPartShadow):
     def __init__(self, w_self, invalid):
@@ -661,11 +679,11 @@
     def store_w_receiver(self, w_receiver):
         self.w_self()._vars[constants.MTHDCTX_RECEIVER] = w_receiver
 
-    def gettemp(self, index):
-        return self.w_self()._vars[constants.MTHDCTX_TEMP_FRAME_START + index]
+    def gettemp(self, index0):
+        return self.w_self()._vars[constants.MTHDCTX_TEMP_FRAME_START + index0]
 
-    def settemp(self, index, w_value):
-        self.w_self()._vars[constants.MTHDCTX_TEMP_FRAME_START + index] = w_value
+    def settemp(self, index0, w_value):
+        self.w_self()._vars[constants.MTHDCTX_TEMP_FRAME_START + index0] = w_value
 
     def w_home(self):
         return self.w_self()
@@ -673,8 +691,14 @@
     def s_home(self):
         return self
 
+    def store_stackpointer(self, pointer):
+        ContextPartShadow.store_stackpointer(self,
+                                             pointer+self.w_method().tempframesize())
+
+    def stackpointer_offset(self):
+        return constants.MTHDCTX_TEMP_FRAME_START
+
     def stackstart(self):
         w_method = self.w_method()
         return (constants.MTHDCTX_TEMP_FRAME_START +
-                w_method.argsize +
-                w_method.tempsize)
+                w_method.tempframesize())

Modified: pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/squeakimage.py
==============================================================================
--- pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/squeakimage.py	(original)
+++ pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/squeakimage.py	Fri May 16 09:26:21 2008
@@ -204,9 +204,9 @@
 
         from pypy.lang.smalltalk import objtable
         # XXX Used for non-PyPy way of doing become.
-        #objtable.objects.extend(
-        #    [chunk.g_object.w_object
-        #        for chunk in reader.chunklist])
+        objtable.objects.extend(
+            [chunk.g_object.w_object
+                for chunk in reader.chunklist])
         for name, idx in constants.objects_in_special_object_table.items():
             objtable.objtable["w_" + name] = self.special_objects[idx]
 

Modified: pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/test/test_interpreter.py
==============================================================================
--- pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/test/test_interpreter.py	(original)
+++ pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/test/test_interpreter.py	Fri May 16 09:26:21 2008
@@ -799,3 +799,12 @@
         [[classtable.w_CompiledMethod, primitives.OBJECT_AT, 1, "objectAt:"],
          [classtable.w_CompiledMethod, primitives.OBJECT_AT_PUT, 2, "objectAt:put:"]],
         test)
+
+def test_runwithtrace():
+    # We run random tests with the bc_trace option turned on explicitely
+    from pypy.lang.smalltalk.conftest import option
+    bc_trace = option.bc_trace
+    option.bc_trace = True
+    test_storeAndPopReceiverVariableBytecode()
+    test_bc_objectAtAndAtPut()
+    option.bc_trace = bc_trace

Modified: pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/test/test_miniimage.py
==============================================================================
--- pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/test/test_miniimage.py	(original)
+++ pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/test/test_miniimage.py	Fri May 16 09:26:21 2008
@@ -227,19 +227,31 @@
     assert w_false is objtable.w_false
     
 def test_runimage():
-    py.test.skip("This method actually runs an image. Fails since no graphical primitives yet")
+    #py.test.skip("This method actually runs an image. Fails since no graphical primitives yet")
     from pypy.lang.smalltalk.shadow import SemaphoreShadow
     s_semaphore = SemaphoreShadow(None)
     s_scheduler = s_semaphore.s_scheduler()
     s_ap = s_scheduler.s_active_process()
-    s_ctx = s_ap.s_suspended_context()
+    s_ctx = s_ap.w_suspended_context().as_methodcontext_get_shadow()
     s_ap.store_w_suspended_context(objtable.w_nil)
+
+    print "BOE"
+    print [x for x in enumerate(s_ctx.w_self()._vars)]
+    print s_ctx.stackstart()
+    print s_ctx.stackpointer()
+
+    # XXX Important
+    # Push return value of snapshot primitive
+    # The interpreter must resume at the very moment the snapshot primitive
+    # returns.
+    s_ctx._stack = [objtable.w_true]
+
     interp = interpreter.Interpreter()
     interp.store_w_active_context(s_ctx.w_self())
     interp.interpret()
     
 def test_compile_method():
-    py.test.skip("fails again because of the removed become. should work with new pypy become")
+    #py.test.skip("fails again because of the removed become. should work with new pypy become")
     sourcecode = """fib 
                         ^self < 2 
                             ifTrue: [ 1 ] 

Modified: pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/test/test_shadow.py
==============================================================================
--- pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/test/test_shadow.py	(original)
+++ pypy/branch/smalltalk-shadow-changes/pypy/lang/smalltalk/test/test_shadow.py	Fri May 16 09:26:21 2008
@@ -88,11 +88,10 @@
 
 def methodcontext(w_sender=objtable.w_nil, pc=1, stackpointer=0, stacksize=5,
                   method=method()):
-    stackstart = 6+method.argsize+method.tempsize # (len notation, not idx notation)
-    w_object = model.W_PointersObject(classtable.w_MethodContext, stackstart+stacksize)
+    w_object = model.W_PointersObject(classtable.w_MethodContext, constants.MTHDCTX_TEMP_FRAME_START+method.tempframesize()+stacksize)
     w_object.store(constants.CTXPART_SENDER_INDEX, w_sender)
     w_object.store(constants.CTXPART_PC_INDEX, utility.wrap_int(pc))
-    w_object.store(constants.CTXPART_STACKP_INDEX, utility.wrap_int(stackstart+stackpointer))
+    w_object.store(constants.CTXPART_STACKP_INDEX, utility.wrap_int(method.tempframesize()+stackpointer))
     w_object.store(constants.MTHDCTX_METHOD, method)
     # XXX
     w_object.store(constants.MTHDCTX_RECEIVER_MAP, '???')
@@ -119,12 +118,12 @@
     assert s_object2.gettemp(0) == 'a'
     assert s_object.w_method() == w_m
     idx = s_object.stackstart()
-    w_object.store(idx + 1, 'f')
-    w_object.store(idx + 2, 'g')
-    w_object.store(idx + 3, 'h')
+    w_object.store(idx, 'f')
+    w_object.store(idx + 1, 'g')
+    w_object.store(idx + 2, 'h')
     s_object.update_shadow()
-    assert s_object.top() == 'h'
     assert s_object.stack() == ['f', 'g', 'h' ]
+    assert s_object.top() == 'h'
     s_object.push('i')
     assert s_object.top() == 'i'
     assert s_object.peek(1) == 'h'



More information about the Pypy-commit mailing list