OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_COMPILER_FRAME_H_ | 5 #ifndef V8_COMPILER_FRAME_H_ |
6 #define V8_COMPILER_FRAME_H_ | 6 #define V8_COMPILER_FRAME_H_ |
7 | 7 |
8 #include "src/bit-vector.h" | 8 #include "src/bit-vector.h" |
9 #include "src/frames.h" | 9 #include "src/frames.h" |
10 | 10 |
11 namespace v8 { | 11 namespace v8 { |
12 namespace internal { | 12 namespace internal { |
13 namespace compiler { | 13 namespace compiler { |
14 | 14 |
15 // Collects the spill slot and other frame slot requirements for a compiled | 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 | 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. | 17 // by Linkage to generate code for the prologue and epilogue to compiled code. |
18 // | 18 // |
19 // Frames are divided up into four regions. | 19 // Frames are divided up into three regions. |
20 // - The first is the fixed header, which always has a constant size and can be | 20 // - The first is the fixed header, which always has a constant size and can be |
21 // predicted before code generation begins depending on the type of code being | 21 // predicted before code generation begins depending on the type of code being |
22 // generated. | 22 // generated. |
23 // - The second is the region for spill slots, which is immediately below the | 23 // - The second is the region for spill slots, which is immediately below the |
24 // fixed header and grows as the register allocator needs to spill to the | 24 // fixed header and grows as the register allocator needs to spill to the |
25 // stack and asks the frame for more space. | 25 // stack and asks the frame for more space. |
26 // - The third region, which contains the callee-saved registers must be | 26 // - The third region, which contains the callee-saved registers must be |
27 // reserved after register allocation, since its size can only be precisely | 27 // reserved after register allocation, since its size can only be precisely |
28 // determined after register allocation once the number of used callee-saved | 28 // determined after register allocation once the number of used callee-saved |
29 // register is certain. | 29 // register is certain. |
30 // - The fourth region is used to pass arguments to other functions. It should | |
31 // be empty except when a call is being prepared. | |
32 // | 30 // |
33 // Every pointer in a frame has a slot id. On 32-bit platforms, doubles consume | 31 // Every pointer in a frame has a slot id. On 32-bit platforms, doubles consume |
34 // two slots. | 32 // two slots. |
35 // | 33 // |
36 // Stack slot indices >= 0 access the callee stack with slot 0 corresponding to | 34 // Stack slot indices >= 0 access the callee stack with slot 0 corresponding to |
37 // the callee's saved return address and 1 corresponding to the saved frame | 35 // the callee's saved return address and 1 corresponding to the saved frame |
38 // pointer. Some frames have additional information stored in the fixed header, | 36 // pointer. Some frames have additional information stored in the fixed header, |
39 // for example JSFunctions store the function context and marker in the fixed | 37 // for example JSFunctions store the function context and marker in the fixed |
40 // header, with slot index 2 corresponding to the current function context and 3 | 38 // header, with slot index 2 corresponding to the current function context and 3 |
41 // corresponding to the frame marker/JSFunction. The frame region immediately | 39 // corresponding to the frame marker/JSFunction. The frame region immediately |
(...skipping 29 matching lines...) Expand all Loading... |
71 // 4 | spill 1 | ^ Callee | 69 // 4 | spill 1 | ^ Callee |
72 // |- - - - - - - - -| | frame slots | 70 // |- - - - - - - - -| | frame slots |
73 // ... | ... | Spill slots (slot >= 0) | 71 // ... | ... | Spill slots (slot >= 0) |
74 // |- - - - - - - - -| | | | 72 // |- - - - - - - - -| | | |
75 // m+4 | spill m | v | | 73 // m+4 | spill m | v | |
76 // +-----------------+---- | | 74 // +-----------------+---- | |
77 // m+5 | callee-saved 1 | ^ | | 75 // m+5 | callee-saved 1 | ^ | |
78 // |- - - - - - - - -| | | | 76 // |- - - - - - - - -| | | |
79 // | ... | Callee-saved | | 77 // | ... | Callee-saved | |
80 // |- - - - - - - - -| | | | 78 // |- - - - - - - - -| | | |
81 // m+r+4 | callee-saved r | v | | 79 // m+r+4 | callee-saved r | v v |
82 // +-----------------+---- | | |
83 // | parameter 0 | ^ | | |
84 // |- - - - - - - - -| | | | |
85 // | ... | Outgoing parameters | | |
86 // |- - - - - - - - -| | (for function calls) | | |
87 // | parameter p | v v | |
88 // -----+-----------------+----- <-- stack ptr ------------- | 80 // -----+-----------------+----- <-- stack ptr ------------- |
89 // | 81 // |
90 class Frame : public ZoneObject { | 82 class Frame : public ZoneObject { |
91 public: | 83 public: |
92 explicit Frame(int fixed_frame_size_in_slots); | 84 explicit Frame(int fixed_frame_size_in_slots, bool needs_frame); |
| 85 |
| 86 static int FPOffsetToSlot(int frame_offset) { |
| 87 return StandardFrameConstants::kFixedSlotCountAboveFp - 1 - |
| 88 frame_offset / kPointerSize; |
| 89 } |
| 90 |
| 91 static int SlotToFPOffset(int slot) { |
| 92 return (StandardFrameConstants::kFixedSlotCountAboveFp - 1 - slot) * |
| 93 kPointerSize; |
| 94 } |
| 95 |
| 96 inline bool needs_frame() const { return needs_frame_; } |
| 97 inline void MarkNeedsFrame() { needs_frame_ = true; } |
93 | 98 |
94 inline int GetTotalFrameSlotCount() const { return frame_slot_count_; } | 99 inline int GetTotalFrameSlotCount() const { return frame_slot_count_; } |
95 | 100 |
96 inline int GetSpToFpSlotCount() const { | 101 inline int GetSpToFpSlotCount() const { |
97 return GetTotalFrameSlotCount() - | 102 return GetTotalFrameSlotCount() - |
98 StandardFrameConstants::kFixedSlotCountAboveFp; | 103 StandardFrameConstants::kFixedSlotCountAboveFp; |
99 } | 104 } |
100 inline int GetOutgoingParameterSlotCount() const { | |
101 return outgoing_parameter_slot_count_; | |
102 } | |
103 inline int GetSavedCalleeRegisterSlotCount() const { | 105 inline int GetSavedCalleeRegisterSlotCount() const { |
104 return callee_saved_slot_count_; | 106 return callee_saved_slot_count_; |
105 } | 107 } |
106 inline int GetSpillSlotCount() const { return spill_slot_count_; } | 108 inline int GetSpillSlotCount() const { return spill_slot_count_; } |
107 | 109 |
108 inline void SetElidedFrameSizeInSlots(int slots) { | 110 inline void SetElidedFrameSizeInSlots(int slots) { |
109 DCHECK_EQ(0, callee_saved_slot_count_); | 111 DCHECK_EQ(0, callee_saved_slot_count_); |
110 DCHECK_EQ(0, spill_slot_count_); | 112 DCHECK_EQ(0, spill_slot_count_); |
111 frame_slot_count_ = slots; | 113 frame_slot_count_ = slots; |
112 } | 114 } |
113 | 115 |
114 void SetAllocatedRegisters(BitVector* regs) { | 116 void SetAllocatedRegisters(BitVector* regs) { |
115 DCHECK(allocated_registers_ == NULL); | 117 DCHECK(allocated_registers_ == NULL); |
116 allocated_registers_ = regs; | 118 allocated_registers_ = regs; |
117 } | 119 } |
118 | 120 |
119 void SetAllocatedDoubleRegisters(BitVector* regs) { | 121 void SetAllocatedDoubleRegisters(BitVector* regs) { |
120 DCHECK(allocated_double_registers_ == NULL); | 122 DCHECK(allocated_double_registers_ == NULL); |
121 allocated_double_registers_ = regs; | 123 allocated_double_registers_ = regs; |
122 } | 124 } |
123 | 125 |
124 bool DidAllocateDoubleRegisters() const { | 126 bool DidAllocateDoubleRegisters() const { |
125 return !allocated_double_registers_->IsEmpty(); | 127 return !allocated_double_registers_->IsEmpty(); |
126 } | 128 } |
127 | 129 |
128 void AllocateOutgoingParameterSlots(int count) { | |
129 outgoing_parameter_slot_count_ += count; | |
130 frame_slot_count_ += count; | |
131 } | |
132 | |
133 void ClearOutgoingParameterSlots() { | |
134 frame_slot_count_ -= outgoing_parameter_slot_count_; | |
135 outgoing_parameter_slot_count_ = 0; | |
136 } | |
137 | |
138 int AlignSavedCalleeRegisterSlots() { | 130 int AlignSavedCalleeRegisterSlots() { |
139 DCHECK_EQ(0, callee_saved_slot_count_); | 131 DCHECK_EQ(0, callee_saved_slot_count_); |
| 132 needs_frame_ = true; |
140 int delta = frame_slot_count_ & 1; | 133 int delta = frame_slot_count_ & 1; |
141 frame_slot_count_ += delta; | 134 frame_slot_count_ += delta; |
142 return delta; | 135 return delta; |
143 } | 136 } |
144 | 137 |
145 void AllocateSavedCalleeRegisterSlots(int count) { | 138 void AllocateSavedCalleeRegisterSlots(int count) { |
146 DCHECK_EQ(0, outgoing_parameter_slot_count_); | 139 needs_frame_ = true; |
147 frame_slot_count_ += count; | 140 frame_slot_count_ += count; |
148 callee_saved_slot_count_ += count; | 141 callee_saved_slot_count_ += count; |
149 } | 142 } |
150 | 143 |
151 int AllocateSpillSlot(int width) { | 144 int AllocateSpillSlot(int width) { |
152 DCHECK_EQ(0, outgoing_parameter_slot_count_); | |
153 DCHECK_EQ(0, callee_saved_slot_count_); | 145 DCHECK_EQ(0, callee_saved_slot_count_); |
| 146 needs_frame_ = true; |
154 int frame_slot_count_before = frame_slot_count_; | 147 int frame_slot_count_before = frame_slot_count_; |
155 int slot = AllocateAlignedFrameSlot(width); | 148 int slot = AllocateAlignedFrameSlot(width); |
156 spill_slot_count_ += (frame_slot_count_ - frame_slot_count_before); | 149 spill_slot_count_ += (frame_slot_count_ - frame_slot_count_before); |
157 return slot; | 150 return slot; |
158 } | 151 } |
159 | 152 |
160 int ReserveSpillSlots(size_t slot_count) { | 153 int ReserveSpillSlots(size_t slot_count) { |
161 DCHECK_EQ(0, outgoing_parameter_slot_count_); | |
162 DCHECK_EQ(0, callee_saved_slot_count_); | 154 DCHECK_EQ(0, callee_saved_slot_count_); |
163 DCHECK_EQ(0, spill_slot_count_); | 155 DCHECK_EQ(0, spill_slot_count_); |
| 156 needs_frame_ = true; |
164 spill_slot_count_ += static_cast<int>(slot_count); | 157 spill_slot_count_ += static_cast<int>(slot_count); |
165 frame_slot_count_ += static_cast<int>(slot_count); | 158 frame_slot_count_ += static_cast<int>(slot_count); |
166 return frame_slot_count_ - 1; | 159 return frame_slot_count_ - 1; |
167 } | 160 } |
168 | 161 |
169 static const int kContextSlot = 2 + StandardFrameConstants::kCPSlotCount; | 162 static const int kContextSlot = 2 + StandardFrameConstants::kCPSlotCount; |
170 static const int kJSFunctionSlot = 3 + StandardFrameConstants::kCPSlotCount; | 163 static const int kJSFunctionSlot = 3 + StandardFrameConstants::kCPSlotCount; |
171 | 164 |
172 private: | 165 private: |
173 int AllocateAlignedFrameSlot(int width) { | 166 int AllocateAlignedFrameSlot(int width) { |
174 DCHECK(width == 4 || width == 8); | 167 DCHECK(width == 4 || width == 8); |
175 // Skip one slot if necessary. | 168 // Skip one slot if necessary. |
176 if (width > kPointerSize) { | 169 if (width > kPointerSize) { |
177 DCHECK(width == kPointerSize * 2); | 170 DCHECK(width == kPointerSize * 2); |
178 frame_slot_count_++; | 171 frame_slot_count_++; |
179 frame_slot_count_ |= 1; | 172 frame_slot_count_ |= 1; |
180 } | 173 } |
181 return frame_slot_count_++; | 174 return frame_slot_count_++; |
182 } | 175 } |
183 | 176 |
184 private: | 177 private: |
| 178 bool needs_frame_; |
185 int frame_slot_count_; | 179 int frame_slot_count_; |
186 int outgoing_parameter_slot_count_; | |
187 int callee_saved_slot_count_; | 180 int callee_saved_slot_count_; |
188 int spill_slot_count_; | 181 int spill_slot_count_; |
189 BitVector* allocated_registers_; | 182 BitVector* allocated_registers_; |
190 BitVector* allocated_double_registers_; | 183 BitVector* allocated_double_registers_; |
191 | 184 |
192 DISALLOW_COPY_AND_ASSIGN(Frame); | 185 DISALLOW_COPY_AND_ASSIGN(Frame); |
193 }; | 186 }; |
194 | 187 |
195 | 188 |
196 // Represents an offset from either the stack pointer or frame pointer. | 189 // Represents an offset from either the stack pointer or frame pointer. |
(...skipping 19 matching lines...) Expand all Loading... |
216 int offset_; // Encodes SP or FP in the low order bit. | 209 int offset_; // Encodes SP or FP in the low order bit. |
217 | 210 |
218 static const int kFromSp = 1; | 211 static const int kFromSp = 1; |
219 static const int kFromFp = 0; | 212 static const int kFromFp = 0; |
220 }; | 213 }; |
221 } // namespace compiler | 214 } // namespace compiler |
222 } // namespace internal | 215 } // namespace internal |
223 } // namespace v8 | 216 } // namespace v8 |
224 | 217 |
225 #endif // V8_COMPILER_FRAME_H_ | 218 #endif // V8_COMPILER_FRAME_H_ |
OLD | NEW |