growable-fixed-array-gen.cc 3.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2018 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 "src/builtins/growable-fixed-array-gen.h"

#include "src/compiler/code-assembler.h"

namespace v8 {
namespace internal {

12 13 14
void GrowableFixedArray::Push(const TNode<Object> value) {
  const TNode<IntPtrT> length = var_length_.value();
  const TNode<IntPtrT> capacity = var_capacity_.value();
15 16 17 18 19 20 21

  Label grow(this), store(this);
  Branch(IntPtrEqual(capacity, length), &grow, &store);

  BIND(&grow);
  {
    var_capacity_ = NewCapacity(capacity);
22
    var_array_ = ResizeFixedArray(length, var_capacity_.value());
23 24 25 26 27 28

    Goto(&store);
  }

  BIND(&store);
  {
29
    const TNode<FixedArray> array = var_array_.value();
30
    UnsafeStoreFixedArrayElement(array, length, value);
31 32 33 34 35

    var_length_ = IntPtrAdd(length, IntPtrConstant(1));
  }
}

36 37 38 39
TNode<FixedArray> GrowableFixedArray::ToFixedArray() {
  return ResizeFixedArray(length(), length());
}

40
TNode<JSArray> GrowableFixedArray::ToJSArray(const TNode<Context> context) {
41 42
  const ElementsKind kind = PACKED_ELEMENTS;

43 44
  const TNode<NativeContext> native_context = LoadNativeContext(context);
  const TNode<Map> array_map = LoadJSArrayElementsMap(kind, native_context);
45 46 47 48 49

  // Shrink to fit if necessary.
  {
    Label next(this);

50 51
    const TNode<IntPtrT> length = var_length_.value();
    const TNode<IntPtrT> capacity = var_capacity_.value();
52 53 54 55 56 57 58 59 60 61

    GotoIf(WordEqual(length, capacity), &next);

    var_array_ = ResizeFixedArray(length, length);
    var_capacity_ = length;
    Goto(&next);

    BIND(&next);
  }

62 63
  const TNode<Smi> result_length = SmiTag(length());
  const TNode<JSArray> result =
64
      AllocateJSArray(array_map, var_array_.value(), result_length);
65 66 67 68 69 70 71 72 73 74 75
  return result;
}

TNode<IntPtrT> GrowableFixedArray::NewCapacity(
    TNode<IntPtrT> current_capacity) {
  CSA_ASSERT(this,
             IntPtrGreaterThanOrEqual(current_capacity, IntPtrConstant(0)));

  // Growth rate is analog to JSObject::NewElementsCapacity:
  // new_capacity = (current_capacity + (current_capacity >> 1)) + 16.

76
  const TNode<IntPtrT> new_capacity =
77 78 79 80 81 82 83
      IntPtrAdd(IntPtrAdd(current_capacity, WordShr(current_capacity, 1)),
                IntPtrConstant(16));

  return new_capacity;
}

TNode<FixedArray> GrowableFixedArray::ResizeFixedArray(
84
    const TNode<IntPtrT> element_count, const TNode<IntPtrT> new_capacity) {
85 86 87 88
  CSA_ASSERT(this, IntPtrGreaterThanOrEqual(element_count, IntPtrConstant(0)));
  CSA_ASSERT(this, IntPtrGreaterThanOrEqual(new_capacity, IntPtrConstant(0)));
  CSA_ASSERT(this, IntPtrGreaterThanOrEqual(new_capacity, element_count));

89
  const TNode<FixedArray> from_array = var_array_.value();
90 91 92

  CodeStubAssembler::ExtractFixedArrayFlags flags;
  flags |= CodeStubAssembler::ExtractFixedArrayFlag::kFixedArrays;
93
  TNode<FixedArray> to_array = CAST(ExtractFixedArray(
94 95 96
      from_array, base::Optional<TNode<IntPtrT>>(base::nullopt),
      base::Optional<TNode<IntPtrT>>(element_count),
      base::Optional<TNode<IntPtrT>>(new_capacity), flags));
97 98 99 100 101 102

  return to_array;
}

}  // namespace internal
}  // namespace v8