OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 23 matching lines...) Expand all Loading... |
34 | 34 |
35 // ------------------------------------------------------------------------- | 35 // ------------------------------------------------------------------------- |
36 // Virtual frames | 36 // Virtual frames |
37 // | 37 // |
38 // The virtual frame is an abstraction of the physical stack frame. It | 38 // The virtual frame is an abstraction of the physical stack frame. It |
39 // encapsulates the parameters, frame-allocated locals, and the expression | 39 // encapsulates the parameters, frame-allocated locals, and the expression |
40 // stack. It supports push/pop operations on the expression stack, as well | 40 // stack. It supports push/pop operations on the expression stack, as well |
41 // as random access to the expression stack elements, locals, and | 41 // as random access to the expression stack elements, locals, and |
42 // parameters. | 42 // parameters. |
43 | 43 |
44 class VirtualFrame : public Malloced { | 44 class VirtualFrame : public ZoneObject { |
45 public: | 45 public: |
46 // A utility class to introduce a scope where the virtual frame is | 46 // A utility class to introduce a scope where the virtual frame is |
47 // expected to remain spilled. The constructor spills the code | 47 // expected to remain spilled. The constructor spills the code |
48 // generator's current frame, but no attempt is made to require it | 48 // generator's current frame, but no attempt is made to require it |
49 // to stay spilled. It is intended as documentation while the code | 49 // to stay spilled. It is intended as documentation while the code |
50 // generator is being transformed. | 50 // generator is being transformed. |
51 class SpilledScope BASE_EMBEDDED { | 51 class SpilledScope BASE_EMBEDDED { |
52 public: | 52 public: |
53 explicit SpilledScope(CodeGenerator* cgen); | 53 explicit SpilledScope(CodeGenerator* cgen) |
| 54 : cgen_(cgen), |
| 55 previous_state_(cgen->in_spilled_code()) { |
| 56 ASSERT(cgen->has_valid_frame()); |
| 57 cgen->frame()->SpillAll(); |
| 58 cgen->set_in_spilled_code(true); |
| 59 } |
54 | 60 |
55 ~SpilledScope(); | 61 ~SpilledScope() { |
| 62 cgen_->set_in_spilled_code(previous_state_); |
| 63 } |
56 | 64 |
57 private: | 65 private: |
58 CodeGenerator* cgen_; | 66 CodeGenerator* cgen_; |
59 bool previous_state_; | 67 bool previous_state_; |
60 }; | 68 }; |
61 | 69 |
62 // An illegal index into the virtual frame. | 70 // An illegal index into the virtual frame. |
63 static const int kIllegalIndex = -1; | 71 static const int kIllegalIndex = -1; |
64 | 72 |
65 // Construct an initial virtual frame on entry to a JS function. | 73 // Construct an initial virtual frame on entry to a JS function. |
(...skipping 25 matching lines...) Expand all Loading... |
91 // Add extra in-memory elements to the top of the frame to match an actual | 99 // Add extra in-memory elements to the top of the frame to match an actual |
92 // frame (eg, the frame after an exception handler is pushed). No code is | 100 // frame (eg, the frame after an exception handler is pushed). No code is |
93 // emitted. | 101 // emitted. |
94 void Adjust(int count); | 102 void Adjust(int count); |
95 | 103 |
96 // Forget count elements from the top of the frame all in-memory | 104 // Forget count elements from the top of the frame all in-memory |
97 // (including synced) and adjust the stack pointer downward, to | 105 // (including synced) and adjust the stack pointer downward, to |
98 // match an external frame effect (examples include a call removing | 106 // match an external frame effect (examples include a call removing |
99 // its arguments, and exiting a try/catch removing an exception | 107 // its arguments, and exiting a try/catch removing an exception |
100 // handler). No code will be emitted. | 108 // handler). No code will be emitted. |
101 void Forget(int count); | 109 void Forget(int count) { |
| 110 ASSERT(count >= 0); |
| 111 ASSERT(stack_pointer_ == elements_.length() - 1); |
| 112 stack_pointer_ -= count; |
| 113 ForgetElements(count); |
| 114 } |
102 | 115 |
103 // Forget count elements from the top of the frame without adjusting | 116 // Forget count elements from the top of the frame without adjusting |
104 // the stack pointer downward. This is used, for example, before | 117 // the stack pointer downward. This is used, for example, before |
105 // merging frames at break, continue, and return targets. | 118 // merging frames at break, continue, and return targets. |
106 void ForgetElements(int count); | 119 void ForgetElements(int count); |
107 | 120 |
108 // Spill all values from the frame to memory. | 121 // Spill all values from the frame to memory. |
109 void SpillAll(); | 122 void SpillAll(); |
110 | 123 |
111 // Spill all occurrences of a specific register from the frame. | 124 // Spill all occurrences of a specific register from the frame. |
112 void Spill(Register reg); | 125 void Spill(Register reg) { |
| 126 if (is_used(reg)) SpillElementAt(register_index(reg)); |
| 127 } |
113 | 128 |
114 // Spill all occurrences of an arbitrary register if possible. Return the | 129 // Spill all occurrences of an arbitrary register if possible. Return the |
115 // register spilled or no_reg if it was not possible to free any register | 130 // register spilled or no_reg if it was not possible to free any register |
116 // (ie, they all have frame-external references). | 131 // (ie, they all have frame-external references). |
117 Register SpillAnyRegister(); | 132 Register SpillAnyRegister(); |
118 | 133 |
| 134 // Make this frame so that an arbitrary frame of the same height can |
| 135 // be merged to it. Copies and constants are removed from the |
| 136 // topmost mergable_elements elements of the frame. A |
| 137 // mergable_elements of JumpTarget::kAllElements indicates constants |
| 138 // and copies are should be removed from the entire frame. |
| 139 void MakeMergable(int mergable_elements); |
| 140 |
119 // Prepare this virtual frame for merging to an expected frame by | 141 // Prepare this virtual frame for merging to an expected frame by |
120 // performing some state changes that do not require generating | 142 // performing some state changes that do not require generating |
121 // code. It is guaranteed that no code will be generated. | 143 // code. It is guaranteed that no code will be generated. |
122 void PrepareMergeTo(VirtualFrame* expected); | 144 void PrepareMergeTo(VirtualFrame* expected); |
123 | 145 |
124 // Make this virtual frame have a state identical to an expected virtual | 146 // Make this virtual frame have a state identical to an expected virtual |
125 // frame. As a side effect, code may be emitted to make this frame match | 147 // frame. As a side effect, code may be emitted to make this frame match |
126 // the expected one. | 148 // the expected one. |
127 void MergeTo(VirtualFrame* expected); | 149 void MergeTo(VirtualFrame* expected); |
128 | 150 |
129 // Detach a frame from its code generator, perhaps temporarily. This | 151 // Detach a frame from its code generator, perhaps temporarily. This |
130 // tells the register allocator that it is free to use frame-internal | 152 // tells the register allocator that it is free to use frame-internal |
131 // registers. Used when the code generator's frame is switched from this | 153 // registers. Used when the code generator's frame is switched from this |
132 // one to NULL by an unconditional jump. | 154 // one to NULL by an unconditional jump. |
133 void DetachFromCodeGenerator() { | 155 void DetachFromCodeGenerator() { |
134 RegisterAllocator* cgen_allocator = cgen_->allocator(); | 156 RegisterAllocator* cgen_allocator = cgen_->allocator(); |
135 for (int i = 0; i < kNumRegisters; i++) { | 157 for (int i = 0; i < kNumRegisters; i++) { |
136 if (is_used(i)) { | 158 if (is_used(i)) { |
137 Register temp = { i }; | 159 Register temp = { i }; |
138 cgen_allocator->Unuse(temp); | 160 cgen_allocator->Unuse(temp); |
139 } | 161 } |
140 } | 162 } |
141 } | 163 } |
| 164 |
142 // (Re)attach a frame to its code generator. This informs the register | 165 // (Re)attach a frame to its code generator. This informs the register |
143 // allocator that the frame-internal register references are active again. | 166 // allocator that the frame-internal register references are active again. |
144 // Used when a code generator's frame is switched from NULL to this one by | 167 // Used when a code generator's frame is switched from NULL to this one by |
145 // binding a label. | 168 // binding a label. |
146 void AttachToCodeGenerator() { | 169 void AttachToCodeGenerator() { |
147 RegisterAllocator* cgen_allocator = cgen_->allocator(); | 170 RegisterAllocator* cgen_allocator = cgen_->allocator(); |
148 for (int i = 0; i < kNumRegisters; i++) { | 171 for (int i = 0; i < kNumRegisters; i++) { |
149 if (is_used(i)) { | 172 if (is_used(i)) { |
150 Register temp = { i }; | 173 Register temp = { i }; |
151 cgen_allocator->Use(temp); | 174 cgen_allocator->Use(temp); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 } | 278 } |
256 | 279 |
257 // The receiver frame slot. | 280 // The receiver frame slot. |
258 Operand Receiver() const { return ParameterAt(-1); } | 281 Operand Receiver() const { return ParameterAt(-1); } |
259 | 282 |
260 // Push a try-catch or try-finally handler on top of the virtual frame. | 283 // Push a try-catch or try-finally handler on top of the virtual frame. |
261 void PushTryHandler(HandlerType type); | 284 void PushTryHandler(HandlerType type); |
262 | 285 |
263 // Call stub given the number of arguments it expects on (and | 286 // Call stub given the number of arguments it expects on (and |
264 // removes from) the stack. | 287 // removes from) the stack. |
265 Result CallStub(CodeStub* stub, int arg_count); | 288 Result CallStub(CodeStub* stub, int arg_count) { |
| 289 PrepareForCall(arg_count, arg_count); |
| 290 return RawCallStub(stub); |
| 291 } |
266 | 292 |
267 // Call stub that takes a single argument passed in eax. The | 293 // Call stub that takes a single argument passed in eax. The |
268 // argument is given as a result which does not have to be eax or | 294 // argument is given as a result which does not have to be eax or |
269 // even a register. The argument is consumed by the call. | 295 // even a register. The argument is consumed by the call. |
270 Result CallStub(CodeStub* stub, Result* arg); | 296 Result CallStub(CodeStub* stub, Result* arg); |
271 | 297 |
272 // Call stub that takes a pair of arguments passed in edx (arg0) and | 298 // Call stub that takes a pair of arguments passed in edx (arg0) and |
273 // eax (arg1). The arguments are given as results which do not have | 299 // eax (arg1). The arguments are given as results which do not have |
274 // to be in the proper registers or even in registers. The | 300 // to be in the proper registers or even in registers. The |
275 // arguments are consumed by the call. | 301 // arguments are consumed by the call. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 void EmitPush(Operand operand); | 365 void EmitPush(Operand operand); |
340 void EmitPush(Immediate immediate); | 366 void EmitPush(Immediate immediate); |
341 | 367 |
342 // Push an element on the virtual frame. | 368 // Push an element on the virtual frame. |
343 void Push(Register reg, StaticType static_type = StaticType()); | 369 void Push(Register reg, StaticType static_type = StaticType()); |
344 void Push(Handle<Object> value); | 370 void Push(Handle<Object> value); |
345 void Push(Smi* value) { Push(Handle<Object>(value)); } | 371 void Push(Smi* value) { Push(Handle<Object>(value)); } |
346 | 372 |
347 // Pushing a result invalidates it (its contents become owned by the | 373 // Pushing a result invalidates it (its contents become owned by the |
348 // frame). | 374 // frame). |
349 void Push(Result* result); | 375 void Push(Result* result) { |
| 376 if (result->is_register()) { |
| 377 Push(result->reg(), result->static_type()); |
| 378 } else { |
| 379 ASSERT(result->is_constant()); |
| 380 Push(result->handle()); |
| 381 } |
| 382 result->Unuse(); |
| 383 } |
350 | 384 |
351 // Nip removes zero or more elements from immediately below the top | 385 // Nip removes zero or more elements from immediately below the top |
352 // of the frame, leaving the previous top-of-frame value on top of | 386 // of the frame, leaving the previous top-of-frame value on top of |
353 // the frame. Nip(k) is equivalent to x = Pop(), Drop(k), Push(x). | 387 // the frame. Nip(k) is equivalent to x = Pop(), Drop(k), Push(x). |
354 void Nip(int num_dropped); | 388 void Nip(int num_dropped); |
355 | 389 |
356 private: | 390 private: |
357 static const int kLocal0Offset = JavaScriptFrameConstants::kLocal0Offset; | 391 static const int kLocal0Offset = JavaScriptFrameConstants::kLocal0Offset; |
358 static const int kFunctionOffset = JavaScriptFrameConstants::kFunctionOffset; | 392 static const int kFunctionOffset = JavaScriptFrameConstants::kFunctionOffset; |
359 static const int kContextOffset = StandardFrameConstants::kContextOffset; | 393 static const int kContextOffset = StandardFrameConstants::kContextOffset; |
360 | 394 |
361 static const int kHandlerSize = StackHandlerConstants::kSize / kPointerSize; | 395 static const int kHandlerSize = StackHandlerConstants::kSize / kPointerSize; |
362 static const int kPreallocatedElements = 5 + 8; // 8 expression stack slots. | 396 static const int kPreallocatedElements = 5 + 8; // 8 expression stack slots. |
363 | 397 |
364 CodeGenerator* cgen_; | 398 CodeGenerator* cgen_; |
365 MacroAssembler* masm_; | 399 MacroAssembler* masm_; |
366 | 400 |
367 List<FrameElement> elements_; | 401 ZoneList<FrameElement> elements_; |
368 | 402 |
369 // The number of frame-allocated locals and parameters respectively. | 403 // The number of frame-allocated locals and parameters respectively. |
370 int parameter_count_; | 404 int16_t parameter_count_; |
371 int local_count_; | 405 int16_t local_count_; |
372 | 406 |
373 // The index of the element that is at the processor's stack pointer | 407 // The index of the element that is at the processor's stack pointer |
374 // (the esp register). | 408 // (the esp register). |
375 int stack_pointer_; | 409 int16_t stack_pointer_; |
376 | 410 |
377 // The index of the element that is at the processor's frame pointer | 411 // The index of the element that is at the processor's frame pointer |
378 // (the ebp register). | 412 // (the ebp register). |
379 int frame_pointer_; | 413 int16_t frame_pointer_; |
380 | 414 |
381 // The index of the register frame element using each register, or | 415 // The index of the register frame element using each register, or |
382 // kIllegalIndex if a register is not on the frame. | 416 // kIllegalIndex if a register is not on the frame. |
383 int register_locations_[kNumRegisters]; | 417 int16_t register_locations_[kNumRegisters]; |
384 | 418 |
385 // The index of the first parameter. The receiver lies below the first | 419 // The index of the first parameter. The receiver lies below the first |
386 // parameter. | 420 // parameter. |
387 int param0_index() const { return 1; } | 421 int param0_index() const { return 1; } |
388 | 422 |
389 // The index of the context slot in the frame. | 423 // The index of the context slot in the frame. |
390 int context_index() const { | 424 int context_index() const { |
391 ASSERT(frame_pointer_ != kIllegalIndex); | 425 ASSERT(frame_pointer_ != kIllegalIndex); |
392 return frame_pointer_ + 1; | 426 return frame_pointer_ + 1; |
393 } | 427 } |
(...skipping 18 matching lines...) Expand all Loading... |
412 | 446 |
413 // Convert a frame index into a frame pointer relative offset into the | 447 // Convert a frame index into a frame pointer relative offset into the |
414 // actual stack. | 448 // actual stack. |
415 int fp_relative(int index) const { | 449 int fp_relative(int index) const { |
416 return (frame_pointer_ - index) * kPointerSize; | 450 return (frame_pointer_ - index) * kPointerSize; |
417 } | 451 } |
418 | 452 |
419 // Record an occurrence of a register in the virtual frame. This has the | 453 // Record an occurrence of a register in the virtual frame. This has the |
420 // effect of incrementing the register's external reference count and | 454 // effect of incrementing the register's external reference count and |
421 // of updating the index of the register's location in the frame. | 455 // of updating the index of the register's location in the frame. |
422 void Use(Register reg, int index); | 456 void Use(Register reg, int index) { |
| 457 ASSERT(!is_used(reg)); |
| 458 register_locations_[reg.code()] = index; |
| 459 cgen_->allocator()->Use(reg); |
| 460 } |
423 | 461 |
424 // Record that a register reference has been dropped from the frame. This | 462 // Record that a register reference has been dropped from the frame. This |
425 // decrements the register's external reference count and invalidates the | 463 // decrements the register's external reference count and invalidates the |
426 // index of the register's location in the frame. | 464 // index of the register's location in the frame. |
427 void Unuse(Register reg); | 465 void Unuse(Register reg) { |
| 466 ASSERT(register_locations_[reg.code()] != kIllegalIndex); |
| 467 register_locations_[reg.code()] = kIllegalIndex; |
| 468 cgen_->allocator()->Unuse(reg); |
| 469 } |
428 | 470 |
429 // Spill the element at a particular index---write it to memory if | 471 // Spill the element at a particular index---write it to memory if |
430 // necessary, free any associated register, and forget its value if | 472 // necessary, free any associated register, and forget its value if |
431 // constant. | 473 // constant. |
432 void SpillElementAt(int index); | 474 void SpillElementAt(int index); |
433 | 475 |
434 // Sync the element at a particular index. If it is a register or | 476 // Sync the element at a particular index. If it is a register or |
435 // constant that disagrees with the value on the stack, write it to memory. | 477 // constant that disagrees with the value on the stack, write it to memory. |
436 // Keep the element type as register or constant, and clear the dirty bit. | 478 // Keep the element type as register or constant, and clear the dirty bit. |
437 void SyncElementAt(int index); | 479 void SyncElementAt(int index); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
499 Result RawCallCodeObject(Handle<Code> code, RelocInfo::Mode rmode); | 541 Result RawCallCodeObject(Handle<Code> code, RelocInfo::Mode rmode); |
500 | 542 |
501 bool Equals(VirtualFrame* other); | 543 bool Equals(VirtualFrame* other); |
502 | 544 |
503 friend class JumpTarget; | 545 friend class JumpTarget; |
504 }; | 546 }; |
505 | 547 |
506 } } // namespace v8::internal | 548 } } // namespace v8::internal |
507 | 549 |
508 #endif // V8_X64_VIRTUAL_FRAME_X64_H_ | 550 #endif // V8_X64_VIRTUAL_FRAME_X64_H_ |
OLD | NEW |