Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1 | // Copyright 2010 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 | |
| 28 | |
| 29 | #ifndef V8_MIPS_VIRTUAL_FRAME_MIPS_H_ |
| 30 | #define V8_MIPS_VIRTUAL_FRAME_MIPS_H_ |
| 31 | |
| 32 | #include "register-allocator.h" |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 33 | |
| 34 | namespace v8 { |
| 35 | namespace internal { |
| 36 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 37 | // This dummy class is only used to create invalid virtual frames. |
| 38 | extern class InvalidVirtualFrameInitializer {}* kInvalidVirtualFrameInitializer; |
| 39 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 40 | |
| 41 | // ------------------------------------------------------------------------- |
| 42 | // Virtual frames |
| 43 | // |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 44 | // The virtual frame is an abstraction of the physical stack frame. It |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 45 | // encapsulates the parameters, frame-allocated locals, and the expression |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 46 | // stack. It supports push/pop operations on the expression stack, as well |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 47 | // as random access to the expression stack elements, locals, and |
| 48 | // parameters. |
| 49 | |
| 50 | class VirtualFrame : public ZoneObject { |
| 51 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 52 | class RegisterAllocationScope; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 53 | // A utility class to introduce a scope where the virtual frame is |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 54 | // expected to remain spilled. The constructor spills the code |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 55 | // generator's current frame, and keeps it spilled. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 56 | class SpilledScope BASE_EMBEDDED { |
| 57 | public: |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 58 | explicit SpilledScope(VirtualFrame* frame) |
| 59 | : old_is_spilled_( |
| 60 | Isolate::Current()->is_virtual_frame_in_spilled_scope()) { |
| 61 | if (frame != NULL) { |
| 62 | if (!old_is_spilled_) { |
| 63 | frame->SpillAll(); |
| 64 | } else { |
| 65 | frame->AssertIsSpilled(); |
| 66 | } |
| 67 | } |
| 68 | Isolate::Current()->set_is_virtual_frame_in_spilled_scope(true); |
| 69 | } |
| 70 | ~SpilledScope() { |
| 71 | Isolate::Current()->set_is_virtual_frame_in_spilled_scope( |
| 72 | old_is_spilled_); |
| 73 | } |
| 74 | static bool is_spilled() { |
| 75 | return Isolate::Current()->is_virtual_frame_in_spilled_scope(); |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | int old_is_spilled_; |
| 80 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 81 | SpilledScope() {} |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 82 | |
| 83 | friend class RegisterAllocationScope; |
| 84 | }; |
| 85 | |
| 86 | class RegisterAllocationScope BASE_EMBEDDED { |
| 87 | public: |
| 88 | // A utility class to introduce a scope where the virtual frame |
| 89 | // is not spilled, ie. where register allocation occurs. Eventually |
| 90 | // when RegisterAllocationScope is ubiquitous it can be removed |
| 91 | // along with the (by then unused) SpilledScope class. |
| 92 | inline explicit RegisterAllocationScope(CodeGenerator* cgen); |
| 93 | inline ~RegisterAllocationScope(); |
| 94 | |
| 95 | private: |
| 96 | CodeGenerator* cgen_; |
| 97 | bool old_is_spilled_; |
| 98 | |
| 99 | RegisterAllocationScope() {} |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | // An illegal index into the virtual frame. |
| 103 | static const int kIllegalIndex = -1; |
| 104 | |
| 105 | // Construct an initial virtual frame on entry to a JS function. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 106 | inline VirtualFrame(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 107 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 108 | // Construct an invalid virtual frame, used by JumpTargets. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 109 | explicit inline VirtualFrame(InvalidVirtualFrameInitializer* dummy); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 110 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 111 | // Construct a virtual frame as a clone of an existing one. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 112 | explicit inline VirtualFrame(VirtualFrame* original); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 113 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 114 | inline CodeGenerator* cgen() const; |
| 115 | inline MacroAssembler* masm(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 116 | |
| 117 | // The number of elements on the virtual frame. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 118 | int element_count() const { return element_count_; } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 119 | |
| 120 | // The height of the virtual expression stack. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 121 | inline int height() const; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 122 | |
| 123 | bool is_used(int num) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 124 | switch (num) { |
| 125 | case 0: { // a0. |
| 126 | return kA0InUse[top_of_stack_state_]; |
| 127 | } |
| 128 | case 1: { // a1. |
| 129 | return kA1InUse[top_of_stack_state_]; |
| 130 | } |
| 131 | case 2: |
| 132 | case 3: |
| 133 | case 4: |
| 134 | case 5: |
| 135 | case 6: { // a2 to a3, t0 to t2. |
| 136 | ASSERT(num - kFirstAllocatedRegister < kNumberOfAllocatedRegisters); |
| 137 | ASSERT(num >= kFirstAllocatedRegister); |
| 138 | if ((register_allocation_map_ & |
| 139 | (1 << (num - kFirstAllocatedRegister))) == 0) { |
| 140 | return false; |
| 141 | } else { |
| 142 | return true; |
| 143 | } |
| 144 | } |
| 145 | default: { |
| 146 | ASSERT(num < kFirstAllocatedRegister || |
| 147 | num >= kFirstAllocatedRegister + kNumberOfAllocatedRegisters); |
| 148 | return false; |
| 149 | } |
| 150 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // Add extra in-memory elements to the top of the frame to match an actual |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 154 | // frame (eg, the frame after an exception handler is pushed). No code is |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 155 | // emitted. |
| 156 | void Adjust(int count); |
| 157 | |
| 158 | // Forget elements from the top of the frame to match an actual frame (eg, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 159 | // the frame after a runtime call). No code is emitted except to bring the |
| 160 | // frame to a spilled state. |
| 161 | void Forget(int count); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 162 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 163 | |
| 164 | // Spill all values from the frame to memory. |
| 165 | void SpillAll(); |
| 166 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 167 | void AssertIsSpilled() const { |
| 168 | ASSERT(top_of_stack_state_ == NO_TOS_REGISTERS); |
| 169 | ASSERT(register_allocation_map_ == 0); |
| 170 | } |
| 171 | |
| 172 | void AssertIsNotSpilled() { |
| 173 | ASSERT(!SpilledScope::is_spilled()); |
| 174 | } |
| 175 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 176 | // Spill all occurrences of a specific register from the frame. |
| 177 | void Spill(Register reg) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 178 | UNIMPLEMENTED(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 181 | // Spill all occurrences of an arbitrary register if possible. Return the |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 182 | // register spilled or no_reg if it was not possible to free any register |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 183 | // (ie, they all have frame-external references). Unimplemented. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 184 | Register SpillAnyRegister(); |
| 185 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 186 | // Make this virtual frame have a state identical to an expected virtual |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 187 | // frame. As a side effect, code may be emitted to make this frame match |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 188 | // the expected one. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 189 | void MergeTo(const VirtualFrame* expected, |
| 190 | Condition cond = al, |
| 191 | Register r1 = no_reg, |
| 192 | const Operand& r2 = Operand(no_reg)); |
| 193 | |
| 194 | void MergeTo(VirtualFrame* expected, |
| 195 | Condition cond = al, |
| 196 | Register r1 = no_reg, |
| 197 | const Operand& r2 = Operand(no_reg)); |
| 198 | |
| 199 | // Checks whether this frame can be branched to by the other frame. |
| 200 | bool IsCompatibleWith(const VirtualFrame* other) const { |
| 201 | return (tos_known_smi_map_ & (~other->tos_known_smi_map_)) == 0; |
| 202 | } |
| 203 | |
| 204 | inline void ForgetTypeInfo() { |
| 205 | tos_known_smi_map_ = 0; |
| 206 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 207 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 208 | // Detach a frame from its code generator, perhaps temporarily. This |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 209 | // tells the register allocator that it is free to use frame-internal |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 210 | // registers. Used when the code generator's frame is switched from this |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 211 | // one to NULL by an unconditional jump. |
| 212 | void DetachFromCodeGenerator() { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 215 | // (Re)attach a frame to its code generator. This informs the register |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 216 | // allocator that the frame-internal register references are active again. |
| 217 | // Used when a code generator's frame is switched from NULL to this one by |
| 218 | // binding a label. |
| 219 | void AttachToCodeGenerator() { |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 222 | // Emit code for the physical JS entry and exit frame sequences. After |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 223 | // calling Enter, the virtual frame is ready for use; and after calling |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 224 | // Exit it should not be used. Note that Enter does not allocate space in |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 225 | // the physical frame for storing frame-allocated locals. |
| 226 | void Enter(); |
| 227 | void Exit(); |
| 228 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 229 | // Prepare for returning from the frame by elements in the virtual frame. |
| 230 | // This avoids generating unnecessary merge code when jumping to the shared |
| 231 | // return site. No spill code emitted. Value to return should be in v0. |
| 232 | inline void PrepareForReturn(); |
| 233 | |
| 234 | // Number of local variables after when we use a loop for allocating. |
| 235 | static const int kLocalVarBound = 5; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 236 | |
| 237 | // Allocate and initialize the frame-allocated locals. |
| 238 | void AllocateStackSlots(); |
| 239 | |
| 240 | // The current top of the expression stack as an assembly operand. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 241 | MemOperand Top() { |
| 242 | AssertIsSpilled(); |
| 243 | return MemOperand(sp, 0); |
| 244 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 245 | |
| 246 | // An element of the expression stack as an assembly operand. |
| 247 | MemOperand ElementAt(int index) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 248 | int adjusted_index = index - kVirtualElements[top_of_stack_state_]; |
| 249 | ASSERT(adjusted_index >= 0); |
| 250 | return MemOperand(sp, adjusted_index * kPointerSize); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 253 | bool KnownSmiAt(int index) { |
| 254 | if (index >= kTOSKnownSmiMapSize) return false; |
| 255 | return (tos_known_smi_map_ & (1 << index)) != 0; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 256 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 257 | // A frame-allocated local as an assembly operand. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 258 | inline MemOperand LocalAt(int index); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 259 | |
| 260 | // Push the address of the receiver slot on the frame. |
| 261 | void PushReceiverSlotAddress(); |
| 262 | |
| 263 | // The function frame slot. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 264 | MemOperand Function() { return MemOperand(fp, kFunctionOffset); } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 265 | |
| 266 | // The context frame slot. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 267 | MemOperand Context() { return MemOperand(fp, kContextOffset); } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 268 | |
| 269 | // A parameter as an assembly operand. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 270 | inline MemOperand ParameterAt(int index); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 271 | |
| 272 | // The receiver frame slot. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 273 | inline MemOperand Receiver(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 274 | |
| 275 | // Push a try-catch or try-finally handler on top of the virtual frame. |
| 276 | void PushTryHandler(HandlerType type); |
| 277 | |
| 278 | // Call stub given the number of arguments it expects on (and |
| 279 | // removes from) the stack. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 280 | inline void CallStub(CodeStub* stub, int arg_count); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 281 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 282 | // Call JS function from top of the stack with arguments |
| 283 | // taken from the stack. |
| 284 | void CallJSFunction(int arg_count); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 285 | |
| 286 | // Call runtime given the number of arguments expected on (and |
| 287 | // removed from) the stack. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 288 | void CallRuntime(const Runtime::Function* f, int arg_count); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 289 | void CallRuntime(Runtime::FunctionId id, int arg_count); |
| 290 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 291 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 292 | void DebugBreak(); |
| 293 | #endif |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 294 | |
| 295 | // Invoke builtin given the number of arguments it expects on (and |
| 296 | // removes from) the stack. |
| 297 | void InvokeBuiltin(Builtins::JavaScript id, |
| 298 | InvokeJSFlags flag, |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 299 | int arg_count); |
| 300 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 301 | // Call load IC. Receiver is on the stack and is consumed. Result is returned |
| 302 | // in v0. |
| 303 | void CallLoadIC(Handle<String> name, RelocInfo::Mode mode); |
| 304 | |
| 305 | // Call store IC. If the load is contextual, value is found on top of the |
| 306 | // frame. If not, value and receiver are on the frame. Both are consumed. |
| 307 | // Result is returned in v0. |
| 308 | void CallStoreIC(Handle<String> name, bool is_contextual); |
| 309 | |
| 310 | // Call keyed load IC. Key and receiver are on the stack. Both are consumed. |
| 311 | // Result is returned in v0. |
| 312 | void CallKeyedLoadIC(); |
| 313 | |
| 314 | // Call keyed store IC. Value, key and receiver are on the stack. All three |
| 315 | // are consumed. Result is returned in v0 (and a0). |
| 316 | void CallKeyedStoreIC(); |
| 317 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 318 | // Call into an IC stub given the number of arguments it removes |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 319 | // from the stack. Register arguments to the IC stub are implicit, |
| 320 | // and depend on the type of IC stub. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 321 | void CallCodeObject(Handle<Code> ic, |
| 322 | RelocInfo::Mode rmode, |
| 323 | int dropped_args); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 324 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 325 | // Drop a number of elements from the top of the expression stack. May |
| 326 | // emit code to affect the physical frame. Does not clobber any registers |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 327 | // excepting possibly the stack pointer. |
| 328 | void Drop(int count); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 329 | |
| 330 | // Drop one element. |
| 331 | void Drop() { Drop(1); } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 332 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 333 | // Pop an element from the top of the expression stack. Discards |
| 334 | // the result. |
| 335 | void Pop(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 336 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 337 | // Pop an element from the top of the expression stack. The register |
| 338 | // will be one normally used for the top of stack register allocation |
| 339 | // so you can't hold on to it if you push on the stack. |
| 340 | Register PopToRegister(Register but_not_to_this_one = no_reg); |
| 341 | |
| 342 | // Look at the top of the stack. The register returned is aliased and |
| 343 | // must be copied to a scratch register before modification. |
| 344 | Register Peek(); |
| 345 | |
| 346 | // Look at the value beneath the top of the stack. The register returned is |
| 347 | // aliased and must be copied to a scratch register before modification. |
| 348 | Register Peek2(); |
| 349 | |
| 350 | // Duplicate the top of stack. |
| 351 | void Dup(); |
| 352 | |
| 353 | // Duplicate the two elements on top of stack. |
| 354 | void Dup2(); |
| 355 | |
| 356 | // Flushes all registers, but it puts a copy of the top-of-stack in a0. |
| 357 | void SpillAllButCopyTOSToA0(); |
| 358 | |
| 359 | // Flushes all registers, but it puts a copy of the top-of-stack in a1. |
| 360 | void SpillAllButCopyTOSToA1(); |
| 361 | |
| 362 | // Flushes all registers, but it puts a copy of the top-of-stack in a1 |
| 363 | // and the next value on the stack in a0. |
| 364 | void SpillAllButCopyTOSToA1A0(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 365 | |
| 366 | // Pop and save an element from the top of the expression stack and |
| 367 | // emit a corresponding pop instruction. |
| 368 | void EmitPop(Register reg); |
| 369 | // Same but for multiple registers |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 370 | void EmitMultiPop(RegList regs); |
| 371 | void EmitMultiPopReversed(RegList regs); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 372 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 373 | |
| 374 | // Takes the top two elements and puts them in a0 (top element) and a1 |
| 375 | // (second element). |
| 376 | void PopToA1A0(); |
| 377 | |
| 378 | // Takes the top element and puts it in a1. |
| 379 | void PopToA1(); |
| 380 | |
| 381 | // Takes the top element and puts it in a0. |
| 382 | void PopToA0(); |
| 383 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 384 | // Push an element on top of the expression stack and emit a |
| 385 | // corresponding push instruction. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 386 | void EmitPush(Register reg, TypeInfo type_info = TypeInfo::Unknown()); |
| 387 | void EmitPush(Operand operand, TypeInfo type_info = TypeInfo::Unknown()); |
| 388 | void EmitPush(MemOperand operand, TypeInfo type_info = TypeInfo::Unknown()); |
| 389 | void EmitPushRoot(Heap::RootListIndex index); |
| 390 | |
| 391 | // Overwrite the nth thing on the stack. If the nth position is in a |
| 392 | // register then this turns into a Move, otherwise an sw. Afterwards |
| 393 | // you can still use the register even if it is a register that can be |
| 394 | // used for TOS (a0 or a1). |
| 395 | void SetElementAt(Register reg, int this_far_down); |
| 396 | |
| 397 | // Get a register which is free and which must be immediately used to |
| 398 | // push on the top of the stack. |
| 399 | Register GetTOSRegister(); |
| 400 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 401 | // Same but for multiple registers. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 402 | void EmitMultiPush(RegList regs); |
| 403 | void EmitMultiPushReversed(RegList regs); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 404 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 405 | static Register scratch0() { return t4; } |
| 406 | static Register scratch1() { return t5; } |
| 407 | static Register scratch2() { return t6; } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 408 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 409 | private: |
| 410 | static const int kLocal0Offset = JavaScriptFrameConstants::kLocal0Offset; |
| 411 | static const int kFunctionOffset = JavaScriptFrameConstants::kFunctionOffset; |
| 412 | static const int kContextOffset = StandardFrameConstants::kContextOffset; |
| 413 | |
| 414 | static const int kHandlerSize = StackHandlerConstants::kSize / kPointerSize; |
| 415 | static const int kPreallocatedElements = 5 + 8; // 8 expression stack slots. |
| 416 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 417 | // 5 states for the top of stack, which can be in memory or in a0 and a1. |
| 418 | enum TopOfStack { NO_TOS_REGISTERS, A0_TOS, A1_TOS, A1_A0_TOS, A0_A1_TOS, |
| 419 | TOS_STATES}; |
| 420 | static const int kMaxTOSRegisters = 2; |
| 421 | |
| 422 | static const bool kA0InUse[TOS_STATES]; |
| 423 | static const bool kA1InUse[TOS_STATES]; |
| 424 | static const int kVirtualElements[TOS_STATES]; |
| 425 | static const TopOfStack kStateAfterPop[TOS_STATES]; |
| 426 | static const TopOfStack kStateAfterPush[TOS_STATES]; |
| 427 | static const Register kTopRegister[TOS_STATES]; |
| 428 | static const Register kBottomRegister[TOS_STATES]; |
| 429 | |
| 430 | // We allocate up to 5 locals in registers. |
| 431 | static const int kNumberOfAllocatedRegisters = 5; |
| 432 | // r2 to r6 are allocated to locals. |
| 433 | static const int kFirstAllocatedRegister = 2; |
| 434 | |
| 435 | static const Register kAllocatedRegisters[kNumberOfAllocatedRegisters]; |
| 436 | |
| 437 | static Register AllocatedRegister(int r) { |
| 438 | ASSERT(r >= 0 && r < kNumberOfAllocatedRegisters); |
| 439 | return kAllocatedRegisters[r]; |
| 440 | } |
| 441 | |
| 442 | // The number of elements on the stack frame. |
| 443 | int element_count_; |
| 444 | TopOfStack top_of_stack_state_:3; |
| 445 | int register_allocation_map_:kNumberOfAllocatedRegisters; |
| 446 | static const int kTOSKnownSmiMapSize = 4; |
| 447 | unsigned tos_known_smi_map_:kTOSKnownSmiMapSize; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 448 | |
| 449 | // The index of the element that is at the processor's stack pointer |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 450 | // (the sp register). For now since everything is in memory it is given |
| 451 | // by the number of elements on the not-very-virtual stack frame. |
| 452 | int stack_pointer() { return element_count_ - 1; } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 453 | |
| 454 | // The number of frame-allocated locals and parameters respectively. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 455 | inline int parameter_count() const; |
| 456 | inline int local_count() const; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 457 | |
| 458 | // The index of the element that is at the processor's frame pointer |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 459 | // (the fp register). The parameters, receiver, function, and context |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 460 | // are below the frame pointer. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 461 | inline int frame_pointer() const; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 462 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 463 | // The index of the first parameter. The receiver lies below the first |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 464 | // parameter. |
| 465 | int param0_index() { return 1; } |
| 466 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 467 | // The index of the context slot in the frame. It is immediately |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 468 | // below the frame pointer. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 469 | inline int context_index(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 470 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 471 | // The index of the function slot in the frame. It is below the frame |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 472 | // pointer and context slot. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 473 | inline int function_index(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 474 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 475 | // The index of the first local. Between the frame pointer and the |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 476 | // locals lies the return address. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 477 | inline int local0_index() const; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 478 | |
| 479 | // The index of the base of the expression stack. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 480 | inline int expression_base_index() const; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 481 | |
| 482 | // Convert a frame index into a frame pointer relative offset into the |
| 483 | // actual stack. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 484 | inline int fp_relative(int index); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 485 | |
| 486 | // Spill all elements in registers. Spill the top spilled_args elements |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 487 | // on the frame. Sync all other frame elements. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 488 | // Then drop dropped_args elements from the virtual frame, to match |
| 489 | // the effect of an upcoming call that will drop them from the stack. |
| 490 | void PrepareForCall(int spilled_args, int dropped_args); |
| 491 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 492 | // If all top-of-stack registers are in use then the lowest one is pushed |
| 493 | // onto the physical stack and made free. |
| 494 | void EnsureOneFreeTOSRegister(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 495 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 496 | // Emit instructions to get the top of stack state from where we are to where |
| 497 | // we want to be. |
| 498 | void MergeTOSTo(TopOfStack expected_state, |
| 499 | Condition cond = al, |
| 500 | Register r1 = no_reg, |
| 501 | const Operand& r2 = Operand(no_reg)); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 502 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 503 | inline bool Equals(const VirtualFrame* other); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 504 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 505 | inline void LowerHeight(int count) { |
| 506 | element_count_ -= count; |
| 507 | if (count >= kTOSKnownSmiMapSize) { |
| 508 | tos_known_smi_map_ = 0; |
| 509 | } else { |
| 510 | tos_known_smi_map_ >>= count; |
| 511 | } |
| 512 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 513 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 514 | inline void RaiseHeight(int count, unsigned known_smi_map = 0) { |
| 515 | ASSERT(known_smi_map < (1u << count)); |
| 516 | element_count_ += count; |
| 517 | if (count >= kTOSKnownSmiMapSize) { |
| 518 | tos_known_smi_map_ = known_smi_map; |
| 519 | } else { |
| 520 | tos_known_smi_map_ = ((tos_known_smi_map_ << count) | known_smi_map); |
| 521 | } |
| 522 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 523 | friend class JumpTarget; |
| 524 | }; |
| 525 | |
| 526 | |
| 527 | } } // namespace v8::internal |
| 528 | |
| 529 | #endif // V8_MIPS_VIRTUAL_FRAME_MIPS_H_ |
| 530 | |