[pypy-svn] r65947 - in pypy/branch/pyjitpl5/pypy/jit/tl/spli: . test
benjamin at codespeak.net
benjamin at codespeak.net
Thu Jun 25 00:10:23 CEST 2009
Author: benjamin
Date: Thu Jun 25 00:10:22 2009
New Revision: 65947
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 calls and a global scope
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 Thu Jun 25 00:10:22 2009
@@ -29,16 +29,16 @@
reds = ['frame'],
virtualizables = ['frame'])
-def spli_run_from_cpython_code(co, args=[]):
+def spli_run_from_cpython_code(co, args=[], locs=None, globs=None):
space = objects.DumbObjSpace()
pyco = Code._from_code(space, co)
print dis.dis(co)
- return run(pyco, args, space)
+ return run(pyco, args, locs, globs, space)
-def run(pyco, args, space=None):
+def run(pyco, args, locs=None, globs=None, space=None):
if space is None:
space = objects.DumbObjSpace()
- frame = SPLIFrame(pyco)
+ frame = SPLIFrame(pyco, locs, globs)
for i, arg in enumerate(args):
frame.locals[i] = space.wrap(arg)
return frame.run()
@@ -58,12 +58,24 @@
_virtualizable2_ = ['value_stack[*]', 'locals[*]', 'stack_depth']
- def __init__(self, code):
+ def __init__(self, code, locs=None, globs=None):
self.code = code
self.value_stack = [None] * code.co_stacksize
self.locals = [None] * code.co_nlocals
+ if locs is not None:
+ self.locals_dict = locs
+ else:
+ self.locals_dict = {}
+ if globs is not None:
+ self.globs = globs
+ else:
+ self.globs = {}
self.stack_depth = 0
+ def set_args(self, args):
+ for i in range(len(args)):
+ self.locals[i] = args[i]
+
def run(self):
self.stack_depth = 0
try:
@@ -109,6 +121,9 @@
self.value_stack[self.stack_depth] = None
return val
+ def pop_many(self, n):
+ return [self.pop() for i in range(n)]
+
def peek(self):
return self.value_stack[self.stack_depth - 1]
@@ -124,6 +139,21 @@
self.locals[name_index] = self.pop()
return next_instr
+ def LOAD_NAME(self, name_index, next_instr, code):
+ name = self.code.co_names_w[name_index].as_str()
+ self.push(self.locals_dict[name])
+ return next_instr
+
+ def STORE_NAME(self, name_index, next_instr, code):
+ name = self.code.co_names_w[name_index].as_str()
+ self.locals_dict[name] = self.pop()
+ return next_instr
+
+ def LOAD_GLOBAL(self, name_index, next_instr, code):
+ name = self.code.co_names_w[name_index].as_str()
+ self.push(self.globs[name])
+ return next_instr
+
def RETURN_VALUE(self, _, next_instr, code):
raise Return(self.pop())
@@ -161,6 +191,18 @@
self.push(getattr(left, name)(right))
return next_instr
+ def MAKE_FUNCTION(self, _, next_instr, code):
+ func = objects.Function(self.pop(), self.globs)
+ self.push(func)
+ return next_instr
+
+ def CALL_FUNCTION(self, arg_count, next_instr, code):
+ args = self.pop_many(arg_count)
+ func = self.pop()
+ self.push(func.call(args))
+ return next_instr
+
+
items = []
for item in unrolling_opcode_descs._items:
if getattr(SPLIFrame, item.methodname, None) is not None:
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 Thu Jun 25 00:10:22 2009
@@ -1,4 +1,4 @@
-from pypy.interpreter.baseobjspace import ObjSpace
+from pypy.interpreter.baseobjspace import ObjSpace, Wrappable
from pypy.rlib.objectmodel import specialize
class DumbObjSpace(ObjSpace):
@@ -12,6 +12,8 @@
return Str(x)
elif x is None:
return spli_None
+ elif isinstance(x, Wrappable):
+ return x.__spacebind__(self)
else:
raise NotImplementedError("Wrapping %s" % x)
@@ -117,9 +119,12 @@
class Function(SPLIObject):
- def __init__(self, code):
+ def __init__(self, code, globs):
self.code = code
+ self.globs = globs
def call(self, args):
from pypy.jit.tl.spli import interpreter
- return interpreter.SPLIFrame(self.code).run()
+ frame = interpreter.SPLIFrame(self.code, None, self.globs)
+ frame.set_args(args)
+ return frame.run()
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 Thu Jun 25 00:10:22 2009
@@ -55,3 +55,16 @@
def f():
3 + "3"
py.test.raises(objects.W_TypeError, self.eval, f)
+
+ def test_call(self):
+ code = compile("""
+def g():
+ return 4
+def f():
+ return g() + 3
+res = f()""", "<string>", "exec")
+ globs = {}
+ mod_res = interpreter.spli_run_from_cpython_code(code, [], globs, globs)
+ assert mod_res is objects.spli_None
+ assert len(globs) == 3
+ assert globs["res"].as_int() == 7
More information about the Pypy-commit
mailing list