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 three regions. The first is the fixed header, | 19 // Frames are divided up into four regions. |
20 // which always has a constant size and can be predicted before code generation | 20 // - The first is the fixed header, which always has a constant size and can be |
21 // begins depending on the type of code being generated. The second is the | 21 // predicted before code generation begins depending on the type of code being |
22 // region for spill slots, which is immediately below the fixed header and grows | 22 // generated. |
23 // as the register allocator needs to spill to the stack and asks the frame for | 23 // - The second is the region for spill slots, which is immediately below the |
24 // more space. The third region, which contains the callee-saved registers must | 24 // fixed header and grows as the register allocator needs to spill to the |
25 // be reserved after register allocation, since its size can only be precisely | 25 // stack and asks the frame for more space. |
26 // determined after register allocation once the number of used callee-saved | 26 // - The third region, which contains the callee-saved registers must be |
27 // register is certain. | 27 // reserved after register allocation, since its size can only be precisely |
| 28 // determined after register allocation once the number of used callee-saved |
| 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. |
28 // | 32 // |
29 // Every pointer in a frame has a slot id. On 32-bit platforms, doubles consume | 33 // Every pointer in a frame has a slot id. On 32-bit platforms, doubles consume |
30 // two slots. | 34 // two slots. |
31 // | 35 // |
32 // Stack slot indices >= 0 access the callee stack with slot 0 corresponding to | 36 // 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 | 37 // 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, | 38 // pointer. Some frames have additional information stored in the fixed header, |
35 // for example JSFunctions store the function context and marker in the fixed | 39 // 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 | 40 // header, with slot index 2 corresponding to the current function context and 3 |
37 // corresponding to the frame marker/JSFunction. The frame region immediately | 41 // corresponding to the frame marker/JSFunction. The frame region immediately |
38 // below the fixed header contains spill slots starting a 4 for JsFunctions. The | 42 // below the fixed header contains spill slots starting at 4 for JsFunctions. |
39 // callee-saved frame region below that starts at 4+spilled_slot_count. Callee | 43 // The callee-saved frame region below that starts at 4+spill_slot_count_. |
40 // stack slots corresponding to parameters are accessible through negative slot | 44 // Callee stack slots corresponding to parameters are accessible through |
41 // ids. | 45 // negative slot ids. |
42 // | 46 // |
43 // Every slot of a caller or callee frame is accessible by the register | 47 // Every slot of a caller or callee frame is accessible by the register |
44 // allocator and gap resolver with a SpillSlotOperand containing its | 48 // allocator and gap resolver with a SpillSlotOperand containing its |
45 // corresponding slot id. | 49 // corresponding slot id. |
46 // | 50 // |
47 // Below an example JSFunction Frame with slot ids, frame regions and contents: | 51 // Below an example JSFunction Frame with slot ids, frame regions and contents: |
48 // | 52 // |
49 // slot JS frame | 53 // slot JS frame |
50 // +-----------------+---------------------------- | 54 // +-----------------+-------------------------------- |
51 // -n-1 | parameter 0 | ^ | 55 // -n-1 | parameter 0 | ^ |
52 // |- - - - - - - - -| | | 56 // |- - - - - - - - -| | |
53 // -n | | Caller | 57 // -n | | Caller |
54 // ... | ... | frame slots | 58 // ... | ... | frame slots |
55 // -2 | parameter n-1 | (slot < 0) | 59 // -2 | parameter n-1 | (slot < 0) |
56 // |- - - - - - - - -| | | 60 // |- - - - - - - - -| | |
57 // -1 | parameter n | v | 61 // -1 | parameter n | v |
58 // -----+-----------------+---------------------------- | 62 // -----+-----------------+-------------------------------- |
59 // 0 | return addr | ^ ^ | 63 // 0 | return addr | ^ ^ |
60 // |- - - - - - - - -| | | | 64 // |- - - - - - - - -| | | |
61 // 1 | saved frame ptr | Fixed | | 65 // 1 | saved frame ptr | Fixed | |
62 // |- - - - - - - - -| Header <-- frame ptr | | 66 // |- - - - - - - - -| Header <-- frame ptr | |
63 // 2 | Context | | | | 67 // 2 | Context | | | |
64 // |- - - - - - - - -| | | | 68 // |- - - - - - - - -| | | |
65 // 3 |JSFunction/Marker| v | | 69 // 3 |JSFunction/Marker| v | |
66 // +-----------------+---- | | 70 // +-----------------+---- | |
67 // 4 | spill 1 | ^ Callee | 71 // 4 | spill 1 | ^ Callee |
68 // |- - - - - - - - -| | frame slots | 72 // |- - - - - - - - -| | frame slots |
69 // ... | ... | Spill slots (slot >= 0) | 73 // ... | ... | Spill slots (slot >= 0) |
70 // |- - - - - - - - -| | | | 74 // |- - - - - - - - -| | | |
71 // m+4 | spill m | v | | 75 // m+4 | spill m | v | |
72 // +-----------------+---- | | 76 // +-----------------+---- | |
73 // m+5 | callee-saved 1 | ^ | | 77 // m+5 | callee-saved 1 | ^ | |
74 // |- - - - - - - - -| | | | 78 // |- - - - - - - - -| | | |
75 // | ... | Callee-saved | | 79 // | ... | Callee-saved | |
76 // |- - - - - - - - -| | | | 80 // |- - - - - - - - -| | | |
77 // m+r+4 | callee-saved r | v v | 81 // m+r+4 | callee-saved r | v | |
78 // -----+-----------------+----- <-- stack ptr --------- | 82 // +-----------------+---- | |
| 83 // | parameter 0 | ^ | |
| 84 // |- - - - - - - - -| | | |
| 85 // | ... | Outgoing parameters | |
| 86 // |- - - - - - - - -| | (for function calls) | |
| 87 // | parameter p | v v |
| 88 // -----+-----------------+----- <-- stack ptr ------------- |
79 // | 89 // |
80 class Frame : public ZoneObject { | 90 class Frame : public ZoneObject { |
81 public: | 91 public: |
82 explicit Frame(int fixed_frame_size_in_slots); | 92 explicit Frame(int fixed_frame_size_in_slots); |
83 | 93 |
84 inline int GetTotalFrameSlotCount() { return frame_slot_count_; } | 94 inline int GetTotalFrameSlotCount() const { return frame_slot_count_; } |
85 | 95 |
86 inline int GetSavedCalleeRegisterSlotCount() { | 96 inline int GetSpToFpSlotCount() const { |
87 return spilled_callee_register_slot_count_; | 97 return GetTotalFrameSlotCount() - |
| 98 StandardFrameConstants::kFixedSlotCountAboveFp; |
88 } | 99 } |
89 inline int GetSpillSlotCount() { return stack_slot_count_; } | 100 inline int GetOutgoingParameterSlotCount() const { |
| 101 return outgoing_parameter_slot_count_; |
| 102 } |
| 103 inline int GetSavedCalleeRegisterSlotCount() const { |
| 104 return callee_saved_slot_count_; |
| 105 } |
| 106 inline int GetSpillSlotCount() const { return spill_slot_count_; } |
90 | 107 |
91 inline void SetElidedFrameSizeInSlots(int slots) { | 108 inline void SetElidedFrameSizeInSlots(int slots) { |
92 DCHECK_EQ(0, spilled_callee_register_slot_count_); | 109 DCHECK_EQ(0, callee_saved_slot_count_); |
93 DCHECK_EQ(0, stack_slot_count_); | 110 DCHECK_EQ(0, spill_slot_count_); |
94 frame_slot_count_ = slots; | 111 frame_slot_count_ = slots; |
95 } | 112 } |
96 | 113 |
97 void SetAllocatedRegisters(BitVector* regs) { | 114 void SetAllocatedRegisters(BitVector* regs) { |
98 DCHECK(allocated_registers_ == NULL); | 115 DCHECK(allocated_registers_ == NULL); |
99 allocated_registers_ = regs; | 116 allocated_registers_ = regs; |
100 } | 117 } |
101 | 118 |
102 void SetAllocatedDoubleRegisters(BitVector* regs) { | 119 void SetAllocatedDoubleRegisters(BitVector* regs) { |
103 DCHECK(allocated_double_registers_ == NULL); | 120 DCHECK(allocated_double_registers_ == NULL); |
104 allocated_double_registers_ = regs; | 121 allocated_double_registers_ = regs; |
105 } | 122 } |
106 | 123 |
107 bool DidAllocateDoubleRegisters() { | 124 bool DidAllocateDoubleRegisters() const { |
108 return !allocated_double_registers_->IsEmpty(); | 125 return !allocated_double_registers_->IsEmpty(); |
109 } | 126 } |
110 | 127 |
| 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 |
111 int AlignSavedCalleeRegisterSlots() { | 138 int AlignSavedCalleeRegisterSlots() { |
112 DCHECK_EQ(0, spilled_callee_register_slot_count_); | 139 DCHECK_EQ(0, callee_saved_slot_count_); |
113 int delta = frame_slot_count_ & 1; | 140 int delta = frame_slot_count_ & 1; |
114 frame_slot_count_ += delta; | 141 frame_slot_count_ += delta; |
115 return delta; | 142 return delta; |
116 } | 143 } |
117 | 144 |
118 void AllocateSavedCalleeRegisterSlots(int count) { | 145 void AllocateSavedCalleeRegisterSlots(int count) { |
| 146 DCHECK_EQ(0, outgoing_parameter_slot_count_); |
119 frame_slot_count_ += count; | 147 frame_slot_count_ += count; |
120 spilled_callee_register_slot_count_ += count; | 148 callee_saved_slot_count_ += count; |
121 } | 149 } |
122 | 150 |
123 int AllocateSpillSlot(int width) { | 151 int AllocateSpillSlot(int width) { |
124 DCHECK_EQ(0, spilled_callee_register_slot_count_); | 152 DCHECK_EQ(0, outgoing_parameter_slot_count_); |
| 153 DCHECK_EQ(0, callee_saved_slot_count_); |
125 int frame_slot_count_before = frame_slot_count_; | 154 int frame_slot_count_before = frame_slot_count_; |
126 int slot = AllocateAlignedFrameSlot(width); | 155 int slot = AllocateAlignedFrameSlot(width); |
127 stack_slot_count_ += (frame_slot_count_ - frame_slot_count_before); | 156 spill_slot_count_ += (frame_slot_count_ - frame_slot_count_before); |
128 return slot; | 157 return slot; |
129 } | 158 } |
130 | 159 |
131 int ReserveSpillSlots(size_t slot_count) { | 160 int ReserveSpillSlots(size_t slot_count) { |
132 DCHECK_EQ(0, spilled_callee_register_slot_count_); | 161 DCHECK_EQ(0, outgoing_parameter_slot_count_); |
133 DCHECK_EQ(0, stack_slot_count_); | 162 DCHECK_EQ(0, callee_saved_slot_count_); |
134 stack_slot_count_ += static_cast<int>(slot_count); | 163 DCHECK_EQ(0, spill_slot_count_); |
| 164 spill_slot_count_ += static_cast<int>(slot_count); |
135 frame_slot_count_ += static_cast<int>(slot_count); | 165 frame_slot_count_ += static_cast<int>(slot_count); |
136 return frame_slot_count_ - 1; | 166 return frame_slot_count_ - 1; |
137 } | 167 } |
138 | 168 |
139 private: | 169 private: |
140 int AllocateAlignedFrameSlot(int width) { | 170 int AllocateAlignedFrameSlot(int width) { |
141 DCHECK(width == 4 || width == 8); | 171 DCHECK(width == 4 || width == 8); |
142 // Skip one slot if necessary. | 172 // Skip one slot if necessary. |
143 if (width > kPointerSize) { | 173 if (width > kPointerSize) { |
144 DCHECK(width == kPointerSize * 2); | 174 DCHECK(width == kPointerSize * 2); |
145 frame_slot_count_++; | 175 frame_slot_count_++; |
146 frame_slot_count_ |= 1; | 176 frame_slot_count_ |= 1; |
147 } | 177 } |
148 return frame_slot_count_++; | 178 return frame_slot_count_++; |
149 } | 179 } |
150 | 180 |
151 private: | 181 private: |
152 int frame_slot_count_; | 182 int frame_slot_count_; |
153 int spilled_callee_register_slot_count_; | 183 int outgoing_parameter_slot_count_; |
154 int stack_slot_count_; | 184 int callee_saved_slot_count_; |
| 185 int spill_slot_count_; |
155 BitVector* allocated_registers_; | 186 BitVector* allocated_registers_; |
156 BitVector* allocated_double_registers_; | 187 BitVector* allocated_double_registers_; |
157 | 188 |
158 DISALLOW_COPY_AND_ASSIGN(Frame); | 189 DISALLOW_COPY_AND_ASSIGN(Frame); |
159 }; | 190 }; |
160 | 191 |
161 | 192 |
162 // Represents an offset from either the stack pointer or frame pointer. | 193 // Represents an offset from either the stack pointer or frame pointer. |
163 class FrameOffset { | 194 class FrameOffset { |
164 public: | 195 public: |
(...skipping 17 matching lines...) Expand all Loading... |
182 int offset_; // Encodes SP or FP in the low order bit. | 213 int offset_; // Encodes SP or FP in the low order bit. |
183 | 214 |
184 static const int kFromSp = 1; | 215 static const int kFromSp = 1; |
185 static const int kFromFp = 0; | 216 static const int kFromFp = 0; |
186 }; | 217 }; |
187 } // namespace compiler | 218 } // namespace compiler |
188 } // namespace internal | 219 } // namespace internal |
189 } // namespace v8 | 220 } // namespace v8 |
190 | 221 |
191 #endif // V8_COMPILER_FRAME_H_ | 222 #endif // V8_COMPILER_FRAME_H_ |
OLD | NEW |