| OLD | NEW |
| (Empty) | |
| 1 //===- subzero/unittest/AssemblerX8632/Locked.cpp -------------------------===// |
| 2 // |
| 3 // The Subzero Code Generator |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 #include "AssemblerX8632/TestUtil.h" |
| 10 |
| 11 namespace Ice { |
| 12 namespace X8632 { |
| 13 namespace Test { |
| 14 namespace { |
| 15 |
| 16 TEST_F(AssemblerX8632LowLevelTest, Mfence) { |
| 17 __ mfence(); |
| 18 |
| 19 static constexpr uint8_t ByteCount = 3; |
| 20 ASSERT_EQ(ByteCount, codeBytesSize()); |
| 21 verifyBytes<ByteCount>(codeBytes(), 0x0F, 0xAE, 0xF0); |
| 22 } |
| 23 |
| 24 TEST_F(AssemblerX8632LowLevelTest, Lock) { |
| 25 __ lock(); |
| 26 |
| 27 static constexpr uint8_t ByteCount = 1; |
| 28 ASSERT_EQ(ByteCount, codeBytesSize()); |
| 29 verifyBytes<ByteCount>(codeBytes(), 0xF0); |
| 30 } |
| 31 |
| 32 TEST_F(AssemblerX8632Test, Xchg) { |
| 33 static constexpr uint32_t Mask8 = 0x000000FF; |
| 34 static constexpr uint32_t Mask16 = 0x0000FFFF; |
| 35 static constexpr uint32_t Mask32 = 0xFFFFFFFF; |
| 36 |
| 37 #define TestImplAddrReg(Value0, Dst1, Value1, Size) \ |
| 38 do { \ |
| 39 static constexpr char TestString[] = \ |
| 40 "(" #Value0 ", " #Dst1 ", " #Value1 ", " #Size ")"; \ |
| 41 const uint32_t T0 = allocateDword(); \ |
| 42 const uint32_t V0 = (Value0)&Mask##Size; \ |
| 43 const uint32_t V1 = (Value1)&Mask##Size; \ |
| 44 \ |
| 45 __ mov(IceType_i##Size, GPRRegister::Encoded_Reg_##Dst1, \ |
| 46 Immediate(Value1)); \ |
| 47 __ xchg(IceType_i##Size, dwordAddress(T0), \ |
| 48 GPRRegister::Encoded_Reg_##Dst1); \ |
| 49 __ And(IceType_i32, GPRRegister::Encoded_Reg_##Dst1, \ |
| 50 Immediate(Mask##Size)); \ |
| 51 \ |
| 52 AssembledTest test = assemble(); \ |
| 53 test.setDwordTo(T0, V0); \ |
| 54 test.run(); \ |
| 55 \ |
| 56 ASSERT_EQ(V0, test.Dst1()) << TestString; \ |
| 57 ASSERT_EQ(V1, test.contentsOfDword(T0)) << TestString; \ |
| 58 reset(); \ |
| 59 } while (0) |
| 60 |
| 61 #define TestImplSize(Dst1, Size) \ |
| 62 do { \ |
| 63 TestImplAddrReg(0xa2b34567, Dst1, 0x0507ddee, Size); \ |
| 64 } while (0) |
| 65 |
| 66 #define TestImpl(Dst1) \ |
| 67 do { \ |
| 68 if (GPRRegister::Encoded_Reg_##Dst1 < 4) { \ |
| 69 TestImplSize(Dst1, 8); \ |
| 70 } \ |
| 71 TestImplSize(Dst1, 16); \ |
| 72 TestImplSize(Dst1, 32); \ |
| 73 } while (0) |
| 74 |
| 75 TestImpl(eax); |
| 76 TestImpl(ebx); |
| 77 TestImpl(ecx); |
| 78 TestImpl(edx); |
| 79 TestImpl(esi); |
| 80 TestImpl(edi); |
| 81 |
| 82 #undef TestImpl |
| 83 #undef TestImplSize |
| 84 #undef TestImplAddrReg |
| 85 } |
| 86 |
| 87 TEST_F(AssemblerX8632Test, Xadd) { |
| 88 static constexpr bool NotLocked = false; |
| 89 static constexpr bool Locked = true; |
| 90 |
| 91 static constexpr uint32_t Mask8 = 0x000000FF; |
| 92 static constexpr uint32_t Mask16 = 0x0000FFFF; |
| 93 static constexpr uint32_t Mask32 = 0xFFFFFFFF; |
| 94 |
| 95 #define TestImplAddrReg(Value0, Dst1, Value1, LockedOrNot, Size) \ |
| 96 do { \ |
| 97 static constexpr char TestString[] = \ |
| 98 "(" #Value0 ", " #Dst1 ", " #Value1 ", " #Size ")"; \ |
| 99 const uint32_t T0 = allocateDword(); \ |
| 100 const uint32_t V0 = (Value0)&Mask##Size; \ |
| 101 const uint32_t V1 = (Value1)&Mask##Size; \ |
| 102 \ |
| 103 __ mov(IceType_i##Size, GPRRegister::Encoded_Reg_##Dst1, \ |
| 104 Immediate(Value1)); \ |
| 105 __ xadd(IceType_i##Size, dwordAddress(T0), \ |
| 106 GPRRegister::Encoded_Reg_##Dst1, LockedOrNot); \ |
| 107 __ And(IceType_i32, GPRRegister::Encoded_Reg_##Dst1, \ |
| 108 Immediate(Mask##Size)); \ |
| 109 \ |
| 110 AssembledTest test = assemble(); \ |
| 111 test.setDwordTo(T0, V0); \ |
| 112 test.run(); \ |
| 113 \ |
| 114 ASSERT_EQ(V0, test.Dst1()) << TestString; \ |
| 115 ASSERT_EQ(Mask##Size &(V1 + V0), test.contentsOfDword(T0)) << TestString; \ |
| 116 reset(); \ |
| 117 } while (0) |
| 118 |
| 119 #define TestImplSize(Dst1, Size) \ |
| 120 do { \ |
| 121 TestImplAddrReg(0xa2b34567, Dst1, 0x0507ddee, NotLocked, Size); \ |
| 122 TestImplAddrReg(0xa2b34567, Dst1, 0x0507ddee, Locked, Size); \ |
| 123 } while (0) |
| 124 |
| 125 #define TestImpl(Dst1) \ |
| 126 do { \ |
| 127 if (GPRRegister::Encoded_Reg_##Dst1 < 4) { \ |
| 128 TestImplSize(Dst1, 8); \ |
| 129 } \ |
| 130 TestImplSize(Dst1, 16); \ |
| 131 TestImplSize(Dst1, 32); \ |
| 132 } while (0) |
| 133 |
| 134 TestImpl(eax); |
| 135 TestImpl(ebx); |
| 136 TestImpl(ecx); |
| 137 TestImpl(edx); |
| 138 TestImpl(esi); |
| 139 TestImpl(edi); |
| 140 |
| 141 #undef TestImpl |
| 142 #undef TestImplSize |
| 143 #undef TestImplAddrReg |
| 144 } |
| 145 |
| 146 TEST_F(AssemblerX8632LowLevelTest, Xadd) { |
| 147 static constexpr bool NotLocked = false; |
| 148 static constexpr bool Locked = true; |
| 149 |
| 150 // Ensures that xadd emits a lock prefix accordingly. |
| 151 { |
| 152 __ xadd(IceType_i8, Address::Absolute(0x1FF00), |
| 153 GPRRegister::Encoded_Reg_esi, NotLocked); |
| 154 static constexpr uint8_t ByteCountNotLocked8 = 7; |
| 155 ASSERT_EQ(ByteCountNotLocked8, codeBytesSize()); |
| 156 verifyBytes<ByteCountNotLocked8>(codeBytes(), 0x0F, 0xC0, 0x35, 0x00, 0xFF, |
| 157 0x01, 0x00); |
| 158 reset(); |
| 159 |
| 160 __ xadd(IceType_i8, Address::Absolute(0x1FF00), |
| 161 GPRRegister::Encoded_Reg_esi, Locked); |
| 162 static constexpr uint8_t ByteCountLocked8 = 1 + ByteCountNotLocked8; |
| 163 ASSERT_EQ(ByteCountLocked8, codeBytesSize()); |
| 164 verifyBytes<ByteCountLocked8>(codeBytes(), 0xF0, 0x0F, 0xC0, 0x35, 0x00, |
| 165 0xFF, 0x01, 0x00); |
| 166 reset(); |
| 167 } |
| 168 |
| 169 { |
| 170 __ xadd(IceType_i16, Address::Absolute(0x1FF00), |
| 171 GPRRegister::Encoded_Reg_esi, NotLocked); |
| 172 static constexpr uint8_t ByteCountNotLocked16 = 8; |
| 173 ASSERT_EQ(ByteCountNotLocked16, codeBytesSize()); |
| 174 verifyBytes<ByteCountNotLocked16>(codeBytes(), 0x66, 0x0F, 0xC1, 0x35, 0x00, |
| 175 0xFF, 0x01, 0x00); |
| 176 reset(); |
| 177 |
| 178 __ xadd(IceType_i16, Address::Absolute(0x1FF00), |
| 179 GPRRegister::Encoded_Reg_esi, Locked); |
| 180 static constexpr uint8_t ByteCountLocked16 = 1 + ByteCountNotLocked16; |
| 181 ASSERT_EQ(ByteCountLocked16, codeBytesSize()); |
| 182 verifyBytes<ByteCountLocked16>(codeBytes(), 0x66, 0xF0, 0x0F, 0xC1, 0x35, |
| 183 0x00, 0xFF, 0x01, 0x00); |
| 184 reset(); |
| 185 } |
| 186 |
| 187 { |
| 188 __ xadd(IceType_i32, Address::Absolute(0x1FF00), |
| 189 GPRRegister::Encoded_Reg_esi, NotLocked); |
| 190 static constexpr uint8_t ByteCountNotLocked32 = 7; |
| 191 ASSERT_EQ(ByteCountNotLocked32, codeBytesSize()); |
| 192 verifyBytes<ByteCountNotLocked32>(codeBytes(), 0x0F, 0xC1, 0x35, 0x00, 0xFF, |
| 193 0x01, 0x00); |
| 194 reset(); |
| 195 |
| 196 __ xadd(IceType_i32, Address::Absolute(0x1FF00), |
| 197 GPRRegister::Encoded_Reg_esi, Locked); |
| 198 static constexpr uint8_t ByteCountLocked32 = 1 + ByteCountNotLocked32; |
| 199 ASSERT_EQ(ByteCountLocked32, codeBytesSize()); |
| 200 verifyBytes<ByteCountLocked32>(codeBytes(), 0xF0, 0x0F, 0xC1, 0x35, 0x00, |
| 201 0xFF, 0x01, 0x00); |
| 202 reset(); |
| 203 } |
| 204 } |
| 205 |
| 206 TEST_F(AssemblerX8632Test, Cmpxchg8b) { |
| 207 static constexpr bool NotLocked = false; |
| 208 static constexpr bool Locked = true; |
| 209 |
| 210 #define TestImpl(Value0, Value1, ValueMem, LockedOrNot) \ |
| 211 do { \ |
| 212 static constexpr char TestString[] = \ |
| 213 "(" #Value0 ", " #Value1 ", " #ValueMem ", " #LockedOrNot ")"; \ |
| 214 const uint32_t T0 = allocateQword(); \ |
| 215 static constexpr uint64_t V0 = ValueMem; \ |
| 216 const uint32_t ZeroFlag = allocateDword(); \ |
| 217 \ |
| 218 __ mov(IceType_i32, GPRRegister::Encoded_Reg_eax, \ |
| 219 Immediate(uint64_t(Value0) & 0xFFFFFFFF)); \ |
| 220 __ mov(IceType_i32, GPRRegister::Encoded_Reg_edx, \ |
| 221 Immediate(uint64_t(Value0) >> 32)); \ |
| 222 __ mov(IceType_i32, GPRRegister::Encoded_Reg_ebx, \ |
| 223 Immediate(uint64_t(Value1) & 0xFFFFFFFF)); \ |
| 224 __ mov(IceType_i32, GPRRegister::Encoded_Reg_ecx, \ |
| 225 Immediate(uint64_t(Value1) >> 32)); \ |
| 226 __ cmpxchg8b(dwordAddress(T0), LockedOrNot); \ |
| 227 __ setcc(Cond::Br_e, dwordAddress(ZeroFlag)); \ |
| 228 \ |
| 229 AssembledTest test = assemble(); \ |
| 230 test.setQwordTo(T0, V0); \ |
| 231 test.setDwordTo(ZeroFlag, uint32_t(0xFF)); \ |
| 232 test.run(); \ |
| 233 \ |
| 234 if (V0 == (Value0)) { \ |
| 235 ASSERT_EQ(uint64_t(Value1), test.contentsOfQword(T0)) << TestString; \ |
| 236 ASSERT_EQ(1u, test.contentsOfDword(ZeroFlag)) << TestString; \ |
| 237 } else { \ |
| 238 ASSERT_EQ(uint64_t(ValueMem) & 0xFFFFFFFF, test.eax()) << TestString; \ |
| 239 ASSERT_EQ((uint64_t(ValueMem) >> 32) & 0xFFFFFFFF, test.edx()) \ |
| 240 << TestString; \ |
| 241 ASSERT_EQ(0u, test.contentsOfDword(ZeroFlag)) << TestString; \ |
| 242 } \ |
| 243 reset(); \ |
| 244 } while (0) |
| 245 |
| 246 TestImpl(0x98987676543210ull, 0x1, 0x98987676543210ull, NotLocked); |
| 247 TestImpl(0x98987676543210ull, 0x1, 0x98987676543210ull, Locked); |
| 248 TestImpl(0x98987676543210ull, 0x1, 0x98987676543211ull, NotLocked); |
| 249 TestImpl(0x98987676543210ull, 0x1, 0x98987676543211ull, Locked); |
| 250 |
| 251 #undef TestImpl |
| 252 } |
| 253 |
| 254 TEST_F(AssemblerX8632LowLevelTest, Cmpxchg8b) { |
| 255 static constexpr bool NotLocked = false; |
| 256 static constexpr bool Locked = true; |
| 257 |
| 258 // Ensures that cmpxchg8b emits a lock prefix accordingly. |
| 259 __ cmpxchg8b(Address::Absolute(0x1FF00), NotLocked); |
| 260 static constexpr uint8_t ByteCountNotLocked = 7; |
| 261 ASSERT_EQ(ByteCountNotLocked, codeBytesSize()); |
| 262 verifyBytes<ByteCountNotLocked>(codeBytes(), 0x0F, 0xC7, 0x0D, 0x00, 0xFF, |
| 263 0x01, 0x00); |
| 264 reset(); |
| 265 |
| 266 __ cmpxchg8b(Address::Absolute(0x1FF00), Locked); |
| 267 static constexpr uint8_t ByteCountLocked = 1 + ByteCountNotLocked; |
| 268 ASSERT_EQ(ByteCountLocked, codeBytesSize()); |
| 269 verifyBytes<ByteCountLocked>(codeBytes(), 0xF0, 0x0F, 0xC7, 0x0D, 0x00, 0xFF, |
| 270 0x01, 0x00); |
| 271 reset(); |
| 272 } |
| 273 |
| 274 TEST_F(AssemblerX8632Test, Cmpxchg) { |
| 275 static constexpr bool NotLocked = false; |
| 276 static constexpr bool Locked = true; |
| 277 |
| 278 static constexpr uint32_t Mask8 = 0x000000FF; |
| 279 static constexpr uint32_t Mask16 = 0x0000FFFF; |
| 280 static constexpr uint32_t Mask32 = 0xFFFFFFFF; |
| 281 |
| 282 #define TestImplAddrReg(Value0, Src, Value1, ValueMem, LockedOrNot, Size) \ |
| 283 do { \ |
| 284 static constexpr char TestString[] = \ |
| 285 "(" #Value0 ", " #Src ", " #Value1 ", " #ValueMem ", " #LockedOrNot \ |
| 286 ", " #Size ")"; \ |
| 287 const uint32_t T0 = allocateDword(); \ |
| 288 static constexpr uint32_t V0 = (ValueMem)&Mask##Size; \ |
| 289 const uint32_t ZeroFlag = allocateDword(); \ |
| 290 \ |
| 291 __ mov(IceType_i##Size, GPRRegister::Encoded_Reg_eax, \ |
| 292 Immediate((Value0)&Mask##Size)); \ |
| 293 __ mov(IceType_i##Size, GPRRegister::Encoded_Reg_##Src, \ |
| 294 Immediate((Value1)&Mask##Size)); \ |
| 295 __ cmpxchg(IceType_i##Size, dwordAddress(T0), \ |
| 296 GPRRegister::Encoded_Reg_##Src, LockedOrNot); \ |
| 297 __ setcc(Cond::Br_e, dwordAddress(ZeroFlag)); \ |
| 298 \ |
| 299 AssembledTest test = assemble(); \ |
| 300 test.setDwordTo(T0, V0); \ |
| 301 test.setDwordTo(ZeroFlag, uint32_t(0xFF)); \ |
| 302 test.run(); \ |
| 303 \ |
| 304 if (V0 == (Mask##Size & (Value0))) { \ |
| 305 ASSERT_EQ(uint32_t((Value1)&Mask##Size), test.contentsOfDword(T0)) \ |
| 306 << TestString; \ |
| 307 ASSERT_EQ(1u, test.contentsOfDword(ZeroFlag)) << TestString; \ |
| 308 } else { \ |
| 309 ASSERT_EQ(uint32_t((ValueMem)&Mask##Size), test.eax()) << TestString; \ |
| 310 ASSERT_EQ(0u, test.contentsOfDword(ZeroFlag)) << TestString; \ |
| 311 } \ |
| 312 reset(); \ |
| 313 } while (0) |
| 314 |
| 315 #define TestImplValue(Value0, Src, Value1, ValueMem, LockedOrNot) \ |
| 316 do { \ |
| 317 if (GPRRegister::Encoded_Reg_##Src < 4) { \ |
| 318 TestImplAddrReg(Value0, Src, Value1, ValueMem, LockedOrNot, 8); \ |
| 319 } \ |
| 320 TestImplAddrReg(Value0, Src, Value1, ValueMem, LockedOrNot, 16); \ |
| 321 TestImplAddrReg(Value0, Src, Value1, ValueMem, LockedOrNot, 32); \ |
| 322 } while (0) |
| 323 |
| 324 #define TestImpl(Src, LockedOrNot) \ |
| 325 do { \ |
| 326 TestImplValue(0xFFFFFFFF, Src, 0x1, 0xFFFFFFFF, LockedOrNot); \ |
| 327 TestImplValue(0x0FFF0F0F, Src, 0x1, 0xFFFFFFFF, LockedOrNot); \ |
| 328 } while (0) |
| 329 |
| 330 TestImpl(ebx, Locked); |
| 331 TestImpl(edx, NotLocked); |
| 332 TestImpl(ecx, Locked); |
| 333 TestImpl(ecx, NotLocked); |
| 334 TestImpl(edx, Locked); |
| 335 TestImpl(edx, NotLocked); |
| 336 TestImpl(esi, Locked); |
| 337 TestImpl(esi, NotLocked); |
| 338 TestImpl(edi, Locked); |
| 339 TestImpl(edi, NotLocked); |
| 340 |
| 341 #undef TestImpl |
| 342 #undef TestImplValue |
| 343 #undef TestImplAddrReg |
| 344 } |
| 345 |
| 346 TEST_F(AssemblerX8632LowLevelTest, Cmpxchg) { |
| 347 static constexpr bool NotLocked = false; |
| 348 static constexpr bool Locked = true; |
| 349 |
| 350 // Ensures that cmpxchg emits a lock prefix accordingly. |
| 351 { |
| 352 __ cmpxchg(IceType_i8, Address::Absolute(0x1FF00), |
| 353 GPRRegister::Encoded_Reg_esi, NotLocked); |
| 354 static constexpr uint8_t ByteCountNotLocked8 = 7; |
| 355 ASSERT_EQ(ByteCountNotLocked8, codeBytesSize()); |
| 356 verifyBytes<ByteCountNotLocked8>(codeBytes(), 0x0F, 0xB0, 0x35, 0x00, 0xFF, |
| 357 0x01, 0x00); |
| 358 reset(); |
| 359 |
| 360 __ cmpxchg(IceType_i8, Address::Absolute(0x1FF00), |
| 361 GPRRegister::Encoded_Reg_esi, Locked); |
| 362 static constexpr uint8_t ByteCountLocked8 = 1 + ByteCountNotLocked8; |
| 363 ASSERT_EQ(ByteCountLocked8, codeBytesSize()); |
| 364 verifyBytes<ByteCountLocked8>(codeBytes(), 0xF0, 0x0F, 0xB0, 0x35, 0x00, |
| 365 0xFF, 0x01, 0x00); |
| 366 reset(); |
| 367 } |
| 368 |
| 369 { |
| 370 __ cmpxchg(IceType_i16, Address::Absolute(0x1FF00), |
| 371 GPRRegister::Encoded_Reg_esi, NotLocked); |
| 372 static constexpr uint8_t ByteCountNotLocked16 = 8; |
| 373 ASSERT_EQ(ByteCountNotLocked16, codeBytesSize()); |
| 374 verifyBytes<ByteCountNotLocked16>(codeBytes(), 0x66, 0x0F, 0xB1, 0x35, 0x00, |
| 375 0xFF, 0x01, 0x00); |
| 376 reset(); |
| 377 |
| 378 __ cmpxchg(IceType_i16, Address::Absolute(0x1FF00), |
| 379 GPRRegister::Encoded_Reg_esi, Locked); |
| 380 static constexpr uint8_t ByteCountLocked16 = 1 + ByteCountNotLocked16; |
| 381 ASSERT_EQ(ByteCountLocked16, codeBytesSize()); |
| 382 verifyBytes<ByteCountLocked16>(codeBytes(), 0x66, 0xF0, 0x0F, 0xB1, 0x35, |
| 383 0x00, 0xFF, 0x01, 0x00); |
| 384 reset(); |
| 385 } |
| 386 |
| 387 { |
| 388 __ cmpxchg(IceType_i32, Address::Absolute(0x1FF00), |
| 389 GPRRegister::Encoded_Reg_esi, NotLocked); |
| 390 static constexpr uint8_t ByteCountNotLocked32 = 7; |
| 391 ASSERT_EQ(ByteCountNotLocked32, codeBytesSize()); |
| 392 verifyBytes<ByteCountNotLocked32>(codeBytes(), 0x0F, 0xB1, 0x35, 0x00, 0xFF, |
| 393 0x01, 0x00); |
| 394 reset(); |
| 395 |
| 396 __ cmpxchg(IceType_i32, Address::Absolute(0x1FF00), |
| 397 GPRRegister::Encoded_Reg_esi, Locked); |
| 398 static constexpr uint8_t ByteCountLocked32 = 1 + ByteCountNotLocked32; |
| 399 ASSERT_EQ(ByteCountLocked32, codeBytesSize()); |
| 400 verifyBytes<ByteCountLocked32>(codeBytes(), 0xF0, 0x0F, 0xB1, 0x35, 0x00, |
| 401 0xFF, 0x01, 0x00); |
| 402 reset(); |
| 403 } |
| 404 } |
| 405 |
| 406 } // end of anonymous namespace |
| 407 } // end of namespace Test |
| 408 } // end of namespace X8632 |
| 409 } // end of namespace Ice |
| OLD | NEW |