test-unboxed-doubles.cc 59.1 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdlib.h>
#include <utility>

8
#include "src/init/v8.h"
9

10
#include "src/api/api-inl.h"
11
#include "src/base/overflowing-math.h"
12
#include "src/builtins/accessors.h"
13
#include "src/codegen/compilation-cache.h"
14
#include "src/execution/execution.h"
15
#include "src/handles/global-handles.h"
16
#include "src/heap/factory.h"
17
#include "src/heap/heap-inl.h"
18 19
#include "src/heap/incremental-marking.h"
#include "src/heap/spaces.h"
20
#include "src/ic/ic.h"
21
#include "src/objects/api-callbacks.h"
22
#include "src/objects/field-type.h"
23
#include "src/objects/heap-number-inl.h"
24
#include "src/objects/layout-descriptor.h"
25
#include "src/objects/objects-inl.h"
26
#include "src/objects/property.h"
27
#include "test/cctest/cctest.h"
28
#include "test/cctest/heap/heap-utils.h"
29

30 31
namespace v8 {
namespace internal {
32
namespace test_unboxed_doubles {
33

34
#if V8_DOUBLE_FIELDS_UNBOXING
35 36


37 38 39 40
//
// Helper functions.
//

41
static void InitializeVerifiedMapDescriptors(
42 43
    Isolate* isolate, Map map, DescriptorArray descriptors,
    LayoutDescriptor layout_descriptor) {
44 45
  map.InitializeDescriptors(isolate, descriptors, layout_descriptor);
  CHECK(layout_descriptor.IsConsistentWithMap(map, true));
46 47
}

48
Handle<JSObject> GetObject(const char* name) {
49 50 51 52 53 54
  return Handle<JSObject>::cast(
      v8::Utils::OpenHandle(*v8::Local<v8::Object>::Cast(
          CcTest::global()
              ->Get(v8::Isolate::GetCurrent()->GetCurrentContext(),
                    v8_str(name))
              .ToLocalChecked())));
55 56
}

57
static double GetDoubleFieldValue(JSObject obj, FieldIndex field_index) {
58 59
  if (obj.IsUnboxedDoubleField(field_index)) {
    return obj.RawFastDoublePropertyAt(field_index);
60
  } else {
61
    Object value = obj.RawFastPropertyAt(field_index);
62 63
    CHECK(value.IsHeapNumber());
    return HeapNumber::cast(value).value();
64 65 66
  }
}

67
void WriteToField(JSObject object, int index, Object value) {
68
  DescriptorArray descriptors = object.map().instance_descriptors(kRelaxedLoad);
69
  InternalIndex descriptor(index);
70 71
  PropertyDetails details = descriptors.GetDetails(descriptor);
  object.WriteToField(descriptor, details, value);
72 73
}

74
const int kNumberOfBits = 32;
75
const int kBitsInSmiLayout = SmiValuesAre32Bits() ? 32 : kSmiValueSize - 1;
76

77
enum TestPropertyKind {
78
  PROP_ACCESSOR_INFO,
79 80 81
  PROP_SMI,
  PROP_DOUBLE,
  PROP_TAGGED,
82
  PROP_KIND_NUMBER
83 84 85 86 87 88 89 90
};

static Representation representations[PROP_KIND_NUMBER] = {
    Representation::None(), Representation::Smi(), Representation::Double(),
    Representation::Tagged()};


static Handle<DescriptorArray> CreateDescriptorArray(Isolate* isolate,
91
                                                     TestPropertyKind* props,
92 93 94 95 96 97 98 99 100 101
                                                     int kPropsCount) {
  Factory* factory = isolate->factory();

  Handle<DescriptorArray> descriptors =
      DescriptorArray::Allocate(isolate, 0, kPropsCount);

  int next_field_offset = 0;
  for (int i = 0; i < kPropsCount; i++) {
    EmbeddedVector<char, 64> buffer;
    SNPrintF(buffer, "prop%d", i);
102
    Handle<String> name = factory->InternalizeUtf8String(buffer.begin());
103

104
    TestPropertyKind kind = props[i];
105

106 107 108
    Descriptor d;
    if (kind == PROP_ACCESSOR_INFO) {
      Handle<AccessorInfo> info =
109
          Accessors::MakeAccessor(isolate, name, nullptr, nullptr);
110
      d = Descriptor::AccessorConstant(name, info, NONE);
111 112

    } else {
113
      d = Descriptor::DataField(isolate, name, next_field_offset, NONE,
114 115 116 117 118 119
                                representations[kind]);
    }
    descriptors->Append(&d);
    PropertyDetails details = d.GetDetails();
    if (details.location() == kField) {
      next_field_offset += details.field_width_in_words();
120 121 122 123 124 125 126 127 128 129
    }
  }
  return descriptors;
}


TEST(LayoutDescriptorBasicFast) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

130
  LayoutDescriptor layout_desc = LayoutDescriptor::FastPointerLayout();
131

132 133 134
  CHECK(!layout_desc.IsSlowLayout());
  CHECK(layout_desc.IsFastPointerLayout());
  CHECK_EQ(kBitsInSmiLayout, layout_desc.capacity());
135

136
  for (int i = 0; i < kBitsInSmiLayout + 13; i++) {
137
    CHECK(layout_desc.IsTagged(i));
138
  }
139 140 141 142
  CHECK(layout_desc.IsTagged(-1));
  CHECK(layout_desc.IsTagged(-12347));
  CHECK(layout_desc.IsTagged(15635));
  CHECK(layout_desc.IsFastPointerLayout());
143

144
  for (int i = 0; i < kBitsInSmiLayout; i++) {
145 146 147 148
    layout_desc = layout_desc.SetTaggedForTesting(i, false);
    CHECK(!layout_desc.IsTagged(i));
    layout_desc = layout_desc.SetTaggedForTesting(i, true);
    CHECK(layout_desc.IsTagged(i));
149
  }
150
  CHECK(layout_desc.IsFastPointerLayout());
151 152

  int sequence_length;
153 154
  CHECK_EQ(true, layout_desc.IsTagged(0, std::numeric_limits<int>::max(),
                                      &sequence_length));
155 156
  CHECK_EQ(std::numeric_limits<int>::max(), sequence_length);

157
  CHECK(layout_desc.IsTagged(0, 7, &sequence_length));
158
  CHECK_EQ(7, sequence_length);
159 160 161 162 163 164 165 166 167
}


TEST(LayoutDescriptorBasicSlow) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  v8::HandleScope scope(CcTest::isolate());

  Handle<LayoutDescriptor> layout_descriptor;
168
  const int kPropsCount = kBitsInSmiLayout * 3;
169
  TestPropertyKind props[kPropsCount];
170 171 172 173 174 175 176 177 178 179 180
  for (int i = 0; i < kPropsCount; i++) {
    // All properties tagged.
    props[i] = PROP_TAGGED;
  }

  {
    Handle<DescriptorArray> descriptors =
        CreateDescriptorArray(isolate, props, kPropsCount);

    Handle<Map> map = Map::Create(isolate, kPropsCount);

181 182
    layout_descriptor =
        LayoutDescriptor::New(isolate, map, descriptors, kPropsCount);
183
    CHECK_EQ(LayoutDescriptor::FastPointerLayout(), *layout_descriptor);
184
    CHECK_EQ(kBitsInSmiLayout, layout_descriptor->capacity());
185 186
    InitializeVerifiedMapDescriptors(isolate, *map, *descriptors,
                                     *layout_descriptor);
187 188 189 190 191 192 193 194 195 196 197 198 199
  }

  props[0] = PROP_DOUBLE;
  props[kPropsCount - 1] = PROP_DOUBLE;

  Handle<DescriptorArray> descriptors =
      CreateDescriptorArray(isolate, props, kPropsCount);

  {
    int inobject_properties = kPropsCount - 1;
    Handle<Map> map = Map::Create(isolate, inobject_properties);

    // Should be fast as the only double property is the first one.
200 201
    layout_descriptor =
        LayoutDescriptor::New(isolate, map, descriptors, kPropsCount);
202 203 204 205
    CHECK_NE(LayoutDescriptor::FastPointerLayout(), *layout_descriptor);
    CHECK(!layout_descriptor->IsSlowLayout());
    CHECK(!layout_descriptor->IsFastPointerLayout());

206
    CHECK(!layout_descriptor->IsTagged(0));
207
    for (int i = 1; i < kPropsCount; i++) {
208
      CHECK(layout_descriptor->IsTagged(i));
209
    }
210 211
    InitializeVerifiedMapDescriptors(isolate, *map, *descriptors,
                                     *layout_descriptor);
212 213 214 215 216 217
  }

  {
    int inobject_properties = kPropsCount;
    Handle<Map> map = Map::Create(isolate, inobject_properties);

218 219
    layout_descriptor =
        LayoutDescriptor::New(isolate, map, descriptors, kPropsCount);
220 221 222
    CHECK_NE(LayoutDescriptor::FastPointerLayout(), *layout_descriptor);
    CHECK(layout_descriptor->IsSlowLayout());
    CHECK(!layout_descriptor->IsFastPointerLayout());
223
    CHECK_GT(layout_descriptor->capacity(), kBitsInSmiLayout);
224

225 226
    CHECK(!layout_descriptor->IsTagged(0));
    CHECK(!layout_descriptor->IsTagged(kPropsCount - 1));
227
    for (int i = 1; i < kPropsCount - 1; i++) {
228
      CHECK(layout_descriptor->IsTagged(i));
229 230
    }

231 232
    InitializeVerifiedMapDescriptors(isolate, *map, *descriptors,
                                     *layout_descriptor);
233 234

    // Here we have truly slow layout descriptor, so play with the bits.
235 236 237
    CHECK(layout_descriptor->IsTagged(-1));
    CHECK(layout_descriptor->IsTagged(-12347));
    CHECK(layout_descriptor->IsTagged(15635));
238

239
    LayoutDescriptor layout_desc = *layout_descriptor;
240 241
    // Play with the bits but leave it in consistent state with map at the end.
    for (int i = 1; i < kPropsCount - 1; i++) {
242 243 244 245
      layout_desc = layout_desc.SetTaggedForTesting(i, false);
      CHECK(!layout_desc.IsTagged(i));
      layout_desc = layout_desc.SetTaggedForTesting(i, true);
      CHECK(layout_desc.IsTagged(i));
246
    }
247 248
    CHECK(layout_desc.IsSlowLayout());
    CHECK(!layout_desc.IsFastPointerLayout());
249
    CHECK(layout_descriptor->IsConsistentWithMap(*map, true));
250 251 252 253
  }
}


254 255 256 257 258 259
static void TestLayoutDescriptorQueries(int layout_descriptor_length,
                                        int* bit_flip_positions,
                                        int max_sequence_length) {
  Handle<LayoutDescriptor> layout_descriptor = LayoutDescriptor::NewForTesting(
      CcTest::i_isolate(), layout_descriptor_length);
  layout_descriptor_length = layout_descriptor->capacity();
260
  LayoutDescriptor layout_desc = *layout_descriptor;
261 262 263 264 265 266 267 268 269 270 271

  {
    // Fill in the layout descriptor.
    int cur_bit_flip_index = 0;
    bool tagged = true;
    for (int i = 0; i < layout_descriptor_length; i++) {
      if (i == bit_flip_positions[cur_bit_flip_index]) {
        tagged = !tagged;
        ++cur_bit_flip_index;
        CHECK(i < bit_flip_positions[cur_bit_flip_index]);  // check test data
      }
272
      layout_desc = layout_desc.SetTaggedForTesting(i, tagged);
273 274 275
    }
  }

276
  if (layout_desc.IsFastPointerLayout()) {
277 278 279 280 281 282 283 284 285 286 287 288
    return;
  }

  {
    // Check queries.
    int cur_bit_flip_index = 0;
    bool tagged = true;
    for (int i = 0; i < layout_descriptor_length; i++) {
      if (i == bit_flip_positions[cur_bit_flip_index]) {
        tagged = !tagged;
        ++cur_bit_flip_index;
      }
289
      CHECK_EQ(tagged, layout_desc.IsTagged(i));
290 291 292

      int next_bit_flip_position = bit_flip_positions[cur_bit_flip_index];
      int expected_sequence_length;
293
      if (next_bit_flip_position < layout_desc.capacity()) {
294 295 296
        expected_sequence_length = next_bit_flip_position - i;
      } else {
        expected_sequence_length = tagged ? std::numeric_limits<int>::max()
297
                                          : (layout_desc.capacity() - i);
298 299
      }
      expected_sequence_length =
300
          std::min(expected_sequence_length, max_sequence_length);
301 302
      int sequence_length;
      CHECK_EQ(tagged,
303
               layout_desc.IsTagged(i, max_sequence_length, &sequence_length));
304
      CHECK_GT(sequence_length, 0);
305 306 307 308 309

      CHECK_EQ(expected_sequence_length, sequence_length);
    }

    int sequence_length;
310 311
    CHECK_EQ(true, layout_desc.IsTagged(layout_descriptor_length,
                                        max_sequence_length, &sequence_length));
312 313 314 315 316 317 318
    CHECK_EQ(max_sequence_length, sequence_length);
  }
}


static void TestLayoutDescriptorQueriesFast(int max_sequence_length) {
  {
319
    LayoutDescriptor layout_desc = LayoutDescriptor::FastPointerLayout();
320 321 322
    int sequence_length;
    for (int i = 0; i < kNumberOfBits; i++) {
      CHECK_EQ(true,
323
               layout_desc.IsTagged(i, max_sequence_length, &sequence_length));
324
      CHECK_GT(sequence_length, 0);
325 326 327 328 329 330
      CHECK_EQ(max_sequence_length, sequence_length);
    }
  }

  {
    int bit_flip_positions[] = {1000};
331
    TestLayoutDescriptorQueries(kBitsInSmiLayout, bit_flip_positions,
332 333 334 335 336
                                max_sequence_length);
  }

  {
    int bit_flip_positions[] = {0, 1000};
337
    TestLayoutDescriptorQueries(kBitsInSmiLayout, bit_flip_positions,
338 339 340 341 342 343 344 345
                                max_sequence_length);
  }

  {
    int bit_flip_positions[kNumberOfBits + 1];
    for (int i = 0; i <= kNumberOfBits; i++) {
      bit_flip_positions[i] = i;
    }
346
    TestLayoutDescriptorQueries(kBitsInSmiLayout, bit_flip_positions,
347 348 349 350 351
                                max_sequence_length);
  }

  {
    int bit_flip_positions[] = {3, 7, 8, 10, 15, 21, 30, 1000};
352
    TestLayoutDescriptorQueries(kBitsInSmiLayout, bit_flip_positions,
353 354 355 356 357 358
                                max_sequence_length);
  }

  {
    int bit_flip_positions[] = {0,  1,  2,  3,  5,  7,  9,
                                12, 15, 18, 22, 26, 29, 1000};
359
    TestLayoutDescriptorQueries(kBitsInSmiLayout, bit_flip_positions,
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
                                max_sequence_length);
  }
}


TEST(LayoutDescriptorQueriesFastLimited7) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

  TestLayoutDescriptorQueriesFast(7);
}


TEST(LayoutDescriptorQueriesFastLimited13) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

  TestLayoutDescriptorQueriesFast(13);
}


TEST(LayoutDescriptorQueriesFastUnlimited) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

  TestLayoutDescriptorQueriesFast(std::numeric_limits<int>::max());
}


static void TestLayoutDescriptorQueriesSlow(int max_sequence_length) {
  {
    int bit_flip_positions[] = {10000};
    TestLayoutDescriptorQueries(kMaxNumberOfDescriptors, bit_flip_positions,
                                max_sequence_length);
  }

  {
    int bit_flip_positions[] = {0, 10000};
    TestLayoutDescriptorQueries(kMaxNumberOfDescriptors, bit_flip_positions,
                                max_sequence_length);
  }

  {
    int bit_flip_positions[kMaxNumberOfDescriptors + 1];
    for (int i = 0; i < kMaxNumberOfDescriptors; i++) {
      bit_flip_positions[i] = i;
    }
    bit_flip_positions[kMaxNumberOfDescriptors] = 10000;
    TestLayoutDescriptorQueries(kMaxNumberOfDescriptors, bit_flip_positions,
                                max_sequence_length);
  }

  {
    int bit_flip_positions[] = {3,  7,  8,  10, 15,  21,   30,
                                37, 54, 80, 99, 383, 10000};
    TestLayoutDescriptorQueries(kMaxNumberOfDescriptors, bit_flip_positions,
                                max_sequence_length);
  }

  {
    int bit_flip_positions[] = {0,   10,  20,  30,  50,  70,  90,
                                120, 150, 180, 220, 260, 290, 10000};
    TestLayoutDescriptorQueries(kMaxNumberOfDescriptors, bit_flip_positions,
                                max_sequence_length);
  }

  {
    int bit_flip_positions[kMaxNumberOfDescriptors + 1];
    int cur = 0;
    for (int i = 0; i < kMaxNumberOfDescriptors; i++) {
      bit_flip_positions[i] = cur;
431
      cur = base::MulWithWraparound((cur + 1), 2);
432
    }
433
    CHECK_LT(cur, 10000);
434 435 436 437 438 439 440 441 442 443
    bit_flip_positions[kMaxNumberOfDescriptors] = 10000;
    TestLayoutDescriptorQueries(kMaxNumberOfDescriptors, bit_flip_positions,
                                max_sequence_length);
  }

  {
    int bit_flip_positions[kMaxNumberOfDescriptors + 1];
    int cur = 3;
    for (int i = 0; i < kMaxNumberOfDescriptors; i++) {
      bit_flip_positions[i] = cur;
444
      cur = base::MulWithWraparound((cur + 1), 2);
445
    }
446
    CHECK_LT(cur, 10000);
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
    bit_flip_positions[kMaxNumberOfDescriptors] = 10000;
    TestLayoutDescriptorQueries(kMaxNumberOfDescriptors, bit_flip_positions,
                                max_sequence_length);
  }
}


TEST(LayoutDescriptorQueriesSlowLimited7) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

  TestLayoutDescriptorQueriesSlow(7);
}


TEST(LayoutDescriptorQueriesSlowLimited13) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

  TestLayoutDescriptorQueriesSlow(13);
}


TEST(LayoutDescriptorQueriesSlowLimited42) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

  TestLayoutDescriptorQueriesSlow(42);
}


TEST(LayoutDescriptorQueriesSlowUnlimited) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());

  TestLayoutDescriptorQueriesSlow(std::numeric_limits<int>::max());
}


486 487 488 489 490 491
TEST(LayoutDescriptorCreateNewFast) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  v8::HandleScope scope(CcTest::isolate());

  Handle<LayoutDescriptor> layout_descriptor;
492
  TestPropertyKind props[] = {
493
      PROP_ACCESSOR_INFO,
494
      PROP_TAGGED,  // field #0
495
      PROP_ACCESSOR_INFO,
496
      PROP_DOUBLE,  // field #1
497
      PROP_ACCESSOR_INFO,
498
      PROP_TAGGED,  // field #2
499
      PROP_ACCESSOR_INFO,
500 501 502 503 504 505 506 507
  };
  const int kPropsCount = arraysize(props);

  Handle<DescriptorArray> descriptors =
      CreateDescriptorArray(isolate, props, kPropsCount);

  {
    Handle<Map> map = Map::Create(isolate, 0);
508 509
    layout_descriptor =
        LayoutDescriptor::New(isolate, map, descriptors, kPropsCount);
510
    CHECK_EQ(LayoutDescriptor::FastPointerLayout(), *layout_descriptor);
511 512
    InitializeVerifiedMapDescriptors(isolate, *map, *descriptors,
                                     *layout_descriptor);
513 514 515 516
  }

  {
    Handle<Map> map = Map::Create(isolate, 1);
517 518
    layout_descriptor =
        LayoutDescriptor::New(isolate, map, descriptors, kPropsCount);
519
    CHECK_EQ(LayoutDescriptor::FastPointerLayout(), *layout_descriptor);
520 521
    InitializeVerifiedMapDescriptors(isolate, *map, *descriptors,
                                     *layout_descriptor);
522 523 524 525
  }

  {
    Handle<Map> map = Map::Create(isolate, 2);
526 527
    layout_descriptor =
        LayoutDescriptor::New(isolate, map, descriptors, kPropsCount);
528 529
    CHECK_NE(LayoutDescriptor::FastPointerLayout(), *layout_descriptor);
    CHECK(!layout_descriptor->IsSlowLayout());
530 531 532 533
    CHECK(layout_descriptor->IsTagged(0));
    CHECK(!layout_descriptor->IsTagged(1));
    CHECK(layout_descriptor->IsTagged(2));
    CHECK(layout_descriptor->IsTagged(125));
534 535
    InitializeVerifiedMapDescriptors(isolate, *map, *descriptors,
                                     *layout_descriptor);
536 537 538 539 540 541 542 543 544 545
  }
}


TEST(LayoutDescriptorCreateNewSlow) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  v8::HandleScope scope(CcTest::isolate());

  Handle<LayoutDescriptor> layout_descriptor;
546
  const int kPropsCount = kBitsInSmiLayout * 3;
547
  TestPropertyKind props[kPropsCount];
548
  for (int i = 0; i < kPropsCount; i++) {
549
    props[i] = static_cast<TestPropertyKind>(i % PROP_KIND_NUMBER);
550 551 552 553 554 555 556
  }

  Handle<DescriptorArray> descriptors =
      CreateDescriptorArray(isolate, props, kPropsCount);

  {
    Handle<Map> map = Map::Create(isolate, 0);
557 558
    layout_descriptor =
        LayoutDescriptor::New(isolate, map, descriptors, kPropsCount);
559
    CHECK_EQ(LayoutDescriptor::FastPointerLayout(), *layout_descriptor);
560 561
    InitializeVerifiedMapDescriptors(isolate, *map, *descriptors,
                                     *layout_descriptor);
562 563 564 565
  }

  {
    Handle<Map> map = Map::Create(isolate, 1);
566 567
    layout_descriptor =
        LayoutDescriptor::New(isolate, map, descriptors, kPropsCount);
568
    CHECK_EQ(LayoutDescriptor::FastPointerLayout(), *layout_descriptor);
569 570
    InitializeVerifiedMapDescriptors(isolate, *map, *descriptors,
                                     *layout_descriptor);
571 572 573 574
  }

  {
    Handle<Map> map = Map::Create(isolate, 2);
575 576
    layout_descriptor =
        LayoutDescriptor::New(isolate, map, descriptors, kPropsCount);
577 578
    CHECK_NE(LayoutDescriptor::FastPointerLayout(), *layout_descriptor);
    CHECK(!layout_descriptor->IsSlowLayout());
579 580 581 582
    CHECK(layout_descriptor->IsTagged(0));
    CHECK(!layout_descriptor->IsTagged(1));
    CHECK(layout_descriptor->IsTagged(2));
    CHECK(layout_descriptor->IsTagged(125));
583 584
    InitializeVerifiedMapDescriptors(isolate, *map, *descriptors,
                                     *layout_descriptor);
585 586 587 588 589
  }

  {
    int inobject_properties = kPropsCount / 2;
    Handle<Map> map = Map::Create(isolate, inobject_properties);
590 591
    layout_descriptor =
        LayoutDescriptor::New(isolate, map, descriptors, kPropsCount);
592 593 594
    CHECK_NE(LayoutDescriptor::FastPointerLayout(), *layout_descriptor);
    CHECK(layout_descriptor->IsSlowLayout());
    for (int i = 0; i < inobject_properties; i++) {
595
      // PROP_DOUBLE has index 1 among DATA properties.
596 597 598 599 600
      const bool tagged = (i % (PROP_KIND_NUMBER - 1)) != 1;
      CHECK_EQ(tagged, layout_descriptor->IsTagged(i));
    }
    // Every property after inobject_properties must be tagged.
    for (int i = inobject_properties; i < kPropsCount; i++) {
601
      CHECK(layout_descriptor->IsTagged(i));
602
    }
603 604
    InitializeVerifiedMapDescriptors(isolate, *map, *descriptors,
                                     *layout_descriptor);
605 606 607

    // Now test LayoutDescriptor::cast_gc_safe().
    Handle<LayoutDescriptor> layout_descriptor_copy =
608
        LayoutDescriptor::New(isolate, map, descriptors, kPropsCount);
609

610
    LayoutDescriptor layout_desc = *layout_descriptor;
611 612
    CHECK_EQ(layout_desc, LayoutDescriptor::cast(layout_desc));
    CHECK_EQ(layout_desc, LayoutDescriptor::cast_gc_safe(layout_desc));
613
    CHECK(layout_desc.IsSlowLayout());
614
    // Now make it look like a forwarding pointer to layout_descriptor_copy.
615
    MapWord map_word = layout_desc.map_word();
616
    CHECK(!map_word.IsForwardingAddress());
617
    layout_desc.set_map_word(
618
        MapWord::FromForwardingAddress(*layout_descriptor_copy));
619
    CHECK(layout_desc.map_word().IsForwardingAddress());
620
    CHECK_EQ(layout_desc, LayoutDescriptor::cast_gc_safe(layout_desc));
621 622

    // Restore it back.
623
    layout_desc.set_map_word(map_word);
624 625 626 627 628 629
    CHECK_EQ(layout_desc, LayoutDescriptor::cast(layout_desc));
  }
}


static Handle<LayoutDescriptor> TestLayoutDescriptorAppend(
630
    Isolate* isolate, int inobject_properties, TestPropertyKind* props,
631 632 633 634 635 636 637
    int kPropsCount) {
  Factory* factory = isolate->factory();

  Handle<DescriptorArray> descriptors =
      DescriptorArray::Allocate(isolate, 0, kPropsCount);

  Handle<Map> map = Map::Create(isolate, inobject_properties);
638
  map->InitializeDescriptors(isolate, *descriptors,
639 640 641 642 643 644
                             LayoutDescriptor::FastPointerLayout());

  int next_field_offset = 0;
  for (int i = 0; i < kPropsCount; i++) {
    EmbeddedVector<char, 64> buffer;
    SNPrintF(buffer, "prop%d", i);
645
    Handle<String> name = factory->InternalizeUtf8String(buffer.begin());
646 647

    Handle<LayoutDescriptor> layout_descriptor;
648
    TestPropertyKind kind = props[i];
649 650 651
    Descriptor d;
    if (kind == PROP_ACCESSOR_INFO) {
      Handle<AccessorInfo> info =
652
          Accessors::MakeAccessor(isolate, name, nullptr, nullptr);
653
      d = Descriptor::AccessorConstant(name, info, NONE);
654 655

    } else {
656
      d = Descriptor::DataField(isolate, name, next_field_offset, NONE,
657 658 659
                                representations[kind]);
    }
    PropertyDetails details = d.GetDetails();
660
    layout_descriptor = LayoutDescriptor::ShareAppend(isolate, map, details);
661 662 663
    descriptors->Append(&d);
    if (details.location() == kField) {
      int field_width_in_words = details.field_width_in_words();
664 665
      next_field_offset += field_width_in_words;

666
      int field_index = details.field_index();
667
      bool is_inobject = field_index < map->GetInObjectProperties();
668 669 670 671 672 673
      for (int bit = 0; bit < field_width_in_words; bit++) {
        CHECK_EQ(is_inobject && (kind == PROP_DOUBLE),
                 !layout_descriptor->IsTagged(field_index + bit));
      }
      CHECK(layout_descriptor->IsTagged(next_field_offset));
    }
674
    map->InitializeDescriptors(isolate, *descriptors, *layout_descriptor);
675
  }
676 677
  Handle<LayoutDescriptor> layout_descriptor(
      map->layout_descriptor(kAcquireLoad), isolate);
678
  CHECK(layout_descriptor->IsConsistentWithMap(*map, true));
679 680 681 682 683 684 685 686 687 688
  return layout_descriptor;
}


TEST(LayoutDescriptorAppend) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  v8::HandleScope scope(CcTest::isolate());

  Handle<LayoutDescriptor> layout_descriptor;
689
  const int kPropsCount = kBitsInSmiLayout * 3;
690
  TestPropertyKind props[kPropsCount];
691
  for (int i = 0; i < kPropsCount; i++) {
692
    props[i] = static_cast<TestPropertyKind>(i % PROP_KIND_NUMBER);
693 694 695 696 697 698 699 700 701 702 703
  }

  layout_descriptor =
      TestLayoutDescriptorAppend(isolate, 0, props, kPropsCount);
  CHECK(!layout_descriptor->IsSlowLayout());

  layout_descriptor =
      TestLayoutDescriptorAppend(isolate, 13, props, kPropsCount);
  CHECK(!layout_descriptor->IsSlowLayout());

  layout_descriptor =
704
      TestLayoutDescriptorAppend(isolate, kBitsInSmiLayout, props, kPropsCount);
705 706
  CHECK(!layout_descriptor->IsSlowLayout());

707
  layout_descriptor = TestLayoutDescriptorAppend(isolate, kBitsInSmiLayout * 2,
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
                                                 props, kPropsCount);
  CHECK(layout_descriptor->IsSlowLayout());

  layout_descriptor =
      TestLayoutDescriptorAppend(isolate, kPropsCount, props, kPropsCount);
  CHECK(layout_descriptor->IsSlowLayout());
}


TEST(LayoutDescriptorAppendAllDoubles) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  v8::HandleScope scope(CcTest::isolate());

  Handle<LayoutDescriptor> layout_descriptor;
723
  const int kPropsCount = kBitsInSmiLayout * 3;
724
  TestPropertyKind props[kPropsCount];
725 726 727 728 729 730 731 732 733 734 735 736 737
  for (int i = 0; i < kPropsCount; i++) {
    props[i] = PROP_DOUBLE;
  }

  layout_descriptor =
      TestLayoutDescriptorAppend(isolate, 0, props, kPropsCount);
  CHECK(!layout_descriptor->IsSlowLayout());

  layout_descriptor =
      TestLayoutDescriptorAppend(isolate, 13, props, kPropsCount);
  CHECK(!layout_descriptor->IsSlowLayout());

  layout_descriptor =
738
      TestLayoutDescriptorAppend(isolate, kBitsInSmiLayout, props, kPropsCount);
739 740
  CHECK(!layout_descriptor->IsSlowLayout());

741
  layout_descriptor = TestLayoutDescriptorAppend(isolate, kBitsInSmiLayout + 1,
742 743 744
                                                 props, kPropsCount);
  CHECK(layout_descriptor->IsSlowLayout());

745
  layout_descriptor = TestLayoutDescriptorAppend(isolate, kBitsInSmiLayout * 2,
746 747 748 749 750 751 752 753 754
                                                 props, kPropsCount);
  CHECK(layout_descriptor->IsSlowLayout());

  layout_descriptor =
      TestLayoutDescriptorAppend(isolate, kPropsCount, props, kPropsCount);
  CHECK(layout_descriptor->IsSlowLayout());

  {
    // Ensure layout descriptor switches into slow mode at the right moment.
755 756
    layout_descriptor = TestLayoutDescriptorAppend(isolate, kPropsCount, props,
                                                   kBitsInSmiLayout);
757 758 759
    CHECK(!layout_descriptor->IsSlowLayout());

    layout_descriptor = TestLayoutDescriptorAppend(isolate, kPropsCount, props,
760
                                                   kBitsInSmiLayout + 1);
761 762 763 764 765 766 767 768
    CHECK(layout_descriptor->IsSlowLayout());
  }
}


static Handle<LayoutDescriptor> TestLayoutDescriptorAppendIfFastOrUseFull(
    Isolate* isolate, int inobject_properties,
    Handle<DescriptorArray> descriptors, int number_of_descriptors) {
769
  Handle<Map> initial_map = Map::Create(isolate, inobject_properties);
770 771

  Handle<LayoutDescriptor> full_layout_descriptor = LayoutDescriptor::New(
772
      isolate, initial_map, descriptors, descriptors->number_of_descriptors());
773 774 775 776

  int nof = 0;
  bool switched_to_slow_mode = false;

777 778 779
  // This method calls LayoutDescriptor::AppendIfFastOrUseFull() internally
  // and does all the required map-descriptors related book keeping.
  Handle<Map> last_map = Map::AddMissingTransitionsForTesting(
780
      isolate, initial_map, descriptors, full_layout_descriptor);
781

782 783 784 785 786
  // Follow back pointers to construct a sequence of maps from |map|
  // to |last_map|.
  int descriptors_length = descriptors->number_of_descriptors();
  std::vector<Handle<Map>> maps(descriptors_length);
  {
787
    CHECK(last_map->is_stable());
788
    Map map = *last_map;
789 790
    for (int i = 0; i < descriptors_length; i++) {
      maps[descriptors_length - 1 - i] = handle(map, isolate);
791 792
      Object maybe_map = map.GetBackPointer();
      CHECK(maybe_map.IsMap());
793
      map = Map::cast(maybe_map);
794
      CHECK(!map.is_stable());
795 796 797
    }
    CHECK_EQ(1, maps[0]->NumberOfOwnDescriptors());
  }
798

799 800 801
  Handle<Map> map;
  // Now check layout descriptors of all intermediate maps.
  for (int i = 0; i < number_of_descriptors; i++) {
802
    PropertyDetails details = descriptors->GetDetails(InternalIndex(i));
803
    map = maps[i];
804
    LayoutDescriptor layout_desc = map->layout_descriptor(kAcquireLoad);
805

806
    if (layout_desc.IsSlowLayout()) {
807 808 809 810
      switched_to_slow_mode = true;
      CHECK_EQ(*full_layout_descriptor, layout_desc);
    } else {
      CHECK(!switched_to_slow_mode);
811
      if (details.location() == kField) {
812 813 814 815
        nof++;
        int field_index = details.field_index();
        int field_width_in_words = details.field_width_in_words();

816
        bool is_inobject = field_index < map->GetInObjectProperties();
817 818
        for (int bit = 0; bit < field_width_in_words; bit++) {
          CHECK_EQ(is_inobject && details.representation().IsDouble(),
819
                   !layout_desc.IsTagged(field_index + bit));
820
        }
821
        CHECK(layout_desc.IsTagged(field_index + field_width_in_words));
822 823
      }
    }
824
    CHECK(map->layout_descriptor(kAcquireLoad).IsConsistentWithMap(*map));
825 826
  }

827 828
  Handle<LayoutDescriptor> layout_descriptor(map->GetLayoutDescriptor(),
                                             isolate);
829
  CHECK(layout_descriptor->IsConsistentWithMap(*map));
830 831 832 833 834 835 836 837 838 839
  return layout_descriptor;
}


TEST(LayoutDescriptorAppendIfFastOrUseFull) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  v8::HandleScope scope(CcTest::isolate());

  Handle<LayoutDescriptor> layout_descriptor;
840
  const int kPropsCount = kBitsInSmiLayout * 3;
841
  TestPropertyKind props[kPropsCount];
842
  for (int i = 0; i < kPropsCount; i++) {
843
    props[i] = static_cast<TestPropertyKind>(i % PROP_KIND_NUMBER);
844 845 846 847 848 849 850 851 852 853 854 855 856
  }
  Handle<DescriptorArray> descriptors =
      CreateDescriptorArray(isolate, props, kPropsCount);

  layout_descriptor = TestLayoutDescriptorAppendIfFastOrUseFull(
      isolate, 0, descriptors, kPropsCount);
  CHECK(!layout_descriptor->IsSlowLayout());

  layout_descriptor = TestLayoutDescriptorAppendIfFastOrUseFull(
      isolate, 13, descriptors, kPropsCount);
  CHECK(!layout_descriptor->IsSlowLayout());

  layout_descriptor = TestLayoutDescriptorAppendIfFastOrUseFull(
857
      isolate, kBitsInSmiLayout, descriptors, kPropsCount);
858 859 860
  CHECK(!layout_descriptor->IsSlowLayout());

  layout_descriptor = TestLayoutDescriptorAppendIfFastOrUseFull(
861
      isolate, kBitsInSmiLayout * 2, descriptors, kPropsCount);
862 863 864 865 866 867 868 869 870 871 872 873 874 875
  CHECK(layout_descriptor->IsSlowLayout());

  layout_descriptor = TestLayoutDescriptorAppendIfFastOrUseFull(
      isolate, kPropsCount, descriptors, kPropsCount);
  CHECK(layout_descriptor->IsSlowLayout());
}


TEST(LayoutDescriptorAppendIfFastOrUseFullAllDoubles) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  v8::HandleScope scope(CcTest::isolate());

  Handle<LayoutDescriptor> layout_descriptor;
876
  const int kPropsCount = kBitsInSmiLayout * 3;
877
  TestPropertyKind props[kPropsCount];
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
  for (int i = 0; i < kPropsCount; i++) {
    props[i] = PROP_DOUBLE;
  }
  Handle<DescriptorArray> descriptors =
      CreateDescriptorArray(isolate, props, kPropsCount);

  layout_descriptor = TestLayoutDescriptorAppendIfFastOrUseFull(
      isolate, 0, descriptors, kPropsCount);
  CHECK(!layout_descriptor->IsSlowLayout());

  layout_descriptor = TestLayoutDescriptorAppendIfFastOrUseFull(
      isolate, 13, descriptors, kPropsCount);
  CHECK(!layout_descriptor->IsSlowLayout());

  layout_descriptor = TestLayoutDescriptorAppendIfFastOrUseFull(
893
      isolate, kBitsInSmiLayout, descriptors, kPropsCount);
894 895 896
  CHECK(!layout_descriptor->IsSlowLayout());

  layout_descriptor = TestLayoutDescriptorAppendIfFastOrUseFull(
897
      isolate, kBitsInSmiLayout + 1, descriptors, kPropsCount);
898 899 900
  CHECK(layout_descriptor->IsSlowLayout());

  layout_descriptor = TestLayoutDescriptorAppendIfFastOrUseFull(
901
      isolate, kBitsInSmiLayout * 2, descriptors, kPropsCount);
902 903 904 905 906 907 908 909 910
  CHECK(layout_descriptor->IsSlowLayout());

  layout_descriptor = TestLayoutDescriptorAppendIfFastOrUseFull(
      isolate, kPropsCount, descriptors, kPropsCount);
  CHECK(layout_descriptor->IsSlowLayout());

  {
    // Ensure layout descriptor switches into slow mode at the right moment.
    layout_descriptor = TestLayoutDescriptorAppendIfFastOrUseFull(
911
        isolate, kPropsCount, descriptors, kBitsInSmiLayout);
912 913 914
    CHECK(!layout_descriptor->IsSlowLayout());

    layout_descriptor = TestLayoutDescriptorAppendIfFastOrUseFull(
915
        isolate, kPropsCount, descriptors, kBitsInSmiLayout + 1);
916 917 918 919 920
    CHECK(layout_descriptor->IsSlowLayout());
  }
}


921
TEST(Regress436816) {
922
  ManualGCScope manual_gc_scope;
923 924 925 926 927
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  Factory* factory = isolate->factory();
  v8::HandleScope scope(CcTest::isolate());

928 929
  // Force a GC to free up space before we allocate objects whose
  // mid-test states would fail heap verification.
930
  CcTest::CollectAllGarbage();
931

932
  const int kPropsCount = kBitsInSmiLayout * 3;
933
  TestPropertyKind props[kPropsCount];
934 935 936 937 938 939 940 941
  for (int i = 0; i < kPropsCount; i++) {
    props[i] = PROP_DOUBLE;
  }
  Handle<DescriptorArray> descriptors =
      CreateDescriptorArray(isolate, props, kPropsCount);

  Handle<Map> map = Map::Create(isolate, kPropsCount);
  Handle<LayoutDescriptor> layout_descriptor =
942
      LayoutDescriptor::New(isolate, map, descriptors, kPropsCount);
943
  map->InitializeDescriptors(isolate, *descriptors, *layout_descriptor);
944

945 946
  Handle<JSObject> object =
      factory->NewJSObjectFromMap(map, AllocationType::kOld);
947

948
  Address fake_address = static_cast<Address>(~kHeapObjectTagMask);
949
  HeapObject fake_object = HeapObject::FromAddress(fake_address);
950
  CHECK(fake_object.IsHeapObject());
951

952
  uint64_t boom_value = bit_cast<uint64_t>(fake_object);
953
  for (InternalIndex i : InternalIndex::Range(kPropsCount)) {
954 955
    FieldIndex index = FieldIndex::ForDescriptor(*map, i);
    CHECK(map->IsUnboxedDoubleField(index));
956
    object->RawFastDoublePropertyAsBitsAtPut(index, boom_value);
957 958
  }
  CHECK(object->HasFastProperties());
959
  CHECK(!object->map().HasFastPointerLayout());
960 961

  Handle<Map> normalized_map =
962
      Map::Normalize(isolate, map, KEEP_INOBJECT_PROPERTIES, "testing");
963
  JSObject::MigrateToMap(isolate, object, normalized_map);
964
  CHECK(!object->HasFastProperties());
965
  CHECK(object->map().HasFastPointerLayout());
966 967

  // Trigger GCs and heap verification.
968
  CcTest::CollectAllGarbage();
969 970 971
}


972
TEST(DescriptorArrayTrimming) {
973
  ManualGCScope manual_gc_scope;
974 975 976 977 978 979 980 981
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
  Isolate* isolate = CcTest::i_isolate();

  const int kFieldCount = 128;
  const int kSplitFieldIndex = 32;
  const int kTrimmedLayoutDescriptorLength = 64;

982
  Handle<FieldType> any_type = FieldType::Any(isolate);
983 984
  Handle<Map> map = Map::Create(isolate, kFieldCount);
  for (int i = 0; i < kSplitFieldIndex; i++) {
985 986 987
    map = Map::CopyWithField(isolate, map, CcTest::MakeName("prop", i),
                             any_type, NONE, PropertyConstness::kMutable,
                             Representation::Smi(), INSERT_TRANSITION)
988
              .ToHandleChecked();
989
  }
990 991 992
  map = Map::CopyWithField(isolate, map,
                           CcTest::MakeName("dbl", kSplitFieldIndex), any_type,
                           NONE, PropertyConstness::kMutable,
993 994
                           Representation::Double(), INSERT_TRANSITION)
            .ToHandleChecked();
995 996
  CHECK(map->layout_descriptor(kAcquireLoad).IsConsistentWithMap(*map, true));
  CHECK(map->layout_descriptor(kAcquireLoad).IsSlowLayout());
997
  CHECK(map->owns_descriptors());
998
  CHECK_EQ(8, map->layout_descriptor(kAcquireLoad).length());
999 1000 1001 1002 1003 1004 1005

  {
    // Add transitions to double fields.
    v8::HandleScope scope(CcTest::isolate());

    Handle<Map> tmp_map = map;
    for (int i = kSplitFieldIndex + 1; i < kFieldCount; i++) {
1006
      tmp_map = Map::CopyWithField(isolate, tmp_map, CcTest::MakeName("dbl", i),
1007
                                   any_type, NONE, PropertyConstness::kMutable,
1008
                                   Representation::Double(), INSERT_TRANSITION)
1009
                    .ToHandleChecked();
1010 1011
      CHECK(tmp_map->layout_descriptor(kAcquireLoad)
                .IsConsistentWithMap(*tmp_map, true));
1012 1013 1014
    }
    // Check that descriptors are shared.
    CHECK(tmp_map->owns_descriptors());
1015 1016 1017 1018
    CHECK_EQ(map->instance_descriptors(kRelaxedLoad),
             tmp_map->instance_descriptors(kRelaxedLoad));
    CHECK_EQ(map->layout_descriptor(kAcquireLoad),
             tmp_map->layout_descriptor(kAcquireLoad));
1019
  }
1020 1021
  CHECK(map->layout_descriptor(kAcquireLoad).IsSlowLayout());
  CHECK_EQ(16, map->layout_descriptor(kAcquireLoad).length());
1022 1023

  // The unused tail of the layout descriptor is now "durty" because of sharing.
1024
  CHECK(map->layout_descriptor(kAcquireLoad).IsConsistentWithMap(*map));
1025
  for (int i = kSplitFieldIndex + 1; i < kTrimmedLayoutDescriptorLength; i++) {
1026
    CHECK(!map->layout_descriptor(kAcquireLoad).IsTagged(i));
1027 1028
  }
  CHECK_LT(map->NumberOfOwnDescriptors(),
1029
           map->instance_descriptors(kRelaxedLoad).number_of_descriptors());
1030 1031 1032

  // Call GC that should trim both |map|'s descriptor array and layout
  // descriptor.
1033
  CcTest::CollectAllGarbage();
1034 1035

  // The unused tail of the layout descriptor is now "clean" again.
1036
  CHECK(map->layout_descriptor(kAcquireLoad).IsConsistentWithMap(*map, true));
1037 1038
  CHECK(map->owns_descriptors());
  CHECK_EQ(map->NumberOfOwnDescriptors(),
1039 1040 1041
           map->instance_descriptors(kRelaxedLoad).number_of_descriptors());
  CHECK(map->layout_descriptor(kAcquireLoad).IsSlowLayout());
  CHECK_EQ(8, map->layout_descriptor(kAcquireLoad).length());
1042 1043 1044 1045 1046 1047 1048

  {
    // Add transitions to tagged fields.
    v8::HandleScope scope(CcTest::isolate());

    Handle<Map> tmp_map = map;
    for (int i = kSplitFieldIndex + 1; i < kFieldCount - 1; i++) {
1049 1050 1051 1052 1053
      tmp_map =
          Map::CopyWithField(isolate, tmp_map, CcTest::MakeName("tagged", i),
                             any_type, NONE, PropertyConstness::kMutable,
                             Representation::Tagged(), INSERT_TRANSITION)
              .ToHandleChecked();
1054 1055
      CHECK(tmp_map->layout_descriptor(kAcquireLoad)
                .IsConsistentWithMap(*tmp_map, true));
1056
    }
1057 1058
    tmp_map = Map::CopyWithField(isolate, tmp_map, CcTest::MakeString("dbl"),
                                 any_type, NONE, PropertyConstness::kMutable,
1059 1060
                                 Representation::Double(), INSERT_TRANSITION)
                  .ToHandleChecked();
1061 1062
    CHECK(tmp_map->layout_descriptor(kAcquireLoad)
              .IsConsistentWithMap(*tmp_map, true));
1063 1064
    // Check that descriptors are shared.
    CHECK(tmp_map->owns_descriptors());
1065 1066
    CHECK_EQ(map->instance_descriptors(kRelaxedLoad),
             tmp_map->instance_descriptors(kRelaxedLoad));
1067
  }
1068
  CHECK(map->layout_descriptor(kAcquireLoad).IsSlowLayout());
1069 1070 1071
}


1072 1073
TEST(DoScavenge) {
  CcTest::InitializeVM();
1074
  v8::HandleScope scope(CcTest::isolate());
1075 1076 1077
  Isolate* isolate = CcTest::i_isolate();
  Factory* factory = isolate->factory();

1078 1079 1080 1081
  // The plan: create |obj| with double field in new space, do scanvenge so
  // that |obj| is moved to old space, construct a double value that looks like
  // a pointer to "from space" pointer. Do scavenge one more time and ensure
  // that it didn't crash or corrupt the double value stored in the object.
1082

1083
  Handle<FieldType> any_type = FieldType::Any(isolate);
1084
  Handle<Map> map = Map::Create(isolate, 10);
1085 1086
  map = Map::CopyWithField(isolate, map, CcTest::MakeName("prop", 0), any_type,
                           NONE, PropertyConstness::kMutable,
1087 1088
                           Representation::Double(), INSERT_TRANSITION)
            .ToHandleChecked();
1089

1090
  // Create object in new space.
1091 1092
  Handle<JSObject> obj =
      factory->NewJSObjectFromMap(map, AllocationType::kYoung);
1093 1094

  Handle<HeapNumber> heap_number = factory->NewHeapNumber(42.5);
1095
  WriteToField(*obj, 0, *heap_number);
1096 1097 1098

  {
    // Ensure the object is properly set up.
1099
    FieldIndex field_index = FieldIndex::ForDescriptor(*map, InternalIndex(0));
1100 1101 1102 1103 1104 1105
    CHECK(field_index.is_inobject() && field_index.is_double());
    CHECK_EQ(FLAG_unbox_double_fields, map->IsUnboxedDoubleField(field_index));
    CHECK_EQ(42.5, GetDoubleFieldValue(*obj, field_index));
  }
  CHECK(isolate->heap()->new_space()->Contains(*obj));

1106
  // Do scavenge so that |obj| is moved to survivor space.
1107
  CcTest::CollectGarbage(i::NEW_SPACE);
1108 1109

  // Create temp object in the new space.
1110
  Handle<JSArray> temp = factory->NewJSArray(0, PACKED_ELEMENTS);
1111 1112 1113 1114
  CHECK(isolate->heap()->new_space()->Contains(*temp));

  // Construct a double value that looks like a pointer to the new space object
  // and store it into the obj.
1115
  Address fake_object = temp->ptr() + kSystemPointerSize;
1116 1117
  double boom_value = bit_cast<double>(fake_object);

1118 1119
  FieldIndex field_index =
      FieldIndex::ForDescriptor(obj->map(), InternalIndex(0));
1120
  auto boom_number = factory->NewHeapNumber(boom_value);
1121 1122
  obj->FastPropertyAtPut(field_index, *boom_number);

1123
  // Now |obj| moves to old gen and it has a double field that looks like
1124
  // a pointer to a from semi-space.
1125
  CcTest::CollectGarbage(i::NEW_SPACE);
1126

1127
  CHECK(isolate->heap()->old_space()->Contains(*obj));
1128 1129 1130 1131 1132

  CHECK_EQ(boom_value, GetDoubleFieldValue(*obj, field_index));
}


1133 1134
TEST(DoScavengeWithIncrementalWriteBarrier) {
  if (FLAG_never_compact || !FLAG_incremental_marking) return;
1135
  ManualGCScope manual_gc_scope;
1136 1137 1138 1139 1140
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
  Isolate* isolate = CcTest::i_isolate();
  Factory* factory = isolate->factory();
  Heap* heap = CcTest::heap();
1141
  PagedSpace* old_space = heap->old_space();
1142 1143 1144 1145 1146 1147 1148

  // The plan: create |obj_value| in old space and ensure that it is allocated
  // on evacuation candidate page, create |obj| with double and tagged fields
  // in new space and write |obj_value| to tagged field of |obj|, do two
  // scavenges to promote |obj| to old space, a GC in old space and ensure that
  // the tagged value was properly updated after candidates evacuation.

1149
  Handle<FieldType> any_type = FieldType::Any(isolate);
1150
  Handle<Map> map = Map::Create(isolate, 10);
1151 1152
  map = Map::CopyWithField(isolate, map, CcTest::MakeName("prop", 0), any_type,
                           NONE, PropertyConstness::kMutable,
1153 1154
                           Representation::Double(), INSERT_TRANSITION)
            .ToHandleChecked();
1155 1156
  map = Map::CopyWithField(isolate, map, CcTest::MakeName("prop", 1), any_type,
                           NONE, PropertyConstness::kMutable,
1157 1158
                           Representation::Tagged(), INSERT_TRANSITION)
            .ToHandleChecked();
1159 1160 1161 1162 1163 1164 1165

  // Create |obj_value| in old space.
  Handle<HeapObject> obj_value;
  Page* ec_page;
  {
    AlwaysAllocateScope always_allocate(isolate);
    // Make sure |obj_value| is placed on an old-space evacuation candidate.
1166
    heap::SimulateFullSpace(old_space);
1167 1168
    obj_value =
        factory->NewJSArray(32 * KB, HOLEY_ELEMENTS, AllocationType::kOld);
1169
    ec_page = Page::FromHeapObject(*obj_value);
1170 1171 1172
  }

  // Create object in new space.
1173 1174
  Handle<JSObject> obj =
      factory->NewJSObjectFromMap(map, AllocationType::kYoung);
1175 1176

  Handle<HeapNumber> heap_number = factory->NewHeapNumber(42.5);
1177 1178
  WriteToField(*obj, 0, *heap_number);
  WriteToField(*obj, 1, *obj_value);
1179 1180 1181

  {
    // Ensure the object is properly set up.
1182
    FieldIndex field_index = FieldIndex::ForDescriptor(*map, InternalIndex(0));
1183 1184 1185 1186
    CHECK(field_index.is_inobject() && field_index.is_double());
    CHECK_EQ(FLAG_unbox_double_fields, map->IsUnboxedDoubleField(field_index));
    CHECK_EQ(42.5, GetDoubleFieldValue(*obj, field_index));

1187
    field_index = FieldIndex::ForDescriptor(*map, InternalIndex(1));
1188 1189 1190 1191 1192 1193 1194 1195 1196
    CHECK(field_index.is_inobject() && !field_index.is_double());
    CHECK(!map->IsUnboxedDoubleField(field_index));
  }
  CHECK(isolate->heap()->new_space()->Contains(*obj));

  // Heap is ready, force |ec_page| to become an evacuation candidate and
  // simulate incremental marking.
  FLAG_stress_compaction = true;
  FLAG_manual_evacuation_candidates_selection = true;
1197
  heap::ForceEvacuationCandidate(ec_page);
1198
  heap::SimulateIncrementalMarking(heap);
1199 1200 1201 1202 1203 1204 1205 1206
  // Disable stress compaction mode in order to let GC do scavenge.
  FLAG_stress_compaction = false;

  // Check that everything is ready for triggering incremental write barrier
  // during scavenge (i.e. that |obj| is black and incremental marking is
  // in compacting mode and |obj_value|'s page is an evacuation candidate).
  IncrementalMarking* marking = heap->incremental_marking();
  CHECK(marking->IsCompacting());
1207 1208
  IncrementalMarking::MarkingState* marking_state =
      heap->incremental_marking()->marking_state();
1209
  CHECK(marking_state->IsBlack(*obj));
1210 1211 1212
  CHECK(MarkCompactCollector::IsOnEvacuationCandidate(*obj_value));

  // Trigger GCs so that |obj| moves to old gen.
1213 1214
  CcTest::CollectGarbage(i::NEW_SPACE);  // in survivor space now
  CcTest::CollectGarbage(i::NEW_SPACE);  // in old gen now
1215

1216 1217
  CHECK(isolate->heap()->old_space()->Contains(*obj));
  CHECK(isolate->heap()->old_space()->Contains(*obj_value));
1218 1219
  CHECK(MarkCompactCollector::IsOnEvacuationCandidate(*obj_value));

1220
  CcTest::CollectGarbage(i::OLD_SPACE);
1221 1222 1223 1224

  // |obj_value| must be evacuated.
  CHECK(!MarkCompactCollector::IsOnEvacuationCandidate(*obj_value));

1225
  FieldIndex field_index = FieldIndex::ForDescriptor(*map, InternalIndex(1));
1226 1227 1228 1229
  CHECK_EQ(*obj_value, obj->RawFastPropertyAt(field_index));
}


1230 1231 1232 1233 1234 1235 1236
static void TestLayoutDescriptorHelper(Isolate* isolate,
                                       int inobject_properties,
                                       Handle<DescriptorArray> descriptors,
                                       int number_of_descriptors) {
  Handle<Map> map = Map::Create(isolate, inobject_properties);

  Handle<LayoutDescriptor> layout_descriptor = LayoutDescriptor::New(
1237
      isolate, map, descriptors, descriptors->number_of_descriptors());
1238 1239
  InitializeVerifiedMapDescriptors(isolate, *map, *descriptors,
                                   *layout_descriptor);
1240 1241 1242 1243 1244 1245 1246 1247

  LayoutDescriptorHelper helper(*map);
  bool all_fields_tagged = true;

  int instance_size = map->instance_size();

  int end_offset = instance_size * 2;
  int first_non_tagged_field_offset = end_offset;
1248
  for (InternalIndex i : InternalIndex::Range(number_of_descriptors)) {
1249
    PropertyDetails details = descriptors->GetDetails(i);
1250
    if (details.location() != kField) continue;
1251 1252 1253 1254 1255 1256
    FieldIndex index = FieldIndex::ForDescriptor(*map, i);
    if (!index.is_inobject()) continue;
    all_fields_tagged &= !details.representation().IsDouble();
    bool expected_tagged = !index.is_double();
    if (!expected_tagged) {
      first_non_tagged_field_offset =
1257
          std::min(first_non_tagged_field_offset, index.offset());
1258 1259 1260 1261 1262 1263
    }

    int end_of_region_offset;
    CHECK_EQ(expected_tagged, helper.IsTagged(index.offset()));
    CHECK_EQ(expected_tagged, helper.IsTagged(index.offset(), instance_size,
                                              &end_of_region_offset));
1264
    CHECK_GT(end_of_region_offset, 0);
1265
    CHECK_EQ(end_of_region_offset % kTaggedSize, 0);
1266 1267 1268
    CHECK(end_of_region_offset <= instance_size);

    for (int offset = index.offset(); offset < end_of_region_offset;
1269
         offset += kTaggedSize) {
1270 1271 1272 1273 1274
      CHECK_EQ(expected_tagged, helper.IsTagged(index.offset()));
    }
    if (end_of_region_offset < instance_size) {
      CHECK_EQ(!expected_tagged, helper.IsTagged(end_of_region_offset));
    } else {
1275
      CHECK(helper.IsTagged(end_of_region_offset));
1276 1277 1278
    }
  }

1279
  for (int offset = 0; offset < JSObject::kHeaderSize; offset += kTaggedSize) {
1280
    // Header queries
1281
    CHECK(helper.IsTagged(offset));
1282
    int end_of_region_offset;
1283
    CHECK(helper.IsTagged(offset, end_offset, &end_of_region_offset));
1284 1285 1286
    CHECK_EQ(first_non_tagged_field_offset, end_of_region_offset);

    // Out of bounds queries
1287
    CHECK(helper.IsTagged(offset + instance_size));
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
  }

  CHECK_EQ(all_fields_tagged, helper.all_fields_tagged());
}


TEST(LayoutDescriptorHelperMixed) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  v8::HandleScope scope(CcTest::isolate());

  Handle<LayoutDescriptor> layout_descriptor;
1300
  const int kPropsCount = kBitsInSmiLayout * 3;
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
  TestPropertyKind props[kPropsCount];
  for (int i = 0; i < kPropsCount; i++) {
    props[i] = static_cast<TestPropertyKind>(i % PROP_KIND_NUMBER);
  }
  Handle<DescriptorArray> descriptors =
      CreateDescriptorArray(isolate, props, kPropsCount);

  TestLayoutDescriptorHelper(isolate, 0, descriptors, kPropsCount);

  TestLayoutDescriptorHelper(isolate, 13, descriptors, kPropsCount);

1312 1313
  TestLayoutDescriptorHelper(isolate, kBitsInSmiLayout, descriptors,
                             kPropsCount);
1314

1315
  TestLayoutDescriptorHelper(isolate, kBitsInSmiLayout * 2, descriptors,
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
                             kPropsCount);

  TestLayoutDescriptorHelper(isolate, kPropsCount, descriptors, kPropsCount);
}


TEST(LayoutDescriptorHelperAllTagged) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  v8::HandleScope scope(CcTest::isolate());

  Handle<LayoutDescriptor> layout_descriptor;
1328
  const int kPropsCount = kBitsInSmiLayout * 3;
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
  TestPropertyKind props[kPropsCount];
  for (int i = 0; i < kPropsCount; i++) {
    props[i] = PROP_TAGGED;
  }
  Handle<DescriptorArray> descriptors =
      CreateDescriptorArray(isolate, props, kPropsCount);

  TestLayoutDescriptorHelper(isolate, 0, descriptors, kPropsCount);

  TestLayoutDescriptorHelper(isolate, 13, descriptors, kPropsCount);

1340 1341
  TestLayoutDescriptorHelper(isolate, kBitsInSmiLayout, descriptors,
                             kPropsCount);
1342

1343
  TestLayoutDescriptorHelper(isolate, kBitsInSmiLayout * 2, descriptors,
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
                             kPropsCount);

  TestLayoutDescriptorHelper(isolate, kPropsCount, descriptors, kPropsCount);
}


TEST(LayoutDescriptorHelperAllDoubles) {
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  v8::HandleScope scope(CcTest::isolate());

  Handle<LayoutDescriptor> layout_descriptor;
1356
  const int kPropsCount = kBitsInSmiLayout * 3;
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
  TestPropertyKind props[kPropsCount];
  for (int i = 0; i < kPropsCount; i++) {
    props[i] = PROP_DOUBLE;
  }
  Handle<DescriptorArray> descriptors =
      CreateDescriptorArray(isolate, props, kPropsCount);

  TestLayoutDescriptorHelper(isolate, 0, descriptors, kPropsCount);

  TestLayoutDescriptorHelper(isolate, 13, descriptors, kPropsCount);

1368 1369
  TestLayoutDescriptorHelper(isolate, kBitsInSmiLayout, descriptors,
                             kPropsCount);
1370

1371
  TestLayoutDescriptorHelper(isolate, kBitsInSmiLayout * 2, descriptors,
1372 1373 1374 1375 1376 1377
                             kPropsCount);

  TestLayoutDescriptorHelper(isolate, kPropsCount, descriptors, kPropsCount);
}


1378 1379 1380 1381
TEST(LayoutDescriptorSharing) {
  CcTest::InitializeVM();
  v8::HandleScope scope(CcTest::isolate());
  Isolate* isolate = CcTest::i_isolate();
1382
  Handle<FieldType> any_type = FieldType::Any(isolate);
1383 1384 1385 1386 1387

  Handle<Map> split_map;
  {
    Handle<Map> map = Map::Create(isolate, 64);
    for (int i = 0; i < 32; i++) {
1388
      Handle<String> name = CcTest::MakeName("prop", i);
1389
      map = Map::CopyWithField(isolate, map, name, any_type, NONE,
1390
                               PropertyConstness::kMutable,
1391 1392
                               Representation::Smi(), INSERT_TRANSITION)
                .ToHandleChecked();
1393
    }
1394 1395
    split_map = Map::CopyWithField(isolate, map, CcTest::MakeString("dbl"),
                                   any_type, NONE, PropertyConstness::kMutable,
1396 1397
                                   Representation::Double(), INSERT_TRANSITION)
                    .ToHandleChecked();
1398 1399
  }
  Handle<LayoutDescriptor> split_layout_descriptor(
1400
      split_map->layout_descriptor(kAcquireLoad), isolate);
1401
  CHECK(split_layout_descriptor->IsConsistentWithMap(*split_map, true));
1402 1403 1404
  CHECK(split_layout_descriptor->IsSlowLayout());
  CHECK(split_map->owns_descriptors());

1405
  Handle<Map> map1 =
1406 1407 1408
      Map::CopyWithField(isolate, split_map, CcTest::MakeString("foo"),
                         any_type, NONE, PropertyConstness::kMutable,
                         Representation::Double(), INSERT_TRANSITION)
1409
          .ToHandleChecked();
1410
  CHECK(!split_map->owns_descriptors());
1411 1412
  CHECK_EQ(*split_layout_descriptor,
           split_map->layout_descriptor(kAcquireLoad));
1413 1414 1415

  // Layout descriptors should be shared with |split_map|.
  CHECK(map1->owns_descriptors());
1416 1417
  CHECK_EQ(*split_layout_descriptor, map1->layout_descriptor(kAcquireLoad));
  CHECK(map1->layout_descriptor(kAcquireLoad).IsConsistentWithMap(*map1, true));
1418

1419
  Handle<Map> map2 =
1420 1421 1422
      Map::CopyWithField(isolate, split_map, CcTest::MakeString("bar"),
                         any_type, NONE, PropertyConstness::kMutable,
                         Representation::Tagged(), INSERT_TRANSITION)
1423
          .ToHandleChecked();
1424 1425 1426

  // Layout descriptors should not be shared with |split_map|.
  CHECK(map2->owns_descriptors());
1427 1428
  CHECK_NE(*split_layout_descriptor, map2->layout_descriptor(kAcquireLoad));
  CHECK(map2->layout_descriptor(kAcquireLoad).IsConsistentWithMap(*map2, true));
1429 1430
}

1431
static void TestWriteBarrier(Handle<Map> map, Handle<Map> new_map,
1432 1433
                             InternalIndex tagged_descriptor,
                             InternalIndex double_descriptor,
1434 1435 1436 1437 1438 1439
                             bool check_tagged_value = true) {
  FLAG_stress_compaction = true;
  FLAG_manual_evacuation_candidates_selection = true;
  Isolate* isolate = CcTest::i_isolate();
  Factory* factory = isolate->factory();
  Heap* heap = CcTest::heap();
1440
  PagedSpace* old_space = heap->old_space();
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452

  // The plan: create |obj| by |map| in old space, create |obj_value| in
  // new space and ensure that write barrier is triggered when |obj_value| is
  // written to property |tagged_descriptor| of |obj|.
  // Then migrate object to |new_map| and set proper value for property
  // |double_descriptor|. Call GC and ensure that it did not crash during
  // store buffer entries updating.

  Handle<JSObject> obj;
  Handle<HeapObject> obj_value;
  {
    AlwaysAllocateScope always_allocate(isolate);
1453
    obj = factory->NewJSObjectFromMap(map, AllocationType::kOld);
1454
    CHECK(old_space->Contains(*obj));
1455

1456
    obj_value = factory->NewHeapNumber(0.);
1457 1458
  }

1459
  CHECK(Heap::InYoungGeneration(*obj_value));
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470

  {
    FieldIndex index = FieldIndex::ForDescriptor(*map, tagged_descriptor);
    const int n = 153;
    for (int i = 0; i < n; i++) {
      obj->FastPropertyAtPut(index, *obj_value);
    }
  }

  // Migrate |obj| to |new_map| which should shift fields and put the
  // |boom_value| to the slot that was earlier recorded by write barrier.
1471
  JSObject::MigrateToMap(isolate, obj, new_map);
1472

1473
  Address fake_object = obj_value->ptr() + kTaggedSize;
1474
  uint64_t boom_value = bit_cast<uint64_t>(fake_object);
1475 1476 1477 1478

  FieldIndex double_field_index =
      FieldIndex::ForDescriptor(*new_map, double_descriptor);
  CHECK(obj->IsUnboxedDoubleField(double_field_index));
1479
  obj->RawFastDoublePropertyAsBitsAtPut(double_field_index, boom_value);
1480 1481

  // Trigger GC to evacuate all candidates.
1482
  CcTest::CollectGarbage(NEW_SPACE);
1483 1484 1485 1486 1487 1488

  if (check_tagged_value) {
    FieldIndex tagged_field_index =
        FieldIndex::ForDescriptor(*new_map, tagged_descriptor);
    CHECK_EQ(*obj_value, obj->RawFastPropertyAt(tagged_field_index));
  }
1489
  CHECK_EQ(boom_value, obj->RawFastDoublePropertyAsBitsAt(double_field_index));
1490 1491 1492
}

static void TestIncrementalWriteBarrier(Handle<Map> map, Handle<Map> new_map,
1493 1494
                                        InternalIndex tagged_descriptor,
                                        InternalIndex double_descriptor,
1495 1496
                                        bool check_tagged_value = true) {
  if (FLAG_never_compact || !FLAG_incremental_marking) return;
1497
  ManualGCScope manual_gc_scope;
1498 1499 1500 1501
  FLAG_manual_evacuation_candidates_selection = true;
  Isolate* isolate = CcTest::i_isolate();
  Factory* factory = isolate->factory();
  Heap* heap = CcTest::heap();
1502
  PagedSpace* old_space = heap->old_space();
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516

  // The plan: create |obj| by |map| in old space, create |obj_value| in
  // old space and ensure it end up in evacuation candidate page. Start
  // incremental marking and ensure that incremental write barrier is triggered
  // when |obj_value| is written to property |tagged_descriptor| of |obj|.
  // Then migrate object to |new_map| and set proper value for property
  // |double_descriptor|. Call GC and ensure that it did not crash during
  // slots buffer entries updating.

  Handle<JSObject> obj;
  Handle<HeapObject> obj_value;
  Page* ec_page;
  {
    AlwaysAllocateScope always_allocate(isolate);
1517
    obj = factory->NewJSObjectFromMap(map, AllocationType::kOld);
1518
    CHECK(old_space->Contains(*obj));
1519 1520

    // Make sure |obj_value| is placed on an old-space evacuation candidate.
1521
    heap::SimulateFullSpace(old_space);
1522 1523
    obj_value =
        factory->NewJSArray(32 * KB, HOLEY_ELEMENTS, AllocationType::kOld);
1524 1525
    ec_page = Page::FromHeapObject(*obj_value);
    CHECK_NE(ec_page, Page::FromHeapObject(*obj));
1526 1527 1528 1529
  }

  // Heap is ready, force |ec_page| to become an evacuation candidate and
  // simulate incremental marking.
1530
  heap::ForceEvacuationCandidate(ec_page);
1531
  heap::SimulateIncrementalMarking(heap);
1532 1533 1534 1535 1536 1537

  // Check that everything is ready for triggering incremental write barrier
  // (i.e. that both |obj| and |obj_value| are black and the marking phase is
  // still active and |obj_value|'s page is indeed an evacuation candidate).
  IncrementalMarking* marking = heap->incremental_marking();
  CHECK(marking->IsMarking());
1538
  IncrementalMarking::MarkingState* marking_state = marking->marking_state();
1539 1540
  CHECK(marking_state->IsBlack(*obj));
  CHECK(marking_state->IsBlack(*obj_value));
1541 1542
  CHECK(MarkCompactCollector::IsOnEvacuationCandidate(*obj_value));

1543 1544
  // Trigger incremental write barrier, which should add a slot to remembered
  // set.
1545 1546
  {
    FieldIndex index = FieldIndex::ForDescriptor(*map, tagged_descriptor);
1547
    obj->FastPropertyAtPut(index, *obj_value);
1548 1549 1550 1551 1552
  }

  // Migrate |obj| to |new_map| which should shift fields and put the
  // |boom_value| to the slot that was earlier recorded by incremental write
  // barrier.
1553
  JSObject::MigrateToMap(isolate, obj, new_map);
1554

1555
  uint64_t boom_value = UINT64_C(0xBAAD0176A37C28E1);
1556 1557 1558 1559

  FieldIndex double_field_index =
      FieldIndex::ForDescriptor(*new_map, double_descriptor);
  CHECK(obj->IsUnboxedDoubleField(double_field_index));
1560
  obj->RawFastDoublePropertyAsBitsAtPut(double_field_index, boom_value);
1561 1562

  // Trigger GC to evacuate all candidates.
1563
  CcTest::CollectGarbage(OLD_SPACE);
1564 1565 1566 1567 1568 1569 1570 1571 1572

  // Ensure that the values are still there and correct.
  CHECK(!MarkCompactCollector::IsOnEvacuationCandidate(*obj_value));

  if (check_tagged_value) {
    FieldIndex tagged_field_index =
        FieldIndex::ForDescriptor(*new_map, tagged_descriptor);
    CHECK_EQ(*obj_value, obj->RawFastPropertyAt(tagged_field_index));
  }
1573
  CHECK_EQ(boom_value, obj->RawFastDoublePropertyAsBitsAt(double_field_index));
1574 1575
}

1576 1577 1578 1579
enum OldToWriteBarrierKind {
  OLD_TO_OLD_WRITE_BARRIER,
  OLD_TO_NEW_WRITE_BARRIER
};
1580
static void TestWriteBarrierObjectShiftFieldsRight(
1581
    OldToWriteBarrierKind write_barrier_kind) {
1582
  ManualGCScope manual_gc_scope;
1583 1584 1585 1586
  CcTest::InitializeVM();
  Isolate* isolate = CcTest::i_isolate();
  v8::HandleScope scope(CcTest::isolate());

1587
  Handle<FieldType> any_type = FieldType::Any(isolate);
1588 1589 1590 1591 1592 1593

  CompileRun("function func() { return 1; }");

  Handle<JSObject> func = GetObject("func");

  Handle<Map> map = Map::Create(isolate, 10);
1594 1595
  map = Map::CopyWithConstant(isolate, map, CcTest::MakeName("prop", 0), func,
                              NONE, INSERT_TRANSITION)
1596
            .ToHandleChecked();
1597 1598
  map = Map::CopyWithField(isolate, map, CcTest::MakeName("prop", 1), any_type,
                           NONE, PropertyConstness::kMutable,
1599 1600
                           Representation::Double(), INSERT_TRANSITION)
            .ToHandleChecked();
1601 1602
  map = Map::CopyWithField(isolate, map, CcTest::MakeName("prop", 2), any_type,
                           NONE, PropertyConstness::kMutable,
1603 1604
                           Representation::Tagged(), INSERT_TRANSITION)
            .ToHandleChecked();
1605 1606

  // Shift fields right by turning constant property to a field.
1607 1608 1609
  Handle<Map> new_map =
      Map::ReconfigureProperty(isolate, map, InternalIndex(0), kData, NONE,
                               Representation::Tagged(), any_type);
1610 1611

  if (write_barrier_kind == OLD_TO_NEW_WRITE_BARRIER) {
1612
    TestWriteBarrier(map, new_map, InternalIndex(2), InternalIndex(1));
1613 1614
  } else {
    CHECK_EQ(OLD_TO_OLD_WRITE_BARRIER, write_barrier_kind);
1615 1616
    TestIncrementalWriteBarrier(map, new_map, InternalIndex(2),
                                InternalIndex(1));
1617 1618 1619
  }
}

1620
TEST(WriteBarrierObjectShiftFieldsRight) {
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
  TestWriteBarrierObjectShiftFieldsRight(OLD_TO_NEW_WRITE_BARRIER);
}


TEST(IncrementalWriteBarrierObjectShiftFieldsRight) {
  TestWriteBarrierObjectShiftFieldsRight(OLD_TO_OLD_WRITE_BARRIER);
}


// TODO(ishell): add respective tests for property kind reconfiguring from
// accessor field to double, once accessor fields are supported by
// Map::ReconfigureProperty().


// TODO(ishell): add respective tests for fast property removal case once
// Map::ReconfigureProperty() supports that.

1638
#endif
1639

1640
}  // namespace test_unboxed_doubles
1641 1642
}  // namespace internal
}  // namespace v8