v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
frame.h
Go to the documentation of this file.
1// Copyright 2014 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_FRAME_H_
6#define V8_COMPILER_FRAME_H_
7
8#include "src/base/bits.h"
12
13namespace v8 {
14namespace internal {
15namespace compiler {
16
17class CallDescriptor;
18
19// Collects the spill slot and other frame slot requirements for a compiled
20// function. Frames are usually populated by the register allocator and are used
21// by Linkage to generate code for the prologue and epilogue to compiled
22// code. Frame objects must be considered immutable once they've been
23// instantiated and the basic information about the frame has been collected
24// into them. Mutable state associated with the frame is stored separately in
25// FrameAccessState.
26//
27// Frames are divided up into four regions.
28// - The first is the fixed header, which always has a constant size and can be
29// predicted before code generation begins depending on the type of code being
30// generated.
31// - The second is the region for spill slots, which is immediately below the
32// fixed header and grows as the register allocator needs to spill to the
33// stack and asks the frame for more space.
34// - The third region, which contains the callee-saved registers must be
35// reserved after register allocation, since its size can only be precisely
36// determined after register allocation once the number of used callee-saved
37// register is certain.
38// - The fourth region is a scratch area for return values from other functions
39// called, if multiple returns cannot all be passed in registers. This region
40// Must be last in a stack frame, so that it is positioned immediately below
41// the stack frame of a callee to store to.
42//
43// The frame region immediately below the fixed header contains spill slots
44// starting at slot 4 for JSFunctions. The callee-saved frame region below that
45// starts at 4+spill_slot_count_. Callee stack slots correspond to
46// parameters that are accessible through negative slot ids.
47//
48// Every slot of a caller or callee frame is accessible by the register
49// allocator and gap resolver with a SpillSlotOperand containing its
50// corresponding slot id.
51//
52// Below an example JSFunction Frame with slot ids, frame regions and contents:
53//
54// slot JS frame
55// +-----------------+--------------------------------
56// -n-1 | parameter n | ^
57// |- - - - - - - - -| |
58// -n | parameter n-1 | Caller
59// ... | ... | frame slots
60// -2 | parameter 1 | (slot < 0)
61// |- - - - - - - - -| |
62// -1 | parameter 0 | v
63// -----+-----------------+--------------------------------
64// 0 | return addr | ^ ^
65// |- - - - - - - - -| | |
66// 1 | saved frame ptr | Fixed |
67// |- - - - - - - - -| Header <-- frame ptr |
68// 2 |Context/Frm. Type| | |
69// |- - - - - - - - -| | |
70// 3 | [JSFunction] | v |
71// +-----------------+---- |
72// 4 | spill 1 | ^ Callee
73// |- - - - - - - - -| | frame slots
74// ... | ... | Spill slots (slot >= 0)
75// |- - - - - - - - -| | |
76// m+3 | spill m | v |
77// +-----------------+---- |
78// m+4 | callee-saved 1 | ^ |
79// |- - - - - - - - -| | |
80// | ... | Callee-saved |
81// |- - - - - - - - -| | |
82// m+r+3 | callee-saved r | v |
83// +-----------------+---- |
84// m+r+4 | return 0 | ^ |
85// |- - - - - - - - -| | |
86// | ... | Return |
87// |- - - - - - - - -| | |
88// | return q-1 | v v
89// -----+-----------------+----- <-- stack ptr -------------
90//
92 public:
93 explicit Frame(int fixed_frame_size_in_slots, Zone* zone);
94 Frame(const Frame&) = delete;
95 Frame& operator=(const Frame&) = delete;
96
97 inline int GetTotalFrameSlotCount() const {
98 return slot_allocator_.Size() + return_slot_count_;
99 }
100 inline int GetFixedSlotCount() const { return fixed_slot_count_; }
101 inline int GetSpillSlotCount() const { return spill_slot_count_; }
102 inline int GetReturnSlotCount() const { return return_slot_count_; }
103
108
110 DCHECK_NULL(allocated_double_registers_);
111 allocated_double_registers_ = regs;
112 }
113
115 return !allocated_double_registers_->IsEmpty();
116 }
117
118 void AlignSavedCalleeRegisterSlots(int alignment = kDoubleSize) {
119 DCHECK(!frame_aligned_);
120#if DEBUG
121 spill_slots_finished_ = true;
122#endif
123 DCHECK(base::bits::IsPowerOfTwo(alignment));
124 DCHECK_LE(alignment, kSimd128Size);
125 int alignment_in_slots = AlignedSlotAllocator::NumSlotsForWidth(alignment);
126 int padding = slot_allocator_.Align(alignment_in_slots);
127 spill_slot_count_ += padding;
128 }
129
131 DCHECK(!frame_aligned_);
132#if DEBUG
133 spill_slots_finished_ = true;
134#endif
135 slot_allocator_.AllocateUnaligned(count);
136 }
137
138 int AllocateSpillSlot(int width, int alignment = 0, bool is_tagged = false) {
139 DCHECK_EQ(GetTotalFrameSlotCount(),
140 fixed_slot_count_ + spill_slot_count_ + return_slot_count_);
141 DCHECK_IMPLIES(is_tagged, width == sizeof(uintptr_t));
142 DCHECK_IMPLIES(is_tagged, alignment == sizeof(uintptr_t));
143 // Never allocate spill slots after the callee-saved slots are defined.
144 DCHECK(!spill_slots_finished_);
145 DCHECK(!frame_aligned_);
146 int actual_width = std::max({width, AlignedSlotAllocator::kSlotSize});
147 int actual_alignment =
148 std::max({alignment, AlignedSlotAllocator::kSlotSize});
149 int slots = AlignedSlotAllocator::NumSlotsForWidth(actual_width);
150 int old_end = slot_allocator_.Size();
151 int slot;
152 if (actual_width == actual_alignment) {
153 // Simple allocation, alignment equal to width.
154 slot = slot_allocator_.Allocate(slots);
155 } else {
156 // Complex allocation, alignment different from width.
157 if (actual_alignment > AlignedSlotAllocator::kSlotSize) {
158 // Alignment required.
159 int alignment_in_slots =
160 AlignedSlotAllocator::NumSlotsForWidth(actual_alignment);
161 slot_allocator_.Align(alignment_in_slots);
162 }
163 slot = slot_allocator_.AllocateUnaligned(slots);
164 }
165 int end = slot_allocator_.Size();
166
167 spill_slot_count_ += end - old_end;
168 int result_slot = slot + slots - 1;
169 if (is_tagged) tagged_slots_bits_.Add(result_slot, zone_);
170 return result_slot;
171 }
172
174 DCHECK(!frame_aligned_);
175 return_slot_count_ = std::max(return_slot_count_, count);
176 }
177
178 void AlignFrame(int alignment = kDoubleSize);
179
180 int ReserveSpillSlots(size_t slot_count) {
181 DCHECK_EQ(0, spill_slot_count_);
182 DCHECK(!frame_aligned_);
183 spill_slot_count_ += static_cast<int>(slot_count);
184 slot_allocator_.AllocateUnaligned(static_cast<int>(slot_count));
185 return slot_allocator_.Size() - 1;
186 }
187
188 const GrowableBitVector& tagged_slots() const { return tagged_slots_bits_; }
189
190 private:
192 int spill_slot_count_ = 0;
193 // Account for return slots separately. Conceptually, they follow all
194 // allocated spill slots.
195 int return_slot_count_ = 0;
201#if DEBUG
202 bool spill_slots_finished_ = false;
203 bool frame_aligned_ = false;
204#endif
205};
206
207// Represents an offset from either the stack pointer or frame pointer.
209 public:
210 inline bool from_stack_pointer() { return (offset_ & 1) == kFromSp; }
211 inline bool from_frame_pointer() { return (offset_ & 1) == kFromFp; }
212 inline int offset() { return offset_ & ~1; }
213
215 DCHECK_EQ(0, offset & 1);
216 return FrameOffset(offset | kFromSp);
217 }
218
220 DCHECK_EQ(0, offset & 1);
221 return FrameOffset(offset | kFromFp);
222 }
223
224 private:
225 explicit FrameOffset(int offset) : offset_(offset) {}
226
227 int offset_; // Encodes SP or FP in the low order bit.
228
229 static const int kFromSp = 1;
230 static const int kFromFp = 0;
231};
232
233// Encapsulates the mutable state maintained during code generation about the
234// current function's frame.
236 public:
237 explicit FrameAccessState(const Frame* const frame)
238 : frame_(frame),
240 fp_relative_only_(false),
241 sp_delta_(0),
242 has_frame_(false) {}
243
244 const Frame* frame() const { return frame_; }
245 V8_EXPORT_PRIVATE void MarkHasFrame(bool state);
246 void SetFPRelativeOnly(bool state);
248
249 int sp_delta() const { return sp_delta_; }
250 void ClearSPDelta() { sp_delta_ = 0; }
251 void IncreaseSPDelta(int amount) { sp_delta_ += amount; }
252
254
255 // Regardless of how we access slots on the stack - using sp or fp - do we
256 // have a frame, at the current stage in code generation.
257 bool has_frame() const { return has_frame_; }
258
262
263 int GetSPToFPSlotCount() const {
264 int frame_slot_count =
265 (has_frame() ? frame()->GetTotalFrameSlotCount() : kElidedFrameSlots) -
267 return frame_slot_count + sp_delta();
268 }
269 int GetSPToFPOffset() const {
271 }
272
273 // Get the frame offset for a given spill slot. The location depends on the
274 // calling convention and the specific frame layout, and may thus be
275 // architecture-specific. Negative spill slots indicate arguments on the
276 // caller's frame.
277 FrameOffset GetFrameOffset(int spill_slot) const;
278
279 private:
280 const Frame* const frame_;
285};
286} // namespace compiler
287} // namespace internal
288} // namespace v8
289
290#endif // V8_COMPILER_FRAME_H_
std::vector< Register * > allocated_registers_
static constexpr int kFixedSlotCountAboveFp
V8_EXPORT_PRIVATE void MarkHasFrame(bool state)
Definition frame.cc:44
FrameOffset GetFrameOffset(int spill_slot) const
Definition frame.cc:61
FrameAccessState(const Frame *const frame)
Definition frame.h:237
static FrameOffset FromFramePointer(int offset)
Definition frame.h:219
static FrameOffset FromStackPointer(int offset)
Definition frame.h:214
void SetAllocatedRegisters(BitVector *regs)
Definition frame.h:104
Frame(const Frame &)=delete
GrowableBitVector tagged_slots_bits_
Definition frame.h:200
int GetTotalFrameSlotCount() const
Definition frame.h:97
int ReserveSpillSlots(size_t slot_count)
Definition frame.h:180
int GetSpillSlotCount() const
Definition frame.h:101
void EnsureReturnSlots(int count)
Definition frame.h:173
int GetReturnSlotCount() const
Definition frame.h:102
void AllocateSavedCalleeRegisterSlots(int count)
Definition frame.h:130
const GrowableBitVector & tagged_slots() const
Definition frame.h:188
Frame & operator=(const Frame &)=delete
int AllocateSpillSlot(int width, int alignment=0, bool is_tagged=false)
Definition frame.h:138
void SetAllocatedDoubleRegisters(BitVector *regs)
Definition frame.h:109
BitVector * allocated_registers_
Definition frame.h:197
BitVector * allocated_double_registers_
Definition frame.h:198
int GetFixedSlotCount() const
Definition frame.h:100
AlignedSlotAllocator slot_allocator_
Definition frame.h:196
void AlignSavedCalleeRegisterSlots(int alignment=kDoubleSize)
Definition frame.h:118
bool DidAllocateDoubleRegisters() const
Definition frame.h:114
Zone * zone_
int end
constexpr int kSimd128Size
Definition globals.h:706
constexpr int kSystemPointerSize
Definition globals.h:410
constexpr int kElidedFrameSlots
Definition globals.h:418
#define DCHECK_LE(v1, v2)
Definition logging.h:490
#define DCHECK_NULL(val)
Definition logging.h:491
#define DCHECK_IMPLIES(v1, v2)
Definition logging.h:493
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define V8_EXPORT_PRIVATE
Definition macros.h:460