blob: fe6d945a2889509f374c034aeff9c14cb7968871 [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.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000292 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000293 ProcessDeferred();
294 }
295
296 allocator_ = NULL;
297 scope_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298}
299
300
ager@chromium.org7c537e22008-10-16 08:43:32 +0000301MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
302 // Currently, this assertion will fail if we try to assign to
303 // a constant variable that is constant because it is read-only
304 // (such as the variable referring to a named function expression).
305 // We need to implement assignments to read-only variables.
306 // Ideally, we should do this during AST generation (by converting
307 // such assignments into expression statements); however, in general
308 // we may not be able to make the decision until past AST generation,
309 // that is when the entire program is known.
310 ASSERT(slot != NULL);
311 int index = slot->index();
312 switch (slot->type()) {
313 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000314 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000315
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000316 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000317 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000318
319 case Slot::CONTEXT: {
320 // Follow the context chain if necessary.
321 ASSERT(!tmp.is(cp)); // do not overwrite context register
322 Register context = cp;
323 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000324 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000325 // Load the closure.
326 // (All contexts, even 'with' contexts, have a closure,
327 // and it is the same for all contexts inside a function.
328 // There is no need to go to the function context first.)
329 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
330 // Load the function context (which is the incoming, outer context).
331 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
332 context = tmp;
333 }
334 // We may have a 'with' context now. Get the function context.
335 // (In fact this mov may never be the needed, since the scope analysis
336 // may not permit a direct context access in this case and thus we are
337 // always at a function context. However it is safe to dereference be-
338 // cause the function context of a function context is itself. Before
339 // deleting this mov we should try to create a counter-example first,
340 // though...)
341 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
342 return ContextOperand(tmp, index);
343 }
344
345 default:
346 UNREACHABLE();
347 return MemOperand(r0, 0);
348 }
349}
350
351
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000352MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
353 Slot* slot,
354 Register tmp,
355 Register tmp2,
356 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000357 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000358 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000359
ager@chromium.org381abbb2009-02-25 13:23:22 +0000360 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
361 if (s->num_heap_slots() > 0) {
362 if (s->calls_eval()) {
363 // Check that extension is NULL.
364 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
365 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000366 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000367 }
368 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
369 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
370 context = tmp;
371 }
372 }
373 // Check that last extension is NULL.
374 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
375 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000376 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000377 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000378 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000379}
380
381
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000382void CodeGenerator::LoadConditionAndSpill(Expression* expression,
383 TypeofState typeof_state,
384 JumpTarget* true_target,
385 JumpTarget* false_target,
386 bool force_control) {
387 ASSERT(in_spilled_code());
388 set_in_spilled_code(false);
389 LoadCondition(expression, typeof_state, true_target, false_target,
390 force_control);
391 if (frame_ != NULL) {
392 frame_->SpillAll();
393 }
394 set_in_spilled_code(true);
395}
396
397
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000398// Loads a value on TOS. If it is a boolean value, the result may have been
399// (partially) translated into branches, or it may have set the condition
400// code register. If force_cc is set, the value is forced to set the
401// condition code register and no value is pushed. If the condition code
402// register was set, has_cc() is true and cc_reg_ contains the condition to
403// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000404void CodeGenerator::LoadCondition(Expression* x,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000405 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000406 JumpTarget* true_target,
407 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000408 bool force_cc) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000409 ASSERT(!in_spilled_code());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000410 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000411 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000412
ager@chromium.org7c537e22008-10-16 08:43:32 +0000413 { CodeGenState new_state(this, typeof_state, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000414 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000415
416 // If we hit a stack overflow, we may not have actually visited
417 // the expression. In that case, we ensure that we have a
418 // valid-looking frame state because we will continue to generate
419 // code as we unwind the C++ stack.
420 //
421 // It's possible to have both a stack overflow and a valid frame
422 // state (eg, a subexpression overflowed, visiting it returned
423 // with a dummied frame state, and visiting this expression
424 // returned with a normal-looking state).
425 if (HasStackOverflow() &&
426 has_valid_frame() &&
427 !has_cc() &&
428 frame_->height() == original_height) {
429 true_target->Jump();
430 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000431 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000432 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000433 // Convert the TOS value to a boolean in the condition code register.
434 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000435 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000436 ASSERT(!force_cc || !has_valid_frame() || has_cc());
437 ASSERT(!has_valid_frame() ||
438 (has_cc() && frame_->height() == original_height) ||
439 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000440}
441
442
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000443void CodeGenerator::LoadAndSpill(Expression* expression,
444 TypeofState typeof_state) {
445 ASSERT(in_spilled_code());
446 set_in_spilled_code(false);
447 Load(expression, typeof_state);
448 frame_->SpillAll();
449 set_in_spilled_code(true);
450}
451
452
ager@chromium.org7c537e22008-10-16 08:43:32 +0000453void CodeGenerator::Load(Expression* x, TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000454#ifdef DEBUG
455 int original_height = frame_->height();
456#endif
457 ASSERT(!in_spilled_code());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000458 JumpTarget true_target;
459 JumpTarget false_target;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000460 LoadCondition(x, typeof_state, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461
462 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000463 // Convert cc_reg_ into a boolean value.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000464 JumpTarget loaded;
465 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000466 materialize_true.Branch(cc_reg_);
mads.s.ager31e71382008-08-13 09:32:07 +0000467 __ mov(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000468 frame_->EmitPush(r0);
469 loaded.Jump();
470 materialize_true.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000471 __ mov(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000472 frame_->EmitPush(r0);
473 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000474 cc_reg_ = al;
475 }
476
477 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000478 // We have at least one condition value that has been "translated"
479 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000480 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000481 if (frame_ != NULL) {
482 loaded.Jump(); // Don't lose the current TOS.
483 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000484 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000485 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000487 true_target.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000488 __ mov(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000489 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000490 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000491 // If both "true" and "false" need to be loaded jump across the code for
492 // "false".
493 if (both) {
494 loaded.Jump();
495 }
496 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000497 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000498 false_target.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000499 __ mov(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000500 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000501 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000502 // A value is loaded on all paths reaching this point.
503 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000504 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000505 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000506 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000507 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000508}
509
510
ager@chromium.org7c537e22008-10-16 08:43:32 +0000511void CodeGenerator::LoadGlobal() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000512 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000513 __ ldr(r0, GlobalObject());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000514 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515}
516
517
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000518void CodeGenerator::LoadGlobalReceiver(Register scratch) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000519 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000520 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
521 __ ldr(scratch,
522 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000523 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000524}
525
526
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000527// TODO(1241834): Get rid of this function in favor of just using Load, now
ager@chromium.org7c537e22008-10-16 08:43:32 +0000528// that we have the INSIDE_TYPEOF typeof state. => Need to handle global
529// variables w/o reference errors elsewhere.
530void CodeGenerator::LoadTypeofExpression(Expression* x) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000531 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532 Variable* variable = x->AsVariableProxy()->AsVariable();
533 if (variable != NULL && !variable->is_this() && variable->is_global()) {
534 // NOTE: This is somewhat nasty. We force the compiler to load
535 // the variable as if through '<global>.<variable>' to make sure we
536 // do not get reference errors.
537 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
538 Literal key(variable->name());
539 // TODO(1241834): Fetch the position from the variable instead of using
540 // no position.
ager@chromium.org236ad962008-09-25 09:45:57 +0000541 Property property(&global, &key, RelocInfo::kNoPosition);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000542 LoadAndSpill(&property);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000544 LoadAndSpill(x, INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 }
546}
547
548
ager@chromium.org7c537e22008-10-16 08:43:32 +0000549Reference::Reference(CodeGenerator* cgen, Expression* expression)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000550 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
551 cgen->LoadReference(this);
552}
553
554
555Reference::~Reference() {
556 cgen_->UnloadReference(this);
557}
558
559
ager@chromium.org7c537e22008-10-16 08:43:32 +0000560void CodeGenerator::LoadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000561 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000562 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563 Expression* e = ref->expression();
564 Property* property = e->AsProperty();
565 Variable* var = e->AsVariableProxy()->AsVariable();
566
567 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000568 // The expression is either a property or a variable proxy that rewrites
569 // to a property.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000570 LoadAndSpill(property->obj());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000571 // We use a named reference if the key is a literal symbol, unless it is
572 // a string that can be legally parsed as an integer. This is because
573 // otherwise we will not get into the slow case code that handles [] on
574 // String objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575 Literal* literal = property->key()->AsLiteral();
576 uint32_t dummy;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000577 if (literal != NULL &&
578 literal->handle()->IsSymbol() &&
579 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000580 ref->set_type(Reference::NAMED);
581 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000582 LoadAndSpill(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583 ref->set_type(Reference::KEYED);
584 }
585 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000586 // The expression is a variable proxy that does not rewrite to a
587 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000589 LoadGlobal();
590 ref->set_type(Reference::NAMED);
591 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000592 ASSERT(var->slot() != NULL);
593 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594 }
595 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000596 // Anything else is a runtime error.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000597 LoadAndSpill(e);
598 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000599 }
600}
601
602
ager@chromium.org7c537e22008-10-16 08:43:32 +0000603void CodeGenerator::UnloadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000604 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000605 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000606 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000607 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000608 if (size > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000609 frame_->EmitPop(r0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000610 frame_->Drop(size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000611 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000612 }
613}
614
615
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000616// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
617// register to a boolean in the condition code register. The code
618// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000619void CodeGenerator::ToBoolean(JumpTarget* true_target,
620 JumpTarget* false_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000621 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000622 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000623 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000624 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000625
626 // Fast case checks
627
mads.s.ager31e71382008-08-13 09:32:07 +0000628 // Check if the value is 'false'.
629 __ cmp(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000630 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000631
mads.s.ager31e71382008-08-13 09:32:07 +0000632 // Check if the value is 'true'.
633 __ cmp(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000634 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000635
mads.s.ager31e71382008-08-13 09:32:07 +0000636 // Check if the value is 'undefined'.
637 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000638 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639
mads.s.ager31e71382008-08-13 09:32:07 +0000640 // Check if the value is a smi.
641 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000642 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000643 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000644 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000645
646 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000647 frame_->EmitPush(r0);
648 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000649 // Convert the result (r0) to a condition code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000651
652 cc_reg_ = ne;
653}
654
655
kasper.lund7276f142008-07-30 08:49:36 +0000656class GenericBinaryOpStub : public CodeStub {
657 public:
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000658 GenericBinaryOpStub(Token::Value op,
659 OverwriteMode mode)
660 : op_(op), mode_(mode) { }
kasper.lund7276f142008-07-30 08:49:36 +0000661
662 private:
663 Token::Value op_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000664 OverwriteMode mode_;
665
666 // Minor key encoding in 16 bits.
667 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
668 class OpBits: public BitField<Token::Value, 2, 14> {};
kasper.lund7276f142008-07-30 08:49:36 +0000669
670 Major MajorKey() { return GenericBinaryOp; }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000671 int MinorKey() {
672 // Encode the parameters in a unique 16 bit value.
673 return OpBits::encode(op_)
674 | ModeBits::encode(mode_);
675 }
676
kasper.lund7276f142008-07-30 08:49:36 +0000677 void Generate(MacroAssembler* masm);
678
679 const char* GetName() {
680 switch (op_) {
681 case Token::ADD: return "GenericBinaryOpStub_ADD";
682 case Token::SUB: return "GenericBinaryOpStub_SUB";
683 case Token::MUL: return "GenericBinaryOpStub_MUL";
684 case Token::DIV: return "GenericBinaryOpStub_DIV";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000685 case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR";
686 case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND";
687 case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR";
688 case Token::SAR: return "GenericBinaryOpStub_SAR";
689 case Token::SHL: return "GenericBinaryOpStub_SHL";
690 case Token::SHR: return "GenericBinaryOpStub_SHR";
kasper.lund7276f142008-07-30 08:49:36 +0000691 default: return "GenericBinaryOpStub";
692 }
693 }
694
695#ifdef DEBUG
696 void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); }
697#endif
698};
699
700
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000701void CodeGenerator::GenericBinaryOperation(Token::Value op,
702 OverwriteMode overwrite_mode) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000703 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000704 // sp[0] : y
705 // sp[1] : x
706 // result : r0
707
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708 // Stub is entered with a call: 'return address' is in lr.
709 switch (op) {
710 case Token::ADD: // fall through.
711 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000712 case Token::MUL:
713 case Token::BIT_OR:
714 case Token::BIT_AND:
715 case Token::BIT_XOR:
716 case Token::SHL:
717 case Token::SHR:
718 case Token::SAR: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000719 frame_->EmitPop(r0); // r0 : y
720 frame_->EmitPop(r1); // r1 : x
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000721 GenericBinaryOpStub stub(op, overwrite_mode);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000722 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000723 break;
724 }
725
726 case Token::DIV: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000727 Result arg_count = allocator_->Allocate(r0);
728 ASSERT(arg_count.is_valid());
729 __ mov(arg_count.reg(), Operand(1));
730 frame_->InvokeBuiltin(Builtins::DIV, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000731 break;
732 }
733
734 case Token::MOD: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000735 Result arg_count = allocator_->Allocate(r0);
736 ASSERT(arg_count.is_valid());
737 __ mov(arg_count.reg(), Operand(1));
738 frame_->InvokeBuiltin(Builtins::MOD, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000739 break;
740 }
741
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742 case Token::COMMA:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000743 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000744 // simply discard left value
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000745 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000746 break;
747
748 default:
749 // Other cases should have been handled before this point.
750 UNREACHABLE();
751 break;
752 }
753}
754
755
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000756class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000757 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000758 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000759 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000760 bool reversed,
761 OverwriteMode overwrite_mode)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000762 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000763 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000764 reversed_(reversed),
765 overwrite_mode_(overwrite_mode) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000766 set_comment("[ DeferredInlinedSmiOperation");
767 }
768
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000769 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000770
771 private:
772 Token::Value op_;
773 int value_;
774 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000775 OverwriteMode overwrite_mode_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000776};
777
778
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000779#undef __
780#define __ ACCESS_MASM(masm)
781
782
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000783void DeferredInlineSmiOperation::Generate() {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000784 MacroAssembler* masm = cgen()->masm();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000785 enter()->Bind();
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000786 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000787
788 switch (op_) {
789 case Token::ADD: {
790 if (reversed_) {
791 // revert optimistic add
792 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
793 __ mov(r1, Operand(Smi::FromInt(value_)));
794 } else {
795 // revert optimistic add
796 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
797 __ mov(r0, Operand(Smi::FromInt(value_)));
798 }
799 break;
800 }
801
802 case Token::SUB: {
803 if (reversed_) {
804 // revert optimistic sub
805 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
806 __ mov(r1, Operand(Smi::FromInt(value_)));
807 } else {
808 __ add(r1, r0, Operand(Smi::FromInt(value_)));
809 __ mov(r0, Operand(Smi::FromInt(value_)));
810 }
811 break;
812 }
813
814 case Token::BIT_OR:
815 case Token::BIT_XOR:
816 case Token::BIT_AND: {
817 if (reversed_) {
818 __ mov(r1, Operand(Smi::FromInt(value_)));
819 } else {
820 __ mov(r1, Operand(r0));
821 __ mov(r0, Operand(Smi::FromInt(value_)));
822 }
823 break;
824 }
825
826 case Token::SHL:
827 case Token::SHR:
828 case Token::SAR: {
829 if (!reversed_) {
830 __ mov(r1, Operand(r0));
831 __ mov(r0, Operand(Smi::FromInt(value_)));
832 } else {
833 UNREACHABLE(); // should have been handled in SmiOperation
834 }
835 break;
836 }
837
838 default:
839 // other cases should have been handled before this point.
840 UNREACHABLE();
841 break;
842 }
843
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000844 GenericBinaryOpStub igostub(op_, overwrite_mode_);
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000845 Result arg0 = cgen()->allocator()->Allocate(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000846 ASSERT(arg0.is_valid());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000847 Result arg1 = cgen()->allocator()->Allocate(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000848 ASSERT(arg1.is_valid());
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000849 cgen()->frame()->CallStub(&igostub, &arg0, &arg1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000850 exit_.Jump();
851}
852
853
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000854#undef __
855#define __ ACCESS_MASM(masm_)
856
857
ager@chromium.org7c537e22008-10-16 08:43:32 +0000858void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000859 Handle<Object> value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000860 bool reversed,
861 OverwriteMode mode) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000862 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000863 // NOTE: This is an attempt to inline (a bit) more of the code for
864 // some possible smi operations (like + and -) when (at least) one
865 // of the operands is a literal smi. With this optimization, the
866 // performance of the system is increased by ~15%, and the generated
867 // code size is increased by ~1% (measured on a combination of
868 // different benchmarks).
869
mads.s.ager31e71382008-08-13 09:32:07 +0000870 // sp[0] : operand
871
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000872 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000874 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000875 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876
877 switch (op) {
878 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000879 DeferredCode* deferred =
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000880 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000882 __ add(r0, r0, Operand(value), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000883 deferred->enter()->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000884 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000885 deferred->enter()->Branch(ne);
886 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887 break;
888 }
889
890 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000891 DeferredCode* deferred =
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000892 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 if (!reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000895 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896 } else {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000897 __ rsb(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000899 deferred->enter()->Branch(vs);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000900 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000901 deferred->enter()->Branch(ne);
902 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000903 break;
904 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000906 case Token::BIT_OR:
907 case Token::BIT_XOR:
908 case Token::BIT_AND: {
909 DeferredCode* deferred =
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000910 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000911 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000912 deferred->enter()->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000913 switch (op) {
914 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
915 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
916 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
917 default: UNREACHABLE();
918 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000919 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000920 break;
921 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000922
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000923 case Token::SHL:
924 case Token::SHR:
925 case Token::SAR: {
926 if (reversed) {
927 __ mov(ip, Operand(value));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000928 frame_->EmitPush(ip);
929 frame_->EmitPush(r0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000930 GenericBinaryOperation(op, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000931
932 } else {
933 int shift_value = int_value & 0x1f; // least significant 5 bits
934 DeferredCode* deferred =
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000935 new DeferredInlineSmiOperation(op, shift_value, false, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000936 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000937 deferred->enter()->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000938 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
939 switch (op) {
940 case Token::SHL: {
941 __ mov(r2, Operand(r2, LSL, shift_value));
942 // check that the *unsigned* result fits in a smi
943 __ add(r3, r2, Operand(0x40000000), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000944 deferred->enter()->Branch(mi);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000945 break;
946 }
947 case Token::SHR: {
948 // LSR by immediate 0 means shifting 32 bits.
949 if (shift_value != 0) {
950 __ mov(r2, Operand(r2, LSR, shift_value));
951 }
952 // check that the *unsigned* result fits in a smi
953 // neither of the two high-order bits can be set:
954 // - 0x80000000: high bit would be lost when smi tagging
955 // - 0x40000000: this number would convert to negative when
956 // smi tagging these two cases can only happen with shifts
957 // by 0 or 1 when handed a valid smi
958 __ and_(r3, r2, Operand(0xc0000000), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000959 deferred->enter()->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000960 break;
961 }
962 case Token::SAR: {
963 if (shift_value != 0) {
964 // ASR by immediate 0 means shifting 32 bits.
965 __ mov(r2, Operand(r2, ASR, shift_value));
966 }
967 break;
968 }
969 default: UNREACHABLE();
970 }
971 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000972 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000973 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000974 break;
975 }
976
977 default:
978 if (!reversed) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000979 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +0000980 __ mov(r0, Operand(value));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000981 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982 } else {
983 __ mov(ip, Operand(value));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000984 frame_->EmitPush(ip);
985 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000986 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000987 GenericBinaryOperation(op, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000988 break;
989 }
990
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000991 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000992}
993
994
ager@chromium.org7c537e22008-10-16 08:43:32 +0000995void CodeGenerator::Comparison(Condition cc, bool strict) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000996 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000997 // sp[0] : y
998 // sp[1] : x
999 // result : cc register
1000
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001001 // Strict only makes sense for equality comparisons.
1002 ASSERT(!strict || cc == eq);
1003
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001004 JumpTarget exit;
1005 JumpTarget smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001006 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1007 if (cc == gt || cc == le) {
1008 cc = ReverseCondition(cc);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001009 frame_->EmitPop(r1);
1010 frame_->EmitPop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001011 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001012 frame_->EmitPop(r0);
1013 frame_->EmitPop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001014 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001015 __ orr(r2, r0, Operand(r1));
1016 __ tst(r2, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001017 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001018
1019 // Perform non-smi comparison by runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001020 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001021
1022 // Figure out which native to call and setup the arguments.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001023 Builtins::JavaScript native;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001024 int arg_count = 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001025 if (cc == eq) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001026 native = strict ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001027 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001028 native = Builtins::COMPARE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001029 int ncr; // NaN compare result
1030 if (cc == lt || cc == le) {
1031 ncr = GREATER;
1032 } else {
1033 ASSERT(cc == gt || cc == ge); // remaining cases
1034 ncr = LESS;
1035 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001036 frame_->EmitPush(r0);
1037 arg_count++;
mads.s.ager31e71382008-08-13 09:32:07 +00001038 __ mov(r0, Operand(Smi::FromInt(ncr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039 }
1040
1041 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1042 // tagged as a small integer.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001043 frame_->EmitPush(r0);
1044 Result arg_count_register = allocator_->Allocate(r0);
1045 ASSERT(arg_count_register.is_valid());
1046 __ mov(arg_count_register.reg(), Operand(arg_count));
1047 Result result = frame_->InvokeBuiltin(native,
1048 CALL_JS,
1049 &arg_count_register,
1050 arg_count + 1);
1051 __ cmp(result.reg(), Operand(0));
1052 result.Unuse();
1053 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001054
1055 // test smi equality by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001056 smi.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057 __ cmp(r1, Operand(r0));
1058
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001059 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001060 cc_reg_ = cc;
1061}
1062
1063
kasper.lund7276f142008-07-30 08:49:36 +00001064class CallFunctionStub: public CodeStub {
1065 public:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001066 CallFunctionStub(int argc, InLoopFlag in_loop)
1067 : argc_(argc), in_loop_(in_loop) {}
kasper.lund7276f142008-07-30 08:49:36 +00001068
1069 void Generate(MacroAssembler* masm);
1070
1071 private:
1072 int argc_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001073 InLoopFlag in_loop_;
kasper.lund7276f142008-07-30 08:49:36 +00001074
kasper.lund7276f142008-07-30 08:49:36 +00001075#if defined(DEBUG)
1076 void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); }
1077#endif // defined(DEBUG)
1078
1079 Major MajorKey() { return CallFunction; }
1080 int MinorKey() { return argc_; }
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001081 InLoopFlag InLoop() { return in_loop_; }
kasper.lund7276f142008-07-30 08:49:36 +00001082};
1083
1084
mads.s.ager31e71382008-08-13 09:32:07 +00001085// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001086void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087 int position) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001088 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001090 int arg_count = args->length();
1091 for (int i = 0; i < arg_count; i++) {
1092 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001093 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001094
kasper.lund7276f142008-07-30 08:49:36 +00001095 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001096 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097
kasper.lund7276f142008-07-30 08:49:36 +00001098 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001099 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
1100 CallFunctionStub call_function(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001101 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001102
1103 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001104 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001105 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001106}
1107
1108
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001109void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001110 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001111 ASSERT(has_cc());
1112 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001113 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001114 cc_reg_ = al;
1115}
1116
1117
ager@chromium.org7c537e22008-10-16 08:43:32 +00001118void CodeGenerator::CheckStack() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001119 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120 if (FLAG_check_stack) {
1121 Comment cmnt(masm_, "[ check stack");
1122 StackCheckStub stub;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001123 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001124 }
1125}
1126
1127
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001128void CodeGenerator::VisitAndSpill(Statement* statement) {
1129 ASSERT(in_spilled_code());
1130 set_in_spilled_code(false);
1131 Visit(statement);
1132 if (frame_ != NULL) {
1133 frame_->SpillAll();
1134 }
1135 set_in_spilled_code(true);
1136}
1137
1138
1139void CodeGenerator::VisitStatementsAndSpill(ZoneList<Statement*>* statements) {
1140 ASSERT(in_spilled_code());
1141 set_in_spilled_code(false);
1142 VisitStatements(statements);
1143 if (frame_ != NULL) {
1144 frame_->SpillAll();
1145 }
1146 set_in_spilled_code(true);
1147}
1148
1149
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001150void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1151#ifdef DEBUG
1152 int original_height = frame_->height();
1153#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001154 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001155 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1156 VisitAndSpill(statements->at(i));
1157 }
1158 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1159}
1160
1161
ager@chromium.org7c537e22008-10-16 08:43:32 +00001162void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001163#ifdef DEBUG
1164 int original_height = frame_->height();
1165#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001166 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001168 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001169 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001170 VisitStatementsAndSpill(node->statements());
1171 if (node->break_target()->is_linked()) {
1172 node->break_target()->Bind();
1173 }
1174 node->break_target()->Unuse();
1175 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001176}
1177
1178
ager@chromium.org7c537e22008-10-16 08:43:32 +00001179void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001180 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001181 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001182 frame_->EmitPush(r0);
1183 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001184 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001185 frame_->EmitPush(r0);
1186 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001187 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001188}
1189
1190
ager@chromium.org7c537e22008-10-16 08:43:32 +00001191void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001192#ifdef DEBUG
1193 int original_height = frame_->height();
1194#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001195 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001196 Comment cmnt(masm_, "[ Declaration");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001197 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001198 Variable* var = node->proxy()->var();
1199 ASSERT(var != NULL); // must have been resolved
1200 Slot* slot = var->slot();
1201
1202 // If it was not possible to allocate the variable at compile time,
1203 // we need to "declare" it at runtime to make sure it actually
1204 // exists in the local context.
1205 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1206 // Variables with a "LOOKUP" slot were introduced as non-locals
1207 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001208 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001209 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001210 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001211 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001212 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001213 // Declaration nodes are always declared in only two modes.
1214 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1215 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001216 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001217 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001218 // Push initial value, if any.
1219 // Note: For variables we must not push an initial value (such as
1220 // 'undefined') because we may have a (legal) redeclaration and we
1221 // must not destroy the current value.
1222 if (node->mode() == Variable::CONST) {
mads.s.ager31e71382008-08-13 09:32:07 +00001223 __ mov(r0, Operand(Factory::the_hole_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001224 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001225 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001226 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001227 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001228 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001229 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001230 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001231 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001232 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001233 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001234 return;
1235 }
1236
1237 ASSERT(!var->is_global());
1238
1239 // If we have a function or a constant, we need to initialize the variable.
1240 Expression* val = NULL;
1241 if (node->mode() == Variable::CONST) {
1242 val = new Literal(Factory::the_hole_value());
1243 } else {
1244 val = node->fun(); // NULL if we don't have a function
1245 }
1246
1247 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001248 {
1249 // Set initial value.
1250 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001251 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001252 target.SetValue(NOT_CONST_INIT);
1253 // The reference is removed from the stack (preserving TOS) when
1254 // it goes out of scope.
1255 }
1256 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001257 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001258 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001259 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260}
1261
1262
ager@chromium.org7c537e22008-10-16 08:43:32 +00001263void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001264#ifdef DEBUG
1265 int original_height = frame_->height();
1266#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001267 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001268 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001269 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001270 Expression* expression = node->expression();
1271 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001272 LoadAndSpill(expression);
1273 frame_->Drop();
1274 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275}
1276
1277
ager@chromium.org7c537e22008-10-16 08:43:32 +00001278void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001279#ifdef DEBUG
1280 int original_height = frame_->height();
1281#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001282 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001283 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001284 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001285 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001286 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287}
1288
1289
ager@chromium.org7c537e22008-10-16 08:43:32 +00001290void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001291#ifdef DEBUG
1292 int original_height = frame_->height();
1293#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001294 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001295 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001296 // Generate different code depending on which parts of the if statement
1297 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001298 bool has_then_stm = node->HasThenStatement();
1299 bool has_else_stm = node->HasElseStatement();
1300
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001301 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001302
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001303 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001305 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001306 JumpTarget then;
1307 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001309 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1310 &then, &else_, true);
1311 if (frame_ != NULL) {
1312 Branch(false, &else_);
1313 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001315 if (frame_ != NULL || then.is_linked()) {
1316 then.Bind();
1317 VisitAndSpill(node->then_statement());
1318 }
1319 if (frame_ != NULL) {
1320 exit.Jump();
1321 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001323 if (else_.is_linked()) {
1324 else_.Bind();
1325 VisitAndSpill(node->else_statement());
1326 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001327
1328 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001329 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001330 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001331 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001332 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001333 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1334 &then, &exit, true);
1335 if (frame_ != NULL) {
1336 Branch(false, &exit);
1337 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001339 if (frame_ != NULL || then.is_linked()) {
1340 then.Bind();
1341 VisitAndSpill(node->then_statement());
1342 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001343
1344 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001345 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001346 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001347 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001348 // if (!cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001349 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1350 &exit, &else_, true);
1351 if (frame_ != NULL) {
1352 Branch(true, &exit);
1353 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001354 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001355 if (frame_ != NULL || else_.is_linked()) {
1356 else_.Bind();
1357 VisitAndSpill(node->else_statement());
1358 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001359
1360 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001361 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001362 ASSERT(!has_then_stm && !has_else_stm);
1363 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001364 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1365 &exit, &exit, false);
1366 if (frame_ != NULL) {
1367 if (has_cc()) {
1368 cc_reg_ = al;
1369 } else {
1370 frame_->Drop();
1371 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001372 }
1373 }
1374
1375 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001376 if (exit.is_linked()) {
1377 exit.Bind();
1378 }
1379 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001380}
1381
1382
ager@chromium.org7c537e22008-10-16 08:43:32 +00001383void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001384 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001385 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001386 CodeForStatementPosition(node);
1387 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001388}
1389
1390
ager@chromium.org7c537e22008-10-16 08:43:32 +00001391void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001392 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001393 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001394 CodeForStatementPosition(node);
1395 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001396}
1397
1398
ager@chromium.org7c537e22008-10-16 08:43:32 +00001399void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001400 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001401 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001402
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001403 if (function_return_is_shadowed_) {
1404 CodeForStatementPosition(node);
1405 LoadAndSpill(node->expression());
1406 frame_->EmitPop(r0);
1407 function_return_.Jump();
1408 } else {
1409 // Load the returned value.
1410 CodeForStatementPosition(node);
1411 LoadAndSpill(node->expression());
1412
1413 // Pop the result from the frame and prepare the frame for
1414 // returning thus making it easier to merge.
1415 frame_->EmitPop(r0);
1416 frame_->PrepareForReturn();
1417
1418 function_return_.Jump();
1419 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001420}
1421
1422
ager@chromium.org7c537e22008-10-16 08:43:32 +00001423void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001424#ifdef DEBUG
1425 int original_height = frame_->height();
1426#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001427 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001428 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001429 CodeForStatementPosition(node);
1430 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001431 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001432 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001433 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001434 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001435 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001436#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001437 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001438 __ cmp(r0, Operand(cp));
1439 verified_true.Branch(eq);
1440 __ stop("PushContext: r0 is expected to be the same as cp");
1441 verified_true.Bind();
1442#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001444 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001445 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001446}
1447
1448
ager@chromium.org7c537e22008-10-16 08:43:32 +00001449void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001450#ifdef DEBUG
1451 int original_height = frame_->height();
1452#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001453 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001454 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001455 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001456 // Pop context.
1457 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1458 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001459 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001460 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001461}
1462
1463
ager@chromium.org7c537e22008-10-16 08:43:32 +00001464int CodeGenerator::FastCaseSwitchMaxOverheadFactor() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001465 return kFastSwitchMaxOverheadFactor;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001466}
1467
ager@chromium.org7c537e22008-10-16 08:43:32 +00001468int CodeGenerator::FastCaseSwitchMinCaseCount() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001469 return kFastSwitchMinCaseCount;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001470}
1471
1472
ager@chromium.org7c537e22008-10-16 08:43:32 +00001473void CodeGenerator::GenerateFastCaseSwitchJumpTable(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001474 SwitchStatement* node,
1475 int min_index,
1476 int range,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001477 Label* default_label,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001478 Vector<Label*> case_targets,
1479 Vector<Label> case_labels) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001480 VirtualFrame::SpilledScope spilled_scope;
1481 JumpTarget setup_default;
1482 JumpTarget is_smi;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001483
1484 // A non-null default label pointer indicates a default case among
1485 // the case labels. Otherwise we use the break target as a
1486 // "default" for failure to hit the jump table.
1487 JumpTarget* default_target =
1488 (default_label == NULL) ? node->break_target() : &setup_default;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001489
1490 ASSERT(kSmiTag == 0 && kSmiTagSize <= 2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001491 frame_->EmitPop(r0);
ager@chromium.org870a0b62008-11-04 11:43:05 +00001492
1493 // Test for a Smi value in a HeapNumber.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001494 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001495 is_smi.Branch(eq);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001496 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1497 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
ager@chromium.org870a0b62008-11-04 11:43:05 +00001498 __ cmp(r1, Operand(HEAP_NUMBER_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001499 default_target->Branch(ne);
1500 frame_->EmitPush(r0);
1501 frame_->CallRuntime(Runtime::kNumberToSmi, 1);
1502 is_smi.Bind();
ager@chromium.org870a0b62008-11-04 11:43:05 +00001503
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001504 if (min_index != 0) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001505 // Small positive numbers can be immediate operands.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001506 if (min_index < 0) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001507 // If min_index is Smi::kMinValue, -min_index is not a Smi.
1508 if (Smi::IsValid(-min_index)) {
1509 __ add(r0, r0, Operand(Smi::FromInt(-min_index)));
1510 } else {
1511 __ add(r0, r0, Operand(Smi::FromInt(-min_index - 1)));
1512 __ add(r0, r0, Operand(Smi::FromInt(1)));
1513 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001514 } else {
1515 __ sub(r0, r0, Operand(Smi::FromInt(min_index)));
1516 }
1517 }
1518 __ tst(r0, Operand(0x80000000 | kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001519 default_target->Branch(ne);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001520 __ cmp(r0, Operand(Smi::FromInt(range)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001521 default_target->Branch(ge);
1522 VirtualFrame* start_frame = new VirtualFrame(frame_);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001523 __ SmiJumpTable(r0, case_targets);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001524
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001525 GenerateFastCaseSwitchCases(node, case_labels, start_frame);
1526
1527 // If there was a default case among the case labels, we need to
1528 // emit code to jump to it from the default target used for failure
1529 // to hit the jump table.
1530 if (default_label != NULL) {
1531 if (has_valid_frame()) {
1532 node->break_target()->Jump();
1533 }
1534 setup_default.Bind();
1535 frame_->MergeTo(start_frame);
1536 __ b(default_label);
1537 DeleteFrame();
1538 }
1539 if (node->break_target()->is_linked()) {
1540 node->break_target()->Bind();
1541 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001542}
1543
1544
ager@chromium.org7c537e22008-10-16 08:43:32 +00001545void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001546#ifdef DEBUG
1547 int original_height = frame_->height();
1548#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001549 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001550 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001551 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001552 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001553
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001554 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001555 if (TryGenerateFastCaseSwitchStatement(node)) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001556 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1557 return;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001558 }
1559
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001560 JumpTarget next_test;
1561 JumpTarget fall_through;
1562 JumpTarget default_entry;
1563 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001564 ZoneList<CaseClause*>* cases = node->cases();
1565 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001566 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001567
1568 for (int i = 0; i < length; i++) {
1569 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001570 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001571 // Remember the default clause and compile it at the end.
1572 default_clause = clause;
1573 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001574 }
1575
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001576 Comment cmnt(masm_, "[ Case clause");
1577 // Compile the test.
1578 next_test.Bind();
1579 next_test.Unuse();
1580 // Duplicate TOS.
1581 __ ldr(r0, frame_->Top());
1582 frame_->EmitPush(r0);
1583 LoadAndSpill(clause->label());
1584 Comparison(eq, true);
1585 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001586
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001587 // Before entering the body from the test, remove the switch value from
1588 // the stack.
1589 frame_->Drop();
1590
1591 // Label the body so that fall through is enabled.
1592 if (i > 0 && cases->at(i - 1)->is_default()) {
1593 default_exit.Bind();
1594 } else {
1595 fall_through.Bind();
1596 fall_through.Unuse();
1597 }
1598 VisitStatementsAndSpill(clause->statements());
1599
1600 // If control flow can fall through from the body, jump to the next body
1601 // or the end of the statement.
1602 if (frame_ != NULL) {
1603 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1604 default_entry.Jump();
1605 } else {
1606 fall_through.Jump();
1607 }
1608 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001609 }
1610
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001611 // The final "test" removes the switch value.
1612 next_test.Bind();
1613 frame_->Drop();
1614
1615 // If there is a default clause, compile it.
1616 if (default_clause != NULL) {
1617 Comment cmnt(masm_, "[ Default clause");
1618 default_entry.Bind();
1619 VisitStatementsAndSpill(default_clause->statements());
1620 // If control flow can fall out of the default and there is a case after
1621 // it, jup to that case's body.
1622 if (frame_ != NULL && default_exit.is_bound()) {
1623 default_exit.Jump();
1624 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001625 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001626
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001627 if (fall_through.is_linked()) {
1628 fall_through.Bind();
1629 }
1630
1631 if (node->break_target()->is_linked()) {
1632 node->break_target()->Bind();
1633 }
1634 node->break_target()->Unuse();
1635 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636}
1637
1638
ager@chromium.org7c537e22008-10-16 08:43:32 +00001639void CodeGenerator::VisitLoopStatement(LoopStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001640#ifdef DEBUG
1641 int original_height = frame_->height();
1642#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001643 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001644 Comment cmnt(masm_, "[ LoopStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001645 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001646 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001647
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001648 // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a
1649 // known result for the test expression, with no side effects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001650 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW;
1651 if (node->cond() == NULL) {
1652 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1653 info = ALWAYS_TRUE;
1654 } else {
1655 Literal* lit = node->cond()->AsLiteral();
1656 if (lit != NULL) {
1657 if (lit->IsTrue()) {
1658 info = ALWAYS_TRUE;
1659 } else if (lit->IsFalse()) {
1660 info = ALWAYS_FALSE;
1661 }
1662 }
1663 }
1664
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001665 switch (node->type()) {
1666 case LoopStatement::DO_LOOP: {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001667 JumpTarget body(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001668
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001669 // Label the top of the loop for the backward CFG edge. If the test
1670 // is always true we can use the continue target, and if the test is
1671 // always false there is no need.
1672 if (info == ALWAYS_TRUE) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001673 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001674 node->continue_target()->Bind();
1675 } else if (info == ALWAYS_FALSE) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001676 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001677 } else {
1678 ASSERT(info == DONT_KNOW);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001679 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001680 body.Bind();
1681 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001682
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001683 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001684 VisitAndSpill(node->body());
1685
1686 // Compile the test.
1687 if (info == ALWAYS_TRUE) {
1688 if (has_valid_frame()) {
1689 // If control can fall off the end of the body, jump back to the
1690 // top.
1691 node->continue_target()->Jump();
1692 }
1693 } else if (info == ALWAYS_FALSE) {
1694 // If we have a continue in the body, we only have to bind its jump
1695 // target.
1696 if (node->continue_target()->is_linked()) {
1697 node->continue_target()->Bind();
1698 }
1699 } else {
1700 ASSERT(info == DONT_KNOW);
1701 // We have to compile the test expression if it can be reached by
1702 // control flow falling out of the body or via continue.
1703 if (node->continue_target()->is_linked()) {
1704 node->continue_target()->Bind();
1705 }
1706 if (has_valid_frame()) {
1707 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1708 &body, node->break_target(), true);
1709 if (has_valid_frame()) {
1710 // A invalid frame here indicates that control did not
1711 // fall out of the test expression.
1712 Branch(true, &body);
1713 }
1714 }
1715 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001716 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001717 }
1718
1719 case LoopStatement::WHILE_LOOP: {
1720 // If the test is never true and has no side effects there is no need
1721 // to compile the test or body.
1722 if (info == ALWAYS_FALSE) break;
1723
1724 // Label the top of the loop with the continue target for the backward
1725 // CFG edge.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001726 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001727 node->continue_target()->Bind();
1728
1729 if (info == DONT_KNOW) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001730 JumpTarget body;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001731 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1732 &body, node->break_target(), true);
1733 if (has_valid_frame()) {
1734 // A NULL frame indicates that control did not fall out of the
1735 // test expression.
1736 Branch(false, node->break_target());
1737 }
1738 if (has_valid_frame() || body.is_linked()) {
1739 body.Bind();
1740 }
1741 }
1742
1743 if (has_valid_frame()) {
1744 CheckStack(); // TODO(1222600): ignore if body contains calls.
1745 VisitAndSpill(node->body());
1746
1747 // If control flow can fall out of the body, jump back to the top.
1748 if (has_valid_frame()) {
1749 node->continue_target()->Jump();
1750 }
1751 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001752 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001753 }
1754
1755 case LoopStatement::FOR_LOOP: {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001756 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001757
1758 if (node->init() != NULL) {
1759 VisitAndSpill(node->init());
1760 }
1761
1762 // There is no need to compile the test or body.
1763 if (info == ALWAYS_FALSE) break;
1764
1765 // If there is no update statement, label the top of the loop with the
1766 // continue target, otherwise with the loop target.
1767 if (node->next() == NULL) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001768 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001769 node->continue_target()->Bind();
1770 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001771 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001772 loop.Bind();
1773 }
1774
1775 // If the test is always true, there is no need to compile it.
1776 if (info == DONT_KNOW) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001777 JumpTarget body;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001778 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1779 &body, node->break_target(), true);
1780 if (has_valid_frame()) {
1781 Branch(false, node->break_target());
1782 }
1783 if (has_valid_frame() || body.is_linked()) {
1784 body.Bind();
1785 }
1786 }
1787
1788 if (has_valid_frame()) {
1789 CheckStack(); // TODO(1222600): ignore if body contains calls.
1790 VisitAndSpill(node->body());
1791
1792 if (node->next() == NULL) {
1793 // If there is no update statement and control flow can fall out
1794 // of the loop, jump directly to the continue label.
1795 if (has_valid_frame()) {
1796 node->continue_target()->Jump();
1797 }
1798 } else {
1799 // If there is an update statement and control flow can reach it
1800 // via falling out of the body of the loop or continuing, we
1801 // compile the update statement.
1802 if (node->continue_target()->is_linked()) {
1803 node->continue_target()->Bind();
1804 }
1805 if (has_valid_frame()) {
1806 // Record source position of the statement as this code which is
1807 // after the code for the body actually belongs to the loop
1808 // statement and not the body.
1809 CodeForStatementPosition(node);
1810 VisitAndSpill(node->next());
1811 loop.Jump();
1812 }
1813 }
1814 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001815 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001816 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001817 }
1818
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001819 if (node->break_target()->is_linked()) {
1820 node->break_target()->Bind();
1821 }
1822 node->continue_target()->Unuse();
1823 node->break_target()->Unuse();
1824 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001825}
1826
1827
ager@chromium.org7c537e22008-10-16 08:43:32 +00001828void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001829#ifdef DEBUG
1830 int original_height = frame_->height();
1831#endif
1832 ASSERT(!in_spilled_code());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001833 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001834 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001835 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001837 JumpTarget primitive;
1838 JumpTarget jsobject;
1839 JumpTarget fixed_array;
1840 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
1841 JumpTarget end_del_check;
1842 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001843
1844 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001845 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001846
1847 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1848 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001849 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001850 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001851 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001852 __ cmp(r0, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001853 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001854
1855 // Stack layout in body:
1856 // [iteration counter (Smi)]
1857 // [length of array]
1858 // [FixedArray]
1859 // [Map or 0]
1860 // [Object]
1861
1862 // Check if enumerable is already a JSObject
1863 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001864 primitive.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001865 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1866 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00001867 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001868 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001869
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001870 primitive.Bind();
1871 frame_->EmitPush(r0);
1872 Result arg_count = allocator_->Allocate(r0);
1873 ASSERT(arg_count.is_valid());
1874 __ mov(arg_count.reg(), Operand(0));
1875 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001876
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001877 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001878 // Get the set of properties (as a FixedArray or Map).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001879 frame_->EmitPush(r0); // duplicate the object being enumerated
1880 frame_->EmitPush(r0);
1881 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001882
1883 // If we got a Map, we can do a fast modification check.
1884 // Otherwise, we got a FixedArray, and we have to do a slow check.
1885 __ mov(r2, Operand(r0));
1886 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
1887 __ cmp(r1, Operand(Factory::meta_map()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001888 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001889
1890 // Get enum cache
1891 __ mov(r1, Operand(r0));
1892 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1893 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1894 __ ldr(r2,
1895 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1896
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001897 frame_->EmitPush(r0); // map
1898 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001899 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001900 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001901 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001902 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001903 frame_->EmitPush(r0);
1904 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001905
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001906 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001908 frame_->EmitPush(r1); // insert 0 in place of Map
1909 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001910
1911 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001912 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001914 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001915 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001916 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001917
1918 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001919 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00001920 // sp[0] : index
1921 // sp[1] : array/enum cache length
1922 // sp[2] : array or enum cache
1923 // sp[3] : 0 or map
1924 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001925 // Grab the current frame's height for the break and continue
1926 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001927 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1928 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001929
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001930 __ ldr(r0, frame_->ElementAt(0)); // load the current count
1931 __ ldr(r1, frame_->ElementAt(1)); // load the length
1932 __ cmp(r0, Operand(r1)); // compare to the array length
1933 node->break_target()->Branch(hs);
1934
1935 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001936
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001937 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001938 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001939 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1940 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1941
1942 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001943 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001944 // Check if this (still) matches the map of the enumerable.
1945 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001946 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001947 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1948 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001949 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950
1951 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001952 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
1953 frame_->EmitPush(r0);
1954 frame_->EmitPush(r3); // push entry
1955 Result arg_count_register = allocator_->Allocate(r0);
1956 ASSERT(arg_count_register.is_valid());
1957 __ mov(arg_count_register.reg(), Operand(1));
1958 Result result = frame_->InvokeBuiltin(Builtins::FILTER_KEY,
1959 CALL_JS,
1960 &arg_count_register,
1961 2);
1962 __ mov(r3, Operand(result.reg()));
1963 result.Unuse();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001964
1965 // If the property has been removed while iterating, we just skip it.
1966 __ cmp(r3, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001967 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001968
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001969 end_del_check.Bind();
1970 // Store the entry in the 'each' expression and take another spin in the
1971 // loop. r3: i'th entry of the enum cache (or string there of)
1972 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001973 { Reference each(this, node->each());
1974 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001975 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001976 __ ldr(r0, frame_->ElementAt(each.size()));
1977 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001978 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001979 // If the reference was to a slot we rely on the convenient property
1980 // that it doesn't matter whether a value (eg, r3 pushed above) is
1981 // right on top of or right underneath a zero-sized reference.
1982 each.SetValue(NOT_CONST_INIT);
mads.s.ager31e71382008-08-13 09:32:07 +00001983 if (each.size() > 0) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00001984 // It's safe to pop the value lying on top of the reference before
1985 // unloading the reference itself (which preserves the top of stack,
1986 // ie, now the topmost value of the non-zero sized reference), since
1987 // we will discard the top of stack after unloading the reference
1988 // anyway.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001989 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001990 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001991 }
1992 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001993 // Discard the i'th entry pushed above or else the remainder of the
1994 // reference, whichever is currently on top of the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001995 frame_->Drop();
1996
1997 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001998 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001999 VisitAndSpill(node->body());
2000
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002001 // Next. Reestablish a spilled frame in case we are coming here via
2002 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002003 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002004 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002005 frame_->EmitPop(r0);
2006 __ add(r0, r0, Operand(Smi::FromInt(1)));
2007 frame_->EmitPush(r0);
2008 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002009
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002010 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2011 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002012 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002013 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002014
2015 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002016 exit.Bind();
2017 node->continue_target()->Unuse();
2018 node->break_target()->Unuse();
2019 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020}
2021
2022
ager@chromium.org7c537e22008-10-16 08:43:32 +00002023void CodeGenerator::VisitTryCatch(TryCatch* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002024#ifdef DEBUG
2025 int original_height = frame_->height();
2026#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002027 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002028 Comment cmnt(masm_, "[ TryCatch");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002029 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002030
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002031 JumpTarget try_block;
2032 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002033
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002034 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002036 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002037
2038 // Store the caught exception in the catch variable.
2039 { Reference ref(this, node->catch_var());
ager@chromium.org7c537e22008-10-16 08:43:32 +00002040 ASSERT(ref.is_slot());
2041 // Here we make use of the convenient property that it doesn't matter
2042 // whether a value is immediately on top of or underneath a zero-sized
2043 // reference.
2044 ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045 }
2046
2047 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002048 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002049
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002050 VisitStatementsAndSpill(node->catch_block()->statements());
2051 if (frame_ != NULL) {
2052 exit.Jump();
2053 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054
2055
2056 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002057 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002058
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002059 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2060 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002061
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002062 // Shadow the labels for all escapes from the try block, including
2063 // returns. During shadowing, the original label is hidden as the
2064 // LabelShadow and operations on the original actually affect the
2065 // shadowing label.
2066 //
2067 // We should probably try to unify the escaping labels and the return
2068 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002069 int nof_escapes = node->escaping_targets()->length();
2070 List<ShadowTarget*> shadows(1 + nof_escapes);
2071
2072 // Add the shadow target for the function return.
2073 static const int kReturnShadowIndex = 0;
2074 shadows.Add(new ShadowTarget(&function_return_));
2075 bool function_return_was_shadowed = function_return_is_shadowed_;
2076 function_return_is_shadowed_ = true;
2077 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2078
2079 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002080 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002081 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002082 }
2083
2084 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002085 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086
2087 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002088 // After shadowing stops, the original labels are unshadowed and the
2089 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002090 bool has_unlinks = false;
2091 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002093 has_unlinks = has_unlinks || shadows[i]->is_linked();
2094 }
2095 function_return_is_shadowed_ = function_return_was_shadowed;
2096
2097 // Get an external reference to the handler address.
2098 ExternalReference handler_address(Top::k_handler_address);
2099
2100 // The next handler address is at kNextIndex in the stack.
2101 const int kNextIndex = StackHandlerConstants::kNextOffset / kPointerSize;
2102 // If we can fall off the end of the try block, unlink from try chain.
2103 if (has_valid_frame()) {
2104 __ ldr(r1, frame_->ElementAt(kNextIndex));
2105 __ mov(r3, Operand(handler_address));
2106 __ str(r1, MemOperand(r3));
2107 frame_->Drop(StackHandlerConstants::kSize / kPointerSize);
2108 if (has_unlinks) {
2109 exit.Jump();
2110 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002111 }
2112
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002113 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002114 // jumped to. Deallocate each shadow target.
2115 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002116 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002117 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002118 shadows[i]->Bind();
2119 // Because we can be jumping here (to spilled code) from unspilled
2120 // code, we need to reestablish a spilled frame at this block.
2121 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002122
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123 // Reload sp from the top handler, because some statements that we
2124 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002125 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002126 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002127 // The stack pointer was restored to just below the code slot
2128 // (the topmost slot) in the handler.
2129 frame_->Forget(frame_->height() - handler_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002130
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002131 // kNextIndex is off by one because the code slot has already
2132 // been dropped.
2133 __ ldr(r1, frame_->ElementAt(kNextIndex - 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002134 __ str(r1, MemOperand(r3));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002135 // The code slot has already been dropped from the handler.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002136 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002137
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002138 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2139 frame_->PrepareForReturn();
2140 }
2141 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002142 }
2143 }
2144
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002145 exit.Bind();
2146 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002147}
2148
2149
ager@chromium.org7c537e22008-10-16 08:43:32 +00002150void CodeGenerator::VisitTryFinally(TryFinally* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002151#ifdef DEBUG
2152 int original_height = frame_->height();
2153#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002154 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002155 Comment cmnt(masm_, "[ TryFinally");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002156 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002157
2158 // State: Used to keep track of reason for entering the finally
2159 // block. Should probably be extended to hold information for
2160 // break/continue from within the try block.
2161 enum { FALLING, THROWING, JUMPING };
2162
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002163 JumpTarget try_block;
2164 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002165
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002166 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002168 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002169 // In case of thrown exceptions, this is where we continue.
2170 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002171 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002172
2173 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002174 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002176 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2177 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002178
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002179 // Shadow the labels for all escapes from the try block, including
2180 // returns. Shadowing hides the original label as the LabelShadow and
2181 // operations on the original actually affect the shadowing label.
2182 //
2183 // We should probably try to unify the escaping labels and the return
2184 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002185 int nof_escapes = node->escaping_targets()->length();
2186 List<ShadowTarget*> shadows(1 + nof_escapes);
2187
2188 // Add the shadow target for the function return.
2189 static const int kReturnShadowIndex = 0;
2190 shadows.Add(new ShadowTarget(&function_return_));
2191 bool function_return_was_shadowed = function_return_is_shadowed_;
2192 function_return_is_shadowed_ = true;
2193 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2194
2195 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002196 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002197 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002198 }
2199
2200 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002201 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002202
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002203 // Stop the introduced shadowing and count the number of required unlinks.
2204 // After shadowing stops, the original labels are unshadowed and the
2205 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002206 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002207 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002208 shadows[i]->StopShadowing();
2209 if (shadows[i]->is_linked()) nof_unlinks++;
2210 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002211 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002212
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002213 // Get an external reference to the handler address.
2214 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002215
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002216 // The next handler address is at kNextIndex in the stack.
2217 const int kNextIndex = StackHandlerConstants::kNextOffset / kPointerSize;
2218 // If we can fall off the end of the try block, unlink from the try
2219 // chain and set the state on the frame to FALLING.
2220 if (has_valid_frame()) {
2221 __ ldr(r1, frame_->ElementAt(kNextIndex));
2222 __ mov(r3, Operand(handler_address));
2223 __ str(r1, MemOperand(r3));
2224 frame_->Drop(StackHandlerConstants::kSize / kPointerSize);
2225
2226 // Fake a top of stack value (unneeded when FALLING) and set the
2227 // state in r2, then jump around the unlink blocks if any.
2228 __ mov(r0, Operand(Factory::undefined_value()));
2229 frame_->EmitPush(r0);
2230 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2231 if (nof_unlinks > 0) {
2232 finally_block.Jump();
2233 }
2234 }
2235
2236 // Generate code to unlink and set the state for the (formerly)
2237 // shadowing targets that have been jumped to.
2238 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002239 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002240 // If we have come from the shadowed return, the return value is
2241 // in (a non-refcounted reference to) r0. We must preserve it
2242 // until it is pushed.
2243 //
2244 // Because we can be jumping here (to spilled code) from
2245 // unspilled code, we need to reestablish a spilled frame at
2246 // this block.
2247 shadows[i]->Bind();
2248 frame_->SpillAll();
2249
2250 // Reload sp from the top handler, because some statements that
2251 // we break from (eg, for...in) may have left stuff on the
2252 // stack.
2253 __ mov(r3, Operand(handler_address));
2254 __ ldr(sp, MemOperand(r3));
2255 // The stack pointer was restored to the address slot in the handler.
2256 ASSERT(StackHandlerConstants::kNextOffset == 1 * kPointerSize);
2257 frame_->Forget(frame_->height() - handler_height + 1);
2258
2259 // Unlink this handler and drop it from the frame. The next
2260 // handler address is now on top of the frame.
2261 frame_->EmitPop(r1);
2262 __ str(r1, MemOperand(r3));
2263 // The top (code) and the second (handler) slot have both been
2264 // dropped already.
2265 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 2);
2266
2267 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002268 // If this label shadowed the function return, materialize the
2269 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002270 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002271 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002272 // Fake TOS for targets that shadowed breaks and continues.
mads.s.ager31e71382008-08-13 09:32:07 +00002273 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002274 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002275 }
2276 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002277 if (--nof_unlinks > 0) {
2278 // If this is not the last unlink block, jump around the next.
2279 finally_block.Jump();
2280 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002281 }
2282 }
2283
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002284 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002285 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002287 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002288 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002289
2290 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002291 // and the state - while evaluating the finally block.
2292 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002293 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002294 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002295
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002296 if (has_valid_frame()) {
2297 // Restore state and return value or faked TOS.
2298 frame_->EmitPop(r2);
2299 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002300 }
2301
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002302 // Generate code to jump to the right destination for all used
2303 // formerly shadowing targets. Deallocate each shadow target.
2304 for (int i = 0; i < shadows.length(); i++) {
2305 if (has_valid_frame() && shadows[i]->is_bound()) {
2306 JumpTarget* original = shadows[i]->other_target();
2307 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2308 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002309 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002310 skip.Branch(ne);
2311 frame_->PrepareForReturn();
2312 original->Jump();
2313 skip.Bind();
2314 } else {
2315 original->Branch(eq);
2316 }
2317 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002318 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002319
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002320 if (has_valid_frame()) {
2321 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002322 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002323 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2324 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002325
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002326 // Rethrow exception.
2327 frame_->EmitPush(r0);
2328 frame_->CallRuntime(Runtime::kReThrow, 1);
2329
2330 // Done.
2331 exit.Bind();
2332 }
2333 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002334}
2335
2336
ager@chromium.org7c537e22008-10-16 08:43:32 +00002337void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002338#ifdef DEBUG
2339 int original_height = frame_->height();
2340#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002341 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002342 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002343 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002344#ifdef ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002345 frame_->CallRuntime(Runtime::kDebugBreak, 0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002346#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002347 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002348 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002349}
2350
2351
ager@chromium.org7c537e22008-10-16 08:43:32 +00002352void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002353 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002354 ASSERT(boilerplate->IsBoilerplate());
2355
2356 // Push the boilerplate on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002357 __ mov(r0, Operand(boilerplate));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002358 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002359
2360 // Create a new closure.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002361 frame_->EmitPush(cp);
2362 frame_->CallRuntime(Runtime::kNewClosure, 2);
2363 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002364}
2365
2366
ager@chromium.org7c537e22008-10-16 08:43:32 +00002367void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002368#ifdef DEBUG
2369 int original_height = frame_->height();
2370#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002371 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002372 Comment cmnt(masm_, "[ FunctionLiteral");
2373
2374 // Build the function boilerplate and instantiate it.
2375 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00002376 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002377 if (HasStackOverflow()) {
2378 ASSERT(frame_->height() == original_height);
2379 return;
2380 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002381 InstantiateBoilerplate(boilerplate);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002382 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002383}
2384
2385
ager@chromium.org7c537e22008-10-16 08:43:32 +00002386void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002387 FunctionBoilerplateLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002388#ifdef DEBUG
2389 int original_height = frame_->height();
2390#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002391 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002392 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2393 InstantiateBoilerplate(node->boilerplate());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002394 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002395}
2396
2397
ager@chromium.org7c537e22008-10-16 08:43:32 +00002398void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002399#ifdef DEBUG
2400 int original_height = frame_->height();
2401#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002402 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002403 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002404 JumpTarget then;
2405 JumpTarget else_;
2406 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002407 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
2408 &then, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002409 Branch(false, &else_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002410 then.Bind();
2411 LoadAndSpill(node->then_expression(), typeof_state());
2412 exit.Jump();
2413 else_.Bind();
2414 LoadAndSpill(node->else_expression(), typeof_state());
2415 exit.Bind();
2416 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002417}
2418
2419
ager@chromium.org7c537e22008-10-16 08:43:32 +00002420void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002421 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002422 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002423 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002424
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002425 JumpTarget slow;
2426 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002427
2428 // Generate fast-case code for variables that might be shadowed by
2429 // eval-introduced variables. Eval is used a lot without
2430 // introducing variables. In those cases, we do not want to
2431 // perform a runtime call for all variables in the scope
2432 // containing the eval.
2433 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2434 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002435 // If there was no control flow to slow, we can exit early.
2436 if (!slow.is_linked()) {
2437 frame_->EmitPush(r0);
2438 return;
2439 }
2440
2441 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002442
2443 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2444 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2445 // Only generate the fast case for locals that rewrite to slots.
2446 // This rules out argument loads.
2447 if (potential_slot != NULL) {
2448 __ ldr(r0,
2449 ContextSlotOperandCheckExtensions(potential_slot,
2450 r1,
2451 r2,
2452 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002453 if (potential_slot->var()->mode() == Variable::CONST) {
2454 __ cmp(r0, Operand(Factory::the_hole_value()));
2455 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
2456 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002457 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002458 // ContextSlotOperandCheckExtensions so we have to jump around
2459 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002460 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002461 }
2462 }
2463
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002464 slow.Bind();
2465 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002466 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002467 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468
ager@chromium.org7c537e22008-10-16 08:43:32 +00002469 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002470 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002471 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002472 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002473 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002474
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002475 done.Bind();
2476 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002477
2478 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002479 // Note: We would like to keep the assert below, but it fires because of
2480 // some nasty code in LoadTypeofExpression() which should be removed...
ager@chromium.org381abbb2009-02-25 13:23:22 +00002481 // ASSERT(!slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002482
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002483 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002484 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002485 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002486 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002487 // Const slots may contain 'the hole' value (the constant hasn't been
2488 // initialized yet) which needs to be converted into the 'undefined'
2489 // value.
2490 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002491 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002492 __ cmp(r0, Operand(Factory::the_hole_value()));
2493 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002494 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002495 }
2496 }
2497}
2498
2499
ager@chromium.org381abbb2009-02-25 13:23:22 +00002500void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2501 TypeofState typeof_state,
2502 Register tmp,
2503 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002504 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002505 // Check that no extension objects have been created by calls to
2506 // eval from the current scope to the global scope.
2507 Register context = cp;
2508 Scope* s = scope();
2509 while (s != NULL) {
2510 if (s->num_heap_slots() > 0) {
2511 if (s->calls_eval()) {
2512 // Check that extension is NULL.
2513 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2514 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002515 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002516 }
2517 // Load next context in chain.
2518 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2519 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2520 context = tmp;
2521 }
2522 // If no outer scope calls eval, we do not need to check more
2523 // context extensions.
2524 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2525 s = s->outer_scope();
2526 }
2527
2528 if (s->is_eval_scope()) {
2529 Label next, fast;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002530 if (!context.is(tmp)) {
2531 __ mov(tmp, Operand(context));
2532 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002533 __ bind(&next);
2534 // Terminate at global context.
2535 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
2536 __ cmp(tmp2, Operand(Factory::global_context_map()));
2537 __ b(eq, &fast);
2538 // Check that extension is NULL.
2539 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2540 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002541 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002542 // Load next context in chain.
2543 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2544 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2545 __ b(&next);
2546 __ bind(&fast);
2547 }
2548
2549 // All extension objects were empty and it is safe to use a global
2550 // load IC call.
2551 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2552 // Load the global object.
2553 LoadGlobal();
2554 // Setup the name register.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002555 Result name = allocator_->Allocate(r2);
2556 ASSERT(name.is_valid()); // We are in spilled code.
2557 __ mov(name.reg(), Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002558 // Call IC stub.
2559 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002560 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002561 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002562 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002563 }
2564
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002565 // Drop the global object. The result is in r0.
2566 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002567}
2568
2569
ager@chromium.org7c537e22008-10-16 08:43:32 +00002570void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002571#ifdef DEBUG
2572 int original_height = frame_->height();
2573#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002574 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002575 Comment cmnt(masm_, "[ Slot");
2576 LoadFromSlot(node, typeof_state());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002577 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002578}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002579
ager@chromium.org7c537e22008-10-16 08:43:32 +00002580
2581void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002582#ifdef DEBUG
2583 int original_height = frame_->height();
2584#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002585 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002586 Comment cmnt(masm_, "[ VariableProxy");
2587
2588 Variable* var = node->var();
2589 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002590 if (expr != NULL) {
2591 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002592 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002593 ASSERT(var->is_global());
2594 Reference ref(this, node);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002595 ref.GetValueAndSpill(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002596 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002597 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002598}
2599
2600
ager@chromium.org7c537e22008-10-16 08:43:32 +00002601void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002602#ifdef DEBUG
2603 int original_height = frame_->height();
2604#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002605 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002606 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002607 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002608 frame_->EmitPush(r0);
2609 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002610}
2611
2612
ager@chromium.org7c537e22008-10-16 08:43:32 +00002613void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002614#ifdef DEBUG
2615 int original_height = frame_->height();
2616#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002617 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002618 Comment cmnt(masm_, "[ RexExp Literal");
2619
2620 // Retrieve the literal array and check the allocated entry.
2621
2622 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002623 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002624
2625 // Load the literals array of the function.
2626 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2627
2628 // Load the literal at the ast saved index.
2629 int literal_offset =
2630 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2631 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2632
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002633 JumpTarget done;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002634 __ cmp(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002635 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002636
2637 // If the entry is undefined we call the runtime system to computed
2638 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002639 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002640 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002641 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002642 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002643 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002644 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002645 frame_->EmitPush(r0);
2646 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002647 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002648
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002649 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002650 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002651 frame_->EmitPush(r2);
2652 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002653}
2654
2655
2656// This deferred code stub will be used for creating the boilerplate
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002657// by calling Runtime_CreateObjectLiteralBoilerplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002658// Each created boilerplate is stored in the JSFunction and they are
2659// therefore context dependent.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002660class DeferredObjectLiteral: public DeferredCode {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002661 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002662 explicit DeferredObjectLiteral(ObjectLiteral* node) : node_(node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002663 set_comment("[ DeferredObjectLiteral");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002664 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002665
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002666 virtual void Generate();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002667
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002668 private:
2669 ObjectLiteral* node_;
2670};
2671
2672
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002673#undef __
2674#define __ ACCESS_MASM(masm)
2675
2676
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002677void DeferredObjectLiteral::Generate() {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002678 MacroAssembler* masm = cgen()->masm();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002679 // Argument is passed in r1.
2680 enter()->Bind();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002681 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002682
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002683 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002684 // the literal.
2685
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002686 VirtualFrame* frame = cgen()->frame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002687 // Literal array (0).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002688 frame->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002689 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00002690 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002691 frame->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002692 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00002693 __ mov(r0, Operand(node_->constant_properties()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002694 frame->EmitPush(r0);
2695 Result boilerplate =
2696 frame->CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
2697 __ mov(r2, Operand(boilerplate.reg()));
2698 // Result is returned in r2.
2699 exit_.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002700}
2701
2702
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002703#undef __
2704#define __ ACCESS_MASM(masm_)
2705
2706
ager@chromium.org7c537e22008-10-16 08:43:32 +00002707void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002708#ifdef DEBUG
2709 int original_height = frame_->height();
2710#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002711 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002712 Comment cmnt(masm_, "[ ObjectLiteral");
2713
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002714 DeferredObjectLiteral* deferred = new DeferredObjectLiteral(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002715
2716 // Retrieve the literal array and check the allocated entry.
2717
2718 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002719 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002720
2721 // Load the literals array of the function.
2722 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2723
2724 // Load the literal at the ast saved index.
2725 int literal_offset =
2726 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2727 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2728
2729 // Check whether we need to materialize the object literal boilerplate.
2730 // If so, jump to the deferred code.
2731 __ cmp(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002732 deferred->enter()->Branch(eq);
2733 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002734
2735 // Push the object literal boilerplate.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002736 frame_->EmitPush(r2);
mads.s.ager31e71382008-08-13 09:32:07 +00002737
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002738 // Clone the boilerplate object.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002739 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2740 if (node->depth() == 1) {
2741 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2742 }
2743 frame_->CallRuntime(clone_function_id, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002744 frame_->EmitPush(r0); // save the result
mads.s.ager31e71382008-08-13 09:32:07 +00002745 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002746
2747 for (int i = 0; i < node->properties()->length(); i++) {
2748 ObjectLiteral::Property* property = node->properties()->at(i);
2749 Literal* key = property->key();
2750 Expression* value = property->value();
2751 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002752 case ObjectLiteral::Property::CONSTANT:
2753 break;
2754 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2755 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
2756 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002757 case ObjectLiteral::Property::COMPUTED: // fall through
2758 case ObjectLiteral::Property::PROTOTYPE: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002759 frame_->EmitPush(r0); // dup the result
2760 LoadAndSpill(key);
2761 LoadAndSpill(value);
2762 frame_->CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002763 // restore r0
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002764 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002765 break;
2766 }
2767 case ObjectLiteral::Property::SETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002768 frame_->EmitPush(r0);
2769 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002770 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002771 frame_->EmitPush(r0);
2772 LoadAndSpill(value);
2773 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002774 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002775 break;
2776 }
2777 case ObjectLiteral::Property::GETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002778 frame_->EmitPush(r0);
2779 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002780 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002781 frame_->EmitPush(r0);
2782 LoadAndSpill(value);
2783 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002784 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002785 break;
2786 }
2787 }
2788 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002789 ASSERT(frame_->height() == original_height + 1);
2790}
2791
2792
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002793// This deferred code stub will be used for creating the boilerplate
2794// by calling Runtime_CreateArrayLiteralBoilerplate.
2795// Each created boilerplate is stored in the JSFunction and they are
2796// therefore context dependent.
2797class DeferredArrayLiteral: public DeferredCode {
2798 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002799 explicit DeferredArrayLiteral(ArrayLiteral* node) : node_(node) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002800 set_comment("[ DeferredArrayLiteral");
2801 }
2802
2803 virtual void Generate();
2804
2805 private:
2806 ArrayLiteral* node_;
2807};
2808
2809
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002810#undef __
2811#define __ ACCESS_MASM(masm)
2812
2813
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002814void DeferredArrayLiteral::Generate() {
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002815 MacroAssembler* masm = cgen()->masm();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002816 // Argument is passed in r1.
2817 enter()->Bind();
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002818 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002819
2820 // If the entry is undefined we call the runtime system to computed
2821 // the literal.
2822
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002823 VirtualFrame* frame = cgen()->frame();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002824 // Literal array (0).
2825 frame->EmitPush(r1);
2826 // Literal index (1).
2827 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
2828 frame->EmitPush(r0);
2829 // Constant properties (2).
2830 __ mov(r0, Operand(node_->literals()));
2831 frame->EmitPush(r0);
2832 Result boilerplate =
2833 frame->CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3);
2834 __ mov(r2, Operand(boilerplate.reg()));
2835 // Result is returned in r2.
2836 exit_.Jump();
2837}
2838
2839
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002840#undef __
2841#define __ ACCESS_MASM(masm_)
2842
2843
ager@chromium.org7c537e22008-10-16 08:43:32 +00002844void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002845#ifdef DEBUG
2846 int original_height = frame_->height();
2847#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002848 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002849 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002850
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002851 DeferredArrayLiteral* deferred = new DeferredArrayLiteral(node);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002852
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002853 // Retrieve the literal array and check the allocated entry.
2854
2855 // Load the function of this activation.
2856 __ ldr(r1, frame_->Function());
2857
2858 // Load the literals array of the function.
2859 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2860
2861 // Load the literal at the ast saved index.
2862 int literal_offset =
2863 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2864 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2865
2866 // Check whether we need to materialize the object literal boilerplate.
2867 // If so, jump to the deferred code.
2868 __ cmp(r2, Operand(Factory::undefined_value()));
2869 deferred->enter()->Branch(eq);
2870 deferred->BindExit();
2871
2872 // Push the object literal boilerplate.
2873 frame_->EmitPush(r2);
2874
2875 // Clone the boilerplate object.
2876 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2877 if (node->depth() == 1) {
2878 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2879 }
2880 frame_->CallRuntime(clone_function_id, 1);
2881 frame_->EmitPush(r0); // save the result
2882 // r0: cloned object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002883
2884 // Generate code to set the elements in the array that are not
2885 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002886 for (int i = 0; i < node->values()->length(); i++) {
2887 Expression* value = node->values()->at(i);
2888
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002889 // If value is a literal the property value is already set in the
2890 // boilerplate object.
2891 if (value->AsLiteral() != NULL) continue;
2892 // If value is a materialized literal the property value is already set
2893 // in the boilerplate object if it is simple.
2894 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002895
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002896 // The property must be set by generated code.
2897 LoadAndSpill(value);
2898 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002899
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002900 // Fetch the object literal.
2901 __ ldr(r1, frame_->Top());
2902 // Get the elements array.
2903 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002904
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002905 // Write to the indexed properties array.
2906 int offset = i * kPointerSize + Array::kHeaderSize;
2907 __ str(r0, FieldMemOperand(r1, offset));
2908
2909 // Update the write barrier for the array address.
2910 __ mov(r3, Operand(offset));
2911 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002912 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002913 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002914}
2915
2916
ager@chromium.org32912102009-01-16 10:38:43 +00002917void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002918#ifdef DEBUG
2919 int original_height = frame_->height();
2920#endif
2921 ASSERT(!in_spilled_code());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002922 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org32912102009-01-16 10:38:43 +00002923 // Call runtime routine to allocate the catch extension object and
2924 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002925 Comment cmnt(masm_, "[ CatchExtensionObject");
2926 LoadAndSpill(node->key());
2927 LoadAndSpill(node->value());
2928 Result result =
2929 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2930 frame_->EmitPush(result.reg());
2931 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002932}
2933
2934
ager@chromium.org7c537e22008-10-16 08:43:32 +00002935void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002936#ifdef DEBUG
2937 int original_height = frame_->height();
2938#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002939 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002940 Comment cmnt(masm_, "[ Assignment");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002941 CodeForStatementPosition(node);
mads.s.ager31e71382008-08-13 09:32:07 +00002942
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002943 { Reference target(this, node->target());
2944 if (target.is_illegal()) {
2945 // Fool the virtual frame into thinking that we left the assignment's
2946 // value on the frame.
2947 __ mov(r0, Operand(Smi::FromInt(0)));
2948 frame_->EmitPush(r0);
2949 ASSERT(frame_->height() == original_height + 1);
2950 return;
2951 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002952
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002953 if (node->op() == Token::ASSIGN ||
2954 node->op() == Token::INIT_VAR ||
2955 node->op() == Token::INIT_CONST) {
2956 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002957
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002959 // +=, *= and similar binary assignments.
2960 // Get the old value of the lhs.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002961 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
2962 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002963 bool overwrite =
2964 (node->value()->AsBinaryOperation() != NULL &&
2965 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002966 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002967 SmiOperation(node->binary_op(),
2968 literal->handle(),
2969 false,
2970 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002971 frame_->EmitPush(r0);
2972
2973 } else {
2974 LoadAndSpill(node->value());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002975 GenericBinaryOperation(node->binary_op(),
2976 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002977 frame_->EmitPush(r0);
2978 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002979 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002980
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002981 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2982 if (var != NULL &&
2983 (var->mode() == Variable::CONST) &&
2984 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2985 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002986
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002987 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002988 CodeForSourcePosition(node->position());
2989 if (node->op() == Token::INIT_CONST) {
2990 // Dynamic constant initializations must use the function context
2991 // and initialize the actual constant declared. Dynamic variable
2992 // initializations are simply assignments and use SetValue.
2993 target.SetValue(CONST_INIT);
2994 } else {
2995 target.SetValue(NOT_CONST_INIT);
2996 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002997 }
2998 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002999 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003000}
3001
3002
ager@chromium.org7c537e22008-10-16 08:43:32 +00003003void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003004#ifdef DEBUG
3005 int original_height = frame_->height();
3006#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003007 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003008 Comment cmnt(masm_, "[ Throw");
3009
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003010 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003011 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003012 frame_->CallRuntime(Runtime::kThrow, 1);
3013 frame_->EmitPush(r0);
3014 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003015}
3016
3017
ager@chromium.org7c537e22008-10-16 08:43:32 +00003018void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003019#ifdef DEBUG
3020 int original_height = frame_->height();
3021#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003022 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003023 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003024
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003025 { Reference property(this, node);
3026 property.GetValueAndSpill(typeof_state());
3027 }
3028 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003029}
3030
3031
ager@chromium.org7c537e22008-10-16 08:43:32 +00003032void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003033#ifdef DEBUG
3034 int original_height = frame_->height();
3035#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003036 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003037 Comment cmnt(masm_, "[ Call");
3038
3039 ZoneList<Expression*>* args = node->arguments();
3040
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003041 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003042 // Standard function call.
3043
3044 // Check if the function is a variable or a property.
3045 Expression* function = node->expression();
3046 Variable* var = function->AsVariableProxy()->AsVariable();
3047 Property* property = function->AsProperty();
3048
3049 // ------------------------------------------------------------------------
3050 // Fast-case: Use inline caching.
3051 // ---
3052 // According to ECMA-262, section 11.2.3, page 44, the function to call
3053 // must be resolved after the arguments have been evaluated. The IC code
3054 // automatically handles this by loading the arguments before the function
3055 // is resolved in cache misses (this also holds for megamorphic calls).
3056 // ------------------------------------------------------------------------
3057
3058 if (var != NULL && !var->is_this() && var->is_global()) {
3059 // ----------------------------------
3060 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3061 // ----------------------------------
3062
3063 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003064 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003065 frame_->EmitPush(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003066
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003067 // Pass the global object as the receiver and let the IC stub
3068 // patch the stack to use the global proxy as 'this' in the
3069 // invoked function.
3070 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003071
3072 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003073 int arg_count = args->length();
3074 for (int i = 0; i < arg_count; i++) {
3075 LoadAndSpill(args->at(i));
3076 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003077
3078 // Setup the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003079 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3080 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003081 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003082 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3083 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003084 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003085 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003086 frame_->Drop();
3087 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003088
3089 } else if (var != NULL && var->slot() != NULL &&
3090 var->slot()->type() == Slot::LOOKUP) {
3091 // ----------------------------------
3092 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3093 // ----------------------------------
3094
3095 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003096 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003097 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003098 frame_->EmitPush(r0);
3099 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003100 // r0: slot value; r1: receiver
3101
3102 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003103 frame_->EmitPush(r0); // function
3104 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003105
3106 // Call the function.
3107 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003108 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003109
3110 } else if (property != NULL) {
3111 // Check if the key is a literal string.
3112 Literal* literal = property->key()->AsLiteral();
3113
3114 if (literal != NULL && literal->handle()->IsSymbol()) {
3115 // ------------------------------------------------------------------
3116 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3117 // ------------------------------------------------------------------
3118
3119 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003120 __ mov(r0, Operand(literal->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003121 frame_->EmitPush(r0);
3122 LoadAndSpill(property->obj());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003123
3124 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003125 int arg_count = args->length();
3126 for (int i = 0; i < arg_count; i++) {
3127 LoadAndSpill(args->at(i));
3128 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003129
3130 // Set the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003131 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3132 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003133 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003134 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003135 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003136
3137 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003138 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00003139
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003140 frame_->EmitPush(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003141
3142 } else {
3143 // -------------------------------------------
3144 // JavaScript example: 'array[index](1, 2, 3)'
3145 // -------------------------------------------
3146
3147 // Load the function to call from the property through a reference.
3148 Reference ref(this, property);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003149 ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003150
3151 // Pass receiver to called function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003152 if (property->is_synthetic()) {
3153 LoadGlobalReceiver(r0);
3154 } else {
3155 __ ldr(r0, frame_->ElementAt(ref.size()));
3156 frame_->EmitPush(r0);
3157 }
3158
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003159 // Call the function.
3160 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003161 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003162 }
3163
3164 } else {
3165 // ----------------------------------
3166 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3167 // ----------------------------------
3168
3169 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003170 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003171
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003172 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003173 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003174
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003175 // Call the function.
3176 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003177 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003178 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003179 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003180}
3181
3182
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003183void CodeGenerator::VisitCallEval(CallEval* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003184#ifdef DEBUG
3185 int original_height = frame_->height();
3186#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003187 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003188 Comment cmnt(masm_, "[ CallEval");
3189
3190 // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve
3191 // the function we need to call and the receiver of the call.
3192 // Then we call the resolved function using the given arguments.
3193
3194 ZoneList<Expression*>* args = node->arguments();
3195 Expression* function = node->expression();
3196
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003197 CodeForStatementPosition(node);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003198
3199 // Prepare stack for call to resolved function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003200 LoadAndSpill(function);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003201 __ mov(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003202 frame_->EmitPush(r2); // Slot for receiver
3203 int arg_count = args->length();
3204 for (int i = 0; i < arg_count; i++) {
3205 LoadAndSpill(args->at(i));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003206 }
3207
3208 // Prepare stack for call to ResolvePossiblyDirectEval.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003209 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3210 frame_->EmitPush(r1);
3211 if (arg_count > 0) {
3212 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3213 frame_->EmitPush(r1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003214 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003215 frame_->EmitPush(r2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003216 }
3217
3218 // Resolve the call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003219 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003220
3221 // Touch up stack with the right values for the function and the receiver.
3222 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003223 __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003224 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003225 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003226
3227 // Call the function.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003228 CodeForSourcePosition(node->position());
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003229
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003230 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3231 CallFunctionStub call_function(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003232 frame_->CallStub(&call_function, arg_count + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003233
3234 __ ldr(cp, frame_->Context());
3235 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003236 frame_->Drop();
3237 frame_->EmitPush(r0);
3238 ASSERT(frame_->height() == original_height + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003239}
3240
3241
ager@chromium.org7c537e22008-10-16 08:43:32 +00003242void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003243#ifdef DEBUG
3244 int original_height = frame_->height();
3245#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003246 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003247 Comment cmnt(masm_, "[ CallNew");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003248 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003249
3250 // According to ECMA-262, section 11.2.2, page 44, the function
3251 // expression in new calls must be evaluated before the
3252 // arguments. This is different from ordinary calls, where the
3253 // actual function to call is resolved after the arguments have been
3254 // evaluated.
3255
3256 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003257 // receiver. There is no need to use the global proxy here because
3258 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003259 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003260 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003261
3262 // Push the arguments ("left-to-right") on the stack.
3263 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003264 int arg_count = args->length();
3265 for (int i = 0; i < arg_count; i++) {
3266 LoadAndSpill(args->at(i));
3267 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003268
mads.s.ager31e71382008-08-13 09:32:07 +00003269 // r0: the number of arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003270 Result num_args = allocator_->Allocate(r0);
3271 ASSERT(num_args.is_valid());
3272 __ mov(num_args.reg(), Operand(arg_count));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003273
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003274 // Load the function into r1 as per calling convention.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003275 Result function = allocator_->Allocate(r1);
3276 ASSERT(function.is_valid());
3277 __ ldr(function.reg(), frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003278
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003279 // Call the construct call builtin that handles allocation and
3280 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003281 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003282 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
3283 Result result = frame_->CallCodeObject(ic,
3284 RelocInfo::CONSTRUCT_CALL,
3285 &num_args,
3286 &function,
3287 arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003288
3289 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003290 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003291 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003292}
3293
3294
ager@chromium.org7c537e22008-10-16 08:43:32 +00003295void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003296 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003297 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003298 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003299 LoadAndSpill(args->at(0));
3300 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003301 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003302 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003303 leave.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003304 // It is a heap object - get map.
3305 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3306 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00003307 // if (!object->IsJSValue()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003308 __ cmp(r1, Operand(JS_VALUE_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003309 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003310 // Load the value.
3311 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003312 leave.Bind();
3313 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003314}
3315
3316
ager@chromium.org7c537e22008-10-16 08:43:32 +00003317void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003318 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003319 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003320 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003321 LoadAndSpill(args->at(0)); // Load the object.
3322 LoadAndSpill(args->at(1)); // Load the value.
3323 frame_->EmitPop(r0); // r0 contains value
3324 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003325 // if (object->IsSmi()) return object.
3326 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003327 leave.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003328 // It is a heap object - get map.
3329 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3330 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3331 // if (!object->IsJSValue()) return object.
3332 __ cmp(r2, Operand(JS_VALUE_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003333 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003334 // Store the value.
3335 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3336 // Update the write barrier.
3337 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3338 __ RecordWrite(r1, r2, r3);
3339 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003340 leave.Bind();
3341 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003342}
3343
3344
ager@chromium.org7c537e22008-10-16 08:43:32 +00003345void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003346 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003347 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003348 LoadAndSpill(args->at(0));
3349 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003350 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003351 cc_reg_ = eq;
3352}
3353
3354
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003355void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003356 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003357 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3358 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003359#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003360 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003361 LoadAndSpill(args->at(1));
3362 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003363 __ CallRuntime(Runtime::kLog, 2);
3364 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003365#endif
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003366 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003367 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003368}
3369
3370
ager@chromium.org7c537e22008-10-16 08:43:32 +00003371void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003372 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003373 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003374 LoadAndSpill(args->at(0));
3375 frame_->EmitPop(r0);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003376 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
3377 cc_reg_ = eq;
3378}
3379
3380
kasper.lund7276f142008-07-30 08:49:36 +00003381// This should generate code that performs a charCodeAt() call or returns
3382// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3383// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003384void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003385 VirtualFrame::SpilledScope spilled_scope;
kasper.lund7276f142008-07-30 08:49:36 +00003386 ASSERT(args->length() == 2);
kasper.lund7276f142008-07-30 08:49:36 +00003387 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003388 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003389}
3390
3391
ager@chromium.org7c537e22008-10-16 08:43:32 +00003392void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003393 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003394 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003395 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003396 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003397 // We need the CC bits to come out as not_equal in the case where the
3398 // object is a smi. This can't be done with the usual test opcode so
3399 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003400 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003401 __ and_(r1, r0, Operand(kSmiTagMask));
3402 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003403 answer.Branch(ne);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003404 // It is a heap object - get the map.
3405 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3406 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3407 // Check if the object is a JS array or not.
3408 __ cmp(r1, Operand(JS_ARRAY_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003409 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003410 cc_reg_ = eq;
3411}
3412
3413
ager@chromium.org7c537e22008-10-16 08:43:32 +00003414void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003415 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003416 ASSERT(args->length() == 0);
3417
mads.s.ager31e71382008-08-13 09:32:07 +00003418 // Seed the result with the formal parameters count, which will be used
3419 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003420 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
3421
3422 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003423 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003424 frame_->CallStub(&stub, 0);
3425 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003426}
3427
3428
ager@chromium.org7c537e22008-10-16 08:43:32 +00003429void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003430 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003431 ASSERT(args->length() == 1);
3432
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003433 // Satisfy contract with ArgumentsAccessStub:
3434 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003435 LoadAndSpill(args->at(0));
3436 frame_->EmitPop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003437 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003438
3439 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003440 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003441 frame_->CallStub(&stub, 0);
3442 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003443}
3444
3445
ager@chromium.org7c537e22008-10-16 08:43:32 +00003446void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003447 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003448 ASSERT(args->length() == 2);
3449
3450 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003451 LoadAndSpill(args->at(0));
3452 LoadAndSpill(args->at(1));
3453 frame_->EmitPop(r0);
3454 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003455 __ cmp(r0, Operand(r1));
3456 cc_reg_ = eq;
3457}
3458
3459
ager@chromium.org7c537e22008-10-16 08:43:32 +00003460void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003461#ifdef DEBUG
3462 int original_height = frame_->height();
3463#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003464 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003465 if (CheckForInlineRuntimeCall(node)) {
3466 ASSERT((has_cc() && frame_->height() == original_height) ||
3467 (!has_cc() && frame_->height() == original_height + 1));
3468 return;
3469 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003470
3471 ZoneList<Expression*>* args = node->arguments();
3472 Comment cmnt(masm_, "[ CallRuntime");
3473 Runtime::Function* function = node->function();
3474
ager@chromium.org41826e72009-03-30 13:30:57 +00003475 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003476 // Prepare stack for calling JS runtime function.
3477 __ mov(r0, Operand(node->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003478 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003479 // Push the builtins object found in the current global object.
3480 __ ldr(r1, GlobalObject());
3481 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003482 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003483 }
mads.s.ager31e71382008-08-13 09:32:07 +00003484
ager@chromium.org41826e72009-03-30 13:30:57 +00003485 // Push the arguments ("left-to-right").
3486 int arg_count = args->length();
3487 for (int i = 0; i < arg_count; i++) {
3488 LoadAndSpill(args->at(i));
3489 }
mads.s.ager31e71382008-08-13 09:32:07 +00003490
ager@chromium.org41826e72009-03-30 13:30:57 +00003491 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003492 // Call the JS runtime function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003493 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3494 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003495 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003496 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003497 frame_->Drop();
3498 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003499 } else {
3500 // Call the C runtime function.
3501 frame_->CallRuntime(function, arg_count);
3502 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003503 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003504 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003505}
3506
3507
ager@chromium.org7c537e22008-10-16 08:43:32 +00003508void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003509#ifdef DEBUG
3510 int original_height = frame_->height();
3511#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003512 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003513 Comment cmnt(masm_, "[ UnaryOperation");
3514
3515 Token::Value op = node->op();
3516
3517 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003518 LoadConditionAndSpill(node->expression(),
3519 NOT_INSIDE_TYPEOF,
3520 false_target(),
3521 true_target(),
3522 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003523 cc_reg_ = NegateCondition(cc_reg_);
3524
3525 } else if (op == Token::DELETE) {
3526 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003527 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003528 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003529 LoadAndSpill(property->obj());
3530 LoadAndSpill(property->key());
3531 Result arg_count = allocator_->Allocate(r0);
3532 ASSERT(arg_count.is_valid());
3533 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3534 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003535
mads.s.ager31e71382008-08-13 09:32:07 +00003536 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003537 Slot* slot = variable->slot();
3538 if (variable->is_global()) {
3539 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003540 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003541 frame_->EmitPush(r0);
3542 Result arg_count = allocator_->Allocate(r0);
3543 ASSERT(arg_count.is_valid());
3544 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3545 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003546
3547 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3548 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003549 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003550 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003551 frame_->EmitPush(r0);
3552 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003553 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003554 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003555 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003556 frame_->EmitPush(r0);
3557 Result arg_count = allocator_->Allocate(r0);
3558 ASSERT(arg_count.is_valid());
3559 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3560 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003561
mads.s.ager31e71382008-08-13 09:32:07 +00003562 } else {
3563 // Default: Result of deleting non-global, not dynamically
3564 // introduced variables is false.
3565 __ mov(r0, Operand(Factory::false_value()));
3566 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003567
3568 } else {
3569 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003570 LoadAndSpill(node->expression()); // may have side-effects
3571 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003572 __ mov(r0, Operand(Factory::true_value()));
3573 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003574 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003575
3576 } else if (op == Token::TYPEOF) {
3577 // Special case for loading the typeof expression; see comment on
3578 // LoadTypeofExpression().
3579 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003580 frame_->CallRuntime(Runtime::kTypeof, 1);
3581 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003582
3583 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003584 LoadAndSpill(node->expression());
3585 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003586 switch (op) {
3587 case Token::NOT:
3588 case Token::DELETE:
3589 case Token::TYPEOF:
3590 UNREACHABLE(); // handled above
3591 break;
3592
3593 case Token::SUB: {
3594 UnarySubStub stub;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003595 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003596 break;
3597 }
3598
3599 case Token::BIT_NOT: {
3600 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003601 JumpTarget smi_label;
3602 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003603 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003604 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003605
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003606 frame_->EmitPush(r0);
3607 Result arg_count = allocator_->Allocate(r0);
3608 ASSERT(arg_count.is_valid());
3609 __ mov(arg_count.reg(), Operand(0)); // not counting receiver
3610 frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003611
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003612 continue_label.Jump();
3613 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003614 __ mvn(r0, Operand(r0));
3615 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003616 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003617 break;
3618 }
3619
3620 case Token::VOID:
3621 // since the stack top is cached in r0, popping and then
3622 // pushing a value can be done by just writing to r0.
3623 __ mov(r0, Operand(Factory::undefined_value()));
3624 break;
3625
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003626 case Token::ADD: {
3627 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003628 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003629 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003630 continue_label.Branch(eq);
3631 frame_->EmitPush(r0);
3632 Result arg_count = allocator_->Allocate(r0);
3633 ASSERT(arg_count.is_valid());
3634 __ mov(arg_count.reg(), Operand(0)); // not counting receiver
3635 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3636 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003637 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003638 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003639 default:
3640 UNREACHABLE();
3641 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003642 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003643 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003644 ASSERT((has_cc() && frame_->height() == original_height) ||
3645 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003646}
3647
3648
ager@chromium.org7c537e22008-10-16 08:43:32 +00003649void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003650#ifdef DEBUG
3651 int original_height = frame_->height();
3652#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003653 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003654 Comment cmnt(masm_, "[ CountOperation");
3655
3656 bool is_postfix = node->is_postfix();
3657 bool is_increment = node->op() == Token::INC;
3658
3659 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3660 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3661
3662 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003663 if (is_postfix) {
3664 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003665 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003666 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003667
3668 { Reference target(this, node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003669 if (target.is_illegal()) {
3670 // Spoof the virtual frame to have the expected height (one higher
3671 // than on entry).
3672 if (!is_postfix) {
3673 __ mov(r0, Operand(Smi::FromInt(0)));
3674 frame_->EmitPush(r0);
3675 }
3676 ASSERT(frame_->height() == original_height + 1);
3677 return;
3678 }
3679 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
3680 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003681
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003682 JumpTarget slow;
3683 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003684
3685 // Load the value (1) into register r1.
3686 __ mov(r1, Operand(Smi::FromInt(1)));
3687
3688 // Check for smi operand.
3689 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003690 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003691
3692 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003693 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003694 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003695 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003696
3697 // Perform optimistic increment/decrement.
3698 if (is_increment) {
3699 __ add(r0, r0, Operand(r1), SetCC);
3700 } else {
3701 __ sub(r0, r0, Operand(r1), SetCC);
3702 }
3703
3704 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003705 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003706
3707 // Revert optimistic increment/decrement.
3708 if (is_increment) {
3709 __ sub(r0, r0, Operand(r1));
3710 } else {
3711 __ add(r0, r0, Operand(r1));
3712 }
3713
3714 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003715 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003716 {
3717 // Convert the operand to a number.
3718 frame_->EmitPush(r0);
3719 Result arg_count = allocator_->Allocate(r0);
3720 ASSERT(arg_count.is_valid());
3721 __ mov(arg_count.reg(), Operand(0));
3722 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3723 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003724 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003725 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003726 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003727 }
3728
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003729 // Compute the new value.
3730 __ mov(r1, Operand(Smi::FromInt(1)));
3731 frame_->EmitPush(r0);
3732 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003733 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003734 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003735 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003736 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003737 }
3738
3739 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003740 exit.Bind();
3741 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003742 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003743 }
3744
3745 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003746 if (is_postfix) frame_->EmitPop(r0);
3747 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003748}
3749
3750
ager@chromium.org7c537e22008-10-16 08:43:32 +00003751void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003752#ifdef DEBUG
3753 int original_height = frame_->height();
3754#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003755 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003756 Comment cmnt(masm_, "[ BinaryOperation");
3757 Token::Value op = node->op();
3758
3759 // According to ECMA-262 section 11.11, page 58, the binary logical
3760 // operators must yield the result of one of the two expressions
3761 // before any ToBoolean() conversions. This means that the value
3762 // produced by a && or || operator is not necessarily a boolean.
3763
3764 // NOTE: If the left hand side produces a materialized value (not in
3765 // the CC register), we force the right hand side to do the
3766 // same. This is necessary because we may have to branch to the exit
3767 // after evaluating the left hand side (due to the shortcut
3768 // semantics), but the compiler must (statically) know if the result
3769 // of compiling the binary operation is materialized or not.
3770
3771 if (op == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003772 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003773 LoadConditionAndSpill(node->left(),
3774 NOT_INSIDE_TYPEOF,
3775 &is_true,
3776 false_target(),
3777 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003778 if (has_cc()) {
3779 Branch(false, false_target());
3780
3781 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003782 is_true.Bind();
3783 LoadConditionAndSpill(node->right(),
3784 NOT_INSIDE_TYPEOF,
3785 true_target(),
3786 false_target(),
3787 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003788
3789 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003790 JumpTarget pop_and_continue;
3791 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003792
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003793 __ ldr(r0, frame_->Top()); // dup the stack top
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003794 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003795 // Avoid popping the result if it converts to 'false' using the
3796 // standard ToBoolean() conversion as described in ECMA-262,
3797 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003798 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003799 Branch(false, &exit);
3800
3801 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003802 pop_and_continue.Bind();
3803 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003804
3805 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003806 is_true.Bind();
3807 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003808
3809 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003810 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003811 }
3812
3813 } else if (op == Token::OR) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003814 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003815 LoadConditionAndSpill(node->left(),
3816 NOT_INSIDE_TYPEOF,
3817 true_target(),
3818 &is_false,
3819 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003820 if (has_cc()) {
3821 Branch(true, true_target());
3822
3823 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003824 is_false.Bind();
3825 LoadConditionAndSpill(node->right(),
3826 NOT_INSIDE_TYPEOF,
3827 true_target(),
3828 false_target(),
3829 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003830
3831 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003832 JumpTarget pop_and_continue;
3833 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003834
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003835 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003836 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003837 // Avoid popping the result if it converts to 'true' using the
3838 // standard ToBoolean() conversion as described in ECMA-262,
3839 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003840 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003841 Branch(true, &exit);
3842
3843 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003844 pop_and_continue.Bind();
3845 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003846
3847 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003848 is_false.Bind();
3849 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003850
3851 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003852 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003853 }
3854
3855 } else {
3856 // Optimize for the case where (at least) one of the expressions
3857 // is a literal small integer.
3858 Literal* lliteral = node->left()->AsLiteral();
3859 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003860 // NOTE: The code below assumes that the slow cases (calls to runtime)
3861 // never return a constant/immutable object.
3862 bool overwrite_left =
3863 (node->left()->AsBinaryOperation() != NULL &&
3864 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
3865 bool overwrite_right =
3866 (node->right()->AsBinaryOperation() != NULL &&
3867 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003868
3869 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003870 LoadAndSpill(node->left());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003871 SmiOperation(node->op(),
3872 rliteral->handle(),
3873 false,
3874 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003875
3876 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003877 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003878 SmiOperation(node->op(),
3879 lliteral->handle(),
3880 true,
3881 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003882
3883 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003884 OverwriteMode overwrite_mode = NO_OVERWRITE;
3885 if (overwrite_left) {
3886 overwrite_mode = OVERWRITE_LEFT;
3887 } else if (overwrite_right) {
3888 overwrite_mode = OVERWRITE_RIGHT;
3889 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003890 LoadAndSpill(node->left());
3891 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003892 GenericBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003893 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003894 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003895 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003896 ASSERT((has_cc() && frame_->height() == original_height) ||
3897 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003898}
3899
3900
ager@chromium.org7c537e22008-10-16 08:43:32 +00003901void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003902#ifdef DEBUG
3903 int original_height = frame_->height();
3904#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003905 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003906 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003907 frame_->EmitPush(r0);
3908 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003909}
3910
3911
ager@chromium.org7c537e22008-10-16 08:43:32 +00003912void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003913#ifdef DEBUG
3914 int original_height = frame_->height();
3915#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003916 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003917 Comment cmnt(masm_, "[ CompareOperation");
3918
3919 // Get the expressions from the node.
3920 Expression* left = node->left();
3921 Expression* right = node->right();
3922 Token::Value op = node->op();
3923
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003924 // To make null checks efficient, we check if either left or right is the
3925 // literal 'null'. If so, we optimize the code by inlining a null check
3926 // instead of calling the (very) general runtime routine for checking
3927 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003928 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003929 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003930 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003931 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003932 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
3933 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003934 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003935 LoadAndSpill(left_is_null ? right : left);
3936 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003937 __ cmp(r0, Operand(Factory::null_value()));
3938
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003939 // The 'null' value is only equal to 'undefined' if using non-strict
3940 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003941 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003942 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003943
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003944 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003945 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003946
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003947 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003948 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003949
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003950 // It can be an undetectable object.
3951 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3952 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
3953 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3954 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003955 }
3956
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003957 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003958 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003959 return;
3960 }
3961 }
3962
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003963 // To make typeof testing for natives implemented in JavaScript really
3964 // efficient, we generate special code for expressions of the form:
3965 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003966 UnaryOperation* operation = left->AsUnaryOperation();
3967 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
3968 (operation != NULL && operation->op() == Token::TYPEOF) &&
3969 (right->AsLiteral() != NULL &&
3970 right->AsLiteral()->handle()->IsString())) {
3971 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
3972
mads.s.ager31e71382008-08-13 09:32:07 +00003973 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003974 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003975 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003976
3977 if (check->Equals(Heap::number_symbol())) {
3978 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003979 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003980 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3981 __ cmp(r1, Operand(Factory::heap_number_map()));
3982 cc_reg_ = eq;
3983
3984 } else if (check->Equals(Heap::string_symbol())) {
3985 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003986 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003987
3988 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3989
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003990 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003991 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3992 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3993 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003994 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003995
3996 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3997 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
3998 cc_reg_ = lt;
3999
4000 } else if (check->Equals(Heap::boolean_symbol())) {
4001 __ cmp(r1, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004002 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004003 __ cmp(r1, Operand(Factory::false_value()));
4004 cc_reg_ = eq;
4005
4006 } else if (check->Equals(Heap::undefined_symbol())) {
4007 __ cmp(r1, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004008 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004009
4010 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004011 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004012
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004013 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004014 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4015 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4016 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4017 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4018
4019 cc_reg_ = eq;
4020
4021 } else if (check->Equals(Heap::function_symbol())) {
4022 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004023 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004024 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4025 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
4026 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
4027 cc_reg_ = eq;
4028
4029 } else if (check->Equals(Heap::object_symbol())) {
4030 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004031 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004032
4033 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4034 __ cmp(r1, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004035 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004036
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004037 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004038 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
4039 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4040 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004041 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004042
4043 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4044 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004045 false_target()->Branch(lt);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004046 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
4047 cc_reg_ = le;
4048
4049 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004050 // Uncommon case: typeof testing against a string literal that is
4051 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004052 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004053 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004054 ASSERT(!has_valid_frame() ||
4055 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004056 return;
4057 }
4058
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004059 LoadAndSpill(left);
4060 LoadAndSpill(right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004061 switch (op) {
4062 case Token::EQ:
4063 Comparison(eq, false);
4064 break;
4065
4066 case Token::LT:
4067 Comparison(lt);
4068 break;
4069
4070 case Token::GT:
4071 Comparison(gt);
4072 break;
4073
4074 case Token::LTE:
4075 Comparison(le);
4076 break;
4077
4078 case Token::GTE:
4079 Comparison(ge);
4080 break;
4081
4082 case Token::EQ_STRICT:
4083 Comparison(eq, true);
4084 break;
4085
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004086 case Token::IN: {
4087 Result arg_count = allocator_->Allocate(r0);
4088 ASSERT(arg_count.is_valid());
4089 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
4090 Result result = frame_->InvokeBuiltin(Builtins::IN,
4091 CALL_JS,
4092 &arg_count,
4093 2);
4094 frame_->EmitPush(result.reg());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004095 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004096 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004097
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004098 case Token::INSTANCEOF: {
4099 Result arg_count = allocator_->Allocate(r0);
4100 ASSERT(arg_count.is_valid());
4101 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
4102 Result result = frame_->InvokeBuiltin(Builtins::INSTANCE_OF,
4103 CALL_JS,
4104 &arg_count,
4105 2);
4106 __ tst(result.reg(), Operand(result.reg()));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004107 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004108 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004109 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004110
4111 default:
4112 UNREACHABLE();
4113 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004114 ASSERT((has_cc() && frame_->height() == original_height) ||
4115 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004116}
4117
4118
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004119#ifdef DEBUG
4120bool CodeGenerator::HasValidEntryRegisters() { return true; }
4121#endif
4122
4123
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004124#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004125#define __ ACCESS_MASM(masm)
4126
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004127
ager@chromium.org7c537e22008-10-16 08:43:32 +00004128Handle<String> Reference::GetName() {
4129 ASSERT(type_ == NAMED);
4130 Property* property = expression_->AsProperty();
4131 if (property == NULL) {
4132 // Global variable reference treated as a named property reference.
4133 VariableProxy* proxy = expression_->AsVariableProxy();
4134 ASSERT(proxy->AsVariable() != NULL);
4135 ASSERT(proxy->AsVariable()->is_global());
4136 return proxy->name();
4137 } else {
4138 Literal* raw_name = property->key()->AsLiteral();
4139 ASSERT(raw_name != NULL);
4140 return Handle<String>(String::cast(*raw_name->handle()));
4141 }
4142}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004143
ager@chromium.org7c537e22008-10-16 08:43:32 +00004144
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004145void Reference::GetValueAndSpill(TypeofState typeof_state) {
4146 ASSERT(cgen_->in_spilled_code());
4147 cgen_->set_in_spilled_code(false);
4148 GetValue(typeof_state);
4149 cgen_->frame()->SpillAll();
4150 cgen_->set_in_spilled_code(true);
4151}
4152
4153
ager@chromium.org7c537e22008-10-16 08:43:32 +00004154void Reference::GetValue(TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004155 ASSERT(!cgen_->in_spilled_code());
4156 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004157 ASSERT(!is_illegal());
4158 ASSERT(!cgen_->has_cc());
4159 MacroAssembler* masm = cgen_->masm();
4160 Property* property = expression_->AsProperty();
4161 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004162 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004163 }
4164
4165 switch (type_) {
4166 case SLOT: {
4167 Comment cmnt(masm, "[ Load from Slot");
4168 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4169 ASSERT(slot != NULL);
4170 cgen_->LoadFromSlot(slot, typeof_state);
4171 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004172 }
4173
ager@chromium.org7c537e22008-10-16 08:43:32 +00004174 case NAMED: {
4175 // TODO(1241834): Make sure that this it is safe to ignore the
4176 // distinction between expressions in a typeof and not in a typeof. If
4177 // there is a chance that reference errors can be thrown below, we
4178 // must distinguish between the two kinds of loads (typeof expression
4179 // loads must not throw a reference error).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004180 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004181 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004182 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004183 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004184 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4185 // Setup the name register.
4186 Result name_reg = cgen_->allocator()->Allocate(r2);
4187 ASSERT(name_reg.is_valid());
4188 __ mov(name_reg.reg(), Operand(name));
4189 ASSERT(var == NULL || var->is_global());
4190 RelocInfo::Mode rmode = (var == NULL)
4191 ? RelocInfo::CODE_TARGET
4192 : RelocInfo::CODE_TARGET_CONTEXT;
4193 Result answer = frame->CallCodeObject(ic, rmode, &name_reg, 0);
4194 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004195 break;
4196 }
4197
4198 case KEYED: {
4199 // TODO(1241834): Make sure that this it is safe to ignore the
4200 // distinction between expressions in a typeof and not in a typeof.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004201
4202 // TODO(181): Implement inlined version of array indexing once
4203 // loop nesting is properly tracked on ARM.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004204 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004205 Comment cmnt(masm, "[ Load from keyed Property");
4206 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004207 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004208 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004209 ASSERT(var == NULL || var->is_global());
4210 RelocInfo::Mode rmode = (var == NULL)
4211 ? RelocInfo::CODE_TARGET
4212 : RelocInfo::CODE_TARGET_CONTEXT;
4213 Result answer = frame->CallCodeObject(ic, rmode, 0);
4214 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004215 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004216 }
4217
4218 default:
4219 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004220 }
4221}
4222
4223
ager@chromium.org7c537e22008-10-16 08:43:32 +00004224void Reference::SetValue(InitState init_state) {
4225 ASSERT(!is_illegal());
4226 ASSERT(!cgen_->has_cc());
4227 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004228 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004229 Property* property = expression_->AsProperty();
4230 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004231 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004232 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004233
ager@chromium.org7c537e22008-10-16 08:43:32 +00004234 switch (type_) {
4235 case SLOT: {
4236 Comment cmnt(masm, "[ Store to Slot");
4237 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4238 ASSERT(slot != NULL);
4239 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004240 ASSERT(slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004241
ager@chromium.org7c537e22008-10-16 08:43:32 +00004242 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004243 frame->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004244 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004245 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004246
ager@chromium.org7c537e22008-10-16 08:43:32 +00004247 if (init_state == CONST_INIT) {
4248 // Same as the case for a normal store, but ignores attribute
4249 // (e.g. READ_ONLY) of context slot so that we can initialize
4250 // const properties (introduced via eval("const foo = (some
4251 // expr);")). Also, uses the current function context instead of
4252 // the top context.
4253 //
4254 // Note that we must declare the foo upon entry of eval(), via a
4255 // context slot declaration, but we cannot initialize it at the
4256 // same time, because the const declaration may be at the end of
4257 // the eval code (sigh...) and the const variable may have been
4258 // used before (where its value is 'undefined'). Thus, we can only
4259 // do the initialization when we actually encounter the expression
4260 // and when the expression operands are defined and valid, and
4261 // thus we need the split into 2 operations: declaration of the
4262 // context slot followed by initialization.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004263 frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004264 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004265 frame->CallRuntime(Runtime::kStoreContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004266 }
4267 // Storing a variable must keep the (new) value on the expression
4268 // stack. This is necessary for compiling assignment expressions.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004269 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004270
ager@chromium.org7c537e22008-10-16 08:43:32 +00004271 } else {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004272 ASSERT(!slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004273
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004274 JumpTarget exit;
ager@chromium.org7c537e22008-10-16 08:43:32 +00004275 if (init_state == CONST_INIT) {
4276 ASSERT(slot->var()->mode() == Variable::CONST);
4277 // Only the first const initialization must be executed (the slot
4278 // still contains 'the hole' value). When the assignment is
4279 // executed, the code is identical to a normal store (see below).
4280 Comment cmnt(masm, "[ Init const");
4281 __ ldr(r2, cgen_->SlotOperand(slot, r2));
4282 __ cmp(r2, Operand(Factory::the_hole_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004283 exit.Branch(ne);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004284 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004285
ager@chromium.org7c537e22008-10-16 08:43:32 +00004286 // We must execute the store. Storing a variable must keep the
4287 // (new) value on the stack. This is necessary for compiling
4288 // assignment expressions.
4289 //
4290 // Note: We will reach here even with slot->var()->mode() ==
4291 // Variable::CONST because of const declarations which will
4292 // initialize consts to 'the hole' value and by doing so, end up
4293 // calling this code. r2 may be loaded with context; used below in
4294 // RecordWrite.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004295 frame->EmitPop(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004296 __ str(r0, cgen_->SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004297 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004298 if (slot->type() == Slot::CONTEXT) {
4299 // Skip write barrier if the written value is a smi.
4300 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004301 exit.Branch(eq);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004302 // r2 is loaded with context when calling SlotOperand above.
4303 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
4304 __ mov(r3, Operand(offset));
4305 __ RecordWrite(r2, r3, r1);
4306 }
4307 // If we definitely did not jump over the assignment, we do not need
4308 // to bind the exit label. Doing so can defeat peephole
4309 // optimization.
4310 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004311 exit.Bind();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004312 }
4313 }
4314 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004315 }
4316
ager@chromium.org7c537e22008-10-16 08:43:32 +00004317 case NAMED: {
4318 Comment cmnt(masm, "[ Store to named Property");
4319 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004320 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004321 Handle<String> name(GetName());
4322
4323 Result value = cgen_->allocator()->Allocate(r0);
4324 ASSERT(value.is_valid());
4325 frame->EmitPop(value.reg());
4326
4327 // Setup the name register.
4328 Result property_name = cgen_->allocator()->Allocate(r2);
4329 ASSERT(property_name.is_valid());
4330 __ mov(property_name.reg(), Operand(name));
4331 Result answer = frame->CallCodeObject(ic,
4332 RelocInfo::CODE_TARGET,
4333 &value,
4334 &property_name,
4335 0);
4336 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004337 break;
4338 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004339
ager@chromium.org7c537e22008-10-16 08:43:32 +00004340 case KEYED: {
4341 Comment cmnt(masm, "[ Store to keyed Property");
4342 Property* property = expression_->AsProperty();
4343 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004344 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004345
4346 // Call IC code.
4347 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
4348 // TODO(1222589): Make the IC grab the values from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004349 Result value = cgen_->allocator()->Allocate(r0);
4350 ASSERT(value.is_valid());
4351 frame->EmitPop(value.reg()); // value
4352 Result result =
4353 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0);
4354 frame->EmitPush(result.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004355 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004356 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004357
4358 default:
4359 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004360 }
4361}
4362
4363
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004364static void AllocateHeapNumber(
4365 MacroAssembler* masm,
4366 Label* need_gc, // Jump here if young space is full.
4367 Register result_reg, // The tagged address of the new heap number.
4368 Register allocation_top_addr_reg, // A scratch register.
4369 Register scratch2) { // Another scratch register.
4370 ExternalReference allocation_top =
4371 ExternalReference::new_space_allocation_top_address();
4372 ExternalReference allocation_limit =
4373 ExternalReference::new_space_allocation_limit_address();
4374
4375 // allocat := the address of the allocation top variable.
4376 __ mov(allocation_top_addr_reg, Operand(allocation_top));
4377 // result_reg := the old allocation top.
4378 __ ldr(result_reg, MemOperand(allocation_top_addr_reg));
4379 // scratch2 := the address of the allocation limit.
4380 __ mov(scratch2, Operand(allocation_limit));
4381 // scratch2 := the allocation limit.
4382 __ ldr(scratch2, MemOperand(scratch2));
4383 // result_reg := the new allocation top.
4384 __ add(result_reg, result_reg, Operand(HeapNumber::kSize));
4385 // Compare new new allocation top and limit.
4386 __ cmp(result_reg, Operand(scratch2));
4387 // Branch if out of space in young generation.
4388 __ b(hi, need_gc);
4389 // Store new allocation top.
4390 __ str(result_reg, MemOperand(allocation_top_addr_reg)); // store new top
4391 // Tag and adjust back to start of new object.
4392 __ sub(result_reg, result_reg, Operand(HeapNumber::kSize - kHeapObjectTag));
4393 // Get heap number map into scratch2.
4394 __ mov(scratch2, Operand(Factory::heap_number_map()));
4395 // Store heap number map in new object.
4396 __ str(scratch2, FieldMemOperand(result_reg, HeapObject::kMapOffset));
4397}
4398
4399
4400// We fall into this code if the operands were Smis, but the result was
4401// not (eg. overflow). We branch into this code (to the not_smi label) if
4402// the operands were not both Smi.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004403static void HandleBinaryOpSlowCases(MacroAssembler* masm,
4404 Label* not_smi,
4405 const Builtins::JavaScript& builtin,
4406 Token::Value operation,
4407 int swi_number,
4408 OverwriteMode mode) {
4409 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004410 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004411 __ push(r1);
4412 __ push(r0);
4413 __ mov(r0, Operand(1)); // Set number of arguments.
4414 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004415
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004416 __ bind(not_smi);
4417 __ tst(r0, Operand(kSmiTagMask));
4418 __ b(eq, &slow); // We can't handle a Smi-double combination yet.
4419 __ tst(r1, Operand(kSmiTagMask));
4420 __ b(eq, &slow); // We can't handle a Smi-double combination yet.
4421 // Get map of r0 into r2.
4422 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4423 // Get type of r0 into r3.
4424 __ ldrb(r3, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4425 __ cmp(r3, Operand(HEAP_NUMBER_TYPE));
4426 __ b(ne, &slow);
4427 // Get type of r1 into r3.
4428 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
4429 // Check they are both the same map (heap number map).
4430 __ cmp(r2, r3);
4431 __ b(ne, &slow);
4432 // Both are doubles.
4433 // Calling convention says that second double is in r2 and r3.
4434 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
4435 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4436
4437 if (mode == NO_OVERWRITE) {
4438 // Get address of new heap number into r5.
4439 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004440 __ push(lr);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004441 __ push(r5);
4442 } else if (mode == OVERWRITE_LEFT) {
4443 __ push(lr);
4444 __ push(r1);
4445 } else {
4446 ASSERT(mode == OVERWRITE_RIGHT);
4447 __ push(lr);
4448 __ push(r0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004449 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004450 // Calling convention says that first double is in r0 and r1.
4451 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
4452 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4453 // Call C routine that may not cause GC or other trouble.
4454 __ mov(r5, Operand(ExternalReference::double_fp_operation(operation)));
4455#if !defined(__arm__)
4456 // Notify the simulator that we are calling an add routine in C.
4457 __ swi(swi_number);
4458#else
4459 // Actually call the add routine written in C.
4460 __ Call(r5);
4461#endif
4462 // Store answer in the overwritable heap number.
4463 __ pop(r4);
4464#if !defined(__ARM_EABI__) && defined(__arm__)
4465 // Double returned in fp coprocessor register 0 and 1, encoded as register
4466 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
4467 // substract the tag from r4.
4468 __ sub(r5, r4, Operand(kHeapObjectTag));
4469 __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset));
4470#else
4471 // Double returned in fp coprocessor register 0 and 1.
4472 __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset));
4473 __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + kPointerSize));
4474#endif
4475 __ mov(r0, Operand(r4));
4476 // And we are done.
4477 __ pop(pc);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004478}
4479
4480
4481void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
4482 // r1 : x
4483 // r0 : y
4484 // result : r0
4485
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004486 // All ops need to know whether we are dealing with two Smis. Set up r2 to
4487 // tell us that.
4488 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
4489
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004490 switch (op_) {
4491 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004492 Label not_smi;
4493 // Fast path.
4494 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004495 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004496 __ b(ne, &not_smi);
4497 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
4498 // Return if no overflow.
4499 __ Ret(vc);
4500 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
4501
4502 HandleBinaryOpSlowCases(masm,
4503 &not_smi,
4504 Builtins::ADD,
4505 Token::ADD,
4506 assembler::arm::simulator_fp_add,
4507 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004508 break;
4509 }
4510
4511 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004512 Label not_smi;
4513 // Fast path.
4514 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004515 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004516 __ b(ne, &not_smi);
4517 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
4518 // Return if no overflow.
4519 __ Ret(vc);
4520 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
4521
4522 HandleBinaryOpSlowCases(masm,
4523 &not_smi,
4524 Builtins::SUB,
4525 Token::SUB,
4526 assembler::arm::simulator_fp_sub,
4527 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004528 break;
4529 }
4530
4531 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004532 Label not_smi, slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004533 ASSERT(kSmiTag == 0); // adjust code below
4534 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004535 __ b(ne, &not_smi);
4536 // Remove tag from one operand (but keep sign), so that result is Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004537 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004538 // Do multiplication
4539 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1.
4540 // Go slow on overflows (overflow bit is not set).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004541 __ mov(ip, Operand(r3, ASR, 31));
4542 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
4543 __ b(ne, &slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004544 // Go slow on zero result to handle -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004545 __ tst(r3, Operand(r3));
4546 __ mov(r0, Operand(r3), LeaveCC, ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004547 __ Ret(ne);
4548 // Slow case.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004549 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004550
4551 HandleBinaryOpSlowCases(masm,
4552 &not_smi,
4553 Builtins::MUL,
4554 Token::MUL,
4555 assembler::arm::simulator_fp_mul,
4556 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004557 break;
4558 }
4559
4560 case Token::BIT_OR:
4561 case Token::BIT_AND:
4562 case Token::BIT_XOR: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004563 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004564 ASSERT(kSmiTag == 0); // adjust code below
4565 __ tst(r2, Operand(kSmiTagMask));
4566 __ b(ne, &slow);
4567 switch (op_) {
4568 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
4569 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
4570 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
4571 default: UNREACHABLE();
4572 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004573 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004574 __ bind(&slow);
4575 __ push(r1); // restore stack
4576 __ push(r0);
4577 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
4578 switch (op_) {
4579 case Token::BIT_OR:
4580 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
4581 break;
4582 case Token::BIT_AND:
4583 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
4584 break;
4585 case Token::BIT_XOR:
4586 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
4587 break;
4588 default:
4589 UNREACHABLE();
4590 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004591 break;
4592 }
4593
4594 case Token::SHL:
4595 case Token::SHR:
4596 case Token::SAR: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004597 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004598 ASSERT(kSmiTag == 0); // adjust code below
4599 __ tst(r2, Operand(kSmiTagMask));
4600 __ b(ne, &slow);
4601 // remove tags from operands (but keep sign)
4602 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
4603 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
4604 // use only the 5 least significant bits of the shift count
4605 __ and_(r2, r2, Operand(0x1f));
4606 // perform operation
4607 switch (op_) {
4608 case Token::SAR:
4609 __ mov(r3, Operand(r3, ASR, r2));
4610 // no checks of result necessary
4611 break;
4612
4613 case Token::SHR:
4614 __ mov(r3, Operand(r3, LSR, r2));
4615 // check that the *unsigned* result fits in a smi
4616 // neither of the two high-order bits can be set:
4617 // - 0x80000000: high bit would be lost when smi tagging
4618 // - 0x40000000: this number would convert to negative when
4619 // smi tagging these two cases can only happen with shifts
4620 // by 0 or 1 when handed a valid smi
4621 __ and_(r2, r3, Operand(0xc0000000), SetCC);
4622 __ b(ne, &slow);
4623 break;
4624
4625 case Token::SHL:
4626 __ mov(r3, Operand(r3, LSL, r2));
4627 // check that the *signed* result fits in a smi
4628 __ add(r2, r3, Operand(0x40000000), SetCC);
4629 __ b(mi, &slow);
4630 break;
4631
4632 default: UNREACHABLE();
4633 }
4634 // tag result and store it in r0
4635 ASSERT(kSmiTag == 0); // adjust code below
4636 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004637 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004638 // slow case
4639 __ bind(&slow);
4640 __ push(r1); // restore stack
4641 __ push(r0);
4642 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
4643 switch (op_) {
4644 case Token::SAR: __ InvokeBuiltin(Builtins::SAR, JUMP_JS); break;
4645 case Token::SHR: __ InvokeBuiltin(Builtins::SHR, JUMP_JS); break;
4646 case Token::SHL: __ InvokeBuiltin(Builtins::SHL, JUMP_JS); break;
4647 default: UNREACHABLE();
4648 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004649 break;
4650 }
4651
4652 default: UNREACHABLE();
4653 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004654 // This code should be unreachable.
4655 __ stop("Unreachable");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004656}
4657
4658
4659void StackCheckStub::Generate(MacroAssembler* masm) {
4660 Label within_limit;
4661 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
4662 __ ldr(ip, MemOperand(ip));
4663 __ cmp(sp, Operand(ip));
4664 __ b(hs, &within_limit);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00004665 // Do tail-call to runtime routine. Runtime routines expect at least one
4666 // argument, so give it a Smi.
4667 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004668 __ push(r0);
4669 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
4670 __ bind(&within_limit);
4671
4672 __ StubReturn(1);
4673}
4674
4675
4676void UnarySubStub::Generate(MacroAssembler* masm) {
4677 Label undo;
4678 Label slow;
4679 Label done;
4680
4681 // Enter runtime system if the value is not a smi.
4682 __ tst(r0, Operand(kSmiTagMask));
4683 __ b(ne, &slow);
4684
4685 // Enter runtime system if the value of the expression is zero
4686 // to make sure that we switch between 0 and -0.
4687 __ cmp(r0, Operand(0));
4688 __ b(eq, &slow);
4689
4690 // The value of the expression is a smi that is not zero. Try
4691 // optimistic subtraction '0 - value'.
4692 __ rsb(r1, r0, Operand(0), SetCC);
4693 __ b(vs, &slow);
4694
4695 // If result is a smi we are done.
4696 __ tst(r1, Operand(kSmiTagMask));
4697 __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result
4698 __ b(eq, &done);
4699
4700 // Enter runtime system.
4701 __ bind(&slow);
4702 __ push(r0);
4703 __ mov(r0, Operand(0)); // set number of arguments
4704 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
4705
4706 __ bind(&done);
4707 __ StubReturn(1);
4708}
4709
4710
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004711void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
4712 // r0 holds exception
4713 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
4714 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
4715 __ ldr(sp, MemOperand(r3));
4716 __ pop(r2); // pop next in chain
4717 __ str(r2, MemOperand(r3));
4718 // restore parameter- and frame-pointer and pop state.
4719 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
4720 // Before returning we restore the context from the frame pointer if not NULL.
4721 // The frame pointer is NULL in the exception handler of a JS entry frame.
4722 __ cmp(fp, Operand(0));
4723 // Set cp to NULL if fp is NULL.
4724 __ mov(cp, Operand(0), LeaveCC, eq);
4725 // Restore cp otherwise.
4726 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004727#ifdef DEBUG
4728 if (FLAG_debug_code) {
4729 __ mov(lr, Operand(pc));
4730 }
4731#endif
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004732 __ pop(pc);
4733}
4734
4735
4736void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
4737 // Fetch top stack handler.
4738 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
4739 __ ldr(r3, MemOperand(r3));
4740
4741 // Unwind the handlers until the ENTRY handler is found.
4742 Label loop, done;
4743 __ bind(&loop);
4744 // Load the type of the current stack handler.
4745 const int kStateOffset = StackHandlerConstants::kAddressDisplacement +
4746 StackHandlerConstants::kStateOffset;
4747 __ ldr(r2, MemOperand(r3, kStateOffset));
4748 __ cmp(r2, Operand(StackHandler::ENTRY));
4749 __ b(eq, &done);
4750 // Fetch the next handler in the list.
4751 const int kNextOffset = StackHandlerConstants::kAddressDisplacement +
4752 StackHandlerConstants::kNextOffset;
4753 __ ldr(r3, MemOperand(r3, kNextOffset));
4754 __ jmp(&loop);
4755 __ bind(&done);
4756
4757 // Set the top handler address to next handler past the current ENTRY handler.
4758 __ ldr(r0, MemOperand(r3, kNextOffset));
4759 __ mov(r2, Operand(ExternalReference(Top::k_handler_address)));
4760 __ str(r0, MemOperand(r2));
4761
4762 // Set external caught exception to false.
4763 __ mov(r0, Operand(false));
4764 ExternalReference external_caught(Top::k_external_caught_exception_address);
4765 __ mov(r2, Operand(external_caught));
4766 __ str(r0, MemOperand(r2));
4767
4768 // Set pending exception and r0 to out of memory exception.
4769 Failure* out_of_memory = Failure::OutOfMemoryException();
4770 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
4771 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
4772 __ str(r0, MemOperand(r2));
4773
4774 // Restore the stack to the address of the ENTRY handler
4775 __ mov(sp, Operand(r3));
4776
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004777 // Stack layout at this point. See also PushTryHandler
4778 // r3, sp -> next handler
4779 // state (ENTRY)
4780 // pp
4781 // fp
4782 // lr
4783
4784 // Discard ENTRY state (r2 is not used), and restore parameter-
4785 // and frame-pointer and pop state.
4786 __ ldm(ia_w, sp, r2.bit() | r3.bit() | pp.bit() | fp.bit());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004787 // Before returning we restore the context from the frame pointer if not NULL.
4788 // The frame pointer is NULL in the exception handler of a JS entry frame.
4789 __ cmp(fp, Operand(0));
4790 // Set cp to NULL if fp is NULL.
4791 __ mov(cp, Operand(0), LeaveCC, eq);
4792 // Restore cp otherwise.
4793 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004794#ifdef DEBUG
4795 if (FLAG_debug_code) {
4796 __ mov(lr, Operand(pc));
4797 }
4798#endif
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004799 __ pop(pc);
4800}
4801
4802
4803void CEntryStub::GenerateCore(MacroAssembler* masm,
4804 Label* throw_normal_exception,
4805 Label* throw_out_of_memory_exception,
4806 StackFrame::Type frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004807 bool do_gc,
4808 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004809 // r0: result parameter for PerformGC, if any
4810 // r4: number of arguments including receiver (C callee-saved)
4811 // r5: pointer to builtin function (C callee-saved)
4812 // r6: pointer to the first argument (C callee-saved)
4813
4814 if (do_gc) {
4815 // Passing r0.
4816 __ Call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
4817 }
4818
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004819 ExternalReference scope_depth =
4820 ExternalReference::heap_always_allocate_scope_depth();
4821 if (always_allocate) {
4822 __ mov(r0, Operand(scope_depth));
4823 __ ldr(r1, MemOperand(r0));
4824 __ add(r1, r1, Operand(1));
4825 __ str(r1, MemOperand(r0));
4826 }
4827
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004828 // Call C built-in.
4829 // r0 = argc, r1 = argv
4830 __ mov(r0, Operand(r4));
4831 __ mov(r1, Operand(r6));
4832
4833 // TODO(1242173): To let the GC traverse the return address of the exit
4834 // frames, we need to know where the return address is. Right now,
4835 // we push it on the stack to be able to find it again, but we never
4836 // restore from it in case of changes, which makes it impossible to
4837 // support moving the C entry code stub. This should be fixed, but currently
4838 // this is OK because the CEntryStub gets generated so early in the V8 boot
4839 // sequence that it is not moving ever.
4840 __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
4841 __ push(lr);
4842#if !defined(__arm__)
4843 // Notify the simulator of the transition to C code.
4844 __ swi(assembler::arm::call_rt_r5);
4845#else /* !defined(__arm__) */
ager@chromium.org41826e72009-03-30 13:30:57 +00004846 __ Jump(r5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004847#endif /* !defined(__arm__) */
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004848
4849 if (always_allocate) {
4850 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
4851 // though (contain the result).
4852 __ mov(r2, Operand(scope_depth));
4853 __ ldr(r3, MemOperand(r2));
4854 __ sub(r3, r3, Operand(1));
4855 __ str(r3, MemOperand(r2));
4856 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004857
4858 // check for failure result
4859 Label failure_returned;
4860 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4861 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
4862 __ add(r2, r0, Operand(1));
4863 __ tst(r2, Operand(kFailureTagMask));
4864 __ b(eq, &failure_returned);
4865
4866 // Exit C frame and return.
4867 // r0:r1: result
4868 // sp: stack pointer
4869 // fp: frame pointer
4870 // pp: caller's parameter pointer pp (restored as C callee-saved)
4871 __ LeaveExitFrame(frame_type);
4872
4873 // check if we should retry or throw exception
4874 Label retry;
4875 __ bind(&failure_returned);
4876 ASSERT(Failure::RETRY_AFTER_GC == 0);
4877 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
4878 __ b(eq, &retry);
4879
4880 Label continue_exception;
4881 // If the returned failure is EXCEPTION then promote Top::pending_exception().
4882 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
4883 __ b(ne, &continue_exception);
4884
4885 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00004886 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004887 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00004888 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004889 __ ldr(r0, MemOperand(ip));
4890 __ str(r3, MemOperand(ip));
4891
4892 __ bind(&continue_exception);
4893 // Special handling of out of memory exception.
4894 Failure* out_of_memory = Failure::OutOfMemoryException();
4895 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
4896 __ b(eq, throw_out_of_memory_exception);
4897
4898 // Handle normal exception.
4899 __ jmp(throw_normal_exception);
4900
4901 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
4902}
4903
4904
4905void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
4906 // Called from JavaScript; parameters are on stack as if calling JS function
4907 // r0: number of arguments including receiver
4908 // r1: pointer to builtin function
4909 // fp: frame pointer (restored after C call)
4910 // sp: stack pointer (restored as callee's pp after C call)
4911 // cp: current context (C callee-saved)
4912 // pp: caller's parameter pointer pp (C callee-saved)
4913
4914 // NOTE: Invocations of builtins may return failure objects
4915 // instead of a proper result. The builtin entry handles
4916 // this by performing a garbage collection and retrying the
4917 // builtin once.
4918
4919 StackFrame::Type frame_type = is_debug_break
4920 ? StackFrame::EXIT_DEBUG
4921 : StackFrame::EXIT;
4922
4923 // Enter the exit frame that transitions from JavaScript to C++.
4924 __ EnterExitFrame(frame_type);
4925
4926 // r4: number of arguments (C callee-saved)
4927 // r5: pointer to builtin function (C callee-saved)
4928 // r6: pointer to first argument (C callee-saved)
4929
4930 Label throw_out_of_memory_exception;
4931 Label throw_normal_exception;
4932
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004933 // Call into the runtime system. Collect garbage before the call if
4934 // running with --gc-greedy set.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004935 if (FLAG_gc_greedy) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004936 Failure* failure = Failure::RetryAfterGC(0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004937 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
4938 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004939 GenerateCore(masm, &throw_normal_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004940 &throw_out_of_memory_exception,
4941 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004942 FLAG_gc_greedy,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004943 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004944
4945 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004946 GenerateCore(masm,
4947 &throw_normal_exception,
4948 &throw_out_of_memory_exception,
4949 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004950 true,
4951 false);
4952
4953 // Do full GC and retry runtime call one final time.
4954 Failure* failure = Failure::InternalError();
4955 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
4956 GenerateCore(masm,
4957 &throw_normal_exception,
4958 &throw_out_of_memory_exception,
4959 frame_type,
4960 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004961 true);
4962
4963 __ bind(&throw_out_of_memory_exception);
4964 GenerateThrowOutOfMemory(masm);
4965 // control flow for generated will not return.
4966
4967 __ bind(&throw_normal_exception);
4968 GenerateThrowTOS(masm);
4969}
4970
4971
4972void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4973 // r0: code entry
4974 // r1: function
4975 // r2: receiver
4976 // r3: argc
4977 // [sp+0]: argv
4978
4979 Label invoke, exit;
4980
4981 // Called from C, so do not pop argc and args on exit (preserve sp)
4982 // No need to save register-passed args
4983 // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr
4984 __ stm(db_w, sp, kCalleeSaved | lr.bit());
4985
4986 // Get address of argv, see stm above.
4987 // r0: code entry
4988 // r1: function
4989 // r2: receiver
4990 // r3: argc
4991 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
4992 __ ldr(r4, MemOperand(r4)); // argv
4993
4994 // Push a frame with special values setup to mark it as an entry frame.
4995 // r0: code entry
4996 // r1: function
4997 // r2: receiver
4998 // r3: argc
4999 // r4: argv
5000 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
5001 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
5002 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
5003 __ mov(r6, Operand(Smi::FromInt(marker)));
5004 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
5005 __ ldr(r5, MemOperand(r5));
5006 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
5007
5008 // Setup frame pointer for the frame to be pushed.
5009 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
5010
5011 // Call a faked try-block that does the invoke.
5012 __ bl(&invoke);
5013
5014 // Caught exception: Store result (exception) in the pending
5015 // exception field in the JSEnv and return a failure sentinel.
5016 // Coming in here the fp will be invalid because the PushTryHandler below
5017 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00005018 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005019 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005020 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005021 __ b(&exit);
5022
5023 // Invoke: Link this frame into the handler chain.
5024 __ bind(&invoke);
5025 // Must preserve r0-r4, r5-r7 are available.
5026 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
5027 // If an exception not caught by another handler occurs, this handler returns
5028 // control to the code after the bl(&invoke) above, which restores all
5029 // kCalleeSaved registers (including cp, pp and fp) to their saved values
5030 // before returning a failure to C.
5031
5032 // Clear any pending exceptions.
5033 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
5034 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00005035 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005036 __ str(r5, MemOperand(ip));
5037
5038 // Invoke the function by calling through JS entry trampoline builtin.
5039 // Notice that we cannot store a reference to the trampoline code directly in
5040 // this stub, because runtime stubs are not traversed when doing GC.
5041
5042 // Expected registers by Builtins::JSEntryTrampoline
5043 // r0: code entry
5044 // r1: function
5045 // r2: receiver
5046 // r3: argc
5047 // r4: argv
5048 if (is_construct) {
5049 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
5050 __ mov(ip, Operand(construct_entry));
5051 } else {
5052 ExternalReference entry(Builtins::JSEntryTrampoline);
5053 __ mov(ip, Operand(entry));
5054 }
5055 __ ldr(ip, MemOperand(ip)); // deref address
5056
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005057 // Branch and link to JSEntryTrampoline. We don't use the double underscore
5058 // macro for the add instruction because we don't want the coverage tool
5059 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005060 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005061 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005062
5063 // Unlink this frame from the handler chain. When reading the
5064 // address of the next handler, there is no need to use the address
5065 // displacement since the current stack pointer (sp) points directly
5066 // to the stack handler.
5067 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
5068 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
5069 __ str(r3, MemOperand(ip));
5070 // No need to restore registers
5071 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
5072
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005073
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005074 __ bind(&exit); // r0 holds result
5075 // Restore the top frame descriptors from the stack.
5076 __ pop(r3);
5077 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
5078 __ str(r3, MemOperand(ip));
5079
5080 // Reset the stack to the callee saved registers.
5081 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
5082
5083 // Restore callee-saved registers and return.
5084#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005085 if (FLAG_debug_code) {
5086 __ mov(lr, Operand(pc));
5087 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005088#endif
5089 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
5090}
5091
5092
ager@chromium.org7c537e22008-10-16 08:43:32 +00005093void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005094 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005095 Label adaptor;
5096 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5097 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5098 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
ager@chromium.org7c537e22008-10-16 08:43:32 +00005099 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005100
ager@chromium.org7c537e22008-10-16 08:43:32 +00005101 // Nothing to do: The formal number of parameters has already been
5102 // passed in register r0 by calling function. Just return it.
ager@chromium.org9085a012009-05-11 19:22:57 +00005103 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005104
ager@chromium.org7c537e22008-10-16 08:43:32 +00005105 // Arguments adaptor case: Read the arguments length from the
5106 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005107 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005108 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org9085a012009-05-11 19:22:57 +00005109 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005110}
5111
5112
ager@chromium.org7c537e22008-10-16 08:43:32 +00005113void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
5114 // The displacement is the offset of the last parameter (if any)
5115 // relative to the frame pointer.
5116 static const int kDisplacement =
5117 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005118
ager@chromium.org7c537e22008-10-16 08:43:32 +00005119 // Check that the key is a smi.
5120 Label slow;
5121 __ tst(r1, Operand(kSmiTagMask));
5122 __ b(ne, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005123
ager@chromium.org7c537e22008-10-16 08:43:32 +00005124 // Check if the calling frame is an arguments adaptor frame.
5125 Label adaptor;
5126 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5127 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5128 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
5129 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005130
ager@chromium.org7c537e22008-10-16 08:43:32 +00005131 // Check index against formal parameters count limit passed in
5132 // through register eax. Use unsigned comparison to get negative
5133 // check for free.
5134 __ cmp(r1, r0);
5135 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005136
ager@chromium.org7c537e22008-10-16 08:43:32 +00005137 // Read the argument from the stack and return it.
5138 __ sub(r3, r0, r1);
5139 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
5140 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00005141 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005142
5143 // Arguments adaptor case: Check index against actual arguments
5144 // limit found in the arguments adaptor frame. Use unsigned
5145 // comparison to get negative check for free.
5146 __ bind(&adaptor);
5147 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
5148 __ cmp(r1, r0);
5149 __ b(cs, &slow);
5150
5151 // Read the argument from the adaptor frame and return it.
5152 __ sub(r3, r0, r1);
5153 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
5154 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00005155 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005156
5157 // Slow-case: Handle non-smi or out-of-bounds access to arguments
5158 // by calling the runtime system.
5159 __ bind(&slow);
5160 __ push(r1);
5161 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
5162}
5163
5164
5165void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
5166 // Check if the calling frame is an arguments adaptor frame.
5167 Label runtime;
5168 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5169 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5170 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
5171 __ b(ne, &runtime);
5172
5173 // Patch the arguments.length and the parameters pointer.
5174 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
5175 __ str(r0, MemOperand(sp, 0 * kPointerSize));
5176 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
5177 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
5178 __ str(r3, MemOperand(sp, 1 * kPointerSize));
5179
5180 // Do the runtime call to allocate the arguments object.
5181 __ bind(&runtime);
5182 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005183}
5184
5185
5186void CallFunctionStub::Generate(MacroAssembler* masm) {
5187 Label slow;
5188 // Get the function to call from the stack.
5189 // function, receiver [, arguments]
5190 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
5191
5192 // Check that the function is really a JavaScript function.
5193 // r1: pushed function (to be verified)
5194 __ tst(r1, Operand(kSmiTagMask));
5195 __ b(eq, &slow);
5196 // Get the map of the function object.
5197 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
5198 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
5199 __ cmp(r2, Operand(JS_FUNCTION_TYPE));
5200 __ b(ne, &slow);
5201
5202 // Fast-case: Invoke the function now.
5203 // r1: pushed function
5204 ParameterCount actual(argc_);
5205 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
5206
5207 // Slow-case: Non-function called.
5208 __ bind(&slow);
5209 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005210 __ mov(r2, Operand(0));
5211 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
5212 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
5213 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005214}
5215
5216
5217#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005218
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005219} } // namespace v8::internal