Author: Richard Plangger <planri...@gmail.com>
Branch: s390x-backend
Changeset: r82621:53b6b15a4a97
Date: 2016-03-01 08:05 +0100
http://bitbucket.org/pypy/pypy/changeset/53b6b15a4a97/

Log:    list append, insert, del. indexing functions do not generate byte
        code if IndexError would be raised

diff --git a/rpython/jit/backend/llsupport/tl/code.py 
b/rpython/jit/backend/llsupport/tl/code.py
--- a/rpython/jit/backend/llsupport/tl/code.py
+++ b/rpython/jit/backend/llsupport/tl/code.py
@@ -14,14 +14,16 @@
 
 LIST_TYP = 'l'
 INT_TYP = 'i'
+OBJ_TYP = 'o'
+STR_TYP = 's'
+VAL_TYP = 'v' # either one of the earlier
+
+all_types = [INT_TYP, LIST_TYP, STR_TYP] # TODO OBJ_TYP
+
 SHORT_TYP = 'h'
 BYTE_TYP = 'b'
-OBJ_TYP = 'o'
-STR_TYP = 's'
 COND_TYP = 'c'
-VAL_TYP = 'v' # either one of the earlier
-
-all_types = [INT_TYP, LIST_TYP, STR_TYP]
+IDX_TYP = 'x'
 
 
 def unique_code():
@@ -146,6 +148,27 @@
         ctx.append_byte(self.BYTE_CODE)
         ctx.append_short(self.size)
 
+@requires_stack(LIST_TYP, IDX_TYP, INT_TYP) # TODO VAL_TYP
+@leaves_on_stack(LIST_TYP)
+class InsertList(ByteCode):
+    BYTE_CODE = unique_code()
+    def __init__(self):
+        pass
+
+@requires_stack(LIST_TYP, IDX_TYP)
+@leaves_on_stack(LIST_TYP)
+class DelList(ByteCode):
+    BYTE_CODE = unique_code()
+    def __init__(self):
+        pass
+
+@requires_stack(LIST_TYP, INT_TYP) # TODO VAL_TYP)
+@leaves_on_stack(LIST_TYP)
+class AppendList(ByteCode):
+    BYTE_CODE = unique_code()
+    def __init__(self):
+        pass
+
 
 # remove comment one by one!
 
@@ -176,33 +199,6 @@
 #        pass
 #
 
-#@requires_stack(LIST_TYP, INT_TYP, INT_TYP) # TODO VAL_TYP
-#class InsertList(ByteCode):
-#    BYTE_CODE = unique_code()
-#    @requires_param(INT_TYP)
-#    def __init__(self, index):
-#        self.index = index
-#    def encode(self, ctx):
-#        ctx.append_byte(self.BYTE_CODE)
-#        ctx.append_int(self.index)
-#
-#@requires_stack(LIST_TYP, INT_TYP)
-#@leaves_on_stack(LIST_TYP)
-#class DelList(ByteCode):
-#    BYTE_CODE = unique_code()
-#    @requires_param(INT_TYP)
-#    def __init__(self, index):
-#        self.index = index
-#    def encode(self, ctx):
-#        ctx.append_byte(self.BYTE_CODE)
-#        ctx.append_int(self.index)
-#
-#@requires_stack(LIST_TYP, INT_TYP, INT_TYP) # TODO VAL_TYP)
-#class AppendList(ByteCode):
-#    BYTE_CODE = unique_code()
-#    def __init__(self):
-#        pass
-#
 #@requires_stack(LIST_TYP)
 #@leaves_on_stack(LIST_TYP, INT_TYP)
 #class LenList(ByteCode):
diff --git a/rpython/jit/backend/llsupport/tl/interp.py 
b/rpython/jit/backend/llsupport/tl/interp.py
--- a/rpython/jit/backend/llsupport/tl/interp.py
+++ b/rpython/jit/backend/llsupport/tl/interp.py
@@ -84,6 +84,21 @@
         size = runpack('h', bytecode[i+1:i+3])
         stack.append(space.wrap([None] * size))
         i += 2
+    elif opcode == code.AppendList.BYTE_CODE:
+        w_val = stack.pop()
+        w_lst = stack.peek(0)
+        w_lst.items.append(w_val)
+    elif opcode == code.InsertList.BYTE_CODE:
+        w_val = stack.pop()
+        w_idx = stack.pop()
+        w_lst = stack.peek(0)
+        w_lst.items[w_idx.value] = w_val
+        # index error, just crash here!
+    elif opcode == code.DelList.BYTE_CODE:
+        w_idx = stack.pop()
+        w_lst = stack.peek(0)
+        del w_lst.items[w_idx.value]
+        # index error, just crash the machine!!
     else:
         raise NotImplementedError
     return i + 1
diff --git a/rpython/jit/backend/llsupport/tl/stack.py 
b/rpython/jit/backend/llsupport/tl/stack.py
--- a/rpython/jit/backend/llsupport/tl/stack.py
+++ b/rpython/jit/backend/llsupport/tl/stack.py
@@ -17,6 +17,12 @@
         self.stack[self.stackpos] = elem
         self.stackpos += 1
 
+    def peek(self, i):
+        stackpos = self.stackpos - i - 1
+        if stackpos < 0:
+            raise IndexError
+        return self.stack[stackpos]
+
     def pop(self):
         stackpos = self.stackpos - 1
         if stackpos < 0:
diff --git a/rpython/jit/backend/llsupport/tl/test/code_strategies.py 
b/rpython/jit/backend/llsupport/tl/test/code_strategies.py
--- a/rpython/jit/backend/llsupport/tl/test/code_strategies.py
+++ b/rpython/jit/backend/llsupport/tl/test/code_strategies.py
@@ -1,15 +1,18 @@
 from hypothesis import strategies as st
+from hypothesis.control import assume
 from hypothesis.strategies import defines_strategy, composite
 from rpython.jit.backend.llsupport.tl import code, interp, stack
 from rpython.jit.backend.llsupport.tl.code import (all_types,
         INT_TYP, STR_TYP, LIST_TYP, SHORT_TYP, BYTE_TYP,
-        COND_TYP)
+        COND_TYP, IDX_TYP)
 from hypothesis.searchstrategy.strategies import OneOfStrategy
 from hypothesis.searchstrategy.collections import TupleStrategy
 
 def get_strategy_for(typ):
     if typ == INT_TYP:
         return st.integers(min_value=-2**31, max_value=2**31-1)
+    elif typ == IDX_TYP:
+        return st.integers(min_value=-2**31, max_value=2**31-1)
     elif typ == SHORT_TYP:
         return st.integers(min_value=-2**15, max_value=2**15-1)
     elif typ == BYTE_TYP:
@@ -23,21 +26,23 @@
     else:
         raise NotImplementedError("type: " + str(typ))
 
-@defines_strategy
-def wrapped_tl_objects(self, types=all_types):
-    if len(types) == 1:
-        return get_strategy_for(types[0])
-    return OneOfStrategy([get_strategy_for(t) for t in types])
-
 STD_SPACE = interp.Space()
 
 @composite
 def runtime_stack(draw, clazz):
     strats = [get_strategy_for(t) for t in clazz._stack_types]
-    st = stack.Stack(len(strats))
-    for strat in strats:
-        st.append(STD_SPACE.wrap(draw(strat)))
-    return st
+    stack_obj = stack.Stack(len(strats))
+    for i,strat in enumerate(strats):
+        if clazz._stack_types[i] == IDX_TYP:
+            # it is only valid to access a list with a valid index!
+            w_list = stack_obj.peek(i-1)
+            l = len(w_list.items)
+            assume(l > 0)
+            integrals = st.integers(min_value=0, max_value=l-1)
+            stack_obj.append(STD_SPACE.wrap(draw(integrals)))
+            continue
+        stack_obj.append(STD_SPACE.wrap(draw(strat)))
+    return stack_obj
 
 def byte_code_classes():
     for name, clazz in code.__dict__.items():
@@ -45,9 +50,9 @@
             yield clazz
 
 @composite
-def single_bytecode(draw, clazzes=st.sampled_from(byte_code_classes()),
-                    integrals=st.integers(),
-                    texts=st.text()):
+def single_bytecode(draw,
+        clazzes=st.sampled_from(byte_code_classes()),
+        integrals=st.integers(), texts=st.text()):
     clazz = draw(clazzes)
     inst = clazz.create_from(draw, get_strategy_for)
     bytecode, consts = code.Context().transform([inst])
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to