Commits

Tobias Pape committed 4da7f6a

Re-import a FixedStack implementation

- fix deprecated overflowcheck w/ shift
- fix r_uints missing

Comments (0)

Files changed (5)

spyvm/fixedstack.py

+"""
+A Fixed stack for SPy.
+"""
+
+import types
+
+from pypy.rlib.rarithmetic import r_uint
+
+class FixedStack(object):
+    _annspecialcase_ = "specialize:ctr_location" # polymorphic
+    
+    def __init__(self):
+        pass
+    
+    def setup(self, stacksize):
+        self.ptr = r_uint(0) # we point after the last element
+        self.items = [None] * stacksize
+    
+    def clone(self):
+        # this is only needed if we support flow space
+        s = self.__class__()
+        s.setup(len(self.items))
+        for item in self.items[:self.ptr]:
+            try:
+                item = item.clone()
+            except AttributeError:
+                pass
+            s.push(item)
+        return s
+    
+    def push(self, item):
+        ptr = self.ptr
+        self.items[ptr] = item
+        self.ptr = ptr + 1
+    
+    def pop(self):
+        ptr = self.ptr - 1
+        ret = self.items[ptr]   # you get OverflowError if the stack is empty
+        self.items[ptr] = None
+        self.ptr = ptr
+        return ret
+    
+    def drop(self, n):
+        while n > 0:
+            n -= 1
+            self.ptr -= 1
+            self.items[self.ptr] = None
+    
+    def top(self, position=0):
+        # for a fixed stack, we assume correct indices
+        rpos = r_uint(position)
+        return self.items[self.ptr + ~rpos]
+    
+    def set_top(self, value, position=0):
+        # for a fixed stack, we assume correct indices
+        rpos = r_uint(position)
+        self.items[self.ptr + ~rpos] = value
+    
+    def depth(self):
+        return self.ptr
+    
+    def empty(self):
+        return not self.ptr
+    
+
         return self.words[n]
         
     def setword(self, n, word):
-        self.words[n] = word        
+        self.words[n] = r_uint(word)
 
     def size(self):
         return len(self.words)   

spyvm/objspace.py

             val = w_value.value
             if val < 0:
                 raise UnwrappingError("got negative integer")
-            return w_value.value
+            return r_uint(w_value.value)
         if isinstance(w_value, model.W_BytesObject):
             # TODO: Completely untested! This failed translation bigtime...
             # XXX Probably we want to allow all subclasses

spyvm/primitives.py

     # left shift, must fail if we lose bits beyond 32
     if argument > 0:
         try:
-            shifted = rarithmetic.ovfcheck_lshift(receiver, argument)
+            shifted = rarithmetic.ovfcheck(receiver << argument)
         except OverflowError:
             raise PrimitiveFailedError()
         return interp.space.wrap_int(shifted)
     __metaclass__ = extendabletype
 
     def __init__(self, space, w_self):
-        from pypy.interpreter.miscutils import FixedStack
+        from spyvm.fixedstack import FixedStack
 
         self._w_sender = space.w_nil
         self._stack = FixedStack()