blob: c7e32c3c3351ab97d8ae18f5b229677cea28cef5 [file] [log] [blame]
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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#include "v8.h"
29
30#include "bootstrapper.h"
31#include "codegen-inl.h"
32#include "debug.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000033#include "parser.h"
34#include "register-allocator-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035#include "runtime.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000036#include "scopes.h"
37
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038
kasperl@chromium.org71affb52009-05-26 05:44:31 +000039namespace v8 {
40namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041
ager@chromium.org65dad4b2009-04-23 08:48:43 +000042#define __ ACCESS_MASM(masm_)
43
ager@chromium.org3bf7b912008-11-17 09:09:45 +000044
45// -------------------------------------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000046// CodeGenState implementation.
47
ager@chromium.org7c537e22008-10-16 08:43:32 +000048CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000049 : owner_(owner),
ager@chromium.org7c537e22008-10-16 08:43:32 +000050 typeof_state_(NOT_INSIDE_TYPEOF),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000051 true_target_(NULL),
52 false_target_(NULL),
53 previous_(NULL) {
54 owner_->set_state(this);
55}
56
57
ager@chromium.org7c537e22008-10-16 08:43:32 +000058CodeGenState::CodeGenState(CodeGenerator* owner,
59 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000060 JumpTarget* true_target,
61 JumpTarget* false_target)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000062 : owner_(owner),
ager@chromium.org7c537e22008-10-16 08:43:32 +000063 typeof_state_(typeof_state),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000064 true_target_(true_target),
65 false_target_(false_target),
66 previous_(owner->state()) {
67 owner_->set_state(this);
68}
69
70
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000071CodeGenState::~CodeGenState() {
72 ASSERT(owner_->state() == this);
73 owner_->set_state(previous_);
74}
75
76
ager@chromium.org3bf7b912008-11-17 09:09:45 +000077// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +000078// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000079
ager@chromium.org7c537e22008-10-16 08:43:32 +000080CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script,
81 bool is_eval)
82 : is_eval_(is_eval),
83 script_(script),
84 deferred_(8),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000085 masm_(new MacroAssembler(NULL, buffer_size)),
86 scope_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +000087 frame_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000088 allocator_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000089 cc_reg_(al),
90 state_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000091 function_return_is_shadowed_(false),
92 in_spilled_code_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093}
94
95
96// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000097// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000099// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000100// cp: callee's context
101
ager@chromium.org7c537e22008-10-16 08:43:32 +0000102void CodeGenerator::GenCode(FunctionLiteral* fun) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000103 ZoneList<Statement*>* body = fun->body();
104
105 // Initialize state.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000106 ASSERT(scope_ == NULL);
107 scope_ = fun->scope();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000108 ASSERT(allocator_ == NULL);
109 RegisterAllocator register_allocator(this);
110 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000111 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000112 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000113 cc_reg_ = al;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000114 set_in_spilled_code(false);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000115 {
116 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000117
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000118 // Entry:
119 // Stack: receiver, arguments
120 // lr: return address
121 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000122 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000123 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000125 allocator_->Initialize();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000126 frame_->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000127 // tos: code slot
128#ifdef DEBUG
129 if (strlen(FLAG_stop_at) > 0 &&
130 fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000131 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000132 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133 }
134#endif
135
136 // Allocate space for locals and initialize them.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000137 frame_->AllocateStackSlots();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000138 // Initialize the function return target after the locals are set
139 // up, because it needs the expected frame height from the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000140 function_return_.set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000141 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000143 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000144 if (scope_->num_heap_slots() > 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145 // Allocate local context.
146 // Get outer context and create a new context based on it.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000147 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000148 frame_->EmitPush(r0);
149 frame_->CallRuntime(Runtime::kNewContext, 1); // r0 holds the result
kasper.lund7276f142008-07-30 08:49:36 +0000150
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000151#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000152 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000153 __ cmp(r0, Operand(cp));
154 verified_true.Branch(eq);
155 __ stop("NewContext: r0 is expected to be the same as cp");
156 verified_true.Bind();
157#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000159 __ str(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000160 }
161
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000162 // TODO(1241774): Improve this code:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000163 // 1) only needed if we have a context
164 // 2) no need to recompute context ptr every single time
165 // 3) don't copy parameter operand code from SlotOperand!
166 {
167 Comment cmnt2(masm_, "[ copy context parameters into .context");
168
169 // Note that iteration order is relevant here! If we have the same
170 // parameter twice (e.g., function (x, y, x)), and that parameter
171 // needs to be copied into the context, it must be the last argument
172 // passed to the parameter that needs to be copied. This is a rare
173 // case so we don't check for it, instead we rely on the copying
174 // order: such a parameter is copied repeatedly into the same
175 // context location and thus the last value is what is seen inside
176 // the function.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000177 for (int i = 0; i < scope_->num_parameters(); i++) {
178 Variable* par = scope_->parameter(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000179 Slot* slot = par->slot();
180 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000181 ASSERT(!scope_->is_global_scope()); // no parameters in global scope
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000182 __ ldr(r1, frame_->ParameterAt(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183 // Loads r2 with context; used below in RecordWrite.
184 __ str(r1, SlotOperand(slot, r2));
185 // Load the offset into r3.
186 int slot_offset =
187 FixedArray::kHeaderSize + slot->index() * kPointerSize;
188 __ mov(r3, Operand(slot_offset));
189 __ RecordWrite(r2, r3, r1);
190 }
191 }
192 }
193
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000194 // Store the arguments object. This must happen after context
195 // initialization because the arguments object may be stored in the
196 // context.
197 if (scope_->arguments() != NULL) {
198 ASSERT(scope_->arguments_shadow() != NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199 Comment cmnt(masm_, "[ allocate arguments object");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000200 { Reference shadow_ref(this, scope_->arguments_shadow());
201 { Reference arguments_ref(this, scope_->arguments());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000202 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000203 __ ldr(r2, frame_->Function());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000204 // The receiver is below the arguments, the return address,
205 // and the frame pointer on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000206 const int kReceiverDisplacement = 2 + scope_->num_parameters();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000207 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000208 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000209 frame_->Adjust(3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000210 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000211 frame_->CallStub(&stub, 3);
212 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000213 arguments_ref.SetValue(NOT_CONST_INIT);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000214 }
ager@chromium.org7c537e22008-10-16 08:43:32 +0000215 shadow_ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000217 frame_->Drop(); // Value is no longer needed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000218 }
219
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000220 // Generate code to 'execute' declarations and initialize functions
221 // (source elements). In case of an illegal redeclaration we need to
222 // handle that instead of processing the declarations.
223 if (scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000225 scope_->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000226 } else {
227 Comment cmnt(masm_, "[ declarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000228 ProcessDeclarations(scope_->declarations());
229 // Bail out if a stack-overflow exception occurred when processing
230 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000231 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000232 }
233
mads.s.ager31e71382008-08-13 09:32:07 +0000234 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000235 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000236 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000237 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000238 CheckStack();
239
240 // Compile the body of the function in a vanilla state. Don't
241 // bother compiling all the code if the scope has an illegal
242 // redeclaration.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000243 if (!scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244 Comment cmnt(masm_, "[ function body");
245#ifdef DEBUG
246 bool is_builtin = Bootstrapper::IsActive();
247 bool should_trace =
248 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000249 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000250 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000251 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000252 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000253#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000254 VisitStatementsAndSpill(body);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000255 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000256 }
257
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000258 // Generate the return sequence if necessary.
259 if (frame_ != NULL || function_return_.is_linked()) {
260 // exit
261 // r0: result
262 // sp: stack pointer
263 // fp: frame pointer
264 // pp: parameter pointer
265 // cp: callee's context
266 __ mov(r0, Operand(Factory::undefined_value()));
mads.s.ager31e71382008-08-13 09:32:07 +0000267
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000268 function_return_.Bind();
269 if (FLAG_trace) {
270 // Push the return value on the stack as the parameter.
271 // Runtime::TraceExit returns the parameter as it is.
272 frame_->EmitPush(r0);
273 frame_->CallRuntime(Runtime::kTraceExit, 1);
274 }
275
276 // Tear down the frame which will restore the caller's frame pointer and
277 // the link register.
278 frame_->Exit();
279
280 __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize));
ager@chromium.org9085a012009-05-11 19:22:57 +0000281 __ Jump(lr);
mads.s.ager31e71382008-08-13 09:32:07 +0000282 }
283
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000285 ASSERT(!has_cc());
286 ASSERT(state_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000287 ASSERT(!function_return_is_shadowed_);
288 function_return_.Unuse();
289 DeleteFrame();
290
291 // Process any deferred code using the register allocator.
292 if (HasStackOverflow()) {
293 ClearDeferred();
294 } else {
295 ProcessDeferred();
296 }
297
298 allocator_ = NULL;
299 scope_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300}
301
302
ager@chromium.org7c537e22008-10-16 08:43:32 +0000303MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
304 // Currently, this assertion will fail if we try to assign to
305 // a constant variable that is constant because it is read-only
306 // (such as the variable referring to a named function expression).
307 // We need to implement assignments to read-only variables.
308 // Ideally, we should do this during AST generation (by converting
309 // such assignments into expression statements); however, in general
310 // we may not be able to make the decision until past AST generation,
311 // that is when the entire program is known.
312 ASSERT(slot != NULL);
313 int index = slot->index();
314 switch (slot->type()) {
315 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000316 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000317
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000318 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000319 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000320
321 case Slot::CONTEXT: {
322 // Follow the context chain if necessary.
323 ASSERT(!tmp.is(cp)); // do not overwrite context register
324 Register context = cp;
325 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000326 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000327 // Load the closure.
328 // (All contexts, even 'with' contexts, have a closure,
329 // and it is the same for all contexts inside a function.
330 // There is no need to go to the function context first.)
331 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
332 // Load the function context (which is the incoming, outer context).
333 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
334 context = tmp;
335 }
336 // We may have a 'with' context now. Get the function context.
337 // (In fact this mov may never be the needed, since the scope analysis
338 // may not permit a direct context access in this case and thus we are
339 // always at a function context. However it is safe to dereference be-
340 // cause the function context of a function context is itself. Before
341 // deleting this mov we should try to create a counter-example first,
342 // though...)
343 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
344 return ContextOperand(tmp, index);
345 }
346
347 default:
348 UNREACHABLE();
349 return MemOperand(r0, 0);
350 }
351}
352
353
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000354MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
355 Slot* slot,
356 Register tmp,
357 Register tmp2,
358 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000359 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000360 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000361
ager@chromium.org381abbb2009-02-25 13:23:22 +0000362 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
363 if (s->num_heap_slots() > 0) {
364 if (s->calls_eval()) {
365 // Check that extension is NULL.
366 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
367 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000368 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000369 }
370 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
371 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
372 context = tmp;
373 }
374 }
375 // Check that last extension is NULL.
376 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
377 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000378 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000379 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000380 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000381}
382
383
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000384void CodeGenerator::LoadConditionAndSpill(Expression* expression,
385 TypeofState typeof_state,
386 JumpTarget* true_target,
387 JumpTarget* false_target,
388 bool force_control) {
389 ASSERT(in_spilled_code());
390 set_in_spilled_code(false);
391 LoadCondition(expression, typeof_state, true_target, false_target,
392 force_control);
393 if (frame_ != NULL) {
394 frame_->SpillAll();
395 }
396 set_in_spilled_code(true);
397}
398
399
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000400// Loads a value on TOS. If it is a boolean value, the result may have been
401// (partially) translated into branches, or it may have set the condition
402// code register. If force_cc is set, the value is forced to set the
403// condition code register and no value is pushed. If the condition code
404// register was set, has_cc() is true and cc_reg_ contains the condition to
405// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000406void CodeGenerator::LoadCondition(Expression* x,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000407 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000408 JumpTarget* true_target,
409 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000410 bool force_cc) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000411 ASSERT(!in_spilled_code());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000412 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000413 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000414
ager@chromium.org7c537e22008-10-16 08:43:32 +0000415 { CodeGenState new_state(this, typeof_state, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000416 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000417
418 // If we hit a stack overflow, we may not have actually visited
419 // the expression. In that case, we ensure that we have a
420 // valid-looking frame state because we will continue to generate
421 // code as we unwind the C++ stack.
422 //
423 // It's possible to have both a stack overflow and a valid frame
424 // state (eg, a subexpression overflowed, visiting it returned
425 // with a dummied frame state, and visiting this expression
426 // returned with a normal-looking state).
427 if (HasStackOverflow() &&
428 has_valid_frame() &&
429 !has_cc() &&
430 frame_->height() == original_height) {
431 true_target->Jump();
432 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000433 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000434 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000435 // Convert the TOS value to a boolean in the condition code register.
436 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000438 ASSERT(!force_cc || !has_valid_frame() || has_cc());
439 ASSERT(!has_valid_frame() ||
440 (has_cc() && frame_->height() == original_height) ||
441 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000442}
443
444
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000445void CodeGenerator::LoadAndSpill(Expression* expression,
446 TypeofState typeof_state) {
447 ASSERT(in_spilled_code());
448 set_in_spilled_code(false);
449 Load(expression, typeof_state);
450 frame_->SpillAll();
451 set_in_spilled_code(true);
452}
453
454
ager@chromium.org7c537e22008-10-16 08:43:32 +0000455void CodeGenerator::Load(Expression* x, TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000456#ifdef DEBUG
457 int original_height = frame_->height();
458#endif
459 ASSERT(!in_spilled_code());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000460 JumpTarget true_target;
461 JumpTarget false_target;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000462 LoadCondition(x, typeof_state, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000463
464 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000465 // Convert cc_reg_ into a boolean value.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000466 JumpTarget loaded;
467 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000468 materialize_true.Branch(cc_reg_);
mads.s.ager31e71382008-08-13 09:32:07 +0000469 __ mov(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000470 frame_->EmitPush(r0);
471 loaded.Jump();
472 materialize_true.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000473 __ mov(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000474 frame_->EmitPush(r0);
475 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000476 cc_reg_ = al;
477 }
478
479 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000480 // We have at least one condition value that has been "translated"
481 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000482 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000483 if (frame_ != NULL) {
484 loaded.Jump(); // Don't lose the current TOS.
485 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000487 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000489 true_target.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000490 __ mov(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000491 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000493 // If both "true" and "false" need to be loaded jump across the code for
494 // "false".
495 if (both) {
496 loaded.Jump();
497 }
498 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000499 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000500 false_target.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000501 __ mov(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000502 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000504 // A value is loaded on all paths reaching this point.
505 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000506 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000507 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000508 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000509 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000510}
511
512
ager@chromium.org7c537e22008-10-16 08:43:32 +0000513void CodeGenerator::LoadGlobal() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000514 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000515 __ ldr(r0, GlobalObject());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000516 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000517}
518
519
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000520void CodeGenerator::LoadGlobalReceiver(Register scratch) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000521 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000522 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
523 __ ldr(scratch,
524 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000525 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000526}
527
528
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529// TODO(1241834): Get rid of this function in favor of just using Load, now
ager@chromium.org7c537e22008-10-16 08:43:32 +0000530// that we have the INSIDE_TYPEOF typeof state. => Need to handle global
531// variables w/o reference errors elsewhere.
532void CodeGenerator::LoadTypeofExpression(Expression* x) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000533 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534 Variable* variable = x->AsVariableProxy()->AsVariable();
535 if (variable != NULL && !variable->is_this() && variable->is_global()) {
536 // NOTE: This is somewhat nasty. We force the compiler to load
537 // the variable as if through '<global>.<variable>' to make sure we
538 // do not get reference errors.
539 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
540 Literal key(variable->name());
541 // TODO(1241834): Fetch the position from the variable instead of using
542 // no position.
ager@chromium.org236ad962008-09-25 09:45:57 +0000543 Property property(&global, &key, RelocInfo::kNoPosition);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000544 LoadAndSpill(&property);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000546 LoadAndSpill(x, INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000547 }
548}
549
550
ager@chromium.org7c537e22008-10-16 08:43:32 +0000551Reference::Reference(CodeGenerator* cgen, Expression* expression)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
553 cgen->LoadReference(this);
554}
555
556
557Reference::~Reference() {
558 cgen_->UnloadReference(this);
559}
560
561
ager@chromium.org7c537e22008-10-16 08:43:32 +0000562void CodeGenerator::LoadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000563 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000564 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565 Expression* e = ref->expression();
566 Property* property = e->AsProperty();
567 Variable* var = e->AsVariableProxy()->AsVariable();
568
569 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000570 // The expression is either a property or a variable proxy that rewrites
571 // to a property.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000572 LoadAndSpill(property->obj());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000573 // We use a named reference if the key is a literal symbol, unless it is
574 // a string that can be legally parsed as an integer. This is because
575 // otherwise we will not get into the slow case code that handles [] on
576 // String objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 Literal* literal = property->key()->AsLiteral();
578 uint32_t dummy;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000579 if (literal != NULL &&
580 literal->handle()->IsSymbol() &&
581 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000582 ref->set_type(Reference::NAMED);
583 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000584 LoadAndSpill(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000585 ref->set_type(Reference::KEYED);
586 }
587 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000588 // The expression is a variable proxy that does not rewrite to a
589 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000590 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000591 LoadGlobal();
592 ref->set_type(Reference::NAMED);
593 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000594 ASSERT(var->slot() != NULL);
595 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000596 }
597 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000598 // Anything else is a runtime error.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000599 LoadAndSpill(e);
600 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601 }
602}
603
604
ager@chromium.org7c537e22008-10-16 08:43:32 +0000605void CodeGenerator::UnloadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000606 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000607 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000608 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000609 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000610 if (size > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000611 frame_->EmitPop(r0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000612 frame_->Drop(size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000613 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000614 }
615}
616
617
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000618// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
619// register to a boolean in the condition code register. The code
620// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000621void CodeGenerator::ToBoolean(JumpTarget* true_target,
622 JumpTarget* false_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000623 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000624 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000625 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000626 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627
628 // Fast case checks
629
mads.s.ager31e71382008-08-13 09:32:07 +0000630 // Check if the value is 'false'.
631 __ cmp(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000632 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000633
mads.s.ager31e71382008-08-13 09:32:07 +0000634 // Check if the value is 'true'.
635 __ cmp(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000636 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637
mads.s.ager31e71382008-08-13 09:32:07 +0000638 // Check if the value is 'undefined'.
639 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000640 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641
mads.s.ager31e71382008-08-13 09:32:07 +0000642 // Check if the value is a smi.
643 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000644 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000645 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000646 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000647
648 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000649 frame_->EmitPush(r0);
650 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000651 // Convert the result (r0) to a condition code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653
654 cc_reg_ = ne;
655}
656
657
kasper.lund7276f142008-07-30 08:49:36 +0000658class GenericBinaryOpStub : public CodeStub {
659 public:
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000660 GenericBinaryOpStub(Token::Value op,
661 OverwriteMode mode)
662 : op_(op), mode_(mode) { }
kasper.lund7276f142008-07-30 08:49:36 +0000663
664 private:
665 Token::Value op_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000666 OverwriteMode mode_;
667
668 // Minor key encoding in 16 bits.
669 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
670 class OpBits: public BitField<Token::Value, 2, 14> {};
kasper.lund7276f142008-07-30 08:49:36 +0000671
672 Major MajorKey() { return GenericBinaryOp; }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000673 int MinorKey() {
674 // Encode the parameters in a unique 16 bit value.
675 return OpBits::encode(op_)
676 | ModeBits::encode(mode_);
677 }
678
kasper.lund7276f142008-07-30 08:49:36 +0000679 void Generate(MacroAssembler* masm);
680
681 const char* GetName() {
682 switch (op_) {
683 case Token::ADD: return "GenericBinaryOpStub_ADD";
684 case Token::SUB: return "GenericBinaryOpStub_SUB";
685 case Token::MUL: return "GenericBinaryOpStub_MUL";
686 case Token::DIV: return "GenericBinaryOpStub_DIV";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000687 case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR";
688 case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND";
689 case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR";
690 case Token::SAR: return "GenericBinaryOpStub_SAR";
691 case Token::SHL: return "GenericBinaryOpStub_SHL";
692 case Token::SHR: return "GenericBinaryOpStub_SHR";
kasper.lund7276f142008-07-30 08:49:36 +0000693 default: return "GenericBinaryOpStub";
694 }
695 }
696
697#ifdef DEBUG
698 void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); }
699#endif
700};
701
702
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000703void CodeGenerator::GenericBinaryOperation(Token::Value op,
704 OverwriteMode overwrite_mode) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000705 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000706 // sp[0] : y
707 // sp[1] : x
708 // result : r0
709
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000710 // Stub is entered with a call: 'return address' is in lr.
711 switch (op) {
712 case Token::ADD: // fall through.
713 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000714 case Token::MUL:
715 case Token::BIT_OR:
716 case Token::BIT_AND:
717 case Token::BIT_XOR:
718 case Token::SHL:
719 case Token::SHR:
720 case Token::SAR: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000721 frame_->EmitPop(r0); // r0 : y
722 frame_->EmitPop(r1); // r1 : x
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000723 GenericBinaryOpStub stub(op, overwrite_mode);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000724 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725 break;
726 }
727
728 case Token::DIV: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000729 Result arg_count = allocator_->Allocate(r0);
730 ASSERT(arg_count.is_valid());
731 __ mov(arg_count.reg(), Operand(1));
732 frame_->InvokeBuiltin(Builtins::DIV, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000733 break;
734 }
735
736 case Token::MOD: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000737 Result arg_count = allocator_->Allocate(r0);
738 ASSERT(arg_count.is_valid());
739 __ mov(arg_count.reg(), Operand(1));
740 frame_->InvokeBuiltin(Builtins::MOD, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000741 break;
742 }
743
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000744 case Token::COMMA:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000745 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000746 // simply discard left value
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000747 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000748 break;
749
750 default:
751 // Other cases should have been handled before this point.
752 UNREACHABLE();
753 break;
754 }
755}
756
757
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000758class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000759 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000760 DeferredInlineSmiOperation(CodeGenerator* generator,
761 Token::Value op,
762 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000763 bool reversed,
764 OverwriteMode overwrite_mode)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000765 : DeferredCode(generator),
766 op_(op),
767 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000768 reversed_(reversed),
769 overwrite_mode_(overwrite_mode) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000770 set_comment("[ DeferredInlinedSmiOperation");
771 }
772
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000773 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000774
775 private:
776 Token::Value op_;
777 int value_;
778 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000779 OverwriteMode overwrite_mode_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000780};
781
782
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000783void DeferredInlineSmiOperation::Generate() {
784 enter()->Bind();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000785 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000786
787 switch (op_) {
788 case Token::ADD: {
789 if (reversed_) {
790 // revert optimistic add
791 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
792 __ mov(r1, Operand(Smi::FromInt(value_)));
793 } else {
794 // revert optimistic add
795 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
796 __ mov(r0, Operand(Smi::FromInt(value_)));
797 }
798 break;
799 }
800
801 case Token::SUB: {
802 if (reversed_) {
803 // revert optimistic sub
804 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
805 __ mov(r1, Operand(Smi::FromInt(value_)));
806 } else {
807 __ add(r1, r0, Operand(Smi::FromInt(value_)));
808 __ mov(r0, Operand(Smi::FromInt(value_)));
809 }
810 break;
811 }
812
813 case Token::BIT_OR:
814 case Token::BIT_XOR:
815 case Token::BIT_AND: {
816 if (reversed_) {
817 __ mov(r1, Operand(Smi::FromInt(value_)));
818 } else {
819 __ mov(r1, Operand(r0));
820 __ mov(r0, Operand(Smi::FromInt(value_)));
821 }
822 break;
823 }
824
825 case Token::SHL:
826 case Token::SHR:
827 case Token::SAR: {
828 if (!reversed_) {
829 __ mov(r1, Operand(r0));
830 __ mov(r0, Operand(Smi::FromInt(value_)));
831 } else {
832 UNREACHABLE(); // should have been handled in SmiOperation
833 }
834 break;
835 }
836
837 default:
838 // other cases should have been handled before this point.
839 UNREACHABLE();
840 break;
841 }
842
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000843 GenericBinaryOpStub igostub(op_, overwrite_mode_);
ager@chromium.org41826e72009-03-30 13:30:57 +0000844 Result arg0 = generator()->allocator()->Allocate(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000845 ASSERT(arg0.is_valid());
ager@chromium.org41826e72009-03-30 13:30:57 +0000846 Result arg1 = generator()->allocator()->Allocate(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000847 ASSERT(arg1.is_valid());
ager@chromium.org41826e72009-03-30 13:30:57 +0000848 generator()->frame()->CallStub(&igostub, &arg0, &arg1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000849 exit_.Jump();
850}
851
852
ager@chromium.org7c537e22008-10-16 08:43:32 +0000853void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000854 Handle<Object> value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000855 bool reversed,
856 OverwriteMode mode) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000857 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858 // NOTE: This is an attempt to inline (a bit) more of the code for
859 // some possible smi operations (like + and -) when (at least) one
860 // of the operands is a literal smi. With this optimization, the
861 // performance of the system is increased by ~15%, and the generated
862 // code size is increased by ~1% (measured on a combination of
863 // different benchmarks).
864
mads.s.ager31e71382008-08-13 09:32:07 +0000865 // sp[0] : operand
866
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000867 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000869 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000870 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000871
872 switch (op) {
873 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000874 DeferredCode* deferred =
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000875 new DeferredInlineSmiOperation(this, op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000877 __ add(r0, r0, Operand(value), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000878 deferred->enter()->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000879 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000880 deferred->enter()->Branch(ne);
881 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000882 break;
883 }
884
885 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000886 DeferredCode* deferred =
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000887 new DeferredInlineSmiOperation(this, op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889 if (!reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000890 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000891 } else {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000892 __ rsb(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000894 deferred->enter()->Branch(vs);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000895 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000896 deferred->enter()->Branch(ne);
897 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000898 break;
899 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000901 case Token::BIT_OR:
902 case Token::BIT_XOR:
903 case Token::BIT_AND: {
904 DeferredCode* deferred =
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000905 new DeferredInlineSmiOperation(this, op, int_value, reversed, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000906 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000907 deferred->enter()->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000908 switch (op) {
909 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
910 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
911 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
912 default: UNREACHABLE();
913 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000914 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000915 break;
916 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000918 case Token::SHL:
919 case Token::SHR:
920 case Token::SAR: {
921 if (reversed) {
922 __ mov(ip, Operand(value));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000923 frame_->EmitPush(ip);
924 frame_->EmitPush(r0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000925 GenericBinaryOperation(op, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000926
927 } else {
928 int shift_value = int_value & 0x1f; // least significant 5 bits
929 DeferredCode* deferred =
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000930 new DeferredInlineSmiOperation(this, op, shift_value, false, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000931 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000932 deferred->enter()->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000933 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
934 switch (op) {
935 case Token::SHL: {
936 __ mov(r2, Operand(r2, LSL, shift_value));
937 // check that the *unsigned* result fits in a smi
938 __ add(r3, r2, Operand(0x40000000), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000939 deferred->enter()->Branch(mi);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000940 break;
941 }
942 case Token::SHR: {
943 // LSR by immediate 0 means shifting 32 bits.
944 if (shift_value != 0) {
945 __ mov(r2, Operand(r2, LSR, shift_value));
946 }
947 // check that the *unsigned* result fits in a smi
948 // neither of the two high-order bits can be set:
949 // - 0x80000000: high bit would be lost when smi tagging
950 // - 0x40000000: this number would convert to negative when
951 // smi tagging these two cases can only happen with shifts
952 // by 0 or 1 when handed a valid smi
953 __ and_(r3, r2, Operand(0xc0000000), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000954 deferred->enter()->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000955 break;
956 }
957 case Token::SAR: {
958 if (shift_value != 0) {
959 // ASR by immediate 0 means shifting 32 bits.
960 __ mov(r2, Operand(r2, ASR, shift_value));
961 }
962 break;
963 }
964 default: UNREACHABLE();
965 }
966 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000967 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000968 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000969 break;
970 }
971
972 default:
973 if (!reversed) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000974 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +0000975 __ mov(r0, Operand(value));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000976 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977 } else {
978 __ mov(ip, Operand(value));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000979 frame_->EmitPush(ip);
980 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000982 GenericBinaryOperation(op, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000983 break;
984 }
985
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000986 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987}
988
989
ager@chromium.org7c537e22008-10-16 08:43:32 +0000990void CodeGenerator::Comparison(Condition cc, bool strict) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000991 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000992 // sp[0] : y
993 // sp[1] : x
994 // result : cc register
995
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000996 // Strict only makes sense for equality comparisons.
997 ASSERT(!strict || cc == eq);
998
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000999 JumpTarget exit;
1000 JumpTarget smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001001 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1002 if (cc == gt || cc == le) {
1003 cc = ReverseCondition(cc);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001004 frame_->EmitPop(r1);
1005 frame_->EmitPop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001006 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001007 frame_->EmitPop(r0);
1008 frame_->EmitPop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001009 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010 __ orr(r2, r0, Operand(r1));
1011 __ tst(r2, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001012 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001013
1014 // Perform non-smi comparison by runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001015 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001016
1017 // Figure out which native to call and setup the arguments.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001018 Builtins::JavaScript native;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001019 int arg_count = 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001020 if (cc == eq) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001021 native = strict ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001023 native = Builtins::COMPARE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024 int ncr; // NaN compare result
1025 if (cc == lt || cc == le) {
1026 ncr = GREATER;
1027 } else {
1028 ASSERT(cc == gt || cc == ge); // remaining cases
1029 ncr = LESS;
1030 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001031 frame_->EmitPush(r0);
1032 arg_count++;
mads.s.ager31e71382008-08-13 09:32:07 +00001033 __ mov(r0, Operand(Smi::FromInt(ncr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001034 }
1035
1036 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1037 // tagged as a small integer.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001038 frame_->EmitPush(r0);
1039 Result arg_count_register = allocator_->Allocate(r0);
1040 ASSERT(arg_count_register.is_valid());
1041 __ mov(arg_count_register.reg(), Operand(arg_count));
1042 Result result = frame_->InvokeBuiltin(native,
1043 CALL_JS,
1044 &arg_count_register,
1045 arg_count + 1);
1046 __ cmp(result.reg(), Operand(0));
1047 result.Unuse();
1048 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001049
1050 // test smi equality by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001051 smi.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001052 __ cmp(r1, Operand(r0));
1053
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001054 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001055 cc_reg_ = cc;
1056}
1057
1058
kasper.lund7276f142008-07-30 08:49:36 +00001059class CallFunctionStub: public CodeStub {
1060 public:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001061 CallFunctionStub(int argc, InLoopFlag in_loop)
1062 : argc_(argc), in_loop_(in_loop) {}
kasper.lund7276f142008-07-30 08:49:36 +00001063
1064 void Generate(MacroAssembler* masm);
1065
1066 private:
1067 int argc_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001068 InLoopFlag in_loop_;
kasper.lund7276f142008-07-30 08:49:36 +00001069
kasper.lund7276f142008-07-30 08:49:36 +00001070#if defined(DEBUG)
1071 void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); }
1072#endif // defined(DEBUG)
1073
1074 Major MajorKey() { return CallFunction; }
1075 int MinorKey() { return argc_; }
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001076 InLoopFlag InLoop() { return in_loop_; }
kasper.lund7276f142008-07-30 08:49:36 +00001077};
1078
1079
mads.s.ager31e71382008-08-13 09:32:07 +00001080// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001081void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001082 int position) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001083 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001084 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001085 int arg_count = args->length();
1086 for (int i = 0; i < arg_count; i++) {
1087 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001088 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089
kasper.lund7276f142008-07-30 08:49:36 +00001090 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001091 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001092
kasper.lund7276f142008-07-30 08:49:36 +00001093 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001094 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
1095 CallFunctionStub call_function(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001096 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097
1098 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001099 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001100 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101}
1102
1103
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001104void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001105 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001106 ASSERT(has_cc());
1107 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001108 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109 cc_reg_ = al;
1110}
1111
1112
ager@chromium.org7c537e22008-10-16 08:43:32 +00001113void CodeGenerator::CheckStack() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001114 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001115 if (FLAG_check_stack) {
1116 Comment cmnt(masm_, "[ check stack");
1117 StackCheckStub stub;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001118 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001119 }
1120}
1121
1122
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001123void CodeGenerator::VisitAndSpill(Statement* statement) {
1124 ASSERT(in_spilled_code());
1125 set_in_spilled_code(false);
1126 Visit(statement);
1127 if (frame_ != NULL) {
1128 frame_->SpillAll();
1129 }
1130 set_in_spilled_code(true);
1131}
1132
1133
1134void CodeGenerator::VisitStatementsAndSpill(ZoneList<Statement*>* statements) {
1135 ASSERT(in_spilled_code());
1136 set_in_spilled_code(false);
1137 VisitStatements(statements);
1138 if (frame_ != NULL) {
1139 frame_->SpillAll();
1140 }
1141 set_in_spilled_code(true);
1142}
1143
1144
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001145void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1146#ifdef DEBUG
1147 int original_height = frame_->height();
1148#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001149 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001150 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1151 VisitAndSpill(statements->at(i));
1152 }
1153 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1154}
1155
1156
ager@chromium.org7c537e22008-10-16 08:43:32 +00001157void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001158#ifdef DEBUG
1159 int original_height = frame_->height();
1160#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001161 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001162 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001163 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001164 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001165 VisitStatementsAndSpill(node->statements());
1166 if (node->break_target()->is_linked()) {
1167 node->break_target()->Bind();
1168 }
1169 node->break_target()->Unuse();
1170 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001171}
1172
1173
ager@chromium.org7c537e22008-10-16 08:43:32 +00001174void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001175 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001176 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001177 frame_->EmitPush(r0);
1178 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001179 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001180 frame_->EmitPush(r0);
1181 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001182 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001183}
1184
1185
ager@chromium.org7c537e22008-10-16 08:43:32 +00001186void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001187#ifdef DEBUG
1188 int original_height = frame_->height();
1189#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001190 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001191 Comment cmnt(masm_, "[ Declaration");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001192 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001193 Variable* var = node->proxy()->var();
1194 ASSERT(var != NULL); // must have been resolved
1195 Slot* slot = var->slot();
1196
1197 // If it was not possible to allocate the variable at compile time,
1198 // we need to "declare" it at runtime to make sure it actually
1199 // exists in the local context.
1200 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1201 // Variables with a "LOOKUP" slot were introduced as non-locals
1202 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001203 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001204 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001205 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001206 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001207 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208 // Declaration nodes are always declared in only two modes.
1209 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1210 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001211 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001212 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001213 // Push initial value, if any.
1214 // Note: For variables we must not push an initial value (such as
1215 // 'undefined') because we may have a (legal) redeclaration and we
1216 // must not destroy the current value.
1217 if (node->mode() == Variable::CONST) {
mads.s.ager31e71382008-08-13 09:32:07 +00001218 __ mov(r0, Operand(Factory::the_hole_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001219 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001220 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001221 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001222 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001223 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001224 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001225 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001226 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001227 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001228 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001229 return;
1230 }
1231
1232 ASSERT(!var->is_global());
1233
1234 // If we have a function or a constant, we need to initialize the variable.
1235 Expression* val = NULL;
1236 if (node->mode() == Variable::CONST) {
1237 val = new Literal(Factory::the_hole_value());
1238 } else {
1239 val = node->fun(); // NULL if we don't have a function
1240 }
1241
1242 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001243 {
1244 // Set initial value.
1245 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001246 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001247 target.SetValue(NOT_CONST_INIT);
1248 // The reference is removed from the stack (preserving TOS) when
1249 // it goes out of scope.
1250 }
1251 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001252 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001253 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001254 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001255}
1256
1257
ager@chromium.org7c537e22008-10-16 08:43:32 +00001258void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001259#ifdef DEBUG
1260 int original_height = frame_->height();
1261#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001262 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001264 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001265 Expression* expression = node->expression();
1266 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001267 LoadAndSpill(expression);
1268 frame_->Drop();
1269 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001270}
1271
1272
ager@chromium.org7c537e22008-10-16 08:43:32 +00001273void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001274#ifdef DEBUG
1275 int original_height = frame_->height();
1276#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001277 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001279 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001281 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001282}
1283
1284
ager@chromium.org7c537e22008-10-16 08:43:32 +00001285void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001286#ifdef DEBUG
1287 int original_height = frame_->height();
1288#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001289 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001290 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001291 // Generate different code depending on which parts of the if statement
1292 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001293 bool has_then_stm = node->HasThenStatement();
1294 bool has_else_stm = node->HasElseStatement();
1295
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001296 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001297
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001298 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001299 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001300 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001301 JumpTarget then;
1302 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001304 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1305 &then, &else_, true);
1306 if (frame_ != NULL) {
1307 Branch(false, &else_);
1308 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001310 if (frame_ != NULL || then.is_linked()) {
1311 then.Bind();
1312 VisitAndSpill(node->then_statement());
1313 }
1314 if (frame_ != NULL) {
1315 exit.Jump();
1316 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001317 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001318 if (else_.is_linked()) {
1319 else_.Bind();
1320 VisitAndSpill(node->else_statement());
1321 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322
1323 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001324 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001325 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001326 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001327 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001328 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1329 &then, &exit, true);
1330 if (frame_ != NULL) {
1331 Branch(false, &exit);
1332 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001333 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001334 if (frame_ != NULL || then.is_linked()) {
1335 then.Bind();
1336 VisitAndSpill(node->then_statement());
1337 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338
1339 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001340 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001342 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001343 // if (!cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001344 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1345 &exit, &else_, true);
1346 if (frame_ != NULL) {
1347 Branch(true, &exit);
1348 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001350 if (frame_ != NULL || else_.is_linked()) {
1351 else_.Bind();
1352 VisitAndSpill(node->else_statement());
1353 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001354
1355 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001356 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001357 ASSERT(!has_then_stm && !has_else_stm);
1358 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001359 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1360 &exit, &exit, false);
1361 if (frame_ != NULL) {
1362 if (has_cc()) {
1363 cc_reg_ = al;
1364 } else {
1365 frame_->Drop();
1366 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001367 }
1368 }
1369
1370 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001371 if (exit.is_linked()) {
1372 exit.Bind();
1373 }
1374 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001375}
1376
1377
ager@chromium.org7c537e22008-10-16 08:43:32 +00001378void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001379 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001380 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001381 CodeForStatementPosition(node);
1382 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001383}
1384
1385
ager@chromium.org7c537e22008-10-16 08:43:32 +00001386void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001387 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001388 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001389 CodeForStatementPosition(node);
1390 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001391}
1392
1393
ager@chromium.org7c537e22008-10-16 08:43:32 +00001394void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001395 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001396 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001397
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001398 if (function_return_is_shadowed_) {
1399 CodeForStatementPosition(node);
1400 LoadAndSpill(node->expression());
1401 frame_->EmitPop(r0);
1402 function_return_.Jump();
1403 } else {
1404 // Load the returned value.
1405 CodeForStatementPosition(node);
1406 LoadAndSpill(node->expression());
1407
1408 // Pop the result from the frame and prepare the frame for
1409 // returning thus making it easier to merge.
1410 frame_->EmitPop(r0);
1411 frame_->PrepareForReturn();
1412
1413 function_return_.Jump();
1414 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001415}
1416
1417
ager@chromium.org7c537e22008-10-16 08:43:32 +00001418void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001419#ifdef DEBUG
1420 int original_height = frame_->height();
1421#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001422 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001423 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001424 CodeForStatementPosition(node);
1425 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001426 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001427 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001428 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001429 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001430 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001431#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001432 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001433 __ cmp(r0, Operand(cp));
1434 verified_true.Branch(eq);
1435 __ stop("PushContext: r0 is expected to be the same as cp");
1436 verified_true.Bind();
1437#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001439 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001440 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441}
1442
1443
ager@chromium.org7c537e22008-10-16 08:43:32 +00001444void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001445#ifdef DEBUG
1446 int original_height = frame_->height();
1447#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001448 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001450 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001451 // Pop context.
1452 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1453 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001454 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001455 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001456}
1457
1458
ager@chromium.org7c537e22008-10-16 08:43:32 +00001459int CodeGenerator::FastCaseSwitchMaxOverheadFactor() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001460 return kFastSwitchMaxOverheadFactor;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001461}
1462
ager@chromium.org7c537e22008-10-16 08:43:32 +00001463int CodeGenerator::FastCaseSwitchMinCaseCount() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001464 return kFastSwitchMinCaseCount;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001465}
1466
1467
ager@chromium.org7c537e22008-10-16 08:43:32 +00001468void CodeGenerator::GenerateFastCaseSwitchJumpTable(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001469 SwitchStatement* node,
1470 int min_index,
1471 int range,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001472 Label* default_label,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001473 Vector<Label*> case_targets,
1474 Vector<Label> case_labels) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001475 VirtualFrame::SpilledScope spilled_scope;
1476 JumpTarget setup_default;
1477 JumpTarget is_smi;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001478
1479 // A non-null default label pointer indicates a default case among
1480 // the case labels. Otherwise we use the break target as a
1481 // "default" for failure to hit the jump table.
1482 JumpTarget* default_target =
1483 (default_label == NULL) ? node->break_target() : &setup_default;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001484
1485 ASSERT(kSmiTag == 0 && kSmiTagSize <= 2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001486 frame_->EmitPop(r0);
ager@chromium.org870a0b62008-11-04 11:43:05 +00001487
1488 // Test for a Smi value in a HeapNumber.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001489 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001490 is_smi.Branch(eq);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001491 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1492 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
ager@chromium.org870a0b62008-11-04 11:43:05 +00001493 __ cmp(r1, Operand(HEAP_NUMBER_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001494 default_target->Branch(ne);
1495 frame_->EmitPush(r0);
1496 frame_->CallRuntime(Runtime::kNumberToSmi, 1);
1497 is_smi.Bind();
ager@chromium.org870a0b62008-11-04 11:43:05 +00001498
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001499 if (min_index != 0) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001500 // Small positive numbers can be immediate operands.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001501 if (min_index < 0) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001502 // If min_index is Smi::kMinValue, -min_index is not a Smi.
1503 if (Smi::IsValid(-min_index)) {
1504 __ add(r0, r0, Operand(Smi::FromInt(-min_index)));
1505 } else {
1506 __ add(r0, r0, Operand(Smi::FromInt(-min_index - 1)));
1507 __ add(r0, r0, Operand(Smi::FromInt(1)));
1508 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001509 } else {
1510 __ sub(r0, r0, Operand(Smi::FromInt(min_index)));
1511 }
1512 }
1513 __ tst(r0, Operand(0x80000000 | kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001514 default_target->Branch(ne);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001515 __ cmp(r0, Operand(Smi::FromInt(range)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001516 default_target->Branch(ge);
1517 VirtualFrame* start_frame = new VirtualFrame(frame_);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001518 __ SmiJumpTable(r0, case_targets);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001519
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001520 GenerateFastCaseSwitchCases(node, case_labels, start_frame);
1521
1522 // If there was a default case among the case labels, we need to
1523 // emit code to jump to it from the default target used for failure
1524 // to hit the jump table.
1525 if (default_label != NULL) {
1526 if (has_valid_frame()) {
1527 node->break_target()->Jump();
1528 }
1529 setup_default.Bind();
1530 frame_->MergeTo(start_frame);
1531 __ b(default_label);
1532 DeleteFrame();
1533 }
1534 if (node->break_target()->is_linked()) {
1535 node->break_target()->Bind();
1536 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001537}
1538
1539
ager@chromium.org7c537e22008-10-16 08:43:32 +00001540void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001541#ifdef DEBUG
1542 int original_height = frame_->height();
1543#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001544 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001545 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001546 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001547 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001548
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001549 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001550 if (TryGenerateFastCaseSwitchStatement(node)) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001551 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1552 return;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001553 }
1554
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001555 JumpTarget next_test;
1556 JumpTarget fall_through;
1557 JumpTarget default_entry;
1558 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001559 ZoneList<CaseClause*>* cases = node->cases();
1560 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001561 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001562
1563 for (int i = 0; i < length; i++) {
1564 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001565 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001566 // Remember the default clause and compile it at the end.
1567 default_clause = clause;
1568 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001569 }
1570
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001571 Comment cmnt(masm_, "[ Case clause");
1572 // Compile the test.
1573 next_test.Bind();
1574 next_test.Unuse();
1575 // Duplicate TOS.
1576 __ ldr(r0, frame_->Top());
1577 frame_->EmitPush(r0);
1578 LoadAndSpill(clause->label());
1579 Comparison(eq, true);
1580 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001581
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001582 // Before entering the body from the test, remove the switch value from
1583 // the stack.
1584 frame_->Drop();
1585
1586 // Label the body so that fall through is enabled.
1587 if (i > 0 && cases->at(i - 1)->is_default()) {
1588 default_exit.Bind();
1589 } else {
1590 fall_through.Bind();
1591 fall_through.Unuse();
1592 }
1593 VisitStatementsAndSpill(clause->statements());
1594
1595 // If control flow can fall through from the body, jump to the next body
1596 // or the end of the statement.
1597 if (frame_ != NULL) {
1598 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1599 default_entry.Jump();
1600 } else {
1601 fall_through.Jump();
1602 }
1603 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001604 }
1605
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001606 // The final "test" removes the switch value.
1607 next_test.Bind();
1608 frame_->Drop();
1609
1610 // If there is a default clause, compile it.
1611 if (default_clause != NULL) {
1612 Comment cmnt(masm_, "[ Default clause");
1613 default_entry.Bind();
1614 VisitStatementsAndSpill(default_clause->statements());
1615 // If control flow can fall out of the default and there is a case after
1616 // it, jup to that case's body.
1617 if (frame_ != NULL && default_exit.is_bound()) {
1618 default_exit.Jump();
1619 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001620 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001621
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001622 if (fall_through.is_linked()) {
1623 fall_through.Bind();
1624 }
1625
1626 if (node->break_target()->is_linked()) {
1627 node->break_target()->Bind();
1628 }
1629 node->break_target()->Unuse();
1630 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001631}
1632
1633
ager@chromium.org7c537e22008-10-16 08:43:32 +00001634void CodeGenerator::VisitLoopStatement(LoopStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001635#ifdef DEBUG
1636 int original_height = frame_->height();
1637#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001638 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001639 Comment cmnt(masm_, "[ LoopStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001640 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001641 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001642
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001643 // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a
1644 // known result for the test expression, with no side effects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW;
1646 if (node->cond() == NULL) {
1647 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1648 info = ALWAYS_TRUE;
1649 } else {
1650 Literal* lit = node->cond()->AsLiteral();
1651 if (lit != NULL) {
1652 if (lit->IsTrue()) {
1653 info = ALWAYS_TRUE;
1654 } else if (lit->IsFalse()) {
1655 info = ALWAYS_FALSE;
1656 }
1657 }
1658 }
1659
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001660 switch (node->type()) {
1661 case LoopStatement::DO_LOOP: {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001662 JumpTarget body(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001663
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001664 // Label the top of the loop for the backward CFG edge. If the test
1665 // is always true we can use the continue target, and if the test is
1666 // always false there is no need.
1667 if (info == ALWAYS_TRUE) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001668 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001669 node->continue_target()->Bind();
1670 } else if (info == ALWAYS_FALSE) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001671 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001672 } else {
1673 ASSERT(info == DONT_KNOW);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001674 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001675 body.Bind();
1676 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001677
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001678 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001679 VisitAndSpill(node->body());
1680
1681 // Compile the test.
1682 if (info == ALWAYS_TRUE) {
1683 if (has_valid_frame()) {
1684 // If control can fall off the end of the body, jump back to the
1685 // top.
1686 node->continue_target()->Jump();
1687 }
1688 } else if (info == ALWAYS_FALSE) {
1689 // If we have a continue in the body, we only have to bind its jump
1690 // target.
1691 if (node->continue_target()->is_linked()) {
1692 node->continue_target()->Bind();
1693 }
1694 } else {
1695 ASSERT(info == DONT_KNOW);
1696 // We have to compile the test expression if it can be reached by
1697 // control flow falling out of the body or via continue.
1698 if (node->continue_target()->is_linked()) {
1699 node->continue_target()->Bind();
1700 }
1701 if (has_valid_frame()) {
1702 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1703 &body, node->break_target(), true);
1704 if (has_valid_frame()) {
1705 // A invalid frame here indicates that control did not
1706 // fall out of the test expression.
1707 Branch(true, &body);
1708 }
1709 }
1710 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001711 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001712 }
1713
1714 case LoopStatement::WHILE_LOOP: {
1715 // If the test is never true and has no side effects there is no need
1716 // to compile the test or body.
1717 if (info == ALWAYS_FALSE) break;
1718
1719 // Label the top of the loop with the continue target for the backward
1720 // CFG edge.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001721 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001722 node->continue_target()->Bind();
1723
1724 if (info == DONT_KNOW) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001725 JumpTarget body;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001726 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1727 &body, node->break_target(), true);
1728 if (has_valid_frame()) {
1729 // A NULL frame indicates that control did not fall out of the
1730 // test expression.
1731 Branch(false, node->break_target());
1732 }
1733 if (has_valid_frame() || body.is_linked()) {
1734 body.Bind();
1735 }
1736 }
1737
1738 if (has_valid_frame()) {
1739 CheckStack(); // TODO(1222600): ignore if body contains calls.
1740 VisitAndSpill(node->body());
1741
1742 // If control flow can fall out of the body, jump back to the top.
1743 if (has_valid_frame()) {
1744 node->continue_target()->Jump();
1745 }
1746 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001747 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001748 }
1749
1750 case LoopStatement::FOR_LOOP: {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001751 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001752
1753 if (node->init() != NULL) {
1754 VisitAndSpill(node->init());
1755 }
1756
1757 // There is no need to compile the test or body.
1758 if (info == ALWAYS_FALSE) break;
1759
1760 // If there is no update statement, label the top of the loop with the
1761 // continue target, otherwise with the loop target.
1762 if (node->next() == NULL) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001763 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001764 node->continue_target()->Bind();
1765 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001766 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001767 loop.Bind();
1768 }
1769
1770 // If the test is always true, there is no need to compile it.
1771 if (info == DONT_KNOW) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001772 JumpTarget body;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001773 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1774 &body, node->break_target(), true);
1775 if (has_valid_frame()) {
1776 Branch(false, node->break_target());
1777 }
1778 if (has_valid_frame() || body.is_linked()) {
1779 body.Bind();
1780 }
1781 }
1782
1783 if (has_valid_frame()) {
1784 CheckStack(); // TODO(1222600): ignore if body contains calls.
1785 VisitAndSpill(node->body());
1786
1787 if (node->next() == NULL) {
1788 // If there is no update statement and control flow can fall out
1789 // of the loop, jump directly to the continue label.
1790 if (has_valid_frame()) {
1791 node->continue_target()->Jump();
1792 }
1793 } else {
1794 // If there is an update statement and control flow can reach it
1795 // via falling out of the body of the loop or continuing, we
1796 // compile the update statement.
1797 if (node->continue_target()->is_linked()) {
1798 node->continue_target()->Bind();
1799 }
1800 if (has_valid_frame()) {
1801 // Record source position of the statement as this code which is
1802 // after the code for the body actually belongs to the loop
1803 // statement and not the body.
1804 CodeForStatementPosition(node);
1805 VisitAndSpill(node->next());
1806 loop.Jump();
1807 }
1808 }
1809 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001810 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001811 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001812 }
1813
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001814 if (node->break_target()->is_linked()) {
1815 node->break_target()->Bind();
1816 }
1817 node->continue_target()->Unuse();
1818 node->break_target()->Unuse();
1819 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820}
1821
1822
ager@chromium.org7c537e22008-10-16 08:43:32 +00001823void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001824#ifdef DEBUG
1825 int original_height = frame_->height();
1826#endif
1827 ASSERT(!in_spilled_code());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001828 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001830 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001831
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001832 JumpTarget primitive;
1833 JumpTarget jsobject;
1834 JumpTarget fixed_array;
1835 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
1836 JumpTarget end_del_check;
1837 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001838
1839 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001840 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001841
1842 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1843 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001844 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001845 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001846 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001847 __ cmp(r0, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001848 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001849
1850 // Stack layout in body:
1851 // [iteration counter (Smi)]
1852 // [length of array]
1853 // [FixedArray]
1854 // [Map or 0]
1855 // [Object]
1856
1857 // Check if enumerable is already a JSObject
1858 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001859 primitive.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001860 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1861 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00001862 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001863 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001864
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001865 primitive.Bind();
1866 frame_->EmitPush(r0);
1867 Result arg_count = allocator_->Allocate(r0);
1868 ASSERT(arg_count.is_valid());
1869 __ mov(arg_count.reg(), Operand(0));
1870 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001871
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001872 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001873 // Get the set of properties (as a FixedArray or Map).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001874 frame_->EmitPush(r0); // duplicate the object being enumerated
1875 frame_->EmitPush(r0);
1876 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001877
1878 // If we got a Map, we can do a fast modification check.
1879 // Otherwise, we got a FixedArray, and we have to do a slow check.
1880 __ mov(r2, Operand(r0));
1881 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
1882 __ cmp(r1, Operand(Factory::meta_map()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001883 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884
1885 // Get enum cache
1886 __ mov(r1, Operand(r0));
1887 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1888 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1889 __ ldr(r2,
1890 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1891
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001892 frame_->EmitPush(r0); // map
1893 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001894 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001895 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001896 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001897 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001898 frame_->EmitPush(r0);
1899 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001900
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001901 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001902 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001903 frame_->EmitPush(r1); // insert 0 in place of Map
1904 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001905
1906 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001907 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001908 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001909 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001910 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001911 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001912
1913 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001914 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00001915 // sp[0] : index
1916 // sp[1] : array/enum cache length
1917 // sp[2] : array or enum cache
1918 // sp[3] : 0 or map
1919 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001920 // Grab the current frame's height for the break and continue
1921 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001922 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1923 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001925 __ ldr(r0, frame_->ElementAt(0)); // load the current count
1926 __ ldr(r1, frame_->ElementAt(1)); // load the length
1927 __ cmp(r0, Operand(r1)); // compare to the array length
1928 node->break_target()->Branch(hs);
1929
1930 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001931
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001932 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001933 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001934 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1935 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1936
1937 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001938 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001939 // Check if this (still) matches the map of the enumerable.
1940 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001941 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001942 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1943 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001944 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945
1946 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001947 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
1948 frame_->EmitPush(r0);
1949 frame_->EmitPush(r3); // push entry
1950 Result arg_count_register = allocator_->Allocate(r0);
1951 ASSERT(arg_count_register.is_valid());
1952 __ mov(arg_count_register.reg(), Operand(1));
1953 Result result = frame_->InvokeBuiltin(Builtins::FILTER_KEY,
1954 CALL_JS,
1955 &arg_count_register,
1956 2);
1957 __ mov(r3, Operand(result.reg()));
1958 result.Unuse();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959
1960 // If the property has been removed while iterating, we just skip it.
1961 __ cmp(r3, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001962 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001963
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001964 end_del_check.Bind();
1965 // Store the entry in the 'each' expression and take another spin in the
1966 // loop. r3: i'th entry of the enum cache (or string there of)
1967 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001968 { Reference each(this, node->each());
1969 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001970 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001971 __ ldr(r0, frame_->ElementAt(each.size()));
1972 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001973 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001974 // If the reference was to a slot we rely on the convenient property
1975 // that it doesn't matter whether a value (eg, r3 pushed above) is
1976 // right on top of or right underneath a zero-sized reference.
1977 each.SetValue(NOT_CONST_INIT);
mads.s.ager31e71382008-08-13 09:32:07 +00001978 if (each.size() > 0) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00001979 // It's safe to pop the value lying on top of the reference before
1980 // unloading the reference itself (which preserves the top of stack,
1981 // ie, now the topmost value of the non-zero sized reference), since
1982 // we will discard the top of stack after unloading the reference
1983 // anyway.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001984 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001985 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001986 }
1987 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001988 // Discard the i'th entry pushed above or else the remainder of the
1989 // reference, whichever is currently on top of the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001990 frame_->Drop();
1991
1992 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001994 VisitAndSpill(node->body());
1995
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001996 // Next. Reestablish a spilled frame in case we are coming here via
1997 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001998 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001999 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002000 frame_->EmitPop(r0);
2001 __ add(r0, r0, Operand(Smi::FromInt(1)));
2002 frame_->EmitPush(r0);
2003 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002004
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002005 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2006 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002007 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002008 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002009
2010 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002011 exit.Bind();
2012 node->continue_target()->Unuse();
2013 node->break_target()->Unuse();
2014 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002015}
2016
2017
ager@chromium.org7c537e22008-10-16 08:43:32 +00002018void CodeGenerator::VisitTryCatch(TryCatch* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002019#ifdef DEBUG
2020 int original_height = frame_->height();
2021#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002022 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002023 Comment cmnt(masm_, "[ TryCatch");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002024 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002025
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002026 JumpTarget try_block;
2027 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002028
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002029 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002030 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002031 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032
2033 // Store the caught exception in the catch variable.
2034 { Reference ref(this, node->catch_var());
ager@chromium.org7c537e22008-10-16 08:43:32 +00002035 ASSERT(ref.is_slot());
2036 // Here we make use of the convenient property that it doesn't matter
2037 // whether a value is immediately on top of or underneath a zero-sized
2038 // reference.
2039 ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040 }
2041
2042 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002043 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002044
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002045 VisitStatementsAndSpill(node->catch_block()->statements());
2046 if (frame_ != NULL) {
2047 exit.Jump();
2048 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002049
2050
2051 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002052 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002053
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002054 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2055 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002057 // Shadow the labels for all escapes from the try block, including
2058 // returns. During shadowing, the original label is hidden as the
2059 // LabelShadow and operations on the original actually affect the
2060 // shadowing label.
2061 //
2062 // We should probably try to unify the escaping labels and the return
2063 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002064 int nof_escapes = node->escaping_targets()->length();
2065 List<ShadowTarget*> shadows(1 + nof_escapes);
2066
2067 // Add the shadow target for the function return.
2068 static const int kReturnShadowIndex = 0;
2069 shadows.Add(new ShadowTarget(&function_return_));
2070 bool function_return_was_shadowed = function_return_is_shadowed_;
2071 function_return_is_shadowed_ = true;
2072 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2073
2074 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002076 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002077 }
2078
2079 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002080 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081
2082 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002083 // After shadowing stops, the original labels are unshadowed and the
2084 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002085 bool has_unlinks = false;
2086 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002087 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002088 has_unlinks = has_unlinks || shadows[i]->is_linked();
2089 }
2090 function_return_is_shadowed_ = function_return_was_shadowed;
2091
2092 // Get an external reference to the handler address.
2093 ExternalReference handler_address(Top::k_handler_address);
2094
2095 // The next handler address is at kNextIndex in the stack.
2096 const int kNextIndex = StackHandlerConstants::kNextOffset / kPointerSize;
2097 // If we can fall off the end of the try block, unlink from try chain.
2098 if (has_valid_frame()) {
2099 __ ldr(r1, frame_->ElementAt(kNextIndex));
2100 __ mov(r3, Operand(handler_address));
2101 __ str(r1, MemOperand(r3));
2102 frame_->Drop(StackHandlerConstants::kSize / kPointerSize);
2103 if (has_unlinks) {
2104 exit.Jump();
2105 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106 }
2107
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002108 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002109 // jumped to. Deallocate each shadow target.
2110 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002111 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002112 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002113 shadows[i]->Bind();
2114 // Because we can be jumping here (to spilled code) from unspilled
2115 // code, we need to reestablish a spilled frame at this block.
2116 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002117
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002118 // Reload sp from the top handler, because some statements that we
2119 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002120 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002122 // The stack pointer was restored to just below the code slot
2123 // (the topmost slot) in the handler.
2124 frame_->Forget(frame_->height() - handler_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002125
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002126 // kNextIndex is off by one because the code slot has already
2127 // been dropped.
2128 __ ldr(r1, frame_->ElementAt(kNextIndex - 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002129 __ str(r1, MemOperand(r3));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002130 // The code slot has already been dropped from the handler.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002131 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002132
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002133 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2134 frame_->PrepareForReturn();
2135 }
2136 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002137 }
2138 }
2139
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002140 exit.Bind();
2141 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002142}
2143
2144
ager@chromium.org7c537e22008-10-16 08:43:32 +00002145void CodeGenerator::VisitTryFinally(TryFinally* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002146#ifdef DEBUG
2147 int original_height = frame_->height();
2148#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002149 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002150 Comment cmnt(masm_, "[ TryFinally");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002151 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002152
2153 // State: Used to keep track of reason for entering the finally
2154 // block. Should probably be extended to hold information for
2155 // break/continue from within the try block.
2156 enum { FALLING, THROWING, JUMPING };
2157
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002158 JumpTarget try_block;
2159 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002160
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002161 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002162
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002163 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002164 // In case of thrown exceptions, this is where we continue.
2165 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002166 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167
2168 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002169 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002170
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002171 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2172 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002173
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002174 // Shadow the labels for all escapes from the try block, including
2175 // returns. Shadowing hides the original label as the LabelShadow and
2176 // operations on the original actually affect the shadowing label.
2177 //
2178 // We should probably try to unify the escaping labels and the return
2179 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002180 int nof_escapes = node->escaping_targets()->length();
2181 List<ShadowTarget*> shadows(1 + nof_escapes);
2182
2183 // Add the shadow target for the function return.
2184 static const int kReturnShadowIndex = 0;
2185 shadows.Add(new ShadowTarget(&function_return_));
2186 bool function_return_was_shadowed = function_return_is_shadowed_;
2187 function_return_is_shadowed_ = true;
2188 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2189
2190 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002191 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002192 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002193 }
2194
2195 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002196 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002197
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002198 // Stop the introduced shadowing and count the number of required unlinks.
2199 // After shadowing stops, the original labels are unshadowed and the
2200 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002202 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002203 shadows[i]->StopShadowing();
2204 if (shadows[i]->is_linked()) nof_unlinks++;
2205 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002206 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002207
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002208 // Get an external reference to the handler address.
2209 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002210
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002211 // The next handler address is at kNextIndex in the stack.
2212 const int kNextIndex = StackHandlerConstants::kNextOffset / kPointerSize;
2213 // If we can fall off the end of the try block, unlink from the try
2214 // chain and set the state on the frame to FALLING.
2215 if (has_valid_frame()) {
2216 __ ldr(r1, frame_->ElementAt(kNextIndex));
2217 __ mov(r3, Operand(handler_address));
2218 __ str(r1, MemOperand(r3));
2219 frame_->Drop(StackHandlerConstants::kSize / kPointerSize);
2220
2221 // Fake a top of stack value (unneeded when FALLING) and set the
2222 // state in r2, then jump around the unlink blocks if any.
2223 __ mov(r0, Operand(Factory::undefined_value()));
2224 frame_->EmitPush(r0);
2225 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2226 if (nof_unlinks > 0) {
2227 finally_block.Jump();
2228 }
2229 }
2230
2231 // Generate code to unlink and set the state for the (formerly)
2232 // shadowing targets that have been jumped to.
2233 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002234 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002235 // If we have come from the shadowed return, the return value is
2236 // in (a non-refcounted reference to) r0. We must preserve it
2237 // until it is pushed.
2238 //
2239 // Because we can be jumping here (to spilled code) from
2240 // unspilled code, we need to reestablish a spilled frame at
2241 // this block.
2242 shadows[i]->Bind();
2243 frame_->SpillAll();
2244
2245 // Reload sp from the top handler, because some statements that
2246 // we break from (eg, for...in) may have left stuff on the
2247 // stack.
2248 __ mov(r3, Operand(handler_address));
2249 __ ldr(sp, MemOperand(r3));
2250 // The stack pointer was restored to the address slot in the handler.
2251 ASSERT(StackHandlerConstants::kNextOffset == 1 * kPointerSize);
2252 frame_->Forget(frame_->height() - handler_height + 1);
2253
2254 // Unlink this handler and drop it from the frame. The next
2255 // handler address is now on top of the frame.
2256 frame_->EmitPop(r1);
2257 __ str(r1, MemOperand(r3));
2258 // The top (code) and the second (handler) slot have both been
2259 // dropped already.
2260 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 2);
2261
2262 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002263 // If this label shadowed the function return, materialize the
2264 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002265 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002266 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002267 // Fake TOS for targets that shadowed breaks and continues.
mads.s.ager31e71382008-08-13 09:32:07 +00002268 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002269 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002270 }
2271 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002272 if (--nof_unlinks > 0) {
2273 // If this is not the last unlink block, jump around the next.
2274 finally_block.Jump();
2275 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276 }
2277 }
2278
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002279 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002280 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002281
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002282 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002283 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002284
2285 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002286 // and the state - while evaluating the finally block.
2287 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002288 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002289 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002290
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002291 if (has_valid_frame()) {
2292 // Restore state and return value or faked TOS.
2293 frame_->EmitPop(r2);
2294 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002295 }
2296
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002297 // Generate code to jump to the right destination for all used
2298 // formerly shadowing targets. Deallocate each shadow target.
2299 for (int i = 0; i < shadows.length(); i++) {
2300 if (has_valid_frame() && shadows[i]->is_bound()) {
2301 JumpTarget* original = shadows[i]->other_target();
2302 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2303 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002304 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002305 skip.Branch(ne);
2306 frame_->PrepareForReturn();
2307 original->Jump();
2308 skip.Bind();
2309 } else {
2310 original->Branch(eq);
2311 }
2312 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002313 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002314
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002315 if (has_valid_frame()) {
2316 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002317 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002318 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2319 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002320
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002321 // Rethrow exception.
2322 frame_->EmitPush(r0);
2323 frame_->CallRuntime(Runtime::kReThrow, 1);
2324
2325 // Done.
2326 exit.Bind();
2327 }
2328 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002329}
2330
2331
ager@chromium.org7c537e22008-10-16 08:43:32 +00002332void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002333#ifdef DEBUG
2334 int original_height = frame_->height();
2335#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002336 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002337 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002338 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002339#ifdef ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002340 frame_->CallRuntime(Runtime::kDebugBreak, 0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002341#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002342 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002343 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002344}
2345
2346
ager@chromium.org7c537e22008-10-16 08:43:32 +00002347void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002348 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002349 ASSERT(boilerplate->IsBoilerplate());
2350
2351 // Push the boilerplate on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002352 __ mov(r0, Operand(boilerplate));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002353 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002354
2355 // Create a new closure.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002356 frame_->EmitPush(cp);
2357 frame_->CallRuntime(Runtime::kNewClosure, 2);
2358 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002359}
2360
2361
ager@chromium.org7c537e22008-10-16 08:43:32 +00002362void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002363#ifdef DEBUG
2364 int original_height = frame_->height();
2365#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002366 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002367 Comment cmnt(masm_, "[ FunctionLiteral");
2368
2369 // Build the function boilerplate and instantiate it.
2370 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00002371 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002372 if (HasStackOverflow()) {
2373 ASSERT(frame_->height() == original_height);
2374 return;
2375 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002376 InstantiateBoilerplate(boilerplate);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002377 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002378}
2379
2380
ager@chromium.org7c537e22008-10-16 08:43:32 +00002381void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002382 FunctionBoilerplateLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002383#ifdef DEBUG
2384 int original_height = frame_->height();
2385#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002386 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002387 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2388 InstantiateBoilerplate(node->boilerplate());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002389 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002390}
2391
2392
ager@chromium.org7c537e22008-10-16 08:43:32 +00002393void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002394#ifdef DEBUG
2395 int original_height = frame_->height();
2396#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002397 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002398 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002399 JumpTarget then;
2400 JumpTarget else_;
2401 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002402 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
2403 &then, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002404 Branch(false, &else_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002405 then.Bind();
2406 LoadAndSpill(node->then_expression(), typeof_state());
2407 exit.Jump();
2408 else_.Bind();
2409 LoadAndSpill(node->else_expression(), typeof_state());
2410 exit.Bind();
2411 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002412}
2413
2414
ager@chromium.org7c537e22008-10-16 08:43:32 +00002415void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002416 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002417 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002418 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002419
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002420 JumpTarget slow;
2421 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002422
2423 // Generate fast-case code for variables that might be shadowed by
2424 // eval-introduced variables. Eval is used a lot without
2425 // introducing variables. In those cases, we do not want to
2426 // perform a runtime call for all variables in the scope
2427 // containing the eval.
2428 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2429 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002430 // If there was no control flow to slow, we can exit early.
2431 if (!slow.is_linked()) {
2432 frame_->EmitPush(r0);
2433 return;
2434 }
2435
2436 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002437
2438 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2439 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2440 // Only generate the fast case for locals that rewrite to slots.
2441 // This rules out argument loads.
2442 if (potential_slot != NULL) {
2443 __ ldr(r0,
2444 ContextSlotOperandCheckExtensions(potential_slot,
2445 r1,
2446 r2,
2447 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002448 if (potential_slot->var()->mode() == Variable::CONST) {
2449 __ cmp(r0, Operand(Factory::the_hole_value()));
2450 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
2451 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002452 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002453 // ContextSlotOperandCheckExtensions so we have to jump around
2454 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002455 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002456 }
2457 }
2458
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002459 slow.Bind();
2460 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002461 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002462 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002463
ager@chromium.org7c537e22008-10-16 08:43:32 +00002464 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002465 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002466 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002467 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002469
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002470 done.Bind();
2471 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002472
2473 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002474 // Note: We would like to keep the assert below, but it fires because of
2475 // some nasty code in LoadTypeofExpression() which should be removed...
ager@chromium.org381abbb2009-02-25 13:23:22 +00002476 // ASSERT(!slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002477
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002478 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002479 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002480 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002481 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002482 // Const slots may contain 'the hole' value (the constant hasn't been
2483 // initialized yet) which needs to be converted into the 'undefined'
2484 // value.
2485 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002486 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002487 __ cmp(r0, Operand(Factory::the_hole_value()));
2488 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002489 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002490 }
2491 }
2492}
2493
2494
ager@chromium.org381abbb2009-02-25 13:23:22 +00002495void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2496 TypeofState typeof_state,
2497 Register tmp,
2498 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002499 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002500 // Check that no extension objects have been created by calls to
2501 // eval from the current scope to the global scope.
2502 Register context = cp;
2503 Scope* s = scope();
2504 while (s != NULL) {
2505 if (s->num_heap_slots() > 0) {
2506 if (s->calls_eval()) {
2507 // Check that extension is NULL.
2508 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2509 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002510 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002511 }
2512 // Load next context in chain.
2513 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2514 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2515 context = tmp;
2516 }
2517 // If no outer scope calls eval, we do not need to check more
2518 // context extensions.
2519 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2520 s = s->outer_scope();
2521 }
2522
2523 if (s->is_eval_scope()) {
2524 Label next, fast;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002525 if (!context.is(tmp)) {
2526 __ mov(tmp, Operand(context));
2527 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002528 __ bind(&next);
2529 // Terminate at global context.
2530 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
2531 __ cmp(tmp2, Operand(Factory::global_context_map()));
2532 __ b(eq, &fast);
2533 // Check that extension is NULL.
2534 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2535 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002536 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002537 // Load next context in chain.
2538 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2539 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2540 __ b(&next);
2541 __ bind(&fast);
2542 }
2543
2544 // All extension objects were empty and it is safe to use a global
2545 // load IC call.
2546 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2547 // Load the global object.
2548 LoadGlobal();
2549 // Setup the name register.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002550 Result name = allocator_->Allocate(r2);
2551 ASSERT(name.is_valid()); // We are in spilled code.
2552 __ mov(name.reg(), Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002553 // Call IC stub.
2554 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002555 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002556 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002557 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002558 }
2559
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002560 // Drop the global object. The result is in r0.
2561 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002562}
2563
2564
ager@chromium.org7c537e22008-10-16 08:43:32 +00002565void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002566#ifdef DEBUG
2567 int original_height = frame_->height();
2568#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002569 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002570 Comment cmnt(masm_, "[ Slot");
2571 LoadFromSlot(node, typeof_state());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002572 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002573}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002574
ager@chromium.org7c537e22008-10-16 08:43:32 +00002575
2576void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002577#ifdef DEBUG
2578 int original_height = frame_->height();
2579#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002580 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002581 Comment cmnt(masm_, "[ VariableProxy");
2582
2583 Variable* var = node->var();
2584 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002585 if (expr != NULL) {
2586 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002587 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002588 ASSERT(var->is_global());
2589 Reference ref(this, node);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002590 ref.GetValueAndSpill(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002591 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002592 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002593}
2594
2595
ager@chromium.org7c537e22008-10-16 08:43:32 +00002596void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002597#ifdef DEBUG
2598 int original_height = frame_->height();
2599#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002600 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002601 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002602 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002603 frame_->EmitPush(r0);
2604 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002605}
2606
2607
ager@chromium.org7c537e22008-10-16 08:43:32 +00002608void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002609#ifdef DEBUG
2610 int original_height = frame_->height();
2611#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002612 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002613 Comment cmnt(masm_, "[ RexExp Literal");
2614
2615 // Retrieve the literal array and check the allocated entry.
2616
2617 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002618 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002619
2620 // Load the literals array of the function.
2621 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2622
2623 // Load the literal at the ast saved index.
2624 int literal_offset =
2625 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2626 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2627
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002628 JumpTarget done;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002629 __ cmp(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002630 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002631
2632 // If the entry is undefined we call the runtime system to computed
2633 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002634 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002635 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002636 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002637 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002638 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002639 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002640 frame_->EmitPush(r0);
2641 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002642 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002643
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002644 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002645 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002646 frame_->EmitPush(r2);
2647 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002648}
2649
2650
2651// This deferred code stub will be used for creating the boilerplate
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002652// by calling Runtime_CreateObjectLiteralBoilerplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002653// Each created boilerplate is stored in the JSFunction and they are
2654// therefore context dependent.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002655class DeferredObjectLiteral: public DeferredCode {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002656 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002657 DeferredObjectLiteral(CodeGenerator* generator, ObjectLiteral* node)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002658 : DeferredCode(generator), node_(node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002659 set_comment("[ DeferredObjectLiteral");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002660 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002661
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002662 virtual void Generate();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002663
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002664 private:
2665 ObjectLiteral* node_;
2666};
2667
2668
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002669void DeferredObjectLiteral::Generate() {
2670 // Argument is passed in r1.
2671 enter()->Bind();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002672 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002673
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002674 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002675 // the literal.
2676
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002677 VirtualFrame* frame = generator()->frame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002678 // Literal array (0).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002679 frame->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002680 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00002681 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002682 frame->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002683 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00002684 __ mov(r0, Operand(node_->constant_properties()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002685 frame->EmitPush(r0);
2686 Result boilerplate =
2687 frame->CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
2688 __ mov(r2, Operand(boilerplate.reg()));
2689 // Result is returned in r2.
2690 exit_.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002691}
2692
2693
ager@chromium.org7c537e22008-10-16 08:43:32 +00002694void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002695#ifdef DEBUG
2696 int original_height = frame_->height();
2697#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002698 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699 Comment cmnt(masm_, "[ ObjectLiteral");
2700
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002701 DeferredObjectLiteral* deferred = new DeferredObjectLiteral(this, node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002702
2703 // Retrieve the literal array and check the allocated entry.
2704
2705 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002706 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002707
2708 // Load the literals array of the function.
2709 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2710
2711 // Load the literal at the ast saved index.
2712 int literal_offset =
2713 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2714 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2715
2716 // Check whether we need to materialize the object literal boilerplate.
2717 // If so, jump to the deferred code.
2718 __ cmp(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002719 deferred->enter()->Branch(eq);
2720 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002721
2722 // Push the object literal boilerplate.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002723 frame_->EmitPush(r2);
mads.s.ager31e71382008-08-13 09:32:07 +00002724
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002725 // Clone the boilerplate object.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002726 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2727 if (node->depth() == 1) {
2728 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2729 }
2730 frame_->CallRuntime(clone_function_id, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002731 frame_->EmitPush(r0); // save the result
mads.s.ager31e71382008-08-13 09:32:07 +00002732 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002733
2734 for (int i = 0; i < node->properties()->length(); i++) {
2735 ObjectLiteral::Property* property = node->properties()->at(i);
2736 Literal* key = property->key();
2737 Expression* value = property->value();
2738 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002739 case ObjectLiteral::Property::CONSTANT:
2740 break;
2741 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2742 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
2743 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002744 case ObjectLiteral::Property::COMPUTED: // fall through
2745 case ObjectLiteral::Property::PROTOTYPE: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002746 frame_->EmitPush(r0); // dup the result
2747 LoadAndSpill(key);
2748 LoadAndSpill(value);
2749 frame_->CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002750 // restore r0
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002751 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002752 break;
2753 }
2754 case ObjectLiteral::Property::SETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002755 frame_->EmitPush(r0);
2756 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002757 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002758 frame_->EmitPush(r0);
2759 LoadAndSpill(value);
2760 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002761 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002762 break;
2763 }
2764 case ObjectLiteral::Property::GETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002765 frame_->EmitPush(r0);
2766 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002767 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002768 frame_->EmitPush(r0);
2769 LoadAndSpill(value);
2770 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002771 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002772 break;
2773 }
2774 }
2775 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002776 ASSERT(frame_->height() == original_height + 1);
2777}
2778
2779
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002780// This deferred code stub will be used for creating the boilerplate
2781// by calling Runtime_CreateArrayLiteralBoilerplate.
2782// Each created boilerplate is stored in the JSFunction and they are
2783// therefore context dependent.
2784class DeferredArrayLiteral: public DeferredCode {
2785 public:
2786 DeferredArrayLiteral(CodeGenerator* generator, ArrayLiteral* node)
2787 : DeferredCode(generator), node_(node) {
2788 set_comment("[ DeferredArrayLiteral");
2789 }
2790
2791 virtual void Generate();
2792
2793 private:
2794 ArrayLiteral* node_;
2795};
2796
2797
2798void DeferredArrayLiteral::Generate() {
2799 // Argument is passed in r1.
2800 enter()->Bind();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002801 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002802
2803 // If the entry is undefined we call the runtime system to computed
2804 // the literal.
2805
2806 VirtualFrame* frame = generator()->frame();
2807 // Literal array (0).
2808 frame->EmitPush(r1);
2809 // Literal index (1).
2810 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
2811 frame->EmitPush(r0);
2812 // Constant properties (2).
2813 __ mov(r0, Operand(node_->literals()));
2814 frame->EmitPush(r0);
2815 Result boilerplate =
2816 frame->CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3);
2817 __ mov(r2, Operand(boilerplate.reg()));
2818 // Result is returned in r2.
2819 exit_.Jump();
2820}
2821
2822
ager@chromium.org7c537e22008-10-16 08:43:32 +00002823void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002824#ifdef DEBUG
2825 int original_height = frame_->height();
2826#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002827 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002828 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002829
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002830 DeferredArrayLiteral* deferred = new DeferredArrayLiteral(this, node);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002831
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002832 // Retrieve the literal array and check the allocated entry.
2833
2834 // Load the function of this activation.
2835 __ ldr(r1, frame_->Function());
2836
2837 // Load the literals array of the function.
2838 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2839
2840 // Load the literal at the ast saved index.
2841 int literal_offset =
2842 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2843 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2844
2845 // Check whether we need to materialize the object literal boilerplate.
2846 // If so, jump to the deferred code.
2847 __ cmp(r2, Operand(Factory::undefined_value()));
2848 deferred->enter()->Branch(eq);
2849 deferred->BindExit();
2850
2851 // Push the object literal boilerplate.
2852 frame_->EmitPush(r2);
2853
2854 // Clone the boilerplate object.
2855 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2856 if (node->depth() == 1) {
2857 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2858 }
2859 frame_->CallRuntime(clone_function_id, 1);
2860 frame_->EmitPush(r0); // save the result
2861 // r0: cloned object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002862
2863 // Generate code to set the elements in the array that are not
2864 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002865 for (int i = 0; i < node->values()->length(); i++) {
2866 Expression* value = node->values()->at(i);
2867
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002868 // If value is a literal the property value is already set in the
2869 // boilerplate object.
2870 if (value->AsLiteral() != NULL) continue;
2871 // If value is a materialized literal the property value is already set
2872 // in the boilerplate object if it is simple.
2873 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002874
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002875 // The property must be set by generated code.
2876 LoadAndSpill(value);
2877 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002878
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002879 // Fetch the object literal.
2880 __ ldr(r1, frame_->Top());
2881 // Get the elements array.
2882 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002883
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002884 // Write to the indexed properties array.
2885 int offset = i * kPointerSize + Array::kHeaderSize;
2886 __ str(r0, FieldMemOperand(r1, offset));
2887
2888 // Update the write barrier for the array address.
2889 __ mov(r3, Operand(offset));
2890 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002891 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002892 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002893}
2894
2895
ager@chromium.org32912102009-01-16 10:38:43 +00002896void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002897#ifdef DEBUG
2898 int original_height = frame_->height();
2899#endif
2900 ASSERT(!in_spilled_code());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002901 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org32912102009-01-16 10:38:43 +00002902 // Call runtime routine to allocate the catch extension object and
2903 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002904 Comment cmnt(masm_, "[ CatchExtensionObject");
2905 LoadAndSpill(node->key());
2906 LoadAndSpill(node->value());
2907 Result result =
2908 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2909 frame_->EmitPush(result.reg());
2910 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002911}
2912
2913
ager@chromium.org7c537e22008-10-16 08:43:32 +00002914void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002915#ifdef DEBUG
2916 int original_height = frame_->height();
2917#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002918 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002919 Comment cmnt(masm_, "[ Assignment");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002920 CodeForStatementPosition(node);
mads.s.ager31e71382008-08-13 09:32:07 +00002921
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002922 { Reference target(this, node->target());
2923 if (target.is_illegal()) {
2924 // Fool the virtual frame into thinking that we left the assignment's
2925 // value on the frame.
2926 __ mov(r0, Operand(Smi::FromInt(0)));
2927 frame_->EmitPush(r0);
2928 ASSERT(frame_->height() == original_height + 1);
2929 return;
2930 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002931
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002932 if (node->op() == Token::ASSIGN ||
2933 node->op() == Token::INIT_VAR ||
2934 node->op() == Token::INIT_CONST) {
2935 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002936
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002937 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002938 // +=, *= and similar binary assignments.
2939 // Get the old value of the lhs.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002940 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
2941 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002942 bool overwrite =
2943 (node->value()->AsBinaryOperation() != NULL &&
2944 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002945 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002946 SmiOperation(node->binary_op(),
2947 literal->handle(),
2948 false,
2949 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002950 frame_->EmitPush(r0);
2951
2952 } else {
2953 LoadAndSpill(node->value());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002954 GenericBinaryOperation(node->binary_op(),
2955 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002956 frame_->EmitPush(r0);
2957 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002959
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002960 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2961 if (var != NULL &&
2962 (var->mode() == Variable::CONST) &&
2963 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2964 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002965
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002966 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002967 CodeForSourcePosition(node->position());
2968 if (node->op() == Token::INIT_CONST) {
2969 // Dynamic constant initializations must use the function context
2970 // and initialize the actual constant declared. Dynamic variable
2971 // initializations are simply assignments and use SetValue.
2972 target.SetValue(CONST_INIT);
2973 } else {
2974 target.SetValue(NOT_CONST_INIT);
2975 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002976 }
2977 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002978 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002979}
2980
2981
ager@chromium.org7c537e22008-10-16 08:43:32 +00002982void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002983#ifdef DEBUG
2984 int original_height = frame_->height();
2985#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002986 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002987 Comment cmnt(masm_, "[ Throw");
2988
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002989 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002990 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002991 frame_->CallRuntime(Runtime::kThrow, 1);
2992 frame_->EmitPush(r0);
2993 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002994}
2995
2996
ager@chromium.org7c537e22008-10-16 08:43:32 +00002997void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002998#ifdef DEBUG
2999 int original_height = frame_->height();
3000#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003001 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003002 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003003
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003004 { Reference property(this, node);
3005 property.GetValueAndSpill(typeof_state());
3006 }
3007 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003008}
3009
3010
ager@chromium.org7c537e22008-10-16 08:43:32 +00003011void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003012#ifdef DEBUG
3013 int original_height = frame_->height();
3014#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003015 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003016 Comment cmnt(masm_, "[ Call");
3017
3018 ZoneList<Expression*>* args = node->arguments();
3019
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003020 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003021 // Standard function call.
3022
3023 // Check if the function is a variable or a property.
3024 Expression* function = node->expression();
3025 Variable* var = function->AsVariableProxy()->AsVariable();
3026 Property* property = function->AsProperty();
3027
3028 // ------------------------------------------------------------------------
3029 // Fast-case: Use inline caching.
3030 // ---
3031 // According to ECMA-262, section 11.2.3, page 44, the function to call
3032 // must be resolved after the arguments have been evaluated. The IC code
3033 // automatically handles this by loading the arguments before the function
3034 // is resolved in cache misses (this also holds for megamorphic calls).
3035 // ------------------------------------------------------------------------
3036
3037 if (var != NULL && !var->is_this() && var->is_global()) {
3038 // ----------------------------------
3039 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3040 // ----------------------------------
3041
3042 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003043 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003044 frame_->EmitPush(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003045
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003046 // Pass the global object as the receiver and let the IC stub
3047 // patch the stack to use the global proxy as 'this' in the
3048 // invoked function.
3049 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003050
3051 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003052 int arg_count = args->length();
3053 for (int i = 0; i < arg_count; i++) {
3054 LoadAndSpill(args->at(i));
3055 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003056
3057 // Setup the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003058 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3059 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003060 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003061 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3062 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003063 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003064 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003065 frame_->Drop();
3066 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003067
3068 } else if (var != NULL && var->slot() != NULL &&
3069 var->slot()->type() == Slot::LOOKUP) {
3070 // ----------------------------------
3071 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3072 // ----------------------------------
3073
3074 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003075 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003076 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003077 frame_->EmitPush(r0);
3078 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003079 // r0: slot value; r1: receiver
3080
3081 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003082 frame_->EmitPush(r0); // function
3083 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003084
3085 // Call the function.
3086 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003087 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003088
3089 } else if (property != NULL) {
3090 // Check if the key is a literal string.
3091 Literal* literal = property->key()->AsLiteral();
3092
3093 if (literal != NULL && literal->handle()->IsSymbol()) {
3094 // ------------------------------------------------------------------
3095 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3096 // ------------------------------------------------------------------
3097
3098 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003099 __ mov(r0, Operand(literal->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003100 frame_->EmitPush(r0);
3101 LoadAndSpill(property->obj());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003102
3103 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003104 int arg_count = args->length();
3105 for (int i = 0; i < arg_count; i++) {
3106 LoadAndSpill(args->at(i));
3107 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003108
3109 // Set the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003110 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3111 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003112 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003113 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003114 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003115
3116 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003117 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00003118
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003119 frame_->EmitPush(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003120
3121 } else {
3122 // -------------------------------------------
3123 // JavaScript example: 'array[index](1, 2, 3)'
3124 // -------------------------------------------
3125
3126 // Load the function to call from the property through a reference.
3127 Reference ref(this, property);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003128 ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003129
3130 // Pass receiver to called function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003131 if (property->is_synthetic()) {
3132 LoadGlobalReceiver(r0);
3133 } else {
3134 __ ldr(r0, frame_->ElementAt(ref.size()));
3135 frame_->EmitPush(r0);
3136 }
3137
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003138 // Call the function.
3139 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003140 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003141 }
3142
3143 } else {
3144 // ----------------------------------
3145 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3146 // ----------------------------------
3147
3148 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003149 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003150
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003151 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003152 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003153
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003154 // Call the function.
3155 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003156 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003157 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003158 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003159}
3160
3161
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003162void CodeGenerator::VisitCallEval(CallEval* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003163#ifdef DEBUG
3164 int original_height = frame_->height();
3165#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003166 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003167 Comment cmnt(masm_, "[ CallEval");
3168
3169 // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve
3170 // the function we need to call and the receiver of the call.
3171 // Then we call the resolved function using the given arguments.
3172
3173 ZoneList<Expression*>* args = node->arguments();
3174 Expression* function = node->expression();
3175
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003176 CodeForStatementPosition(node);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003177
3178 // Prepare stack for call to resolved function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003179 LoadAndSpill(function);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003180 __ mov(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003181 frame_->EmitPush(r2); // Slot for receiver
3182 int arg_count = args->length();
3183 for (int i = 0; i < arg_count; i++) {
3184 LoadAndSpill(args->at(i));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003185 }
3186
3187 // Prepare stack for call to ResolvePossiblyDirectEval.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003188 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3189 frame_->EmitPush(r1);
3190 if (arg_count > 0) {
3191 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3192 frame_->EmitPush(r1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003193 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003194 frame_->EmitPush(r2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003195 }
3196
3197 // Resolve the call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003198 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003199
3200 // Touch up stack with the right values for the function and the receiver.
3201 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003202 __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003203 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003204 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003205
3206 // Call the function.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003207 CodeForSourcePosition(node->position());
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003208
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003209 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3210 CallFunctionStub call_function(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003211 frame_->CallStub(&call_function, arg_count + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003212
3213 __ ldr(cp, frame_->Context());
3214 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003215 frame_->Drop();
3216 frame_->EmitPush(r0);
3217 ASSERT(frame_->height() == original_height + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003218}
3219
3220
ager@chromium.org7c537e22008-10-16 08:43:32 +00003221void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003222#ifdef DEBUG
3223 int original_height = frame_->height();
3224#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003225 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003226 Comment cmnt(masm_, "[ CallNew");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003227 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003228
3229 // According to ECMA-262, section 11.2.2, page 44, the function
3230 // expression in new calls must be evaluated before the
3231 // arguments. This is different from ordinary calls, where the
3232 // actual function to call is resolved after the arguments have been
3233 // evaluated.
3234
3235 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003236 // receiver. There is no need to use the global proxy here because
3237 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003238 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003239 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003240
3241 // Push the arguments ("left-to-right") on the stack.
3242 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003243 int arg_count = args->length();
3244 for (int i = 0; i < arg_count; i++) {
3245 LoadAndSpill(args->at(i));
3246 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003247
mads.s.ager31e71382008-08-13 09:32:07 +00003248 // r0: the number of arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003249 Result num_args = allocator_->Allocate(r0);
3250 ASSERT(num_args.is_valid());
3251 __ mov(num_args.reg(), Operand(arg_count));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003252
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003253 // Load the function into r1 as per calling convention.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003254 Result function = allocator_->Allocate(r1);
3255 ASSERT(function.is_valid());
3256 __ ldr(function.reg(), frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003257
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003258 // Call the construct call builtin that handles allocation and
3259 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003260 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003261 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
3262 Result result = frame_->CallCodeObject(ic,
3263 RelocInfo::CONSTRUCT_CALL,
3264 &num_args,
3265 &function,
3266 arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003267
3268 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003269 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003270 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003271}
3272
3273
ager@chromium.org7c537e22008-10-16 08:43:32 +00003274void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003275 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003276 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003277 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003278 LoadAndSpill(args->at(0));
3279 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003280 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003281 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003282 leave.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003283 // It is a heap object - get map.
3284 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3285 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00003286 // if (!object->IsJSValue()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003287 __ cmp(r1, Operand(JS_VALUE_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003288 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003289 // Load the value.
3290 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003291 leave.Bind();
3292 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003293}
3294
3295
ager@chromium.org7c537e22008-10-16 08:43:32 +00003296void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003297 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003298 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003299 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003300 LoadAndSpill(args->at(0)); // Load the object.
3301 LoadAndSpill(args->at(1)); // Load the value.
3302 frame_->EmitPop(r0); // r0 contains value
3303 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003304 // if (object->IsSmi()) return object.
3305 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003306 leave.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003307 // It is a heap object - get map.
3308 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3309 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3310 // if (!object->IsJSValue()) return object.
3311 __ cmp(r2, Operand(JS_VALUE_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003312 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003313 // Store the value.
3314 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3315 // Update the write barrier.
3316 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3317 __ RecordWrite(r1, r2, r3);
3318 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003319 leave.Bind();
3320 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003321}
3322
3323
ager@chromium.org7c537e22008-10-16 08:43:32 +00003324void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003325 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003326 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003327 LoadAndSpill(args->at(0));
3328 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003329 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003330 cc_reg_ = eq;
3331}
3332
3333
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003334void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003335 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003336 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3337 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003338#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003339 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003340 LoadAndSpill(args->at(1));
3341 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003342 __ CallRuntime(Runtime::kLog, 2);
3343 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003344#endif
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003345 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003346 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003347}
3348
3349
ager@chromium.org7c537e22008-10-16 08:43:32 +00003350void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003351 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003352 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003353 LoadAndSpill(args->at(0));
3354 frame_->EmitPop(r0);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003355 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
3356 cc_reg_ = eq;
3357}
3358
3359
kasper.lund7276f142008-07-30 08:49:36 +00003360// This should generate code that performs a charCodeAt() call or returns
3361// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3362// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003363void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003364 VirtualFrame::SpilledScope spilled_scope;
kasper.lund7276f142008-07-30 08:49:36 +00003365 ASSERT(args->length() == 2);
kasper.lund7276f142008-07-30 08:49:36 +00003366 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003367 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003368}
3369
3370
ager@chromium.org7c537e22008-10-16 08:43:32 +00003371void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003372 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003373 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003374 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003375 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003376 // We need the CC bits to come out as not_equal in the case where the
3377 // object is a smi. This can't be done with the usual test opcode so
3378 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003379 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003380 __ and_(r1, r0, Operand(kSmiTagMask));
3381 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003382 answer.Branch(ne);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003383 // It is a heap object - get the map.
3384 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3385 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3386 // Check if the object is a JS array or not.
3387 __ cmp(r1, Operand(JS_ARRAY_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003388 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003389 cc_reg_ = eq;
3390}
3391
3392
ager@chromium.org7c537e22008-10-16 08:43:32 +00003393void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003394 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003395 ASSERT(args->length() == 0);
3396
mads.s.ager31e71382008-08-13 09:32:07 +00003397 // Seed the result with the formal parameters count, which will be used
3398 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003399 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
3400
3401 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003402 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003403 frame_->CallStub(&stub, 0);
3404 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003405}
3406
3407
ager@chromium.org7c537e22008-10-16 08:43:32 +00003408void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003409 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003410 ASSERT(args->length() == 1);
3411
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003412 // Satisfy contract with ArgumentsAccessStub:
3413 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003414 LoadAndSpill(args->at(0));
3415 frame_->EmitPop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003416 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003417
3418 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003419 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003420 frame_->CallStub(&stub, 0);
3421 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003422}
3423
3424
ager@chromium.org7c537e22008-10-16 08:43:32 +00003425void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003426 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003427 ASSERT(args->length() == 2);
3428
3429 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003430 LoadAndSpill(args->at(0));
3431 LoadAndSpill(args->at(1));
3432 frame_->EmitPop(r0);
3433 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003434 __ cmp(r0, Operand(r1));
3435 cc_reg_ = eq;
3436}
3437
3438
ager@chromium.org7c537e22008-10-16 08:43:32 +00003439void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003440#ifdef DEBUG
3441 int original_height = frame_->height();
3442#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003443 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003444 if (CheckForInlineRuntimeCall(node)) {
3445 ASSERT((has_cc() && frame_->height() == original_height) ||
3446 (!has_cc() && frame_->height() == original_height + 1));
3447 return;
3448 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003449
3450 ZoneList<Expression*>* args = node->arguments();
3451 Comment cmnt(masm_, "[ CallRuntime");
3452 Runtime::Function* function = node->function();
3453
ager@chromium.org41826e72009-03-30 13:30:57 +00003454 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003455 // Prepare stack for calling JS runtime function.
3456 __ mov(r0, Operand(node->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003457 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003458 // Push the builtins object found in the current global object.
3459 __ ldr(r1, GlobalObject());
3460 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003461 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003462 }
mads.s.ager31e71382008-08-13 09:32:07 +00003463
ager@chromium.org41826e72009-03-30 13:30:57 +00003464 // Push the arguments ("left-to-right").
3465 int arg_count = args->length();
3466 for (int i = 0; i < arg_count; i++) {
3467 LoadAndSpill(args->at(i));
3468 }
mads.s.ager31e71382008-08-13 09:32:07 +00003469
ager@chromium.org41826e72009-03-30 13:30:57 +00003470 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003471 // Call the JS runtime function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003472 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3473 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003474 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003475 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003476 frame_->Drop();
3477 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003478 } else {
3479 // Call the C runtime function.
3480 frame_->CallRuntime(function, arg_count);
3481 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003482 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003483 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003484}
3485
3486
ager@chromium.org7c537e22008-10-16 08:43:32 +00003487void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003488#ifdef DEBUG
3489 int original_height = frame_->height();
3490#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003491 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003492 Comment cmnt(masm_, "[ UnaryOperation");
3493
3494 Token::Value op = node->op();
3495
3496 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003497 LoadConditionAndSpill(node->expression(),
3498 NOT_INSIDE_TYPEOF,
3499 false_target(),
3500 true_target(),
3501 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003502 cc_reg_ = NegateCondition(cc_reg_);
3503
3504 } else if (op == Token::DELETE) {
3505 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003506 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003507 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003508 LoadAndSpill(property->obj());
3509 LoadAndSpill(property->key());
3510 Result arg_count = allocator_->Allocate(r0);
3511 ASSERT(arg_count.is_valid());
3512 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3513 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003514
mads.s.ager31e71382008-08-13 09:32:07 +00003515 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003516 Slot* slot = variable->slot();
3517 if (variable->is_global()) {
3518 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003519 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003520 frame_->EmitPush(r0);
3521 Result arg_count = allocator_->Allocate(r0);
3522 ASSERT(arg_count.is_valid());
3523 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3524 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003525
3526 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3527 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003528 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003529 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003530 frame_->EmitPush(r0);
3531 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003532 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003533 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003534 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003535 frame_->EmitPush(r0);
3536 Result arg_count = allocator_->Allocate(r0);
3537 ASSERT(arg_count.is_valid());
3538 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3539 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003540
mads.s.ager31e71382008-08-13 09:32:07 +00003541 } else {
3542 // Default: Result of deleting non-global, not dynamically
3543 // introduced variables is false.
3544 __ mov(r0, Operand(Factory::false_value()));
3545 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003546
3547 } else {
3548 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003549 LoadAndSpill(node->expression()); // may have side-effects
3550 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003551 __ mov(r0, Operand(Factory::true_value()));
3552 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003553 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003554
3555 } else if (op == Token::TYPEOF) {
3556 // Special case for loading the typeof expression; see comment on
3557 // LoadTypeofExpression().
3558 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003559 frame_->CallRuntime(Runtime::kTypeof, 1);
3560 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003561
3562 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003563 LoadAndSpill(node->expression());
3564 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003565 switch (op) {
3566 case Token::NOT:
3567 case Token::DELETE:
3568 case Token::TYPEOF:
3569 UNREACHABLE(); // handled above
3570 break;
3571
3572 case Token::SUB: {
3573 UnarySubStub stub;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003574 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003575 break;
3576 }
3577
3578 case Token::BIT_NOT: {
3579 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003580 JumpTarget smi_label;
3581 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003582 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003583 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003584
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003585 frame_->EmitPush(r0);
3586 Result arg_count = allocator_->Allocate(r0);
3587 ASSERT(arg_count.is_valid());
3588 __ mov(arg_count.reg(), Operand(0)); // not counting receiver
3589 frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003590
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003591 continue_label.Jump();
3592 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003593 __ mvn(r0, Operand(r0));
3594 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003595 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003596 break;
3597 }
3598
3599 case Token::VOID:
3600 // since the stack top is cached in r0, popping and then
3601 // pushing a value can be done by just writing to r0.
3602 __ mov(r0, Operand(Factory::undefined_value()));
3603 break;
3604
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003605 case Token::ADD: {
3606 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003607 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003608 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003609 continue_label.Branch(eq);
3610 frame_->EmitPush(r0);
3611 Result arg_count = allocator_->Allocate(r0);
3612 ASSERT(arg_count.is_valid());
3613 __ mov(arg_count.reg(), Operand(0)); // not counting receiver
3614 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3615 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003616 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003617 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003618 default:
3619 UNREACHABLE();
3620 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003621 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003622 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003623 ASSERT((has_cc() && frame_->height() == original_height) ||
3624 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003625}
3626
3627
ager@chromium.org7c537e22008-10-16 08:43:32 +00003628void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003629#ifdef DEBUG
3630 int original_height = frame_->height();
3631#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003632 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003633 Comment cmnt(masm_, "[ CountOperation");
3634
3635 bool is_postfix = node->is_postfix();
3636 bool is_increment = node->op() == Token::INC;
3637
3638 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3639 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3640
3641 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003642 if (is_postfix) {
3643 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003644 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003645 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003646
3647 { Reference target(this, node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003648 if (target.is_illegal()) {
3649 // Spoof the virtual frame to have the expected height (one higher
3650 // than on entry).
3651 if (!is_postfix) {
3652 __ mov(r0, Operand(Smi::FromInt(0)));
3653 frame_->EmitPush(r0);
3654 }
3655 ASSERT(frame_->height() == original_height + 1);
3656 return;
3657 }
3658 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
3659 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003660
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003661 JumpTarget slow;
3662 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003663
3664 // Load the value (1) into register r1.
3665 __ mov(r1, Operand(Smi::FromInt(1)));
3666
3667 // Check for smi operand.
3668 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003669 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003670
3671 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003672 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003673 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003674 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003675
3676 // Perform optimistic increment/decrement.
3677 if (is_increment) {
3678 __ add(r0, r0, Operand(r1), SetCC);
3679 } else {
3680 __ sub(r0, r0, Operand(r1), SetCC);
3681 }
3682
3683 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003684 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003685
3686 // Revert optimistic increment/decrement.
3687 if (is_increment) {
3688 __ sub(r0, r0, Operand(r1));
3689 } else {
3690 __ add(r0, r0, Operand(r1));
3691 }
3692
3693 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003694 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003695 {
3696 // Convert the operand to a number.
3697 frame_->EmitPush(r0);
3698 Result arg_count = allocator_->Allocate(r0);
3699 ASSERT(arg_count.is_valid());
3700 __ mov(arg_count.reg(), Operand(0));
3701 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3702 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003703 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003704 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003705 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003706 }
3707
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003708 // Compute the new value.
3709 __ mov(r1, Operand(Smi::FromInt(1)));
3710 frame_->EmitPush(r0);
3711 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003712 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003713 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003714 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003715 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003716 }
3717
3718 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003719 exit.Bind();
3720 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003721 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003722 }
3723
3724 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003725 if (is_postfix) frame_->EmitPop(r0);
3726 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003727}
3728
3729
ager@chromium.org7c537e22008-10-16 08:43:32 +00003730void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003731#ifdef DEBUG
3732 int original_height = frame_->height();
3733#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003734 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003735 Comment cmnt(masm_, "[ BinaryOperation");
3736 Token::Value op = node->op();
3737
3738 // According to ECMA-262 section 11.11, page 58, the binary logical
3739 // operators must yield the result of one of the two expressions
3740 // before any ToBoolean() conversions. This means that the value
3741 // produced by a && or || operator is not necessarily a boolean.
3742
3743 // NOTE: If the left hand side produces a materialized value (not in
3744 // the CC register), we force the right hand side to do the
3745 // same. This is necessary because we may have to branch to the exit
3746 // after evaluating the left hand side (due to the shortcut
3747 // semantics), but the compiler must (statically) know if the result
3748 // of compiling the binary operation is materialized or not.
3749
3750 if (op == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003751 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003752 LoadConditionAndSpill(node->left(),
3753 NOT_INSIDE_TYPEOF,
3754 &is_true,
3755 false_target(),
3756 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003757 if (has_cc()) {
3758 Branch(false, false_target());
3759
3760 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003761 is_true.Bind();
3762 LoadConditionAndSpill(node->right(),
3763 NOT_INSIDE_TYPEOF,
3764 true_target(),
3765 false_target(),
3766 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003767
3768 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003769 JumpTarget pop_and_continue;
3770 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003771
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003772 __ ldr(r0, frame_->Top()); // dup the stack top
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003773 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003774 // Avoid popping the result if it converts to 'false' using the
3775 // standard ToBoolean() conversion as described in ECMA-262,
3776 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003777 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003778 Branch(false, &exit);
3779
3780 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003781 pop_and_continue.Bind();
3782 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003783
3784 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003785 is_true.Bind();
3786 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003787
3788 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003789 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003790 }
3791
3792 } else if (op == Token::OR) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003793 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003794 LoadConditionAndSpill(node->left(),
3795 NOT_INSIDE_TYPEOF,
3796 true_target(),
3797 &is_false,
3798 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003799 if (has_cc()) {
3800 Branch(true, true_target());
3801
3802 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003803 is_false.Bind();
3804 LoadConditionAndSpill(node->right(),
3805 NOT_INSIDE_TYPEOF,
3806 true_target(),
3807 false_target(),
3808 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003809
3810 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003811 JumpTarget pop_and_continue;
3812 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003813
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003814 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003815 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003816 // Avoid popping the result if it converts to 'true' using the
3817 // standard ToBoolean() conversion as described in ECMA-262,
3818 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003819 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003820 Branch(true, &exit);
3821
3822 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003823 pop_and_continue.Bind();
3824 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003825
3826 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003827 is_false.Bind();
3828 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003829
3830 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003831 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003832 }
3833
3834 } else {
3835 // Optimize for the case where (at least) one of the expressions
3836 // is a literal small integer.
3837 Literal* lliteral = node->left()->AsLiteral();
3838 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003839 // NOTE: The code below assumes that the slow cases (calls to runtime)
3840 // never return a constant/immutable object.
3841 bool overwrite_left =
3842 (node->left()->AsBinaryOperation() != NULL &&
3843 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
3844 bool overwrite_right =
3845 (node->right()->AsBinaryOperation() != NULL &&
3846 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003847
3848 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003849 LoadAndSpill(node->left());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003850 SmiOperation(node->op(),
3851 rliteral->handle(),
3852 false,
3853 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003854
3855 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003856 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003857 SmiOperation(node->op(),
3858 lliteral->handle(),
3859 true,
3860 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003861
3862 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003863 OverwriteMode overwrite_mode = NO_OVERWRITE;
3864 if (overwrite_left) {
3865 overwrite_mode = OVERWRITE_LEFT;
3866 } else if (overwrite_right) {
3867 overwrite_mode = OVERWRITE_RIGHT;
3868 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003869 LoadAndSpill(node->left());
3870 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003871 GenericBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003872 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003873 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003874 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003875 ASSERT((has_cc() && frame_->height() == original_height) ||
3876 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003877}
3878
3879
ager@chromium.org7c537e22008-10-16 08:43:32 +00003880void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003881#ifdef DEBUG
3882 int original_height = frame_->height();
3883#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003884 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003885 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003886 frame_->EmitPush(r0);
3887 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003888}
3889
3890
ager@chromium.org7c537e22008-10-16 08:43:32 +00003891void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003892#ifdef DEBUG
3893 int original_height = frame_->height();
3894#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003895 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003896 Comment cmnt(masm_, "[ CompareOperation");
3897
3898 // Get the expressions from the node.
3899 Expression* left = node->left();
3900 Expression* right = node->right();
3901 Token::Value op = node->op();
3902
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003903 // To make null checks efficient, we check if either left or right is the
3904 // literal 'null'. If so, we optimize the code by inlining a null check
3905 // instead of calling the (very) general runtime routine for checking
3906 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003907 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003908 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003909 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003910 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003911 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
3912 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003913 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003914 LoadAndSpill(left_is_null ? right : left);
3915 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003916 __ cmp(r0, Operand(Factory::null_value()));
3917
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003918 // The 'null' value is only equal to 'undefined' if using non-strict
3919 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003920 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003921 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003922
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003923 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003924 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003925
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003926 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003927 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003928
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003929 // It can be an undetectable object.
3930 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3931 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
3932 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3933 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003934 }
3935
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003936 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003937 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003938 return;
3939 }
3940 }
3941
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003942 // To make typeof testing for natives implemented in JavaScript really
3943 // efficient, we generate special code for expressions of the form:
3944 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003945 UnaryOperation* operation = left->AsUnaryOperation();
3946 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
3947 (operation != NULL && operation->op() == Token::TYPEOF) &&
3948 (right->AsLiteral() != NULL &&
3949 right->AsLiteral()->handle()->IsString())) {
3950 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
3951
mads.s.ager31e71382008-08-13 09:32:07 +00003952 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003953 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003954 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003955
3956 if (check->Equals(Heap::number_symbol())) {
3957 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003958 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003959 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3960 __ cmp(r1, Operand(Factory::heap_number_map()));
3961 cc_reg_ = eq;
3962
3963 } else if (check->Equals(Heap::string_symbol())) {
3964 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003965 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003966
3967 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3968
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003969 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003970 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3971 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3972 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003973 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003974
3975 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3976 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
3977 cc_reg_ = lt;
3978
3979 } else if (check->Equals(Heap::boolean_symbol())) {
3980 __ cmp(r1, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003981 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003982 __ cmp(r1, Operand(Factory::false_value()));
3983 cc_reg_ = eq;
3984
3985 } else if (check->Equals(Heap::undefined_symbol())) {
3986 __ cmp(r1, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003987 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003988
3989 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003990 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003991
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003992 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003993 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3994 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3995 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3996 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3997
3998 cc_reg_ = eq;
3999
4000 } else if (check->Equals(Heap::function_symbol())) {
4001 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004002 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004003 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4004 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
4005 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
4006 cc_reg_ = eq;
4007
4008 } else if (check->Equals(Heap::object_symbol())) {
4009 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004010 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004011
4012 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4013 __ cmp(r1, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004014 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004015
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004016 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004017 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
4018 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4019 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004020 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004021
4022 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4023 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004024 false_target()->Branch(lt);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004025 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
4026 cc_reg_ = le;
4027
4028 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004029 // Uncommon case: typeof testing against a string literal that is
4030 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004031 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004032 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004033 ASSERT(!has_valid_frame() ||
4034 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004035 return;
4036 }
4037
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004038 LoadAndSpill(left);
4039 LoadAndSpill(right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004040 switch (op) {
4041 case Token::EQ:
4042 Comparison(eq, false);
4043 break;
4044
4045 case Token::LT:
4046 Comparison(lt);
4047 break;
4048
4049 case Token::GT:
4050 Comparison(gt);
4051 break;
4052
4053 case Token::LTE:
4054 Comparison(le);
4055 break;
4056
4057 case Token::GTE:
4058 Comparison(ge);
4059 break;
4060
4061 case Token::EQ_STRICT:
4062 Comparison(eq, true);
4063 break;
4064
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004065 case Token::IN: {
4066 Result arg_count = allocator_->Allocate(r0);
4067 ASSERT(arg_count.is_valid());
4068 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
4069 Result result = frame_->InvokeBuiltin(Builtins::IN,
4070 CALL_JS,
4071 &arg_count,
4072 2);
4073 frame_->EmitPush(result.reg());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004074 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004075 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004076
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004077 case Token::INSTANCEOF: {
4078 Result arg_count = allocator_->Allocate(r0);
4079 ASSERT(arg_count.is_valid());
4080 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
4081 Result result = frame_->InvokeBuiltin(Builtins::INSTANCE_OF,
4082 CALL_JS,
4083 &arg_count,
4084 2);
4085 __ tst(result.reg(), Operand(result.reg()));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004086 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004087 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004088 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004089
4090 default:
4091 UNREACHABLE();
4092 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004093 ASSERT((has_cc() && frame_->height() == original_height) ||
4094 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004095}
4096
4097
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004098#ifdef DEBUG
4099bool CodeGenerator::HasValidEntryRegisters() { return true; }
4100#endif
4101
4102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004103#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004104#define __ ACCESS_MASM(masm)
4105
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004106
ager@chromium.org7c537e22008-10-16 08:43:32 +00004107Handle<String> Reference::GetName() {
4108 ASSERT(type_ == NAMED);
4109 Property* property = expression_->AsProperty();
4110 if (property == NULL) {
4111 // Global variable reference treated as a named property reference.
4112 VariableProxy* proxy = expression_->AsVariableProxy();
4113 ASSERT(proxy->AsVariable() != NULL);
4114 ASSERT(proxy->AsVariable()->is_global());
4115 return proxy->name();
4116 } else {
4117 Literal* raw_name = property->key()->AsLiteral();
4118 ASSERT(raw_name != NULL);
4119 return Handle<String>(String::cast(*raw_name->handle()));
4120 }
4121}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004122
ager@chromium.org7c537e22008-10-16 08:43:32 +00004123
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004124void Reference::GetValueAndSpill(TypeofState typeof_state) {
4125 ASSERT(cgen_->in_spilled_code());
4126 cgen_->set_in_spilled_code(false);
4127 GetValue(typeof_state);
4128 cgen_->frame()->SpillAll();
4129 cgen_->set_in_spilled_code(true);
4130}
4131
4132
ager@chromium.org7c537e22008-10-16 08:43:32 +00004133void Reference::GetValue(TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004134 ASSERT(!cgen_->in_spilled_code());
4135 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004136 ASSERT(!is_illegal());
4137 ASSERT(!cgen_->has_cc());
4138 MacroAssembler* masm = cgen_->masm();
4139 Property* property = expression_->AsProperty();
4140 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004141 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004142 }
4143
4144 switch (type_) {
4145 case SLOT: {
4146 Comment cmnt(masm, "[ Load from Slot");
4147 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4148 ASSERT(slot != NULL);
4149 cgen_->LoadFromSlot(slot, typeof_state);
4150 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004151 }
4152
ager@chromium.org7c537e22008-10-16 08:43:32 +00004153 case NAMED: {
4154 // TODO(1241834): Make sure that this it is safe to ignore the
4155 // distinction between expressions in a typeof and not in a typeof. If
4156 // there is a chance that reference errors can be thrown below, we
4157 // must distinguish between the two kinds of loads (typeof expression
4158 // loads must not throw a reference error).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004159 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004160 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004161 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004162 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004163 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4164 // Setup the name register.
4165 Result name_reg = cgen_->allocator()->Allocate(r2);
4166 ASSERT(name_reg.is_valid());
4167 __ mov(name_reg.reg(), Operand(name));
4168 ASSERT(var == NULL || var->is_global());
4169 RelocInfo::Mode rmode = (var == NULL)
4170 ? RelocInfo::CODE_TARGET
4171 : RelocInfo::CODE_TARGET_CONTEXT;
4172 Result answer = frame->CallCodeObject(ic, rmode, &name_reg, 0);
4173 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004174 break;
4175 }
4176
4177 case KEYED: {
4178 // TODO(1241834): Make sure that this it is safe to ignore the
4179 // distinction between expressions in a typeof and not in a typeof.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004180
4181 // TODO(181): Implement inlined version of array indexing once
4182 // loop nesting is properly tracked on ARM.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004183 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004184 Comment cmnt(masm, "[ Load from keyed Property");
4185 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004186 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004187 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004188 ASSERT(var == NULL || var->is_global());
4189 RelocInfo::Mode rmode = (var == NULL)
4190 ? RelocInfo::CODE_TARGET
4191 : RelocInfo::CODE_TARGET_CONTEXT;
4192 Result answer = frame->CallCodeObject(ic, rmode, 0);
4193 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004194 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004195 }
4196
4197 default:
4198 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004199 }
4200}
4201
4202
ager@chromium.org7c537e22008-10-16 08:43:32 +00004203void Reference::SetValue(InitState init_state) {
4204 ASSERT(!is_illegal());
4205 ASSERT(!cgen_->has_cc());
4206 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004207 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004208 Property* property = expression_->AsProperty();
4209 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004210 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004211 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004212
ager@chromium.org7c537e22008-10-16 08:43:32 +00004213 switch (type_) {
4214 case SLOT: {
4215 Comment cmnt(masm, "[ Store to Slot");
4216 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4217 ASSERT(slot != NULL);
4218 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004219 ASSERT(slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004220
ager@chromium.org7c537e22008-10-16 08:43:32 +00004221 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004222 frame->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004223 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004224 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004225
ager@chromium.org7c537e22008-10-16 08:43:32 +00004226 if (init_state == CONST_INIT) {
4227 // Same as the case for a normal store, but ignores attribute
4228 // (e.g. READ_ONLY) of context slot so that we can initialize
4229 // const properties (introduced via eval("const foo = (some
4230 // expr);")). Also, uses the current function context instead of
4231 // the top context.
4232 //
4233 // Note that we must declare the foo upon entry of eval(), via a
4234 // context slot declaration, but we cannot initialize it at the
4235 // same time, because the const declaration may be at the end of
4236 // the eval code (sigh...) and the const variable may have been
4237 // used before (where its value is 'undefined'). Thus, we can only
4238 // do the initialization when we actually encounter the expression
4239 // and when the expression operands are defined and valid, and
4240 // thus we need the split into 2 operations: declaration of the
4241 // context slot followed by initialization.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004242 frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004243 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004244 frame->CallRuntime(Runtime::kStoreContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004245 }
4246 // Storing a variable must keep the (new) value on the expression
4247 // stack. This is necessary for compiling assignment expressions.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004248 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004249
ager@chromium.org7c537e22008-10-16 08:43:32 +00004250 } else {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004251 ASSERT(!slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004252
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004253 JumpTarget exit;
ager@chromium.org7c537e22008-10-16 08:43:32 +00004254 if (init_state == CONST_INIT) {
4255 ASSERT(slot->var()->mode() == Variable::CONST);
4256 // Only the first const initialization must be executed (the slot
4257 // still contains 'the hole' value). When the assignment is
4258 // executed, the code is identical to a normal store (see below).
4259 Comment cmnt(masm, "[ Init const");
4260 __ ldr(r2, cgen_->SlotOperand(slot, r2));
4261 __ cmp(r2, Operand(Factory::the_hole_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004262 exit.Branch(ne);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004263 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004264
ager@chromium.org7c537e22008-10-16 08:43:32 +00004265 // We must execute the store. Storing a variable must keep the
4266 // (new) value on the stack. This is necessary for compiling
4267 // assignment expressions.
4268 //
4269 // Note: We will reach here even with slot->var()->mode() ==
4270 // Variable::CONST because of const declarations which will
4271 // initialize consts to 'the hole' value and by doing so, end up
4272 // calling this code. r2 may be loaded with context; used below in
4273 // RecordWrite.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004274 frame->EmitPop(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004275 __ str(r0, cgen_->SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004276 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004277 if (slot->type() == Slot::CONTEXT) {
4278 // Skip write barrier if the written value is a smi.
4279 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004280 exit.Branch(eq);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004281 // r2 is loaded with context when calling SlotOperand above.
4282 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
4283 __ mov(r3, Operand(offset));
4284 __ RecordWrite(r2, r3, r1);
4285 }
4286 // If we definitely did not jump over the assignment, we do not need
4287 // to bind the exit label. Doing so can defeat peephole
4288 // optimization.
4289 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004290 exit.Bind();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004291 }
4292 }
4293 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004294 }
4295
ager@chromium.org7c537e22008-10-16 08:43:32 +00004296 case NAMED: {
4297 Comment cmnt(masm, "[ Store to named Property");
4298 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004299 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004300 Handle<String> name(GetName());
4301
4302 Result value = cgen_->allocator()->Allocate(r0);
4303 ASSERT(value.is_valid());
4304 frame->EmitPop(value.reg());
4305
4306 // Setup the name register.
4307 Result property_name = cgen_->allocator()->Allocate(r2);
4308 ASSERT(property_name.is_valid());
4309 __ mov(property_name.reg(), Operand(name));
4310 Result answer = frame->CallCodeObject(ic,
4311 RelocInfo::CODE_TARGET,
4312 &value,
4313 &property_name,
4314 0);
4315 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004316 break;
4317 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004318
ager@chromium.org7c537e22008-10-16 08:43:32 +00004319 case KEYED: {
4320 Comment cmnt(masm, "[ Store to keyed Property");
4321 Property* property = expression_->AsProperty();
4322 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004323 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004324
4325 // Call IC code.
4326 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
4327 // TODO(1222589): Make the IC grab the values from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004328 Result value = cgen_->allocator()->Allocate(r0);
4329 ASSERT(value.is_valid());
4330 frame->EmitPop(value.reg()); // value
4331 Result result =
4332 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0);
4333 frame->EmitPush(result.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004334 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004335 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004336
4337 default:
4338 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004339 }
4340}
4341
4342
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004343static void AllocateHeapNumber(
4344 MacroAssembler* masm,
4345 Label* need_gc, // Jump here if young space is full.
4346 Register result_reg, // The tagged address of the new heap number.
4347 Register allocation_top_addr_reg, // A scratch register.
4348 Register scratch2) { // Another scratch register.
4349 ExternalReference allocation_top =
4350 ExternalReference::new_space_allocation_top_address();
4351 ExternalReference allocation_limit =
4352 ExternalReference::new_space_allocation_limit_address();
4353
4354 // allocat := the address of the allocation top variable.
4355 __ mov(allocation_top_addr_reg, Operand(allocation_top));
4356 // result_reg := the old allocation top.
4357 __ ldr(result_reg, MemOperand(allocation_top_addr_reg));
4358 // scratch2 := the address of the allocation limit.
4359 __ mov(scratch2, Operand(allocation_limit));
4360 // scratch2 := the allocation limit.
4361 __ ldr(scratch2, MemOperand(scratch2));
4362 // result_reg := the new allocation top.
4363 __ add(result_reg, result_reg, Operand(HeapNumber::kSize));
4364 // Compare new new allocation top and limit.
4365 __ cmp(result_reg, Operand(scratch2));
4366 // Branch if out of space in young generation.
4367 __ b(hi, need_gc);
4368 // Store new allocation top.
4369 __ str(result_reg, MemOperand(allocation_top_addr_reg)); // store new top
4370 // Tag and adjust back to start of new object.
4371 __ sub(result_reg, result_reg, Operand(HeapNumber::kSize - kHeapObjectTag));
4372 // Get heap number map into scratch2.
4373 __ mov(scratch2, Operand(Factory::heap_number_map()));
4374 // Store heap number map in new object.
4375 __ str(scratch2, FieldMemOperand(result_reg, HeapObject::kMapOffset));
4376}
4377
4378
4379// We fall into this code if the operands were Smis, but the result was
4380// not (eg. overflow). We branch into this code (to the not_smi label) if
4381// the operands were not both Smi.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004382static void HandleBinaryOpSlowCases(MacroAssembler* masm,
4383 Label* not_smi,
4384 const Builtins::JavaScript& builtin,
4385 Token::Value operation,
4386 int swi_number,
4387 OverwriteMode mode) {
4388 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004389 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004390 __ push(r1);
4391 __ push(r0);
4392 __ mov(r0, Operand(1)); // Set number of arguments.
4393 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004394
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004395 __ bind(not_smi);
4396 __ tst(r0, Operand(kSmiTagMask));
4397 __ b(eq, &slow); // We can't handle a Smi-double combination yet.
4398 __ tst(r1, Operand(kSmiTagMask));
4399 __ b(eq, &slow); // We can't handle a Smi-double combination yet.
4400 // Get map of r0 into r2.
4401 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4402 // Get type of r0 into r3.
4403 __ ldrb(r3, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4404 __ cmp(r3, Operand(HEAP_NUMBER_TYPE));
4405 __ b(ne, &slow);
4406 // Get type of r1 into r3.
4407 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
4408 // Check they are both the same map (heap number map).
4409 __ cmp(r2, r3);
4410 __ b(ne, &slow);
4411 // Both are doubles.
4412 // Calling convention says that second double is in r2 and r3.
4413 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
4414 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4415
4416 if (mode == NO_OVERWRITE) {
4417 // Get address of new heap number into r5.
4418 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004419 __ push(lr);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004420 __ push(r5);
4421 } else if (mode == OVERWRITE_LEFT) {
4422 __ push(lr);
4423 __ push(r1);
4424 } else {
4425 ASSERT(mode == OVERWRITE_RIGHT);
4426 __ push(lr);
4427 __ push(r0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004428 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004429 // Calling convention says that first double is in r0 and r1.
4430 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
4431 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4432 // Call C routine that may not cause GC or other trouble.
4433 __ mov(r5, Operand(ExternalReference::double_fp_operation(operation)));
4434#if !defined(__arm__)
4435 // Notify the simulator that we are calling an add routine in C.
4436 __ swi(swi_number);
4437#else
4438 // Actually call the add routine written in C.
4439 __ Call(r5);
4440#endif
4441 // Store answer in the overwritable heap number.
4442 __ pop(r4);
4443#if !defined(__ARM_EABI__) && defined(__arm__)
4444 // Double returned in fp coprocessor register 0 and 1, encoded as register
4445 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
4446 // substract the tag from r4.
4447 __ sub(r5, r4, Operand(kHeapObjectTag));
4448 __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset));
4449#else
4450 // Double returned in fp coprocessor register 0 and 1.
4451 __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset));
4452 __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + kPointerSize));
4453#endif
4454 __ mov(r0, Operand(r4));
4455 // And we are done.
4456 __ pop(pc);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004457}
4458
4459
4460void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
4461 // r1 : x
4462 // r0 : y
4463 // result : r0
4464
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004465 // All ops need to know whether we are dealing with two Smis. Set up r2 to
4466 // tell us that.
4467 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
4468
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004469 switch (op_) {
4470 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004471 Label not_smi;
4472 // Fast path.
4473 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004474 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004475 __ b(ne, &not_smi);
4476 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
4477 // Return if no overflow.
4478 __ Ret(vc);
4479 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
4480
4481 HandleBinaryOpSlowCases(masm,
4482 &not_smi,
4483 Builtins::ADD,
4484 Token::ADD,
4485 assembler::arm::simulator_fp_add,
4486 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004487 break;
4488 }
4489
4490 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004491 Label not_smi;
4492 // Fast path.
4493 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004494 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004495 __ b(ne, &not_smi);
4496 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
4497 // Return if no overflow.
4498 __ Ret(vc);
4499 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
4500
4501 HandleBinaryOpSlowCases(masm,
4502 &not_smi,
4503 Builtins::SUB,
4504 Token::SUB,
4505 assembler::arm::simulator_fp_sub,
4506 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004507 break;
4508 }
4509
4510 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004511 Label not_smi, slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004512 ASSERT(kSmiTag == 0); // adjust code below
4513 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004514 __ b(ne, &not_smi);
4515 // Remove tag from one operand (but keep sign), so that result is Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004516 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004517 // Do multiplication
4518 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1.
4519 // Go slow on overflows (overflow bit is not set).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004520 __ mov(ip, Operand(r3, ASR, 31));
4521 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
4522 __ b(ne, &slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004523 // Go slow on zero result to handle -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004524 __ tst(r3, Operand(r3));
4525 __ mov(r0, Operand(r3), LeaveCC, ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004526 __ Ret(ne);
4527 // Slow case.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004528 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004529
4530 HandleBinaryOpSlowCases(masm,
4531 &not_smi,
4532 Builtins::MUL,
4533 Token::MUL,
4534 assembler::arm::simulator_fp_mul,
4535 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004536 break;
4537 }
4538
4539 case Token::BIT_OR:
4540 case Token::BIT_AND:
4541 case Token::BIT_XOR: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004542 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004543 ASSERT(kSmiTag == 0); // adjust code below
4544 __ tst(r2, Operand(kSmiTagMask));
4545 __ b(ne, &slow);
4546 switch (op_) {
4547 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
4548 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
4549 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
4550 default: UNREACHABLE();
4551 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004552 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004553 __ bind(&slow);
4554 __ push(r1); // restore stack
4555 __ push(r0);
4556 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
4557 switch (op_) {
4558 case Token::BIT_OR:
4559 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
4560 break;
4561 case Token::BIT_AND:
4562 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
4563 break;
4564 case Token::BIT_XOR:
4565 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
4566 break;
4567 default:
4568 UNREACHABLE();
4569 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004570 break;
4571 }
4572
4573 case Token::SHL:
4574 case Token::SHR:
4575 case Token::SAR: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004576 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004577 ASSERT(kSmiTag == 0); // adjust code below
4578 __ tst(r2, Operand(kSmiTagMask));
4579 __ b(ne, &slow);
4580 // remove tags from operands (but keep sign)
4581 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
4582 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
4583 // use only the 5 least significant bits of the shift count
4584 __ and_(r2, r2, Operand(0x1f));
4585 // perform operation
4586 switch (op_) {
4587 case Token::SAR:
4588 __ mov(r3, Operand(r3, ASR, r2));
4589 // no checks of result necessary
4590 break;
4591
4592 case Token::SHR:
4593 __ mov(r3, Operand(r3, LSR, r2));
4594 // check that the *unsigned* result fits in a smi
4595 // neither of the two high-order bits can be set:
4596 // - 0x80000000: high bit would be lost when smi tagging
4597 // - 0x40000000: this number would convert to negative when
4598 // smi tagging these two cases can only happen with shifts
4599 // by 0 or 1 when handed a valid smi
4600 __ and_(r2, r3, Operand(0xc0000000), SetCC);
4601 __ b(ne, &slow);
4602 break;
4603
4604 case Token::SHL:
4605 __ mov(r3, Operand(r3, LSL, r2));
4606 // check that the *signed* result fits in a smi
4607 __ add(r2, r3, Operand(0x40000000), SetCC);
4608 __ b(mi, &slow);
4609 break;
4610
4611 default: UNREACHABLE();
4612 }
4613 // tag result and store it in r0
4614 ASSERT(kSmiTag == 0); // adjust code below
4615 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004616 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004617 // slow case
4618 __ bind(&slow);
4619 __ push(r1); // restore stack
4620 __ push(r0);
4621 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
4622 switch (op_) {
4623 case Token::SAR: __ InvokeBuiltin(Builtins::SAR, JUMP_JS); break;
4624 case Token::SHR: __ InvokeBuiltin(Builtins::SHR, JUMP_JS); break;
4625 case Token::SHL: __ InvokeBuiltin(Builtins::SHL, JUMP_JS); break;
4626 default: UNREACHABLE();
4627 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004628 break;
4629 }
4630
4631 default: UNREACHABLE();
4632 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004633 // This code should be unreachable.
4634 __ stop("Unreachable");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004635}
4636
4637
4638void StackCheckStub::Generate(MacroAssembler* masm) {
4639 Label within_limit;
4640 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
4641 __ ldr(ip, MemOperand(ip));
4642 __ cmp(sp, Operand(ip));
4643 __ b(hs, &within_limit);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004644 // Do tail-call to runtime routine. Runtime routines expect at least one
4645 // argument, so give it a Smi.
4646 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004647 __ push(r0);
4648 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
4649 __ bind(&within_limit);
4650
4651 __ StubReturn(1);
4652}
4653
4654
4655void UnarySubStub::Generate(MacroAssembler* masm) {
4656 Label undo;
4657 Label slow;
4658 Label done;
4659
4660 // Enter runtime system if the value is not a smi.
4661 __ tst(r0, Operand(kSmiTagMask));
4662 __ b(ne, &slow);
4663
4664 // Enter runtime system if the value of the expression is zero
4665 // to make sure that we switch between 0 and -0.
4666 __ cmp(r0, Operand(0));
4667 __ b(eq, &slow);
4668
4669 // The value of the expression is a smi that is not zero. Try
4670 // optimistic subtraction '0 - value'.
4671 __ rsb(r1, r0, Operand(0), SetCC);
4672 __ b(vs, &slow);
4673
4674 // If result is a smi we are done.
4675 __ tst(r1, Operand(kSmiTagMask));
4676 __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result
4677 __ b(eq, &done);
4678
4679 // Enter runtime system.
4680 __ bind(&slow);
4681 __ push(r0);
4682 __ mov(r0, Operand(0)); // set number of arguments
4683 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
4684
4685 __ bind(&done);
4686 __ StubReturn(1);
4687}
4688
4689
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004690void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
4691 // r0 holds exception
4692 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
4693 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
4694 __ ldr(sp, MemOperand(r3));
4695 __ pop(r2); // pop next in chain
4696 __ str(r2, MemOperand(r3));
4697 // restore parameter- and frame-pointer and pop state.
4698 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
4699 // Before returning we restore the context from the frame pointer if not NULL.
4700 // The frame pointer is NULL in the exception handler of a JS entry frame.
4701 __ cmp(fp, Operand(0));
4702 // Set cp to NULL if fp is NULL.
4703 __ mov(cp, Operand(0), LeaveCC, eq);
4704 // Restore cp otherwise.
4705 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004706#ifdef DEBUG
4707 if (FLAG_debug_code) {
4708 __ mov(lr, Operand(pc));
4709 }
4710#endif
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004711 __ pop(pc);
4712}
4713
4714
4715void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
4716 // Fetch top stack handler.
4717 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
4718 __ ldr(r3, MemOperand(r3));
4719
4720 // Unwind the handlers until the ENTRY handler is found.
4721 Label loop, done;
4722 __ bind(&loop);
4723 // Load the type of the current stack handler.
4724 const int kStateOffset = StackHandlerConstants::kAddressDisplacement +
4725 StackHandlerConstants::kStateOffset;
4726 __ ldr(r2, MemOperand(r3, kStateOffset));
4727 __ cmp(r2, Operand(StackHandler::ENTRY));
4728 __ b(eq, &done);
4729 // Fetch the next handler in the list.
4730 const int kNextOffset = StackHandlerConstants::kAddressDisplacement +
4731 StackHandlerConstants::kNextOffset;
4732 __ ldr(r3, MemOperand(r3, kNextOffset));
4733 __ jmp(&loop);
4734 __ bind(&done);
4735
4736 // Set the top handler address to next handler past the current ENTRY handler.
4737 __ ldr(r0, MemOperand(r3, kNextOffset));
4738 __ mov(r2, Operand(ExternalReference(Top::k_handler_address)));
4739 __ str(r0, MemOperand(r2));
4740
4741 // Set external caught exception to false.
4742 __ mov(r0, Operand(false));
4743 ExternalReference external_caught(Top::k_external_caught_exception_address);
4744 __ mov(r2, Operand(external_caught));
4745 __ str(r0, MemOperand(r2));
4746
4747 // Set pending exception and r0 to out of memory exception.
4748 Failure* out_of_memory = Failure::OutOfMemoryException();
4749 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
4750 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
4751 __ str(r0, MemOperand(r2));
4752
4753 // Restore the stack to the address of the ENTRY handler
4754 __ mov(sp, Operand(r3));
4755
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004756 // Stack layout at this point. See also PushTryHandler
4757 // r3, sp -> next handler
4758 // state (ENTRY)
4759 // pp
4760 // fp
4761 // lr
4762
4763 // Discard ENTRY state (r2 is not used), and restore parameter-
4764 // and frame-pointer and pop state.
4765 __ ldm(ia_w, sp, r2.bit() | r3.bit() | pp.bit() | fp.bit());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004766 // Before returning we restore the context from the frame pointer if not NULL.
4767 // The frame pointer is NULL in the exception handler of a JS entry frame.
4768 __ cmp(fp, Operand(0));
4769 // Set cp to NULL if fp is NULL.
4770 __ mov(cp, Operand(0), LeaveCC, eq);
4771 // Restore cp otherwise.
4772 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004773#ifdef DEBUG
4774 if (FLAG_debug_code) {
4775 __ mov(lr, Operand(pc));
4776 }
4777#endif
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004778 __ pop(pc);
4779}
4780
4781
4782void CEntryStub::GenerateCore(MacroAssembler* masm,
4783 Label* throw_normal_exception,
4784 Label* throw_out_of_memory_exception,
4785 StackFrame::Type frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004786 bool do_gc,
4787 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004788 // r0: result parameter for PerformGC, if any
4789 // r4: number of arguments including receiver (C callee-saved)
4790 // r5: pointer to builtin function (C callee-saved)
4791 // r6: pointer to the first argument (C callee-saved)
4792
4793 if (do_gc) {
4794 // Passing r0.
4795 __ Call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
4796 }
4797
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004798 ExternalReference scope_depth =
4799 ExternalReference::heap_always_allocate_scope_depth();
4800 if (always_allocate) {
4801 __ mov(r0, Operand(scope_depth));
4802 __ ldr(r1, MemOperand(r0));
4803 __ add(r1, r1, Operand(1));
4804 __ str(r1, MemOperand(r0));
4805 }
4806
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004807 // Call C built-in.
4808 // r0 = argc, r1 = argv
4809 __ mov(r0, Operand(r4));
4810 __ mov(r1, Operand(r6));
4811
4812 // TODO(1242173): To let the GC traverse the return address of the exit
4813 // frames, we need to know where the return address is. Right now,
4814 // we push it on the stack to be able to find it again, but we never
4815 // restore from it in case of changes, which makes it impossible to
4816 // support moving the C entry code stub. This should be fixed, but currently
4817 // this is OK because the CEntryStub gets generated so early in the V8 boot
4818 // sequence that it is not moving ever.
4819 __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
4820 __ push(lr);
4821#if !defined(__arm__)
4822 // Notify the simulator of the transition to C code.
4823 __ swi(assembler::arm::call_rt_r5);
4824#else /* !defined(__arm__) */
ager@chromium.org41826e72009-03-30 13:30:57 +00004825 __ Jump(r5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004826#endif /* !defined(__arm__) */
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004827
4828 if (always_allocate) {
4829 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
4830 // though (contain the result).
4831 __ mov(r2, Operand(scope_depth));
4832 __ ldr(r3, MemOperand(r2));
4833 __ sub(r3, r3, Operand(1));
4834 __ str(r3, MemOperand(r2));
4835 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004836
4837 // check for failure result
4838 Label failure_returned;
4839 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4840 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
4841 __ add(r2, r0, Operand(1));
4842 __ tst(r2, Operand(kFailureTagMask));
4843 __ b(eq, &failure_returned);
4844
4845 // Exit C frame and return.
4846 // r0:r1: result
4847 // sp: stack pointer
4848 // fp: frame pointer
4849 // pp: caller's parameter pointer pp (restored as C callee-saved)
4850 __ LeaveExitFrame(frame_type);
4851
4852 // check if we should retry or throw exception
4853 Label retry;
4854 __ bind(&failure_returned);
4855 ASSERT(Failure::RETRY_AFTER_GC == 0);
4856 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
4857 __ b(eq, &retry);
4858
4859 Label continue_exception;
4860 // If the returned failure is EXCEPTION then promote Top::pending_exception().
4861 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
4862 __ b(ne, &continue_exception);
4863
4864 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00004865 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004866 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00004867 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004868 __ ldr(r0, MemOperand(ip));
4869 __ str(r3, MemOperand(ip));
4870
4871 __ bind(&continue_exception);
4872 // Special handling of out of memory exception.
4873 Failure* out_of_memory = Failure::OutOfMemoryException();
4874 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
4875 __ b(eq, throw_out_of_memory_exception);
4876
4877 // Handle normal exception.
4878 __ jmp(throw_normal_exception);
4879
4880 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
4881}
4882
4883
4884void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
4885 // Called from JavaScript; parameters are on stack as if calling JS function
4886 // r0: number of arguments including receiver
4887 // r1: pointer to builtin function
4888 // fp: frame pointer (restored after C call)
4889 // sp: stack pointer (restored as callee's pp after C call)
4890 // cp: current context (C callee-saved)
4891 // pp: caller's parameter pointer pp (C callee-saved)
4892
4893 // NOTE: Invocations of builtins may return failure objects
4894 // instead of a proper result. The builtin entry handles
4895 // this by performing a garbage collection and retrying the
4896 // builtin once.
4897
4898 StackFrame::Type frame_type = is_debug_break
4899 ? StackFrame::EXIT_DEBUG
4900 : StackFrame::EXIT;
4901
4902 // Enter the exit frame that transitions from JavaScript to C++.
4903 __ EnterExitFrame(frame_type);
4904
4905 // r4: number of arguments (C callee-saved)
4906 // r5: pointer to builtin function (C callee-saved)
4907 // r6: pointer to first argument (C callee-saved)
4908
4909 Label throw_out_of_memory_exception;
4910 Label throw_normal_exception;
4911
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004912 // Call into the runtime system. Collect garbage before the call if
4913 // running with --gc-greedy set.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004914 if (FLAG_gc_greedy) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004915 Failure* failure = Failure::RetryAfterGC(0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004916 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
4917 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004918 GenerateCore(masm, &throw_normal_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004919 &throw_out_of_memory_exception,
4920 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004921 FLAG_gc_greedy,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004922 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004923
4924 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004925 GenerateCore(masm,
4926 &throw_normal_exception,
4927 &throw_out_of_memory_exception,
4928 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004929 true,
4930 false);
4931
4932 // Do full GC and retry runtime call one final time.
4933 Failure* failure = Failure::InternalError();
4934 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
4935 GenerateCore(masm,
4936 &throw_normal_exception,
4937 &throw_out_of_memory_exception,
4938 frame_type,
4939 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004940 true);
4941
4942 __ bind(&throw_out_of_memory_exception);
4943 GenerateThrowOutOfMemory(masm);
4944 // control flow for generated will not return.
4945
4946 __ bind(&throw_normal_exception);
4947 GenerateThrowTOS(masm);
4948}
4949
4950
4951void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4952 // r0: code entry
4953 // r1: function
4954 // r2: receiver
4955 // r3: argc
4956 // [sp+0]: argv
4957
4958 Label invoke, exit;
4959
4960 // Called from C, so do not pop argc and args on exit (preserve sp)
4961 // No need to save register-passed args
4962 // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr
4963 __ stm(db_w, sp, kCalleeSaved | lr.bit());
4964
4965 // Get address of argv, see stm above.
4966 // r0: code entry
4967 // r1: function
4968 // r2: receiver
4969 // r3: argc
4970 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
4971 __ ldr(r4, MemOperand(r4)); // argv
4972
4973 // Push a frame with special values setup to mark it as an entry frame.
4974 // r0: code entry
4975 // r1: function
4976 // r2: receiver
4977 // r3: argc
4978 // r4: argv
4979 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4980 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
4981 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
4982 __ mov(r6, Operand(Smi::FromInt(marker)));
4983 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
4984 __ ldr(r5, MemOperand(r5));
4985 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
4986
4987 // Setup frame pointer for the frame to be pushed.
4988 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
4989
4990 // Call a faked try-block that does the invoke.
4991 __ bl(&invoke);
4992
4993 // Caught exception: Store result (exception) in the pending
4994 // exception field in the JSEnv and return a failure sentinel.
4995 // Coming in here the fp will be invalid because the PushTryHandler below
4996 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00004997 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004998 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004999 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005000 __ b(&exit);
5001
5002 // Invoke: Link this frame into the handler chain.
5003 __ bind(&invoke);
5004 // Must preserve r0-r4, r5-r7 are available.
5005 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
5006 // If an exception not caught by another handler occurs, this handler returns
5007 // control to the code after the bl(&invoke) above, which restores all
5008 // kCalleeSaved registers (including cp, pp and fp) to their saved values
5009 // before returning a failure to C.
5010
5011 // Clear any pending exceptions.
5012 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
5013 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00005014 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005015 __ str(r5, MemOperand(ip));
5016
5017 // Invoke the function by calling through JS entry trampoline builtin.
5018 // Notice that we cannot store a reference to the trampoline code directly in
5019 // this stub, because runtime stubs are not traversed when doing GC.
5020
5021 // Expected registers by Builtins::JSEntryTrampoline
5022 // r0: code entry
5023 // r1: function
5024 // r2: receiver
5025 // r3: argc
5026 // r4: argv
5027 if (is_construct) {
5028 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
5029 __ mov(ip, Operand(construct_entry));
5030 } else {
5031 ExternalReference entry(Builtins::JSEntryTrampoline);
5032 __ mov(ip, Operand(entry));
5033 }
5034 __ ldr(ip, MemOperand(ip)); // deref address
5035
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005036 // Branch and link to JSEntryTrampoline. We don't use the double underscore
5037 // macro for the add instruction because we don't want the coverage tool
5038 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005039 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005040 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005041
5042 // Unlink this frame from the handler chain. When reading the
5043 // address of the next handler, there is no need to use the address
5044 // displacement since the current stack pointer (sp) points directly
5045 // to the stack handler.
5046 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
5047 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
5048 __ str(r3, MemOperand(ip));
5049 // No need to restore registers
5050 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
5051
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005052
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005053 __ bind(&exit); // r0 holds result
5054 // Restore the top frame descriptors from the stack.
5055 __ pop(r3);
5056 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
5057 __ str(r3, MemOperand(ip));
5058
5059 // Reset the stack to the callee saved registers.
5060 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
5061
5062 // Restore callee-saved registers and return.
5063#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005064 if (FLAG_debug_code) {
5065 __ mov(lr, Operand(pc));
5066 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005067#endif
5068 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
5069}
5070
5071
ager@chromium.org7c537e22008-10-16 08:43:32 +00005072void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005073 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005074 Label adaptor;
5075 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5076 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5077 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
ager@chromium.org7c537e22008-10-16 08:43:32 +00005078 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005079
ager@chromium.org7c537e22008-10-16 08:43:32 +00005080 // Nothing to do: The formal number of parameters has already been
5081 // passed in register r0 by calling function. Just return it.
ager@chromium.org9085a012009-05-11 19:22:57 +00005082 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005083
ager@chromium.org7c537e22008-10-16 08:43:32 +00005084 // Arguments adaptor case: Read the arguments length from the
5085 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005086 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005087 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org9085a012009-05-11 19:22:57 +00005088 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005089}
5090
5091
ager@chromium.org7c537e22008-10-16 08:43:32 +00005092void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
5093 // The displacement is the offset of the last parameter (if any)
5094 // relative to the frame pointer.
5095 static const int kDisplacement =
5096 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005097
ager@chromium.org7c537e22008-10-16 08:43:32 +00005098 // Check that the key is a smi.
5099 Label slow;
5100 __ tst(r1, Operand(kSmiTagMask));
5101 __ b(ne, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005102
ager@chromium.org7c537e22008-10-16 08:43:32 +00005103 // Check if the calling frame is an arguments adaptor frame.
5104 Label adaptor;
5105 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5106 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5107 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
5108 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005109
ager@chromium.org7c537e22008-10-16 08:43:32 +00005110 // Check index against formal parameters count limit passed in
5111 // through register eax. Use unsigned comparison to get negative
5112 // check for free.
5113 __ cmp(r1, r0);
5114 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005115
ager@chromium.org7c537e22008-10-16 08:43:32 +00005116 // Read the argument from the stack and return it.
5117 __ sub(r3, r0, r1);
5118 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
5119 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00005120 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005121
5122 // Arguments adaptor case: Check index against actual arguments
5123 // limit found in the arguments adaptor frame. Use unsigned
5124 // comparison to get negative check for free.
5125 __ bind(&adaptor);
5126 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
5127 __ cmp(r1, r0);
5128 __ b(cs, &slow);
5129
5130 // Read the argument from the adaptor frame and return it.
5131 __ sub(r3, r0, r1);
5132 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
5133 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00005134 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005135
5136 // Slow-case: Handle non-smi or out-of-bounds access to arguments
5137 // by calling the runtime system.
5138 __ bind(&slow);
5139 __ push(r1);
5140 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
5141}
5142
5143
5144void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
5145 // Check if the calling frame is an arguments adaptor frame.
5146 Label runtime;
5147 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5148 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5149 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
5150 __ b(ne, &runtime);
5151
5152 // Patch the arguments.length and the parameters pointer.
5153 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
5154 __ str(r0, MemOperand(sp, 0 * kPointerSize));
5155 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
5156 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
5157 __ str(r3, MemOperand(sp, 1 * kPointerSize));
5158
5159 // Do the runtime call to allocate the arguments object.
5160 __ bind(&runtime);
5161 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005162}
5163
5164
5165void CallFunctionStub::Generate(MacroAssembler* masm) {
5166 Label slow;
5167 // Get the function to call from the stack.
5168 // function, receiver [, arguments]
5169 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
5170
5171 // Check that the function is really a JavaScript function.
5172 // r1: pushed function (to be verified)
5173 __ tst(r1, Operand(kSmiTagMask));
5174 __ b(eq, &slow);
5175 // Get the map of the function object.
5176 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
5177 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
5178 __ cmp(r2, Operand(JS_FUNCTION_TYPE));
5179 __ b(ne, &slow);
5180
5181 // Fast-case: Invoke the function now.
5182 // r1: pushed function
5183 ParameterCount actual(argc_);
5184 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
5185
5186 // Slow-case: Non-function called.
5187 __ bind(&slow);
5188 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005189 __ mov(r2, Operand(0));
5190 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
5191 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
5192 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005193}
5194
5195
5196#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005197
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005198} } // namespace v8::internal