1// Copyright 2018 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_COMPILER_ALLOCATION_BUILDER_INL_H_
6#define V8_COMPILER_ALLOCATION_BUILDER_INL_H_
7
8#include "src/compiler/access-builder.h"
9#include "src/compiler/allocation-builder.h"
10#include "src/heap/heap-inl.h"
11#include "src/objects/arguments-inl.h"
12#include "src/objects/map-inl.h"
13
14namespace v8 {
15namespace internal {
16namespace compiler {
17
18void AllocationBuilder::Allocate(int size, AllocationType allocation,
19                                 Type type) {
20  CHECK_GT(size, 0);
21  DCHECK_LE(size, isolate()->heap()->MaxRegularHeapObjectSize(allocation));
22  effect_ = graph()->NewNode(
23      common()->BeginRegion(RegionObservability::kNotObservable), effect_);
24  allocation_ = graph()->NewNode(simplified()->Allocate(type, allocation),
25                                 jsgraph()->Constant(size), effect_, control_);
26  effect_ = allocation_;
27}
28
29void AllocationBuilder::AllocateContext(int variadic_part_length, MapRef map) {
30  DCHECK(base::IsInRange(map.instance_type(), FIRST_CONTEXT_TYPE,
31                         LAST_CONTEXT_TYPE));
32  DCHECK_NE(NATIVE_CONTEXT_TYPE, map.instance_type());
33  int size = Context::SizeFor(variadic_part_length);
34  Allocate(size, AllocationType::kYoung, Type::OtherInternal());
35  Store(AccessBuilder::ForMap(), map);
36  STATIC_ASSERT(static_cast<int>(Context::kLengthOffset) ==
37                static_cast<int>(FixedArray::kLengthOffset));
38  Store(AccessBuilder::ForFixedArrayLength(),
39        jsgraph()->Constant(variadic_part_length));
40}
41
42bool AllocationBuilder::CanAllocateArray(int length, MapRef map,
43                                         AllocationType allocation) {
44  DCHECK(map.instance_type() == FIXED_ARRAY_TYPE ||
45         map.instance_type() == FIXED_DOUBLE_ARRAY_TYPE);
46  int const size = (map.instance_type() == FIXED_ARRAY_TYPE)
47                       ? FixedArray::SizeFor(length)
48                       : FixedDoubleArray::SizeFor(length);
49  return size <= isolate()->heap()->MaxRegularHeapObjectSize(allocation);
50}
51
52// Compound allocation of a FixedArray.
53void AllocationBuilder::AllocateArray(int length, MapRef map,
54                                      AllocationType allocation) {
55  DCHECK(CanAllocateArray(length, map, allocation));
56  int size = (map.instance_type() == FIXED_ARRAY_TYPE)
57                 ? FixedArray::SizeFor(length)
58                 : FixedDoubleArray::SizeFor(length);
59  Allocate(size, allocation, Type::OtherInternal());
60  Store(AccessBuilder::ForMap(), map);
61  Store(AccessBuilder::ForFixedArrayLength(), jsgraph()->Constant(length));
62}
63
64bool AllocationBuilder::CanAllocateSloppyArgumentElements(
65    int length, MapRef map, AllocationType allocation) {
66  int const size = SloppyArgumentsElements::SizeFor(length);
67  return size <= isolate()->heap()->MaxRegularHeapObjectSize(allocation);
68}
69
70void AllocationBuilder::AllocateSloppyArgumentElements(
71    int length, MapRef map, AllocationType allocation) {
72  DCHECK(CanAllocateSloppyArgumentElements(length, map, allocation));
73  int size = SloppyArgumentsElements::SizeFor(length);
74  Allocate(size, allocation, Type::OtherInternal());
75  Store(AccessBuilder::ForMap(), map);
76  Store(AccessBuilder::ForFixedArrayLength(), jsgraph()->Constant(length));
77}
78
79}  // namespace compiler
80}  // namespace internal
81}  // namespace v8
82
83#endif  // V8_COMPILER_ALLOCATION_BUILDER_INL_H_
84