blob: 9337454bb8a4a3959cb64da79fadd2622c79578e [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
39namespace v8 { namespace internal {
40
ager@chromium.org65dad4b2009-04-23 08:48:43 +000041#define __ ACCESS_MASM(masm_)
42
ager@chromium.org3bf7b912008-11-17 09:09:45 +000043
44// -------------------------------------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000045// CodeGenState implementation.
46
ager@chromium.org7c537e22008-10-16 08:43:32 +000047CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000048 : owner_(owner),
ager@chromium.org7c537e22008-10-16 08:43:32 +000049 typeof_state_(NOT_INSIDE_TYPEOF),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000050 true_target_(NULL),
51 false_target_(NULL),
52 previous_(NULL) {
53 owner_->set_state(this);
54}
55
56
ager@chromium.org7c537e22008-10-16 08:43:32 +000057CodeGenState::CodeGenState(CodeGenerator* owner,
58 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000059 JumpTarget* true_target,
60 JumpTarget* false_target)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000061 : owner_(owner),
ager@chromium.org7c537e22008-10-16 08:43:32 +000062 typeof_state_(typeof_state),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000063 true_target_(true_target),
64 false_target_(false_target),
65 previous_(owner->state()) {
66 owner_->set_state(this);
67}
68
69
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000070CodeGenState::~CodeGenState() {
71 ASSERT(owner_->state() == this);
72 owner_->set_state(previous_);
73}
74
75
ager@chromium.org3bf7b912008-11-17 09:09:45 +000076// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +000077// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078
ager@chromium.org7c537e22008-10-16 08:43:32 +000079CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script,
80 bool is_eval)
81 : is_eval_(is_eval),
82 script_(script),
83 deferred_(8),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000084 masm_(new MacroAssembler(NULL, buffer_size)),
85 scope_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +000086 frame_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000087 allocator_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000088 cc_reg_(al),
89 state_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000090 function_return_is_shadowed_(false),
91 in_spilled_code_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000092}
93
94
95// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000096// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000098// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099// cp: callee's context
100
ager@chromium.org7c537e22008-10-16 08:43:32 +0000101void CodeGenerator::GenCode(FunctionLiteral* fun) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000102 ZoneList<Statement*>* body = fun->body();
103
104 // Initialize state.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000105 ASSERT(scope_ == NULL);
106 scope_ = fun->scope();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000107 ASSERT(allocator_ == NULL);
108 RegisterAllocator register_allocator(this);
109 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000110 ASSERT(frame_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000111 frame_ = new VirtualFrame(this);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000112 cc_reg_ = al;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000113 set_in_spilled_code(false);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000114 {
115 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000116
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000117 // Entry:
118 // Stack: receiver, arguments
119 // lr: return address
120 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000122 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000124 allocator_->Initialize();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000125 frame_->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000126 // tos: code slot
127#ifdef DEBUG
128 if (strlen(FLAG_stop_at) > 0 &&
129 fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000130 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000131 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000132 }
133#endif
134
135 // Allocate space for locals and initialize them.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000136 frame_->AllocateStackSlots(scope_->num_stack_slots());
137 // Initialize the function return target after the locals are set
138 // up, because it needs the expected frame height from the frame.
139 function_return_.Initialize(this, JumpTarget::BIDIRECTIONAL);
140 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000142 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000143 if (scope_->num_heap_slots() > 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144 // Allocate local context.
145 // Get outer context and create a new context based on it.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000146 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000147 frame_->EmitPush(r0);
148 frame_->CallRuntime(Runtime::kNewContext, 1); // r0 holds the result
kasper.lund7276f142008-07-30 08:49:36 +0000149
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000150#ifdef DEBUG
151 JumpTarget verified_true(this);
152 __ cmp(r0, Operand(cp));
153 verified_true.Branch(eq);
154 __ stop("NewContext: r0 is expected to be the same as cp");
155 verified_true.Bind();
156#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000157 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000158 __ str(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159 }
160
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000161 // TODO(1241774): Improve this code:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162 // 1) only needed if we have a context
163 // 2) no need to recompute context ptr every single time
164 // 3) don't copy parameter operand code from SlotOperand!
165 {
166 Comment cmnt2(masm_, "[ copy context parameters into .context");
167
168 // Note that iteration order is relevant here! If we have the same
169 // parameter twice (e.g., function (x, y, x)), and that parameter
170 // needs to be copied into the context, it must be the last argument
171 // passed to the parameter that needs to be copied. This is a rare
172 // case so we don't check for it, instead we rely on the copying
173 // order: such a parameter is copied repeatedly into the same
174 // context location and thus the last value is what is seen inside
175 // the function.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000176 for (int i = 0; i < scope_->num_parameters(); i++) {
177 Variable* par = scope_->parameter(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000178 Slot* slot = par->slot();
179 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000180 ASSERT(!scope_->is_global_scope()); // no parameters in global scope
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000181 __ ldr(r1, frame_->ParameterAt(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182 // Loads r2 with context; used below in RecordWrite.
183 __ str(r1, SlotOperand(slot, r2));
184 // Load the offset into r3.
185 int slot_offset =
186 FixedArray::kHeaderSize + slot->index() * kPointerSize;
187 __ mov(r3, Operand(slot_offset));
188 __ RecordWrite(r2, r3, r1);
189 }
190 }
191 }
192
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000193 // Store the arguments object. This must happen after context
194 // initialization because the arguments object may be stored in the
195 // context.
196 if (scope_->arguments() != NULL) {
197 ASSERT(scope_->arguments_shadow() != NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000198 Comment cmnt(masm_, "[ allocate arguments object");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000199 { Reference shadow_ref(this, scope_->arguments_shadow());
200 { Reference arguments_ref(this, scope_->arguments());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000201 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000202 __ ldr(r2, frame_->Function());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000203 // The receiver is below the arguments, the return address,
204 // and the frame pointer on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000205 const int kReceiverDisplacement = 2 + scope_->num_parameters();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000206 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000207 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000208 frame_->Adjust(3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000209 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000210 frame_->CallStub(&stub, 3);
211 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000212 arguments_ref.SetValue(NOT_CONST_INIT);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000213 }
ager@chromium.org7c537e22008-10-16 08:43:32 +0000214 shadow_ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000216 frame_->Drop(); // Value is no longer needed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217 }
218
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000219 // Generate code to 'execute' declarations and initialize functions
220 // (source elements). In case of an illegal redeclaration we need to
221 // handle that instead of processing the declarations.
222 if (scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000224 scope_->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225 } else {
226 Comment cmnt(masm_, "[ declarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000227 ProcessDeclarations(scope_->declarations());
228 // Bail out if a stack-overflow exception occurred when processing
229 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000230 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000231 }
232
mads.s.ager31e71382008-08-13 09:32:07 +0000233 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000234 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000235 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000236 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000237 CheckStack();
238
239 // Compile the body of the function in a vanilla state. Don't
240 // bother compiling all the code if the scope has an illegal
241 // redeclaration.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000242 if (!scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000243 Comment cmnt(masm_, "[ function body");
244#ifdef DEBUG
245 bool is_builtin = Bootstrapper::IsActive();
246 bool should_trace =
247 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000248 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000249 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000250 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000251 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000252#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000253 VisitStatementsAndSpill(body);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000254 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000255 }
256
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000257 // Generate the return sequence if necessary.
258 if (frame_ != NULL || function_return_.is_linked()) {
259 // exit
260 // r0: result
261 // sp: stack pointer
262 // fp: frame pointer
263 // pp: parameter pointer
264 // cp: callee's context
265 __ mov(r0, Operand(Factory::undefined_value()));
mads.s.ager31e71382008-08-13 09:32:07 +0000266
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000267 function_return_.Bind();
268 if (FLAG_trace) {
269 // Push the return value on the stack as the parameter.
270 // Runtime::TraceExit returns the parameter as it is.
271 frame_->EmitPush(r0);
272 frame_->CallRuntime(Runtime::kTraceExit, 1);
273 }
274
275 // Tear down the frame which will restore the caller's frame pointer and
276 // the link register.
277 frame_->Exit();
278
279 __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize));
280 __ mov(pc, lr);
mads.s.ager31e71382008-08-13 09:32:07 +0000281 }
282
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284 ASSERT(!has_cc());
285 ASSERT(state_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000286 ASSERT(!function_return_is_shadowed_);
287 function_return_.Unuse();
288 DeleteFrame();
289
290 // Process any deferred code using the register allocator.
291 if (HasStackOverflow()) {
292 ClearDeferred();
293 } else {
294 ProcessDeferred();
295 }
296
297 allocator_ = NULL;
298 scope_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299}
300
301
ager@chromium.org7c537e22008-10-16 08:43:32 +0000302MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
303 // Currently, this assertion will fail if we try to assign to
304 // a constant variable that is constant because it is read-only
305 // (such as the variable referring to a named function expression).
306 // We need to implement assignments to read-only variables.
307 // Ideally, we should do this during AST generation (by converting
308 // such assignments into expression statements); however, in general
309 // we may not be able to make the decision until past AST generation,
310 // that is when the entire program is known.
311 ASSERT(slot != NULL);
312 int index = slot->index();
313 switch (slot->type()) {
314 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000315 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000316
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000317 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000318 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000319
320 case Slot::CONTEXT: {
321 // Follow the context chain if necessary.
322 ASSERT(!tmp.is(cp)); // do not overwrite context register
323 Register context = cp;
324 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000325 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000326 // Load the closure.
327 // (All contexts, even 'with' contexts, have a closure,
328 // and it is the same for all contexts inside a function.
329 // There is no need to go to the function context first.)
330 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
331 // Load the function context (which is the incoming, outer context).
332 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
333 context = tmp;
334 }
335 // We may have a 'with' context now. Get the function context.
336 // (In fact this mov may never be the needed, since the scope analysis
337 // may not permit a direct context access in this case and thus we are
338 // always at a function context. However it is safe to dereference be-
339 // cause the function context of a function context is itself. Before
340 // deleting this mov we should try to create a counter-example first,
341 // though...)
342 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
343 return ContextOperand(tmp, index);
344 }
345
346 default:
347 UNREACHABLE();
348 return MemOperand(r0, 0);
349 }
350}
351
352
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000353MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
354 Slot* slot,
355 Register tmp,
356 Register tmp2,
357 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000358 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000359 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000360
ager@chromium.org381abbb2009-02-25 13:23:22 +0000361 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
362 if (s->num_heap_slots() > 0) {
363 if (s->calls_eval()) {
364 // Check that extension is NULL.
365 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
366 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000367 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000368 }
369 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
370 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
371 context = tmp;
372 }
373 }
374 // Check that last extension is NULL.
375 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
376 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000377 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000378 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000379 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000380}
381
382
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000383void CodeGenerator::LoadConditionAndSpill(Expression* expression,
384 TypeofState typeof_state,
385 JumpTarget* true_target,
386 JumpTarget* false_target,
387 bool force_control) {
388 ASSERT(in_spilled_code());
389 set_in_spilled_code(false);
390 LoadCondition(expression, typeof_state, true_target, false_target,
391 force_control);
392 if (frame_ != NULL) {
393 frame_->SpillAll();
394 }
395 set_in_spilled_code(true);
396}
397
398
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000399// Loads a value on TOS. If it is a boolean value, the result may have been
400// (partially) translated into branches, or it may have set the condition
401// code register. If force_cc is set, the value is forced to set the
402// condition code register and no value is pushed. If the condition code
403// register was set, has_cc() is true and cc_reg_ contains the condition to
404// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000405void CodeGenerator::LoadCondition(Expression* x,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000406 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000407 JumpTarget* true_target,
408 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000409 bool force_cc) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000410 ASSERT(!in_spilled_code());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000411 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000412 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000413
ager@chromium.org7c537e22008-10-16 08:43:32 +0000414 { CodeGenState new_state(this, typeof_state, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000415 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000416
417 // If we hit a stack overflow, we may not have actually visited
418 // the expression. In that case, we ensure that we have a
419 // valid-looking frame state because we will continue to generate
420 // code as we unwind the C++ stack.
421 //
422 // It's possible to have both a stack overflow and a valid frame
423 // state (eg, a subexpression overflowed, visiting it returned
424 // with a dummied frame state, and visiting this expression
425 // returned with a normal-looking state).
426 if (HasStackOverflow() &&
427 has_valid_frame() &&
428 !has_cc() &&
429 frame_->height() == original_height) {
430 true_target->Jump();
431 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000432 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000433 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000434 // Convert the TOS value to a boolean in the condition code register.
435 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000436 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000437 ASSERT(!force_cc || !has_valid_frame() || has_cc());
438 ASSERT(!has_valid_frame() ||
439 (has_cc() && frame_->height() == original_height) ||
440 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000441}
442
443
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000444void CodeGenerator::LoadAndSpill(Expression* expression,
445 TypeofState typeof_state) {
446 ASSERT(in_spilled_code());
447 set_in_spilled_code(false);
448 Load(expression, typeof_state);
449 frame_->SpillAll();
450 set_in_spilled_code(true);
451}
452
453
ager@chromium.org7c537e22008-10-16 08:43:32 +0000454void CodeGenerator::Load(Expression* x, TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000455#ifdef DEBUG
456 int original_height = frame_->height();
457#endif
458 ASSERT(!in_spilled_code());
459 JumpTarget true_target(this);
460 JumpTarget false_target(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000461 LoadCondition(x, typeof_state, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000462
463 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000464 // Convert cc_reg_ into a boolean value.
465 JumpTarget loaded(this);
466 JumpTarget materialize_true(this);
467 materialize_true.Branch(cc_reg_);
mads.s.ager31e71382008-08-13 09:32:07 +0000468 __ mov(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000469 frame_->EmitPush(r0);
470 loaded.Jump();
471 materialize_true.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000472 __ mov(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000473 frame_->EmitPush(r0);
474 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 cc_reg_ = al;
476 }
477
478 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000479 // We have at least one condition value that has been "translated"
480 // into a branch, thus it needs to be loaded explicitly.
481 JumpTarget loaded(this);
482 if (frame_ != NULL) {
483 loaded.Jump(); // Don't lose the current TOS.
484 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000485 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000486 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000487 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000488 true_target.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000489 __ mov(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000490 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000491 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000492 // If both "true" and "false" need to be loaded jump across the code for
493 // "false".
494 if (both) {
495 loaded.Jump();
496 }
497 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000499 false_target.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000500 __ mov(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000501 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000503 // A value is loaded on all paths reaching this point.
504 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000505 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000506 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000507 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000508 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000509}
510
511
ager@chromium.org7c537e22008-10-16 08:43:32 +0000512void CodeGenerator::LoadGlobal() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000513 VirtualFrame::SpilledScope spilled_scope(this);
mads.s.ager31e71382008-08-13 09:32:07 +0000514 __ ldr(r0, GlobalObject());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000515 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516}
517
518
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000519void CodeGenerator::LoadGlobalReceiver(Register scratch) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000520 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000521 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
522 __ ldr(scratch,
523 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000524 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000525}
526
527
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000528// TODO(1241834): Get rid of this function in favor of just using Load, now
ager@chromium.org7c537e22008-10-16 08:43:32 +0000529// that we have the INSIDE_TYPEOF typeof state. => Need to handle global
530// variables w/o reference errors elsewhere.
531void CodeGenerator::LoadTypeofExpression(Expression* x) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000532 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000533 Variable* variable = x->AsVariableProxy()->AsVariable();
534 if (variable != NULL && !variable->is_this() && variable->is_global()) {
535 // NOTE: This is somewhat nasty. We force the compiler to load
536 // the variable as if through '<global>.<variable>' to make sure we
537 // do not get reference errors.
538 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
539 Literal key(variable->name());
540 // TODO(1241834): Fetch the position from the variable instead of using
541 // no position.
ager@chromium.org236ad962008-09-25 09:45:57 +0000542 Property property(&global, &key, RelocInfo::kNoPosition);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000543 LoadAndSpill(&property);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000544 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000545 LoadAndSpill(x, INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000546 }
547}
548
549
ager@chromium.org7c537e22008-10-16 08:43:32 +0000550Reference::Reference(CodeGenerator* cgen, Expression* expression)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000551 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
552 cgen->LoadReference(this);
553}
554
555
556Reference::~Reference() {
557 cgen_->UnloadReference(this);
558}
559
560
ager@chromium.org7c537e22008-10-16 08:43:32 +0000561void CodeGenerator::LoadReference(Reference* ref) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000562 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000563 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564 Expression* e = ref->expression();
565 Property* property = e->AsProperty();
566 Variable* var = e->AsVariableProxy()->AsVariable();
567
568 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000569 // The expression is either a property or a variable proxy that rewrites
570 // to a property.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000571 LoadAndSpill(property->obj());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000572 // We use a named reference if the key is a literal symbol, unless it is
573 // a string that can be legally parsed as an integer. This is because
574 // otherwise we will not get into the slow case code that handles [] on
575 // String objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576 Literal* literal = property->key()->AsLiteral();
577 uint32_t dummy;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000578 if (literal != NULL &&
579 literal->handle()->IsSymbol() &&
580 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581 ref->set_type(Reference::NAMED);
582 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000583 LoadAndSpill(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584 ref->set_type(Reference::KEYED);
585 }
586 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000587 // The expression is a variable proxy that does not rewrite to a
588 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000589 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000590 LoadGlobal();
591 ref->set_type(Reference::NAMED);
592 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000593 ASSERT(var->slot() != NULL);
594 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000595 }
596 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000597 // Anything else is a runtime error.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000598 LoadAndSpill(e);
599 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600 }
601}
602
603
ager@chromium.org7c537e22008-10-16 08:43:32 +0000604void CodeGenerator::UnloadReference(Reference* ref) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000605 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000606 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000607 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000609 if (size > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000610 frame_->EmitPop(r0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000611 frame_->Drop(size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000612 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000613 }
614}
615
616
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
618// register to a boolean in the condition code register. The code
619// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000620void CodeGenerator::ToBoolean(JumpTarget* true_target,
621 JumpTarget* false_target) {
622 VirtualFrame::SpilledScope spilled_scope(this);
mads.s.ager31e71382008-08-13 09:32:07 +0000623 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000624 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000625 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000626
627 // Fast case checks
628
mads.s.ager31e71382008-08-13 09:32:07 +0000629 // Check if the value is 'false'.
630 __ cmp(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000631 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000632
mads.s.ager31e71382008-08-13 09:32:07 +0000633 // Check if the value is 'true'.
634 __ cmp(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000635 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000636
mads.s.ager31e71382008-08-13 09:32:07 +0000637 // Check if the value is 'undefined'.
638 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000639 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000640
mads.s.ager31e71382008-08-13 09:32:07 +0000641 // Check if the value is a smi.
642 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000643 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000644 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000645 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000646
647 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000648 frame_->EmitPush(r0);
649 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000650 // Convert the result (r0) to a condition code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000651 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652
653 cc_reg_ = ne;
654}
655
656
kasper.lund7276f142008-07-30 08:49:36 +0000657class GenericBinaryOpStub : public CodeStub {
658 public:
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000659 GenericBinaryOpStub(Token::Value op,
660 OverwriteMode mode)
661 : op_(op), mode_(mode) { }
kasper.lund7276f142008-07-30 08:49:36 +0000662
663 private:
664 Token::Value op_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000665 OverwriteMode mode_;
666
667 // Minor key encoding in 16 bits.
668 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
669 class OpBits: public BitField<Token::Value, 2, 14> {};
kasper.lund7276f142008-07-30 08:49:36 +0000670
671 Major MajorKey() { return GenericBinaryOp; }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000672 int MinorKey() {
673 // Encode the parameters in a unique 16 bit value.
674 return OpBits::encode(op_)
675 | ModeBits::encode(mode_);
676 }
677
kasper.lund7276f142008-07-30 08:49:36 +0000678 void Generate(MacroAssembler* masm);
679
680 const char* GetName() {
681 switch (op_) {
682 case Token::ADD: return "GenericBinaryOpStub_ADD";
683 case Token::SUB: return "GenericBinaryOpStub_SUB";
684 case Token::MUL: return "GenericBinaryOpStub_MUL";
685 case Token::DIV: return "GenericBinaryOpStub_DIV";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000686 case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR";
687 case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND";
688 case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR";
689 case Token::SAR: return "GenericBinaryOpStub_SAR";
690 case Token::SHL: return "GenericBinaryOpStub_SHL";
691 case Token::SHR: return "GenericBinaryOpStub_SHR";
kasper.lund7276f142008-07-30 08:49:36 +0000692 default: return "GenericBinaryOpStub";
693 }
694 }
695
696#ifdef DEBUG
697 void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); }
698#endif
699};
700
701
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000702void CodeGenerator::GenericBinaryOperation(Token::Value op,
703 OverwriteMode overwrite_mode) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000704 VirtualFrame::SpilledScope spilled_scope(this);
mads.s.ager31e71382008-08-13 09:32:07 +0000705 // sp[0] : y
706 // sp[1] : x
707 // result : r0
708
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000709 // Stub is entered with a call: 'return address' is in lr.
710 switch (op) {
711 case Token::ADD: // fall through.
712 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000713 case Token::MUL:
714 case Token::BIT_OR:
715 case Token::BIT_AND:
716 case Token::BIT_XOR:
717 case Token::SHL:
718 case Token::SHR:
719 case Token::SAR: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000720 frame_->EmitPop(r0); // r0 : y
721 frame_->EmitPop(r1); // r1 : x
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000722 GenericBinaryOpStub stub(op, overwrite_mode);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000723 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000724 break;
725 }
726
727 case Token::DIV: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000728 Result arg_count = allocator_->Allocate(r0);
729 ASSERT(arg_count.is_valid());
730 __ mov(arg_count.reg(), Operand(1));
731 frame_->InvokeBuiltin(Builtins::DIV, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732 break;
733 }
734
735 case Token::MOD: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000736 Result arg_count = allocator_->Allocate(r0);
737 ASSERT(arg_count.is_valid());
738 __ mov(arg_count.reg(), Operand(1));
739 frame_->InvokeBuiltin(Builtins::MOD, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000740 break;
741 }
742
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000743 case Token::COMMA:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000744 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000745 // simply discard left value
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000746 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747 break;
748
749 default:
750 // Other cases should have been handled before this point.
751 UNREACHABLE();
752 break;
753 }
754}
755
756
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000757class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000758 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000759 DeferredInlineSmiOperation(CodeGenerator* generator,
760 Token::Value op,
761 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000762 bool reversed,
763 OverwriteMode overwrite_mode)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000764 : DeferredCode(generator),
765 op_(op),
766 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000767 reversed_(reversed),
768 overwrite_mode_(overwrite_mode) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000769 set_comment("[ DeferredInlinedSmiOperation");
770 }
771
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000772 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000773
774 private:
775 Token::Value op_;
776 int value_;
777 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000778 OverwriteMode overwrite_mode_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000779};
780
781
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000782void DeferredInlineSmiOperation::Generate() {
783 enter()->Bind();
784 VirtualFrame::SpilledScope spilled_scope(generator());
785
786 switch (op_) {
787 case Token::ADD: {
788 if (reversed_) {
789 // revert optimistic add
790 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
791 __ mov(r1, Operand(Smi::FromInt(value_)));
792 } else {
793 // revert optimistic add
794 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
795 __ mov(r0, Operand(Smi::FromInt(value_)));
796 }
797 break;
798 }
799
800 case Token::SUB: {
801 if (reversed_) {
802 // revert optimistic sub
803 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
804 __ mov(r1, Operand(Smi::FromInt(value_)));
805 } else {
806 __ add(r1, r0, Operand(Smi::FromInt(value_)));
807 __ mov(r0, Operand(Smi::FromInt(value_)));
808 }
809 break;
810 }
811
812 case Token::BIT_OR:
813 case Token::BIT_XOR:
814 case Token::BIT_AND: {
815 if (reversed_) {
816 __ mov(r1, Operand(Smi::FromInt(value_)));
817 } else {
818 __ mov(r1, Operand(r0));
819 __ mov(r0, Operand(Smi::FromInt(value_)));
820 }
821 break;
822 }
823
824 case Token::SHL:
825 case Token::SHR:
826 case Token::SAR: {
827 if (!reversed_) {
828 __ mov(r1, Operand(r0));
829 __ mov(r0, Operand(Smi::FromInt(value_)));
830 } else {
831 UNREACHABLE(); // should have been handled in SmiOperation
832 }
833 break;
834 }
835
836 default:
837 // other cases should have been handled before this point.
838 UNREACHABLE();
839 break;
840 }
841
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000842 GenericBinaryOpStub igostub(op_, overwrite_mode_);
ager@chromium.org41826e72009-03-30 13:30:57 +0000843 Result arg0 = generator()->allocator()->Allocate(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000844 ASSERT(arg0.is_valid());
ager@chromium.org41826e72009-03-30 13:30:57 +0000845 Result arg1 = generator()->allocator()->Allocate(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000846 ASSERT(arg1.is_valid());
ager@chromium.org41826e72009-03-30 13:30:57 +0000847 generator()->frame()->CallStub(&igostub, &arg0, &arg1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000848 exit_.Jump();
849}
850
851
ager@chromium.org7c537e22008-10-16 08:43:32 +0000852void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000853 Handle<Object> value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000854 bool reversed,
855 OverwriteMode mode) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000856 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857 // NOTE: This is an attempt to inline (a bit) more of the code for
858 // some possible smi operations (like + and -) when (at least) one
859 // of the operands is a literal smi. With this optimization, the
860 // performance of the system is increased by ~15%, and the generated
861 // code size is increased by ~1% (measured on a combination of
862 // different benchmarks).
863
mads.s.ager31e71382008-08-13 09:32:07 +0000864 // sp[0] : operand
865
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000866 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000868 JumpTarget exit(this);
869 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870
871 switch (op) {
872 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000873 DeferredCode* deferred =
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000874 new DeferredInlineSmiOperation(this, op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000875
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000876 __ add(r0, r0, Operand(value), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000877 deferred->enter()->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000878 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000879 deferred->enter()->Branch(ne);
880 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881 break;
882 }
883
884 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000885 DeferredCode* deferred =
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000886 new DeferredInlineSmiOperation(this, op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888 if (!reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000889 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000890 } else {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000891 __ rsb(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000893 deferred->enter()->Branch(vs);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000894 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000895 deferred->enter()->Branch(ne);
896 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000897 break;
898 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000900 case Token::BIT_OR:
901 case Token::BIT_XOR:
902 case Token::BIT_AND: {
903 DeferredCode* deferred =
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000904 new DeferredInlineSmiOperation(this, op, int_value, reversed, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000905 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000906 deferred->enter()->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000907 switch (op) {
908 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
909 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
910 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
911 default: UNREACHABLE();
912 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000913 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000914 break;
915 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000916
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000917 case Token::SHL:
918 case Token::SHR:
919 case Token::SAR: {
920 if (reversed) {
921 __ mov(ip, Operand(value));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000922 frame_->EmitPush(ip);
923 frame_->EmitPush(r0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000924 GenericBinaryOperation(op, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000925
926 } else {
927 int shift_value = int_value & 0x1f; // least significant 5 bits
928 DeferredCode* deferred =
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000929 new DeferredInlineSmiOperation(this, op, shift_value, false, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000930 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000931 deferred->enter()->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000932 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
933 switch (op) {
934 case Token::SHL: {
935 __ mov(r2, Operand(r2, LSL, shift_value));
936 // check that the *unsigned* result fits in a smi
937 __ add(r3, r2, Operand(0x40000000), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000938 deferred->enter()->Branch(mi);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000939 break;
940 }
941 case Token::SHR: {
942 // LSR by immediate 0 means shifting 32 bits.
943 if (shift_value != 0) {
944 __ mov(r2, Operand(r2, LSR, shift_value));
945 }
946 // check that the *unsigned* result fits in a smi
947 // neither of the two high-order bits can be set:
948 // - 0x80000000: high bit would be lost when smi tagging
949 // - 0x40000000: this number would convert to negative when
950 // smi tagging these two cases can only happen with shifts
951 // by 0 or 1 when handed a valid smi
952 __ and_(r3, r2, Operand(0xc0000000), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000953 deferred->enter()->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000954 break;
955 }
956 case Token::SAR: {
957 if (shift_value != 0) {
958 // ASR by immediate 0 means shifting 32 bits.
959 __ mov(r2, Operand(r2, ASR, shift_value));
960 }
961 break;
962 }
963 default: UNREACHABLE();
964 }
965 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000966 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000967 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968 break;
969 }
970
971 default:
972 if (!reversed) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000973 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +0000974 __ mov(r0, Operand(value));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000975 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000976 } else {
977 __ mov(ip, Operand(value));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000978 frame_->EmitPush(ip);
979 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000981 GenericBinaryOperation(op, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982 break;
983 }
984
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000985 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000986}
987
988
ager@chromium.org7c537e22008-10-16 08:43:32 +0000989void CodeGenerator::Comparison(Condition cc, bool strict) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000990 VirtualFrame::SpilledScope spilled_scope(this);
mads.s.ager31e71382008-08-13 09:32:07 +0000991 // sp[0] : y
992 // sp[1] : x
993 // result : cc register
994
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000995 // Strict only makes sense for equality comparisons.
996 ASSERT(!strict || cc == eq);
997
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000998 JumpTarget exit(this);
999 JumpTarget smi(this);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001000 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1001 if (cc == gt || cc == le) {
1002 cc = ReverseCondition(cc);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001003 frame_->EmitPop(r1);
1004 frame_->EmitPop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001005 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001006 frame_->EmitPop(r0);
1007 frame_->EmitPop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001008 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001009 __ orr(r2, r0, Operand(r1));
1010 __ tst(r2, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001011 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001012
1013 // Perform non-smi comparison by runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001014 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001015
1016 // Figure out which native to call and setup the arguments.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001017 Builtins::JavaScript native;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001018 int arg_count = 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001019 if (cc == eq) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001020 native = strict ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001021 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001022 native = Builtins::COMPARE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023 int ncr; // NaN compare result
1024 if (cc == lt || cc == le) {
1025 ncr = GREATER;
1026 } else {
1027 ASSERT(cc == gt || cc == ge); // remaining cases
1028 ncr = LESS;
1029 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001030 frame_->EmitPush(r0);
1031 arg_count++;
mads.s.ager31e71382008-08-13 09:32:07 +00001032 __ mov(r0, Operand(Smi::FromInt(ncr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001033 }
1034
1035 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1036 // tagged as a small integer.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001037 frame_->EmitPush(r0);
1038 Result arg_count_register = allocator_->Allocate(r0);
1039 ASSERT(arg_count_register.is_valid());
1040 __ mov(arg_count_register.reg(), Operand(arg_count));
1041 Result result = frame_->InvokeBuiltin(native,
1042 CALL_JS,
1043 &arg_count_register,
1044 arg_count + 1);
1045 __ cmp(result.reg(), Operand(0));
1046 result.Unuse();
1047 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001048
1049 // test smi equality by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001050 smi.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051 __ cmp(r1, Operand(r0));
1052
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001053 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001054 cc_reg_ = cc;
1055}
1056
1057
kasper.lund7276f142008-07-30 08:49:36 +00001058class CallFunctionStub: public CodeStub {
1059 public:
1060 explicit CallFunctionStub(int argc) : argc_(argc) {}
1061
1062 void Generate(MacroAssembler* masm);
1063
1064 private:
1065 int argc_;
1066
kasper.lund7276f142008-07-30 08:49:36 +00001067#if defined(DEBUG)
1068 void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); }
1069#endif // defined(DEBUG)
1070
1071 Major MajorKey() { return CallFunction; }
1072 int MinorKey() { return argc_; }
1073};
1074
1075
mads.s.ager31e71382008-08-13 09:32:07 +00001076// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001077void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001078 int position) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001079 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001080 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001081 int arg_count = args->length();
1082 for (int i = 0; i < arg_count; i++) {
1083 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001084 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085
kasper.lund7276f142008-07-30 08:49:36 +00001086 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001087 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001088
kasper.lund7276f142008-07-30 08:49:36 +00001089 // Use the shared code stub to call the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001090 CallFunctionStub call_function(arg_count);
1091 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001092
1093 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001094 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001095 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001096}
1097
1098
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001099void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
1100 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101 ASSERT(has_cc());
1102 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001103 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104 cc_reg_ = al;
1105}
1106
1107
ager@chromium.org7c537e22008-10-16 08:43:32 +00001108void CodeGenerator::CheckStack() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001109 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001110 if (FLAG_check_stack) {
1111 Comment cmnt(masm_, "[ check stack");
1112 StackCheckStub stub;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001113 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001114 }
1115}
1116
1117
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001118void CodeGenerator::VisitAndSpill(Statement* statement) {
1119 ASSERT(in_spilled_code());
1120 set_in_spilled_code(false);
1121 Visit(statement);
1122 if (frame_ != NULL) {
1123 frame_->SpillAll();
1124 }
1125 set_in_spilled_code(true);
1126}
1127
1128
1129void CodeGenerator::VisitStatementsAndSpill(ZoneList<Statement*>* statements) {
1130 ASSERT(in_spilled_code());
1131 set_in_spilled_code(false);
1132 VisitStatements(statements);
1133 if (frame_ != NULL) {
1134 frame_->SpillAll();
1135 }
1136 set_in_spilled_code(true);
1137}
1138
1139
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001140void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1141#ifdef DEBUG
1142 int original_height = frame_->height();
1143#endif
1144 VirtualFrame::SpilledScope spilled_scope(this);
1145 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1146 VisitAndSpill(statements->at(i));
1147 }
1148 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1149}
1150
1151
ager@chromium.org7c537e22008-10-16 08:43:32 +00001152void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001153#ifdef DEBUG
1154 int original_height = frame_->height();
1155#endif
1156 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001157 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001158 CodeForStatementPosition(node);
1159 node->break_target()->Initialize(this);
1160 VisitStatementsAndSpill(node->statements());
1161 if (node->break_target()->is_linked()) {
1162 node->break_target()->Bind();
1163 }
1164 node->break_target()->Unuse();
1165 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001166}
1167
1168
ager@chromium.org7c537e22008-10-16 08:43:32 +00001169void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001170 VirtualFrame::SpilledScope spilled_scope(this);
mads.s.ager31e71382008-08-13 09:32:07 +00001171 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001172 frame_->EmitPush(r0);
1173 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001174 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001175 frame_->EmitPush(r0);
1176 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001177 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001178}
1179
1180
ager@chromium.org7c537e22008-10-16 08:43:32 +00001181void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001182#ifdef DEBUG
1183 int original_height = frame_->height();
1184#endif
1185 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186 Comment cmnt(masm_, "[ Declaration");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001187 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001188 Variable* var = node->proxy()->var();
1189 ASSERT(var != NULL); // must have been resolved
1190 Slot* slot = var->slot();
1191
1192 // If it was not possible to allocate the variable at compile time,
1193 // we need to "declare" it at runtime to make sure it actually
1194 // exists in the local context.
1195 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1196 // Variables with a "LOOKUP" slot were introduced as non-locals
1197 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001198 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001200 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001201 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001202 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001203 // Declaration nodes are always declared in only two modes.
1204 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1205 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001206 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001207 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208 // Push initial value, if any.
1209 // Note: For variables we must not push an initial value (such as
1210 // 'undefined') because we may have a (legal) redeclaration and we
1211 // must not destroy the current value.
1212 if (node->mode() == Variable::CONST) {
mads.s.ager31e71382008-08-13 09:32:07 +00001213 __ mov(r0, Operand(Factory::the_hole_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001214 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001215 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001216 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001217 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001218 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001219 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001220 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001221 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001222 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001223 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001224 return;
1225 }
1226
1227 ASSERT(!var->is_global());
1228
1229 // If we have a function or a constant, we need to initialize the variable.
1230 Expression* val = NULL;
1231 if (node->mode() == Variable::CONST) {
1232 val = new Literal(Factory::the_hole_value());
1233 } else {
1234 val = node->fun(); // NULL if we don't have a function
1235 }
1236
1237 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001238 {
1239 // Set initial value.
1240 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001241 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001242 target.SetValue(NOT_CONST_INIT);
1243 // The reference is removed from the stack (preserving TOS) when
1244 // it goes out of scope.
1245 }
1246 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001247 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001248 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001249 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001250}
1251
1252
ager@chromium.org7c537e22008-10-16 08:43:32 +00001253void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001254#ifdef DEBUG
1255 int original_height = frame_->height();
1256#endif
1257 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001258 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001259 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260 Expression* expression = node->expression();
1261 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001262 LoadAndSpill(expression);
1263 frame_->Drop();
1264 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001265}
1266
1267
ager@chromium.org7c537e22008-10-16 08:43:32 +00001268void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001269#ifdef DEBUG
1270 int original_height = frame_->height();
1271#endif
1272 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001274 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001276 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001277}
1278
1279
ager@chromium.org7c537e22008-10-16 08:43:32 +00001280void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001281#ifdef DEBUG
1282 int original_height = frame_->height();
1283#endif
1284 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001285 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001286 // Generate different code depending on which parts of the if statement
1287 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001288 bool has_then_stm = node->HasThenStatement();
1289 bool has_else_stm = node->HasElseStatement();
1290
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001291 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001293 JumpTarget exit(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001295 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001296 JumpTarget then(this);
1297 JumpTarget else_(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001298 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001299 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1300 &then, &else_, true);
1301 if (frame_ != NULL) {
1302 Branch(false, &else_);
1303 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001304 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001305 if (frame_ != NULL || then.is_linked()) {
1306 then.Bind();
1307 VisitAndSpill(node->then_statement());
1308 }
1309 if (frame_ != NULL) {
1310 exit.Jump();
1311 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001313 if (else_.is_linked()) {
1314 else_.Bind();
1315 VisitAndSpill(node->else_statement());
1316 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001317
1318 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001319 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001320 ASSERT(!has_else_stm);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001321 JumpTarget then(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001322 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001323 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1324 &then, &exit, true);
1325 if (frame_ != NULL) {
1326 Branch(false, &exit);
1327 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001329 if (frame_ != NULL || then.is_linked()) {
1330 then.Bind();
1331 VisitAndSpill(node->then_statement());
1332 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001333
1334 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001335 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336 ASSERT(!has_then_stm);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001337 JumpTarget else_(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338 // if (!cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001339 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1340 &exit, &else_, true);
1341 if (frame_ != NULL) {
1342 Branch(true, &exit);
1343 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001344 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001345 if (frame_ != NULL || else_.is_linked()) {
1346 else_.Bind();
1347 VisitAndSpill(node->else_statement());
1348 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349
1350 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001351 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352 ASSERT(!has_then_stm && !has_else_stm);
1353 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001354 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1355 &exit, &exit, false);
1356 if (frame_ != NULL) {
1357 if (has_cc()) {
1358 cc_reg_ = al;
1359 } else {
1360 frame_->Drop();
1361 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001362 }
1363 }
1364
1365 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001366 if (exit.is_linked()) {
1367 exit.Bind();
1368 }
1369 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370}
1371
1372
ager@chromium.org7c537e22008-10-16 08:43:32 +00001373void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001374 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001375 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001376 CodeForStatementPosition(node);
1377 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001378}
1379
1380
ager@chromium.org7c537e22008-10-16 08:43:32 +00001381void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001382 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001383 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001384 CodeForStatementPosition(node);
1385 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001386}
1387
1388
ager@chromium.org7c537e22008-10-16 08:43:32 +00001389void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001390 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001391 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001392
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001393 if (function_return_is_shadowed_) {
1394 CodeForStatementPosition(node);
1395 LoadAndSpill(node->expression());
1396 frame_->EmitPop(r0);
1397 function_return_.Jump();
1398 } else {
1399 // Load the returned value.
1400 CodeForStatementPosition(node);
1401 LoadAndSpill(node->expression());
1402
1403 // Pop the result from the frame and prepare the frame for
1404 // returning thus making it easier to merge.
1405 frame_->EmitPop(r0);
1406 frame_->PrepareForReturn();
1407
1408 function_return_.Jump();
1409 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001410}
1411
1412
ager@chromium.org7c537e22008-10-16 08:43:32 +00001413void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001414#ifdef DEBUG
1415 int original_height = frame_->height();
1416#endif
1417 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001418 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001419 CodeForStatementPosition(node);
1420 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001421 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001422 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001423 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001424 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001425 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001426#ifdef DEBUG
1427 JumpTarget verified_true(this);
1428 __ cmp(r0, Operand(cp));
1429 verified_true.Branch(eq);
1430 __ stop("PushContext: r0 is expected to be the same as cp");
1431 verified_true.Bind();
1432#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001434 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001435 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001436}
1437
1438
ager@chromium.org7c537e22008-10-16 08:43:32 +00001439void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001440#ifdef DEBUG
1441 int original_height = frame_->height();
1442#endif
1443 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001444 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001445 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001446 // Pop context.
1447 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1448 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001449 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001450 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001451}
1452
1453
ager@chromium.org7c537e22008-10-16 08:43:32 +00001454int CodeGenerator::FastCaseSwitchMaxOverheadFactor() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001455 return kFastSwitchMaxOverheadFactor;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001456}
1457
ager@chromium.org7c537e22008-10-16 08:43:32 +00001458int CodeGenerator::FastCaseSwitchMinCaseCount() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001459 return kFastSwitchMinCaseCount;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001460}
1461
1462
ager@chromium.org7c537e22008-10-16 08:43:32 +00001463void CodeGenerator::GenerateFastCaseSwitchJumpTable(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001464 SwitchStatement* node,
1465 int min_index,
1466 int range,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001467 Label* default_label,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001468 Vector<Label*> case_targets,
1469 Vector<Label> case_labels) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001470 VirtualFrame::SpilledScope spilled_scope(this);
1471 JumpTarget setup_default(this);
1472 JumpTarget is_smi(this);
1473
1474 // A non-null default label pointer indicates a default case among
1475 // the case labels. Otherwise we use the break target as a
1476 // "default" for failure to hit the jump table.
1477 JumpTarget* default_target =
1478 (default_label == NULL) ? node->break_target() : &setup_default;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001479
1480 ASSERT(kSmiTag == 0 && kSmiTagSize <= 2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001481 frame_->EmitPop(r0);
ager@chromium.org870a0b62008-11-04 11:43:05 +00001482
1483 // Test for a Smi value in a HeapNumber.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001484 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001485 is_smi.Branch(eq);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001486 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1487 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
ager@chromium.org870a0b62008-11-04 11:43:05 +00001488 __ cmp(r1, Operand(HEAP_NUMBER_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001489 default_target->Branch(ne);
1490 frame_->EmitPush(r0);
1491 frame_->CallRuntime(Runtime::kNumberToSmi, 1);
1492 is_smi.Bind();
ager@chromium.org870a0b62008-11-04 11:43:05 +00001493
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001494 if (min_index != 0) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001495 // Small positive numbers can be immediate operands.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001496 if (min_index < 0) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001497 // If min_index is Smi::kMinValue, -min_index is not a Smi.
1498 if (Smi::IsValid(-min_index)) {
1499 __ add(r0, r0, Operand(Smi::FromInt(-min_index)));
1500 } else {
1501 __ add(r0, r0, Operand(Smi::FromInt(-min_index - 1)));
1502 __ add(r0, r0, Operand(Smi::FromInt(1)));
1503 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001504 } else {
1505 __ sub(r0, r0, Operand(Smi::FromInt(min_index)));
1506 }
1507 }
1508 __ tst(r0, Operand(0x80000000 | kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001509 default_target->Branch(ne);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001510 __ cmp(r0, Operand(Smi::FromInt(range)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001511 default_target->Branch(ge);
1512 VirtualFrame* start_frame = new VirtualFrame(frame_);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001513 __ SmiJumpTable(r0, case_targets);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001514
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001515 GenerateFastCaseSwitchCases(node, case_labels, start_frame);
1516
1517 // If there was a default case among the case labels, we need to
1518 // emit code to jump to it from the default target used for failure
1519 // to hit the jump table.
1520 if (default_label != NULL) {
1521 if (has_valid_frame()) {
1522 node->break_target()->Jump();
1523 }
1524 setup_default.Bind();
1525 frame_->MergeTo(start_frame);
1526 __ b(default_label);
1527 DeleteFrame();
1528 }
1529 if (node->break_target()->is_linked()) {
1530 node->break_target()->Bind();
1531 }
1532
1533 delete start_frame;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001534}
1535
1536
ager@chromium.org7c537e22008-10-16 08:43:32 +00001537void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001538#ifdef DEBUG
1539 int original_height = frame_->height();
1540#endif
1541 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001542 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001543 CodeForStatementPosition(node);
1544 node->break_target()->Initialize(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001545
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001546 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001547 if (TryGenerateFastCaseSwitchStatement(node)) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001548 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1549 return;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001550 }
1551
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001552 JumpTarget next_test(this);
1553 JumpTarget fall_through(this);
1554 JumpTarget default_entry(this);
1555 JumpTarget default_exit(this, JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001556 ZoneList<CaseClause*>* cases = node->cases();
1557 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001558 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001559
1560 for (int i = 0; i < length; i++) {
1561 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001562 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001563 // Remember the default clause and compile it at the end.
1564 default_clause = clause;
1565 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001566 }
1567
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001568 Comment cmnt(masm_, "[ Case clause");
1569 // Compile the test.
1570 next_test.Bind();
1571 next_test.Unuse();
1572 // Duplicate TOS.
1573 __ ldr(r0, frame_->Top());
1574 frame_->EmitPush(r0);
1575 LoadAndSpill(clause->label());
1576 Comparison(eq, true);
1577 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001578
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001579 // Before entering the body from the test, remove the switch value from
1580 // the stack.
1581 frame_->Drop();
1582
1583 // Label the body so that fall through is enabled.
1584 if (i > 0 && cases->at(i - 1)->is_default()) {
1585 default_exit.Bind();
1586 } else {
1587 fall_through.Bind();
1588 fall_through.Unuse();
1589 }
1590 VisitStatementsAndSpill(clause->statements());
1591
1592 // If control flow can fall through from the body, jump to the next body
1593 // or the end of the statement.
1594 if (frame_ != NULL) {
1595 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1596 default_entry.Jump();
1597 } else {
1598 fall_through.Jump();
1599 }
1600 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601 }
1602
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001603 // The final "test" removes the switch value.
1604 next_test.Bind();
1605 frame_->Drop();
1606
1607 // If there is a default clause, compile it.
1608 if (default_clause != NULL) {
1609 Comment cmnt(masm_, "[ Default clause");
1610 default_entry.Bind();
1611 VisitStatementsAndSpill(default_clause->statements());
1612 // If control flow can fall out of the default and there is a case after
1613 // it, jup to that case's body.
1614 if (frame_ != NULL && default_exit.is_bound()) {
1615 default_exit.Jump();
1616 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001617 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001619 if (fall_through.is_linked()) {
1620 fall_through.Bind();
1621 }
1622
1623 if (node->break_target()->is_linked()) {
1624 node->break_target()->Bind();
1625 }
1626 node->break_target()->Unuse();
1627 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001628}
1629
1630
ager@chromium.org7c537e22008-10-16 08:43:32 +00001631void CodeGenerator::VisitLoopStatement(LoopStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001632#ifdef DEBUG
1633 int original_height = frame_->height();
1634#endif
1635 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636 Comment cmnt(masm_, "[ LoopStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001637 CodeForStatementPosition(node);
1638 node->break_target()->Initialize(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001639
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001640 // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a
1641 // known result for the test expression, with no side effects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001642 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW;
1643 if (node->cond() == NULL) {
1644 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1645 info = ALWAYS_TRUE;
1646 } else {
1647 Literal* lit = node->cond()->AsLiteral();
1648 if (lit != NULL) {
1649 if (lit->IsTrue()) {
1650 info = ALWAYS_TRUE;
1651 } else if (lit->IsFalse()) {
1652 info = ALWAYS_FALSE;
1653 }
1654 }
1655 }
1656
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001657 switch (node->type()) {
1658 case LoopStatement::DO_LOOP: {
1659 JumpTarget body(this, JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001660
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001661 // Label the top of the loop for the backward CFG edge. If the test
1662 // is always true we can use the continue target, and if the test is
1663 // always false there is no need.
1664 if (info == ALWAYS_TRUE) {
1665 node->continue_target()->Initialize(this, JumpTarget::BIDIRECTIONAL);
1666 node->continue_target()->Bind();
1667 } else if (info == ALWAYS_FALSE) {
1668 node->continue_target()->Initialize(this);
1669 } else {
1670 ASSERT(info == DONT_KNOW);
1671 node->continue_target()->Initialize(this);
1672 body.Bind();
1673 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001674
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001675 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001676 VisitAndSpill(node->body());
1677
1678 // Compile the test.
1679 if (info == ALWAYS_TRUE) {
1680 if (has_valid_frame()) {
1681 // If control can fall off the end of the body, jump back to the
1682 // top.
1683 node->continue_target()->Jump();
1684 }
1685 } else if (info == ALWAYS_FALSE) {
1686 // If we have a continue in the body, we only have to bind its jump
1687 // target.
1688 if (node->continue_target()->is_linked()) {
1689 node->continue_target()->Bind();
1690 }
1691 } else {
1692 ASSERT(info == DONT_KNOW);
1693 // We have to compile the test expression if it can be reached by
1694 // control flow falling out of the body or via continue.
1695 if (node->continue_target()->is_linked()) {
1696 node->continue_target()->Bind();
1697 }
1698 if (has_valid_frame()) {
1699 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1700 &body, node->break_target(), true);
1701 if (has_valid_frame()) {
1702 // A invalid frame here indicates that control did not
1703 // fall out of the test expression.
1704 Branch(true, &body);
1705 }
1706 }
1707 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001708 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001709 }
1710
1711 case LoopStatement::WHILE_LOOP: {
1712 // If the test is never true and has no side effects there is no need
1713 // to compile the test or body.
1714 if (info == ALWAYS_FALSE) break;
1715
1716 // Label the top of the loop with the continue target for the backward
1717 // CFG edge.
1718 node->continue_target()->Initialize(this, JumpTarget::BIDIRECTIONAL);
1719 node->continue_target()->Bind();
1720
1721 if (info == DONT_KNOW) {
1722 JumpTarget body(this);
1723 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1724 &body, node->break_target(), true);
1725 if (has_valid_frame()) {
1726 // A NULL frame indicates that control did not fall out of the
1727 // test expression.
1728 Branch(false, node->break_target());
1729 }
1730 if (has_valid_frame() || body.is_linked()) {
1731 body.Bind();
1732 }
1733 }
1734
1735 if (has_valid_frame()) {
1736 CheckStack(); // TODO(1222600): ignore if body contains calls.
1737 VisitAndSpill(node->body());
1738
1739 // If control flow can fall out of the body, jump back to the top.
1740 if (has_valid_frame()) {
1741 node->continue_target()->Jump();
1742 }
1743 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001744 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001745 }
1746
1747 case LoopStatement::FOR_LOOP: {
1748 JumpTarget loop(this, JumpTarget::BIDIRECTIONAL);
1749
1750 if (node->init() != NULL) {
1751 VisitAndSpill(node->init());
1752 }
1753
1754 // There is no need to compile the test or body.
1755 if (info == ALWAYS_FALSE) break;
1756
1757 // If there is no update statement, label the top of the loop with the
1758 // continue target, otherwise with the loop target.
1759 if (node->next() == NULL) {
1760 node->continue_target()->Initialize(this, JumpTarget::BIDIRECTIONAL);
1761 node->continue_target()->Bind();
1762 } else {
1763 node->continue_target()->Initialize(this);
1764 loop.Bind();
1765 }
1766
1767 // If the test is always true, there is no need to compile it.
1768 if (info == DONT_KNOW) {
1769 JumpTarget body(this);
1770 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1771 &body, node->break_target(), true);
1772 if (has_valid_frame()) {
1773 Branch(false, node->break_target());
1774 }
1775 if (has_valid_frame() || body.is_linked()) {
1776 body.Bind();
1777 }
1778 }
1779
1780 if (has_valid_frame()) {
1781 CheckStack(); // TODO(1222600): ignore if body contains calls.
1782 VisitAndSpill(node->body());
1783
1784 if (node->next() == NULL) {
1785 // If there is no update statement and control flow can fall out
1786 // of the loop, jump directly to the continue label.
1787 if (has_valid_frame()) {
1788 node->continue_target()->Jump();
1789 }
1790 } else {
1791 // If there is an update statement and control flow can reach it
1792 // via falling out of the body of the loop or continuing, we
1793 // compile the update statement.
1794 if (node->continue_target()->is_linked()) {
1795 node->continue_target()->Bind();
1796 }
1797 if (has_valid_frame()) {
1798 // Record source position of the statement as this code which is
1799 // after the code for the body actually belongs to the loop
1800 // statement and not the body.
1801 CodeForStatementPosition(node);
1802 VisitAndSpill(node->next());
1803 loop.Jump();
1804 }
1805 }
1806 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001807 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001808 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001809 }
1810
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001811 if (node->break_target()->is_linked()) {
1812 node->break_target()->Bind();
1813 }
1814 node->continue_target()->Unuse();
1815 node->break_target()->Unuse();
1816 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001817}
1818
1819
ager@chromium.org7c537e22008-10-16 08:43:32 +00001820void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001821#ifdef DEBUG
1822 int original_height = frame_->height();
1823#endif
1824 ASSERT(!in_spilled_code());
1825 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001826 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001827 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001828
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001829 JumpTarget primitive(this);
1830 JumpTarget jsobject(this);
1831 JumpTarget fixed_array(this);
1832 JumpTarget entry(this, JumpTarget::BIDIRECTIONAL);
1833 JumpTarget end_del_check(this);
1834 JumpTarget exit(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001835
1836 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001837 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001838
1839 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1840 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001841 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001842 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001843 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844 __ cmp(r0, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001845 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001846
1847 // Stack layout in body:
1848 // [iteration counter (Smi)]
1849 // [length of array]
1850 // [FixedArray]
1851 // [Map or 0]
1852 // [Object]
1853
1854 // Check if enumerable is already a JSObject
1855 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001856 primitive.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1858 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00001859 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001860 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001861
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001862 primitive.Bind();
1863 frame_->EmitPush(r0);
1864 Result arg_count = allocator_->Allocate(r0);
1865 ASSERT(arg_count.is_valid());
1866 __ mov(arg_count.reg(), Operand(0));
1867 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001869 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870 // Get the set of properties (as a FixedArray or Map).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001871 frame_->EmitPush(r0); // duplicate the object being enumerated
1872 frame_->EmitPush(r0);
1873 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001874
1875 // If we got a Map, we can do a fast modification check.
1876 // Otherwise, we got a FixedArray, and we have to do a slow check.
1877 __ mov(r2, Operand(r0));
1878 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
1879 __ cmp(r1, Operand(Factory::meta_map()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001880 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001881
1882 // Get enum cache
1883 __ mov(r1, Operand(r0));
1884 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1885 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1886 __ ldr(r2,
1887 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1888
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001889 frame_->EmitPush(r0); // map
1890 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001891 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001892 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001893 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001894 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001895 frame_->EmitPush(r0);
1896 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001897
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001898 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001899 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001900 frame_->EmitPush(r1); // insert 0 in place of Map
1901 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001902
1903 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001904 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001905 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001906 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001907 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001908 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001909
1910 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001911 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00001912 // sp[0] : index
1913 // sp[1] : array/enum cache length
1914 // sp[2] : array or enum cache
1915 // sp[3] : 0 or map
1916 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001917 // Grab the current frame's height for the break and continue
1918 // targets only after all the state is pushed on the frame.
1919 node->break_target()->Initialize(this);
1920 node->continue_target()->Initialize(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001921
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001922 __ ldr(r0, frame_->ElementAt(0)); // load the current count
1923 __ ldr(r1, frame_->ElementAt(1)); // load the length
1924 __ cmp(r0, Operand(r1)); // compare to the array length
1925 node->break_target()->Branch(hs);
1926
1927 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001928
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001929 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001930 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1932 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1933
1934 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001935 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936 // Check if this (still) matches the map of the enumerable.
1937 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001938 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001939 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1940 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001941 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001942
1943 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001944 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
1945 frame_->EmitPush(r0);
1946 frame_->EmitPush(r3); // push entry
1947 Result arg_count_register = allocator_->Allocate(r0);
1948 ASSERT(arg_count_register.is_valid());
1949 __ mov(arg_count_register.reg(), Operand(1));
1950 Result result = frame_->InvokeBuiltin(Builtins::FILTER_KEY,
1951 CALL_JS,
1952 &arg_count_register,
1953 2);
1954 __ mov(r3, Operand(result.reg()));
1955 result.Unuse();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001956
1957 // If the property has been removed while iterating, we just skip it.
1958 __ cmp(r3, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001959 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001960
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001961 end_del_check.Bind();
1962 // Store the entry in the 'each' expression and take another spin in the
1963 // loop. r3: i'th entry of the enum cache (or string there of)
1964 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001965 { Reference each(this, node->each());
1966 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001967 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001968 __ ldr(r0, frame_->ElementAt(each.size()));
1969 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001970 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001971 // If the reference was to a slot we rely on the convenient property
1972 // that it doesn't matter whether a value (eg, r3 pushed above) is
1973 // right on top of or right underneath a zero-sized reference.
1974 each.SetValue(NOT_CONST_INIT);
mads.s.ager31e71382008-08-13 09:32:07 +00001975 if (each.size() > 0) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00001976 // It's safe to pop the value lying on top of the reference before
1977 // unloading the reference itself (which preserves the top of stack,
1978 // ie, now the topmost value of the non-zero sized reference), since
1979 // we will discard the top of stack after unloading the reference
1980 // anyway.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001981 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001982 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001983 }
1984 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001985 // Discard the i'th entry pushed above or else the remainder of the
1986 // reference, whichever is currently on top of the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001987 frame_->Drop();
1988
1989 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001990 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001991 VisitAndSpill(node->body());
1992
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001993 // Next. Reestablish a spilled frame in case we are coming here via
1994 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001995 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001996 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001997 frame_->EmitPop(r0);
1998 __ add(r0, r0, Operand(Smi::FromInt(1)));
1999 frame_->EmitPush(r0);
2000 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002001
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002002 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2003 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002004 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002005 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002006
2007 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002008 exit.Bind();
2009 node->continue_target()->Unuse();
2010 node->break_target()->Unuse();
2011 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002012}
2013
2014
ager@chromium.org7c537e22008-10-16 08:43:32 +00002015void CodeGenerator::VisitTryCatch(TryCatch* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002016#ifdef DEBUG
2017 int original_height = frame_->height();
2018#endif
2019 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020 Comment cmnt(masm_, "[ TryCatch");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002021 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002022
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002023 JumpTarget try_block(this);
2024 JumpTarget exit(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002025
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002026 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002027 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002028 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002029
2030 // Store the caught exception in the catch variable.
2031 { Reference ref(this, node->catch_var());
ager@chromium.org7c537e22008-10-16 08:43:32 +00002032 ASSERT(ref.is_slot());
2033 // Here we make use of the convenient property that it doesn't matter
2034 // whether a value is immediately on top of or underneath a zero-sized
2035 // reference.
2036 ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002037 }
2038
2039 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002040 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002042 VisitStatementsAndSpill(node->catch_block()->statements());
2043 if (frame_ != NULL) {
2044 exit.Jump();
2045 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046
2047
2048 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002049 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002050
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002051 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2052 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002053
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002054 // Shadow the labels for all escapes from the try block, including
2055 // returns. During shadowing, the original label is hidden as the
2056 // LabelShadow and operations on the original actually affect the
2057 // shadowing label.
2058 //
2059 // We should probably try to unify the escaping labels and the return
2060 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002061 int nof_escapes = node->escaping_targets()->length();
2062 List<ShadowTarget*> shadows(1 + nof_escapes);
2063
2064 // Add the shadow target for the function return.
2065 static const int kReturnShadowIndex = 0;
2066 shadows.Add(new ShadowTarget(&function_return_));
2067 bool function_return_was_shadowed = function_return_is_shadowed_;
2068 function_return_is_shadowed_ = true;
2069 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2070
2071 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002073 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002074 }
2075
2076 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002077 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078
2079 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002080 // After shadowing stops, the original labels are unshadowed and the
2081 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002082 bool has_unlinks = false;
2083 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002084 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002085 has_unlinks = has_unlinks || shadows[i]->is_linked();
2086 }
2087 function_return_is_shadowed_ = function_return_was_shadowed;
2088
2089 // Get an external reference to the handler address.
2090 ExternalReference handler_address(Top::k_handler_address);
2091
2092 // The next handler address is at kNextIndex in the stack.
2093 const int kNextIndex = StackHandlerConstants::kNextOffset / kPointerSize;
2094 // If we can fall off the end of the try block, unlink from try chain.
2095 if (has_valid_frame()) {
2096 __ ldr(r1, frame_->ElementAt(kNextIndex));
2097 __ mov(r3, Operand(handler_address));
2098 __ str(r1, MemOperand(r3));
2099 frame_->Drop(StackHandlerConstants::kSize / kPointerSize);
2100 if (has_unlinks) {
2101 exit.Jump();
2102 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103 }
2104
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002105 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002106 // jumped to. Deallocate each shadow target.
2107 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002108 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002109 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002110 shadows[i]->Bind();
2111 // Because we can be jumping here (to spilled code) from unspilled
2112 // code, we need to reestablish a spilled frame at this block.
2113 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002114
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002115 // Reload sp from the top handler, because some statements that we
2116 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002117 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002118 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002119 // The stack pointer was restored to just below the code slot
2120 // (the topmost slot) in the handler.
2121 frame_->Forget(frame_->height() - handler_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002122
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002123 // kNextIndex is off by one because the code slot has already
2124 // been dropped.
2125 __ ldr(r1, frame_->ElementAt(kNextIndex - 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002126 __ str(r1, MemOperand(r3));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002127 // The code slot has already been dropped from the handler.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002128 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002129
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002130 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2131 frame_->PrepareForReturn();
2132 }
2133 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002134 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002135 delete shadows[i];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002136 }
2137
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002138 exit.Bind();
2139 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002140}
2141
2142
ager@chromium.org7c537e22008-10-16 08:43:32 +00002143void CodeGenerator::VisitTryFinally(TryFinally* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002144#ifdef DEBUG
2145 int original_height = frame_->height();
2146#endif
2147 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148 Comment cmnt(masm_, "[ TryFinally");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002149 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002150
2151 // State: Used to keep track of reason for entering the finally
2152 // block. Should probably be extended to hold information for
2153 // break/continue from within the try block.
2154 enum { FALLING, THROWING, JUMPING };
2155
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002156 JumpTarget try_block(this);
2157 JumpTarget finally_block(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002158
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002159 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002160
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002161 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002162 // In case of thrown exceptions, this is where we continue.
2163 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002164 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002165
2166 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002167 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002168
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002169 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2170 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002171
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002172 // Shadow the labels for all escapes from the try block, including
2173 // returns. Shadowing hides the original label as the LabelShadow and
2174 // operations on the original actually affect the shadowing label.
2175 //
2176 // We should probably try to unify the escaping labels and the return
2177 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002178 int nof_escapes = node->escaping_targets()->length();
2179 List<ShadowTarget*> shadows(1 + nof_escapes);
2180
2181 // Add the shadow target for the function return.
2182 static const int kReturnShadowIndex = 0;
2183 shadows.Add(new ShadowTarget(&function_return_));
2184 bool function_return_was_shadowed = function_return_is_shadowed_;
2185 function_return_is_shadowed_ = true;
2186 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2187
2188 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002189 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002190 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002191 }
2192
2193 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002194 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002195
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002196 // Stop the introduced shadowing and count the number of required unlinks.
2197 // After shadowing stops, the original labels are unshadowed and the
2198 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002199 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002200 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201 shadows[i]->StopShadowing();
2202 if (shadows[i]->is_linked()) nof_unlinks++;
2203 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002204 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002205
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002206 // Get an external reference to the handler address.
2207 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002208
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002209 // The next handler address is at kNextIndex in the stack.
2210 const int kNextIndex = StackHandlerConstants::kNextOffset / kPointerSize;
2211 // If we can fall off the end of the try block, unlink from the try
2212 // chain and set the state on the frame to FALLING.
2213 if (has_valid_frame()) {
2214 __ ldr(r1, frame_->ElementAt(kNextIndex));
2215 __ mov(r3, Operand(handler_address));
2216 __ str(r1, MemOperand(r3));
2217 frame_->Drop(StackHandlerConstants::kSize / kPointerSize);
2218
2219 // Fake a top of stack value (unneeded when FALLING) and set the
2220 // state in r2, then jump around the unlink blocks if any.
2221 __ mov(r0, Operand(Factory::undefined_value()));
2222 frame_->EmitPush(r0);
2223 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2224 if (nof_unlinks > 0) {
2225 finally_block.Jump();
2226 }
2227 }
2228
2229 // Generate code to unlink and set the state for the (formerly)
2230 // shadowing targets that have been jumped to.
2231 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002232 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002233 // If we have come from the shadowed return, the return value is
2234 // in (a non-refcounted reference to) r0. We must preserve it
2235 // until it is pushed.
2236 //
2237 // Because we can be jumping here (to spilled code) from
2238 // unspilled code, we need to reestablish a spilled frame at
2239 // this block.
2240 shadows[i]->Bind();
2241 frame_->SpillAll();
2242
2243 // Reload sp from the top handler, because some statements that
2244 // we break from (eg, for...in) may have left stuff on the
2245 // stack.
2246 __ mov(r3, Operand(handler_address));
2247 __ ldr(sp, MemOperand(r3));
2248 // The stack pointer was restored to the address slot in the handler.
2249 ASSERT(StackHandlerConstants::kNextOffset == 1 * kPointerSize);
2250 frame_->Forget(frame_->height() - handler_height + 1);
2251
2252 // Unlink this handler and drop it from the frame. The next
2253 // handler address is now on top of the frame.
2254 frame_->EmitPop(r1);
2255 __ str(r1, MemOperand(r3));
2256 // The top (code) and the second (handler) slot have both been
2257 // dropped already.
2258 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 2);
2259
2260 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002261 // If this label shadowed the function return, materialize the
2262 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002263 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002264 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002265 // Fake TOS for targets that shadowed breaks and continues.
mads.s.ager31e71382008-08-13 09:32:07 +00002266 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002267 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002268 }
2269 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002270 if (--nof_unlinks > 0) {
2271 // If this is not the last unlink block, jump around the next.
2272 finally_block.Jump();
2273 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002274 }
2275 }
2276
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002277 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002278 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002279
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002280 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002281 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002282
2283 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002284 // and the state - while evaluating the finally block.
2285 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002287 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002288
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002289 if (has_valid_frame()) {
2290 // Restore state and return value or faked TOS.
2291 frame_->EmitPop(r2);
2292 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002293 }
2294
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002295 // Generate code to jump to the right destination for all used
2296 // formerly shadowing targets. Deallocate each shadow target.
2297 for (int i = 0; i < shadows.length(); i++) {
2298 if (has_valid_frame() && shadows[i]->is_bound()) {
2299 JumpTarget* original = shadows[i]->other_target();
2300 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2301 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2302 JumpTarget skip(this);
2303 skip.Branch(ne);
2304 frame_->PrepareForReturn();
2305 original->Jump();
2306 skip.Bind();
2307 } else {
2308 original->Branch(eq);
2309 }
2310 }
2311 delete shadows[i];
2312 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002313
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002314 if (has_valid_frame()) {
2315 // Check if we need to rethrow the exception.
2316 JumpTarget exit(this);
2317 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2318 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002319
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002320 // Rethrow exception.
2321 frame_->EmitPush(r0);
2322 frame_->CallRuntime(Runtime::kReThrow, 1);
2323
2324 // Done.
2325 exit.Bind();
2326 }
2327 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002328}
2329
2330
ager@chromium.org7c537e22008-10-16 08:43:32 +00002331void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002332#ifdef DEBUG
2333 int original_height = frame_->height();
2334#endif
2335 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002337 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002338#ifdef ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002339 frame_->CallRuntime(Runtime::kDebugBreak, 0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002340#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002341 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002342 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002343}
2344
2345
ager@chromium.org7c537e22008-10-16 08:43:32 +00002346void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002347 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002348 ASSERT(boilerplate->IsBoilerplate());
2349
2350 // Push the boilerplate on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002351 __ mov(r0, Operand(boilerplate));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002352 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002353
2354 // Create a new closure.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002355 frame_->EmitPush(cp);
2356 frame_->CallRuntime(Runtime::kNewClosure, 2);
2357 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002358}
2359
2360
ager@chromium.org7c537e22008-10-16 08:43:32 +00002361void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002362#ifdef DEBUG
2363 int original_height = frame_->height();
2364#endif
2365 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002366 Comment cmnt(masm_, "[ FunctionLiteral");
2367
2368 // Build the function boilerplate and instantiate it.
2369 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00002370 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002371 if (HasStackOverflow()) {
2372 ASSERT(frame_->height() == original_height);
2373 return;
2374 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002375 InstantiateBoilerplate(boilerplate);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002376 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002377}
2378
2379
ager@chromium.org7c537e22008-10-16 08:43:32 +00002380void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002381 FunctionBoilerplateLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002382#ifdef DEBUG
2383 int original_height = frame_->height();
2384#endif
2385 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002386 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2387 InstantiateBoilerplate(node->boilerplate());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002388 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002389}
2390
2391
ager@chromium.org7c537e22008-10-16 08:43:32 +00002392void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002393#ifdef DEBUG
2394 int original_height = frame_->height();
2395#endif
2396 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002397 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002398 JumpTarget then(this);
2399 JumpTarget else_(this);
2400 JumpTarget exit(this);
2401 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
2402 &then, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002403 Branch(false, &else_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002404 then.Bind();
2405 LoadAndSpill(node->then_expression(), typeof_state());
2406 exit.Jump();
2407 else_.Bind();
2408 LoadAndSpill(node->else_expression(), typeof_state());
2409 exit.Bind();
2410 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002411}
2412
2413
ager@chromium.org7c537e22008-10-16 08:43:32 +00002414void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002415 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002416 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002417 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002418
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002419 JumpTarget slow(this);
2420 JumpTarget done(this);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002421
2422 // Generate fast-case code for variables that might be shadowed by
2423 // eval-introduced variables. Eval is used a lot without
2424 // introducing variables. In those cases, we do not want to
2425 // perform a runtime call for all variables in the scope
2426 // containing the eval.
2427 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2428 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002429 // If there was no control flow to slow, we can exit early.
2430 if (!slow.is_linked()) {
2431 frame_->EmitPush(r0);
2432 return;
2433 }
2434
2435 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002436
2437 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2438 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2439 // Only generate the fast case for locals that rewrite to slots.
2440 // This rules out argument loads.
2441 if (potential_slot != NULL) {
2442 __ ldr(r0,
2443 ContextSlotOperandCheckExtensions(potential_slot,
2444 r1,
2445 r2,
2446 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002447 if (potential_slot->var()->mode() == Variable::CONST) {
2448 __ cmp(r0, Operand(Factory::the_hole_value()));
2449 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
2450 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002451 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002452 // ContextSlotOperandCheckExtensions so we have to jump around
2453 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002454 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002455 }
2456 }
2457
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002458 slow.Bind();
2459 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002460 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002461 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002462
ager@chromium.org7c537e22008-10-16 08:43:32 +00002463 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002464 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002465 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002466 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002467 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002468
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002469 done.Bind();
2470 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002471
2472 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002473 // Note: We would like to keep the assert below, but it fires because of
2474 // some nasty code in LoadTypeofExpression() which should be removed...
ager@chromium.org381abbb2009-02-25 13:23:22 +00002475 // ASSERT(!slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002476
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002477 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002478 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002479 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002480 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002481 // Const slots may contain 'the hole' value (the constant hasn't been
2482 // initialized yet) which needs to be converted into the 'undefined'
2483 // value.
2484 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002485 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002486 __ cmp(r0, Operand(Factory::the_hole_value()));
2487 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002488 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002489 }
2490 }
2491}
2492
2493
ager@chromium.org381abbb2009-02-25 13:23:22 +00002494void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2495 TypeofState typeof_state,
2496 Register tmp,
2497 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002498 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002499 // Check that no extension objects have been created by calls to
2500 // eval from the current scope to the global scope.
2501 Register context = cp;
2502 Scope* s = scope();
2503 while (s != NULL) {
2504 if (s->num_heap_slots() > 0) {
2505 if (s->calls_eval()) {
2506 // Check that extension is NULL.
2507 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2508 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002509 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002510 }
2511 // Load next context in chain.
2512 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2513 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2514 context = tmp;
2515 }
2516 // If no outer scope calls eval, we do not need to check more
2517 // context extensions.
2518 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2519 s = s->outer_scope();
2520 }
2521
2522 if (s->is_eval_scope()) {
2523 Label next, fast;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002524 if (!context.is(tmp)) {
2525 __ mov(tmp, Operand(context));
2526 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002527 __ bind(&next);
2528 // Terminate at global context.
2529 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
2530 __ cmp(tmp2, Operand(Factory::global_context_map()));
2531 __ b(eq, &fast);
2532 // Check that extension is NULL.
2533 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2534 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002535 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002536 // Load next context in chain.
2537 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2538 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2539 __ b(&next);
2540 __ bind(&fast);
2541 }
2542
2543 // All extension objects were empty and it is safe to use a global
2544 // load IC call.
2545 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2546 // Load the global object.
2547 LoadGlobal();
2548 // Setup the name register.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002549 Result name = allocator_->Allocate(r2);
2550 ASSERT(name.is_valid()); // We are in spilled code.
2551 __ mov(name.reg(), Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002552 // Call IC stub.
2553 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002554 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002555 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002556 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002557 }
2558
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002559 // Drop the global object. The result is in r0.
2560 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002561}
2562
2563
ager@chromium.org7c537e22008-10-16 08:43:32 +00002564void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002565#ifdef DEBUG
2566 int original_height = frame_->height();
2567#endif
2568 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002569 Comment cmnt(masm_, "[ Slot");
2570 LoadFromSlot(node, typeof_state());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002571 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002572}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002573
ager@chromium.org7c537e22008-10-16 08:43:32 +00002574
2575void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002576#ifdef DEBUG
2577 int original_height = frame_->height();
2578#endif
2579 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002580 Comment cmnt(masm_, "[ VariableProxy");
2581
2582 Variable* var = node->var();
2583 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002584 if (expr != NULL) {
2585 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002586 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002587 ASSERT(var->is_global());
2588 Reference ref(this, node);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002589 ref.GetValueAndSpill(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002590 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002591 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002592}
2593
2594
ager@chromium.org7c537e22008-10-16 08:43:32 +00002595void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002596#ifdef DEBUG
2597 int original_height = frame_->height();
2598#endif
2599 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002600 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002601 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002602 frame_->EmitPush(r0);
2603 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002604}
2605
2606
ager@chromium.org7c537e22008-10-16 08:43:32 +00002607void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002608#ifdef DEBUG
2609 int original_height = frame_->height();
2610#endif
2611 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002612 Comment cmnt(masm_, "[ RexExp Literal");
2613
2614 // Retrieve the literal array and check the allocated entry.
2615
2616 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002617 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002618
2619 // Load the literals array of the function.
2620 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2621
2622 // Load the literal at the ast saved index.
2623 int literal_offset =
2624 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2625 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2626
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002627 JumpTarget done(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002628 __ cmp(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002629 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002630
2631 // If the entry is undefined we call the runtime system to computed
2632 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002633 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002634 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002635 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002636 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002637 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002638 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002639 frame_->EmitPush(r0);
2640 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002641 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002642
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002643 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002644 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002645 frame_->EmitPush(r2);
2646 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002647}
2648
2649
2650// This deferred code stub will be used for creating the boilerplate
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002651// by calling Runtime_CreateObjectLiteralBoilerplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002652// Each created boilerplate is stored in the JSFunction and they are
2653// therefore context dependent.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002654class DeferredObjectLiteral: public DeferredCode {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002655 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002656 DeferredObjectLiteral(CodeGenerator* generator, ObjectLiteral* node)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002657 : DeferredCode(generator), node_(node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002658 set_comment("[ DeferredObjectLiteral");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002659 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002660
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002661 virtual void Generate();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002662
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002663 private:
2664 ObjectLiteral* node_;
2665};
2666
2667
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002668void DeferredObjectLiteral::Generate() {
2669 // Argument is passed in r1.
2670 enter()->Bind();
2671 VirtualFrame::SpilledScope spilled_scope(generator());
2672
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002673 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002674 // the literal.
2675
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002676 VirtualFrame* frame = generator()->frame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002677 // Literal array (0).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002678 frame->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002679 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00002680 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002681 frame->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002682 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00002683 __ mov(r0, Operand(node_->constant_properties()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002684 frame->EmitPush(r0);
2685 Result boilerplate =
2686 frame->CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
2687 __ mov(r2, Operand(boilerplate.reg()));
2688 // Result is returned in r2.
2689 exit_.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002690}
2691
2692
ager@chromium.org7c537e22008-10-16 08:43:32 +00002693void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002694#ifdef DEBUG
2695 int original_height = frame_->height();
2696#endif
2697 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002698 Comment cmnt(masm_, "[ ObjectLiteral");
2699
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002700 DeferredObjectLiteral* deferred = new DeferredObjectLiteral(this, node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002701
2702 // Retrieve the literal array and check the allocated entry.
2703
2704 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002705 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002706
2707 // Load the literals array of the function.
2708 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2709
2710 // Load the literal at the ast saved index.
2711 int literal_offset =
2712 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2713 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2714
2715 // Check whether we need to materialize the object literal boilerplate.
2716 // If so, jump to the deferred code.
2717 __ cmp(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002718 deferred->enter()->Branch(eq);
2719 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002720
2721 // Push the object literal boilerplate.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002722 frame_->EmitPush(r2);
mads.s.ager31e71382008-08-13 09:32:07 +00002723
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002724 // Clone the boilerplate object.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002725 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2726 if (node->depth() == 1) {
2727 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2728 }
2729 frame_->CallRuntime(clone_function_id, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002730 frame_->EmitPush(r0); // save the result
mads.s.ager31e71382008-08-13 09:32:07 +00002731 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002732
2733 for (int i = 0; i < node->properties()->length(); i++) {
2734 ObjectLiteral::Property* property = node->properties()->at(i);
2735 Literal* key = property->key();
2736 Expression* value = property->value();
2737 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002738 case ObjectLiteral::Property::CONSTANT:
2739 break;
2740 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2741 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
2742 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002743 case ObjectLiteral::Property::COMPUTED: // fall through
2744 case ObjectLiteral::Property::PROTOTYPE: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002745 frame_->EmitPush(r0); // dup the result
2746 LoadAndSpill(key);
2747 LoadAndSpill(value);
2748 frame_->CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002749 // restore r0
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002750 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002751 break;
2752 }
2753 case ObjectLiteral::Property::SETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002754 frame_->EmitPush(r0);
2755 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002756 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002757 frame_->EmitPush(r0);
2758 LoadAndSpill(value);
2759 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002760 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002761 break;
2762 }
2763 case ObjectLiteral::Property::GETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002764 frame_->EmitPush(r0);
2765 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002766 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002767 frame_->EmitPush(r0);
2768 LoadAndSpill(value);
2769 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002770 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002771 break;
2772 }
2773 }
2774 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002775 ASSERT(frame_->height() == original_height + 1);
2776}
2777
2778
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002779// This deferred code stub will be used for creating the boilerplate
2780// by calling Runtime_CreateArrayLiteralBoilerplate.
2781// Each created boilerplate is stored in the JSFunction and they are
2782// therefore context dependent.
2783class DeferredArrayLiteral: public DeferredCode {
2784 public:
2785 DeferredArrayLiteral(CodeGenerator* generator, ArrayLiteral* node)
2786 : DeferredCode(generator), node_(node) {
2787 set_comment("[ DeferredArrayLiteral");
2788 }
2789
2790 virtual void Generate();
2791
2792 private:
2793 ArrayLiteral* node_;
2794};
2795
2796
2797void DeferredArrayLiteral::Generate() {
2798 // Argument is passed in r1.
2799 enter()->Bind();
2800 VirtualFrame::SpilledScope spilled_scope(generator());
2801
2802 // If the entry is undefined we call the runtime system to computed
2803 // the literal.
2804
2805 VirtualFrame* frame = generator()->frame();
2806 // Literal array (0).
2807 frame->EmitPush(r1);
2808 // Literal index (1).
2809 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
2810 frame->EmitPush(r0);
2811 // Constant properties (2).
2812 __ mov(r0, Operand(node_->literals()));
2813 frame->EmitPush(r0);
2814 Result boilerplate =
2815 frame->CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3);
2816 __ mov(r2, Operand(boilerplate.reg()));
2817 // Result is returned in r2.
2818 exit_.Jump();
2819}
2820
2821
ager@chromium.org7c537e22008-10-16 08:43:32 +00002822void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002823#ifdef DEBUG
2824 int original_height = frame_->height();
2825#endif
2826 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002827 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002828
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002829 DeferredArrayLiteral* deferred = new DeferredArrayLiteral(this, node);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002830
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002831 // Retrieve the literal array and check the allocated entry.
2832
2833 // Load the function of this activation.
2834 __ ldr(r1, frame_->Function());
2835
2836 // Load the literals array of the function.
2837 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2838
2839 // Load the literal at the ast saved index.
2840 int literal_offset =
2841 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2842 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2843
2844 // Check whether we need to materialize the object literal boilerplate.
2845 // If so, jump to the deferred code.
2846 __ cmp(r2, Operand(Factory::undefined_value()));
2847 deferred->enter()->Branch(eq);
2848 deferred->BindExit();
2849
2850 // Push the object literal boilerplate.
2851 frame_->EmitPush(r2);
2852
2853 // Clone the boilerplate object.
2854 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2855 if (node->depth() == 1) {
2856 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2857 }
2858 frame_->CallRuntime(clone_function_id, 1);
2859 frame_->EmitPush(r0); // save the result
2860 // r0: cloned object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002861
2862 // Generate code to set the elements in the array that are not
2863 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002864 for (int i = 0; i < node->values()->length(); i++) {
2865 Expression* value = node->values()->at(i);
2866
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002867 // If value is a literal the property value is already set in the
2868 // boilerplate object.
2869 if (value->AsLiteral() != NULL) continue;
2870 // If value is a materialized literal the property value is already set
2871 // in the boilerplate object if it is simple.
2872 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002873
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002874 // The property must be set by generated code.
2875 LoadAndSpill(value);
2876 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002877
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002878 // Fetch the object literal.
2879 __ ldr(r1, frame_->Top());
2880 // Get the elements array.
2881 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002882
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002883 // Write to the indexed properties array.
2884 int offset = i * kPointerSize + Array::kHeaderSize;
2885 __ str(r0, FieldMemOperand(r1, offset));
2886
2887 // Update the write barrier for the array address.
2888 __ mov(r3, Operand(offset));
2889 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002890 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002891 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002892}
2893
2894
ager@chromium.org32912102009-01-16 10:38:43 +00002895void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002896#ifdef DEBUG
2897 int original_height = frame_->height();
2898#endif
2899 ASSERT(!in_spilled_code());
2900 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org32912102009-01-16 10:38:43 +00002901 // Call runtime routine to allocate the catch extension object and
2902 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002903 Comment cmnt(masm_, "[ CatchExtensionObject");
2904 LoadAndSpill(node->key());
2905 LoadAndSpill(node->value());
2906 Result result =
2907 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2908 frame_->EmitPush(result.reg());
2909 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002910}
2911
2912
ager@chromium.org7c537e22008-10-16 08:43:32 +00002913void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002914#ifdef DEBUG
2915 int original_height = frame_->height();
2916#endif
2917 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002918 Comment cmnt(masm_, "[ Assignment");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002919 CodeForStatementPosition(node);
mads.s.ager31e71382008-08-13 09:32:07 +00002920
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002921 { Reference target(this, node->target());
2922 if (target.is_illegal()) {
2923 // Fool the virtual frame into thinking that we left the assignment's
2924 // value on the frame.
2925 __ mov(r0, Operand(Smi::FromInt(0)));
2926 frame_->EmitPush(r0);
2927 ASSERT(frame_->height() == original_height + 1);
2928 return;
2929 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002930
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002931 if (node->op() == Token::ASSIGN ||
2932 node->op() == Token::INIT_VAR ||
2933 node->op() == Token::INIT_CONST) {
2934 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002935
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002936 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002937 // +=, *= and similar binary assignments.
2938 // Get the old value of the lhs.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002939 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
2940 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002941 bool overwrite =
2942 (node->value()->AsBinaryOperation() != NULL &&
2943 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002944 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002945 SmiOperation(node->binary_op(),
2946 literal->handle(),
2947 false,
2948 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002949 frame_->EmitPush(r0);
2950
2951 } else {
2952 LoadAndSpill(node->value());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002953 GenericBinaryOperation(node->binary_op(),
2954 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002955 frame_->EmitPush(r0);
2956 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002957 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002959 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2960 if (var != NULL &&
2961 (var->mode() == Variable::CONST) &&
2962 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2963 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002964
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002965 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002966 CodeForSourcePosition(node->position());
2967 if (node->op() == Token::INIT_CONST) {
2968 // Dynamic constant initializations must use the function context
2969 // and initialize the actual constant declared. Dynamic variable
2970 // initializations are simply assignments and use SetValue.
2971 target.SetValue(CONST_INIT);
2972 } else {
2973 target.SetValue(NOT_CONST_INIT);
2974 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002975 }
2976 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002977 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002978}
2979
2980
ager@chromium.org7c537e22008-10-16 08:43:32 +00002981void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002982#ifdef DEBUG
2983 int original_height = frame_->height();
2984#endif
2985 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002986 Comment cmnt(masm_, "[ Throw");
2987
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002988 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002989 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002990 frame_->CallRuntime(Runtime::kThrow, 1);
2991 frame_->EmitPush(r0);
2992 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002993}
2994
2995
ager@chromium.org7c537e22008-10-16 08:43:32 +00002996void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002997#ifdef DEBUG
2998 int original_height = frame_->height();
2999#endif
3000 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003001 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003002
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003003 { Reference property(this, node);
3004 property.GetValueAndSpill(typeof_state());
3005 }
3006 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003007}
3008
3009
ager@chromium.org7c537e22008-10-16 08:43:32 +00003010void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003011#ifdef DEBUG
3012 int original_height = frame_->height();
3013#endif
3014 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003015 Comment cmnt(masm_, "[ Call");
3016
3017 ZoneList<Expression*>* args = node->arguments();
3018
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003019 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003020 // Standard function call.
3021
3022 // Check if the function is a variable or a property.
3023 Expression* function = node->expression();
3024 Variable* var = function->AsVariableProxy()->AsVariable();
3025 Property* property = function->AsProperty();
3026
3027 // ------------------------------------------------------------------------
3028 // Fast-case: Use inline caching.
3029 // ---
3030 // According to ECMA-262, section 11.2.3, page 44, the function to call
3031 // must be resolved after the arguments have been evaluated. The IC code
3032 // automatically handles this by loading the arguments before the function
3033 // is resolved in cache misses (this also holds for megamorphic calls).
3034 // ------------------------------------------------------------------------
3035
3036 if (var != NULL && !var->is_this() && var->is_global()) {
3037 // ----------------------------------
3038 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3039 // ----------------------------------
3040
3041 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003042 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003043 frame_->EmitPush(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003044
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003045 // Pass the global object as the receiver and let the IC stub
3046 // patch the stack to use the global proxy as 'this' in the
3047 // invoked function.
3048 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003049
3050 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003051 int arg_count = args->length();
3052 for (int i = 0; i < arg_count; i++) {
3053 LoadAndSpill(args->at(i));
3054 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003055
3056 // Setup the receiver register and call the IC initialization code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003057 Handle<Code> stub = ComputeCallInitialize(arg_count);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003058 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003059 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3060 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003061 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003062 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003063 frame_->Drop();
3064 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003065
3066 } else if (var != NULL && var->slot() != NULL &&
3067 var->slot()->type() == Slot::LOOKUP) {
3068 // ----------------------------------
3069 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3070 // ----------------------------------
3071
3072 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003073 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003074 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003075 frame_->EmitPush(r0);
3076 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003077 // r0: slot value; r1: receiver
3078
3079 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003080 frame_->EmitPush(r0); // function
3081 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003082
3083 // Call the function.
3084 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003085 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003086
3087 } else if (property != NULL) {
3088 // Check if the key is a literal string.
3089 Literal* literal = property->key()->AsLiteral();
3090
3091 if (literal != NULL && literal->handle()->IsSymbol()) {
3092 // ------------------------------------------------------------------
3093 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3094 // ------------------------------------------------------------------
3095
3096 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003097 __ mov(r0, Operand(literal->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003098 frame_->EmitPush(r0);
3099 LoadAndSpill(property->obj());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003100
3101 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003102 int arg_count = args->length();
3103 for (int i = 0; i < arg_count; i++) {
3104 LoadAndSpill(args->at(i));
3105 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003106
3107 // Set the receiver register and call the IC initialization code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003108 Handle<Code> stub = ComputeCallInitialize(arg_count);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003109 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003110 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003111 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003112
3113 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003114 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00003115
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003116 frame_->EmitPush(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003117
3118 } else {
3119 // -------------------------------------------
3120 // JavaScript example: 'array[index](1, 2, 3)'
3121 // -------------------------------------------
3122
3123 // Load the function to call from the property through a reference.
3124 Reference ref(this, property);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003125 ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003126
3127 // Pass receiver to called function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003128 if (property->is_synthetic()) {
3129 LoadGlobalReceiver(r0);
3130 } else {
3131 __ ldr(r0, frame_->ElementAt(ref.size()));
3132 frame_->EmitPush(r0);
3133 }
3134
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003135 // Call the function.
3136 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003137 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003138 }
3139
3140 } else {
3141 // ----------------------------------
3142 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3143 // ----------------------------------
3144
3145 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003146 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003147
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003148 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003149 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003150
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003151 // Call the function.
3152 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003153 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003154 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003155 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003156}
3157
3158
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003159void CodeGenerator::VisitCallEval(CallEval* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003160#ifdef DEBUG
3161 int original_height = frame_->height();
3162#endif
3163 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003164 Comment cmnt(masm_, "[ CallEval");
3165
3166 // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve
3167 // the function we need to call and the receiver of the call.
3168 // Then we call the resolved function using the given arguments.
3169
3170 ZoneList<Expression*>* args = node->arguments();
3171 Expression* function = node->expression();
3172
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003173 CodeForStatementPosition(node);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003174
3175 // Prepare stack for call to resolved function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003176 LoadAndSpill(function);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003177 __ mov(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003178 frame_->EmitPush(r2); // Slot for receiver
3179 int arg_count = args->length();
3180 for (int i = 0; i < arg_count; i++) {
3181 LoadAndSpill(args->at(i));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003182 }
3183
3184 // Prepare stack for call to ResolvePossiblyDirectEval.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003185 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3186 frame_->EmitPush(r1);
3187 if (arg_count > 0) {
3188 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3189 frame_->EmitPush(r1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003190 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003191 frame_->EmitPush(r2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003192 }
3193
3194 // Resolve the call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003195 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003196
3197 // Touch up stack with the right values for the function and the receiver.
3198 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003199 __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003200 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003201 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003202
3203 // Call the function.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003204 CodeForSourcePosition(node->position());
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003205
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003206 CallFunctionStub call_function(arg_count);
3207 frame_->CallStub(&call_function, arg_count + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003208
3209 __ ldr(cp, frame_->Context());
3210 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003211 frame_->Drop();
3212 frame_->EmitPush(r0);
3213 ASSERT(frame_->height() == original_height + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003214}
3215
3216
ager@chromium.org7c537e22008-10-16 08:43:32 +00003217void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003218#ifdef DEBUG
3219 int original_height = frame_->height();
3220#endif
3221 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003222 Comment cmnt(masm_, "[ CallNew");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003223 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003224
3225 // According to ECMA-262, section 11.2.2, page 44, the function
3226 // expression in new calls must be evaluated before the
3227 // arguments. This is different from ordinary calls, where the
3228 // actual function to call is resolved after the arguments have been
3229 // evaluated.
3230
3231 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003232 // receiver. There is no need to use the global proxy here because
3233 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003234 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003235 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003236
3237 // Push the arguments ("left-to-right") on the stack.
3238 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003239 int arg_count = args->length();
3240 for (int i = 0; i < arg_count; i++) {
3241 LoadAndSpill(args->at(i));
3242 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003243
mads.s.ager31e71382008-08-13 09:32:07 +00003244 // r0: the number of arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003245 Result num_args = allocator_->Allocate(r0);
3246 ASSERT(num_args.is_valid());
3247 __ mov(num_args.reg(), Operand(arg_count));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003248
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003249 // Load the function into r1 as per calling convention.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003250 Result function = allocator_->Allocate(r1);
3251 ASSERT(function.is_valid());
3252 __ ldr(function.reg(), frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003253
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003254 // Call the construct call builtin that handles allocation and
3255 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003256 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003257 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
3258 Result result = frame_->CallCodeObject(ic,
3259 RelocInfo::CONSTRUCT_CALL,
3260 &num_args,
3261 &function,
3262 arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003263
3264 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003265 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003266 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003267}
3268
3269
ager@chromium.org7c537e22008-10-16 08:43:32 +00003270void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003271 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003272 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003273 JumpTarget leave(this);
3274 LoadAndSpill(args->at(0));
3275 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003276 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003277 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003278 leave.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003279 // It is a heap object - get map.
3280 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3281 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00003282 // if (!object->IsJSValue()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003283 __ cmp(r1, Operand(JS_VALUE_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003284 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003285 // Load the value.
3286 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003287 leave.Bind();
3288 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003289}
3290
3291
ager@chromium.org7c537e22008-10-16 08:43:32 +00003292void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003293 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003294 ASSERT(args->length() == 2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003295 JumpTarget leave(this);
3296 LoadAndSpill(args->at(0)); // Load the object.
3297 LoadAndSpill(args->at(1)); // Load the value.
3298 frame_->EmitPop(r0); // r0 contains value
3299 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003300 // if (object->IsSmi()) return object.
3301 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003302 leave.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003303 // It is a heap object - get map.
3304 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3305 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3306 // if (!object->IsJSValue()) return object.
3307 __ cmp(r2, Operand(JS_VALUE_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003308 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003309 // Store the value.
3310 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3311 // Update the write barrier.
3312 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3313 __ RecordWrite(r1, r2, r3);
3314 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003315 leave.Bind();
3316 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003317}
3318
3319
ager@chromium.org7c537e22008-10-16 08:43:32 +00003320void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003321 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003322 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003323 LoadAndSpill(args->at(0));
3324 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003325 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003326 cc_reg_ = eq;
3327}
3328
3329
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003330void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003331 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003332 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3333 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003334#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003335 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003336 LoadAndSpill(args->at(1));
3337 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003338 __ CallRuntime(Runtime::kLog, 2);
3339 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003340#endif
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003341 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003342 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003343}
3344
3345
ager@chromium.org7c537e22008-10-16 08:43:32 +00003346void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003347 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003348 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003349 LoadAndSpill(args->at(0));
3350 frame_->EmitPop(r0);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003351 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
3352 cc_reg_ = eq;
3353}
3354
3355
kasper.lund7276f142008-07-30 08:49:36 +00003356// This should generate code that performs a charCodeAt() call or returns
3357// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3358// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003359void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003360 VirtualFrame::SpilledScope spilled_scope(this);
kasper.lund7276f142008-07-30 08:49:36 +00003361 ASSERT(args->length() == 2);
kasper.lund7276f142008-07-30 08:49:36 +00003362 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003363 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003364}
3365
3366
ager@chromium.org7c537e22008-10-16 08:43:32 +00003367void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003368 VirtualFrame::SpilledScope spilled_scope(this);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003369 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003370 LoadAndSpill(args->at(0));
3371 JumpTarget answer(this);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003372 // We need the CC bits to come out as not_equal in the case where the
3373 // object is a smi. This can't be done with the usual test opcode so
3374 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003375 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003376 __ and_(r1, r0, Operand(kSmiTagMask));
3377 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003378 answer.Branch(ne);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003379 // It is a heap object - get the map.
3380 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3381 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3382 // Check if the object is a JS array or not.
3383 __ cmp(r1, Operand(JS_ARRAY_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003384 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003385 cc_reg_ = eq;
3386}
3387
3388
ager@chromium.org7c537e22008-10-16 08:43:32 +00003389void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003390 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003391 ASSERT(args->length() == 0);
3392
mads.s.ager31e71382008-08-13 09:32:07 +00003393 // Seed the result with the formal parameters count, which will be used
3394 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003395 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
3396
3397 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003398 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003399 frame_->CallStub(&stub, 0);
3400 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003401}
3402
3403
ager@chromium.org7c537e22008-10-16 08:43:32 +00003404void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003405 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003406 ASSERT(args->length() == 1);
3407
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003408 // Satisfy contract with ArgumentsAccessStub:
3409 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003410 LoadAndSpill(args->at(0));
3411 frame_->EmitPop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003412 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003413
3414 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003415 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003416 frame_->CallStub(&stub, 0);
3417 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003418}
3419
3420
ager@chromium.org7c537e22008-10-16 08:43:32 +00003421void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003422 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003423 ASSERT(args->length() == 2);
3424
3425 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003426 LoadAndSpill(args->at(0));
3427 LoadAndSpill(args->at(1));
3428 frame_->EmitPop(r0);
3429 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003430 __ cmp(r0, Operand(r1));
3431 cc_reg_ = eq;
3432}
3433
3434
ager@chromium.org7c537e22008-10-16 08:43:32 +00003435void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003436#ifdef DEBUG
3437 int original_height = frame_->height();
3438#endif
3439 VirtualFrame::SpilledScope spilled_scope(this);
3440 if (CheckForInlineRuntimeCall(node)) {
3441 ASSERT((has_cc() && frame_->height() == original_height) ||
3442 (!has_cc() && frame_->height() == original_height + 1));
3443 return;
3444 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003445
3446 ZoneList<Expression*>* args = node->arguments();
3447 Comment cmnt(masm_, "[ CallRuntime");
3448 Runtime::Function* function = node->function();
3449
ager@chromium.org41826e72009-03-30 13:30:57 +00003450 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003451 // Prepare stack for calling JS runtime function.
3452 __ mov(r0, Operand(node->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003453 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003454 // Push the builtins object found in the current global object.
3455 __ ldr(r1, GlobalObject());
3456 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003457 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003458 }
mads.s.ager31e71382008-08-13 09:32:07 +00003459
ager@chromium.org41826e72009-03-30 13:30:57 +00003460 // Push the arguments ("left-to-right").
3461 int arg_count = args->length();
3462 for (int i = 0; i < arg_count; i++) {
3463 LoadAndSpill(args->at(i));
3464 }
mads.s.ager31e71382008-08-13 09:32:07 +00003465
ager@chromium.org41826e72009-03-30 13:30:57 +00003466 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003467 // Call the JS runtime function.
ager@chromium.org41826e72009-03-30 13:30:57 +00003468 Handle<Code> stub = ComputeCallInitialize(arg_count);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003469 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003470 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003471 frame_->Drop();
3472 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003473 } else {
3474 // Call the C runtime function.
3475 frame_->CallRuntime(function, arg_count);
3476 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003477 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003478 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003479}
3480
3481
ager@chromium.org7c537e22008-10-16 08:43:32 +00003482void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003483#ifdef DEBUG
3484 int original_height = frame_->height();
3485#endif
3486 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003487 Comment cmnt(masm_, "[ UnaryOperation");
3488
3489 Token::Value op = node->op();
3490
3491 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003492 LoadConditionAndSpill(node->expression(),
3493 NOT_INSIDE_TYPEOF,
3494 false_target(),
3495 true_target(),
3496 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003497 cc_reg_ = NegateCondition(cc_reg_);
3498
3499 } else if (op == Token::DELETE) {
3500 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003501 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003502 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003503 LoadAndSpill(property->obj());
3504 LoadAndSpill(property->key());
3505 Result arg_count = allocator_->Allocate(r0);
3506 ASSERT(arg_count.is_valid());
3507 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3508 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003509
mads.s.ager31e71382008-08-13 09:32:07 +00003510 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003511 Slot* slot = variable->slot();
3512 if (variable->is_global()) {
3513 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003514 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003515 frame_->EmitPush(r0);
3516 Result arg_count = allocator_->Allocate(r0);
3517 ASSERT(arg_count.is_valid());
3518 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3519 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003520
3521 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3522 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003523 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003524 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003525 frame_->EmitPush(r0);
3526 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003527 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003528 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003529 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003530 frame_->EmitPush(r0);
3531 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 {
3537 // Default: Result of deleting non-global, not dynamically
3538 // introduced variables is false.
3539 __ mov(r0, Operand(Factory::false_value()));
3540 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003541
3542 } else {
3543 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003544 LoadAndSpill(node->expression()); // may have side-effects
3545 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003546 __ mov(r0, Operand(Factory::true_value()));
3547 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003548 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003549
3550 } else if (op == Token::TYPEOF) {
3551 // Special case for loading the typeof expression; see comment on
3552 // LoadTypeofExpression().
3553 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003554 frame_->CallRuntime(Runtime::kTypeof, 1);
3555 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003556
3557 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003558 LoadAndSpill(node->expression());
3559 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003560 switch (op) {
3561 case Token::NOT:
3562 case Token::DELETE:
3563 case Token::TYPEOF:
3564 UNREACHABLE(); // handled above
3565 break;
3566
3567 case Token::SUB: {
3568 UnarySubStub stub;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003569 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003570 break;
3571 }
3572
3573 case Token::BIT_NOT: {
3574 // smi check
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003575 JumpTarget smi_label(this);
3576 JumpTarget continue_label(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003577 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003578 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003579
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003580 frame_->EmitPush(r0);
3581 Result arg_count = allocator_->Allocate(r0);
3582 ASSERT(arg_count.is_valid());
3583 __ mov(arg_count.reg(), Operand(0)); // not counting receiver
3584 frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003585
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003586 continue_label.Jump();
3587 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003588 __ mvn(r0, Operand(r0));
3589 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003590 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003591 break;
3592 }
3593
3594 case Token::VOID:
3595 // since the stack top is cached in r0, popping and then
3596 // pushing a value can be done by just writing to r0.
3597 __ mov(r0, Operand(Factory::undefined_value()));
3598 break;
3599
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003600 case Token::ADD: {
3601 // Smi check.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003602 JumpTarget continue_label(this);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003603 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003604 continue_label.Branch(eq);
3605 frame_->EmitPush(r0);
3606 Result arg_count = allocator_->Allocate(r0);
3607 ASSERT(arg_count.is_valid());
3608 __ mov(arg_count.reg(), Operand(0)); // not counting receiver
3609 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3610 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003611 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003612 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003613 default:
3614 UNREACHABLE();
3615 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003616 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003617 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003618 ASSERT((has_cc() && frame_->height() == original_height) ||
3619 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003620}
3621
3622
ager@chromium.org7c537e22008-10-16 08:43:32 +00003623void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003624#ifdef DEBUG
3625 int original_height = frame_->height();
3626#endif
3627 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003628 Comment cmnt(masm_, "[ CountOperation");
3629
3630 bool is_postfix = node->is_postfix();
3631 bool is_increment = node->op() == Token::INC;
3632
3633 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3634 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3635
3636 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003637 if (is_postfix) {
3638 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003639 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003640 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003641
3642 { Reference target(this, node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003643 if (target.is_illegal()) {
3644 // Spoof the virtual frame to have the expected height (one higher
3645 // than on entry).
3646 if (!is_postfix) {
3647 __ mov(r0, Operand(Smi::FromInt(0)));
3648 frame_->EmitPush(r0);
3649 }
3650 ASSERT(frame_->height() == original_height + 1);
3651 return;
3652 }
3653 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
3654 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003655
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003656 JumpTarget slow(this);
3657 JumpTarget exit(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003658
3659 // Load the value (1) into register r1.
3660 __ mov(r1, Operand(Smi::FromInt(1)));
3661
3662 // Check for smi operand.
3663 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003664 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003665
3666 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003667 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003668 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003669 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003670
3671 // Perform optimistic increment/decrement.
3672 if (is_increment) {
3673 __ add(r0, r0, Operand(r1), SetCC);
3674 } else {
3675 __ sub(r0, r0, Operand(r1), SetCC);
3676 }
3677
3678 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003679 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003680
3681 // Revert optimistic increment/decrement.
3682 if (is_increment) {
3683 __ sub(r0, r0, Operand(r1));
3684 } else {
3685 __ add(r0, r0, Operand(r1));
3686 }
3687
3688 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003689 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003690 {
3691 // Convert the operand to a number.
3692 frame_->EmitPush(r0);
3693 Result arg_count = allocator_->Allocate(r0);
3694 ASSERT(arg_count.is_valid());
3695 __ mov(arg_count.reg(), Operand(0));
3696 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3697 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003698 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003699 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003700 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003701 }
3702
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003703 // Compute the new value.
3704 __ mov(r1, Operand(Smi::FromInt(1)));
3705 frame_->EmitPush(r0);
3706 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003707 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003708 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003709 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003710 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003711 }
3712
3713 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003714 exit.Bind();
3715 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003716 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003717 }
3718
3719 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003720 if (is_postfix) frame_->EmitPop(r0);
3721 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003722}
3723
3724
ager@chromium.org7c537e22008-10-16 08:43:32 +00003725void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003726#ifdef DEBUG
3727 int original_height = frame_->height();
3728#endif
3729 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003730 Comment cmnt(masm_, "[ BinaryOperation");
3731 Token::Value op = node->op();
3732
3733 // According to ECMA-262 section 11.11, page 58, the binary logical
3734 // operators must yield the result of one of the two expressions
3735 // before any ToBoolean() conversions. This means that the value
3736 // produced by a && or || operator is not necessarily a boolean.
3737
3738 // NOTE: If the left hand side produces a materialized value (not in
3739 // the CC register), we force the right hand side to do the
3740 // same. This is necessary because we may have to branch to the exit
3741 // after evaluating the left hand side (due to the shortcut
3742 // semantics), but the compiler must (statically) know if the result
3743 // of compiling the binary operation is materialized or not.
3744
3745 if (op == Token::AND) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003746 JumpTarget is_true(this);
3747 LoadConditionAndSpill(node->left(),
3748 NOT_INSIDE_TYPEOF,
3749 &is_true,
3750 false_target(),
3751 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003752 if (has_cc()) {
3753 Branch(false, false_target());
3754
3755 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003756 is_true.Bind();
3757 LoadConditionAndSpill(node->right(),
3758 NOT_INSIDE_TYPEOF,
3759 true_target(),
3760 false_target(),
3761 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003762
3763 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003764 JumpTarget pop_and_continue(this);
3765 JumpTarget exit(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003766
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003767 __ ldr(r0, frame_->Top()); // dup the stack top
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003768 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003769 // Avoid popping the result if it converts to 'false' using the
3770 // standard ToBoolean() conversion as described in ECMA-262,
3771 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003772 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003773 Branch(false, &exit);
3774
3775 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003776 pop_and_continue.Bind();
3777 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003778
3779 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003780 is_true.Bind();
3781 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003782
3783 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003784 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003785 }
3786
3787 } else if (op == Token::OR) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003788 JumpTarget is_false(this);
3789 LoadConditionAndSpill(node->left(),
3790 NOT_INSIDE_TYPEOF,
3791 true_target(),
3792 &is_false,
3793 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003794 if (has_cc()) {
3795 Branch(true, true_target());
3796
3797 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003798 is_false.Bind();
3799 LoadConditionAndSpill(node->right(),
3800 NOT_INSIDE_TYPEOF,
3801 true_target(),
3802 false_target(),
3803 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003804
3805 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003806 JumpTarget pop_and_continue(this);
3807 JumpTarget exit(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003808
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003809 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003810 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003811 // Avoid popping the result if it converts to 'true' using the
3812 // standard ToBoolean() conversion as described in ECMA-262,
3813 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003814 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003815 Branch(true, &exit);
3816
3817 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003818 pop_and_continue.Bind();
3819 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003820
3821 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003822 is_false.Bind();
3823 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003824
3825 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003826 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003827 }
3828
3829 } else {
3830 // Optimize for the case where (at least) one of the expressions
3831 // is a literal small integer.
3832 Literal* lliteral = node->left()->AsLiteral();
3833 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003834 // NOTE: The code below assumes that the slow cases (calls to runtime)
3835 // never return a constant/immutable object.
3836 bool overwrite_left =
3837 (node->left()->AsBinaryOperation() != NULL &&
3838 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
3839 bool overwrite_right =
3840 (node->right()->AsBinaryOperation() != NULL &&
3841 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003842
3843 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003844 LoadAndSpill(node->left());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003845 SmiOperation(node->op(),
3846 rliteral->handle(),
3847 false,
3848 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003849
3850 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003851 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003852 SmiOperation(node->op(),
3853 lliteral->handle(),
3854 true,
3855 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003856
3857 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003858 OverwriteMode overwrite_mode = NO_OVERWRITE;
3859 if (overwrite_left) {
3860 overwrite_mode = OVERWRITE_LEFT;
3861 } else if (overwrite_right) {
3862 overwrite_mode = OVERWRITE_RIGHT;
3863 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003864 LoadAndSpill(node->left());
3865 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003866 GenericBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003867 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003868 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003869 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003870 ASSERT((has_cc() && frame_->height() == original_height) ||
3871 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003872}
3873
3874
ager@chromium.org7c537e22008-10-16 08:43:32 +00003875void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003876#ifdef DEBUG
3877 int original_height = frame_->height();
3878#endif
3879 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003880 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003881 frame_->EmitPush(r0);
3882 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003883}
3884
3885
ager@chromium.org7c537e22008-10-16 08:43:32 +00003886void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003887#ifdef DEBUG
3888 int original_height = frame_->height();
3889#endif
3890 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003891 Comment cmnt(masm_, "[ CompareOperation");
3892
3893 // Get the expressions from the node.
3894 Expression* left = node->left();
3895 Expression* right = node->right();
3896 Token::Value op = node->op();
3897
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003898 // To make null checks efficient, we check if either left or right is the
3899 // literal 'null'. If so, we optimize the code by inlining a null check
3900 // instead of calling the (very) general runtime routine for checking
3901 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003902 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003903 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003904 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003905 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003906 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
3907 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003908 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003909 LoadAndSpill(left_is_null ? right : left);
3910 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003911 __ cmp(r0, Operand(Factory::null_value()));
3912
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003913 // The 'null' value is only equal to 'undefined' if using non-strict
3914 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003915 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003916 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003917
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003918 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003919 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003920
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003921 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003922 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003923
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003924 // It can be an undetectable object.
3925 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3926 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
3927 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3928 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003929 }
3930
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003931 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003932 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003933 return;
3934 }
3935 }
3936
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003937 // To make typeof testing for natives implemented in JavaScript really
3938 // efficient, we generate special code for expressions of the form:
3939 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003940 UnaryOperation* operation = left->AsUnaryOperation();
3941 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
3942 (operation != NULL && operation->op() == Token::TYPEOF) &&
3943 (right->AsLiteral() != NULL &&
3944 right->AsLiteral()->handle()->IsString())) {
3945 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
3946
mads.s.ager31e71382008-08-13 09:32:07 +00003947 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003948 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003949 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003950
3951 if (check->Equals(Heap::number_symbol())) {
3952 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003953 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003954 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3955 __ cmp(r1, Operand(Factory::heap_number_map()));
3956 cc_reg_ = eq;
3957
3958 } else if (check->Equals(Heap::string_symbol())) {
3959 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003960 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003961
3962 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3963
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003964 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003965 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3966 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3967 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003968 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003969
3970 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3971 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
3972 cc_reg_ = lt;
3973
3974 } else if (check->Equals(Heap::boolean_symbol())) {
3975 __ cmp(r1, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003976 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003977 __ cmp(r1, Operand(Factory::false_value()));
3978 cc_reg_ = eq;
3979
3980 } else if (check->Equals(Heap::undefined_symbol())) {
3981 __ cmp(r1, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003982 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003983
3984 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003985 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003986
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003987 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003988 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3989 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3990 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3991 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3992
3993 cc_reg_ = eq;
3994
3995 } else if (check->Equals(Heap::function_symbol())) {
3996 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003997 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003998 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3999 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
4000 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
4001 cc_reg_ = eq;
4002
4003 } else if (check->Equals(Heap::object_symbol())) {
4004 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004005 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004006
4007 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4008 __ cmp(r1, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004009 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004010
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004011 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004012 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
4013 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4014 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004015 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004016
4017 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4018 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004019 false_target()->Branch(lt);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004020 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
4021 cc_reg_ = le;
4022
4023 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004024 // Uncommon case: typeof testing against a string literal that is
4025 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004026 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004027 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004028 ASSERT(!has_valid_frame() ||
4029 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004030 return;
4031 }
4032
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004033 LoadAndSpill(left);
4034 LoadAndSpill(right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004035 switch (op) {
4036 case Token::EQ:
4037 Comparison(eq, false);
4038 break;
4039
4040 case Token::LT:
4041 Comparison(lt);
4042 break;
4043
4044 case Token::GT:
4045 Comparison(gt);
4046 break;
4047
4048 case Token::LTE:
4049 Comparison(le);
4050 break;
4051
4052 case Token::GTE:
4053 Comparison(ge);
4054 break;
4055
4056 case Token::EQ_STRICT:
4057 Comparison(eq, true);
4058 break;
4059
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004060 case Token::IN: {
4061 Result arg_count = allocator_->Allocate(r0);
4062 ASSERT(arg_count.is_valid());
4063 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
4064 Result result = frame_->InvokeBuiltin(Builtins::IN,
4065 CALL_JS,
4066 &arg_count,
4067 2);
4068 frame_->EmitPush(result.reg());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004069 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004070 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004071
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004072 case Token::INSTANCEOF: {
4073 Result arg_count = allocator_->Allocate(r0);
4074 ASSERT(arg_count.is_valid());
4075 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
4076 Result result = frame_->InvokeBuiltin(Builtins::INSTANCE_OF,
4077 CALL_JS,
4078 &arg_count,
4079 2);
4080 __ tst(result.reg(), Operand(result.reg()));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004081 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004082 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004083 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004084
4085 default:
4086 UNREACHABLE();
4087 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004088 ASSERT((has_cc() && frame_->height() == original_height) ||
4089 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004090}
4091
4092
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004093#ifdef DEBUG
4094bool CodeGenerator::HasValidEntryRegisters() { return true; }
4095#endif
4096
4097
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004098#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004099#define __ ACCESS_MASM(masm)
4100
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004101
ager@chromium.org7c537e22008-10-16 08:43:32 +00004102Handle<String> Reference::GetName() {
4103 ASSERT(type_ == NAMED);
4104 Property* property = expression_->AsProperty();
4105 if (property == NULL) {
4106 // Global variable reference treated as a named property reference.
4107 VariableProxy* proxy = expression_->AsVariableProxy();
4108 ASSERT(proxy->AsVariable() != NULL);
4109 ASSERT(proxy->AsVariable()->is_global());
4110 return proxy->name();
4111 } else {
4112 Literal* raw_name = property->key()->AsLiteral();
4113 ASSERT(raw_name != NULL);
4114 return Handle<String>(String::cast(*raw_name->handle()));
4115 }
4116}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004117
ager@chromium.org7c537e22008-10-16 08:43:32 +00004118
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004119void Reference::GetValueAndSpill(TypeofState typeof_state) {
4120 ASSERT(cgen_->in_spilled_code());
4121 cgen_->set_in_spilled_code(false);
4122 GetValue(typeof_state);
4123 cgen_->frame()->SpillAll();
4124 cgen_->set_in_spilled_code(true);
4125}
4126
4127
ager@chromium.org7c537e22008-10-16 08:43:32 +00004128void Reference::GetValue(TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004129 ASSERT(!cgen_->in_spilled_code());
4130 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004131 ASSERT(!is_illegal());
4132 ASSERT(!cgen_->has_cc());
4133 MacroAssembler* masm = cgen_->masm();
4134 Property* property = expression_->AsProperty();
4135 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004136 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004137 }
4138
4139 switch (type_) {
4140 case SLOT: {
4141 Comment cmnt(masm, "[ Load from Slot");
4142 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4143 ASSERT(slot != NULL);
4144 cgen_->LoadFromSlot(slot, typeof_state);
4145 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004146 }
4147
ager@chromium.org7c537e22008-10-16 08:43:32 +00004148 case NAMED: {
4149 // TODO(1241834): Make sure that this it is safe to ignore the
4150 // distinction between expressions in a typeof and not in a typeof. If
4151 // there is a chance that reference errors can be thrown below, we
4152 // must distinguish between the two kinds of loads (typeof expression
4153 // loads must not throw a reference error).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004154 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004155 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004156 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004157 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004158 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4159 // Setup the name register.
4160 Result name_reg = cgen_->allocator()->Allocate(r2);
4161 ASSERT(name_reg.is_valid());
4162 __ mov(name_reg.reg(), Operand(name));
4163 ASSERT(var == NULL || var->is_global());
4164 RelocInfo::Mode rmode = (var == NULL)
4165 ? RelocInfo::CODE_TARGET
4166 : RelocInfo::CODE_TARGET_CONTEXT;
4167 Result answer = frame->CallCodeObject(ic, rmode, &name_reg, 0);
4168 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004169 break;
4170 }
4171
4172 case KEYED: {
4173 // TODO(1241834): Make sure that this it is safe to ignore the
4174 // distinction between expressions in a typeof and not in a typeof.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004175
4176 // TODO(181): Implement inlined version of array indexing once
4177 // loop nesting is properly tracked on ARM.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004178 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004179 Comment cmnt(masm, "[ Load from keyed Property");
4180 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004181 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004182 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004183 ASSERT(var == NULL || var->is_global());
4184 RelocInfo::Mode rmode = (var == NULL)
4185 ? RelocInfo::CODE_TARGET
4186 : RelocInfo::CODE_TARGET_CONTEXT;
4187 Result answer = frame->CallCodeObject(ic, rmode, 0);
4188 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004189 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004190 }
4191
4192 default:
4193 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004194 }
4195}
4196
4197
ager@chromium.org7c537e22008-10-16 08:43:32 +00004198void Reference::SetValue(InitState init_state) {
4199 ASSERT(!is_illegal());
4200 ASSERT(!cgen_->has_cc());
4201 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004202 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004203 Property* property = expression_->AsProperty();
4204 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004205 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004206 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004207
ager@chromium.org7c537e22008-10-16 08:43:32 +00004208 switch (type_) {
4209 case SLOT: {
4210 Comment cmnt(masm, "[ Store to Slot");
4211 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4212 ASSERT(slot != NULL);
4213 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004214 ASSERT(slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004215
ager@chromium.org7c537e22008-10-16 08:43:32 +00004216 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004217 frame->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004218 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004219 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004220
ager@chromium.org7c537e22008-10-16 08:43:32 +00004221 if (init_state == CONST_INIT) {
4222 // Same as the case for a normal store, but ignores attribute
4223 // (e.g. READ_ONLY) of context slot so that we can initialize
4224 // const properties (introduced via eval("const foo = (some
4225 // expr);")). Also, uses the current function context instead of
4226 // the top context.
4227 //
4228 // Note that we must declare the foo upon entry of eval(), via a
4229 // context slot declaration, but we cannot initialize it at the
4230 // same time, because the const declaration may be at the end of
4231 // the eval code (sigh...) and the const variable may have been
4232 // used before (where its value is 'undefined'). Thus, we can only
4233 // do the initialization when we actually encounter the expression
4234 // and when the expression operands are defined and valid, and
4235 // thus we need the split into 2 operations: declaration of the
4236 // context slot followed by initialization.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004237 frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004238 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004239 frame->CallRuntime(Runtime::kStoreContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004240 }
4241 // Storing a variable must keep the (new) value on the expression
4242 // stack. This is necessary for compiling assignment expressions.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004243 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004244
ager@chromium.org7c537e22008-10-16 08:43:32 +00004245 } else {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004246 ASSERT(!slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004247
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004248 JumpTarget exit(cgen_);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004249 if (init_state == CONST_INIT) {
4250 ASSERT(slot->var()->mode() == Variable::CONST);
4251 // Only the first const initialization must be executed (the slot
4252 // still contains 'the hole' value). When the assignment is
4253 // executed, the code is identical to a normal store (see below).
4254 Comment cmnt(masm, "[ Init const");
4255 __ ldr(r2, cgen_->SlotOperand(slot, r2));
4256 __ cmp(r2, Operand(Factory::the_hole_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004257 exit.Branch(ne);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004258 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004259
ager@chromium.org7c537e22008-10-16 08:43:32 +00004260 // We must execute the store. Storing a variable must keep the
4261 // (new) value on the stack. This is necessary for compiling
4262 // assignment expressions.
4263 //
4264 // Note: We will reach here even with slot->var()->mode() ==
4265 // Variable::CONST because of const declarations which will
4266 // initialize consts to 'the hole' value and by doing so, end up
4267 // calling this code. r2 may be loaded with context; used below in
4268 // RecordWrite.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004269 frame->EmitPop(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004270 __ str(r0, cgen_->SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004271 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004272 if (slot->type() == Slot::CONTEXT) {
4273 // Skip write barrier if the written value is a smi.
4274 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004275 exit.Branch(eq);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004276 // r2 is loaded with context when calling SlotOperand above.
4277 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
4278 __ mov(r3, Operand(offset));
4279 __ RecordWrite(r2, r3, r1);
4280 }
4281 // If we definitely did not jump over the assignment, we do not need
4282 // to bind the exit label. Doing so can defeat peephole
4283 // optimization.
4284 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004285 exit.Bind();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004286 }
4287 }
4288 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004289 }
4290
ager@chromium.org7c537e22008-10-16 08:43:32 +00004291 case NAMED: {
4292 Comment cmnt(masm, "[ Store to named Property");
4293 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004294 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004295 Handle<String> name(GetName());
4296
4297 Result value = cgen_->allocator()->Allocate(r0);
4298 ASSERT(value.is_valid());
4299 frame->EmitPop(value.reg());
4300
4301 // Setup the name register.
4302 Result property_name = cgen_->allocator()->Allocate(r2);
4303 ASSERT(property_name.is_valid());
4304 __ mov(property_name.reg(), Operand(name));
4305 Result answer = frame->CallCodeObject(ic,
4306 RelocInfo::CODE_TARGET,
4307 &value,
4308 &property_name,
4309 0);
4310 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004311 break;
4312 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004313
ager@chromium.org7c537e22008-10-16 08:43:32 +00004314 case KEYED: {
4315 Comment cmnt(masm, "[ Store to keyed Property");
4316 Property* property = expression_->AsProperty();
4317 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004318 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004319
4320 // Call IC code.
4321 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
4322 // TODO(1222589): Make the IC grab the values from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004323 Result value = cgen_->allocator()->Allocate(r0);
4324 ASSERT(value.is_valid());
4325 frame->EmitPop(value.reg()); // value
4326 Result result =
4327 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0);
4328 frame->EmitPush(result.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004329 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004330 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004331
4332 default:
4333 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004334 }
4335}
4336
4337
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004338static void HandleBinaryOpSlowCases(MacroAssembler* masm,
4339 Label* not_smi,
4340 const Builtins::JavaScript& builtin,
4341 Token::Value operation,
4342 int swi_number,
4343 OverwriteMode mode) {
4344 Label slow;
4345 if (mode == NO_OVERWRITE) {
4346 __ bind(not_smi);
4347 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004348 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004349 __ push(r1);
4350 __ push(r0);
4351 __ mov(r0, Operand(1)); // Set number of arguments.
4352 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004353
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004354 // Could it be a double-double op? If we already have a place to put
4355 // the answer then we can do the op and skip the builtin and runtime call.
4356 if (mode != NO_OVERWRITE) {
4357 __ bind(not_smi);
4358 __ tst(r0, Operand(kSmiTagMask));
4359 __ b(eq, &slow); // We can't handle a Smi-double combination yet.
4360 __ tst(r1, Operand(kSmiTagMask));
4361 __ b(eq, &slow); // We can't handle a Smi-double combination yet.
4362 // Get map of r0 into r2.
4363 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4364 // Get type of r0 into r3.
4365 __ ldrb(r3, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4366 __ cmp(r3, Operand(HEAP_NUMBER_TYPE));
4367 __ b(ne, &slow);
4368 // Get type of r1 into r3.
4369 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
4370 // Check they are both the same map (heap number map).
4371 __ cmp(r2, r3);
4372 __ b(ne, &slow);
4373 // Both are doubles.
4374 // Calling convention says that second double is in r2 and r3.
4375 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
4376 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4377 __ push(lr);
4378 if (mode == OVERWRITE_LEFT) {
4379 __ push(r1);
4380 } else {
4381 __ push(r0);
4382 }
4383 // Calling convention says that first double is in r0 and r1.
4384 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
4385 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4386 // Call C routine that may not cause GC or other trouble.
4387 __ mov(r5, Operand(ExternalReference::double_fp_operation(operation)));
4388#if !defined(__arm__)
4389 // Notify the simulator that we are calling an add routine in C.
4390 __ swi(swi_number);
4391#else
4392 // Actually call the add routine written in C.
4393 __ Call(r5);
4394#endif
4395 // Store answer in the overwritable heap number.
4396 __ pop(r4);
4397#if !defined(__ARM_EABI__) && defined(__arm__)
4398 // Double returned in fp coprocessor register 0 and 1, encoded as register
4399 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
4400 // substract the tag from r4.
4401 __ sub(r5, r4, Operand(kHeapObjectTag));
4402 __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset));
4403#else
4404 // Double returned in fp coprocessor register 0 and 1.
4405 __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset));
4406 __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + kPointerSize));
4407#endif
4408 __ mov(r0, Operand(r4));
4409 // And we are done.
4410 __ pop(pc);
4411 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004412}
4413
4414
4415void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
4416 // r1 : x
4417 // r0 : y
4418 // result : r0
4419
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004420 // All ops need to know whether we are dealing with two Smis. Set up r2 to
4421 // tell us that.
4422 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
4423
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004424 switch (op_) {
4425 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004426 Label not_smi;
4427 // Fast path.
4428 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004429 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004430 __ b(ne, &not_smi);
4431 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
4432 // Return if no overflow.
4433 __ Ret(vc);
4434 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
4435
4436 HandleBinaryOpSlowCases(masm,
4437 &not_smi,
4438 Builtins::ADD,
4439 Token::ADD,
4440 assembler::arm::simulator_fp_add,
4441 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004442 break;
4443 }
4444
4445 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004446 Label not_smi;
4447 // Fast path.
4448 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004449 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004450 __ b(ne, &not_smi);
4451 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
4452 // Return if no overflow.
4453 __ Ret(vc);
4454 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
4455
4456 HandleBinaryOpSlowCases(masm,
4457 &not_smi,
4458 Builtins::SUB,
4459 Token::SUB,
4460 assembler::arm::simulator_fp_sub,
4461 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004462 break;
4463 }
4464
4465 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004466 Label not_smi, slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004467 ASSERT(kSmiTag == 0); // adjust code below
4468 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004469 __ b(ne, &not_smi);
4470 // Remove tag from one operand (but keep sign), so that result is Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004471 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004472 // Do multiplication
4473 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1.
4474 // Go slow on overflows (overflow bit is not set).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004475 __ mov(ip, Operand(r3, ASR, 31));
4476 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
4477 __ b(ne, &slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004478 // Go slow on zero result to handle -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004479 __ tst(r3, Operand(r3));
4480 __ mov(r0, Operand(r3), LeaveCC, ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004481 __ Ret(ne);
4482 // Slow case.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004483 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004484
4485 HandleBinaryOpSlowCases(masm,
4486 &not_smi,
4487 Builtins::MUL,
4488 Token::MUL,
4489 assembler::arm::simulator_fp_mul,
4490 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004491 break;
4492 }
4493
4494 case Token::BIT_OR:
4495 case Token::BIT_AND:
4496 case Token::BIT_XOR: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004497 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004498 ASSERT(kSmiTag == 0); // adjust code below
4499 __ tst(r2, Operand(kSmiTagMask));
4500 __ b(ne, &slow);
4501 switch (op_) {
4502 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
4503 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
4504 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
4505 default: UNREACHABLE();
4506 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004507 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004508 __ bind(&slow);
4509 __ push(r1); // restore stack
4510 __ push(r0);
4511 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
4512 switch (op_) {
4513 case Token::BIT_OR:
4514 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
4515 break;
4516 case Token::BIT_AND:
4517 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
4518 break;
4519 case Token::BIT_XOR:
4520 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
4521 break;
4522 default:
4523 UNREACHABLE();
4524 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004525 break;
4526 }
4527
4528 case Token::SHL:
4529 case Token::SHR:
4530 case Token::SAR: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004531 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004532 ASSERT(kSmiTag == 0); // adjust code below
4533 __ tst(r2, Operand(kSmiTagMask));
4534 __ b(ne, &slow);
4535 // remove tags from operands (but keep sign)
4536 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
4537 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
4538 // use only the 5 least significant bits of the shift count
4539 __ and_(r2, r2, Operand(0x1f));
4540 // perform operation
4541 switch (op_) {
4542 case Token::SAR:
4543 __ mov(r3, Operand(r3, ASR, r2));
4544 // no checks of result necessary
4545 break;
4546
4547 case Token::SHR:
4548 __ mov(r3, Operand(r3, LSR, r2));
4549 // check that the *unsigned* result fits in a smi
4550 // neither of the two high-order bits can be set:
4551 // - 0x80000000: high bit would be lost when smi tagging
4552 // - 0x40000000: this number would convert to negative when
4553 // smi tagging these two cases can only happen with shifts
4554 // by 0 or 1 when handed a valid smi
4555 __ and_(r2, r3, Operand(0xc0000000), SetCC);
4556 __ b(ne, &slow);
4557 break;
4558
4559 case Token::SHL:
4560 __ mov(r3, Operand(r3, LSL, r2));
4561 // check that the *signed* result fits in a smi
4562 __ add(r2, r3, Operand(0x40000000), SetCC);
4563 __ b(mi, &slow);
4564 break;
4565
4566 default: UNREACHABLE();
4567 }
4568 // tag result and store it in r0
4569 ASSERT(kSmiTag == 0); // adjust code below
4570 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004571 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004572 // slow case
4573 __ bind(&slow);
4574 __ push(r1); // restore stack
4575 __ push(r0);
4576 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
4577 switch (op_) {
4578 case Token::SAR: __ InvokeBuiltin(Builtins::SAR, JUMP_JS); break;
4579 case Token::SHR: __ InvokeBuiltin(Builtins::SHR, JUMP_JS); break;
4580 case Token::SHL: __ InvokeBuiltin(Builtins::SHL, JUMP_JS); break;
4581 default: UNREACHABLE();
4582 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004583 break;
4584 }
4585
4586 default: UNREACHABLE();
4587 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004588 // This code should be unreachable.
4589 __ stop("Unreachable");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004590}
4591
4592
4593void StackCheckStub::Generate(MacroAssembler* masm) {
4594 Label within_limit;
4595 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
4596 __ ldr(ip, MemOperand(ip));
4597 __ cmp(sp, Operand(ip));
4598 __ b(hs, &within_limit);
4599 // Do tail-call to runtime routine.
4600 __ push(r0);
4601 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
4602 __ bind(&within_limit);
4603
4604 __ StubReturn(1);
4605}
4606
4607
4608void UnarySubStub::Generate(MacroAssembler* masm) {
4609 Label undo;
4610 Label slow;
4611 Label done;
4612
4613 // Enter runtime system if the value is not a smi.
4614 __ tst(r0, Operand(kSmiTagMask));
4615 __ b(ne, &slow);
4616
4617 // Enter runtime system if the value of the expression is zero
4618 // to make sure that we switch between 0 and -0.
4619 __ cmp(r0, Operand(0));
4620 __ b(eq, &slow);
4621
4622 // The value of the expression is a smi that is not zero. Try
4623 // optimistic subtraction '0 - value'.
4624 __ rsb(r1, r0, Operand(0), SetCC);
4625 __ b(vs, &slow);
4626
4627 // If result is a smi we are done.
4628 __ tst(r1, Operand(kSmiTagMask));
4629 __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result
4630 __ b(eq, &done);
4631
4632 // Enter runtime system.
4633 __ bind(&slow);
4634 __ push(r0);
4635 __ mov(r0, Operand(0)); // set number of arguments
4636 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
4637
4638 __ bind(&done);
4639 __ StubReturn(1);
4640}
4641
4642
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004643void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
4644 // r0 holds exception
4645 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
4646 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
4647 __ ldr(sp, MemOperand(r3));
4648 __ pop(r2); // pop next in chain
4649 __ str(r2, MemOperand(r3));
4650 // restore parameter- and frame-pointer and pop state.
4651 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
4652 // Before returning we restore the context from the frame pointer if not NULL.
4653 // The frame pointer is NULL in the exception handler of a JS entry frame.
4654 __ cmp(fp, Operand(0));
4655 // Set cp to NULL if fp is NULL.
4656 __ mov(cp, Operand(0), LeaveCC, eq);
4657 // Restore cp otherwise.
4658 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004659#ifdef DEBUG
4660 if (FLAG_debug_code) {
4661 __ mov(lr, Operand(pc));
4662 }
4663#endif
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004664 __ pop(pc);
4665}
4666
4667
4668void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
4669 // Fetch top stack handler.
4670 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
4671 __ ldr(r3, MemOperand(r3));
4672
4673 // Unwind the handlers until the ENTRY handler is found.
4674 Label loop, done;
4675 __ bind(&loop);
4676 // Load the type of the current stack handler.
4677 const int kStateOffset = StackHandlerConstants::kAddressDisplacement +
4678 StackHandlerConstants::kStateOffset;
4679 __ ldr(r2, MemOperand(r3, kStateOffset));
4680 __ cmp(r2, Operand(StackHandler::ENTRY));
4681 __ b(eq, &done);
4682 // Fetch the next handler in the list.
4683 const int kNextOffset = StackHandlerConstants::kAddressDisplacement +
4684 StackHandlerConstants::kNextOffset;
4685 __ ldr(r3, MemOperand(r3, kNextOffset));
4686 __ jmp(&loop);
4687 __ bind(&done);
4688
4689 // Set the top handler address to next handler past the current ENTRY handler.
4690 __ ldr(r0, MemOperand(r3, kNextOffset));
4691 __ mov(r2, Operand(ExternalReference(Top::k_handler_address)));
4692 __ str(r0, MemOperand(r2));
4693
4694 // Set external caught exception to false.
4695 __ mov(r0, Operand(false));
4696 ExternalReference external_caught(Top::k_external_caught_exception_address);
4697 __ mov(r2, Operand(external_caught));
4698 __ str(r0, MemOperand(r2));
4699
4700 // Set pending exception and r0 to out of memory exception.
4701 Failure* out_of_memory = Failure::OutOfMemoryException();
4702 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
4703 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
4704 __ str(r0, MemOperand(r2));
4705
4706 // Restore the stack to the address of the ENTRY handler
4707 __ mov(sp, Operand(r3));
4708
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004709 // Stack layout at this point. See also PushTryHandler
4710 // r3, sp -> next handler
4711 // state (ENTRY)
4712 // pp
4713 // fp
4714 // lr
4715
4716 // Discard ENTRY state (r2 is not used), and restore parameter-
4717 // and frame-pointer and pop state.
4718 __ ldm(ia_w, sp, r2.bit() | r3.bit() | pp.bit() | fp.bit());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004719 // Before returning we restore the context from the frame pointer if not NULL.
4720 // The frame pointer is NULL in the exception handler of a JS entry frame.
4721 __ cmp(fp, Operand(0));
4722 // Set cp to NULL if fp is NULL.
4723 __ mov(cp, Operand(0), LeaveCC, eq);
4724 // Restore cp otherwise.
4725 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004726#ifdef DEBUG
4727 if (FLAG_debug_code) {
4728 __ mov(lr, Operand(pc));
4729 }
4730#endif
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004731 __ pop(pc);
4732}
4733
4734
4735void CEntryStub::GenerateCore(MacroAssembler* masm,
4736 Label* throw_normal_exception,
4737 Label* throw_out_of_memory_exception,
4738 StackFrame::Type frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004739 bool do_gc,
4740 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004741 // r0: result parameter for PerformGC, if any
4742 // r4: number of arguments including receiver (C callee-saved)
4743 // r5: pointer to builtin function (C callee-saved)
4744 // r6: pointer to the first argument (C callee-saved)
4745
4746 if (do_gc) {
4747 // Passing r0.
4748 __ Call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
4749 }
4750
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004751 ExternalReference scope_depth =
4752 ExternalReference::heap_always_allocate_scope_depth();
4753 if (always_allocate) {
4754 __ mov(r0, Operand(scope_depth));
4755 __ ldr(r1, MemOperand(r0));
4756 __ add(r1, r1, Operand(1));
4757 __ str(r1, MemOperand(r0));
4758 }
4759
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004760 // Call C built-in.
4761 // r0 = argc, r1 = argv
4762 __ mov(r0, Operand(r4));
4763 __ mov(r1, Operand(r6));
4764
4765 // TODO(1242173): To let the GC traverse the return address of the exit
4766 // frames, we need to know where the return address is. Right now,
4767 // we push it on the stack to be able to find it again, but we never
4768 // restore from it in case of changes, which makes it impossible to
4769 // support moving the C entry code stub. This should be fixed, but currently
4770 // this is OK because the CEntryStub gets generated so early in the V8 boot
4771 // sequence that it is not moving ever.
4772 __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
4773 __ push(lr);
4774#if !defined(__arm__)
4775 // Notify the simulator of the transition to C code.
4776 __ swi(assembler::arm::call_rt_r5);
4777#else /* !defined(__arm__) */
ager@chromium.org41826e72009-03-30 13:30:57 +00004778 __ Jump(r5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004779#endif /* !defined(__arm__) */
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004780
4781 if (always_allocate) {
4782 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
4783 // though (contain the result).
4784 __ mov(r2, Operand(scope_depth));
4785 __ ldr(r3, MemOperand(r2));
4786 __ sub(r3, r3, Operand(1));
4787 __ str(r3, MemOperand(r2));
4788 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004789
4790 // check for failure result
4791 Label failure_returned;
4792 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4793 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
4794 __ add(r2, r0, Operand(1));
4795 __ tst(r2, Operand(kFailureTagMask));
4796 __ b(eq, &failure_returned);
4797
4798 // Exit C frame and return.
4799 // r0:r1: result
4800 // sp: stack pointer
4801 // fp: frame pointer
4802 // pp: caller's parameter pointer pp (restored as C callee-saved)
4803 __ LeaveExitFrame(frame_type);
4804
4805 // check if we should retry or throw exception
4806 Label retry;
4807 __ bind(&failure_returned);
4808 ASSERT(Failure::RETRY_AFTER_GC == 0);
4809 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
4810 __ b(eq, &retry);
4811
4812 Label continue_exception;
4813 // If the returned failure is EXCEPTION then promote Top::pending_exception().
4814 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
4815 __ b(ne, &continue_exception);
4816
4817 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00004818 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004819 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00004820 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004821 __ ldr(r0, MemOperand(ip));
4822 __ str(r3, MemOperand(ip));
4823
4824 __ bind(&continue_exception);
4825 // Special handling of out of memory exception.
4826 Failure* out_of_memory = Failure::OutOfMemoryException();
4827 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
4828 __ b(eq, throw_out_of_memory_exception);
4829
4830 // Handle normal exception.
4831 __ jmp(throw_normal_exception);
4832
4833 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
4834}
4835
4836
4837void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
4838 // Called from JavaScript; parameters are on stack as if calling JS function
4839 // r0: number of arguments including receiver
4840 // r1: pointer to builtin function
4841 // fp: frame pointer (restored after C call)
4842 // sp: stack pointer (restored as callee's pp after C call)
4843 // cp: current context (C callee-saved)
4844 // pp: caller's parameter pointer pp (C callee-saved)
4845
4846 // NOTE: Invocations of builtins may return failure objects
4847 // instead of a proper result. The builtin entry handles
4848 // this by performing a garbage collection and retrying the
4849 // builtin once.
4850
4851 StackFrame::Type frame_type = is_debug_break
4852 ? StackFrame::EXIT_DEBUG
4853 : StackFrame::EXIT;
4854
4855 // Enter the exit frame that transitions from JavaScript to C++.
4856 __ EnterExitFrame(frame_type);
4857
4858 // r4: number of arguments (C callee-saved)
4859 // r5: pointer to builtin function (C callee-saved)
4860 // r6: pointer to first argument (C callee-saved)
4861
4862 Label throw_out_of_memory_exception;
4863 Label throw_normal_exception;
4864
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004865 // Call into the runtime system. Collect garbage before the call if
4866 // running with --gc-greedy set.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004867 if (FLAG_gc_greedy) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004868 Failure* failure = Failure::RetryAfterGC(0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004869 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
4870 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004871 GenerateCore(masm, &throw_normal_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004872 &throw_out_of_memory_exception,
4873 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004874 FLAG_gc_greedy,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004875 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004876
4877 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004878 GenerateCore(masm,
4879 &throw_normal_exception,
4880 &throw_out_of_memory_exception,
4881 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004882 true,
4883 false);
4884
4885 // Do full GC and retry runtime call one final time.
4886 Failure* failure = Failure::InternalError();
4887 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
4888 GenerateCore(masm,
4889 &throw_normal_exception,
4890 &throw_out_of_memory_exception,
4891 frame_type,
4892 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004893 true);
4894
4895 __ bind(&throw_out_of_memory_exception);
4896 GenerateThrowOutOfMemory(masm);
4897 // control flow for generated will not return.
4898
4899 __ bind(&throw_normal_exception);
4900 GenerateThrowTOS(masm);
4901}
4902
4903
4904void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4905 // r0: code entry
4906 // r1: function
4907 // r2: receiver
4908 // r3: argc
4909 // [sp+0]: argv
4910
4911 Label invoke, exit;
4912
4913 // Called from C, so do not pop argc and args on exit (preserve sp)
4914 // No need to save register-passed args
4915 // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr
4916 __ stm(db_w, sp, kCalleeSaved | lr.bit());
4917
4918 // Get address of argv, see stm above.
4919 // r0: code entry
4920 // r1: function
4921 // r2: receiver
4922 // r3: argc
4923 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
4924 __ ldr(r4, MemOperand(r4)); // argv
4925
4926 // Push a frame with special values setup to mark it as an entry frame.
4927 // r0: code entry
4928 // r1: function
4929 // r2: receiver
4930 // r3: argc
4931 // r4: argv
4932 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4933 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
4934 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
4935 __ mov(r6, Operand(Smi::FromInt(marker)));
4936 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
4937 __ ldr(r5, MemOperand(r5));
4938 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
4939
4940 // Setup frame pointer for the frame to be pushed.
4941 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
4942
4943 // Call a faked try-block that does the invoke.
4944 __ bl(&invoke);
4945
4946 // Caught exception: Store result (exception) in the pending
4947 // exception field in the JSEnv and return a failure sentinel.
4948 // Coming in here the fp will be invalid because the PushTryHandler below
4949 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00004950 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004951 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004952 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004953 __ b(&exit);
4954
4955 // Invoke: Link this frame into the handler chain.
4956 __ bind(&invoke);
4957 // Must preserve r0-r4, r5-r7 are available.
4958 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4959 // If an exception not caught by another handler occurs, this handler returns
4960 // control to the code after the bl(&invoke) above, which restores all
4961 // kCalleeSaved registers (including cp, pp and fp) to their saved values
4962 // before returning a failure to C.
4963
4964 // Clear any pending exceptions.
4965 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
4966 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00004967 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004968 __ str(r5, MemOperand(ip));
4969
4970 // Invoke the function by calling through JS entry trampoline builtin.
4971 // Notice that we cannot store a reference to the trampoline code directly in
4972 // this stub, because runtime stubs are not traversed when doing GC.
4973
4974 // Expected registers by Builtins::JSEntryTrampoline
4975 // r0: code entry
4976 // r1: function
4977 // r2: receiver
4978 // r3: argc
4979 // r4: argv
4980 if (is_construct) {
4981 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
4982 __ mov(ip, Operand(construct_entry));
4983 } else {
4984 ExternalReference entry(Builtins::JSEntryTrampoline);
4985 __ mov(ip, Operand(entry));
4986 }
4987 __ ldr(ip, MemOperand(ip)); // deref address
4988
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004989 // Branch and link to JSEntryTrampoline. We don't use the double underscore
4990 // macro for the add instruction because we don't want the coverage tool
4991 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004992 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004993 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004994
4995 // Unlink this frame from the handler chain. When reading the
4996 // address of the next handler, there is no need to use the address
4997 // displacement since the current stack pointer (sp) points directly
4998 // to the stack handler.
4999 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
5000 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
5001 __ str(r3, MemOperand(ip));
5002 // No need to restore registers
5003 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
5004
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005005
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005006 __ bind(&exit); // r0 holds result
5007 // Restore the top frame descriptors from the stack.
5008 __ pop(r3);
5009 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
5010 __ str(r3, MemOperand(ip));
5011
5012 // Reset the stack to the callee saved registers.
5013 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
5014
5015 // Restore callee-saved registers and return.
5016#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005017 if (FLAG_debug_code) {
5018 __ mov(lr, Operand(pc));
5019 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005020#endif
5021 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
5022}
5023
5024
ager@chromium.org7c537e22008-10-16 08:43:32 +00005025void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005026 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005027 Label adaptor;
5028 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5029 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5030 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
ager@chromium.org7c537e22008-10-16 08:43:32 +00005031 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005032
ager@chromium.org7c537e22008-10-16 08:43:32 +00005033 // Nothing to do: The formal number of parameters has already been
5034 // passed in register r0 by calling function. Just return it.
5035 __ mov(pc, lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005036
ager@chromium.org7c537e22008-10-16 08:43:32 +00005037 // Arguments adaptor case: Read the arguments length from the
5038 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005039 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005040 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +00005041 __ mov(pc, lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005042}
5043
5044
ager@chromium.org7c537e22008-10-16 08:43:32 +00005045void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
5046 // The displacement is the offset of the last parameter (if any)
5047 // relative to the frame pointer.
5048 static const int kDisplacement =
5049 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005050
ager@chromium.org7c537e22008-10-16 08:43:32 +00005051 // Check that the key is a smi.
5052 Label slow;
5053 __ tst(r1, Operand(kSmiTagMask));
5054 __ b(ne, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005055
ager@chromium.org7c537e22008-10-16 08:43:32 +00005056 // Check if the calling frame is an arguments adaptor frame.
5057 Label adaptor;
5058 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5059 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5060 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
5061 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005062
ager@chromium.org7c537e22008-10-16 08:43:32 +00005063 // Check index against formal parameters count limit passed in
5064 // through register eax. Use unsigned comparison to get negative
5065 // check for free.
5066 __ cmp(r1, r0);
5067 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005068
ager@chromium.org7c537e22008-10-16 08:43:32 +00005069 // Read the argument from the stack and return it.
5070 __ sub(r3, r0, r1);
5071 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
5072 __ ldr(r0, MemOperand(r3, kDisplacement));
5073 __ mov(pc, lr);
5074
5075 // Arguments adaptor case: Check index against actual arguments
5076 // limit found in the arguments adaptor frame. Use unsigned
5077 // comparison to get negative check for free.
5078 __ bind(&adaptor);
5079 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
5080 __ cmp(r1, r0);
5081 __ b(cs, &slow);
5082
5083 // Read the argument from the adaptor frame and return it.
5084 __ sub(r3, r0, r1);
5085 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
5086 __ ldr(r0, MemOperand(r3, kDisplacement));
5087 __ mov(pc, lr);
5088
5089 // Slow-case: Handle non-smi or out-of-bounds access to arguments
5090 // by calling the runtime system.
5091 __ bind(&slow);
5092 __ push(r1);
5093 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
5094}
5095
5096
5097void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
5098 // Check if the calling frame is an arguments adaptor frame.
5099 Label runtime;
5100 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5101 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5102 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
5103 __ b(ne, &runtime);
5104
5105 // Patch the arguments.length and the parameters pointer.
5106 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
5107 __ str(r0, MemOperand(sp, 0 * kPointerSize));
5108 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
5109 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
5110 __ str(r3, MemOperand(sp, 1 * kPointerSize));
5111
5112 // Do the runtime call to allocate the arguments object.
5113 __ bind(&runtime);
5114 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005115}
5116
5117
5118void CallFunctionStub::Generate(MacroAssembler* masm) {
5119 Label slow;
5120 // Get the function to call from the stack.
5121 // function, receiver [, arguments]
5122 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
5123
5124 // Check that the function is really a JavaScript function.
5125 // r1: pushed function (to be verified)
5126 __ tst(r1, Operand(kSmiTagMask));
5127 __ b(eq, &slow);
5128 // Get the map of the function object.
5129 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
5130 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
5131 __ cmp(r2, Operand(JS_FUNCTION_TYPE));
5132 __ b(ne, &slow);
5133
5134 // Fast-case: Invoke the function now.
5135 // r1: pushed function
5136 ParameterCount actual(argc_);
5137 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
5138
5139 // Slow-case: Non-function called.
5140 __ bind(&slow);
5141 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005142 __ mov(r2, Operand(0));
5143 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
5144 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
5145 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005146}
5147
5148
5149#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005150
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005151} } // namespace v8::internal