| 1 |
import py |
| 2 |
import random, sys, os |
| 3 |
|
| 4 |
from rpython.jit.backend.ppc.codebuilder import BasicPPCAssembler, PPCBuilder |
| 5 |
from rpython.jit.backend.ppc.regname import * |
| 6 |
from rpython.jit.backend.ppc.register import * |
| 7 |
from rpython.jit.backend.ppc import form |
| 8 |
from rpython.jit.backend import detect_cpu |
| 9 |
from rpython.jit.backend.ppc.arch import IS_PPC_32, IS_PPC_64, IS_BIG_ENDIAN |
| 10 |
from rpython.jit.backend.ppc.arch import WORD |
| 11 |
|
| 12 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 13 |
from rpython.rtyper.annlowlevel import llhelper |
| 14 |
|
| 15 |
cpu = detect_cpu.autodetect() |
| 16 |
|
| 17 |
class TestDisassemble(object): |
| 18 |
def test_match(self): |
| 19 |
class A(BasicPPCAssembler): |
| 20 |
insts = [] |
| 21 |
a = A() |
| 22 |
a.add(1, 2, 3) |
| 23 |
inst = a.insts[-1] |
| 24 |
assert A.add.match(inst.assemble()) |
| 25 |
|
| 26 |
|
| 27 |
""" |
| 28 |
Creates the boilerplate code for the tests. |
| 29 |
- Make a PPCBuilder object |
| 30 |
- Let the given test create the machine code |
| 31 |
- Create a function and call it |
| 32 |
- Compare the return value with the expected result |
| 33 |
""" |
| 34 |
def asmtest(expected): |
| 35 |
def testmaker(test): |
| 36 |
def newtest(self): |
| 37 |
a = PPCBuilder() |
| 38 |
test(self, a) |
| 39 |
f = a.get_assembler_function() |
| 40 |
assert f() == expected |
| 41 |
return newtest |
| 42 |
return testmaker |
| 43 |
|
| 44 |
|
| 45 |
""" |
| 46 |
Treats the given bitstring as binary representation |
| 47 |
of an integer in two's complement. |
| 48 |
""" |
| 49 |
def bits_to_signed_int(bits): |
| 50 |
assert len(bits) > 0 |
| 51 |
sign = 1 |
| 52 |
if bits[0] == "1": |
| 53 |
sign = -1 |
| 54 |
bits = bits[1:].replace("0", "$").replace("1", "0").replace("$", "1") |
| 55 |
return sign * (int(bits, 2) + 1) |
| 56 |
|
| 57 |
def hex_to_signed_int(hx): |
| 58 |
return bits_to_signed_int(bin(int(hx, 16))[2:]) |
| 59 |
|
| 60 |
|
| 61 |
class TestAssemble(object): |
| 62 |
def setup_class(cls): |
| 63 |
if cpu not in ["ppc", "ppc64", "ppc-64"]: |
| 64 |
py.test.skip("can't test all of ppcgen on non-PPC!") |
| 65 |
|
| 66 |
|
| 67 |
""" |
| 68 |
Tests are build like this: |
| 69 |
|
| 70 |
@asmtest(expected=<EXPECTED RESULT>) |
| 71 |
def testX(self, assembler): |
| 72 |
<Assembler Code> |
| 73 |
|
| 74 |
|
| 75 |
This is equivalent to: |
| 76 |
|
| 77 |
|
| 78 |
def testX(self): |
| 79 |
assembler = MyPPCAssembler() |
| 80 |
|
| 81 |
<Assembler Code> |
| 82 |
|
| 83 |
f = assembler.assemble() |
| 84 |
assert f() == <EXPECTED RESULT> |
| 85 |
""" |
| 86 |
|
| 87 |
@asmtest(expected=200) |
| 88 |
def test_li(self, a): |
| 89 |
a.li(3, 200) |
| 90 |
a.blr() |
| 91 |
|
| 92 |
@asmtest(expected=7) |
| 93 |
def test_add_imm(self, a): |
| 94 |
a.li(3, 6) |
| 95 |
a.addi(3, 3, 1) |
| 96 |
a.blr() |
| 97 |
|
| 98 |
@asmtest(expected=12341234) |
| 99 |
def test_load_imm(self, a): |
| 100 |
a.load_imm(r10, 12341234) |
| 101 |
a.mtctr(10) |
| 102 |
a.mfctr(11) |
| 103 |
a.mr(3, 11) |
| 104 |
a.blr() |
| 105 |
|
| 106 |
@asmtest(expected=33333333) |
| 107 |
def test_add_reg(self, a): |
| 108 |
a.load_imm(r10, 11111111) |
| 109 |
a.load_imm(r11, 22222222) |
| 110 |
a.add(12, 10, 11) |
| 111 |
a.mr(3, 12) |
| 112 |
a.blr() |
| 113 |
|
| 114 |
@asmtest(expected=-1000) |
| 115 |
def test_add_pos_and_neg(self, a): |
| 116 |
a.load_imm(r10, 2000) |
| 117 |
a.load_imm(r11, -3000) |
| 118 |
a.add(3, 10, 11) |
| 119 |
a.blr() |
| 120 |
|
| 121 |
@asmtest(expected=7) |
| 122 |
def test_sub_imm(self, a): |
| 123 |
a.li(3, 10) |
| 124 |
a.subi(3, 3, 3) |
| 125 |
a.blr() |
| 126 |
|
| 127 |
@asmtest(expected=(123435 - 76457)) |
| 128 |
def test_sub_reg(self, a): |
| 129 |
a.load_imm(r5, 123435) |
| 130 |
a.load_imm(r6, 76457) |
| 131 |
a.sub(3, 5, 6) |
| 132 |
a.blr() |
| 133 |
|
| 134 |
@asmtest(expected=(10000 * 5000)) |
| 135 |
def test_mul_imm(self, a): |
| 136 |
a.load_imm(r3, 10000) |
| 137 |
a.mulli(3, 3, 5000) |
| 138 |
a.blr() |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
@asmtest(expected=bits_to_signed_int('11010100101001010001000000000000')) |
| 143 |
def test_mullw(self, a): |
| 144 |
word = 1000000 |
| 145 |
a.load_imm(r5, word) |
| 146 |
a.load_imm(r6, word) |
| 147 |
a.mullw(3, 5, 6) |
| 148 |
if IS_PPC_64: |
| 149 |
a.extsw(3, 3) |
| 150 |
a.blr() |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
@asmtest(expected=int('11101000', 2)) |
| 155 |
def test_mulhw(self, a): |
| 156 |
word = 1000000 |
| 157 |
a.load_imm(r5, word) |
| 158 |
a.load_imm(r6, word) |
| 159 |
a.mulhw(3, 5, 6) |
| 160 |
if IS_PPC_64: |
| 161 |
a.extsw(3, 3) |
| 162 |
a.blr() |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
@asmtest(expected=int('11101000', 2)) |
| 167 |
def test_mulhwu(self, a): |
| 168 |
word = 1000000 |
| 169 |
a.load_imm(r5, word) |
| 170 |
a.load_imm(r6, word) |
| 171 |
a.mulhwu(3, 5, 6) |
| 172 |
if IS_PPC_64: |
| 173 |
a.extsw(3, 3) |
| 174 |
a.blr() |
| 175 |
|
| 176 |
@asmtest(expected=10000) |
| 177 |
def test_divw(self, a): |
| 178 |
divident = 1000000 |
| 179 |
divisor = 100 |
| 180 |
a.load_imm(r10, divident) |
| 181 |
a.load_imm(r11, divisor) |
| 182 |
a.divw(3, 10, 11) |
| 183 |
a.blr() |
| 184 |
|
| 185 |
def test_call_function(self): |
| 186 |
functype = lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Signed)) |
| 187 |
call_addr = rffi.cast(lltype.Signed, llhelper(functype, func)) |
| 188 |
a = PPCBuilder() |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
a.li(3, 50) |
| 198 |
if IS_PPC_32: |
| 199 |
a.load_imm(r10, call_addr) |
| 200 |
elif IS_BIG_ENDIAN: |
| 201 |
|
| 202 |
a.load_from_addr(r10, SCRATCH2, call_addr) |
| 203 |
a.load_from_addr(r2, SCRATCH2, call_addr+WORD) |
| 204 |
a.load_from_addr(r11, SCRATCH2, call_addr+2*WORD) |
| 205 |
py.test.skip("this test started segfaulting on gcc110, but even " |
| 206 |
"reverting to old versions of the code still segfault, " |
| 207 |
"so not clue. Maybe something like a ctypes issue") |
| 208 |
else: |
| 209 |
|
| 210 |
|
| 211 |
a.load_imm(r10, call_addr) |
| 212 |
a.mr(12, 10) |
| 213 |
a.mtctr(10) |
| 214 |
a.bctr() |
| 215 |
a.blr() |
| 216 |
|
| 217 |
f = a.get_assembler_function() |
| 218 |
assert f() == 65 |
| 219 |
|
| 220 |
@asmtest(expected=0) |
| 221 |
def test_and(self, a): |
| 222 |
a.load_imm(r10, 8) |
| 223 |
a.load_imm(r11, 7) |
| 224 |
a.and_(3, 10, 11) |
| 225 |
a.blr() |
| 226 |
|
| 227 |
@asmtest(expected=15) |
| 228 |
def test_or(self, a): |
| 229 |
a.load_imm(r10, 8) |
| 230 |
a.load_imm(r11, 7) |
| 231 |
a.or_(3, 10, 11) |
| 232 |
a.blr() |
| 233 |
|
| 234 |
@asmtest(expected=15) |
| 235 |
def test_nand(self, a): |
| 236 |
a.load_imm(r10, 8) |
| 237 |
a.load_imm(r11, 7) |
| 238 |
a.nand(3, 10, 11) |
| 239 |
a.load_imm(r12, 0x0000000F) |
| 240 |
a.and_(3, 3, 12) |
| 241 |
a.blr() |
| 242 |
|
| 243 |
@asmtest(expected=1) |
| 244 |
def test_nor(self, a): |
| 245 |
a.load_imm(r10, 10) |
| 246 |
a.load_imm(r11, 6) |
| 247 |
a.nor(3, 10, 11) |
| 248 |
a.load_imm(r12, 0x0000000F) |
| 249 |
a.and_(3, 3, 12) |
| 250 |
a.blr() |
| 251 |
|
| 252 |
@asmtest(expected=5) |
| 253 |
def test_xor(self, a): |
| 254 |
a.load_imm(r10, 15) |
| 255 |
a.load_imm(r11, 10) |
| 256 |
a.xor(3, 10, 11) |
| 257 |
a.blr() |
| 258 |
|
| 259 |
@asmtest(expected=0x120) |
| 260 |
def test_slw(self, a): |
| 261 |
a.load_imm(r10, 9) |
| 262 |
a.load_imm(r11, 5) |
| 263 |
a.slw(3, 10, 11) |
| 264 |
a.blr() |
| 265 |
|
| 266 |
@asmtest(expected=9) |
| 267 |
def test_srw(self, a): |
| 268 |
a.load_imm(r10, 0x120) |
| 269 |
a.load_imm(r11, 5) |
| 270 |
a.srw(3, 10, 11) |
| 271 |
a.blr() |
| 272 |
|
| 273 |
def test_neg(self): |
| 274 |
a = PPCBuilder() |
| 275 |
a.load_imm(r10, 0x0000F0F0) |
| 276 |
a.neg(3, 10) |
| 277 |
a.blr() |
| 278 |
f = a.get_assembler_function() |
| 279 |
assert f() == hex_to_signed_int("FFFF0F10") |
| 280 |
|
| 281 |
def test_load_and_store(self): |
| 282 |
a = PPCBuilder() |
| 283 |
word1 = 1000 |
| 284 |
word2 = 2000 |
| 285 |
p = lltype.malloc(rffi.CArray(lltype.Signed), 2, flavor="raw") |
| 286 |
|
| 287 |
a.load_imm(r10, word1) |
| 288 |
a.load_imm(r11, word2) |
| 289 |
|
| 290 |
a.load_imm(r8, rffi.cast(lltype.Signed, p)) |
| 291 |
a.load_imm(r9, rffi.cast(lltype.Signed, p) + WORD) |
| 292 |
|
| 293 |
a.stw(10, 8, 0) |
| 294 |
a.stw(11, 9, 0) |
| 295 |
a.lwz(4, 8, 0) |
| 296 |
a.lwz(5, 9, 0) |
| 297 |
a.add(3, 4, 5) |
| 298 |
a.blr() |
| 299 |
f = a.get_assembler_function() |
| 300 |
assert f() == word1 + word2 |
| 301 |
lltype.free(p, flavor="raw") |
| 302 |
|
| 303 |
def test_load_from(self): |
| 304 |
a = PPCBuilder() |
| 305 |
|
| 306 |
p = lltype.malloc(rffi.CArray(rffi.LONG), 1, flavor="raw") |
| 307 |
addr = rffi.cast(lltype.Signed, p) |
| 308 |
p[0] = rffi.cast(rffi.LONG, 200) |
| 309 |
|
| 310 |
a.load_from_addr(r3, SCRATCH2, addr) |
| 311 |
a.blr() |
| 312 |
f = a.get_assembler_function() |
| 313 |
assert f() == 200 |
| 314 |
p[0] = rffi.cast(rffi.LONG, 300) |
| 315 |
assert f() == 300 |
| 316 |
lltype.free(p, flavor="raw") |
| 317 |
|
| 318 |
|
| 319 |
def func(arg): |
| 320 |
return arg + 15 |
| 321 |
|
| 322 |
def is_64_bit_arch(): |
| 323 |
import sys |
| 324 |
return sys.maxint == 9223372036854775807 |