示例#1
0
 def test_splitblock(self):
     old, new = self.b.splitblock('newblock')
     with self.b.at_front(old):
         self.b.add(types.Int32, [self.a, self.a])
     with self.b.at_end(new):
         self.b.div(types.Int32, [self.a, self.a])
     self.assertEqual(opcodes(self.f), ['add', 'div'])
示例#2
0
文件: test_cfa.py 项目: flypy/pykit
 def test_ssa2(self):
     mod = from_c(source)
     f = mod.get_function('func')
     cfa.run(f)
     verify(f)
     codes = opcodes(f)
     self.assertEqual(codes.count('phi'), 3)
示例#3
0
 def test_ssa2(self):
     mod = from_c(source)
     f = mod.get_function('func')
     cfa.run(f)
     verify(f)
     codes = opcodes(f)
     self.assertEqual(codes.count('phi'), 3)
示例#4
0
 def test_splitblock(self):
     old, new = self.b.splitblock('newblock')
     with self.b.at_front(old):
         self.b.add(self.a, self.a)
     with self.b.at_end(new):
         self.b.div(self.a, self.a)
     self.assertEqual(opcodes(self.f), ['add', 'div'])
示例#5
0
    def test_replace(self):
        entry = self.f.get_block('entry')
        for op in entry:
            if op.opcode == ops.convert:
                r, = op.args
                t = self.b.add(types.Int32, [r, r])
                c = self.b.convert(types.Float32, [t], result=op.result)
                op.replace([t, c])
                break

        cfa.run(self.f)
        self.assertEqual(opcodes(self.f), ['mul', 'add', 'convert', 'ret'])
示例#6
0
    def test_ssa(self):
        mod = from_c(source)
        f = mod.get_function('func_simple')
        verify(f)
        self.assertEqual(opcodes(f.startblock),
                         ['alloca', 'store', 'load', 'gt', 'cbranch'])

        # SSA
        CFG = cfa.cfg(f)
        cfa.ssa(f, CFG)

        assert len(f.blocks) == 4
        blocks = list(f.blocks)
        self.assertEqual(opcodes(blocks[0]), ['gt', 'cbranch'])
        self.assertEqual(opcodes(blocks[1]), ['jump'])
        self.assertEqual(opcodes(blocks[2]), ['jump'])
        self.assertEqual(opcodes(blocks[3]), ['phi', 'ret'])

        phi = findop(f, 'phi')
        iblocks, ivals = phi.args
        self.assertEqual(sorted(iblocks), sorted([blocks[1], blocks[2]]))
        self.assertEqual(len(ivals), 2)
示例#7
0
文件: test_cfa.py 项目: flypy/pykit
    def test_ssa(self):
        mod = from_c(source)
        f = mod.get_function('func_simple')
        verify(f)
        self.assertEqual(opcodes(f.startblock),
                         ['alloca', 'store', 'load', 'gt', 'cbranch'])

        # SSA
        CFG = cfa.cfg(f)
        cfa.ssa(f, CFG)

        assert len(f.blocks) == 4
        blocks = list(f.blocks)
        self.assertEqual(opcodes(blocks[0]), ['gt', 'cbranch'])
        self.assertEqual(opcodes(blocks[1]), ['jump'])
        self.assertEqual(opcodes(blocks[2]), ['jump'])
        self.assertEqual(opcodes(blocks[3]), ['phi', 'convert', 'ret'])

        phi = findop(f, 'phi')
        iblocks, ivals = phi.args
        self.assertEqual(sorted(iblocks), sorted([blocks[1], blocks[2]]))
        self.assertEqual(len(ivals), 2)
    def test_exc_rewrite(self):
        func = Function("foo", [], types.Function(types.Void, (), False))
        entry = func.new_block("entry")
        catch_block = func.new_block("catch")
        b = Builder(func)

        with b.at_front(entry):
            b.exc_setup([catch_block])
            b.exc_throw(Const(StopIteration, types.Exception))
        with b.at_front(catch_block):
            b.exc_catch([Const(Exception, types.Exception)])

        local_exceptions.run(func, {})
        self.assertNotIn('exc_throw', opcodes(func))
示例#9
0
    def test_exc_rewrite(self):
        func = Function("foo", [], types.Function(types.Void, (), False))
        entry = func.new_block("entry")
        catch_block = func.new_block("catch")
        b = Builder(func)

        with b.at_front(entry):
            b.exc_setup([catch_block])
            b.exc_throw(Const(StopIteration, types.Exception))
        with b.at_front(catch_block):
            b.exc_catch([Const(Exception, types.Exception)])

        local_exceptions.run(func, {})
        self.assertNotIn('exc_throw', opcodes(func))
示例#10
0
    def test_inline(self):
        simple = """
        #include <pykit_ir.h>

        int callee(int i) {
            return i * i;
        }

        int caller(int i) {
            int x = call(callee, list(i));
            return x;
        }
        """
        mod = from_c(simple)
        func = mod.get_function("caller")
        [callsite] = findallops(func, 'call')
        inline.inline(func, callsite)
        cfa.run(func)
        verify(func)
        assert interp.run(func, args=[10]) == 100
        assert len(list(func.blocks)) == 1
        assert opcodes(func) == ['mul', 'ret']
示例#11
0
    def test_inline(self):
        simple = textwrap.dedent("""
        #include <pykit_ir.h>

        int callee(int i) {
            return i * i;
        }

        int caller(int i) {
            int x = call(callee, list(i));
            return x;
        }
        """)
        mod = from_c(simple)
        func = mod.get_function("caller")
        [callsite] = findallops(func, 'call')
        inline.inline(func, callsite)
        cfa.run(func)
        verify(func)
        assert interp.run(func, args=[10]) == 100
        assert len(list(func.blocks)) == 1
        assert opcodes(func) == ['mul', 'ret']
示例#12
0
    def test_inline(self):
        simple = textwrap.dedent("""
        #include <pykit_ir.h>

        int callee(int i) {
            return i * i;
        }

        int caller(int i) {
            int x = call(callee, list(i));
            return x;
        }
        """)
        mod = from_c(simple)
        func = mod.get_function("caller")
        [callsite] = findallops(func, 'call')
        inline.inline(func, callsite)
        cfa.run(func)
        verify(func)
        assert interp.run(func, args=[10]) == 100
        assert len(list(func.blocks)) == 1
        self.assertEqual([o for o in opcodes(func) if o != 'convert'],
                         ['mul', 'ret'])
示例#13
0
 def test_normalize(self):
     mod = from_c(testfunc)
     func = mod.get_function("func")
     ret.run(func, fresh_env())
     ops = opcodes(func)
     self.assertEqual(ops.count("ret"), 1, ops)