11cb0ef41Sopenharmony_ci// Copyright 2018 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/builtins/growable-fixed-array-gen.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci#include "src/compiler/code-assembler.h"
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_cinamespace v8 {
101cb0ef41Sopenharmony_cinamespace internal {
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_civoid GrowableFixedArray::Push(const TNode<Object> value) {
131cb0ef41Sopenharmony_ci  const TNode<IntPtrT> length = var_length_.value();
141cb0ef41Sopenharmony_ci  const TNode<IntPtrT> capacity = var_capacity_.value();
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  Label grow(this), store(this);
171cb0ef41Sopenharmony_ci  Branch(IntPtrEqual(capacity, length), &grow, &store);
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  BIND(&grow);
201cb0ef41Sopenharmony_ci  {
211cb0ef41Sopenharmony_ci    var_capacity_ = NewCapacity(capacity);
221cb0ef41Sopenharmony_ci    var_array_ = ResizeFixedArray(length, var_capacity_.value());
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci    Goto(&store);
251cb0ef41Sopenharmony_ci  }
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  BIND(&store);
281cb0ef41Sopenharmony_ci  {
291cb0ef41Sopenharmony_ci    const TNode<FixedArray> array = var_array_.value();
301cb0ef41Sopenharmony_ci    UnsafeStoreFixedArrayElement(array, length, value);
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci    var_length_ = IntPtrAdd(length, IntPtrConstant(1));
331cb0ef41Sopenharmony_ci  }
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciTNode<FixedArray> GrowableFixedArray::ToFixedArray() {
371cb0ef41Sopenharmony_ci  return ResizeFixedArray(length(), length());
381cb0ef41Sopenharmony_ci}
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ciTNode<JSArray> GrowableFixedArray::ToJSArray(const TNode<Context> context) {
411cb0ef41Sopenharmony_ci  const ElementsKind kind = PACKED_ELEMENTS;
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  const TNode<NativeContext> native_context = LoadNativeContext(context);
441cb0ef41Sopenharmony_ci  const TNode<Map> array_map = LoadJSArrayElementsMap(kind, native_context);
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  // Shrink to fit if necessary.
471cb0ef41Sopenharmony_ci  {
481cb0ef41Sopenharmony_ci    Label next(this);
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci    const TNode<IntPtrT> length = var_length_.value();
511cb0ef41Sopenharmony_ci    const TNode<IntPtrT> capacity = var_capacity_.value();
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci    GotoIf(WordEqual(length, capacity), &next);
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci    var_array_ = ResizeFixedArray(length, length);
561cb0ef41Sopenharmony_ci    var_capacity_ = length;
571cb0ef41Sopenharmony_ci    Goto(&next);
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci    BIND(&next);
601cb0ef41Sopenharmony_ci  }
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  const TNode<Smi> result_length = SmiTag(length());
631cb0ef41Sopenharmony_ci  const TNode<JSArray> result =
641cb0ef41Sopenharmony_ci      AllocateJSArray(array_map, var_array_.value(), result_length);
651cb0ef41Sopenharmony_ci  return result;
661cb0ef41Sopenharmony_ci}
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciTNode<IntPtrT> GrowableFixedArray::NewCapacity(
691cb0ef41Sopenharmony_ci    TNode<IntPtrT> current_capacity) {
701cb0ef41Sopenharmony_ci  CSA_DCHECK(this,
711cb0ef41Sopenharmony_ci             IntPtrGreaterThanOrEqual(current_capacity, IntPtrConstant(0)));
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  // Growth rate is analog to JSObject::NewElementsCapacity:
741cb0ef41Sopenharmony_ci  // new_capacity = (current_capacity + (current_capacity >> 1)) + 16.
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci  const TNode<IntPtrT> new_capacity =
771cb0ef41Sopenharmony_ci      IntPtrAdd(IntPtrAdd(current_capacity, WordShr(current_capacity, 1)),
781cb0ef41Sopenharmony_ci                IntPtrConstant(16));
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  return new_capacity;
811cb0ef41Sopenharmony_ci}
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ciTNode<FixedArray> GrowableFixedArray::ResizeFixedArray(
841cb0ef41Sopenharmony_ci    const TNode<IntPtrT> element_count, const TNode<IntPtrT> new_capacity) {
851cb0ef41Sopenharmony_ci  CSA_DCHECK(this, IntPtrGreaterThanOrEqual(element_count, IntPtrConstant(0)));
861cb0ef41Sopenharmony_ci  CSA_DCHECK(this, IntPtrGreaterThanOrEqual(new_capacity, IntPtrConstant(0)));
871cb0ef41Sopenharmony_ci  CSA_DCHECK(this, IntPtrGreaterThanOrEqual(new_capacity, element_count));
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  const TNode<FixedArray> from_array = var_array_.value();
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci  CodeStubAssembler::ExtractFixedArrayFlags flags;
921cb0ef41Sopenharmony_ci  flags |= CodeStubAssembler::ExtractFixedArrayFlag::kFixedArrays;
931cb0ef41Sopenharmony_ci  TNode<FixedArray> to_array = CAST(ExtractFixedArray(
941cb0ef41Sopenharmony_ci      from_array, base::Optional<TNode<IntPtrT>>(base::nullopt),
951cb0ef41Sopenharmony_ci      base::Optional<TNode<IntPtrT>>(element_count),
961cb0ef41Sopenharmony_ci      base::Optional<TNode<IntPtrT>>(new_capacity), flags));
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci  return to_array;
991cb0ef41Sopenharmony_ci}
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci}  // namespace internal
1021cb0ef41Sopenharmony_ci}  // namespace v8
103