Author: Richard Plangger <[email protected]>
Branch: s390x-backend
Changeset: r82610:061da05db893
Date: 2016-02-29 14:27 +0100
http://bitbucket.org/pypy/pypy/changeset/061da05db893/

Log:    renamed module, first hypothesis test that ensures that each interp
        function works as expected

diff --git a/.hgignore b/.hgignore
--- a/.hgignore
+++ b/.hgignore
@@ -94,5 +94,6 @@
 ^pypy/module/test_lib_pypy/ctypes_tests/_ctypes_test\.o$
 ^compiled
 ^.git/
+^.hypothesis/
 ^release/
 ^rpython/_cache$
diff --git a/rpython/jit/backend/llsupport/tl/__init__.py 
b/rpython/jit/backend/llsupport/tl/__init__.py
new file mode 100644
diff --git a/rpython/jit/backend/llsupport/tl/code.py 
b/rpython/jit/backend/llsupport/tl/code.py
new file mode 100644
--- /dev/null
+++ b/rpython/jit/backend/llsupport/tl/code.py
@@ -0,0 +1,219 @@
+
+import struct
+
+class ByteCode(object):
+    def encode(self, ctx):
+        ctx.append_byte(self.BYTE_CODE)
+
+    @classmethod
+    def create_from(self, draw, get_strategy_for):
+        pt = getattr(self.__init__, '_param_types', [])
+        return self(*[draw(get_strategy_for(t)) for t in pt])
+
+_c = 0
+
+LIST_TYP = 'l'
+INT_TYP = 'i'
+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]
+
+
+def unique_code():
+    global _c
+    v = _c
+    _c = v + 1
+    return v
+
+class Context(object):
+    def __init__(self):
+        self.consts = {}
+        self.const_idx = 0
+        self.bytecode = []
+
+    def append_byte(self, byte):
+        self.bytecode.append(('b', byte))
+
+    def get_byte(self, i):
+        typ, byte = self.bytecode[i]
+        assert typ == 'b'
+        return byte
+
+    def get_short(self, i):
+        typ, int = self.bytecode[i]
+        assert typ == 'h'
+        return int
+
+    def append_short(self, byte):
+        self.bytecode.append(('h', byte))
+
+    def append_int(self, byte):
+        self.bytecode.append(('i', byte))
+
+    def const_str(self, str):
+        self.consts[self.const_idx] = str
+        self.append_short(self.const_idx)
+        self.const_idx += 1
+
+    def to_string(self):
+        code = []
+        for typ, nmr in self.bytecode:
+            code.append(struct.pack(typ, nmr))
+        return ''.join(code)
+
+    def transform(self, code_objs):
+        for code_obj in code_objs:
+            code_obj.encode(self)
+
+        return self.to_string(), self.consts
+
+
+def requires_stack(*types):
+    def method(clazz):
+        clazz._stack_types = tuple(types)
+        return clazz
+    return method
+
+def leaves_on_stack(*types):
+    def method(clazz):
+        clazz._return_on_stack_types = tuple(types)
+        return clazz
+    return method
+
+
+def requires_param(*types):
+    def method(m):
+        m._param_types = tuple(types)
+        return m
+    return method
+
+@requires_stack()
+@leaves_on_stack(INT_TYP)
+class PutInt(ByteCode):
+    BYTE_CODE = unique_code()
+    @requires_param(INT_TYP)
+    def __init__(self, value):
+        self.integral = value
+    def encode(self, ctx):
+        ctx.append_byte(self.BYTE_CODE)
+        ctx.append_int(self.integral)
+
+@requires_stack(INT_TYP, INT_TYP)
+@leaves_on_stack(INT_TYP)
+class CompareInt(ByteCode):
+    BYTE_CODE = unique_code()
+    def __init__(self):
+        pass
+
+@requires_stack()
+@leaves_on_stack(STR_TYP)
+class LoadStr(ByteCode):
+    BYTE_CODE = unique_code()
+    @requires_param(STR_TYP)
+    def __init__(self, string):
+        self.string = string
+    def encode(self, ctx):
+        ctx.append_byte(self.BYTE_CODE)
+        ctx.const_str(self.string)
+
+@requires_stack(STR_TYP, STR_TYP)
+@leaves_on_stack(STR_TYP)
+class AddStr(ByteCode):
+    BYTE_CODE = unique_code()
+    def __init__(self):
+        pass
+
+# remove comment one by one!
+
+#@requires_stack()
+#@leaves_on_stack(INT_TYP)
+#class CondJump(ByteCode):
+#    BYTE_CODE = unique_code()
+#
+#    COND_EQ = 0
+#    COND_LT = 1
+#    COND_GT = 2
+#    COND_LE = 3
+#    COND_GE = 4
+#
+#    @requires_param(COND_TYP)
+#    def __init__(self, cond):
+#        self.cond = cond
+#
+#    def encode(self, ctx):
+#        ctx.append_byte(self.BYTE_CODE)
+#        ctx.append_byte(self.cond)
+#
+#@requires_stack()
+#@leaves_on_stack()
+#class Jump(ByteCode):
+#    BYTE_CODE = unique_code()
+#    def __init__(self):
+#        pass
+#
+
+#
+#@requires_stack(LIST_TYP, LIST_TYP)
+#@leaves_on_stack(LIST_TYP)
+#class AddList(ByteCode):
+#    BYTE_CODE = unique_code()
+#    def __init__(self):
+#        pass
+#
+#@requires_stack()
+#class CreateList(ByteCode):
+#    BYTE_CODE = unique_code()
+#    @requires_param(BYTE_TYP)
+#    def __init__(self, size=8):
+#        self.size = size
+#    def encode(self, ctx):
+#        ctx.append_byte(self.BYTE_CODE)
+#        ctx.append_short(self.size)
+#
+#@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):
+#    BYTE_CODE = unique_code()
+#    def __init__(self):
+#        pass
+#
+#
+#@requires_stack(INT_TYP) # TODO VAL_TYP)
+#@leaves_on_stack()
+#class ReturnFrame(ByteCode):
+#    BYTE_CODE = unique_code()
+#    def __init__(self):
+#        pass
+#
diff --git a/rpython/jit/backend/llsupport/tl/interp.py 
b/rpython/jit/backend/llsupport/tl/interp.py
new file mode 100644
--- /dev/null
+++ b/rpython/jit/backend/llsupport/tl/interp.py
@@ -0,0 +1,75 @@
+from rpython.rlib.rstruct.runpack import runpack
+from rpython.rlib.objectmodel import specialize, always_inline
+from rpython.jit.backend.llsupport.tl import code, stack
+
+class W_Root(object):
+    pass
+
+class W_ListObject(W_Root):
+    def __init__(self):
+        self.items = []
+
+class W_IntObject(W_Root):
+    def __init__(self, value):
+        self.value = value
+
+    def compare(self, w_int):
+        assert isinstance(w_int, W_IntObject)
+        return cmp(self.value, w_int.value)
+
+class W_StrObject(W_Root):
+    def __init__(self, value):
+        self.value = value
+
+    def concat(self, w_str):
+        assert isinstance(w_str, W_StrObject)
+        return self.value + w_str.value
+
+class Space(object):
+    @specialize.argtype(1)
+    def wrap(self, val):
+        if isinstance(val, W_Root):
+            return val
+        if isinstance(val, int):
+            return W_IntObject(val)
+        if isinstance(val, str):
+            return W_StrObject(val)
+        if isinstance(val, unicode):
+            return W_StrObject(val.encode('utf-8'))
+        raise NotImplementedError("cannot handle: " + str(val) + 
str(type(val)))
+
+def entry_point(argv):
+    bytecode = argv[0]
+    pc = 0
+    end = len(bytecode)
+    stack = Stack(16)
+    space = space.Space()
+    consts = []
+    while i < end:
+        i = dispatch_once(space, i, bytecode, consts, stack)
+    return 0
+
+@always_inline
+def dispatch_once(space, i, bytecode, consts, stack):
+    opcode = ord(bytecode[i])
+    if opcode == code.PutInt.BYTE_CODE:
+        integral = runpack('i', bytecode[i+1:i+5])
+        stack.append(space.wrap(integral))
+        i += 4
+    elif opcode == code.CompareInt.BYTE_CODE:
+        w_int2 = stack.pop()
+        w_int1 = stack.pop()
+        w_int3 = space.wrap(w_int1.compare(w_int2))
+        stack.append(w_int3)
+    elif opcode == code.LoadStr.BYTE_CODE:
+        pos = runpack('h', bytecode[i+1:i+3])
+        w_str = space.wrap(consts[pos])
+        stack.append(w_str)
+        i += 2
+    elif opcode == code.AddStr.BYTE_CODE:
+        w_str2 = stack.pop()
+        w_str1 = stack.pop()
+        stack.append(space.wrap(w_str1.concat(w_str2)))
+    else:
+        raise NotImplementedError
+    return i + 1
diff --git a/rpython/jit/backend/llsupport/tl/stack.py 
b/rpython/jit/backend/llsupport/tl/stack.py
new file mode 100644
--- /dev/null
+++ b/rpython/jit/backend/llsupport/tl/stack.py
@@ -0,0 +1,60 @@
+from rpython.rlib.jit import JitDriver, hint, dont_look_inside, promote
+
+class Stack(object):
+    _virtualizable_ = ['stackpos', 'stack[*]']
+
+    def __init__(self, size):
+        self = hint(self, access_directly=True, fresh_virtualizable=True)
+        self.stack = [0] * size
+        self.stackpos = 0        # always store a known-nonneg integer here
+
+    def size(self):
+        return self.stackpos
+
+    def append(self, elem):
+        while len(self.stack) <= self.stackpos:
+            self.stack.append(None)
+        self.stack[self.stackpos] = elem
+        self.stackpos += 1
+
+    def pop(self):
+        stackpos = self.stackpos - 1
+        if stackpos < 0:
+            raise IndexError
+        self.stackpos = stackpos     # always store a known-nonneg integer here
+        return self.stack[stackpos]
+
+    def pick(self, i):
+        n = self.stackpos - i - 1
+        assert n >= 0
+        self.append(self.stack[n])
+
+    def put(self, i):
+        elem = self.pop()
+        n = self.stackpos - i - 1
+        assert n >= 0
+        self.stack[n] = elem
+
+    @dont_look_inside
+    def roll(self, r):
+        if r < -1:
+            i = self.stackpos + r
+            if i < 0:
+                raise IndexError
+            n = self.stackpos - 1
+            assert n >= 0
+            elem = self.stack[n]
+            for j in range(self.stackpos - 2, i - 1, -1):
+                assert j >= 0
+                self.stack[j + 1] = self.stack[j]
+            self.stack[i] = elem
+        elif r > 1:
+            i = self.stackpos - r
+            if i < 0:
+                raise IndexError
+            elem = self.stack[i]
+            for j in range(i, self.stackpos - 1):
+                self.stack[j] = self.stack[j + 1]
+            n = self.stackpos - 1
+            assert n >= 0
+            self.stack[n] = elem
diff --git a/rpython/jit/backend/llsupport/tl/test/__init__.py 
b/rpython/jit/backend/llsupport/tl/test/__init__.py
new file mode 100644
diff --git a/rpython/jit/backend/llsupport/tl/test/code_strategies.py 
b/rpython/jit/backend/llsupport/tl/test/code_strategies.py
new file mode 100644
--- /dev/null
+++ b/rpython/jit/backend/llsupport/tl/test/code_strategies.py
@@ -0,0 +1,56 @@
+from hypothesis import strategies as st
+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)
+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 == SHORT_TYP:
+        return st.integers(min_value=-2**15, max_value=2**15-1)
+    elif typ == BYTE_TYP:
+        return st.integers(min_value=-2**7, max_value=2**7-1)
+    elif typ == COND_TYP:
+        return st.integers(min_value=0, max_value=4)
+    elif typ == STR_TYP:
+        return st.text()
+    elif typ == LIST_TYP:
+        return st.lists(elements=st.one_of(st.integers())) # TODO must be 
recursive
+    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
+
+def byte_code_classes():
+    for name, clazz in code.__dict__.items():
+        if hasattr(clazz, 'BYTE_CODE'):
+            yield clazz
+
+@composite
+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])
+    _stack = draw(runtime_stack(clazz))
+    return clazz, bytecode, consts, _stack
+
diff --git a/rpython/jit/backend/llsupport/tl/test/test_tl_interp.py 
b/rpython/jit/backend/llsupport/tl/test/test_tl_interp.py
new file mode 100644
--- /dev/null
+++ b/rpython/jit/backend/llsupport/tl/test/test_tl_interp.py
@@ -0,0 +1,30 @@
+import py
+from hypothesis import given
+from rpython.jit.backend.llsupport.tl import code, stack, interp
+from rpython.jit.backend.llsupport.tl.test import code_strategies as st
+
+class TestByteCode(object):
+    def test_load_str(self):
+        c = code.Context()
+        code.LoadStr("hello world").encode(c)
+        assert c.consts[0] == "hello world"
+        assert c.get_byte(0) == code.LoadStr.BYTE_CODE
+        assert c.get_short(1) == 0
+
+    def test_str_add(self):
+        c = code.Context()
+        code.LoadStr("hello").encode(c)
+        code.LoadStr("world").encode(c)
+        code.AddStr().encode(c)
+        assert len(c.consts) == 2
+        assert c.get_byte(4) == code.AddStr.BYTE_CODE
+        assert c.get_short(3) == 1
+
+class TestInterp(object):
+    @given(st.single_bytecode())
+    def test_consume_stack(self, args):
+        clazz, bytecode, consts, stack = args
+        space = interp.Space()
+        i = interp.dispatch_once(space, 0, bytecode, consts, stack)
+        assert i == len(bytecode)
+        assert stack.size() == len(clazz._return_on_stack_types)
diff --git a/rpython/jit/backend/llsupport/tl/test/zrpy_gc_hypo_test.py 
b/rpython/jit/backend/llsupport/tl/test/zrpy_gc_hypo_test.py
new file mode 100644
--- /dev/null
+++ b/rpython/jit/backend/llsupport/tl/test/zrpy_gc_hypo_test.py
@@ -0,0 +1,33 @@
+from rpython.jit.backend.detect_cpu import getcpuclass
+from rpython.jit.tool.oparser import parse
+from rpython.jit.metainterp.history import JitCellToken, NoStats
+from rpython.jit.metainterp.history import BasicFinalDescr, BasicFailDescr
+from rpython.jit.metainterp.gc import get_description
+from rpython.jit.metainterp.optimize import SpeculativeError
+from rpython.annotator.listdef import s_list_of_strings
+from rpython.rtyper.lltypesystem import lltype, llmemory, rffi
+from rpython.rtyper.rclass import getclassrepr, getinstancerepr
+from rpython.translator.unsimplify import call_initial_function
+from rpython.translator.translator import TranslationContext
+from rpython.translator.c import genc
+from rpython.jit.backend.llsupport.gcstress import interp
+
+class GCHypothesis(object):
+    def setup_class(self):
+        t = TranslationContext()
+        t.config.translation.gc = "incminimark"
+        t.config.translation.gcremovetypeptr = True
+        ann = t.buildannotator()
+        ann.build_types(interp.entry_point, [s_list_of_strings], 
main_entry_point=True)
+        rtyper = t.buildrtyper()
+        rtyper.specialize()
+
+        cbuilder = genc.CStandaloneBuilder(t, f, t.config)
+        cbuilder.generate_source(defines=cbuilder.DEBUG_DEFINES)
+        cbuilder.compile()
+
+        import pdb; pdb.set_trace()
+
+
+    def test_void(self):
+        pass
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to