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.
5 #ifndef V8_COMPILER_FRAME_H_
6 #define V8_COMPILER_FRAME_H_
8 #include "src/bit-vector.h"
9 #include "src/frames.h"
15 // Collects the spill slot and other frame slot requirements for a compiled
16 // function. Frames are usually populated by the register allocator and are used
17 // by Linkage to generate code for the prologue and epilogue to compiled code.
19 // Frames are divided up into three regions. The first is the fixed header,
20 // which always has a constant size and can be predicted before code generation
21 // begins depending on the type of code being generated. The second is the
22 // region for spill slots, which is immediately below the fixed header and grows
23 // as the register allocator needs to spill to the stack and asks the frame for
24 // more space. The third region, which contains the callee-saved registers must
25 // be reserved after register allocation, since its size can only be precisely
26 // determined after register allocation once the number of used callee-saved
27 // register is certain.
29 // Every pointer in a frame has a slot id. On 32-bit platforms, doubles consume
32 // Stack slot indices >= 0 access the callee stack with slot 0 corresponding to
33 // the callee's saved return address and 1 corresponding to the saved frame
34 // pointer. Some frames have additional information stored in the fixed header,
35 // for example JSFunctions store the function context and marker in the fixed
36 // header, with slot index 2 corresponding to the current function context and 3
37 // corresponding to the frame marker/JSFunction. The frame region immediately
38 // below the fixed header contains spill slots starting a 4 for JsFunctions. The
39 // callee-saved frame region below that starts at 4+spilled_slot_count. Callee
40 // stack slots corresponding to parameters are accessible through negative slot
43 // Every slot of a caller or callee frame is accessible by the register
44 // allocator and gap resolver with a SpillSlotOperand containing its
45 // corresponding slot id.
47 // Below an example JSFunction Frame with slot ids, frame regions and contents:
50 // +-----------------+----------------------------
51 // -n-1 | parameter 0 | ^
52 // |- - - - - - - - -| |
54 // ... | ... | frame slots
55 // -2 | parameter n-1 | (slot < 0)
56 // |- - - - - - - - -| |
57 // -1 | parameter n | v
58 // -----+-----------------+----------------------------
59 // 0 | return addr | ^ ^
60 // |- - - - - - - - -| | |
61 // 1 | saved frame ptr | Fixed |
62 // |- - - - - - - - -| Header <-- frame ptr |
64 // |- - - - - - - - -| | |
65 // 3 |JSFunction/Marker| v |
66 // +-----------------+---- |
67 // 4 | spill 1 | ^ Callee
68 // |- - - - - - - - -| | frame slots
69 // ... | ... | Spill slots (slot >= 0)
70 // |- - - - - - - - -| | |
71 // m+4 | spill m | v |
72 // +-----------------+---- |
73 // m+5 | callee-saved 1 | ^ |
74 // |- - - - - - - - -| | |
75 // | ... | Callee-saved |
76 // |- - - - - - - - -| | |
77 // m+r+4 | callee-saved r | v v
78 // -----+-----------------+----- <-- stack ptr ---------
80 class Frame : public ZoneObject {
82 explicit Frame(int fixed_frame_size_in_slots);
84 inline int GetTotalFrameSlotCount() { return frame_slot_count_; }
86 inline int GetSavedCalleeRegisterSlotCount() {
87 return spilled_callee_register_slot_count_;
89 inline int GetSpillSlotCount() { return stack_slot_count_; }
91 inline void SetElidedFrameSizeInSlots(int slots) {
92 DCHECK_EQ(0, spilled_callee_register_slot_count_);
93 DCHECK_EQ(0, stack_slot_count_);
94 frame_slot_count_ = slots;
97 void SetAllocatedRegisters(BitVector* regs) {
98 DCHECK(allocated_registers_ == NULL);
99 allocated_registers_ = regs;
102 void SetAllocatedDoubleRegisters(BitVector* regs) {
103 DCHECK(allocated_double_registers_ == NULL);
104 allocated_double_registers_ = regs;
107 bool DidAllocateDoubleRegisters() {
108 return !allocated_double_registers_->IsEmpty();
111 int AlignSavedCalleeRegisterSlots() {
112 DCHECK_EQ(0, spilled_callee_register_slot_count_);
113 int delta = frame_slot_count_ & 1;
114 frame_slot_count_ += delta;
118 void AllocateSavedCalleeRegisterSlots(int count) {
119 frame_slot_count_ += count;
120 spilled_callee_register_slot_count_ += count;
123 int AllocateSpillSlot(int width) {
124 DCHECK_EQ(0, spilled_callee_register_slot_count_);
125 int frame_slot_count_before = frame_slot_count_;
126 int slot = AllocateAlignedFrameSlot(width);
127 stack_slot_count_ += (frame_slot_count_ - frame_slot_count_before);
131 int ReserveSpillSlots(size_t slot_count) {
132 DCHECK_EQ(0, spilled_callee_register_slot_count_);
133 DCHECK_EQ(0, stack_slot_count_);
134 stack_slot_count_ += static_cast<int>(slot_count);
135 frame_slot_count_ += static_cast<int>(slot_count);
136 return frame_slot_count_ - 1;
140 int AllocateAlignedFrameSlot(int width) {
141 DCHECK(width == 4 || width == 8);
142 // Skip one slot if necessary.
143 if (width > kPointerSize) {
144 DCHECK(width == kPointerSize * 2);
146 frame_slot_count_ |= 1;
148 return frame_slot_count_++;
152 int frame_slot_count_;
153 int spilled_callee_register_slot_count_;
154 int stack_slot_count_;
155 BitVector* allocated_registers_;
156 BitVector* allocated_double_registers_;
158 DISALLOW_COPY_AND_ASSIGN(Frame);
162 // Represents an offset from either the stack pointer or frame pointer.
165 inline bool from_stack_pointer() { return (offset_ & 1) == kFromSp; }
166 inline bool from_frame_pointer() { return (offset_ & 1) == kFromFp; }
167 inline int offset() { return offset_ & ~1; }
169 inline static FrameOffset FromStackPointer(int offset) {
170 DCHECK((offset & 1) == 0);
171 return FrameOffset(offset | kFromSp);
174 inline static FrameOffset FromFramePointer(int offset) {
175 DCHECK((offset & 1) == 0);
176 return FrameOffset(offset | kFromFp);
180 explicit FrameOffset(int offset) : offset_(offset) {}
182 int offset_; // Encodes SP or FP in the low order bit.
184 static const int kFromSp = 1;
185 static const int kFromFp = 0;
189 } // namespace v8::internal::compiler
191 #endif // V8_COMPILER_FRAME_H_