Line data Source code
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/bit-vector.h"
9 : #include "src/frame-constants.h"
10 :
11 : namespace v8 {
12 : namespace internal {
13 : namespace compiler {
14 :
15 : class CallDescriptor;
16 :
17 : // Collects the spill slot and other frame slot requirements for a compiled
18 : // function. Frames are usually populated by the register allocator and are used
19 : // by Linkage to generate code for the prologue and epilogue to compiled
20 : // code. Frame objects must be considered immutable once they've been
21 : // instantiated and the basic information about the frame has been collected
22 : // into them. Mutable state associated with the frame is stored separately in
23 : // FrameAccessState.
24 : //
25 : // Frames are divided up into three regions.
26 : // - The first is the fixed header, which always has a constant size and can be
27 : // predicted before code generation begins depending on the type of code being
28 : // generated.
29 : // - The second is the region for spill slots, which is immediately below the
30 : // fixed header and grows as the register allocator needs to spill to the
31 : // stack and asks the frame for more space.
32 : // - The third region, which contains the callee-saved registers must be
33 : // reserved after register allocation, since its size can only be precisely
34 : // determined after register allocation once the number of used callee-saved
35 : // register is certain.
36 : //
37 : // The frame region immediately below the fixed header contains spill slots
38 : // starting at slot 4 for JSFunctions. The callee-saved frame region below that
39 : // starts at 4+spill_slot_count_. Callee stack slots corresponding to
40 : // parameters are accessible through negative slot ids.
41 : //
42 : // Every slot of a caller or callee frame is accessible by the register
43 : // allocator and gap resolver with a SpillSlotOperand containing its
44 : // corresponding slot id.
45 : //
46 : // Below an example JSFunction Frame with slot ids, frame regions and contents:
47 : //
48 : // slot JS frame
49 : // +-----------------+--------------------------------
50 : // -n-1 | parameter 0 | ^
51 : // |- - - - - - - - -| |
52 : // -n | | Caller
53 : // ... | ... | frame slots
54 : // -2 | parameter n-1 | (slot < 0)
55 : // |- - - - - - - - -| |
56 : // -1 | parameter n | v
57 : // -----+-----------------+--------------------------------
58 : // 0 | return addr | ^ ^
59 : // |- - - - - - - - -| | |
60 : // 1 | saved frame ptr | Fixed |
61 : // |- - - - - - - - -| Header <-- frame ptr |
62 : // 2 |Context/Frm. Type| | |
63 : // |- - - - - - - - -| | |
64 : // 3 | [JSFunction] | v |
65 : // +-----------------+---- |
66 : // 4 | spill 1 | ^ Callee
67 : // |- - - - - - - - -| | frame slots
68 : // ... | ... | Spill slots (slot >= 0)
69 : // |- - - - - - - - -| | |
70 : // m+3 | spill m | v |
71 : // +-----------------+---- |
72 : // m+4 | callee-saved 1 | ^ |
73 : // |- - - - - - - - -| | |
74 : // | ... | Callee-saved |
75 : // |- - - - - - - - -| | |
76 : // m+r+3 | callee-saved r | v v
77 : // -----+-----------------+----- <-- stack ptr -------------
78 : //
79 : class Frame : public ZoneObject {
80 : public:
81 : explicit Frame(int fixed_frame_size_in_slots);
82 :
83 : inline int GetTotalFrameSlotCount() const { return frame_slot_count_; }
84 :
85 : inline int GetSpillSlotCount() const { return spill_slot_count_; }
86 :
87 : void SetAllocatedRegisters(BitVector* regs) {
88 : DCHECK_NULL(allocated_registers_);
89 1301577 : allocated_registers_ = regs;
90 : }
91 :
92 : void SetAllocatedDoubleRegisters(BitVector* regs) {
93 : DCHECK_NULL(allocated_double_registers_);
94 1301577 : allocated_double_registers_ = regs;
95 : }
96 :
97 : bool DidAllocateDoubleRegisters() const {
98 : return !allocated_double_registers_->IsEmpty();
99 : }
100 :
101 : void AlignSavedCalleeRegisterSlots(int alignment = kDoubleSize) {
102 : int alignment_slots = alignment / kPointerSize;
103 : int delta = alignment_slots - (frame_slot_count_ & (alignment_slots - 1));
104 : if (delta != alignment_slots) {
105 : frame_slot_count_ += delta;
106 : }
107 0 : spill_slot_count_ += delta;
108 : }
109 :
110 : void AllocateSavedCalleeRegisterSlots(int count) {
111 228907 : frame_slot_count_ += count;
112 : }
113 :
114 1166113 : int AllocateSpillSlot(int width, int alignment = 0) {
115 1166113 : int frame_slot_count_before = frame_slot_count_;
116 1166113 : if (alignment <= kPointerSize) {
117 : AllocateAlignedFrameSlots(width);
118 : } else {
119 : // We need to allocate more place for spill slot
120 : // in case we need an aligned spill slot to be
121 : // able to properly align start of spill slot
122 : // and still have enough place to hold all the
123 : // data
124 0 : AllocateAlignedFrameSlots(width + alignment - kPointerSize);
125 : }
126 1166113 : spill_slot_count_ += frame_slot_count_ - frame_slot_count_before;
127 1166113 : return frame_slot_count_ - 1;
128 : }
129 :
130 : int AlignFrame(int alignment = kDoubleSize);
131 :
132 : int ReserveSpillSlots(size_t slot_count) {
133 : DCHECK_EQ(0, spill_slot_count_);
134 5809 : spill_slot_count_ += static_cast<int>(slot_count);
135 5809 : frame_slot_count_ += static_cast<int>(slot_count);
136 : return frame_slot_count_ - 1;
137 : }
138 :
139 : static const int kContextSlot = 2 + StandardFrameConstants::kCPSlotCount;
140 : static const int kJSFunctionSlot = 3 + StandardFrameConstants::kCPSlotCount;
141 :
142 : private:
143 : void AllocateAlignedFrameSlots(int width) {
144 : DCHECK_LT(0, width);
145 1166113 : int new_frame_slots = (width + kPointerSize - 1) / kPointerSize;
146 : // Align to 8 bytes if width is a multiple of 8 bytes, and to 16 bytes if
147 : // multiple of 16.
148 1166113 : int align_to = (width & 15) == 0 ? 16 : (width & 7) == 0 ? 8 : kPointerSize;
149 : frame_slot_count_ =
150 2332226 : RoundUp(frame_slot_count_ + new_frame_slots, align_to / kPointerSize);
151 : DCHECK_LT(0, frame_slot_count_);
152 : }
153 :
154 : private:
155 : int frame_slot_count_;
156 : int spill_slot_count_;
157 : BitVector* allocated_registers_;
158 : BitVector* allocated_double_registers_;
159 :
160 : DISALLOW_COPY_AND_ASSIGN(Frame);
161 : };
162 :
163 :
164 : // Represents an offset from either the stack pointer or frame pointer.
165 : class FrameOffset {
166 : public:
167 10687157 : inline bool from_stack_pointer() { return (offset_ & 1) == kFromSp; }
168 : inline bool from_frame_pointer() { return (offset_ & 1) == kFromFp; }
169 10687157 : inline int offset() { return offset_ & ~1; }
170 :
171 : inline static FrameOffset FromStackPointer(int offset) {
172 : DCHECK_EQ(0, offset & 1);
173 78919 : return FrameOffset(offset | kFromSp);
174 : }
175 :
176 : inline static FrameOffset FromFramePointer(int offset) {
177 : DCHECK_EQ(0, offset & 1);
178 : return FrameOffset(offset | kFromFp);
179 : }
180 :
181 : private:
182 : explicit FrameOffset(int offset) : offset_(offset) {}
183 :
184 : int offset_; // Encodes SP or FP in the low order bit.
185 :
186 : static const int kFromSp = 1;
187 : static const int kFromFp = 0;
188 : };
189 :
190 : // Encapsulates the mutable state maintained during code generation about the
191 : // current function's frame.
192 : class FrameAccessState : public ZoneObject {
193 : public:
194 : explicit FrameAccessState(const Frame* const frame)
195 : : frame_(frame),
196 : access_frame_with_fp_(false),
197 : sp_delta_(0),
198 1301552 : has_frame_(false) {}
199 :
200 : const Frame* frame() const { return frame_; }
201 : void MarkHasFrame(bool state);
202 :
203 : int sp_delta() const { return sp_delta_; }
204 4770704 : void ClearSPDelta() { sp_delta_ = 0; }
205 3766047 : void IncreaseSPDelta(int amount) { sp_delta_ += amount; }
206 :
207 : bool access_frame_with_fp() const { return access_frame_with_fp_; }
208 :
209 : // Regardless of how we access slots on the stack - using sp or fp - do we
210 : // have a frame, at the current stage in code generation.
211 : bool has_frame() const { return has_frame_; }
212 :
213 : void SetFrameAccessToDefault();
214 10804643 : void SetFrameAccessToFP() { access_frame_with_fp_ = true; }
215 2695614 : void SetFrameAccessToSP() { access_frame_with_fp_ = false; }
216 :
217 1077759 : int GetSPToFPSlotCount() const {
218 : int frame_slot_count =
219 585536 : (has_frame() ? frame()->GetTotalFrameSlotCount() : kElidedFrameSlots) -
220 : StandardFrameConstants::kFixedSlotCountAboveFp;
221 492223 : return frame_slot_count + sp_delta();
222 : }
223 78919 : int GetSPToFPOffset() const { return GetSPToFPSlotCount() * kPointerSize; }
224 :
225 : // Get the frame offset for a given spill slot. The location depends on the
226 : // calling convention and the specific frame layout, and may thus be
227 : // architecture-specific. Negative spill slots indicate arguments on the
228 : // caller's frame.
229 : FrameOffset GetFrameOffset(int spill_slot) const;
230 :
231 : private:
232 : const Frame* const frame_;
233 : bool access_frame_with_fp_;
234 : int sp_delta_;
235 : bool has_frame_;
236 : };
237 : } // namespace compiler
238 : } // namespace internal
239 : } // namespace v8
240 :
241 : #endif // V8_COMPILER_FRAME_H_
|