File: test_regalloc.py

package info (click to toggle)
pypy 5.6.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 97,040 kB
  • ctags: 185,069
  • sloc: python: 1,147,862; ansic: 49,642; cpp: 5,245; asm: 5,169; makefile: 529; sh: 481; xml: 232; lisp: 45
file content (154 lines) | stat: -rw-r--r-- 5,082 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from rpython.rtyper.lltypesystem import lltype, llmemory
from rpython.rtyper.lltypesystem import rstr
from rpython.rtyper import rclass
from rpython.rtyper.annlowlevel import llhelper
from rpython.rlib.objectmodel import instantiate
from rpython.jit.backend.ppc.locations import (imm, RegisterLocation,
                                               ImmLocation, StackLocation)
from rpython.jit.backend.ppc.register import *
from rpython.jit.backend.ppc.codebuilder import hi, lo
from rpython.jit.backend.ppc.ppc_assembler import AssemblerPPC
from rpython.jit.backend.ppc.arch import WORD
from rpython.jit.codewriter.effectinfo import EffectInfo
from rpython.jit.codewriter import longlong
from rpython.jit.metainterp.history import BasicFailDescr, \
     JitCellToken, TargetToken
from rpython.jit.tool.oparser import parse

class MockBuilder(object):
    
    def __init__(self):
        self.reset()

    def __getattr__(self, name):
        instr = MockInstruction(name)
        self.instrs.append(instr)
        return instr

    def str_instrs(self):
        return [str(inst) for inst in self.instrs]

    def reset(self):
        self.instrs = []

class MockInstruction(object):

    def __init__(self, name, *args):
        self.name = name
        self.args = args

    def __call__(self, *args):
        self.args = args

    def __eq__(self, other):
        assert isinstance(other, MockInstruction)
        return self.name == other.name and self.args == other.args

    def __repr__(self):
        return self.__str__()

    def __str__(self):
        return "%s %r" % (self.name, self.args)
    

MI = MockInstruction

class TestMocks(object):
    
    def setup_method(self, method):
        self.builder = MockBuilder()

    def test_cmp_instruction(self):
        assert MI("a", 1, 2) == MI("a", 1, 2)
        assert not MI("a", 1, 2) == MI("b", 1, 2)
        assert not MI("a", 1, 2) == MI("a", 2, 2)
        assert not MI("a", 1) == MI("a", 1, 2)
        assert not MI("a", 1, 2) == MI("a")
        assert MI("a") == MI("a")

    def test_basic(self):
        exp_instrs = [MI("mr", 3, 5), 
                      MI("foobar"),
                      MI("li", 3, 2),
                      MI("stw", 3, 5, 100)]

        self.builder.mr(3, 5)
        self.builder.foobar()
        self.builder.li(3, 2)
        self.builder.stw(3, 5, 100)
       
        assert self.builder.instrs == exp_instrs
        
        self.builder.blub()
        assert self.builder.instr != exp_instrs

class TestRegallocMov(object):
    
    def setup_method(self, method):
        self.builder = MockBuilder()
        self.asm = instantiate(AssemblerPPC)
        self.asm.mc = self.builder

    def test_immediate_to_reg(self):
        self.asm.regalloc_mov(imm(5), r10)
        big = 2 << 28
        self.asm.regalloc_mov(imm(big), r0)

        exp_instr = [MI("load_imm", r10, 5), 
                     MI("load_imm", r0, big)]
        assert self.asm.mc.instrs == exp_instr

    def test_immediate_to_mem(self):
        self.asm.regalloc_mov(imm(5), stack(6))
        big = 2 << 28
        self.asm.regalloc_mov(imm(big), stack(7))

        exp_instr = [MI("alloc_scratch_reg"),
                     MI("load_imm", r0, 5),
                     MI("store", r0.value, SPP.value, get_spp_offset(6)),
                     MI("free_scratch_reg"),

                     MI("alloc_scratch_reg"),
                     MI("load_imm", r0, big),
                     MI("store", r0.value, SPP.value, get_spp_offset(7)),
                     MI("free_scratch_reg")]
        assert self.asm.mc.instrs == exp_instr

    def test_mem_to_reg(self):
        self.asm.regalloc_mov(stack(5), reg(10))
        self.asm.regalloc_mov(stack(0), reg(0))
        exp_instrs = [MI("load", r10.value, SPP.value, get_spp_offset(5)),
                      MI("load", r0.value, SPP.value, get_spp_offset(0))]
        assert self.asm.mc.instrs == exp_instrs

    def test_mem_to_mem(self):
        self.asm.regalloc_mov(stack(5), stack(6))
        exp_instrs = [
                      MI("alloc_scratch_reg"),
                      MI("load", r0.value, SPP.value, get_spp_offset(5)),
                      MI("store", r0.value, SPP.value, get_spp_offset(6)),
                      MI("free_scratch_reg")]
        assert self.asm.mc.instrs == exp_instrs

    def test_reg_to_reg(self):
        self.asm.regalloc_mov(reg(0), reg(1))
        self.asm.regalloc_mov(reg(5), reg(10))
        exp_instrs = [MI("mr", r1.value, r0.value),
                      MI("mr", r10.value, r5.value)]
        assert self.asm.mc.instrs == exp_instrs

    def test_reg_to_mem(self):
        self.asm.regalloc_mov(reg(5), stack(10))
        self.asm.regalloc_mov(reg(0), stack(2))
        exp_instrs = [MI("store", r5.value, SPP.value, get_spp_offset(10)),
                      MI("store", r0.value, SPP.value, get_spp_offset(2))]
        assert self.asm.mc.instrs == exp_instrs

def reg(i):
    return RegisterLocation(i)

def stack(i):
    return StackLocation(i, get_spp_offset(i))

def get_spp_offset(i):
    return i * 8 + 304