1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
// 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 {
void GrowableFixedArray::Push(const TNode<Object> value) {
const TNode<IntPtrT> length = var_length_.value();
const TNode<IntPtrT> capacity = var_capacity_.value();
Label grow(this), store(this);
Branch(IntPtrEqual(capacity, length), &grow, &store);
BIND(&grow);
{
var_capacity_ = NewCapacity(capacity);
var_array_ = ResizeFixedArray(length, var_capacity_.value());
Goto(&store);
}
BIND(&store);
{
const TNode<FixedArray> array = var_array_.value();
UnsafeStoreFixedArrayElement(array, length, value);
var_length_ = IntPtrAdd(length, IntPtrConstant(1));
}
}
TNode<FixedArray> GrowableFixedArray::ToFixedArray() {
return ResizeFixedArray(length(), length());
}
TNode<JSArray> GrowableFixedArray::ToJSArray(const TNode<Context> context) {
const ElementsKind kind = PACKED_ELEMENTS;
const TNode<NativeContext> native_context = LoadNativeContext(context);
const TNode<Map> array_map = LoadJSArrayElementsMap(kind, native_context);
// Shrink to fit if necessary.
{
Label next(this);
const TNode<IntPtrT> length = var_length_.value();
const TNode<IntPtrT> capacity = var_capacity_.value();
GotoIf(WordEqual(length, capacity), &next);
var_array_ = ResizeFixedArray(length, length);
var_capacity_ = length;
Goto(&next);
BIND(&next);
}
const TNode<Smi> result_length = SmiTag(length());
const TNode<JSArray> result =
AllocateJSArray(array_map, var_array_.value(), result_length);
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.
const TNode<IntPtrT> new_capacity =
IntPtrAdd(IntPtrAdd(current_capacity, WordShr(current_capacity, 1)),
IntPtrConstant(16));
return new_capacity;
}
TNode<FixedArray> GrowableFixedArray::ResizeFixedArray(
const TNode<IntPtrT> element_count, const TNode<IntPtrT> new_capacity) {
CSA_ASSERT(this, IntPtrGreaterThanOrEqual(element_count, IntPtrConstant(0)));
CSA_ASSERT(this, IntPtrGreaterThanOrEqual(new_capacity, IntPtrConstant(0)));
CSA_ASSERT(this, IntPtrGreaterThanOrEqual(new_capacity, element_count));
const TNode<FixedArray> from_array = var_array_.value();
CodeStubAssembler::ExtractFixedArrayFlags flags;
flags |= CodeStubAssembler::ExtractFixedArrayFlag::kFixedArrays;
TNode<FixedArray> to_array = CAST(ExtractFixedArray(
from_array, nullptr, element_count, new_capacity, flags));
return to_array;
}
} // namespace internal
} // namespace v8
|