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 28 #ifndef V8_ARM_VIRTUAL_FRAME_ARM_H_ 29 #define V8_ARM_VIRTUAL_FRAME_ARM_H_ 30 31 #include "register-allocator.h" 32 #include "scopes.h" 33 34 namespace v8 { 35 namespace internal { 36 37 // ------------------------------------------------------------------------- 38 // Virtual frames 39 // 40 // The virtual frame is an abstraction of the physical stack frame. It 41 // encapsulates the parameters, frame-allocated locals, and the expression 42 // stack. It supports push/pop operations on the expression stack, as well 43 // as random access to the expression stack elements, locals, and 44 // parameters. 45 46 class VirtualFrame : public ZoneObject { 47 public: 48 // A utility class to introduce a scope where the virtual frame is 49 // expected to remain spilled. The constructor spills the code 50 // generator's current frame, but no attempt is made to require it 51 // to stay spilled. It is intended as documentation while the code 52 // generator is being transformed. 53 class SpilledScope BASE_EMBEDDED { 54 public: 55 SpilledScope() {} 56 }; 57 58 // An illegal index into the virtual frame. 59 static const int kIllegalIndex = -1; 60 61 // Construct an initial virtual frame on entry to a JS function. 62 VirtualFrame(); 63 64 // Construct a virtual frame as a clone of an existing one. 65 explicit VirtualFrame(VirtualFrame* original); 66 67 CodeGenerator* cgen() { return CodeGeneratorScope::Current(); } 68 MacroAssembler* masm() { return cgen()->masm(); } 69 70 // Create a duplicate of an existing valid frame element. 71 FrameElement CopyElementAt(int index, 72 NumberInfo::Type info = NumberInfo::kUnknown); 73 74 // The number of elements on the virtual frame. 75 int element_count() { return elements_.length(); } 76 77 // The height of the virtual expression stack. 78 int height() { 79 return element_count() - expression_base_index(); 80 } 81 82 int register_location(int num) { 83 ASSERT(num >= 0 && num < RegisterAllocator::kNumRegisters); 84 return register_locations_[num]; 85 } 86 87 int register_location(Register reg) { 88 return register_locations_[RegisterAllocator::ToNumber(reg)]; 89 } 90 91 void set_register_location(Register reg, int index) { 92 register_locations_[RegisterAllocator::ToNumber(reg)] = index; 93 } 94 95 bool is_used(int num) { 96 ASSERT(num >= 0 && num < RegisterAllocator::kNumRegisters); 97 return register_locations_[num] != kIllegalIndex; 98 } 99 100 bool is_used(Register reg) { 101 return register_locations_[RegisterAllocator::ToNumber(reg)] 102 != kIllegalIndex; 103 } 104 105 // Add extra in-memory elements to the top of the frame to match an actual 106 // frame (eg, the frame after an exception handler is pushed). No code is 107 // emitted. 108 void Adjust(int count); 109 110 // Forget elements from the top of the frame to match an actual frame (eg, 111 // the frame after a runtime call). No code is emitted. 112 void Forget(int count) { 113 ASSERT(count >= 0); 114 ASSERT(stack_pointer_ == element_count() - 1); 115 stack_pointer_ -= count; 116 // On ARM, all elements are in memory, so there is no extra bookkeeping 117 // (registers, copies, etc.) beyond dropping the elements. 118 elements_.Rewind(stack_pointer_ + 1); 119 } 120 121 // Forget count elements from the top of the frame and adjust the stack 122 // pointer downward. This is used, for example, before merging frames at 123 // break, continue, and return targets. 124 void ForgetElements(int count); 125 126 // Spill all values from the frame to memory. 127 void SpillAll(); 128 129 // Spill all occurrences of a specific register from the frame. 130 void Spill(Register reg) { 131 if (is_used(reg)) SpillElementAt(register_location(reg)); 132 } 133 134 // Spill all occurrences of an arbitrary register if possible. Return the 135 // register spilled or no_reg if it was not possible to free any register 136 // (ie, they all have frame-external references). 137 Register SpillAnyRegister(); 138 139 // Prepare this virtual frame for merging to an expected frame by 140 // performing some state changes that do not require generating 141 // code. It is guaranteed that no code will be generated. 142 void PrepareMergeTo(VirtualFrame* expected); 143 144 // Make this virtual frame have a state identical to an expected virtual 145 // frame. As a side effect, code may be emitted to make this frame match 146 // the expected one. 147 void MergeTo(VirtualFrame* expected); 148 149 // Detach a frame from its code generator, perhaps temporarily. This 150 // tells the register allocator that it is free to use frame-internal 151 // registers. Used when the code generator's frame is switched from this 152 // one to NULL by an unconditional jump. 153 void DetachFromCodeGenerator() { 154 RegisterAllocator* cgen_allocator = cgen()->allocator(); 155 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { 156 if (is_used(i)) cgen_allocator->Unuse(i); 157 } 158 } 159 160 // (Re)attach a frame to its code generator. This informs the register 161 // allocator that the frame-internal register references are active again. 162 // Used when a code generator's frame is switched from NULL to this one by 163 // binding a label. 164 void AttachToCodeGenerator() { 165 RegisterAllocator* cgen_allocator = cgen()->allocator(); 166 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) { 167 if (is_used(i)) cgen_allocator->Unuse(i); 168 } 169 } 170 171 // Emit code for the physical JS entry and exit frame sequences. After 172 // calling Enter, the virtual frame is ready for use; and after calling 173 // Exit it should not be used. Note that Enter does not allocate space in 174 // the physical frame for storing frame-allocated locals. 175 void Enter(); 176 void Exit(); 177 178 // Prepare for returning from the frame by spilling locals and 179 // dropping all non-locals elements in the virtual frame. This 180 // avoids generating unnecessary merge code when jumping to the 181 // shared return site. Emits code for spills. 182 void PrepareForReturn(); 183 184 // Number of local variables after when we use a loop for allocating. 185 static const int kLocalVarBound = 5; 186 187 // Allocate and initialize the frame-allocated locals. 188 void AllocateStackSlots(); 189 190 // The current top of the expression stack as an assembly operand. 191 MemOperand Top() { return MemOperand(sp, 0); } 192 193 // An element of the expression stack as an assembly operand. 194 MemOperand ElementAt(int index) { 195 return MemOperand(sp, index * kPointerSize); 196 } 197 198 // Random-access store to a frame-top relative frame element. The result 199 // becomes owned by the frame and is invalidated. 200 void SetElementAt(int index, Result* value); 201 202 // Set a frame element to a constant. The index is frame-top relative. 203 void SetElementAt(int index, Handle<Object> value) { 204 Result temp(value); 205 SetElementAt(index, &temp); 206 } 207 208 void PushElementAt(int index) { 209 PushFrameSlotAt(element_count() - index - 1); 210 } 211 212 // A frame-allocated local as an assembly operand. 213 MemOperand LocalAt(int index) { 214 ASSERT(0 <= index); 215 ASSERT(index < local_count()); 216 return MemOperand(fp, kLocal0Offset - index * kPointerSize); 217 } 218 219 // Push a copy of the value of a local frame slot on top of the frame. 220 void PushLocalAt(int index) { 221 PushFrameSlotAt(local0_index() + index); 222 } 223 224 // Push the value of a local frame slot on top of the frame and invalidate 225 // the local slot. The slot should be written to before trying to read 226 // from it again. 227 void TakeLocalAt(int index) { 228 TakeFrameSlotAt(local0_index() + index); 229 } 230 231 // Store the top value on the virtual frame into a local frame slot. The 232 // value is left in place on top of the frame. 233 void StoreToLocalAt(int index) { 234 StoreToFrameSlotAt(local0_index() + index); 235 } 236 237 // Push the address of the receiver slot on the frame. 238 void PushReceiverSlotAddress(); 239 240 // The function frame slot. 241 MemOperand Function() { return MemOperand(fp, kFunctionOffset); } 242 243 // Push the function on top of the frame. 244 void PushFunction() { PushFrameSlotAt(function_index()); } 245 246 // The context frame slot. 247 MemOperand Context() { return MemOperand(fp, kContextOffset); } 248 249 // Save the value of the esi register to the context frame slot. 250 void SaveContextRegister(); 251 252 // Restore the esi register from the value of the context frame 253 // slot. 254 void RestoreContextRegister(); 255 256 // A parameter as an assembly operand. 257 MemOperand ParameterAt(int index) { 258 // Index -1 corresponds to the receiver. 259 ASSERT(-1 <= index); // -1 is the receiver. 260 ASSERT(index <= parameter_count()); 261 return MemOperand(fp, (1 + parameter_count() - index) * kPointerSize); 262 } 263 264 // Push a copy of the value of a parameter frame slot on top of the frame. 265 void PushParameterAt(int index) { 266 PushFrameSlotAt(param0_index() + index); 267 } 268 269 // Push the value of a paramter frame slot on top of the frame and 270 // invalidate the parameter slot. The slot should be written to before 271 // trying to read from it again. 272 void TakeParameterAt(int index) { 273 TakeFrameSlotAt(param0_index() + index); 274 } 275 276 // Store the top value on the virtual frame into a parameter frame slot. 277 // The value is left in place on top of the frame. 278 void StoreToParameterAt(int index) { 279 StoreToFrameSlotAt(param0_index() + index); 280 } 281 282 // The receiver frame slot. 283 MemOperand Receiver() { return ParameterAt(-1); } 284 285 // Push a try-catch or try-finally handler on top of the virtual frame. 286 void PushTryHandler(HandlerType type); 287 288 // Call stub given the number of arguments it expects on (and 289 // removes from) the stack. 290 void CallStub(CodeStub* stub, int arg_count) { 291 Forget(arg_count); 292 ASSERT(cgen()->HasValidEntryRegisters()); 293 masm()->CallStub(stub); 294 } 295 296 // Call runtime given the number of arguments expected on (and 297 // removed from) the stack. 298 void CallRuntime(Runtime::Function* f, int arg_count); 299 void CallRuntime(Runtime::FunctionId id, int arg_count); 300 301 #ifdef ENABLE_DEBUGGER_SUPPORT 302 void DebugBreak(); 303 #endif 304 305 // Invoke builtin given the number of arguments it expects on (and 306 // removes from) the stack. 307 void InvokeBuiltin(Builtins::JavaScript id, 308 InvokeJSFlags flag, 309 int arg_count); 310 311 // Call into an IC stub given the number of arguments it removes 312 // from the stack. Register arguments to the IC stub are implicit, 313 // and depend on the type of IC stub. 314 void CallCodeObject(Handle<Code> ic, 315 RelocInfo::Mode rmode, 316 int dropped_args); 317 318 // Drop a number of elements from the top of the expression stack. May 319 // emit code to affect the physical frame. Does not clobber any registers 320 // excepting possibly the stack pointer. 321 void Drop(int count); 322 323 // Drop one element. 324 void Drop() { Drop(1); } 325 326 // Duplicate the top element of the frame. 327 void Dup() { PushFrameSlotAt(element_count() - 1); } 328 329 // Pop an element from the top of the expression stack. Returns a 330 // Result, which may be a constant or a register. 331 Result Pop(); 332 333 // Pop and save an element from the top of the expression stack and 334 // emit a corresponding pop instruction. 335 void EmitPop(Register reg); 336 337 // Push an element on top of the expression stack and emit a 338 // corresponding push instruction. 339 void EmitPush(Register reg); 340 341 // Push multiple registers on the stack and the virtual frame 342 // Register are selected by setting bit in src_regs and 343 // are pushed in decreasing order: r15 .. r0. 344 void EmitPushMultiple(int count, int src_regs); 345 346 // Push an element on the virtual frame. 347 void Push(Register reg, NumberInfo::Type info = NumberInfo::kUnknown); 348 void Push(Handle<Object> value); 349 void Push(Smi* value) { Push(Handle<Object>(value)); } 350 351 // Pushing a result invalidates it (its contents become owned by the frame). 352 void Push(Result* result) { 353 if (result->is_register()) { 354 Push(result->reg()); 355 } else { 356 ASSERT(result->is_constant()); 357 Push(result->handle()); 358 } 359 result->Unuse(); 360 } 361 362 // Nip removes zero or more elements from immediately below the top 363 // of the frame, leaving the previous top-of-frame value on top of 364 // the frame. Nip(k) is equivalent to x = Pop(), Drop(k), Push(x). 365 void Nip(int num_dropped); 366 367 private: 368 static const int kLocal0Offset = JavaScriptFrameConstants::kLocal0Offset; 369 static const int kFunctionOffset = JavaScriptFrameConstants::kFunctionOffset; 370 static const int kContextOffset = StandardFrameConstants::kContextOffset; 371 372 static const int kHandlerSize = StackHandlerConstants::kSize / kPointerSize; 373 static const int kPreallocatedElements = 5 + 8; // 8 expression stack slots. 374 375 ZoneList<FrameElement> elements_; 376 377 // The index of the element that is at the processor's stack pointer 378 // (the sp register). 379 int stack_pointer_; 380 381 // The index of the register frame element using each register, or 382 // kIllegalIndex if a register is not on the frame. 383 int register_locations_[RegisterAllocator::kNumRegisters]; 384 385 // The number of frame-allocated locals and parameters respectively. 386 int parameter_count() { return cgen()->scope()->num_parameters(); } 387 int local_count() { return cgen()->scope()->num_stack_slots(); } 388 389 // The index of the element that is at the processor's frame pointer 390 // (the fp register). The parameters, receiver, function, and context 391 // are below the frame pointer. 392 int frame_pointer() { return parameter_count() + 3; } 393 394 // The index of the first parameter. The receiver lies below the first 395 // parameter. 396 int param0_index() { return 1; } 397 398 // The index of the context slot in the frame. It is immediately 399 // below the frame pointer. 400 int context_index() { return frame_pointer() - 1; } 401 402 // The index of the function slot in the frame. It is below the frame 403 // pointer and context slot. 404 int function_index() { return frame_pointer() - 2; } 405 406 // The index of the first local. Between the frame pointer and the 407 // locals lies the return address. 408 int local0_index() { return frame_pointer() + 2; } 409 410 // The index of the base of the expression stack. 411 int expression_base_index() { return local0_index() + local_count(); } 412 413 // Convert a frame index into a frame pointer relative offset into the 414 // actual stack. 415 int fp_relative(int index) { 416 ASSERT(index < element_count()); 417 ASSERT(frame_pointer() < element_count()); // FP is on the frame. 418 return (frame_pointer() - index) * kPointerSize; 419 } 420 421 // Record an occurrence of a register in the virtual frame. This has the 422 // effect of incrementing the register's external reference count and 423 // of updating the index of the register's location in the frame. 424 void Use(Register reg, int index) { 425 ASSERT(!is_used(reg)); 426 set_register_location(reg, index); 427 cgen()->allocator()->Use(reg); 428 } 429 430 // Record that a register reference has been dropped from the frame. This 431 // decrements the register's external reference count and invalidates the 432 // index of the register's location in the frame. 433 void Unuse(Register reg) { 434 ASSERT(is_used(reg)); 435 set_register_location(reg, kIllegalIndex); 436 cgen()->allocator()->Unuse(reg); 437 } 438 439 // Spill the element at a particular index---write it to memory if 440 // necessary, free any associated register, and forget its value if 441 // constant. 442 void SpillElementAt(int index); 443 444 // Sync the element at a particular index. If it is a register or 445 // constant that disagrees with the value on the stack, write it to memory. 446 // Keep the element type as register or constant, and clear the dirty bit. 447 void SyncElementAt(int index); 448 449 // Sync the range of elements in [begin, end] with memory. 450 void SyncRange(int begin, int end); 451 452 // Sync a single unsynced element that lies beneath or at the stack pointer. 453 void SyncElementBelowStackPointer(int index); 454 455 // Sync a single unsynced element that lies just above the stack pointer. 456 void SyncElementByPushing(int index); 457 458 // Push a copy of a frame slot (typically a local or parameter) on top of 459 // the frame. 460 void PushFrameSlotAt(int index); 461 462 // Push a the value of a frame slot (typically a local or parameter) on 463 // top of the frame and invalidate the slot. 464 void TakeFrameSlotAt(int index); 465 466 // Store the value on top of the frame to a frame slot (typically a local 467 // or parameter). 468 void StoreToFrameSlotAt(int index); 469 470 // Spill all elements in registers. Spill the top spilled_args elements 471 // on the frame. Sync all other frame elements. 472 // Then drop dropped_args elements from the virtual frame, to match 473 // the effect of an upcoming call that will drop them from the stack. 474 void PrepareForCall(int spilled_args, int dropped_args); 475 476 // Move frame elements currently in registers or constants, that 477 // should be in memory in the expected frame, to memory. 478 void MergeMoveRegistersToMemory(VirtualFrame* expected); 479 480 // Make the register-to-register moves necessary to 481 // merge this frame with the expected frame. 482 // Register to memory moves must already have been made, 483 // and memory to register moves must follow this call. 484 // This is because some new memory-to-register moves are 485 // created in order to break cycles of register moves. 486 // Used in the implementation of MergeTo(). 487 void MergeMoveRegistersToRegisters(VirtualFrame* expected); 488 489 // Make the memory-to-register and constant-to-register moves 490 // needed to make this frame equal the expected frame. 491 // Called after all register-to-memory and register-to-register 492 // moves have been made. After this function returns, the frames 493 // should be equal. 494 void MergeMoveMemoryToRegisters(VirtualFrame* expected); 495 496 // Invalidates a frame slot (puts an invalid frame element in it). 497 // Copies on the frame are correctly handled, and if this slot was 498 // the backing store of copies, the index of the new backing store 499 // is returned. Otherwise, returns kIllegalIndex. 500 // Register counts are correctly updated. 501 int InvalidateFrameSlotAt(int index); 502 503 bool Equals(VirtualFrame* other); 504 505 // Classes that need raw access to the elements_ array. 506 friend class DeferredCode; 507 friend class JumpTarget; 508 }; 509 510 511 } } // namespace v8::internal 512 513 #endif // V8_ARM_VIRTUAL_FRAME_ARM_H_ 514