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 | 10 |
10 namespace v8 { | 11 namespace v8 { |
11 namespace internal { | 12 namespace internal { |
12 namespace compiler { | 13 namespace compiler { |
13 | 14 |
14 // Collects the spill slot requirements and the allocated general and double | 15 // Collects the spill slot and other frame slot requirements for a compiled |
15 // registers for a compiled function. Frames are usually populated by the | 16 // function. Frames are usually populated by the register allocator and are used |
16 // register allocator and are used by Linkage to generate code for the prologue | 17 // by Linkage to generate code for the prologue and epilogue to compiled code. |
17 // and epilogue to compiled code. | 18 // |
| 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. |
| 28 // |
| 29 // Every pointer in a frame has a slot id. On 32-bit platforms, doubles consume |
| 30 // two slots. |
| 31 // |
| 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 |
| 41 // ids. |
| 42 // |
| 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. |
| 46 // |
| 47 // Below an example JSFunction Frame with slot ids, frame regions and contents: |
| 48 // |
| 49 // slot JS frame |
| 50 // +-----------------+---------------------------- |
| 51 // -n-1 | parameter 0 | ^ |
| 52 // |- - - - - - - - -| | |
| 53 // -n | | Caller |
| 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 | |
| 63 // 2 | Context | | | |
| 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 --------- |
| 79 // |
18 class Frame : public ZoneObject { | 80 class Frame : public ZoneObject { |
19 public: | 81 public: |
20 Frame() | 82 explicit Frame(int fixed_frame_size_in_slots); |
21 : register_save_area_size_(0), | |
22 spill_slot_count_(0), | |
23 osr_stack_slot_count_(0), | |
24 allocated_registers_(NULL), | |
25 allocated_double_registers_(NULL), | |
26 pc_on_stack_(true) {} | |
27 | 83 |
28 inline int GetSpillSlotCount() { return spill_slot_count_; } | 84 inline int GetTotalFrameSlotCount() { return frame_slot_count_; } |
| 85 |
| 86 inline int GetSavedCalleeRegisterSlotCount() { |
| 87 return spilled_callee_register_slot_count_; |
| 88 } |
| 89 inline int GetSpillSlotCount() { return stack_slot_count_; } |
| 90 |
| 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; |
| 95 } |
29 | 96 |
30 void SetAllocatedRegisters(BitVector* regs) { | 97 void SetAllocatedRegisters(BitVector* regs) { |
31 DCHECK(allocated_registers_ == NULL); | 98 DCHECK(allocated_registers_ == NULL); |
32 allocated_registers_ = regs; | 99 allocated_registers_ = regs; |
33 } | 100 } |
34 | 101 |
35 void SetAllocatedDoubleRegisters(BitVector* regs) { | 102 void SetAllocatedDoubleRegisters(BitVector* regs) { |
36 DCHECK(allocated_double_registers_ == NULL); | 103 DCHECK(allocated_double_registers_ == NULL); |
37 allocated_double_registers_ = regs; | 104 allocated_double_registers_ = regs; |
38 } | 105 } |
39 | 106 |
40 bool DidAllocateDoubleRegisters() { | 107 bool DidAllocateDoubleRegisters() { |
41 return !allocated_double_registers_->IsEmpty(); | 108 return !allocated_double_registers_->IsEmpty(); |
42 } | 109 } |
43 | 110 |
44 void SetRegisterSaveAreaSize(int size) { | 111 int AlignSavedCalleeRegisterSlots() { |
45 DCHECK(IsAligned(size, kPointerSize)); | 112 DCHECK_EQ(0, spilled_callee_register_slot_count_); |
46 register_save_area_size_ = size; | 113 int frame_slot_count_before = frame_slot_count_; |
| 114 frame_slot_count_ = RoundUp(frame_slot_count_, 2); |
| 115 return frame_slot_count_before - frame_slot_count_; |
47 } | 116 } |
48 | 117 |
49 int GetRegisterSaveAreaSize() { return register_save_area_size_; } | 118 void AllocateSavedCalleeRegisterSlots(int count) { |
50 | 119 frame_slot_count_ += count; |
51 // OSR stack slots, including locals and expression stack slots. | 120 spilled_callee_register_slot_count_ += count; |
52 void SetOsrStackSlotCount(int slots) { | |
53 DCHECK(slots >= 0); | |
54 osr_stack_slot_count_ = slots; | |
55 } | 121 } |
56 | 122 |
57 int GetOsrStackSlotCount() { return osr_stack_slot_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); |
| 128 return slot; |
| 129 } |
58 | 130 |
59 int AllocateSpillSlot(int width) { | 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; |
| 137 } |
| 138 |
| 139 private: |
| 140 int AllocateAlignedFrameSlot(int width) { |
60 DCHECK(width == 4 || width == 8); | 141 DCHECK(width == 4 || width == 8); |
61 // Skip one slot if necessary. | 142 // Skip one slot if necessary. |
62 if (width > kPointerSize) { | 143 if (width > kPointerSize) { |
63 DCHECK(width == kPointerSize * 2); | 144 DCHECK(width == kPointerSize * 2); |
64 spill_slot_count_++; | 145 frame_slot_count_++; |
65 spill_slot_count_ |= 1; | 146 frame_slot_count_ |= 1; |
66 } | 147 } |
67 return spill_slot_count_++; | 148 return frame_slot_count_++; |
68 } | 149 } |
69 | 150 |
70 void ReserveSpillSlots(size_t slot_count) { | |
71 DCHECK_EQ(0, spill_slot_count_); // can only reserve before allocation. | |
72 spill_slot_count_ = static_cast<int>(slot_count); | |
73 } | |
74 | |
75 void SetPCOnStack(bool val) { pc_on_stack_ = val; } | |
76 | |
77 int PCOnStackSize() { return pc_on_stack_ ? kRegisterSize : 0; } | |
78 | |
79 private: | 151 private: |
80 int register_save_area_size_; | 152 int frame_slot_count_; |
81 int spill_slot_count_; | 153 int spilled_callee_register_slot_count_; |
82 int osr_stack_slot_count_; | 154 int stack_slot_count_; |
83 BitVector* allocated_registers_; | 155 BitVector* allocated_registers_; |
84 BitVector* allocated_double_registers_; | 156 BitVector* allocated_double_registers_; |
85 bool pc_on_stack_; | |
86 | 157 |
87 DISALLOW_COPY_AND_ASSIGN(Frame); | 158 DISALLOW_COPY_AND_ASSIGN(Frame); |
88 }; | 159 }; |
89 | 160 |
90 | 161 |
91 // Represents an offset from either the stack pointer or frame pointer. | 162 // Represents an offset from either the stack pointer or frame pointer. |
92 class FrameOffset { | 163 class FrameOffset { |
93 public: | 164 public: |
94 inline bool from_stack_pointer() { return (offset_ & 1) == kFromSp; } | 165 inline bool from_stack_pointer() { return (offset_ & 1) == kFromSp; } |
95 inline bool from_frame_pointer() { return (offset_ & 1) == kFromFp; } | 166 inline bool from_frame_pointer() { return (offset_ & 1) == kFromFp; } |
(...skipping 15 matching lines...) Expand all Loading... |
111 int offset_; // Encodes SP or FP in the low order bit. | 182 int offset_; // Encodes SP or FP in the low order bit. |
112 | 183 |
113 static const int kFromSp = 1; | 184 static const int kFromSp = 1; |
114 static const int kFromFp = 0; | 185 static const int kFromFp = 0; |
115 }; | 186 }; |
116 } | 187 } |
117 } | 188 } |
118 } // namespace v8::internal::compiler | 189 } // namespace v8::internal::compiler |
119 | 190 |
120 #endif // V8_COMPILER_FRAME_H_ | 191 #endif // V8_COMPILER_FRAME_H_ |
OLD | NEW |