[pypy-svn] r63114 - pypy/branch/virtualizable-specnodes-2/pypy/jit/metainterp/test
fijal at codespeak.net
fijal at codespeak.net
Fri Mar 20 10:05:43 CET 2009
Author: fijal
Date: Fri Mar 20 10:05:42 2009
New Revision: 63114
Modified:
pypy/branch/virtualizable-specnodes-2/pypy/jit/metainterp/test/test_tl.py
Log:
some example for blog, skip so far
Modified: pypy/branch/virtualizable-specnodes-2/pypy/jit/metainterp/test/test_tl.py
==============================================================================
--- pypy/branch/virtualizable-specnodes-2/pypy/jit/metainterp/test/test_tl.py (original)
+++ pypy/branch/virtualizable-specnodes-2/pypy/jit/metainterp/test/test_tl.py Fri Mar 20 10:05:42 2009
@@ -107,6 +107,97 @@
'int_is_true':1, 'guard_false':1, 'jump':1,
'guard_value':1})
+ def test_example(self):
+ py.test.skip("bugs")
+ jitdriver = JitDriver(greens = ['code', 'i'], reds = ['v1', 'v2', 'v3'])
+
+ class IntObject(object):
+ def __init__(self, value):
+ self.value = value
+
+ def add(self, other):
+ return IntObject(self.value + other.value)
+
+ def gt(self, other):
+ return self.value > other.value
+
+ def store_into_variable(num, v1, v2, v3, value_to_store):
+ if num == 0:
+ return value_to_store, v2, v3
+ elif num == 1:
+ return v1, value_to_store, v3
+ elif num == 2:
+ return v1, v2, value_to_store
+ else:
+ raise Exception("Wrong num")
+
+ def load_variable(num, v1, v2, v3):
+ if num == 0:
+ return v1
+ elif num == 1:
+ return v2
+ elif num == 2:
+ return v3
+ else:
+ raise Exception("Wrong num")
+
+ def interpret(code, i0, i1, i2):
+ v1 = IntObject(i0)
+ v2 = IntObject(i1)
+ v3 = IntObject(i2)
+ i = 0
+ while i < len(code):
+ jitdriver.jit_merge_point(code=code, i=i, v1=v1, v2=v2, v3=v3)
+ if code[i] == ADD:
+ a = load_variable(code[i + 1], v1, v2, v3)
+ b = load_variable(code[i + 2], v1, v2, v3)
+ res_num = code[i + 3]
+ res = a.add(b)
+ v1, v2, v3 = store_into_variable(res_num, v1, v2, v3,
+ res)
+ i += 4
+ elif code[i] == JUMP_IF_GT:
+ a = load_variable(code[i + 1], v1, v2, v3)
+ b = load_variable(code[i + 2], v1, v2, v3)
+ where = code[i + 3]
+ if a.gt(b):
+ i = where
+ jitdriver.can_enter_jit(code=code, i=i, v1=v1, v2=v2,
+ v3=v3)
+ else:
+ i += 4
+ elif code[i] == JUMP:
+ i = code[i + 1]
+ jitdriver.can_enter_jit(code=code, i=i, v1=v1, v2=v2, v3=v3)
+ else:
+ raise Exception("bytecode corruption")
+ return v1.value
+
+ ADD = 0
+ JUMP = 1
+ JUMP_IF_GT = 2
+
+ code = [
+ ADD, 0, 1, 0,
+ ADD, 0, 1, 0,
+ ADD, 0, 1, 0,
+ JUMP_IF_GT, 0, 2, 18,
+ JUMP, 0
+ ]
+
+ # v0 += v1
+ # v0 += v1
+ # v0 += v1
+ # if v1 > v2: jump 18 (after the end of the loop)
+ # jump 0
+
+ def runner(num, i0, i1, i2):
+ return interpret([code, []][num], i0, i1, i2)
+
+ assert interpret(code, 0, 1, 20) == 21
+ res = self.meta_interp(runner, [0, 0, 1, 20])
+ assert res == 21
+
class TestOOtype(ToyLanguageTests, OOJitMixin):
pass
More information about the Pypy-commit
mailing list