| 1 |
import struct, sys |
| 2 |
from rpython.jit.backend.x86.rx86 import R, fits_in_32bits |
| 3 |
from rpython.jit.backend.x86.regloc import * |
| 4 |
from rpython.jit.backend.x86.test.test_rx86 import CodeBuilder32, CodeBuilder64, assert_encodes_as |
| 5 |
from rpython.jit.backend.x86.assembler import heap |
| 6 |
from rpython.jit.backend.x86.arch import IS_X86_64, IS_X86_32 |
| 7 |
from rpython.jit.backend.x86 import codebuf |
| 8 |
from rpython.jit.backend.x86.callbuilder import follow_jump |
| 9 |
from rpython.rlib.rarithmetic import intmask |
| 10 |
import py.test |
| 11 |
|
| 12 |
class LocationCodeBuilder32(CodeBuilder32, LocationCodeBuilder): |
| 13 |
def force_frame_size(self, frame_size): |
| 14 |
pass |
| 15 |
|
| 16 |
def stack_frame_size_delta(self, delta): |
| 17 |
pass |
| 18 |
|
| 19 |
class LocationCodeBuilder64(CodeBuilder64, LocationCodeBuilder): |
| 20 |
def force_frame_size(self, frame_size): |
| 21 |
pass |
| 22 |
|
| 23 |
def stack_frame_size_delta(self, delta): |
| 24 |
pass |
| 25 |
|
| 26 |
cb32 = LocationCodeBuilder32 |
| 27 |
cb64 = LocationCodeBuilder64 |
| 28 |
|
| 29 |
def test_mov_8(): |
| 30 |
assert_encodes_as(cb32, "MOV8_ri", (R.cl, 25), '\xB1\x19') |
| 31 |
|
| 32 |
def test_mov_16(): |
| 33 |
|
| 34 |
|
| 35 |
assert_encodes_as(cb32, "MOV16", (ecx, ebx), '\x66\x89\xD9') |
| 36 |
assert_encodes_as(cb32, "MOV16", |
| 37 |
(AddressLoc(ecx, ImmedLoc(16), 0, 0), ebx), |
| 38 |
'\x66\x89\x59\x10') |
| 39 |
|
| 40 |
assert_encodes_as(cb64, "MOV16", (r8, ebx), '\x66\x41\x89\xD8') |
| 41 |
assert_encodes_as(cb64, "MOV16", (ebx, r8), '\x66\x44\x89\xC3') |
| 42 |
assert_encodes_as(cb64, "MOV16", (ecx, ebx), '\x66\x89\xD9') |
| 43 |
|
| 44 |
expected = '\x66\xC7\xC1\x39\x30' |
| 45 |
assert_encodes_as(cb64, "MOV16", (ecx, ImmedLoc(12345)), expected) |
| 46 |
|
| 47 |
expected = '\x66\xC7\xC1\xC7\xCF' |
| 48 |
assert_encodes_as(cb64, "MOV16", (ecx, ImmedLoc(-12345)), expected) |
| 49 |
|
| 50 |
expected = '\x66\x41\xC7\xC1\x39\x30' |
| 51 |
assert_encodes_as(cb64, "MOV16", (r9, ImmedLoc(12345)), expected) |
| 52 |
|
| 53 |
expected = '\x66\x41\xC7\xC1\xC7\xCF' |
| 54 |
assert_encodes_as(cb64, "MOV16", (r9, ImmedLoc(-12345)), expected) |
| 55 |
assert_encodes_as(cb64, "MOV16", |
| 56 |
(AddressLoc(r13, ImmedLoc(0), 0, 0), ImmedLoc(12345)), |
| 57 |
'\x66\x41\xC7\x45\x00\x39\x30') |
| 58 |
|
| 59 |
def test_cmp_16(): |
| 60 |
|
| 61 |
|
| 62 |
assert_encodes_as(cb32, "CMP16", |
| 63 |
(AddressLoc(ecx, ImmedLoc(0), 0, 0), ImmedLoc(21324)), |
| 64 |
'\x66\x81\x39\x4c\x53') |
| 65 |
assert_encodes_as(cb32, "CMP16", |
| 66 |
(AddressLoc(esi, ImmedLoc(2), 0, 0), ImmedLoc(-12345)), |
| 67 |
'\x66\x81\x7e\x02\xc7\xcf') |
| 68 |
|
| 69 |
assert_encodes_as(cb64, "CMP16", |
| 70 |
(AddressLoc(r13, ImmedLoc(0), 0, 0), ImmedLoc(12345)), |
| 71 |
'\x66\x41\x81\x7D\x00\x39\x30') |
| 72 |
|
| 73 |
def test_relocation(): |
| 74 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 75 |
for target in [0x01020304, -0x05060708, 0x0102030405060708]: |
| 76 |
if target > sys.maxint: |
| 77 |
continue |
| 78 |
mc = codebuf.MachineCodeBlockWrapper() |
| 79 |
mc.CALL(ImmedLoc(target)) |
| 80 |
length = mc.get_relative_pos() |
| 81 |
buf = lltype.malloc(rffi.CCHARP.TO, length, flavor='raw') |
| 82 |
rawstart = rffi.cast(lltype.Signed, buf) |
| 83 |
if IS_X86_32: |
| 84 |
assert length == 5 |
| 85 |
assert mc.relocations == [5] |
| 86 |
expected = "\xE8" + struct.pack('<i', target - (rawstart + 5)) |
| 87 |
elif IS_X86_64: |
| 88 |
assert mc.relocations is None |
| 89 |
if 0 <= target <= 0xffffffff: |
| 90 |
assert length == 9 |
| 91 |
expected = ( |
| 92 |
"\x41\xBB\x04\x03\x02\x01" |
| 93 |
"\x41\xFF\xD3") |
| 94 |
elif -0x80000000 <= target < 0: |
| 95 |
assert length == 10 |
| 96 |
expected = ( |
| 97 |
"\x49\xC7\xC3\xF8\xF8\xF9\xFA" |
| 98 |
"\x41\xFF\xD3") |
| 99 |
else: |
| 100 |
assert length == 13 |
| 101 |
expected = ( |
| 102 |
"\x49\xBB\x08\x07\x06\x05\x04\x03\x02\x01" |
| 103 |
"\x41\xFF\xD3") |
| 104 |
mc.copy_to_raw_memory(rawstart) |
| 105 |
assert ''.join([buf[i] for i in range(length)]) == expected |
| 106 |
lltype.free(buf, flavor='raw') |
| 107 |
|
| 108 |
class Fake32CodeBlockWrapper(codebuf.MachineCodeBlockWrapper): |
| 109 |
def check_stack_size_at_ret(self): |
| 110 |
pass |
| 111 |
|
| 112 |
def test_follow_jump_instructions_32(): |
| 113 |
buf = lltype.malloc(rffi.CCHARP.TO, 80, flavor='raw') |
| 114 |
raw = rffi.cast(lltype.Signed, buf) |
| 115 |
if not fits_in_32bits(raw): |
| 116 |
lltype.free(buf, flavor='raw') |
| 117 |
py.test.skip("not testable") |
| 118 |
mc = Fake32CodeBlockWrapper(); mc.WORD = 4; mc.relocations = [] |
| 119 |
mc.RET() |
| 120 |
mc.copy_to_raw_memory(raw) |
| 121 |
mc = Fake32CodeBlockWrapper(); mc.WORD = 4; mc.relocations = [] |
| 122 |
assert follow_jump(raw) == raw |
| 123 |
mc.JMP(imm(raw)) |
| 124 |
mc.copy_to_raw_memory(raw + 20) |
| 125 |
assert buf[20] == '\xE9' |
| 126 |
assert buf[21] == '\xE7' |
| 127 |
assert buf[22] == '\xFF' |
| 128 |
assert buf[23] == '\xFF' |
| 129 |
assert buf[24] == '\xFF' |
| 130 |
mc = Fake32CodeBlockWrapper(); mc.WORD = 4; mc.relocations = [] |
| 131 |
assert follow_jump(raw + 20) == raw |
| 132 |
mc.JMP(imm(raw)) |
| 133 |
mc.copy_to_raw_memory(raw + 40) |
| 134 |
assert buf[40] == '\xE9' |
| 135 |
assert buf[41] == '\xD3' |
| 136 |
assert buf[42] == '\xFF' |
| 137 |
assert buf[43] == '\xFF' |
| 138 |
assert buf[44] == '\xFF' |
| 139 |
assert follow_jump(raw + 40) == raw |
| 140 |
lltype.free(buf, flavor='raw') |
| 141 |
|
| 142 |
|
| 143 |
class Test64Bits: |
| 144 |
|
| 145 |
def setup_class(cls): |
| 146 |
if not IS_X86_64: |
| 147 |
py.test.skip() |
| 148 |
|
| 149 |
def test_reuse_scratch_register(self): |
| 150 |
base_addr = intmask(0xFEDCBA9876543210) |
| 151 |
cb = LocationCodeBuilder64() |
| 152 |
cb.MOV(ecx, heap(base_addr)) |
| 153 |
cb.MOV(ecx, heap(base_addr + 8)) |
| 154 |
|
| 155 |
expected_instructions = ( |
| 156 |
|
| 157 |
'\x49\xBB\x10\x32\x54\x76\x98\xBA\xDC\xFE' + |
| 158 |
|
| 159 |
'\x49\x8B\x0B' + |
| 160 |
|
| 161 |
'\x49\x8B\x4B\x08' |
| 162 |
) |
| 163 |
assert cb.getvalue() == expected_instructions |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
def test_64bit_address_1(self): |
| 168 |
base_addr = intmask(0xFEDCBA9876543210) |
| 169 |
cb = LocationCodeBuilder64() |
| 170 |
cb.CMP(ecx, AddressLoc(ImmedLoc(0), ImmedLoc(0), 0, base_addr)) |
| 171 |
|
| 172 |
|
| 173 |
expected_instructions = ( |
| 174 |
|
| 175 |
'\x49\xBB\x10\x32\x54\x76\x98\xBA\xDC\xFE' |
| 176 |
|
| 177 |
'\x49\x3B\x0B' |
| 178 |
) |
| 179 |
assert cb.getvalue() == expected_instructions |
| 180 |
|
| 181 |
def test_64bit_address_2(self): |
| 182 |
base_addr = intmask(0xFEDCBA9876543210) |
| 183 |
cb = LocationCodeBuilder64() |
| 184 |
cb.MOV(ecx, AddressLoc(ImmedLoc(0), edx, 3, base_addr)) |
| 185 |
|
| 186 |
|
| 187 |
expected_instructions = ( |
| 188 |
|
| 189 |
'\x49\xBB\x10\x32\x54\x76\x98\xBA\xDC\xFE' |
| 190 |
|
| 191 |
'\x49\x8B\x0C\xD3' |
| 192 |
) |
| 193 |
assert cb.getvalue() == expected_instructions |
| 194 |
|
| 195 |
def test_64bit_address_3(self): |
| 196 |
base_addr = intmask(0xFEDCBA9876543210) |
| 197 |
cb = LocationCodeBuilder64() |
| 198 |
cb.MOV(ecx, AddressLoc(edx, ImmedLoc(0), 0, base_addr)) |
| 199 |
|
| 200 |
|
| 201 |
expected_instructions = ( |
| 202 |
|
| 203 |
'\x49\xBB\x10\x32\x54\x76\x98\xBA\xDC\xFE' |
| 204 |
|
| 205 |
'\x4E\x8D\x1C\x1A' |
| 206 |
|
| 207 |
'\x49\x8B\x0B' |
| 208 |
) |
| 209 |
assert cb.getvalue() == expected_instructions |
| 210 |
|
| 211 |
def test_64bit_address_4(self): |
| 212 |
base_addr = intmask(0xFEDCBA9876543210) |
| 213 |
cb = LocationCodeBuilder64() |
| 214 |
assert cb._scratch_register_value == -1 |
| 215 |
cb.MOV(ecx, AddressLoc(edx, esi, 2, base_addr)) |
| 216 |
assert cb._scratch_register_value == -1 |
| 217 |
|
| 218 |
|
| 219 |
expected_instructions = ( |
| 220 |
|
| 221 |
'\x49\xBB\x10\x32\x54\x76\x98\xBA\xDC\xFE' |
| 222 |
|
| 223 |
'\x4E\x8D\x1C\x1A' |
| 224 |
|
| 225 |
'\x49\x8B\x0C\xB3' |
| 226 |
) |
| 227 |
assert cb.getvalue() == expected_instructions |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
def test_MOV_64bit_constant_into_r11(self): |
| 232 |
base_constant = intmask(0xFEDCBA9876543210) |
| 233 |
cb = LocationCodeBuilder64() |
| 234 |
cb.MOV(r11, imm(base_constant)) |
| 235 |
|
| 236 |
expected_instructions = ( |
| 237 |
|
| 238 |
'\x49\xBB\x10\x32\x54\x76\x98\xBA\xDC\xFE' |
| 239 |
) |
| 240 |
assert cb.getvalue() == expected_instructions |
| 241 |
|
| 242 |
def test_MOV_64bit_constant_into_rax(self): |
| 243 |
base_constant = intmask(0xFEDCBA9876543210) |
| 244 |
cb = LocationCodeBuilder64() |
| 245 |
cb.MOV(eax, imm(base_constant)) |
| 246 |
|
| 247 |
expected_instructions = ( |
| 248 |
|
| 249 |
'\x48\xB8\x10\x32\x54\x76\x98\xBA\xDC\xFE' |
| 250 |
) |
| 251 |
assert cb.getvalue() == expected_instructions |
| 252 |
|
| 253 |
def test_MOV_64bit_address_into_r11(self): |
| 254 |
base_addr = intmask(0xFEDCBA9876543210) |
| 255 |
cb = LocationCodeBuilder64() |
| 256 |
cb.MOV(r11, heap(base_addr)) |
| 257 |
|
| 258 |
expected_instructions = ( |
| 259 |
|
| 260 |
'\x49\xBB\x10\x32\x54\x76\x98\xBA\xDC\xFE' + |
| 261 |
|
| 262 |
'\x4D\x8B\x1B' |
| 263 |
) |
| 264 |
assert cb.getvalue() == expected_instructions |
| 265 |
|
| 266 |
def test_MOV_immed32_into_64bit_address_1(self): |
| 267 |
immed = -0x01234567 |
| 268 |
base_addr = intmask(0xFEDCBA9876543210) |
| 269 |
cb = LocationCodeBuilder64() |
| 270 |
cb.MOV(AddressLoc(ImmedLoc(0), ImmedLoc(0), 0, base_addr), |
| 271 |
ImmedLoc(immed)) |
| 272 |
|
| 273 |
|
| 274 |
expected_instructions = ( |
| 275 |
|
| 276 |
'\x49\xBB\x10\x32\x54\x76\x98\xBA\xDC\xFE' |
| 277 |
|
| 278 |
'\x49\xC7\x03\x99\xBA\xDC\xFE' |
| 279 |
) |
| 280 |
assert cb.getvalue() == expected_instructions |
| 281 |
|
| 282 |
def test_MOV_immed32_into_64bit_address_2(self): |
| 283 |
immed = -0x01234567 |
| 284 |
base_addr = intmask(0xFEDCBA9876543210) |
| 285 |
cb = LocationCodeBuilder64() |
| 286 |
cb.MOV(AddressLoc(ImmedLoc(0), edx, 3, base_addr), |
| 287 |
ImmedLoc(immed)) |
| 288 |
|
| 289 |
|
| 290 |
expected_instructions = ( |
| 291 |
|
| 292 |
'\x49\xBB\x10\x32\x54\x76\x98\xBA\xDC\xFE' |
| 293 |
|
| 294 |
'\x49\xC7\x04\xD3\x99\xBA\xDC\xFE' |
| 295 |
) |
| 296 |
assert cb.getvalue() == expected_instructions |
| 297 |
|
| 298 |
def test_MOV_immed32_into_64bit_address_3(self): |
| 299 |
immed = -0x01234567 |
| 300 |
base_addr = intmask(0xFEDCBA9876543210) |
| 301 |
cb = LocationCodeBuilder64() |
| 302 |
cb.MOV(AddressLoc(edx, ImmedLoc(0), 0, base_addr), |
| 303 |
ImmedLoc(immed)) |
| 304 |
|
| 305 |
|
| 306 |
expected_instructions = ( |
| 307 |
|
| 308 |
'\x49\xBB\x10\x32\x54\x76\x98\xBA\xDC\xFE' |
| 309 |
|
| 310 |
'\x4E\x8D\x1C\x1A' |
| 311 |
|
| 312 |
'\x49\xC7\x03\x99\xBA\xDC\xFE' |
| 313 |
) |
| 314 |
assert cb.getvalue() == expected_instructions |
| 315 |
|
| 316 |
def test_MOV_immed32_into_64bit_address_4(self): |
| 317 |
immed = -0x01234567 |
| 318 |
base_addr = intmask(0xFEDCBA9876543210) |
| 319 |
cb = LocationCodeBuilder64() |
| 320 |
cb.MOV(AddressLoc(edx, esi, 2, base_addr), ImmedLoc(immed)) |
| 321 |
|
| 322 |
|
| 323 |
expected_instructions = ( |
| 324 |
|
| 325 |
'\x49\xBB\x10\x32\x54\x76\x98\xBA\xDC\xFE' |
| 326 |
|
| 327 |
'\x4E\x8D\x1C\x1A' |
| 328 |
|
| 329 |
'\x49\xC7\x04\xB3\x99\xBA\xDC\xFE' |
| 330 |
) |
| 331 |
assert cb.getvalue() == expected_instructions |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
def test_MOV_immed64_into_64bit_address_1(self): |
| 336 |
immed = 0x0123456789ABCDEF |
| 337 |
base_addr = intmask(0xFEDCBA9876543210) |
| 338 |
cb = LocationCodeBuilder64() |
| 339 |
cb.MOV(AddressLoc(ImmedLoc(0), ImmedLoc(0), 0, base_addr), |
| 340 |
ImmedLoc(immed)) |
| 341 |
|
| 342 |
|
| 343 |
expected_instructions = ( |
| 344 |
|
| 345 |
'\x50' |
| 346 |
|
| 347 |
'\x48\xB8\xEF\xCD\xAB\x89\x67\x45\x23\x01' |
| 348 |
|
| 349 |
'\x49\xBB\x10\x32\x54\x76\x98\xBA\xDC\xFE' |
| 350 |
|
| 351 |
'\x49\x89\x03' |
| 352 |
|
| 353 |
'\x58' |
| 354 |
) |
| 355 |
assert cb.getvalue() == expected_instructions |
| 356 |
|
| 357 |
def test_MOV_immed64_into_64bit_address_2(self): |
| 358 |
immed = 0x0123456789ABCDEF |
| 359 |
base_addr = intmask(0xFEDCBA9876543210) |
| 360 |
cb = LocationCodeBuilder64() |
| 361 |
cb.MOV(AddressLoc(ImmedLoc(0), edx, 3, base_addr), |
| 362 |
ImmedLoc(immed)) |
| 363 |
|
| 364 |
|
| 365 |
expected_instructions = ( |
| 366 |
|
| 367 |
'\x50' |
| 368 |
|
| 369 |
'\x48\xB8\xEF\xCD\xAB\x89\x67\x45\x23\x01' |
| 370 |
|
| 371 |
'\x49\xBB\x10\x32\x54\x76\x98\xBA\xDC\xFE' |
| 372 |
|
| 373 |
'\x49\x89\x04\xD3' |
| 374 |
|
| 375 |
'\x58' |
| 376 |
) |
| 377 |
assert cb.getvalue() == expected_instructions |
| 378 |
|
| 379 |
def test_MOV_immed64_into_64bit_address_3(self): |
| 380 |
immed = 0x0123456789ABCDEF |
| 381 |
base_addr = intmask(0xFEDCBA9876543210) |
| 382 |
cb = LocationCodeBuilder64() |
| 383 |
cb.MOV(AddressLoc(eax, ImmedLoc(0), 0, base_addr), |
| 384 |
ImmedLoc(immed)) |
| 385 |
|
| 386 |
|
| 387 |
expected_instructions = ( |
| 388 |
|
| 389 |
'\x52' |
| 390 |
|
| 391 |
'\x48\xBA\xEF\xCD\xAB\x89\x67\x45\x23\x01' |
| 392 |
|
| 393 |
'\x49\xBB\x10\x32\x54\x76\x98\xBA\xDC\xFE' |
| 394 |
|
| 395 |
'\x4E\x8D\x1C\x18' |
| 396 |
|
| 397 |
'\x49\x89\x13' |
| 398 |
|
| 399 |
'\x5A' |
| 400 |
) |
| 401 |
assert cb.getvalue() == expected_instructions |
| 402 |
|
| 403 |
def test_MOV_immed64_into_64bit_address_4(self): |
| 404 |
immed = 0x0123456789ABCDEF |
| 405 |
base_addr = intmask(0xFEDCBA9876543210) |
| 406 |
cb = LocationCodeBuilder64() |
| 407 |
cb.MOV(AddressLoc(edx, eax, 2, base_addr), ImmedLoc(immed)) |
| 408 |
|
| 409 |
|
| 410 |
expected_instructions = ( |
| 411 |
|
| 412 |
'\x51' |
| 413 |
|
| 414 |
'\x48\xB9\xEF\xCD\xAB\x89\x67\x45\x23\x01' |
| 415 |
|
| 416 |
'\x49\xBB\x10\x32\x54\x76\x98\xBA\xDC\xFE' |
| 417 |
|
| 418 |
'\x4E\x8D\x1C\x1A' |
| 419 |
|
| 420 |
'\x49\x89\x0C\x83' |
| 421 |
|
| 422 |
'\x59' |
| 423 |
) |
| 424 |
assert cb.getvalue() == expected_instructions |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
def test_push_immed64(self): |
| 429 |
immed = 0x0123456789ABCDEF |
| 430 |
cb = LocationCodeBuilder64() |
| 431 |
cb.PUSH(imm(immed)) |
| 432 |
|
| 433 |
expected_instructions = ( |
| 434 |
|
| 435 |
'\x49\xBB\xEF\xCD\xAB\x89\x67\x45\x23\x01' |
| 436 |
|
| 437 |
'\x41\x53' |
| 438 |
) |
| 439 |
assert cb.getvalue() == expected_instructions |
| 440 |
|
| 441 |
def test_inc_64bit_address_1(self): |
| 442 |
base_addr = 0x0123456789ABCDEF |
| 443 |
cb = LocationCodeBuilder64() |
| 444 |
cb.INC(AddressLoc(ImmedLoc(0), ImmedLoc(0), 0, base_addr)) |
| 445 |
|
| 446 |
|
| 447 |
expected_instructions = ( |
| 448 |
|
| 449 |
'\x49\xBB\xEF\xCD\xAB\x89\x67\x45\x23\x01' |
| 450 |
|
| 451 |
'\x49\xFF\x03' |
| 452 |
) |
| 453 |
assert cb.getvalue() == expected_instructions |
| 454 |
|
| 455 |
def test_inc_64bit_address_2(self): |
| 456 |
py.test.skip("there is no unary instruction INSN_a so far") |
| 457 |
base_addr = 0x0123456789ABCDEF |
| 458 |
cb = LocationCodeBuilder64() |
| 459 |
cb.INC(AddressLoc(ImmedLoc(0), edx, 3, base_addr)) |
| 460 |
|
| 461 |
xxx |
| 462 |
|
| 463 |
def test_inc_64bit_address_3(self): |
| 464 |
base_addr = 0x0123456789ABCDEF |
| 465 |
cb = LocationCodeBuilder64() |
| 466 |
cb.INC(AddressLoc(eax, ImmedLoc(0), 0, base_addr)) |
| 467 |
|
| 468 |
|
| 469 |
expected_instructions = ( |
| 470 |
|
| 471 |
'\x49\xBB\xEF\xCD\xAB\x89\x67\x45\x23\x01' |
| 472 |
|
| 473 |
'\x4E\x8D\x1C\x18' |
| 474 |
|
| 475 |
'\x49\xFF\x03' |
| 476 |
) |
| 477 |
assert cb.getvalue() == expected_instructions |