[pypy-svn] r73887 - in pypy/branch/blackhole-improvement/pypy/jit/codewriter: . test

arigo at codespeak.net arigo at codespeak.net
Mon Apr 19 17:23:50 CEST 2010


Author: arigo
Date: Mon Apr 19 17:23:47 2010
New Revision: 73887

Added:
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/assembler.py   (contents, props changed)
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/blackhole.py   (contents, props changed)
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_assembler.py   (contents, props changed)
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_blackhole.py   (contents, props changed)
Modified:
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_format.py
Log:
Start writing the blackhole interpreter.


Added: pypy/branch/blackhole-improvement/pypy/jit/codewriter/assembler.py
==============================================================================
--- (empty file)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/assembler.py	Mon Apr 19 17:23:47 2010
@@ -0,0 +1,17 @@
+from pypy.jit.metainterp import history
+
+
+class JitCode(history.AbstractValue):
+    empty_list = []
+
+    def __init__(self, name, cfnptr=None, calldescr=None, called_from=None,
+                 graph=None):
+        self.name = name
+        self.cfnptr = cfnptr
+        self.calldescr = calldescr
+        self.called_from = called_from
+        self.graph = graph
+
+    def setup(self, code, constants):
+        self.code = code
+        self.constants = constants or self.empty_list    # share the empty list

Added: pypy/branch/blackhole-improvement/pypy/jit/codewriter/blackhole.py
==============================================================================
--- (empty file)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/blackhole.py	Mon Apr 19 17:23:47 2010
@@ -0,0 +1,102 @@
+from pypy.rlib.unroll import unrolling_iterable
+from pypy.tool.sourcetools import func_with_new_name
+
+
+def arguments(*argtypes, **kwds):
+    resulttype = kwds.pop('returns', None)
+    assert not kwds
+    def decorate(function):
+        function.argtypes = argtypes
+        function.resulttype = resulttype
+        return function
+    return decorate
+
+class LeaveFrame(Exception):
+    pass
+
+
+class BlackholeInterpreter(object):
+
+    def __init__(self):
+        self.registers_i = [0] * 256
+
+    def setup_insns(self, insns):
+        assert len(insns) <= 256, "too many instructions!"
+        self._insns = [None] * len(insns)
+        for key, value in insns.items():
+            assert self._insns[value] is None
+            self._insns[value] = key
+        #
+        all_funcs = []
+        for key in self._insns:
+            assert key is not None, "hole!"
+            assert key.count('/') == 1, "bad key: %r" % (key,)
+            name, argcodes = key.split('/')
+            all_funcs.append(self._get_method(name, argcodes))
+        all_funcs = unrolling_iterable(enumerate(all_funcs))
+        #
+        def dispatch(code, position):
+            opcode = ord(code[position])
+            position += 1
+            for i, func in all_funcs:
+                if opcode == i:
+                    return func(code, position)
+            else:
+                raise AssertionError("bad opcode")
+        self.dispatch = dispatch
+
+    def _get_method(self, name, argcodes):
+        #
+        def handler(code, position):
+            args = ()
+            for argcode, argtype in arg_codes_and_types:
+                if argcode == 'i':
+                    value = self.registers_i[ord(code[position])]
+                    position += 1
+                    args += (value,)
+                    assert argtype == 'i'
+                else:
+                    raise AssertionError("bad arg code: %r" % (argcode,))
+            result = boundmethod(*args)
+            if resulttype == 'i':
+                assert type(result) is int
+                self.registers_i[ord(code[position])] = result
+                position += 1
+            else:
+                assert resulttype is None
+                assert result is None
+            return position
+        #
+        boundmethod = getattr(self, 'opimpl_' + name)
+        argtypes = boundmethod.argtypes
+        resulttype = boundmethod.resulttype
+        if resulttype is not None:
+            assert argcodes[-1] == 'i'
+            argcodes = argcodes[:-1]
+        assert len(argcodes) == len(argtypes)
+        arg_codes_and_types = unrolling_iterable(zip(argcodes, argtypes))
+        handler = func_with_new_name(handler, 'handler_' + name)
+        return handler
+
+    def setarg_i(self, index, value):
+        self.registers_i[index] = value
+
+    def run(self, jitcode, position):
+        code = jitcode.code
+        constants = jitcode.constants
+        try:
+            while True:
+                position = self.dispatch(code, position)
+        except LeaveFrame:
+            pass
+
+    # ----------
+
+    @arguments("i", "i", returns="i")
+    def opimpl_int_add(self, a, b):
+        return a + b
+
+    @arguments("i")
+    def opimpl_int_return(self, a):
+        self.result_i = a
+        raise LeaveFrame

Added: pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_assembler.py
==============================================================================
--- (empty file)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_assembler.py	Mon Apr 19 17:23:47 2010
@@ -0,0 +1,63 @@
+from pypy.jit.codewriter.assembler import Assembler
+from pypy.jit.codewriter.flatten import SSARepr, Label, TLabel, Register
+from pypy.jit.metainterp.history import ConstInt
+
+
+def test_assemble_simple():
+    ssarepr = SSARepr()
+    i0, i1, i2 = Register(0), Register(1), Register(2)
+    ssarepr.insns = [
+        ('int_add', i0, i1, i2),
+        ('int_return', i2),
+        ]
+    assembler = Assembler()
+    jitcode = assembler.assemble(ssarepr)
+    assert jitcode.code == ("\x00\x00\x01\x02"
+                            "\x01\x02")
+    assert assembler.insns == {'int_add/iii': 0,
+                               'int_return/i': 1}
+
+def test_assemble_consts():
+    ssarepr = SSARepr()
+    ssarepr.insns = [
+        ('int_return', Register(13)),
+        ('int_return', Constant(18)),
+        ('int_return', Constant(-4)),
+        ('int_return', Constant(128)),
+        ('int_return', Constant(-129)),
+        ]
+    assembler = Assembler()
+    jitcode = assembler.assemble(ssarepr)
+    assert jitcode.code == ("\x00\x0D"
+                            "\x01\x12"   # use int_return/c for one-byte consts
+                            "\x01\xFC"
+                            "\x00\xFF"   # use int_return/i for larger consts
+                            "\x00\xFE")
+    assert assembler.insns == {'int_return/i': 0,
+                               'int_return/c': 1}
+    assert assembler.constants == [ConstInt(-129), ConstInt(128)]
+
+def test_assemble_loop():
+    ssarepr = SSARepr()
+    i0, i1 = Register(16), Register(17)
+    ssarepr.insns = [
+        (Label('L1'),),
+        ('goto_if_not_int_gt', TLabel('L2'), i0, Constant(4)),
+        ('int_add', i1, i0, i1),
+        ('int_sub', i0, Constant(1), i0),
+        ('goto', TLabel('L1')),
+        (Label('L2'),),
+        ('int_return', i1),
+        ]
+    assembler = Assembler()
+    jitcode = assembler.assemble(ssarepr)
+    assert jitcode.code == ("\x00\x10\x00\x10\x04"
+                            "\x01\x17\x16\x17"
+                            "\x02\x16\x01\x16"
+                            "\x03\x00\x00"
+                            "\x04\x17")
+    assert assembler.insns == {'goto_if_not_int_gt/Lic': 0,
+                               'int_add/iii': 1,
+                               'int_sub/ici': 2,
+                               'goto/L': 3,
+                               'int_return/i': 4}

Added: pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_blackhole.py
==============================================================================
--- (empty file)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_blackhole.py	Mon Apr 19 17:23:47 2010
@@ -0,0 +1,16 @@
+from pypy.jit.codewriter.assembler import JitCode
+from pypy.jit.codewriter.blackhole import BlackholeInterpreter
+
+
+def test_simple():
+    jitcode = JitCode("test")
+    jitcode.setup("\x00\x00\x01\x02"
+                  "\x01\x02",
+                  [])
+    blackholeinterp = BlackholeInterpreter()
+    blackholeinterp.setup_insns({'int_add/iii': 0,
+                                 'int_return/i': 1})
+    blackholeinterp.setarg_i(0, 40)
+    blackholeinterp.setarg_i(1, 2)
+    blackholeinterp.run(jitcode, 0)
+    assert blackholeinterp.result_i == 42

Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_format.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_format.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_format.py	Mon Apr 19 17:23:47 2010
@@ -8,13 +8,13 @@
     ssarepr = SSARepr()
     i0, i1, i2 = Register(0), Register(1), Register(2)
     ssarepr.insns = [
-        ('livevars', [i0, i1]),
+        ('foobar', [i0, i1]),
         ('int_add', i0, i1, i2),
         ('int_return', i2),
         ]
     asm = format_assembler(ssarepr)
     expected = """
-        livevars [%i0, %i1]
+        foobar [%i0, %i1]
         int_add %i0, %i1, %i2
         int_return %i2
     """
@@ -24,7 +24,7 @@
     ssarepr = SSARepr()
     i0, i1 = Register(0), Register(1)
     ssarepr.insns = [
-        ('livevars', [i0, i1]),
+        ('foobar', [i0, i1]),
         (Label('L1'),),
         ('goto_if_not_int_gt', TLabel('L2'), i0, Constant(0)),
         ('int_add', i1, i0, i1),
@@ -35,7 +35,7 @@
         ]
     asm = format_assembler(ssarepr)
     expected = """
-        livevars [%i0, %i1]
+        foobar [%i0, %i1]
         L1:
         goto_if_not_int_gt L2, %i0, $0
         int_add %i1, %i0, %i1



More information about the Pypy-commit mailing list