[pypy-svn] r73744 - in pypy/trunk/pypy/jit/tl: . test
fijal at codespeak.net
fijal at codespeak.net
Wed Apr 14 20:58:34 CEST 2010
Author: fijal
Date: Wed Apr 14 20:58:32 2010
New Revision: 73744
Modified:
pypy/trunk/pypy/jit/tl/test/test_tinyframe.py
pypy/trunk/pypy/jit/tl/tinyframe.py
Log:
pass another test
Modified: pypy/trunk/pypy/jit/tl/test/test_tinyframe.py
==============================================================================
--- pypy/trunk/pypy/jit/tl/test/test_tinyframe.py (original)
+++ pypy/trunk/pypy/jit/tl/test/test_tinyframe.py Wed Apr 14 20:58:32 2010
@@ -13,3 +13,13 @@
assert disassemble(code) == [
LOAD, 0, 1, LOAD, 1, 0, ADD, 0, 1, 2, PRINT, 2
]
+
+ def test_return(self):
+ code = compile('''
+ LOAD 0 => r1
+ LOAD 1 => r0 # comment
+ # other comment
+ ADD r0 r1 => r2
+ RETURN r2
+ ''')
+ res = interpret(code)
Modified: pypy/trunk/pypy/jit/tl/tinyframe.py
==============================================================================
--- pypy/trunk/pypy/jit/tl/tinyframe.py (original)
+++ pypy/trunk/pypy/jit/tl/tinyframe.py Wed Apr 14 20:58:32 2010
@@ -14,27 +14,25 @@
CALL r1 r2 # call a function in register one with argument in r2
LOAD <name> => r # load a function named name into register r
LOAD <int constant> => r # load an integer constant into register r
+RETURN r1
function argument always comes in r0
"""
-opcodes = ['ADD', 'INTROSPECT', 'PRINT', 'CALL', 'LOAD']
+opcodes = ['ADD', 'INTROSPECT', 'PRINT', 'CALL', 'LOAD', 'RETURN']
for i, opcode in enumerate(opcodes):
globals()[opcode] = i
class Code(object):
- _immutable_ = True
-
- def __init__(self, code):
- code = code
-
-def rint(arg):
- assert arg.startswith('r')
- return int(arg[1:])
+ def __init__(self, code, regno):
+ self.code = code
+ self.regno = regno
class Parser(object):
+
def compile(self, strrepr):
self.code = []
+ self.maxregno = 0
for line in strrepr.splitlines():
comment = line.find('#')
if comment != -1:
@@ -44,25 +42,76 @@
continue
opcode, args = line.split(" ", 1)
getattr(self, 'compile_' + opcode)(args)
- return "".join([chr(i) for i in self.code])
+ return Code("".join([chr(i) for i in self.code]), self.maxregno + 1)
+
+ def rint(self, arg):
+ assert arg.startswith('r')
+ no = int(arg[1:])
+ self.maxregno = max(self.maxregno, no)
+ return no
def compile_ADD(self, args):
args, result = args.split("=>")
arg0, arg1 = args.strip().split(" ")
- self.code += [ADD, rint(arg0), rint(arg1), rint(result.strip())]
+ self.code += [ADD, self.rint(arg0), self.rint(arg1),
+ self.rint(result.strip())]
def compile_LOAD(self, args):
arg0, result = args.split("=>")
arg0 = arg0.strip()
- self.code += [LOAD, int(arg0), rint(result.strip())]
+ self.code += [LOAD, int(arg0), self.rint(result.strip())]
def compile_PRINT(self, args):
- arg = rint(args.strip())
+ arg = self.rint(args.strip())
self.code += [PRINT, arg]
+ def compile_RETURN(self, args):
+ arg = self.rint(args.strip())
+ self.code += [RETURN, arg]
+
def compile(strrepr):
parser = Parser()
return parser.compile(strrepr)
def disassemble(code):
- return [ord(i) for i in code]
+ return [ord(i) for i in code.code]
+
+class Object(object):
+ def __init__(self):
+ raise NotImplementedError("abstract base class")
+
+ def add(self, other):
+ raise NotImplementedError("abstract base class")
+
+class Int(Object):
+ def __init__(self, val):
+ self.val = val
+
+ def add(self, other):
+ return Int(self.val + other.val)
+
+class Frame(object):
+ def __init__(self, code):
+ self.code = code
+ self.registers = [None] * code.regno
+
+ def interpret(self):
+ i = 0
+ code = self.code.code
+ while True:
+ opcode = ord(code[i])
+ if opcode == LOAD:
+ self.registers[ord(code[i + 2])] = Int(ord(code[i + 1]))
+ i += 3
+ elif opcode == ADD:
+ arg1 = self.registers[ord(code[i + 1])]
+ arg2 = self.registers[ord(code[i + 2])]
+ self.registers[ord(code[i + 3])] = arg1.add(arg2)
+ i += 4
+ elif opcode == RETURN:
+ return self.registers[ord(code[i + 1])]
+ else:
+ raise Exception("unimplemented opcode %s" % opcodes[opcode])
+
+def interpret(code):
+ return Frame(code).interpret()
More information about the Pypy-commit
mailing list