[pypy-svn] r65871 - in pypy/branch/pyjitpl5/pypy/jit/tl/spli: . test
fijal at codespeak.net
fijal at codespeak.net
Mon Jun 22 21:21:14 CEST 2009
Author: fijal
Date: Mon Jun 22 21:21:11 2009
New Revision: 65871
Modified:
pypy/branch/pyjitpl5/pypy/jit/tl/spli/interpreter.py
pypy/branch/pyjitpl5/pypy/jit/tl/spli/objects.py
pypy/branch/pyjitpl5/pypy/jit/tl/spli/test/test_interpreter.py
Log:
implement minimum to run a loop
Modified: pypy/branch/pyjitpl5/pypy/jit/tl/spli/interpreter.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/jit/tl/spli/interpreter.py (original)
+++ pypy/branch/pyjitpl5/pypy/jit/tl/spli/interpreter.py Mon Jun 22 21:21:11 2009
@@ -1,12 +1,37 @@
from pypy.interpreter import pycode
from pypy.tool import stdlib_opcode as opcode
from pypy.jit.tl.spli import objects
+import dis
+compare_ops = [
+ "cmp_lt", # "<"
+ "cmp_le", # "<="
+ "cmp_eq", # "=="
+ "cmp_ne", # "!="
+ "cmp_gt", # ">"
+ "cmp_ge", # ">="
+ "cmp_in",
+ "cmp_not_in",
+ "cmp_is",
+ "cmp_is_not",
+ "cmp_exc_match",
+]
+
+def wrap(arg):
+ if isinstance(arg, int):
+ return objects.Int(arg)
+ elif isinstance(arg, str):
+ return object.Str(arg)
+ else:
+ raise NotImplementedError("wrap of %s" % (arg,))
-def spli_run_from_cpython_code(co):
+def spli_run_from_cpython_code(co, args=[]):
pyco = pycode.PyCode._from_code(objects.DumbObjSpace(), co)
- return SPLIFrame(pyco).run()
-
+ print dis.dis(co)
+ frame = SPLIFrame(pyco)
+ for i, arg in enumerate(args):
+ frame.locals[i] = wrap(arg)
+ return frame.run()
class BlockUnroller(Exception):
pass
@@ -45,9 +70,7 @@
else:
oparg = 0
meth = getattr(self, opcode.opcode_method_names[op])
- res = meth(oparg)
- if res is not None:
- instr_index = res
+ instr_index = meth(oparg, instr_index)
def push(self, value):
self.value_stack[self.stack_depth] = value
@@ -58,19 +81,51 @@
val = self.value_stack[self.stack_depth]
return val
- def LOAD_FAST(self, name_index):
+ def peek(self):
+ return self.value_stack[self.stack_depth - 1]
+
+ def POP_TOP(self, _, next_instr):
+ self.pop()
+ return next_instr
+
+ def LOAD_FAST(self, name_index, next_instr):
self.push(self.locals[name_index])
+ return next_instr
- def STORE_FAST(self, name_index):
+ def STORE_FAST(self, name_index, next_instr):
self.locals[name_index] = self.pop()
+ return next_instr
- def RETURN_VALUE(self, _):
+ def RETURN_VALUE(self, _, next_instr):
raise Return(self.pop())
- def LOAD_CONST(self, const_index):
+ def LOAD_CONST(self, const_index, next_instr):
self.push(self.code.co_consts_w[const_index])
+ return next_instr
- def BINARY_ADD(self, _):
+ def BINARY_ADD(self, _, next_instr):
right = self.pop()
left = self.pop()
self.push(left.add(right))
+ return next_instr
+
+ def SETUP_LOOP(self, _, next_instr):
+ return next_instr
+
+ def POP_BLOCK(self, _, next_instr):
+ return next_instr
+
+ def JUMP_IF_FALSE(self, arg, next_instr):
+ w_cond = self.peek()
+ if not w_cond.is_true():
+ next_instr += arg
+ return next_instr
+
+ def JUMP_ABSOLUTE(self, arg, next_instr):
+ return arg
+
+ def COMPARE_OP(self, arg, next_instr):
+ right = self.pop()
+ left = self.pop()
+ self.push(getattr(left, compare_ops[arg])(right))
+ return next_instr
Modified: pypy/branch/pyjitpl5/pypy/jit/tl/spli/objects.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/jit/tl/spli/objects.py (original)
+++ pypy/branch/pyjitpl5/pypy/jit/tl/spli/objects.py Mon Jun 22 21:21:11 2009
@@ -23,6 +23,13 @@
def call(self, args):
raise InvalidOperation
+class Bool(SPLIObject):
+
+ def __init__(self, value):
+ self.value = value
+
+ def is_true(self):
+ return self.value
class Int(SPLIObject):
@@ -32,6 +39,8 @@
def add(self, other):
return Int(self.value + other.value)
+ def cmp_lt(self, other):
+ return Bool(self.value < other.value)
class Str(SPLIObject):
Modified: pypy/branch/pyjitpl5/pypy/jit/tl/spli/test/test_interpreter.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/jit/tl/spli/test/test_interpreter.py (original)
+++ pypy/branch/pyjitpl5/pypy/jit/tl/spli/test/test_interpreter.py Mon Jun 22 21:21:11 2009
@@ -3,8 +3,8 @@
class TestSPLIInterpreter:
- def eval(self, func):
- return interpreter.spli_run_from_cpython_code(func.func_code)
+ def eval(self, func, args=[]):
+ return interpreter.spli_run_from_cpython_code(func.func_code, args)
def test_int_add(self):
def f():
@@ -29,3 +29,21 @@
v = self.eval(f)
assert isinstance(v, objects.Str)
assert v.value == "Hello, SPLI world!"
+
+ def test_comparison(self):
+ def f(i):
+ return i < 10
+
+ v = self.eval(f, [0])
+ assert isinstance(v, objects.Bool)
+ assert v.value == True
+
+ def test_while_loop(self):
+ def f():
+ i = 0
+ while i < 100:
+ i = i + 1
+ return i
+
+ v = self.eval(f)
+ assert v.value == 100
More information about the Pypy-commit
mailing list