blob: e4923050792bccd23b04a25dc709e9708c4d4020 [file] [log] [blame]
ager@chromium.org5ec48922009-05-05 07:25:34 +00001// 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.org9085a012009-05-11 19:22:57 +000028#ifndef V8_X64_VIRTUAL_FRAME_X64_H_
29#define V8_X64_VIRTUAL_FRAME_X64_H_
30
31#include "register-allocator.h"
kasperl@chromium.org71affb52009-05-26 05:44:31 +000032#include "scopes.h"
ager@chromium.org9085a012009-05-11 19:22:57 +000033
kasperl@chromium.org71affb52009-05-26 05:44:31 +000034namespace v8 {
35namespace internal {
ager@chromium.org9085a012009-05-11 19:22:57 +000036
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
kasperl@chromium.org71affb52009-05-26 05:44:31 +000046class VirtualFrame : public ZoneObject {
ager@chromium.org9085a012009-05-11 19:22:57 +000047 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:
kasperl@chromium.org71affb52009-05-26 05:44:31 +000055 SpilledScope() : previous_state_(cgen()->in_spilled_code()) {
56 ASSERT(cgen()->has_valid_frame());
57 cgen()->frame()->SpillAll();
58 cgen()->set_in_spilled_code(true);
59 }
ager@chromium.org9085a012009-05-11 19:22:57 +000060
kasperl@chromium.org71affb52009-05-26 05:44:31 +000061 ~SpilledScope() {
62 cgen()->set_in_spilled_code(previous_state_);
63 }
ager@chromium.org9085a012009-05-11 19:22:57 +000064
65 private:
ager@chromium.org9085a012009-05-11 19:22:57 +000066 bool previous_state_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000067
68 CodeGenerator* cgen() { return CodeGeneratorScope::Current(); }
ager@chromium.org9085a012009-05-11 19:22:57 +000069 };
70
71 // An illegal index into the virtual frame.
72 static const int kIllegalIndex = -1;
73
74 // Construct an initial virtual frame on entry to a JS function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +000075 VirtualFrame();
ager@chromium.org9085a012009-05-11 19:22:57 +000076
77 // Construct a virtual frame as a clone of an existing one.
78 explicit VirtualFrame(VirtualFrame* original);
79
kasperl@chromium.org71affb52009-05-26 05:44:31 +000080 CodeGenerator* cgen() { return CodeGeneratorScope::Current(); }
81 MacroAssembler* masm() { return cgen()->masm(); }
82
ager@chromium.org9085a012009-05-11 19:22:57 +000083 // Create a duplicate of an existing valid frame element.
84 FrameElement CopyElementAt(int index);
85
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000086 // The number of elements on the virtual frame.
87 int element_count() { return elements_.length(); }
88
ager@chromium.org9085a012009-05-11 19:22:57 +000089 // The height of the virtual expression stack.
kasperl@chromium.org71affb52009-05-26 05:44:31 +000090 int height() {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000091 return element_count() - expression_base_index();
ager@chromium.org9085a012009-05-11 19:22:57 +000092 }
93
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000094 int register_location(int num) {
95 ASSERT(num >= 0 && num < RegisterAllocator::kNumRegisters);
96 return register_locations_[num];
ager@chromium.org9085a012009-05-11 19:22:57 +000097 }
98
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000099 int register_location(Register reg) {
100 return register_locations_[RegisterAllocator::ToNumber(reg)];
101 }
102
103 void set_register_location(Register reg, int index) {
104 register_locations_[RegisterAllocator::ToNumber(reg)] = index;
105 }
106
107 bool is_used(int num) {
108 ASSERT(num >= 0 && num < RegisterAllocator::kNumRegisters);
109 return register_locations_[num] != kIllegalIndex;
ager@chromium.org9085a012009-05-11 19:22:57 +0000110 }
111
112 bool is_used(Register reg) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000113 return register_locations_[RegisterAllocator::ToNumber(reg)]
114 != kIllegalIndex;
ager@chromium.org9085a012009-05-11 19:22:57 +0000115 }
116
117 // Add extra in-memory elements to the top of the frame to match an actual
118 // frame (eg, the frame after an exception handler is pushed). No code is
119 // emitted.
120 void Adjust(int count);
121
122 // Forget count elements from the top of the frame all in-memory
123 // (including synced) and adjust the stack pointer downward, to
124 // match an external frame effect (examples include a call removing
125 // its arguments, and exiting a try/catch removing an exception
126 // handler). No code will be emitted.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000127 void Forget(int count) {
128 ASSERT(count >= 0);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000129 ASSERT(stack_pointer_ == element_count() - 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000130 stack_pointer_ -= count;
131 ForgetElements(count);
132 }
ager@chromium.org9085a012009-05-11 19:22:57 +0000133
134 // Forget count elements from the top of the frame without adjusting
135 // the stack pointer downward. This is used, for example, before
136 // merging frames at break, continue, and return targets.
137 void ForgetElements(int count);
138
139 // Spill all values from the frame to memory.
140 void SpillAll();
141
142 // Spill all occurrences of a specific register from the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000143 void Spill(Register reg) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000144 if (is_used(reg)) SpillElementAt(register_location(reg));
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000145 }
ager@chromium.org9085a012009-05-11 19:22:57 +0000146
147 // Spill all occurrences of an arbitrary register if possible. Return the
148 // register spilled or no_reg if it was not possible to free any register
149 // (ie, they all have frame-external references).
150 Register SpillAnyRegister();
151
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000152 // Sync the range of elements in [begin, end] with memory.
153 void SyncRange(int begin, int end);
154
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000155 // Make this frame so that an arbitrary frame of the same height can
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000156 // be merged to it. Copies and constants are removed from the frame.
157 void MakeMergable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000158
ager@chromium.org9085a012009-05-11 19:22:57 +0000159 // Prepare this virtual frame for merging to an expected frame by
160 // performing some state changes that do not require generating
161 // code. It is guaranteed that no code will be generated.
162 void PrepareMergeTo(VirtualFrame* expected);
163
164 // Make this virtual frame have a state identical to an expected virtual
165 // frame. As a side effect, code may be emitted to make this frame match
166 // the expected one.
167 void MergeTo(VirtualFrame* expected);
168
169 // Detach a frame from its code generator, perhaps temporarily. This
170 // tells the register allocator that it is free to use frame-internal
171 // registers. Used when the code generator's frame is switched from this
172 // one to NULL by an unconditional jump.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000173 void DetachFromCodeGenerator() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000174 RegisterAllocator* cgen_allocator = cgen()->allocator();
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000175 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) {
176 if (is_used(i)) cgen_allocator->Unuse(i);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000177 }
178 }
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000179
ager@chromium.org9085a012009-05-11 19:22:57 +0000180 // (Re)attach a frame to its code generator. This informs the register
181 // allocator that the frame-internal register references are active again.
182 // Used when a code generator's frame is switched from NULL to this one by
183 // binding a label.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000184 void AttachToCodeGenerator() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000185 RegisterAllocator* cgen_allocator = cgen()->allocator();
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000186 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) {
187 if (is_used(i)) cgen_allocator->Use(i);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000188 }
189 }
ager@chromium.org9085a012009-05-11 19:22:57 +0000190
191 // Emit code for the physical JS entry and exit frame sequences. After
192 // calling Enter, the virtual frame is ready for use; and after calling
193 // Exit it should not be used. Note that Enter does not allocate space in
194 // the physical frame for storing frame-allocated locals.
195 void Enter();
196 void Exit();
197
198 // Prepare for returning from the frame by spilling locals. This
199 // avoids generating unnecessary merge code when jumping to the
200 // shared return site. Emits code for spills.
201 void PrepareForReturn();
202
203 // Allocate and initialize the frame-allocated locals.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000204 void AllocateStackSlots();
ager@chromium.org9085a012009-05-11 19:22:57 +0000205
206 // An element of the expression stack as an assembly operand.
207 Operand ElementAt(int index) const {
208 return Operand(rsp, index * kPointerSize);
209 }
210
211 // Random-access store to a frame-top relative frame element. The result
212 // becomes owned by the frame and is invalidated.
213 void SetElementAt(int index, Result* value);
214
215 // Set a frame element to a constant. The index is frame-top relative.
216 void SetElementAt(int index, Handle<Object> value) {
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000217 Result temp(value);
ager@chromium.org9085a012009-05-11 19:22:57 +0000218 SetElementAt(index, &temp);
219 }
220
221 void PushElementAt(int index) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000222 PushFrameSlotAt(element_count() - index - 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000223 }
224
225 void StoreToElementAt(int index) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000226 StoreToFrameSlotAt(element_count() - index - 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000227 }
228
229 // A frame-allocated local as an assembly operand.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000230 Operand LocalAt(int index) {
ager@chromium.org9085a012009-05-11 19:22:57 +0000231 ASSERT(0 <= index);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000232 ASSERT(index < local_count());
ager@chromium.org9085a012009-05-11 19:22:57 +0000233 return Operand(rbp, kLocal0Offset - index * kPointerSize);
234 }
235
236 // Push a copy of the value of a local frame slot on top of the frame.
237 void PushLocalAt(int index) {
238 PushFrameSlotAt(local0_index() + index);
239 }
240
241 // Push the value of a local frame slot on top of the frame and invalidate
242 // the local slot. The slot should be written to before trying to read
243 // from it again.
244 void TakeLocalAt(int index) {
245 TakeFrameSlotAt(local0_index() + index);
246 }
247
248 // Store the top value on the virtual frame into a local frame slot. The
249 // value is left in place on top of the frame.
250 void StoreToLocalAt(int index) {
251 StoreToFrameSlotAt(local0_index() + index);
252 }
253
254 // Push the address of the receiver slot on the frame.
255 void PushReceiverSlotAddress();
256
257 // Push the function on top of the frame.
258 void PushFunction() { PushFrameSlotAt(function_index()); }
259
260 // Save the value of the esi register to the context frame slot.
261 void SaveContextRegister();
262
263 // Restore the esi register from the value of the context frame
264 // slot.
265 void RestoreContextRegister();
266
267 // A parameter as an assembly operand.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000268 Operand ParameterAt(int index) {
ager@chromium.org9085a012009-05-11 19:22:57 +0000269 ASSERT(-1 <= index); // -1 is the receiver.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000270 ASSERT(index < parameter_count());
271 return Operand(rbp, (1 + parameter_count() - index) * kPointerSize);
ager@chromium.org9085a012009-05-11 19:22:57 +0000272 }
273
274 // Push a copy of the value of a parameter frame slot on top of the frame.
275 void PushParameterAt(int index) {
276 PushFrameSlotAt(param0_index() + index);
277 }
278
279 // Push the value of a paramter frame slot on top of the frame and
280 // invalidate the parameter slot. The slot should be written to before
281 // trying to read from it again.
282 void TakeParameterAt(int index) {
283 TakeFrameSlotAt(param0_index() + index);
284 }
285
286 // Store the top value on the virtual frame into a parameter frame slot.
287 // The value is left in place on top of the frame.
288 void StoreToParameterAt(int index) {
289 StoreToFrameSlotAt(param0_index() + index);
290 }
291
292 // The receiver frame slot.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000293 Operand Receiver() { return ParameterAt(-1); }
ager@chromium.org9085a012009-05-11 19:22:57 +0000294
295 // Push a try-catch or try-finally handler on top of the virtual frame.
296 void PushTryHandler(HandlerType type);
297
298 // Call stub given the number of arguments it expects on (and
299 // removes from) the stack.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000300 Result CallStub(CodeStub* stub, int arg_count) {
301 PrepareForCall(arg_count, arg_count);
302 return RawCallStub(stub);
303 }
ager@chromium.org9085a012009-05-11 19:22:57 +0000304
305 // Call stub that takes a single argument passed in eax. The
306 // argument is given as a result which does not have to be eax or
307 // even a register. The argument is consumed by the call.
308 Result CallStub(CodeStub* stub, Result* arg);
309
ager@chromium.org3e875802009-06-29 08:26:34 +0000310 // Call stub that takes a pair of arguments passed in edx (arg0, rdx) and
311 // eax (arg1, rax). The arguments are given as results which do not have
ager@chromium.org9085a012009-05-11 19:22:57 +0000312 // to be in the proper registers or even in registers. The
313 // arguments are consumed by the call.
314 Result CallStub(CodeStub* stub, Result* arg0, Result* arg1);
315
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
321 // Invoke builtin given the number of arguments it expects on (and
322 // removes from) the stack.
323 Result InvokeBuiltin(Builtins::JavaScript id,
324 InvokeFlag flag,
325 int arg_count);
326
327 // Call load IC. Name and receiver are found on top of the frame.
328 // Receiver is not dropped.
329 Result CallLoadIC(RelocInfo::Mode mode);
330
331 // Call keyed load IC. Key and receiver are found on top of the
332 // frame. They are not dropped.
333 Result CallKeyedLoadIC(RelocInfo::Mode mode);
334
335 // Call store IC. Name, value, and receiver are found on top of the
336 // frame. Receiver is not dropped.
337 Result CallStoreIC();
338
339 // Call keyed store IC. Value, key, and receiver are found on top
340 // of the frame. Key and receiver are not dropped.
341 Result CallKeyedStoreIC();
342
343 // Call call IC. Arguments, reciever, and function name are found
344 // on top of the frame. Function name slot is not dropped. The
345 // argument count does not include the receiver.
346 Result CallCallIC(RelocInfo::Mode mode, int arg_count, int loop_nesting);
347
348 // Allocate and call JS function as constructor. Arguments,
349 // receiver (global object), and function are found on top of the
350 // frame. Function is not dropped. The argument count does not
351 // include the receiver.
352 Result CallConstructor(int arg_count);
353
354 // Drop a number of elements from the top of the expression stack. May
355 // emit code to affect the physical frame. Does not clobber any registers
356 // excepting possibly the stack pointer.
357 void Drop(int count);
358
359 // Drop one element.
360 void Drop() { Drop(1); }
361
362 // Duplicate the top element of the frame.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000363 void Dup() { PushFrameSlotAt(element_count() - 1); }
ager@chromium.org9085a012009-05-11 19:22:57 +0000364
365 // Pop an element from the top of the expression stack. Returns a
366 // Result, which may be a constant or a register.
367 Result Pop();
368
369 // Pop and save an element from the top of the expression stack and
370 // emit a corresponding pop instruction.
371 void EmitPop(Register reg);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000372 void EmitPop(const Operand& operand);
ager@chromium.org9085a012009-05-11 19:22:57 +0000373
374 // Push an element on top of the expression stack and emit a
375 // corresponding push instruction.
376 void EmitPush(Register reg);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000377 void EmitPush(const Operand& operand);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000378 void EmitPush(Heap::RootListIndex index);
ager@chromium.org9085a012009-05-11 19:22:57 +0000379 void EmitPush(Immediate immediate);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000380 void EmitPush(Smi* value);
ager@chromium.org3e875802009-06-29 08:26:34 +0000381 // Uses kScratchRegister, emits appropriate relocation info.
382 void EmitPush(Handle<Object> value);
ager@chromium.org9085a012009-05-11 19:22:57 +0000383
384 // Push an element on the virtual frame.
ager@chromium.org3e875802009-06-29 08:26:34 +0000385 void Push(Register reg);
ager@chromium.org9085a012009-05-11 19:22:57 +0000386 void Push(Handle<Object> value);
387 void Push(Smi* value) { Push(Handle<Object>(value)); }
388
389 // Pushing a result invalidates it (its contents become owned by the
390 // frame).
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000391 void Push(Result* result) {
392 if (result->is_register()) {
ager@chromium.org3e875802009-06-29 08:26:34 +0000393 Push(result->reg());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000394 } else {
395 ASSERT(result->is_constant());
396 Push(result->handle());
397 }
398 result->Unuse();
399 }
ager@chromium.org9085a012009-05-11 19:22:57 +0000400
401 // Nip removes zero or more elements from immediately below the top
402 // of the frame, leaving the previous top-of-frame value on top of
403 // the frame. Nip(k) is equivalent to x = Pop(), Drop(k), Push(x).
404 void Nip(int num_dropped);
405
406 private:
407 static const int kLocal0Offset = JavaScriptFrameConstants::kLocal0Offset;
408 static const int kFunctionOffset = JavaScriptFrameConstants::kFunctionOffset;
409 static const int kContextOffset = StandardFrameConstants::kContextOffset;
410
411 static const int kHandlerSize = StackHandlerConstants::kSize / kPointerSize;
412 static const int kPreallocatedElements = 5 + 8; // 8 expression stack slots.
413
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000414 ZoneList<FrameElement> elements_;
ager@chromium.org9085a012009-05-11 19:22:57 +0000415
416 // The index of the element that is at the processor's stack pointer
417 // (the esp register).
418 int stack_pointer_;
419
ager@chromium.org9085a012009-05-11 19:22:57 +0000420 // The index of the register frame element using each register, or
421 // kIllegalIndex if a register is not on the frame.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000422 int register_locations_[RegisterAllocator::kNumRegisters];
ager@chromium.org9085a012009-05-11 19:22:57 +0000423
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000424 // The number of frame-allocated locals and parameters respectively.
425 int parameter_count() { return cgen()->scope()->num_parameters(); }
426 int local_count() { return cgen()->scope()->num_stack_slots(); }
427
428 // The index of the element that is at the processor's frame pointer
429 // (the ebp register). The parameters, receiver, and return address
430 // are below the frame pointer.
431 int frame_pointer() { return parameter_count() + 2; }
432
ager@chromium.org9085a012009-05-11 19:22:57 +0000433 // The index of the first parameter. The receiver lies below the first
434 // parameter.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000435 int param0_index() { return 1; }
ager@chromium.org9085a012009-05-11 19:22:57 +0000436
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000437 // The index of the context slot in the frame. It is immediately
438 // above the frame pointer.
439 int context_index() { return frame_pointer() + 1; }
ager@chromium.org9085a012009-05-11 19:22:57 +0000440
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000441 // The index of the function slot in the frame. It is above the frame
442 // pointer and the context slot.
443 int function_index() { return frame_pointer() + 2; }
ager@chromium.org9085a012009-05-11 19:22:57 +0000444
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000445 // The index of the first local. Between the frame pointer and the
446 // locals lie the context and the function.
447 int local0_index() { return frame_pointer() + 3; }
ager@chromium.org9085a012009-05-11 19:22:57 +0000448
449 // The index of the base of the expression stack.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000450 int expression_base_index() { return local0_index() + local_count(); }
ager@chromium.org9085a012009-05-11 19:22:57 +0000451
452 // Convert a frame index into a frame pointer relative offset into the
453 // actual stack.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000454 int fp_relative(int index) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000455 ASSERT(index < element_count());
456 ASSERT(frame_pointer() < element_count()); // FP is on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000457 return (frame_pointer() - index) * kPointerSize;
ager@chromium.org9085a012009-05-11 19:22:57 +0000458 }
459
460 // Record an occurrence of a register in the virtual frame. This has the
461 // effect of incrementing the register's external reference count and
462 // of updating the index of the register's location in the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000463 void Use(Register reg, int index) {
464 ASSERT(!is_used(reg));
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000465 set_register_location(reg, index);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000466 cgen()->allocator()->Use(reg);
467 }
ager@chromium.org9085a012009-05-11 19:22:57 +0000468
469 // Record that a register reference has been dropped from the frame. This
470 // decrements the register's external reference count and invalidates the
471 // index of the register's location in the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000472 void Unuse(Register reg) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000473 ASSERT(is_used(reg));
474 set_register_location(reg, kIllegalIndex);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000475 cgen()->allocator()->Unuse(reg);
476 }
ager@chromium.org9085a012009-05-11 19:22:57 +0000477
478 // Spill the element at a particular index---write it to memory if
479 // necessary, free any associated register, and forget its value if
480 // constant.
481 void SpillElementAt(int index);
482
483 // Sync the element at a particular index. If it is a register or
484 // constant that disagrees with the value on the stack, write it to memory.
485 // Keep the element type as register or constant, and clear the dirty bit.
486 void SyncElementAt(int index);
487
ager@chromium.org9085a012009-05-11 19:22:57 +0000488 // Sync a single unsynced element that lies beneath or at the stack pointer.
489 void SyncElementBelowStackPointer(int index);
490
491 // Sync a single unsynced element that lies just above the stack pointer.
492 void SyncElementByPushing(int index);
493
494 // Push a copy of a frame slot (typically a local or parameter) on top of
495 // the frame.
496 void PushFrameSlotAt(int index);
497
498 // Push a the value of a frame slot (typically a local or parameter) on
499 // top of the frame and invalidate the slot.
500 void TakeFrameSlotAt(int index);
501
502 // Store the value on top of the frame to a frame slot (typically a local
503 // or parameter).
504 void StoreToFrameSlotAt(int index);
505
506 // Spill all elements in registers. Spill the top spilled_args elements
507 // on the frame. Sync all other frame elements.
508 // Then drop dropped_args elements from the virtual frame, to match
509 // the effect of an upcoming call that will drop them from the stack.
510 void PrepareForCall(int spilled_args, int dropped_args);
511
512 // Move frame elements currently in registers or constants, that
513 // should be in memory in the expected frame, to memory.
514 void MergeMoveRegistersToMemory(VirtualFrame* expected);
515
516 // Make the register-to-register moves necessary to
517 // merge this frame with the expected frame.
518 // Register to memory moves must already have been made,
519 // and memory to register moves must follow this call.
520 // This is because some new memory-to-register moves are
521 // created in order to break cycles of register moves.
522 // Used in the implementation of MergeTo().
523 void MergeMoveRegistersToRegisters(VirtualFrame* expected);
524
525 // Make the memory-to-register and constant-to-register moves
526 // needed to make this frame equal the expected frame.
527 // Called after all register-to-memory and register-to-register
528 // moves have been made. After this function returns, the frames
529 // should be equal.
530 void MergeMoveMemoryToRegisters(VirtualFrame* expected);
531
532 // Invalidates a frame slot (puts an invalid frame element in it).
533 // Copies on the frame are correctly handled, and if this slot was
534 // the backing store of copies, the index of the new backing store
535 // is returned. Otherwise, returns kIllegalIndex.
536 // Register counts are correctly updated.
537 int InvalidateFrameSlotAt(int index);
538
539 // Call a code stub that has already been prepared for calling (via
540 // PrepareForCall).
541 Result RawCallStub(CodeStub* stub);
542
543 // Calls a code object which has already been prepared for calling
544 // (via PrepareForCall).
545 Result RawCallCodeObject(Handle<Code> code, RelocInfo::Mode rmode);
546
547 bool Equals(VirtualFrame* other);
548
ager@chromium.orge2902be2009-06-08 12:21:35 +0000549 // Classes that need raw access to the elements_ array.
550 friend class DeferredCode;
ager@chromium.org9085a012009-05-11 19:22:57 +0000551 friend class JumpTarget;
552};
553
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000554
ager@chromium.org9085a012009-05-11 19:22:57 +0000555} } // namespace v8::internal
556
557#endif // V8_X64_VIRTUAL_FRAME_X64_H_