| 1 | from rpython.rtyper.lltypesystem import lltype, llmemory |
| 2 | from rpython.rtyper.lltypesystem import rstr |
| 3 | from rpython.rtyper import rclass |
| 4 | from rpython.rtyper.annlowlevel import llhelper |
| 5 | from rpython.rlib.objectmodel import instantiate |
| 6 | from rpython.jit.backend.ppc.locations import (imm, RegisterLocation, |
| 7 | ImmLocation, StackLocation) |
| 8 | from rpython.jit.backend.ppc.register import * |
| 9 | from rpython.jit.backend.ppc.codebuilder import hi, lo |
| 10 | from rpython.jit.backend.ppc.ppc_assembler import AssemblerPPC |
| 11 | from rpython.jit.backend.ppc.arch import WORD |
| 12 | from rpython.jit.codewriter.effectinfo import EffectInfo |
| 13 | from rpython.jit.codewriter import longlong |
| 14 | from rpython.jit.metainterp.history import BasicFailDescr, \ |
| 15 | JitCellToken, TargetToken |
| 16 | from rpython.jit.tool.oparser import parse |
| 17 | |
| 18 | class MockBuilder(object): |
| 19 | |
| 20 | def __init__(self): |
| 21 | self.reset() |
| 22 | |
| 23 | def __getattr__(self, name): |
| 24 | instr = MockInstruction(name) |
| 25 | self.instrs.append(instr) |
| 26 | return instr |
| 27 | |
| 28 | def str_instrs(self): |
| 29 | return [str(inst) for inst in self.instrs] |
| 30 | |
| 31 | def reset(self): |
| 32 | self.instrs = [] |
| 33 | |
| 34 | class MockInstruction(object): |
| 35 | |
| 36 | def __init__(self, name, *args): |
| 37 | self.name = name |
| 38 | self.args = args |
| 39 | |
| 40 | def __call__(self, *args): |
| 41 | self.args = args |
| 42 | |
| 43 | def __eq__(self, other): |
| 44 | assert isinstance(other, MockInstruction) |
| 45 | return self.name == other.name and self.args == other.args |
| 46 | |
| 47 | def __repr__(self): |
| 48 | return self.__str__() |
| 49 | |
| 50 | def __str__(self): |
| 51 | return "%s %r" % (self.name, self.args) |
| 52 | |
| 53 | |
| 54 | MI = MockInstruction |
| 55 | |
| 56 | class TestMocks(object): |
| 57 | |
| 58 | def setup_method(self, method): |
| 59 | self.builder = MockBuilder() |
| 60 | |
| 61 | def test_cmp_instruction(self): |
| 62 | assert MI("a", 1, 2) == MI("a", 1, 2) |
| 63 | assert not MI("a", 1, 2) == MI("b", 1, 2) |
| 64 | assert not MI("a", 1, 2) == MI("a", 2, 2) |
| 65 | assert not MI("a", 1) == MI("a", 1, 2) |
| 66 | assert not MI("a", 1, 2) == MI("a") |
| 67 | assert MI("a") == MI("a") |
| 68 | |
| 69 | def test_basic(self): |
| 70 | exp_instrs = [MI("mr", 3, 5), |
| 71 | MI("foobar"), |
| 72 | MI("li", 3, 2), |
| 73 | MI("stw", 3, 5, 100)] |
| 74 | |
| 75 | self.builder.mr(3, 5) |
| 76 | self.builder.foobar() |
| 77 | self.builder.li(3, 2) |
| 78 | self.builder.stw(3, 5, 100) |
| 79 | |
| 80 | assert self.builder.instrs == exp_instrs |
| 81 | |
| 82 | self.builder.blub() |
| 83 | assert self.builder.instr != exp_instrs |
| 84 | |
| 85 | class TestRegallocMov(object): |
| 86 | |
| 87 | def setup_method(self, method): |
| 88 | self.builder = MockBuilder() |
| 89 | self.asm = instantiate(AssemblerPPC) |
| 90 | self.asm.mc = self.builder |
| 91 | |
| 92 | def test_immediate_to_reg(self): |
| 93 | self.asm.regalloc_mov(imm(5), r10) |
| 94 | big = 2 << 28 |
| 95 | self.asm.regalloc_mov(imm(big), r0) |
| 96 | |
| 97 | exp_instr = [MI("load_imm", r10, 5), |
| 98 | MI("load_imm", r0, big)] |
| 99 | assert self.asm.mc.instrs == exp_instr |
| 100 | |
| 101 | def test_immediate_to_mem(self): |
| 102 | self.asm.regalloc_mov(imm(5), stack(6)) |
| 103 | big = 2 << 28 |
| 104 | self.asm.regalloc_mov(imm(big), stack(7)) |
| 105 | |
| 106 | exp_instr = [MI("alloc_scratch_reg"), |
| 107 | MI("load_imm", r0, 5), |
| 108 | MI("store", r0.value, SPP.value, get_spp_offset(6)), |
| 109 | MI("free_scratch_reg"), |
| 110 | |
| 111 | MI("alloc_scratch_reg"), |
| 112 | MI("load_imm", r0, big), |
| 113 | MI("store", r0.value, SPP.value, get_spp_offset(7)), |
| 114 | MI("free_scratch_reg")] |
| 115 | assert self.asm.mc.instrs == exp_instr |
| 116 | |
| 117 | def test_mem_to_reg(self): |
| 118 | self.asm.regalloc_mov(stack(5), reg(10)) |
| 119 | self.asm.regalloc_mov(stack(0), reg(0)) |
| 120 | exp_instrs = [MI("load", r10.value, SPP.value, get_spp_offset(5)), |
| 121 | MI("load", r0.value, SPP.value, get_spp_offset(0))] |
| 122 | assert self.asm.mc.instrs == exp_instrs |
| 123 | |
| 124 | def test_mem_to_mem(self): |
| 125 | self.asm.regalloc_mov(stack(5), stack(6)) |
| 126 | exp_instrs = [ |
| 127 | MI("alloc_scratch_reg"), |
| 128 | MI("load", r0.value, SPP.value, get_spp_offset(5)), |
| 129 | MI("store", r0.value, SPP.value, get_spp_offset(6)), |
| 130 | MI("free_scratch_reg")] |
| 131 | assert self.asm.mc.instrs == exp_instrs |
| 132 | |
| 133 | def test_reg_to_reg(self): |
| 134 | self.asm.regalloc_mov(reg(0), reg(1)) |
| 135 | self.asm.regalloc_mov(reg(5), reg(10)) |
| 136 | exp_instrs = [MI("mr", r1.value, r0.value), |
| 137 | MI("mr", r10.value, r5.value)] |
| 138 | assert self.asm.mc.instrs == exp_instrs |
| 139 | |
| 140 | def test_reg_to_mem(self): |
| 141 | self.asm.regalloc_mov(reg(5), stack(10)) |
| 142 | self.asm.regalloc_mov(reg(0), stack(2)) |
| 143 | exp_instrs = [MI("store", r5.value, SPP.value, get_spp_offset(10)), |
| 144 | MI("store", r0.value, SPP.value, get_spp_offset(2))] |
| 145 | assert self.asm.mc.instrs == exp_instrs |
| 146 | |
| 147 | def reg(i): |
| 148 | return RegisterLocation(i) |
| 149 | |
| 150 | def stack(i): |
| 151 | return StackLocation(i, get_spp_offset(i)) |
| 152 | |
| 153 | def get_spp_offset(i): |
| 154 | return i * 8 + 304 |