ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 28 | #ifndef V8_X64_VIRTUAL_FRAME_X64_H_ |
| 29 | #define V8_X64_VIRTUAL_FRAME_X64_H_ |
| 30 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 31 | #include "type-info.h" |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 32 | #include "register-allocator.h" |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 33 | #include "scopes.h" |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 34 | #include "codegen.h" |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 35 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 36 | namespace v8 { |
| 37 | namespace internal { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 38 | |
| 39 | // ------------------------------------------------------------------------- |
| 40 | // Virtual frames |
| 41 | // |
| 42 | // The virtual frame is an abstraction of the physical stack frame. It |
| 43 | // encapsulates the parameters, frame-allocated locals, and the expression |
| 44 | // stack. It supports push/pop operations on the expression stack, as well |
| 45 | // as random access to the expression stack elements, locals, and |
| 46 | // parameters. |
| 47 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 48 | class VirtualFrame : public ZoneObject { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 49 | public: |
| 50 | // A utility class to introduce a scope where the virtual frame is |
| 51 | // expected to remain spilled. The constructor spills the code |
| 52 | // generator's current frame, but no attempt is made to require it |
| 53 | // to stay spilled. It is intended as documentation while the code |
| 54 | // generator is being transformed. |
| 55 | class SpilledScope BASE_EMBEDDED { |
| 56 | public: |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 57 | SpilledScope() : previous_state_(cgen()->in_spilled_code()) { |
| 58 | ASSERT(cgen()->has_valid_frame()); |
| 59 | cgen()->frame()->SpillAll(); |
| 60 | cgen()->set_in_spilled_code(true); |
| 61 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 62 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 63 | ~SpilledScope() { |
| 64 | cgen()->set_in_spilled_code(previous_state_); |
| 65 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 66 | |
| 67 | private: |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 68 | bool previous_state_; |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 69 | |
| 70 | CodeGenerator* cgen() { return CodeGeneratorScope::Current(); } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | // An illegal index into the virtual frame. |
| 74 | static const int kIllegalIndex = -1; |
| 75 | |
| 76 | // Construct an initial virtual frame on entry to a JS function. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 77 | inline VirtualFrame(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 78 | |
| 79 | // Construct a virtual frame as a clone of an existing one. |
sgjesse@chromium.org | 833cdd7 | 2010-02-26 10:06:16 +0000 | [diff] [blame] | 80 | explicit inline VirtualFrame(VirtualFrame* original); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 81 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 82 | CodeGenerator* cgen() { return CodeGeneratorScope::Current(); } |
| 83 | MacroAssembler* masm() { return cgen()->masm(); } |
| 84 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 85 | // Create a duplicate of an existing valid frame element. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 86 | FrameElement CopyElementAt(int index, |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 87 | TypeInfo info = TypeInfo::Uninitialized()); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 88 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 89 | // The number of elements on the virtual frame. |
| 90 | int element_count() { return elements_.length(); } |
| 91 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 92 | // The height of the virtual expression stack. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 93 | int height() { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 94 | return element_count() - expression_base_index(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 95 | } |
| 96 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 97 | int register_location(int num) { |
| 98 | ASSERT(num >= 0 && num < RegisterAllocator::kNumRegisters); |
| 99 | return register_locations_[num]; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 100 | } |
| 101 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 102 | inline int register_location(Register reg); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 103 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 104 | inline void set_register_location(Register reg, int index); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 105 | |
| 106 | bool is_used(int num) { |
| 107 | ASSERT(num >= 0 && num < RegisterAllocator::kNumRegisters); |
| 108 | return register_locations_[num] != kIllegalIndex; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 109 | } |
| 110 | |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 111 | inline bool is_used(Register reg); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 112 | |
| 113 | // Add extra in-memory elements to the top of the frame to match an actual |
| 114 | // frame (eg, the frame after an exception handler is pushed). No code is |
| 115 | // emitted. |
| 116 | void Adjust(int count); |
| 117 | |
| 118 | // Forget count elements from the top of the frame all in-memory |
| 119 | // (including synced) and adjust the stack pointer downward, to |
| 120 | // match an external frame effect (examples include a call removing |
| 121 | // its arguments, and exiting a try/catch removing an exception |
| 122 | // handler). No code will be emitted. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 123 | void Forget(int count) { |
| 124 | ASSERT(count >= 0); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 125 | ASSERT(stack_pointer_ == element_count() - 1); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 126 | stack_pointer_ -= count; |
| 127 | ForgetElements(count); |
| 128 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 129 | |
| 130 | // Forget count elements from the top of the frame without adjusting |
| 131 | // the stack pointer downward. This is used, for example, before |
| 132 | // merging frames at break, continue, and return targets. |
| 133 | void ForgetElements(int count); |
| 134 | |
| 135 | // Spill all values from the frame to memory. |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 136 | inline void SpillAll(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 137 | |
| 138 | // Spill all occurrences of a specific register from the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 139 | void Spill(Register reg) { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 140 | if (is_used(reg)) SpillElementAt(register_location(reg)); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 141 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 142 | |
| 143 | // Spill all occurrences of an arbitrary register if possible. Return the |
| 144 | // register spilled or no_reg if it was not possible to free any register |
| 145 | // (ie, they all have frame-external references). |
| 146 | Register SpillAnyRegister(); |
| 147 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 148 | // Spill the top element of the frame to memory. |
| 149 | void SpillTop() { SpillElementAt(element_count() - 1); } |
| 150 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 151 | // Sync the range of elements in [begin, end] with memory. |
| 152 | void SyncRange(int begin, int end); |
| 153 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 154 | // Make this frame so that an arbitrary frame of the same height can |
ager@chromium.org | 5aa501c | 2009-06-23 07:57:28 +0000 | [diff] [blame] | 155 | // be merged to it. Copies and constants are removed from the frame. |
| 156 | void MakeMergable(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 157 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 158 | // Prepare this virtual frame for merging to an expected frame by |
| 159 | // performing some state changes that do not require generating |
| 160 | // code. It is guaranteed that no code will be generated. |
| 161 | void PrepareMergeTo(VirtualFrame* expected); |
| 162 | |
| 163 | // Make this virtual frame have a state identical to an expected virtual |
| 164 | // frame. As a side effect, code may be emitted to make this frame match |
| 165 | // the expected one. |
| 166 | void MergeTo(VirtualFrame* expected); |
| 167 | |
| 168 | // Detach a frame from its code generator, perhaps temporarily. This |
| 169 | // tells the register allocator that it is free to use frame-internal |
| 170 | // registers. Used when the code generator's frame is switched from this |
| 171 | // one to NULL by an unconditional jump. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 172 | void DetachFromCodeGenerator() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 173 | RegisterAllocator* cgen_allocator = cgen()->allocator(); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 174 | for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { |
| 175 | if (is_used(i)) cgen_allocator->Unuse(i); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 178 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 179 | // (Re)attach a frame to its code generator. This informs the register |
| 180 | // allocator that the frame-internal register references are active again. |
| 181 | // Used when a code generator's frame is switched from NULL to this one by |
| 182 | // binding a label. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 183 | void AttachToCodeGenerator() { |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 184 | RegisterAllocator* cgen_allocator = cgen()->allocator(); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 185 | for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { |
| 186 | if (is_used(i)) cgen_allocator->Use(i); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 189 | |
| 190 | // Emit code for the physical JS entry and exit frame sequences. After |
| 191 | // calling Enter, the virtual frame is ready for use; and after calling |
| 192 | // Exit it should not be used. Note that Enter does not allocate space in |
| 193 | // the physical frame for storing frame-allocated locals. |
| 194 | void Enter(); |
| 195 | void Exit(); |
| 196 | |
| 197 | // Prepare for returning from the frame by spilling locals. This |
| 198 | // avoids generating unnecessary merge code when jumping to the |
| 199 | // shared return site. Emits code for spills. |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 200 | inline void PrepareForReturn(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 201 | |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 202 | // Number of local variables after when we use a loop for allocating. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 203 | static const int kLocalVarBound = 14; |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 204 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 205 | // Allocate and initialize the frame-allocated locals. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 206 | void AllocateStackSlots(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 207 | |
| 208 | // An element of the expression stack as an assembly operand. |
| 209 | Operand ElementAt(int index) const { |
| 210 | return Operand(rsp, index * kPointerSize); |
| 211 | } |
| 212 | |
| 213 | // Random-access store to a frame-top relative frame element. The result |
| 214 | // becomes owned by the frame and is invalidated. |
| 215 | void SetElementAt(int index, Result* value); |
| 216 | |
| 217 | // Set a frame element to a constant. The index is frame-top relative. |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 218 | inline void SetElementAt(int index, Handle<Object> value); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 219 | |
| 220 | void PushElementAt(int index) { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 221 | PushFrameSlotAt(element_count() - index - 1); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | void StoreToElementAt(int index) { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 225 | StoreToFrameSlotAt(element_count() - index - 1); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // A frame-allocated local as an assembly operand. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 229 | Operand LocalAt(int index) { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 230 | ASSERT(0 <= index); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 231 | ASSERT(index < local_count()); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 232 | return Operand(rbp, kLocal0Offset - index * kPointerSize); |
| 233 | } |
| 234 | |
| 235 | // Push a copy of the value of a local frame slot on top of the frame. |
| 236 | void PushLocalAt(int index) { |
| 237 | PushFrameSlotAt(local0_index() + index); |
| 238 | } |
| 239 | |
| 240 | // Push the value of a local frame slot on top of the frame and invalidate |
| 241 | // the local slot. The slot should be written to before trying to read |
| 242 | // from it again. |
| 243 | void TakeLocalAt(int index) { |
| 244 | TakeFrameSlotAt(local0_index() + index); |
| 245 | } |
| 246 | |
| 247 | // Store the top value on the virtual frame into a local frame slot. The |
| 248 | // value is left in place on top of the frame. |
| 249 | void StoreToLocalAt(int index) { |
| 250 | StoreToFrameSlotAt(local0_index() + index); |
| 251 | } |
| 252 | |
| 253 | // Push the address of the receiver slot on the frame. |
| 254 | void PushReceiverSlotAddress(); |
| 255 | |
| 256 | // Push the function on top of the frame. |
| 257 | void PushFunction() { PushFrameSlotAt(function_index()); } |
| 258 | |
| 259 | // Save the value of the esi register to the context frame slot. |
| 260 | void SaveContextRegister(); |
| 261 | |
| 262 | // Restore the esi register from the value of the context frame |
| 263 | // slot. |
| 264 | void RestoreContextRegister(); |
| 265 | |
| 266 | // A parameter as an assembly operand. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 267 | Operand ParameterAt(int index) { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 268 | ASSERT(-1 <= index); // -1 is the receiver. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 269 | ASSERT(index < parameter_count()); |
| 270 | return Operand(rbp, (1 + parameter_count() - index) * kPointerSize); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | // Push a copy of the value of a parameter frame slot on top of the frame. |
| 274 | void PushParameterAt(int index) { |
| 275 | PushFrameSlotAt(param0_index() + index); |
| 276 | } |
| 277 | |
| 278 | // Push the value of a paramter frame slot on top of the frame and |
| 279 | // invalidate the parameter slot. The slot should be written to before |
| 280 | // trying to read from it again. |
| 281 | void TakeParameterAt(int index) { |
| 282 | TakeFrameSlotAt(param0_index() + index); |
| 283 | } |
| 284 | |
| 285 | // Store the top value on the virtual frame into a parameter frame slot. |
| 286 | // The value is left in place on top of the frame. |
| 287 | void StoreToParameterAt(int index) { |
| 288 | StoreToFrameSlotAt(param0_index() + index); |
| 289 | } |
| 290 | |
| 291 | // The receiver frame slot. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 292 | Operand Receiver() { return ParameterAt(-1); } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 293 | |
| 294 | // Push a try-catch or try-finally handler on top of the virtual frame. |
| 295 | void PushTryHandler(HandlerType type); |
| 296 | |
| 297 | // Call stub given the number of arguments it expects on (and |
| 298 | // removes from) the stack. |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 299 | inline Result CallStub(CodeStub* stub, int arg_count); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 300 | |
| 301 | // Call stub that takes a single argument passed in eax. The |
| 302 | // argument is given as a result which does not have to be eax or |
| 303 | // even a register. The argument is consumed by the call. |
| 304 | Result CallStub(CodeStub* stub, Result* arg); |
| 305 | |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 306 | // Call stub that takes a pair of arguments passed in edx (arg0, rdx) and |
| 307 | // eax (arg1, rax). The arguments are given as results which do not have |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 308 | // to be in the proper registers or even in registers. The |
| 309 | // arguments are consumed by the call. |
| 310 | Result CallStub(CodeStub* stub, Result* arg0, Result* arg1); |
| 311 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 312 | // Call JS function from top of the stack with arguments |
| 313 | // taken from the stack. |
| 314 | Result CallJSFunction(int arg_count); |
| 315 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 316 | // Call runtime given the number of arguments expected on (and |
| 317 | // removed from) the stack. |
| 318 | Result CallRuntime(Runtime::Function* f, int arg_count); |
| 319 | Result CallRuntime(Runtime::FunctionId id, int arg_count); |
| 320 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 321 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 322 | void DebugBreak(); |
| 323 | #endif |
| 324 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 325 | // Invoke builtin given the number of arguments it expects on (and |
| 326 | // removes from) the stack. |
| 327 | Result InvokeBuiltin(Builtins::JavaScript id, |
| 328 | InvokeFlag flag, |
| 329 | int arg_count); |
| 330 | |
| 331 | // Call load IC. Name and receiver are found on top of the frame. |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame^] | 332 | // Both are dropped. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 333 | Result CallLoadIC(RelocInfo::Mode mode); |
| 334 | |
| 335 | // Call keyed load IC. Key and receiver are found on top of the |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame^] | 336 | // frame. Both are dropped. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 337 | Result CallKeyedLoadIC(RelocInfo::Mode mode); |
| 338 | |
fschneider@chromium.org | ed78ffd | 2010-07-21 11:05:19 +0000 | [diff] [blame] | 339 | // Call store IC. If the load is contextual, value is found on top of the |
| 340 | // frame. If not, value and receiver are on the frame. Both are dropped. |
| 341 | Result CallStoreIC(Handle<String> name, bool is_contextual); |
| 342 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 343 | // Call keyed store IC. Value, key, and receiver are found on top |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame^] | 344 | // of the frame. All three are dropped. |
| 345 | Result CallKeyedStoreIC(); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 346 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 347 | // Call call IC. Function name, arguments, and receiver are found on top |
| 348 | // of the frame and dropped by the call. |
| 349 | // The argument count does not include the receiver. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 350 | Result CallCallIC(RelocInfo::Mode mode, int arg_count, int loop_nesting); |
| 351 | |
whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [diff] [blame^] | 352 | // Call keyed call IC. Same calling convention as CallCallIC. |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 353 | Result CallKeyedCallIC(RelocInfo::Mode mode, int arg_count, int loop_nesting); |
| 354 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 355 | // Allocate and call JS function as constructor. Arguments, |
| 356 | // receiver (global object), and function are found on top of the |
| 357 | // frame. Function is not dropped. The argument count does not |
| 358 | // include the receiver. |
| 359 | Result CallConstructor(int arg_count); |
| 360 | |
| 361 | // Drop a number of elements from the top of the expression stack. May |
| 362 | // emit code to affect the physical frame. Does not clobber any registers |
| 363 | // excepting possibly the stack pointer. |
| 364 | void Drop(int count); |
| 365 | |
| 366 | // Drop one element. |
| 367 | void Drop() { Drop(1); } |
| 368 | |
| 369 | // Duplicate the top element of the frame. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 370 | void Dup() { PushFrameSlotAt(element_count() - 1); } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 371 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 372 | // Duplicate the n'th element from the top of the frame. |
| 373 | // Dup(1) is equivalent to Dup(). |
| 374 | void Dup(int n) { |
| 375 | ASSERT(n > 0); |
| 376 | PushFrameSlotAt(element_count() - n); |
| 377 | } |
| 378 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 379 | // Pop an element from the top of the expression stack. Returns a |
| 380 | // Result, which may be a constant or a register. |
| 381 | Result Pop(); |
| 382 | |
| 383 | // Pop and save an element from the top of the expression stack and |
| 384 | // emit a corresponding pop instruction. |
| 385 | void EmitPop(Register reg); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 386 | void EmitPop(const Operand& operand); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 387 | |
| 388 | // Push an element on top of the expression stack and emit a |
| 389 | // corresponding push instruction. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 390 | void EmitPush(Register reg, |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 391 | TypeInfo info = TypeInfo::Unknown()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 392 | void EmitPush(const Operand& operand, |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 393 | TypeInfo info = TypeInfo::Unknown()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 394 | void EmitPush(Heap::RootListIndex index, |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 395 | TypeInfo info = TypeInfo::Unknown()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 396 | void EmitPush(Immediate immediate, |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 397 | TypeInfo info = TypeInfo::Unknown()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 398 | void EmitPush(Smi* value); |
ager@chromium.org | 3e87580 | 2009-06-29 08:26:34 +0000 | [diff] [blame] | 399 | // Uses kScratchRegister, emits appropriate relocation info. |
| 400 | void EmitPush(Handle<Object> value); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 401 | |
| 402 | // Push an element on the virtual frame. |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 403 | inline void Push(Register reg, TypeInfo info = TypeInfo::Unknown()); |
sgjesse@chromium.org | 833cdd7 | 2010-02-26 10:06:16 +0000 | [diff] [blame] | 404 | inline void Push(Handle<Object> value); |
| 405 | inline void Push(Smi* value); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 406 | |
| 407 | // Pushing a result invalidates it (its contents become owned by the |
| 408 | // frame). |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 409 | void Push(Result* result) { |
| 410 | if (result->is_register()) { |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 411 | Push(result->reg(), result->type_info()); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 412 | } else { |
| 413 | ASSERT(result->is_constant()); |
| 414 | Push(result->handle()); |
| 415 | } |
| 416 | result->Unuse(); |
| 417 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 418 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 419 | // Pushing an expression expects that the expression is trivial (according |
| 420 | // to Expression::IsTrivial). |
| 421 | void Push(Expression* expr); |
| 422 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 423 | // Nip removes zero or more elements from immediately below the top |
| 424 | // of the frame, leaving the previous top-of-frame value on top of |
| 425 | // the frame. Nip(k) is equivalent to x = Pop(), Drop(k), Push(x). |
sgjesse@chromium.org | 833cdd7 | 2010-02-26 10:06:16 +0000 | [diff] [blame] | 426 | inline void Nip(int num_dropped); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 427 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 428 | inline void SetTypeForLocalAt(int index, TypeInfo info); |
| 429 | inline void SetTypeForParamAt(int index, TypeInfo info); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 430 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 431 | private: |
| 432 | static const int kLocal0Offset = JavaScriptFrameConstants::kLocal0Offset; |
| 433 | static const int kFunctionOffset = JavaScriptFrameConstants::kFunctionOffset; |
| 434 | static const int kContextOffset = StandardFrameConstants::kContextOffset; |
| 435 | |
| 436 | static const int kHandlerSize = StackHandlerConstants::kSize / kPointerSize; |
| 437 | static const int kPreallocatedElements = 5 + 8; // 8 expression stack slots. |
| 438 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 439 | ZoneList<FrameElement> elements_; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 440 | |
| 441 | // The index of the element that is at the processor's stack pointer |
| 442 | // (the esp register). |
| 443 | int stack_pointer_; |
| 444 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 445 | // The index of the register frame element using each register, or |
| 446 | // kIllegalIndex if a register is not on the frame. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 447 | int register_locations_[RegisterAllocator::kNumRegisters]; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 448 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 449 | // The number of frame-allocated locals and parameters respectively. |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 450 | inline int parameter_count(); |
| 451 | inline int local_count(); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 452 | |
| 453 | // The index of the element that is at the processor's frame pointer |
| 454 | // (the ebp register). The parameters, receiver, and return address |
| 455 | // are below the frame pointer. |
| 456 | int frame_pointer() { return parameter_count() + 2; } |
| 457 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 458 | // The index of the first parameter. The receiver lies below the first |
| 459 | // parameter. |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 460 | int param0_index() { return 1; } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 461 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 462 | // The index of the context slot in the frame. It is immediately |
| 463 | // above the frame pointer. |
| 464 | int context_index() { return frame_pointer() + 1; } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 465 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 466 | // The index of the function slot in the frame. It is above the frame |
| 467 | // pointer and the context slot. |
| 468 | int function_index() { return frame_pointer() + 2; } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 469 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 470 | // The index of the first local. Between the frame pointer and the |
| 471 | // locals lie the context and the function. |
| 472 | int local0_index() { return frame_pointer() + 3; } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 473 | |
| 474 | // The index of the base of the expression stack. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 475 | int expression_base_index() { return local0_index() + local_count(); } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 476 | |
| 477 | // Convert a frame index into a frame pointer relative offset into the |
| 478 | // actual stack. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 479 | int fp_relative(int index) { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 480 | ASSERT(index < element_count()); |
| 481 | ASSERT(frame_pointer() < element_count()); // FP is on the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 482 | return (frame_pointer() - index) * kPointerSize; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | // Record an occurrence of a register in the virtual frame. This has the |
| 486 | // effect of incrementing the register's external reference count and |
| 487 | // of updating the index of the register's location in the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 488 | void Use(Register reg, int index) { |
| 489 | ASSERT(!is_used(reg)); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 490 | set_register_location(reg, index); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 491 | cgen()->allocator()->Use(reg); |
| 492 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 493 | |
| 494 | // Record that a register reference has been dropped from the frame. This |
| 495 | // decrements the register's external reference count and invalidates the |
| 496 | // index of the register's location in the frame. |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 497 | void Unuse(Register reg) { |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 498 | ASSERT(is_used(reg)); |
| 499 | set_register_location(reg, kIllegalIndex); |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 500 | cgen()->allocator()->Unuse(reg); |
| 501 | } |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 502 | |
| 503 | // Spill the element at a particular index---write it to memory if |
| 504 | // necessary, free any associated register, and forget its value if |
| 505 | // constant. |
| 506 | void SpillElementAt(int index); |
| 507 | |
| 508 | // Sync the element at a particular index. If it is a register or |
| 509 | // constant that disagrees with the value on the stack, write it to memory. |
| 510 | // Keep the element type as register or constant, and clear the dirty bit. |
| 511 | void SyncElementAt(int index); |
| 512 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 513 | // Sync a single unsynced element that lies beneath or at the stack pointer. |
| 514 | void SyncElementBelowStackPointer(int index); |
| 515 | |
| 516 | // Sync a single unsynced element that lies just above the stack pointer. |
| 517 | void SyncElementByPushing(int index); |
| 518 | |
| 519 | // Push a copy of a frame slot (typically a local or parameter) on top of |
| 520 | // the frame. |
sgjesse@chromium.org | 833cdd7 | 2010-02-26 10:06:16 +0000 | [diff] [blame] | 521 | inline void PushFrameSlotAt(int index); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 522 | |
| 523 | // Push a the value of a frame slot (typically a local or parameter) on |
| 524 | // top of the frame and invalidate the slot. |
| 525 | void TakeFrameSlotAt(int index); |
| 526 | |
| 527 | // Store the value on top of the frame to a frame slot (typically a local |
| 528 | // or parameter). |
| 529 | void StoreToFrameSlotAt(int index); |
| 530 | |
| 531 | // Spill all elements in registers. Spill the top spilled_args elements |
| 532 | // on the frame. Sync all other frame elements. |
| 533 | // Then drop dropped_args elements from the virtual frame, to match |
| 534 | // the effect of an upcoming call that will drop them from the stack. |
| 535 | void PrepareForCall(int spilled_args, int dropped_args); |
| 536 | |
| 537 | // Move frame elements currently in registers or constants, that |
| 538 | // should be in memory in the expected frame, to memory. |
| 539 | void MergeMoveRegistersToMemory(VirtualFrame* expected); |
| 540 | |
| 541 | // Make the register-to-register moves necessary to |
| 542 | // merge this frame with the expected frame. |
| 543 | // Register to memory moves must already have been made, |
| 544 | // and memory to register moves must follow this call. |
| 545 | // This is because some new memory-to-register moves are |
| 546 | // created in order to break cycles of register moves. |
| 547 | // Used in the implementation of MergeTo(). |
| 548 | void MergeMoveRegistersToRegisters(VirtualFrame* expected); |
| 549 | |
| 550 | // Make the memory-to-register and constant-to-register moves |
| 551 | // needed to make this frame equal the expected frame. |
| 552 | // Called after all register-to-memory and register-to-register |
| 553 | // moves have been made. After this function returns, the frames |
| 554 | // should be equal. |
| 555 | void MergeMoveMemoryToRegisters(VirtualFrame* expected); |
| 556 | |
| 557 | // Invalidates a frame slot (puts an invalid frame element in it). |
| 558 | // Copies on the frame are correctly handled, and if this slot was |
| 559 | // the backing store of copies, the index of the new backing store |
| 560 | // is returned. Otherwise, returns kIllegalIndex. |
| 561 | // Register counts are correctly updated. |
| 562 | int InvalidateFrameSlotAt(int index); |
| 563 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 564 | // This function assumes that a and b are the only results that could be in |
| 565 | // the registers a_reg or b_reg. Other results can be live, but must not |
| 566 | // be in the registers a_reg or b_reg. The results a and b are invalidated. |
| 567 | void MoveResultsToRegisters(Result* a, |
| 568 | Result* b, |
| 569 | Register a_reg, |
| 570 | Register b_reg); |
| 571 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 572 | // Call a code stub that has already been prepared for calling (via |
| 573 | // PrepareForCall). |
| 574 | Result RawCallStub(CodeStub* stub); |
| 575 | |
| 576 | // Calls a code object which has already been prepared for calling |
| 577 | // (via PrepareForCall). |
| 578 | Result RawCallCodeObject(Handle<Code> code, RelocInfo::Mode rmode); |
| 579 | |
sgjesse@chromium.org | 833cdd7 | 2010-02-26 10:06:16 +0000 | [diff] [blame] | 580 | inline bool Equals(VirtualFrame* other); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 581 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 582 | // Classes that need raw access to the elements_ array. |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 583 | friend class FrameRegisterState; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 584 | friend class JumpTarget; |
| 585 | }; |
| 586 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 587 | |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 588 | } } // namespace v8::internal |
| 589 | |
| 590 | #endif // V8_X64_VIRTUAL_FRAME_X64_H_ |