blob: 0479ff0c124487fbe7de8a70498498c0427ee745 [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
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000031#include "type-info.h"
ager@chromium.org9085a012009-05-11 19:22:57 +000032#include "register-allocator.h"
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033#include "scopes.h"
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000034#include "codegen.h"
ager@chromium.org9085a012009-05-11 19:22:57 +000035
kasperl@chromium.org71affb52009-05-26 05:44:31 +000036namespace v8 {
37namespace internal {
ager@chromium.org9085a012009-05-11 19:22:57 +000038
39// -------------------------------------------------------------------------
40// Virtual frames
41//
42// The virtual frame is an abstraction of the physical stack frame. It
43// encapsulates the parameters, frame-allocated locals, and the expression
44// stack. It supports push/pop operations on the expression stack, as well
45// as random access to the expression stack elements, locals, and
46// parameters.
47
kasperl@chromium.org71affb52009-05-26 05:44:31 +000048class VirtualFrame : public ZoneObject {
ager@chromium.org9085a012009-05-11 19:22:57 +000049 public:
50 // A utility class to introduce a scope where the virtual frame is
51 // expected to remain spilled. The constructor spills the code
52 // generator's current frame, but no attempt is made to require it
53 // to stay spilled. It is intended as documentation while the code
54 // generator is being transformed.
55 class SpilledScope BASE_EMBEDDED {
56 public:
kasperl@chromium.org71affb52009-05-26 05:44:31 +000057 SpilledScope() : previous_state_(cgen()->in_spilled_code()) {
58 ASSERT(cgen()->has_valid_frame());
59 cgen()->frame()->SpillAll();
60 cgen()->set_in_spilled_code(true);
61 }
ager@chromium.org9085a012009-05-11 19:22:57 +000062
kasperl@chromium.org71affb52009-05-26 05:44:31 +000063 ~SpilledScope() {
64 cgen()->set_in_spilled_code(previous_state_);
65 }
ager@chromium.org9085a012009-05-11 19:22:57 +000066
67 private:
ager@chromium.org9085a012009-05-11 19:22:57 +000068 bool previous_state_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +000069
70 CodeGenerator* cgen() { return CodeGeneratorScope::Current(); }
ager@chromium.org9085a012009-05-11 19:22:57 +000071 };
72
73 // An illegal index into the virtual frame.
74 static const int kIllegalIndex = -1;
75
76 // Construct an initial virtual frame on entry to a JS function.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +000077 inline VirtualFrame();
ager@chromium.org9085a012009-05-11 19:22:57 +000078
79 // Construct a virtual frame as a clone of an existing one.
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000080 explicit inline VirtualFrame(VirtualFrame* original);
ager@chromium.org9085a012009-05-11 19:22:57 +000081
kasperl@chromium.org71affb52009-05-26 05:44:31 +000082 CodeGenerator* cgen() { return CodeGeneratorScope::Current(); }
83 MacroAssembler* masm() { return cgen()->masm(); }
84
ager@chromium.org9085a012009-05-11 19:22:57 +000085 // Create a duplicate of an existing valid frame element.
ager@chromium.org5c838252010-02-19 08:53:10 +000086 FrameElement CopyElementAt(int index,
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +000087 TypeInfo info = TypeInfo::Uninitialized());
ager@chromium.org9085a012009-05-11 19:22:57 +000088
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000089 // The number of elements on the virtual frame.
90 int element_count() { return elements_.length(); }
91
ager@chromium.org9085a012009-05-11 19:22:57 +000092 // The height of the virtual expression stack.
kasperl@chromium.org71affb52009-05-26 05:44:31 +000093 int height() {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000094 return element_count() - expression_base_index();
ager@chromium.org9085a012009-05-11 19:22:57 +000095 }
96
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +000097 int register_location(int num) {
98 ASSERT(num >= 0 && num < RegisterAllocator::kNumRegisters);
99 return register_locations_[num];
ager@chromium.org9085a012009-05-11 19:22:57 +0000100 }
101
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000102 inline int register_location(Register reg);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000103
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000104 inline void set_register_location(Register reg, int index);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000105
106 bool is_used(int num) {
107 ASSERT(num >= 0 && num < RegisterAllocator::kNumRegisters);
108 return register_locations_[num] != kIllegalIndex;
ager@chromium.org9085a012009-05-11 19:22:57 +0000109 }
110
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000111 inline bool is_used(Register reg);
ager@chromium.org9085a012009-05-11 19:22:57 +0000112
113 // Add extra in-memory elements to the top of the frame to match an actual
114 // frame (eg, the frame after an exception handler is pushed). No code is
115 // emitted.
116 void Adjust(int count);
117
118 // Forget count elements from the top of the frame all in-memory
119 // (including synced) and adjust the stack pointer downward, to
120 // match an external frame effect (examples include a call removing
121 // its arguments, and exiting a try/catch removing an exception
122 // handler). No code will be emitted.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000123 void Forget(int count) {
124 ASSERT(count >= 0);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000125 ASSERT(stack_pointer_ == element_count() - 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000126 stack_pointer_ -= count;
127 ForgetElements(count);
128 }
ager@chromium.org9085a012009-05-11 19:22:57 +0000129
130 // Forget count elements from the top of the frame without adjusting
131 // the stack pointer downward. This is used, for example, before
132 // merging frames at break, continue, and return targets.
133 void ForgetElements(int count);
134
135 // Spill all values from the frame to memory.
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000136 inline void SpillAll();
ager@chromium.org9085a012009-05-11 19:22:57 +0000137
138 // Spill all occurrences of a specific register from the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000139 void Spill(Register reg) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000140 if (is_used(reg)) SpillElementAt(register_location(reg));
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000141 }
ager@chromium.org9085a012009-05-11 19:22:57 +0000142
143 // Spill all occurrences of an arbitrary register if possible. Return the
144 // register spilled or no_reg if it was not possible to free any register
145 // (ie, they all have frame-external references).
146 Register SpillAnyRegister();
147
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000148 // Spill the top element of the frame to memory.
149 void SpillTop() { SpillElementAt(element_count() - 1); }
150
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000151 // Sync the range of elements in [begin, end] with memory.
152 void SyncRange(int begin, int end);
153
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000154 // Make this frame so that an arbitrary frame of the same height can
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000155 // be merged to it. Copies and constants are removed from the frame.
156 void MakeMergable();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000157
ager@chromium.org9085a012009-05-11 19:22:57 +0000158 // Prepare this virtual frame for merging to an expected frame by
159 // performing some state changes that do not require generating
160 // code. It is guaranteed that no code will be generated.
161 void PrepareMergeTo(VirtualFrame* expected);
162
163 // Make this virtual frame have a state identical to an expected virtual
164 // frame. As a side effect, code may be emitted to make this frame match
165 // the expected one.
166 void MergeTo(VirtualFrame* expected);
167
168 // Detach a frame from its code generator, perhaps temporarily. This
169 // tells the register allocator that it is free to use frame-internal
170 // registers. Used when the code generator's frame is switched from this
171 // one to NULL by an unconditional jump.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000172 void DetachFromCodeGenerator() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000173 RegisterAllocator* cgen_allocator = cgen()->allocator();
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000174 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) {
175 if (is_used(i)) cgen_allocator->Unuse(i);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000176 }
177 }
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000178
ager@chromium.org9085a012009-05-11 19:22:57 +0000179 // (Re)attach a frame to its code generator. This informs the register
180 // allocator that the frame-internal register references are active again.
181 // Used when a code generator's frame is switched from NULL to this one by
182 // binding a label.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000183 void AttachToCodeGenerator() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000184 RegisterAllocator* cgen_allocator = cgen()->allocator();
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000185 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) {
186 if (is_used(i)) cgen_allocator->Use(i);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000187 }
188 }
ager@chromium.org9085a012009-05-11 19:22:57 +0000189
190 // Emit code for the physical JS entry and exit frame sequences. After
191 // calling Enter, the virtual frame is ready for use; and after calling
192 // Exit it should not be used. Note that Enter does not allocate space in
193 // the physical frame for storing frame-allocated locals.
194 void Enter();
195 void Exit();
196
197 // Prepare for returning from the frame by spilling locals. This
198 // avoids generating unnecessary merge code when jumping to the
199 // shared return site. Emits code for spills.
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000200 inline void PrepareForReturn();
ager@chromium.org9085a012009-05-11 19:22:57 +0000201
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000202 // Number of local variables after when we use a loop for allocating.
fschneider@chromium.org40b9da32010-06-28 11:29:21 +0000203 static const int kLocalVarBound = 14;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000204
ager@chromium.org9085a012009-05-11 19:22:57 +0000205 // Allocate and initialize the frame-allocated locals.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000206 void AllocateStackSlots();
ager@chromium.org9085a012009-05-11 19:22:57 +0000207
208 // An element of the expression stack as an assembly operand.
209 Operand ElementAt(int index) const {
210 return Operand(rsp, index * kPointerSize);
211 }
212
213 // Random-access store to a frame-top relative frame element. The result
214 // becomes owned by the frame and is invalidated.
215 void SetElementAt(int index, Result* value);
216
217 // Set a frame element to a constant. The index is frame-top relative.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000218 inline void SetElementAt(int index, Handle<Object> value);
ager@chromium.org9085a012009-05-11 19:22:57 +0000219
220 void PushElementAt(int index) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000221 PushFrameSlotAt(element_count() - index - 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000222 }
223
224 void StoreToElementAt(int index) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000225 StoreToFrameSlotAt(element_count() - index - 1);
ager@chromium.org9085a012009-05-11 19:22:57 +0000226 }
227
228 // A frame-allocated local as an assembly operand.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000229 Operand LocalAt(int index) {
ager@chromium.org9085a012009-05-11 19:22:57 +0000230 ASSERT(0 <= index);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000231 ASSERT(index < local_count());
ager@chromium.org9085a012009-05-11 19:22:57 +0000232 return Operand(rbp, kLocal0Offset - index * kPointerSize);
233 }
234
235 // Push a copy of the value of a local frame slot on top of the frame.
236 void PushLocalAt(int index) {
237 PushFrameSlotAt(local0_index() + index);
238 }
239
240 // Push the value of a local frame slot on top of the frame and invalidate
241 // the local slot. The slot should be written to before trying to read
242 // from it again.
243 void TakeLocalAt(int index) {
244 TakeFrameSlotAt(local0_index() + index);
245 }
246
247 // Store the top value on the virtual frame into a local frame slot. The
248 // value is left in place on top of the frame.
249 void StoreToLocalAt(int index) {
250 StoreToFrameSlotAt(local0_index() + index);
251 }
252
253 // Push the address of the receiver slot on the frame.
254 void PushReceiverSlotAddress();
255
256 // Push the function on top of the frame.
257 void PushFunction() { PushFrameSlotAt(function_index()); }
258
259 // Save the value of the esi register to the context frame slot.
260 void SaveContextRegister();
261
262 // Restore the esi register from the value of the context frame
263 // slot.
264 void RestoreContextRegister();
265
266 // A parameter as an assembly operand.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000267 Operand ParameterAt(int index) {
ager@chromium.org9085a012009-05-11 19:22:57 +0000268 ASSERT(-1 <= index); // -1 is the receiver.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000269 ASSERT(index < parameter_count());
270 return Operand(rbp, (1 + parameter_count() - index) * kPointerSize);
ager@chromium.org9085a012009-05-11 19:22:57 +0000271 }
272
273 // Push a copy of the value of a parameter frame slot on top of the frame.
274 void PushParameterAt(int index) {
275 PushFrameSlotAt(param0_index() + index);
276 }
277
278 // Push the value of a paramter frame slot on top of the frame and
279 // invalidate the parameter slot. The slot should be written to before
280 // trying to read from it again.
281 void TakeParameterAt(int index) {
282 TakeFrameSlotAt(param0_index() + index);
283 }
284
285 // Store the top value on the virtual frame into a parameter frame slot.
286 // The value is left in place on top of the frame.
287 void StoreToParameterAt(int index) {
288 StoreToFrameSlotAt(param0_index() + index);
289 }
290
291 // The receiver frame slot.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000292 Operand Receiver() { return ParameterAt(-1); }
ager@chromium.org9085a012009-05-11 19:22:57 +0000293
294 // Push a try-catch or try-finally handler on top of the virtual frame.
295 void PushTryHandler(HandlerType type);
296
297 // Call stub given the number of arguments it expects on (and
298 // removes from) the stack.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000299 inline Result CallStub(CodeStub* stub, int arg_count);
ager@chromium.org9085a012009-05-11 19:22:57 +0000300
301 // Call stub that takes a single argument passed in eax. The
302 // argument is given as a result which does not have to be eax or
303 // even a register. The argument is consumed by the call.
304 Result CallStub(CodeStub* stub, Result* arg);
305
ager@chromium.org3e875802009-06-29 08:26:34 +0000306 // Call stub that takes a pair of arguments passed in edx (arg0, rdx) and
307 // eax (arg1, rax). The arguments are given as results which do not have
ager@chromium.org9085a012009-05-11 19:22:57 +0000308 // to be in the proper registers or even in registers. The
309 // arguments are consumed by the call.
310 Result CallStub(CodeStub* stub, Result* arg0, Result* arg1);
311
ager@chromium.org357bf652010-04-12 11:30:10 +0000312 // Call JS function from top of the stack with arguments
313 // taken from the stack.
314 Result CallJSFunction(int arg_count);
315
ager@chromium.org9085a012009-05-11 19:22:57 +0000316 // Call runtime given the number of arguments expected on (and
317 // removed from) the stack.
318 Result CallRuntime(Runtime::Function* f, int arg_count);
319 Result CallRuntime(Runtime::FunctionId id, int arg_count);
320
ager@chromium.org5c838252010-02-19 08:53:10 +0000321#ifdef ENABLE_DEBUGGER_SUPPORT
322 void DebugBreak();
323#endif
324
ager@chromium.org9085a012009-05-11 19:22:57 +0000325 // Invoke builtin given the number of arguments it expects on (and
326 // removes from) the stack.
327 Result InvokeBuiltin(Builtins::JavaScript id,
328 InvokeFlag flag,
329 int arg_count);
330
331 // Call load IC. Name and receiver are found on top of the frame.
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000332 // Both are dropped.
ager@chromium.org9085a012009-05-11 19:22:57 +0000333 Result CallLoadIC(RelocInfo::Mode mode);
334
335 // Call keyed load IC. Key and receiver are found on top of the
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000336 // frame. Both are dropped.
ager@chromium.org9085a012009-05-11 19:22:57 +0000337 Result CallKeyedLoadIC(RelocInfo::Mode mode);
338
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +0000339 // Call store IC. If the load is contextual, value is found on top of the
340 // frame. If not, value and receiver are on the frame. Both are dropped.
341 Result CallStoreIC(Handle<String> name, bool is_contextual);
342
ager@chromium.org9085a012009-05-11 19:22:57 +0000343 // Call keyed store IC. Value, key, and receiver are found on top
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000344 // of the frame. All three are dropped.
345 Result CallKeyedStoreIC();
ager@chromium.org9085a012009-05-11 19:22:57 +0000346
ager@chromium.org5c838252010-02-19 08:53:10 +0000347 // Call call IC. Function name, arguments, and receiver are found on top
348 // of the frame and dropped by the call.
349 // The argument count does not include the receiver.
ager@chromium.org9085a012009-05-11 19:22:57 +0000350 Result CallCallIC(RelocInfo::Mode mode, int arg_count, int loop_nesting);
351
whesse@chromium.orgba5a61b2010-07-26 11:44:40 +0000352 // Call keyed call IC. Same calling convention as CallCallIC.
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000353 Result CallKeyedCallIC(RelocInfo::Mode mode, int arg_count, int loop_nesting);
354
ager@chromium.org9085a012009-05-11 19:22:57 +0000355 // Allocate and call JS function as constructor. Arguments,
356 // receiver (global object), and function are found on top of the
357 // frame. Function is not dropped. The argument count does not
358 // include the receiver.
359 Result CallConstructor(int arg_count);
360
361 // Drop a number of elements from the top of the expression stack. May
362 // emit code to affect the physical frame. Does not clobber any registers
363 // excepting possibly the stack pointer.
364 void Drop(int count);
365
366 // Drop one element.
367 void Drop() { Drop(1); }
368
369 // Duplicate the top element of the frame.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000370 void Dup() { PushFrameSlotAt(element_count() - 1); }
ager@chromium.org9085a012009-05-11 19:22:57 +0000371
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000372 // Duplicate the n'th element from the top of the frame.
373 // Dup(1) is equivalent to Dup().
374 void Dup(int n) {
375 ASSERT(n > 0);
376 PushFrameSlotAt(element_count() - n);
377 }
378
ager@chromium.org9085a012009-05-11 19:22:57 +0000379 // Pop an element from the top of the expression stack. Returns a
380 // Result, which may be a constant or a register.
381 Result Pop();
382
383 // Pop and save an element from the top of the expression stack and
384 // emit a corresponding pop instruction.
385 void EmitPop(Register reg);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000386 void EmitPop(const Operand& operand);
ager@chromium.org9085a012009-05-11 19:22:57 +0000387
388 // Push an element on top of the expression stack and emit a
389 // corresponding push instruction.
ager@chromium.org5c838252010-02-19 08:53:10 +0000390 void EmitPush(Register reg,
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000391 TypeInfo info = TypeInfo::Unknown());
ager@chromium.org5c838252010-02-19 08:53:10 +0000392 void EmitPush(const Operand& operand,
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000393 TypeInfo info = TypeInfo::Unknown());
ager@chromium.org5c838252010-02-19 08:53:10 +0000394 void EmitPush(Heap::RootListIndex index,
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000395 TypeInfo info = TypeInfo::Unknown());
ager@chromium.org5c838252010-02-19 08:53:10 +0000396 void EmitPush(Immediate immediate,
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000397 TypeInfo info = TypeInfo::Unknown());
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000398 void EmitPush(Smi* value);
ager@chromium.org3e875802009-06-29 08:26:34 +0000399 // Uses kScratchRegister, emits appropriate relocation info.
400 void EmitPush(Handle<Object> value);
ager@chromium.org9085a012009-05-11 19:22:57 +0000401
402 // Push an element on the virtual frame.
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000403 inline void Push(Register reg, TypeInfo info = TypeInfo::Unknown());
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +0000404 inline void Push(Handle<Object> value);
405 inline void Push(Smi* value);
ager@chromium.org9085a012009-05-11 19:22:57 +0000406
407 // Pushing a result invalidates it (its contents become owned by the
408 // frame).
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000409 void Push(Result* result) {
410 if (result->is_register()) {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000411 Push(result->reg(), result->type_info());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000412 } else {
413 ASSERT(result->is_constant());
414 Push(result->handle());
415 }
416 result->Unuse();
417 }
ager@chromium.org9085a012009-05-11 19:22:57 +0000418
kmillikin@chromium.org4111b802010-05-03 10:34:42 +0000419 // Pushing an expression expects that the expression is trivial (according
420 // to Expression::IsTrivial).
421 void Push(Expression* expr);
422
ager@chromium.org9085a012009-05-11 19:22:57 +0000423 // Nip removes zero or more elements from immediately below the top
424 // of the frame, leaving the previous top-of-frame value on top of
425 // the frame. Nip(k) is equivalent to x = Pop(), Drop(k), Push(x).
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +0000426 inline void Nip(int num_dropped);
ager@chromium.org9085a012009-05-11 19:22:57 +0000427
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +0000428 inline void SetTypeForLocalAt(int index, TypeInfo info);
429 inline void SetTypeForParamAt(int index, TypeInfo info);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000430
ager@chromium.org9085a012009-05-11 19:22:57 +0000431 private:
432 static const int kLocal0Offset = JavaScriptFrameConstants::kLocal0Offset;
433 static const int kFunctionOffset = JavaScriptFrameConstants::kFunctionOffset;
434 static const int kContextOffset = StandardFrameConstants::kContextOffset;
435
436 static const int kHandlerSize = StackHandlerConstants::kSize / kPointerSize;
437 static const int kPreallocatedElements = 5 + 8; // 8 expression stack slots.
438
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000439 ZoneList<FrameElement> elements_;
ager@chromium.org9085a012009-05-11 19:22:57 +0000440
441 // The index of the element that is at the processor's stack pointer
442 // (the esp register).
443 int stack_pointer_;
444
ager@chromium.org9085a012009-05-11 19:22:57 +0000445 // The index of the register frame element using each register, or
446 // kIllegalIndex if a register is not on the frame.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000447 int register_locations_[RegisterAllocator::kNumRegisters];
ager@chromium.org9085a012009-05-11 19:22:57 +0000448
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000449 // The number of frame-allocated locals and parameters respectively.
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +0000450 inline int parameter_count();
451 inline int local_count();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000452
453 // The index of the element that is at the processor's frame pointer
454 // (the ebp register). The parameters, receiver, and return address
455 // are below the frame pointer.
456 int frame_pointer() { return parameter_count() + 2; }
457
ager@chromium.org9085a012009-05-11 19:22:57 +0000458 // The index of the first parameter. The receiver lies below the first
459 // parameter.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000460 int param0_index() { return 1; }
ager@chromium.org9085a012009-05-11 19:22:57 +0000461
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000462 // The index of the context slot in the frame. It is immediately
463 // above the frame pointer.
464 int context_index() { return frame_pointer() + 1; }
ager@chromium.org9085a012009-05-11 19:22:57 +0000465
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000466 // The index of the function slot in the frame. It is above the frame
467 // pointer and the context slot.
468 int function_index() { return frame_pointer() + 2; }
ager@chromium.org9085a012009-05-11 19:22:57 +0000469
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000470 // The index of the first local. Between the frame pointer and the
471 // locals lie the context and the function.
472 int local0_index() { return frame_pointer() + 3; }
ager@chromium.org9085a012009-05-11 19:22:57 +0000473
474 // The index of the base of the expression stack.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000475 int expression_base_index() { return local0_index() + local_count(); }
ager@chromium.org9085a012009-05-11 19:22:57 +0000476
477 // Convert a frame index into a frame pointer relative offset into the
478 // actual stack.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000479 int fp_relative(int index) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000480 ASSERT(index < element_count());
481 ASSERT(frame_pointer() < element_count()); // FP is on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000482 return (frame_pointer() - index) * kPointerSize;
ager@chromium.org9085a012009-05-11 19:22:57 +0000483 }
484
485 // Record an occurrence of a register in the virtual frame. This has the
486 // effect of incrementing the register's external reference count and
487 // of updating the index of the register's location in the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000488 void Use(Register reg, int index) {
489 ASSERT(!is_used(reg));
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000490 set_register_location(reg, index);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000491 cgen()->allocator()->Use(reg);
492 }
ager@chromium.org9085a012009-05-11 19:22:57 +0000493
494 // Record that a register reference has been dropped from the frame. This
495 // decrements the register's external reference count and invalidates the
496 // index of the register's location in the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000497 void Unuse(Register reg) {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000498 ASSERT(is_used(reg));
499 set_register_location(reg, kIllegalIndex);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000500 cgen()->allocator()->Unuse(reg);
501 }
ager@chromium.org9085a012009-05-11 19:22:57 +0000502
503 // Spill the element at a particular index---write it to memory if
504 // necessary, free any associated register, and forget its value if
505 // constant.
506 void SpillElementAt(int index);
507
508 // Sync the element at a particular index. If it is a register or
509 // constant that disagrees with the value on the stack, write it to memory.
510 // Keep the element type as register or constant, and clear the dirty bit.
511 void SyncElementAt(int index);
512
ager@chromium.org9085a012009-05-11 19:22:57 +0000513 // Sync a single unsynced element that lies beneath or at the stack pointer.
514 void SyncElementBelowStackPointer(int index);
515
516 // Sync a single unsynced element that lies just above the stack pointer.
517 void SyncElementByPushing(int index);
518
519 // Push a copy of a frame slot (typically a local or parameter) on top of
520 // the frame.
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +0000521 inline void PushFrameSlotAt(int index);
ager@chromium.org9085a012009-05-11 19:22:57 +0000522
523 // Push a the value of a frame slot (typically a local or parameter) on
524 // top of the frame and invalidate the slot.
525 void TakeFrameSlotAt(int index);
526
527 // Store the value on top of the frame to a frame slot (typically a local
528 // or parameter).
529 void StoreToFrameSlotAt(int index);
530
531 // Spill all elements in registers. Spill the top spilled_args elements
532 // on the frame. Sync all other frame elements.
533 // Then drop dropped_args elements from the virtual frame, to match
534 // the effect of an upcoming call that will drop them from the stack.
535 void PrepareForCall(int spilled_args, int dropped_args);
536
537 // Move frame elements currently in registers or constants, that
538 // should be in memory in the expected frame, to memory.
539 void MergeMoveRegistersToMemory(VirtualFrame* expected);
540
541 // Make the register-to-register moves necessary to
542 // merge this frame with the expected frame.
543 // Register to memory moves must already have been made,
544 // and memory to register moves must follow this call.
545 // This is because some new memory-to-register moves are
546 // created in order to break cycles of register moves.
547 // Used in the implementation of MergeTo().
548 void MergeMoveRegistersToRegisters(VirtualFrame* expected);
549
550 // Make the memory-to-register and constant-to-register moves
551 // needed to make this frame equal the expected frame.
552 // Called after all register-to-memory and register-to-register
553 // moves have been made. After this function returns, the frames
554 // should be equal.
555 void MergeMoveMemoryToRegisters(VirtualFrame* expected);
556
557 // Invalidates a frame slot (puts an invalid frame element in it).
558 // Copies on the frame are correctly handled, and if this slot was
559 // the backing store of copies, the index of the new backing store
560 // is returned. Otherwise, returns kIllegalIndex.
561 // Register counts are correctly updated.
562 int InvalidateFrameSlotAt(int index);
563
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000564 // This function assumes that a and b are the only results that could be in
565 // the registers a_reg or b_reg. Other results can be live, but must not
566 // be in the registers a_reg or b_reg. The results a and b are invalidated.
567 void MoveResultsToRegisters(Result* a,
568 Result* b,
569 Register a_reg,
570 Register b_reg);
571
ager@chromium.org9085a012009-05-11 19:22:57 +0000572 // Call a code stub that has already been prepared for calling (via
573 // PrepareForCall).
574 Result RawCallStub(CodeStub* stub);
575
576 // Calls a code object which has already been prepared for calling
577 // (via PrepareForCall).
578 Result RawCallCodeObject(Handle<Code> code, RelocInfo::Mode rmode);
579
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +0000580 inline bool Equals(VirtualFrame* other);
ager@chromium.org9085a012009-05-11 19:22:57 +0000581
ager@chromium.orge2902be2009-06-08 12:21:35 +0000582 // Classes that need raw access to the elements_ array.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000583 friend class FrameRegisterState;
ager@chromium.org9085a012009-05-11 19:22:57 +0000584 friend class JumpTarget;
585};
586
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000587
ager@chromium.org9085a012009-05-11 19:22:57 +0000588} } // namespace v8::internal
589
590#endif // V8_X64_VIRTUAL_FRAME_X64_H_