| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
#include <stdlib.h> |
| 6 |
|
| 7 |
#include "jit/shared/IonAssemblerBufferWithConstantPools.h" |
| 8 |
#include "jsapi-tests/tests.h" |
| 9 |
|
| 10 |
// Tests for classes in: |
| 11 |
// |
| 12 |
// jit/shared/IonAssemblerBuffer.h |
| 13 |
// jit/shared/IonAssemblerBufferWithConstantPools.h |
| 14 |
// |
| 15 |
// Classes in js::jit tested: |
| 16 |
// |
| 17 |
// BufferOffset |
| 18 |
// BufferSlice (implicitly) |
| 19 |
// AssemblerBuffer |
| 20 |
// |
| 21 |
// BranchDeadlineSet |
| 22 |
// Pool (implicitly) |
| 23 |
// AssemblerBufferWithConstantPools |
| 24 |
// |
| 25 |
|
| 26 |
BEGIN_TEST(testAssemblerBuffer_BufferOffset) { |
| 27 |
using js::jit::BufferOffset; |
| 28 |
|
| 29 |
BufferOffset off1; |
| 30 |
BufferOffset off2(10); |
| 31 |
|
| 32 |
CHECK(!off1.assigned()); |
| 33 |
CHECK(off2.assigned()); |
| 34 |
CHECK_EQUAL(off2.getOffset(), 10); |
| 35 |
off1 = off2; |
| 36 |
CHECK(off1.assigned()); |
| 37 |
CHECK_EQUAL(off1.getOffset(), 10); |
| 38 |
|
| 39 |
return true; |
| 40 |
} |
| 41 |
END_TEST(testAssemblerBuffer_BufferOffset) |
| 42 |
|
| 43 |
BEGIN_TEST(testAssemblerBuffer_AssemblerBuffer) { |
| 44 |
using js::jit::BufferOffset; |
| 45 |
using AsmBuf = js::jit::AssemblerBuffer<5 * sizeof(uint32_t), uint32_t>; |
| 46 |
|
| 47 |
AsmBuf ab; |
| 48 |
CHECK(ab.isAligned(16)); |
| 49 |
CHECK_EQUAL(ab.size(), 0u); |
| 50 |
CHECK_EQUAL(ab.nextOffset().getOffset(), 0); |
| 51 |
CHECK(!ab.oom()); |
| 52 |
|
| 53 |
BufferOffset off1 = ab.putInt(1000017); |
| 54 |
CHECK_EQUAL(off1.getOffset(), 0); |
| 55 |
CHECK_EQUAL(ab.size(), 4u); |
| 56 |
CHECK_EQUAL(ab.nextOffset().getOffset(), 4); |
| 57 |
CHECK(!ab.isAligned(16)); |
| 58 |
CHECK(ab.isAligned(4)); |
| 59 |
CHECK(ab.isAligned(1)); |
| 60 |
CHECK_EQUAL(*ab.getInst(off1), 1000017u); |
| 61 |
|
| 62 |
BufferOffset off2 = ab.putInt(1000018); |
| 63 |
CHECK_EQUAL(off2.getOffset(), 4); |
| 64 |
|
| 65 |
BufferOffset off3 = ab.putInt(1000019); |
| 66 |
CHECK_EQUAL(off3.getOffset(), 8); |
| 67 |
|
| 68 |
BufferOffset off4 = ab.putInt(1000020); |
| 69 |
CHECK_EQUAL(off4.getOffset(), 12); |
| 70 |
CHECK_EQUAL(ab.size(), 16u); |
| 71 |
CHECK_EQUAL(ab.nextOffset().getOffset(), 16); |
| 72 |
|
| 73 |
// Last one in the slice. |
| 74 |
BufferOffset off5 = ab.putInt(1000021); |
| 75 |
CHECK_EQUAL(off5.getOffset(), 16); |
| 76 |
CHECK_EQUAL(ab.size(), 20u); |
| 77 |
CHECK_EQUAL(ab.nextOffset().getOffset(), 20); |
| 78 |
|
| 79 |
BufferOffset off6 = ab.putInt(1000022); |
| 80 |
CHECK_EQUAL(off6.getOffset(), 20); |
| 81 |
CHECK_EQUAL(ab.size(), 24u); |
| 82 |
CHECK_EQUAL(ab.nextOffset().getOffset(), 24); |
| 83 |
|
| 84 |
// Reference previous slice. Excercise the finger. |
| 85 |
CHECK_EQUAL(*ab.getInst(off1), 1000017u); |
| 86 |
CHECK_EQUAL(*ab.getInst(off6), 1000022u); |
| 87 |
CHECK_EQUAL(*ab.getInst(off1), 1000017u); |
| 88 |
CHECK_EQUAL(*ab.getInst(off5), 1000021u); |
| 89 |
|
| 90 |
// Too much data for one slice. |
| 91 |
const uint32_t fixdata[] = {2000036, 2000037, 2000038, |
| 92 |
2000039, 2000040, 2000041}; |
| 93 |
|
| 94 |
// Split payload across multiple slices. |
| 95 |
CHECK_EQUAL(ab.nextOffset().getOffset(), 24); |
| 96 |
BufferOffset good1 = ab.putBytesLarge(sizeof(fixdata), fixdata); |
| 97 |
CHECK_EQUAL(good1.getOffset(), 24); |
| 98 |
CHECK_EQUAL(ab.nextOffset().getOffset(), 48); |
| 99 |
CHECK_EQUAL(*ab.getInst(good1), 2000036u); |
| 100 |
CHECK_EQUAL(*ab.getInst(BufferOffset(32)), 2000038u); |
| 101 |
CHECK_EQUAL(*ab.getInst(BufferOffset(36)), 2000039u); |
| 102 |
CHECK_EQUAL(*ab.getInst(BufferOffset(40)), 2000040u); |
| 103 |
CHECK_EQUAL(*ab.getInst(BufferOffset(44)), 2000041u); |
| 104 |
|
| 105 |
return true; |
| 106 |
} |
| 107 |
END_TEST(testAssemblerBuffer_AssemblerBuffer) |
| 108 |
|
| 109 |
BEGIN_TEST(testAssemblerBuffer_BranchDeadlineSet) { |
| 110 |
using DLSet = js::jit::BranchDeadlineSet<3>; |
| 111 |
using js::jit::BufferOffset; |
| 112 |
|
| 113 |
js::LifoAlloc alloc(1024, js::MallocArena); |
| 114 |
DLSet dls(alloc); |
| 115 |
|
| 116 |
CHECK(dls.empty()); |
| 117 |
CHECK(alloc.isEmpty()); // Constructor must be infallible. |
| 118 |
CHECK_EQUAL(dls.size(), 0u); |
| 119 |
CHECK_EQUAL(dls.maxRangeSize(), 0u); |
| 120 |
|
| 121 |
// Removing non-existant deadline is OK. |
| 122 |
dls.removeDeadline(1, BufferOffset(7)); |
| 123 |
|
| 124 |
// Add deadlines in increasing order as intended. This is optimal. |
| 125 |
dls.addDeadline(1, BufferOffset(10)); |
| 126 |
CHECK(!dls.empty()); |
| 127 |
CHECK_EQUAL(dls.size(), 1u); |
| 128 |
CHECK_EQUAL(dls.maxRangeSize(), 1u); |
| 129 |
CHECK_EQUAL(dls.earliestDeadline().getOffset(), 10); |
| 130 |
CHECK_EQUAL(dls.earliestDeadlineRange(), 1u); |
| 131 |
|
| 132 |
// Removing non-existant deadline is OK. |
| 133 |
dls.removeDeadline(1, BufferOffset(7)); |
| 134 |
dls.removeDeadline(1, BufferOffset(17)); |
| 135 |
dls.removeDeadline(0, BufferOffset(10)); |
| 136 |
CHECK_EQUAL(dls.size(), 1u); |
| 137 |
CHECK_EQUAL(dls.maxRangeSize(), 1u); |
| 138 |
|
| 139 |
// Two identical deadlines for different ranges. |
| 140 |
dls.addDeadline(2, BufferOffset(10)); |
| 141 |
CHECK(!dls.empty()); |
| 142 |
CHECK_EQUAL(dls.size(), 2u); |
| 143 |
CHECK_EQUAL(dls.maxRangeSize(), 1u); |
| 144 |
CHECK_EQUAL(dls.earliestDeadline().getOffset(), 10); |
| 145 |
|
| 146 |
// It doesn't matter which range earliestDeadlineRange() reports first, |
| 147 |
// but it must report both. |
| 148 |
if (dls.earliestDeadlineRange() == 1) { |
| 149 |
dls.removeDeadline(1, BufferOffset(10)); |
| 150 |
CHECK_EQUAL(dls.earliestDeadline().getOffset(), 10); |
| 151 |
CHECK_EQUAL(dls.earliestDeadlineRange(), 2u); |
| 152 |
} else { |
| 153 |
CHECK_EQUAL(dls.earliestDeadlineRange(), 2u); |
| 154 |
dls.removeDeadline(2, BufferOffset(10)); |
| 155 |
CHECK_EQUAL(dls.earliestDeadline().getOffset(), 10); |
| 156 |
CHECK_EQUAL(dls.earliestDeadlineRange(), 1u); |
| 157 |
} |
| 158 |
|
| 159 |
// Add deadline which is the front of range 0, but not the global earliest. |
| 160 |
dls.addDeadline(0, BufferOffset(20)); |
| 161 |
CHECK_EQUAL(dls.earliestDeadline().getOffset(), 10); |
| 162 |
CHECK(dls.earliestDeadlineRange() > 0); |
| 163 |
|
| 164 |
// Non-optimal add to front of single-entry range 0. |
| 165 |
dls.addDeadline(0, BufferOffset(15)); |
| 166 |
CHECK_EQUAL(dls.earliestDeadline().getOffset(), 10); |
| 167 |
CHECK(dls.earliestDeadlineRange() > 0); |
| 168 |
|
| 169 |
// Append to 2-entry range 0. |
| 170 |
dls.addDeadline(0, BufferOffset(30)); |
| 171 |
CHECK_EQUAL(dls.earliestDeadline().getOffset(), 10); |
| 172 |
CHECK(dls.earliestDeadlineRange() > 0); |
| 173 |
|
| 174 |
// Add penultimate entry. |
| 175 |
dls.addDeadline(0, BufferOffset(25)); |
| 176 |
CHECK_EQUAL(dls.earliestDeadline().getOffset(), 10); |
| 177 |
CHECK(dls.earliestDeadlineRange() > 0); |
| 178 |
|
| 179 |
// Prepend, stealing earliest from other range. |
| 180 |
dls.addDeadline(0, BufferOffset(5)); |
| 181 |
CHECK_EQUAL(dls.earliestDeadline().getOffset(), 5); |
| 182 |
CHECK_EQUAL(dls.earliestDeadlineRange(), 0u); |
| 183 |
|
| 184 |
// Remove central element. |
| 185 |
dls.removeDeadline(0, BufferOffset(20)); |
| 186 |
CHECK_EQUAL(dls.earliestDeadline().getOffset(), 5); |
| 187 |
CHECK_EQUAL(dls.earliestDeadlineRange(), 0u); |
| 188 |
|
| 189 |
// Remove front, giving back the lead. |
| 190 |
dls.removeDeadline(0, BufferOffset(5)); |
| 191 |
CHECK_EQUAL(dls.earliestDeadline().getOffset(), 10); |
| 192 |
CHECK(dls.earliestDeadlineRange() > 0); |
| 193 |
|
| 194 |
// Remove front, giving back earliest to range 0. |
| 195 |
dls.removeDeadline(dls.earliestDeadlineRange(), BufferOffset(10)); |
| 196 |
CHECK_EQUAL(dls.earliestDeadline().getOffset(), 15); |
| 197 |
CHECK_EQUAL(dls.earliestDeadlineRange(), 0u); |
| 198 |
|
| 199 |
// Remove tail. |
| 200 |
dls.removeDeadline(0, BufferOffset(30)); |
| 201 |
CHECK_EQUAL(dls.earliestDeadline().getOffset(), 15); |
| 202 |
CHECK_EQUAL(dls.earliestDeadlineRange(), 0u); |
| 203 |
|
| 204 |
// Now range 0 = [15, 25]. |
| 205 |
CHECK_EQUAL(dls.size(), 2u); |
| 206 |
dls.removeDeadline(0, BufferOffset(25)); |
| 207 |
dls.removeDeadline(0, BufferOffset(15)); |
| 208 |
CHECK(dls.empty()); |
| 209 |
|
| 210 |
return true; |
| 211 |
} |
| 212 |
END_TEST(testAssemblerBuffer_BranchDeadlineSet) |
| 213 |
|
| 214 |
// Mock Assembler class for testing the AssemblerBufferWithConstantPools |
| 215 |
// callbacks. |
| 216 |
namespace { |
| 217 |
|
| 218 |
struct TestAssembler; |
| 219 |
|
| 220 |
using AsmBufWithPool = js::jit::AssemblerBufferWithConstantPools< |
| 221 |
/* SliceSize */ 5 * sizeof(uint32_t), |
| 222 |
/* InstSize */ 4, |
| 223 |
/* Inst */ uint32_t, |
| 224 |
/* Asm */ TestAssembler, |
| 225 |
/* NumShortBranchRanges */ 3>; |
| 226 |
|
| 227 |
struct TestAssembler { |
| 228 |
// Mock instruction set: |
| 229 |
// |
| 230 |
// 0x1111xxxx - align filler instructions. |
| 231 |
// 0x2222xxxx - manually inserted 'arith' instructions. |
| 232 |
// 0xaaaaxxxx - noop filler instruction. |
| 233 |
// 0xb0bbxxxx - branch xxxx bytes forward. (Pool guard). |
| 234 |
// 0xb1bbxxxx - branch xxxx bytes forward. (Short-range branch). |
| 235 |
// 0xb2bbxxxx - branch xxxx bytes forward. (Veneer branch). |
| 236 |
// 0xb3bbxxxx - branch xxxx bytes forward. (Patched short-range branch). |
| 237 |
// 0xc0ccxxxx - constant pool load (uninitialized). |
| 238 |
// 0xc1ccxxxx - constant pool load to index xxxx. |
| 239 |
// 0xc2ccxxxx - constant pool load xxxx bytes ahead. |
| 240 |
// 0xffffxxxx - pool header with xxxx bytes. |
| 241 |
|
| 242 |
static const unsigned BranchRange = 36; |
| 243 |
|
| 244 |
static void InsertIndexIntoTag(uint8_t* load_, uint32_t index) { |
| 245 |
uint32_t* load = reinterpret_cast<uint32_t*>(load_); |
| 246 |
MOZ_ASSERT(*load == 0xc0cc0000, |
| 247 |
"Expected uninitialized constant pool load"); |
| 248 |
MOZ_ASSERT(index < 0x10000); |
| 249 |
*load = 0xc1cc0000 + index; |
| 250 |
} |
| 251 |
|
| 252 |
static void PatchConstantPoolLoad(void* loadAddr, void* constPoolAddr) { |
| 253 |
uint32_t* load = reinterpret_cast<uint32_t*>(loadAddr); |
| 254 |
uint32_t index = *load & 0xffff; |
| 255 |
MOZ_ASSERT(*load == (0xc1cc0000 | index), |
| 256 |
"Expected constant pool load(index)"); |
| 257 |
ptrdiff_t offset = reinterpret_cast<uint8_t*>(constPoolAddr) - |
| 258 |
reinterpret_cast<uint8_t*>(loadAddr); |
| 259 |
offset += index * 4; |
| 260 |
MOZ_ASSERT(offset % 4 == 0, "Unaligned constant pool"); |
| 261 |
MOZ_ASSERT(offset > 0 && offset < 0x10000, "Pool out of range"); |
| 262 |
*load = 0xc2cc0000 + offset; |
| 263 |
} |
| 264 |
|
| 265 |
static void WritePoolGuard(js::jit::BufferOffset branch, uint32_t* dest, |
| 266 |
js::jit::BufferOffset afterPool) { |
| 267 |
MOZ_ASSERT(branch.assigned()); |
| 268 |
MOZ_ASSERT(afterPool.assigned()); |
| 269 |
size_t branchOff = branch.getOffset(); |
| 270 |
size_t afterPoolOff = afterPool.getOffset(); |
| 271 |
MOZ_ASSERT(afterPoolOff > branchOff); |
| 272 |
uint32_t delta = afterPoolOff - branchOff; |
| 273 |
*dest = 0xb0bb0000 + delta; |
| 274 |
} |
| 275 |
|
| 276 |
static void WritePoolHeader(void* start, js::jit::Pool* p, bool isNatural) { |
| 277 |
MOZ_ASSERT(!isNatural, "Natural pool guards not implemented."); |
| 278 |
uint32_t* hdr = reinterpret_cast<uint32_t*>(start); |
| 279 |
*hdr = 0xffff0000 + p->getPoolSize(); |
| 280 |
} |
| 281 |
|
| 282 |
static void PatchShortRangeBranchToVeneer(AsmBufWithPool* buffer, |
| 283 |
unsigned rangeIdx, |
| 284 |
js::jit::BufferOffset deadline, |
| 285 |
js::jit::BufferOffset veneer) { |
| 286 |
size_t branchOff = deadline.getOffset() - BranchRange; |
| 287 |
size_t veneerOff = veneer.getOffset(); |
| 288 |
uint32_t* branch = buffer->getInst(js::jit::BufferOffset(branchOff)); |
| 289 |
|
| 290 |
MOZ_ASSERT((*branch & 0xffff0000) == 0xb1bb0000, |
| 291 |
"Expected short-range branch instruction"); |
| 292 |
// Copy branch offset to veneer. A real instruction set would require |
| 293 |
// some adjustment of the label linked-list. |
| 294 |
*buffer->getInst(veneer) = 0xb2bb0000 | (*branch & 0xffff); |
| 295 |
MOZ_ASSERT(veneerOff > branchOff, "Veneer should follow branch"); |
| 296 |
*branch = 0xb3bb0000 + (veneerOff - branchOff); |
| 297 |
} |
| 298 |
}; |
| 299 |
} // namespace |
| 300 |
|
| 301 |
BEGIN_TEST(testAssemblerBuffer_AssemblerBufferWithConstantPools) { |
| 302 |
using js::jit::BufferOffset; |
| 303 |
|
| 304 |
AsmBufWithPool ab(/* guardSize= */ 1, |
| 305 |
/* headerSize= */ 1, |
| 306 |
/* instBufferAlign(unused)= */ 0, |
| 307 |
/* poolMaxOffset= */ 17, |
| 308 |
/* pcBias= */ 0, |
| 309 |
/* alignFillInst= */ 0x11110000, |
| 310 |
/* nopFillInst= */ 0xaaaa0000, |
| 311 |
/* nopFill= */ 0); |
| 312 |
|
| 313 |
CHECK(ab.isAligned(16)); |
| 314 |
CHECK_EQUAL(ab.size(), 0u); |
| 315 |
CHECK_EQUAL(ab.nextOffset().getOffset(), 0); |
| 316 |
CHECK(!ab.oom()); |
| 317 |
|
| 318 |
// Each slice holds 5 instructions. Trigger a constant pool inside the slice. |
| 319 |
uint32_t poolLoad[] = {0xc0cc0000}; |
| 320 |
uint32_t poolData[] = {0xdddd0000, 0xdddd0001, 0xdddd0002, 0xdddd0003}; |
| 321 |
AsmBufWithPool::PoolEntry pe; |
| 322 |
BufferOffset load = |
| 323 |
ab.allocEntry(1, 1, (uint8_t*)poolLoad, (uint8_t*)poolData, &pe); |
| 324 |
CHECK_EQUAL(pe.index(), 0u); |
| 325 |
CHECK_EQUAL(load.getOffset(), 0); |
| 326 |
|
| 327 |
// Pool hasn't been emitted yet. Load has been patched by |
| 328 |
// InsertIndexIntoTag. |
| 329 |
CHECK_EQUAL(*ab.getInst(load), 0xc1cc0000); |
| 330 |
|
| 331 |
// Expected layout: |
| 332 |
// |
| 333 |
// 0: load [pc+16] |
| 334 |
// 4: 0x22220001 |
| 335 |
// 8: guard branch pc+12 |
| 336 |
// 12: pool header |
| 337 |
// 16: poolData |
| 338 |
// 20: 0x22220002 |
| 339 |
// |
| 340 |
ab.putInt(0x22220001); |
| 341 |
// One could argue that the pool should be flushed here since there is no |
| 342 |
// more room. However, the current implementation doesn't dump pool until |
| 343 |
// asked to add data: |
| 344 |
ab.putInt(0x22220002); |
| 345 |
|
| 346 |
CHECK_EQUAL(*ab.getInst(BufferOffset(0)), 0xc2cc0010u); |
| 347 |
CHECK_EQUAL(*ab.getInst(BufferOffset(4)), 0x22220001u); |
| 348 |
CHECK_EQUAL(*ab.getInst(BufferOffset(8)), 0xb0bb000cu); |
| 349 |
CHECK_EQUAL(*ab.getInst(BufferOffset(12)), 0xffff0004u); |
| 350 |
CHECK_EQUAL(*ab.getInst(BufferOffset(16)), 0xdddd0000u); |
| 351 |
CHECK_EQUAL(*ab.getInst(BufferOffset(20)), 0x22220002u); |
| 352 |
|
| 353 |
// allocEntry() overwrites the load instruction! Restore the original. |
| 354 |
poolLoad[0] = 0xc0cc0000; |
| 355 |
|
| 356 |
// Now try with load and pool data on separate slices. |
| 357 |
load = ab.allocEntry(1, 1, (uint8_t*)poolLoad, (uint8_t*)poolData, &pe); |
| 358 |
CHECK_EQUAL(pe.index(), 1u); // Global pool entry index. |
| 359 |
CHECK_EQUAL(load.getOffset(), 24); |
| 360 |
CHECK_EQUAL(*ab.getInst(load), 0xc1cc0000); // Index into current pool. |
| 361 |
ab.putInt(0x22220001); |
| 362 |
ab.putInt(0x22220002); |
| 363 |
CHECK_EQUAL(*ab.getInst(BufferOffset(24)), 0xc2cc0010u); |
| 364 |
CHECK_EQUAL(*ab.getInst(BufferOffset(28)), 0x22220001u); |
| 365 |
CHECK_EQUAL(*ab.getInst(BufferOffset(32)), 0xb0bb000cu); |
| 366 |
CHECK_EQUAL(*ab.getInst(BufferOffset(36)), 0xffff0004u); |
| 367 |
CHECK_EQUAL(*ab.getInst(BufferOffset(40)), 0xdddd0000u); |
| 368 |
CHECK_EQUAL(*ab.getInst(BufferOffset(44)), 0x22220002u); |
| 369 |
|
| 370 |
// Two adjacent loads to the same pool. |
| 371 |
poolLoad[0] = 0xc0cc0000; |
| 372 |
load = ab.allocEntry(1, 1, (uint8_t*)poolLoad, (uint8_t*)poolData, &pe); |
| 373 |
CHECK_EQUAL(pe.index(), 2u); // Global pool entry index. |
| 374 |
CHECK_EQUAL(load.getOffset(), 48); |
| 375 |
CHECK_EQUAL(*ab.getInst(load), 0xc1cc0000); // Index into current pool. |
| 376 |
|
| 377 |
poolLoad[0] = 0xc0cc0000; |
| 378 |
load = ab.allocEntry(1, 1, (uint8_t*)poolLoad, (uint8_t*)(poolData + 1), &pe); |
| 379 |
CHECK_EQUAL(pe.index(), 3u); // Global pool entry index. |
| 380 |
CHECK_EQUAL(load.getOffset(), 52); |
| 381 |
CHECK_EQUAL(*ab.getInst(load), 0xc1cc0001); // Index into current pool. |
| 382 |
|
| 383 |
ab.putInt(0x22220005); |
| 384 |
|
| 385 |
CHECK_EQUAL(*ab.getInst(BufferOffset(48)), 0xc2cc0010u); // load pc+16. |
| 386 |
CHECK_EQUAL(*ab.getInst(BufferOffset(52)), 0xc2cc0010u); // load pc+16. |
| 387 |
CHECK_EQUAL(*ab.getInst(BufferOffset(56)), |
| 388 |
0xb0bb0010u); // guard branch pc+16. |
| 389 |
CHECK_EQUAL(*ab.getInst(BufferOffset(60)), 0xffff0008u); // header 8 bytes. |
| 390 |
CHECK_EQUAL(*ab.getInst(BufferOffset(64)), 0xdddd0000u); // datum 1. |
| 391 |
CHECK_EQUAL(*ab.getInst(BufferOffset(68)), 0xdddd0001u); // datum 2. |
| 392 |
CHECK_EQUAL(*ab.getInst(BufferOffset(72)), |
| 393 |
0x22220005u); // putInt(0x22220005) |
| 394 |
|
| 395 |
// Two loads as above, but the first load has an 8-byte pool entry, and the |
| 396 |
// second load wouldn't be able to reach its data. This must produce two |
| 397 |
// pools. |
| 398 |
poolLoad[0] = 0xc0cc0000; |
| 399 |
load = ab.allocEntry(1, 2, (uint8_t*)poolLoad, (uint8_t*)(poolData + 2), &pe); |
| 400 |
CHECK_EQUAL(pe.index(), 4u); // Global pool entry index. |
| 401 |
CHECK_EQUAL(load.getOffset(), 76); |
| 402 |
CHECK_EQUAL(*ab.getInst(load), 0xc1cc0000); // Index into current pool. |
| 403 |
|
| 404 |
poolLoad[0] = 0xc0cc0000; |
| 405 |
load = ab.allocEntry(1, 1, (uint8_t*)poolLoad, (uint8_t*)poolData, &pe); |
| 406 |
CHECK_EQUAL(pe.index(), |
| 407 |
6u); // Global pool entry index. (Prev one is two indexes). |
| 408 |
CHECK_EQUAL(load.getOffset(), 96); |
| 409 |
CHECK_EQUAL(*ab.getInst(load), 0xc1cc0000); // Index into current pool. |
| 410 |
|
| 411 |
CHECK_EQUAL(*ab.getInst(BufferOffset(76)), 0xc2cc000cu); // load pc+12. |
| 412 |
CHECK_EQUAL(*ab.getInst(BufferOffset(80)), |
| 413 |
0xb0bb0010u); // guard branch pc+16. |
| 414 |
CHECK_EQUAL(*ab.getInst(BufferOffset(84)), 0xffff0008u); // header 8 bytes. |
| 415 |
CHECK_EQUAL(*ab.getInst(BufferOffset(88)), 0xdddd0002u); // datum 1. |
| 416 |
CHECK_EQUAL(*ab.getInst(BufferOffset(92)), 0xdddd0003u); // datum 2. |
| 417 |
|
| 418 |
// Second pool is not flushed yet, and there is room for one instruction |
| 419 |
// after the load. Test the keep-together feature. |
| 420 |
ab.enterNoPool(2); |
| 421 |
ab.putInt(0x22220006); |
| 422 |
ab.putInt(0x22220007); |
| 423 |
ab.leaveNoPool(); |
| 424 |
|
| 425 |
CHECK_EQUAL(*ab.getInst(BufferOffset(96)), 0xc2cc000cu); // load pc+16. |
| 426 |
CHECK_EQUAL(*ab.getInst(BufferOffset(100)), |
| 427 |
0xb0bb000cu); // guard branch pc+12. |
| 428 |
CHECK_EQUAL(*ab.getInst(BufferOffset(104)), 0xffff0004u); // header 4 bytes. |
| 429 |
CHECK_EQUAL(*ab.getInst(BufferOffset(108)), 0xdddd0000u); // datum 1. |
| 430 |
CHECK_EQUAL(*ab.getInst(BufferOffset(112)), 0x22220006u); |
| 431 |
CHECK_EQUAL(*ab.getInst(BufferOffset(116)), 0x22220007u); |
| 432 |
|
| 433 |
return true; |
| 434 |
} |
| 435 |
END_TEST(testAssemblerBuffer_AssemblerBufferWithConstantPools) |
| 436 |
|
| 437 |
BEGIN_TEST(testAssemblerBuffer_AssemblerBufferWithConstantPools_ShortBranch) { |
| 438 |
using js::jit::BufferOffset; |
| 439 |
|
| 440 |
AsmBufWithPool ab(/* guardSize= */ 1, |
| 441 |
/* headerSize= */ 1, |
| 442 |
/* instBufferAlign(unused)= */ 0, |
| 443 |
/* poolMaxOffset= */ 17, |
| 444 |
/* pcBias= */ 0, |
| 445 |
/* alignFillInst= */ 0x11110000, |
| 446 |
/* nopFillInst= */ 0xaaaa0000, |
| 447 |
/* nopFill= */ 0); |
| 448 |
|
| 449 |
// Insert short-range branch. |
| 450 |
BufferOffset br1 = ab.putInt(0xb1bb00cc); |
| 451 |
ab.registerBranchDeadline( |
| 452 |
1, BufferOffset(br1.getOffset() + TestAssembler::BranchRange)); |
| 453 |
ab.putInt(0x22220001); |
| 454 |
BufferOffset off = ab.putInt(0x22220002); |
| 455 |
ab.registerBranchDeadline( |
| 456 |
1, BufferOffset(off.getOffset() + TestAssembler::BranchRange)); |
| 457 |
ab.putInt(0x22220003); |
| 458 |
ab.putInt(0x22220004); |
| 459 |
|
| 460 |
// Second short-range branch that will be swiped up by hysteresis. |
| 461 |
BufferOffset br2 = ab.putInt(0xb1bb0d2d); |
| 462 |
ab.registerBranchDeadline( |
| 463 |
1, BufferOffset(br2.getOffset() + TestAssembler::BranchRange)); |
| 464 |
|
| 465 |
// Branch should not have been patched yet here. |
| 466 |
CHECK_EQUAL(*ab.getInst(br1), 0xb1bb00cc); |
| 467 |
CHECK_EQUAL(*ab.getInst(br2), 0xb1bb0d2d); |
| 468 |
|
| 469 |
// Cancel one of the pending branches. |
| 470 |
// This is what will happen to most branches as they are bound before |
| 471 |
// expiring by Assembler::bind(). |
| 472 |
ab.unregisterBranchDeadline( |
| 473 |
1, BufferOffset(off.getOffset() + TestAssembler::BranchRange)); |
| 474 |
|
| 475 |
off = ab.putInt(0x22220006); |
| 476 |
// Here we may or may not have patched the branch yet, but it is inevitable |
| 477 |
// now: |
| 478 |
// |
| 479 |
// 0: br1 pc+36 |
| 480 |
// 4: 0x22220001 |
| 481 |
// 8: 0x22220002 (unpatched) |
| 482 |
// 12: 0x22220003 |
| 483 |
// 16: 0x22220004 |
| 484 |
// 20: br2 pc+20 |
| 485 |
// 24: 0x22220006 |
| 486 |
CHECK_EQUAL(off.getOffset(), 24); |
| 487 |
// 28: guard branch pc+16 |
| 488 |
// 32: pool header |
| 489 |
// 36: veneer1 |
| 490 |
// 40: veneer2 |
| 491 |
// 44: 0x22220007 |
| 492 |
|
| 493 |
off = ab.putInt(0x22220007); |
| 494 |
CHECK_EQUAL(off.getOffset(), 44); |
| 495 |
|
| 496 |
// Now the branch must have been patched. |
| 497 |
CHECK_EQUAL(*ab.getInst(br1), 0xb3bb0000 + 36); // br1 pc+36 (patched) |
| 498 |
CHECK_EQUAL(*ab.getInst(BufferOffset(8)), |
| 499 |
0x22220002u); // 0x22220002 (unpatched) |
| 500 |
CHECK_EQUAL(*ab.getInst(br2), 0xb3bb0000 + 20); // br2 pc+20 (patched) |
| 501 |
CHECK_EQUAL(*ab.getInst(BufferOffset(28)), 0xb0bb0010u); // br pc+16 (guard) |
| 502 |
CHECK_EQUAL(*ab.getInst(BufferOffset(32)), |
| 503 |
0xffff0000u); // pool header 0 bytes. |
| 504 |
CHECK_EQUAL(*ab.getInst(BufferOffset(36)), |
| 505 |
0xb2bb00ccu); // veneer1 w/ original 'cc' offset. |
| 506 |
CHECK_EQUAL(*ab.getInst(BufferOffset(40)), |
| 507 |
0xb2bb0d2du); // veneer2 w/ original 'd2d' offset. |
| 508 |
CHECK_EQUAL(*ab.getInst(BufferOffset(44)), 0x22220007u); |
| 509 |
|
| 510 |
return true; |
| 511 |
} |
| 512 |
END_TEST(testAssemblerBuffer_AssemblerBufferWithConstantPools_ShortBranch) |
| 513 |
|
| 514 |
// Test that everything is put together correctly in the ARM64 assembler. |
| 515 |
#if defined(JS_CODEGEN_ARM64) |
| 516 |
|
| 517 |
# include "jit/MacroAssembler-inl.h" |
| 518 |
|
| 519 |
BEGIN_TEST(testAssemblerBuffer_ARM64) { |
| 520 |
using namespace js::jit; |
| 521 |
|
| 522 |
js::LifoAlloc lifo(4096, js::MallocArena); |
| 523 |
TempAllocator alloc(&lifo); |
| 524 |
JitContext jc(cx); |
| 525 |
StackMacroAssembler masm(cx, alloc); |
| 526 |
AutoCreatedBy acb(masm, __func__); |
| 527 |
|
| 528 |
// Branches to an unbound label. |
| 529 |
Label lab1; |
| 530 |
masm.branch(Assembler::Equal, &lab1); |
| 531 |
masm.branch(Assembler::LessThan, &lab1); |
| 532 |
masm.bind(&lab1); |
| 533 |
masm.branch(Assembler::Equal, &lab1); |
| 534 |
|
| 535 |
CHECK_EQUAL(masm.getInstructionAt(BufferOffset(0))->InstructionBits(), |
| 536 |
vixl::B_cond | vixl::Assembler::ImmCondBranch(2) | vixl::eq); |
| 537 |
CHECK_EQUAL(masm.getInstructionAt(BufferOffset(4))->InstructionBits(), |
| 538 |
vixl::B_cond | vixl::Assembler::ImmCondBranch(1) | vixl::lt); |
| 539 |
CHECK_EQUAL(masm.getInstructionAt(BufferOffset(8))->InstructionBits(), |
| 540 |
vixl::B_cond | vixl::Assembler::ImmCondBranch(0) | vixl::eq); |
| 541 |
|
| 542 |
// Branches can reach the label, but the linked list of uses needs to be |
| 543 |
// rearranged. The final conditional branch cannot reach the first branch. |
| 544 |
Label lab2a; |
| 545 |
Label lab2b; |
| 546 |
masm.bind(&lab2a); |
| 547 |
masm.B(&lab2b); |
| 548 |
// Generate 1,100,000 bytes of NOPs. |
| 549 |
for (unsigned n = 0; n < 1100000; n += 4) { |
| 550 |
masm.Nop(); |
| 551 |
} |
| 552 |
masm.branch(Assembler::LessThan, &lab2b); |
| 553 |
masm.bind(&lab2b); |
| 554 |
CHECK_EQUAL( |
| 555 |
masm.getInstructionAt(BufferOffset(lab2a.offset()))->InstructionBits(), |
| 556 |
vixl::B | vixl::Assembler::ImmUncondBranch(1100000 / 4 + 2)); |
| 557 |
CHECK_EQUAL(masm.getInstructionAt(BufferOffset(lab2b.offset() - 4)) |
| 558 |
->InstructionBits(), |
| 559 |
vixl::B_cond | vixl::Assembler::ImmCondBranch(1) | vixl::lt); |
| 560 |
|
| 561 |
// Generate a conditional branch that can't reach its label. |
| 562 |
Label lab3a; |
| 563 |
Label lab3b; |
| 564 |
masm.bind(&lab3a); |
| 565 |
masm.branch(Assembler::LessThan, &lab3b); |
| 566 |
for (unsigned n = 0; n < 1100000; n += 4) { |
| 567 |
masm.Nop(); |
| 568 |
} |
| 569 |
masm.bind(&lab3b); |
| 570 |
masm.B(&lab3a); |
| 571 |
Instruction* bcond3 = masm.getInstructionAt(BufferOffset(lab3a.offset())); |
| 572 |
CHECK_EQUAL(bcond3->BranchType(), vixl::CondBranchType); |
| 573 |
ptrdiff_t delta = bcond3->ImmPCRawOffset() * 4; |
| 574 |
Instruction* veneer = |
| 575 |
masm.getInstructionAt(BufferOffset(lab3a.offset() + delta)); |
| 576 |
CHECK_EQUAL(veneer->BranchType(), vixl::UncondBranchType); |
| 577 |
delta += veneer->ImmPCRawOffset() * 4; |
| 578 |
CHECK_EQUAL(delta, lab3b.offset() - lab3a.offset()); |
| 579 |
Instruction* b3 = masm.getInstructionAt(BufferOffset(lab3b.offset())); |
| 580 |
CHECK_EQUAL(b3->BranchType(), vixl::UncondBranchType); |
| 581 |
CHECK_EQUAL(4 * b3->ImmPCRawOffset(), -delta); |
| 582 |
|
| 583 |
return true; |
| 584 |
} |
| 585 |
END_TEST(testAssemblerBuffer_ARM64) |
| 586 |
#endif /* JS_CODEGEN_ARM64 */ |