blob: 3e5cc46c4d57fff47782b3ecc6c1ca6e7d166472 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 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"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033#include "scopes.h"
34#include "runtime.h"
35
36namespace v8 { namespace internal {
37
ager@chromium.org3bf7b912008-11-17 09:09:45 +000038#define __ masm_->
39
40// -------------------------------------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000041// CodeGenState implementation.
42
ager@chromium.org7c537e22008-10-16 08:43:32 +000043CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000044 : owner_(owner),
ager@chromium.org7c537e22008-10-16 08:43:32 +000045 typeof_state_(NOT_INSIDE_TYPEOF),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000046 true_target_(NULL),
47 false_target_(NULL),
48 previous_(NULL) {
49 owner_->set_state(this);
50}
51
52
ager@chromium.org7c537e22008-10-16 08:43:32 +000053CodeGenState::CodeGenState(CodeGenerator* owner,
54 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000055 JumpTarget* true_target,
56 JumpTarget* false_target)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000057 : owner_(owner),
ager@chromium.org7c537e22008-10-16 08:43:32 +000058 typeof_state_(typeof_state),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000059 true_target_(true_target),
60 false_target_(false_target),
61 previous_(owner->state()) {
62 owner_->set_state(this);
63}
64
65
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000066CodeGenState::~CodeGenState() {
67 ASSERT(owner_->state() == this);
68 owner_->set_state(previous_);
69}
70
71
ager@chromium.org3bf7b912008-11-17 09:09:45 +000072// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +000073// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000074
ager@chromium.org7c537e22008-10-16 08:43:32 +000075CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script,
76 bool is_eval)
77 : is_eval_(is_eval),
78 script_(script),
79 deferred_(8),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000080 masm_(new MacroAssembler(NULL, buffer_size)),
81 scope_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +000082 frame_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000083 allocator_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000084 cc_reg_(al),
85 state_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000086 function_return_is_shadowed_(false),
87 in_spilled_code_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000088}
89
90
91// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000092// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000093// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000094// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000095// cp: callee's context
96
ager@chromium.org7c537e22008-10-16 08:43:32 +000097void CodeGenerator::GenCode(FunctionLiteral* fun) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000098 ZoneList<Statement*>* body = fun->body();
99
100 // Initialize state.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000101 ASSERT(scope_ == NULL);
102 scope_ = fun->scope();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000103 ASSERT(allocator_ == NULL);
104 RegisterAllocator register_allocator(this);
105 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000106 ASSERT(frame_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000107 frame_ = new VirtualFrame(this);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000108 cc_reg_ = al;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000109 set_in_spilled_code(false);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000110 {
111 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000113 // Entry:
114 // Stack: receiver, arguments
115 // lr: return address
116 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000117 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000118 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000119 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000120 allocator_->Initialize();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000121 frame_->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000122 // tos: code slot
123#ifdef DEBUG
124 if (strlen(FLAG_stop_at) > 0 &&
125 fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000126 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000127 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000128 }
129#endif
130
131 // Allocate space for locals and initialize them.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000132 frame_->AllocateStackSlots(scope_->num_stack_slots());
133 // Initialize the function return target after the locals are set
134 // up, because it needs the expected frame height from the frame.
135 function_return_.Initialize(this, JumpTarget::BIDIRECTIONAL);
136 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000138 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000139 if (scope_->num_heap_slots() > 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000140 // Allocate local context.
141 // Get outer context and create a new context based on it.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000142 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000143 frame_->EmitPush(r0);
144 frame_->CallRuntime(Runtime::kNewContext, 1); // r0 holds the result
kasper.lund7276f142008-07-30 08:49:36 +0000145
146 if (kDebug) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000147 JumpTarget verified_true(this);
kasper.lund7276f142008-07-30 08:49:36 +0000148 __ cmp(r0, Operand(cp));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000149 verified_true.Branch(eq);
kasper.lund7276f142008-07-30 08:49:36 +0000150 __ stop("NewContext: r0 is expected to be the same as cp");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000151 verified_true.Bind();
kasper.lund7276f142008-07-30 08:49:36 +0000152 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000153 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000154 __ str(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000155 }
156
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000157 // TODO(1241774): Improve this code:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 // 1) only needed if we have a context
159 // 2) no need to recompute context ptr every single time
160 // 3) don't copy parameter operand code from SlotOperand!
161 {
162 Comment cmnt2(masm_, "[ copy context parameters into .context");
163
164 // Note that iteration order is relevant here! If we have the same
165 // parameter twice (e.g., function (x, y, x)), and that parameter
166 // needs to be copied into the context, it must be the last argument
167 // passed to the parameter that needs to be copied. This is a rare
168 // case so we don't check for it, instead we rely on the copying
169 // order: such a parameter is copied repeatedly into the same
170 // context location and thus the last value is what is seen inside
171 // the function.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000172 for (int i = 0; i < scope_->num_parameters(); i++) {
173 Variable* par = scope_->parameter(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174 Slot* slot = par->slot();
175 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000176 ASSERT(!scope_->is_global_scope()); // no parameters in global scope
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000177 __ ldr(r1, frame_->ParameterAt(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000178 // Loads r2 with context; used below in RecordWrite.
179 __ str(r1, SlotOperand(slot, r2));
180 // Load the offset into r3.
181 int slot_offset =
182 FixedArray::kHeaderSize + slot->index() * kPointerSize;
183 __ mov(r3, Operand(slot_offset));
184 __ RecordWrite(r2, r3, r1);
185 }
186 }
187 }
188
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000189 // Store the arguments object. This must happen after context
190 // initialization because the arguments object may be stored in the
191 // context.
192 if (scope_->arguments() != NULL) {
193 ASSERT(scope_->arguments_shadow() != NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000194 Comment cmnt(masm_, "[ allocate arguments object");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000195 { Reference shadow_ref(this, scope_->arguments_shadow());
196 { Reference arguments_ref(this, scope_->arguments());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000197 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000198 __ ldr(r2, frame_->Function());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000199 // The receiver is below the arguments, the return address,
200 // and the frame pointer on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000201 const int kReceiverDisplacement = 2 + scope_->num_parameters();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000202 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000203 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000204 frame_->Adjust(3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000205 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000206 frame_->CallStub(&stub, 3);
207 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000208 arguments_ref.SetValue(NOT_CONST_INIT);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000209 }
ager@chromium.org7c537e22008-10-16 08:43:32 +0000210 shadow_ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000211 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000212 frame_->Drop(); // Value is no longer needed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000213 }
214
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000215 // Generate code to 'execute' declarations and initialize functions
216 // (source elements). In case of an illegal redeclaration we need to
217 // handle that instead of processing the declarations.
218 if (scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000220 scope_->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221 } else {
222 Comment cmnt(masm_, "[ declarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000223 ProcessDeclarations(scope_->declarations());
224 // Bail out if a stack-overflow exception occurred when processing
225 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000226 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000227 }
228
mads.s.ager31e71382008-08-13 09:32:07 +0000229 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000230 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000231 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000232 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000233 CheckStack();
234
235 // Compile the body of the function in a vanilla state. Don't
236 // bother compiling all the code if the scope has an illegal
237 // redeclaration.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000238 if (!scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239 Comment cmnt(masm_, "[ function body");
240#ifdef DEBUG
241 bool is_builtin = Bootstrapper::IsActive();
242 bool should_trace =
243 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000244 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000245 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000246 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000247 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000249 VisitStatementsAndSpill(body);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251 }
252
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000253 // Generate the return sequence if necessary.
254 if (frame_ != NULL || function_return_.is_linked()) {
255 // exit
256 // r0: result
257 // sp: stack pointer
258 // fp: frame pointer
259 // pp: parameter pointer
260 // cp: callee's context
261 __ mov(r0, Operand(Factory::undefined_value()));
mads.s.ager31e71382008-08-13 09:32:07 +0000262
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000263 function_return_.Bind();
264 if (FLAG_trace) {
265 // Push the return value on the stack as the parameter.
266 // Runtime::TraceExit returns the parameter as it is.
267 frame_->EmitPush(r0);
268 frame_->CallRuntime(Runtime::kTraceExit, 1);
269 }
270
271 // Tear down the frame which will restore the caller's frame pointer and
272 // the link register.
273 frame_->Exit();
274
275 __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize));
276 __ mov(pc, lr);
mads.s.ager31e71382008-08-13 09:32:07 +0000277 }
278
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000280 ASSERT(!has_cc());
281 ASSERT(state_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000282 ASSERT(!function_return_is_shadowed_);
283 function_return_.Unuse();
284 DeleteFrame();
285
286 // Process any deferred code using the register allocator.
287 if (HasStackOverflow()) {
288 ClearDeferred();
289 } else {
290 ProcessDeferred();
291 }
292
293 allocator_ = NULL;
294 scope_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000295}
296
297
ager@chromium.org7c537e22008-10-16 08:43:32 +0000298MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
299 // Currently, this assertion will fail if we try to assign to
300 // a constant variable that is constant because it is read-only
301 // (such as the variable referring to a named function expression).
302 // We need to implement assignments to read-only variables.
303 // Ideally, we should do this during AST generation (by converting
304 // such assignments into expression statements); however, in general
305 // we may not be able to make the decision until past AST generation,
306 // that is when the entire program is known.
307 ASSERT(slot != NULL);
308 int index = slot->index();
309 switch (slot->type()) {
310 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000311 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000312
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000313 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000314 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000315
316 case Slot::CONTEXT: {
317 // Follow the context chain if necessary.
318 ASSERT(!tmp.is(cp)); // do not overwrite context register
319 Register context = cp;
320 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000321 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000322 // Load the closure.
323 // (All contexts, even 'with' contexts, have a closure,
324 // and it is the same for all contexts inside a function.
325 // There is no need to go to the function context first.)
326 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
327 // Load the function context (which is the incoming, outer context).
328 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
329 context = tmp;
330 }
331 // We may have a 'with' context now. Get the function context.
332 // (In fact this mov may never be the needed, since the scope analysis
333 // may not permit a direct context access in this case and thus we are
334 // always at a function context. However it is safe to dereference be-
335 // cause the function context of a function context is itself. Before
336 // deleting this mov we should try to create a counter-example first,
337 // though...)
338 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
339 return ContextOperand(tmp, index);
340 }
341
342 default:
343 UNREACHABLE();
344 return MemOperand(r0, 0);
345 }
346}
347
348
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000349MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
350 Slot* slot,
351 Register tmp,
352 Register tmp2,
353 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000354 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000355 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000356
ager@chromium.org381abbb2009-02-25 13:23:22 +0000357 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
358 if (s->num_heap_slots() > 0) {
359 if (s->calls_eval()) {
360 // Check that extension is NULL.
361 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
362 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000363 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000364 }
365 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
366 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
367 context = tmp;
368 }
369 }
370 // Check that last extension is NULL.
371 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
372 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000373 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000374 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000375 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000376}
377
378
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000379// Loads a value on TOS. If it is a boolean value, the result may have been
380// (partially) translated into branches, or it may have set the condition
381// code register. If force_cc is set, the value is forced to set the
382// condition code register and no value is pushed. If the condition code
383// register was set, has_cc() is true and cc_reg_ contains the condition to
384// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000385void CodeGenerator::LoadCondition(Expression* x,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000386 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000387 JumpTarget* true_target,
388 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000389 bool force_cc) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000390 ASSERT(!in_spilled_code());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000391 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000392 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393
ager@chromium.org7c537e22008-10-16 08:43:32 +0000394 { CodeGenState new_state(this, typeof_state, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000395 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000396
397 // If we hit a stack overflow, we may not have actually visited
398 // the expression. In that case, we ensure that we have a
399 // valid-looking frame state because we will continue to generate
400 // code as we unwind the C++ stack.
401 //
402 // It's possible to have both a stack overflow and a valid frame
403 // state (eg, a subexpression overflowed, visiting it returned
404 // with a dummied frame state, and visiting this expression
405 // returned with a normal-looking state).
406 if (HasStackOverflow() &&
407 has_valid_frame() &&
408 !has_cc() &&
409 frame_->height() == original_height) {
410 true_target->Jump();
411 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000412 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000413 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000414 // Convert the TOS value to a boolean in the condition code register.
415 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000416 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000417 ASSERT(!force_cc || !has_valid_frame() || has_cc());
418 ASSERT(!has_valid_frame() ||
419 (has_cc() && frame_->height() == original_height) ||
420 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000421}
422
423
ager@chromium.org7c537e22008-10-16 08:43:32 +0000424void CodeGenerator::Load(Expression* x, TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000425#ifdef DEBUG
426 int original_height = frame_->height();
427#endif
428 ASSERT(!in_spilled_code());
429 JumpTarget true_target(this);
430 JumpTarget false_target(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000431 LoadCondition(x, typeof_state, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000432
433 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000434 // Convert cc_reg_ into a boolean value.
435 JumpTarget loaded(this);
436 JumpTarget materialize_true(this);
437 materialize_true.Branch(cc_reg_);
mads.s.ager31e71382008-08-13 09:32:07 +0000438 __ mov(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000439 frame_->EmitPush(r0);
440 loaded.Jump();
441 materialize_true.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000442 __ mov(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000443 frame_->EmitPush(r0);
444 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000445 cc_reg_ = al;
446 }
447
448 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000449 // We have at least one condition value that has been "translated"
450 // into a branch, thus it needs to be loaded explicitly.
451 JumpTarget loaded(this);
452 if (frame_ != NULL) {
453 loaded.Jump(); // Don't lose the current TOS.
454 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000455 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000456 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000457 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000458 true_target.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000459 __ mov(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000460 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000461 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000462 // If both "true" and "false" need to be loaded jump across the code for
463 // "false".
464 if (both) {
465 loaded.Jump();
466 }
467 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000468 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000469 false_target.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000470 __ mov(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000471 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000472 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000473 // A value is loaded on all paths reaching this point.
474 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000476 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000477 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000478 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479}
480
481
ager@chromium.org7c537e22008-10-16 08:43:32 +0000482void CodeGenerator::LoadGlobal() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000483 VirtualFrame::SpilledScope spilled_scope(this);
mads.s.ager31e71382008-08-13 09:32:07 +0000484 __ ldr(r0, GlobalObject());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000485 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486}
487
488
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000489void CodeGenerator::LoadGlobalReceiver(Register scratch) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000490 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000491 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
492 __ ldr(scratch,
493 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000494 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000495}
496
497
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498// TODO(1241834): Get rid of this function in favor of just using Load, now
ager@chromium.org7c537e22008-10-16 08:43:32 +0000499// that we have the INSIDE_TYPEOF typeof state. => Need to handle global
500// variables w/o reference errors elsewhere.
501void CodeGenerator::LoadTypeofExpression(Expression* x) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000502 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000503 Variable* variable = x->AsVariableProxy()->AsVariable();
504 if (variable != NULL && !variable->is_this() && variable->is_global()) {
505 // NOTE: This is somewhat nasty. We force the compiler to load
506 // the variable as if through '<global>.<variable>' to make sure we
507 // do not get reference errors.
508 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
509 Literal key(variable->name());
510 // TODO(1241834): Fetch the position from the variable instead of using
511 // no position.
ager@chromium.org236ad962008-09-25 09:45:57 +0000512 Property property(&global, &key, RelocInfo::kNoPosition);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000513 LoadAndSpill(&property);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000515 LoadAndSpill(x, INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516 }
517}
518
519
ager@chromium.org7c537e22008-10-16 08:43:32 +0000520Reference::Reference(CodeGenerator* cgen, Expression* expression)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000521 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
522 cgen->LoadReference(this);
523}
524
525
526Reference::~Reference() {
527 cgen_->UnloadReference(this);
528}
529
530
ager@chromium.org7c537e22008-10-16 08:43:32 +0000531void CodeGenerator::LoadReference(Reference* ref) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000532 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000533 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534 Expression* e = ref->expression();
535 Property* property = e->AsProperty();
536 Variable* var = e->AsVariableProxy()->AsVariable();
537
538 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000539 // The expression is either a property or a variable proxy that rewrites
540 // to a property.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000541 LoadAndSpill(property->obj());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000542 // We use a named reference if the key is a literal symbol, unless it is
543 // a string that can be legally parsed as an integer. This is because
544 // otherwise we will not get into the slow case code that handles [] on
545 // String objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000546 Literal* literal = property->key()->AsLiteral();
547 uint32_t dummy;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000548 if (literal != NULL &&
549 literal->handle()->IsSymbol() &&
550 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000551 ref->set_type(Reference::NAMED);
552 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000553 LoadAndSpill(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000554 ref->set_type(Reference::KEYED);
555 }
556 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000557 // The expression is a variable proxy that does not rewrite to a
558 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000560 LoadGlobal();
561 ref->set_type(Reference::NAMED);
562 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000563 ASSERT(var->slot() != NULL);
564 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000565 }
566 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000567 // Anything else is a runtime error.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000568 LoadAndSpill(e);
569 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 }
571}
572
573
ager@chromium.org7c537e22008-10-16 08:43:32 +0000574void CodeGenerator::UnloadReference(Reference* ref) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000575 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000576 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000577 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000579 if (size > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000580 frame_->EmitPop(r0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000581 frame_->Drop(size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000582 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583 }
584}
585
586
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000587// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
588// register to a boolean in the condition code register. The code
589// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000590void CodeGenerator::ToBoolean(JumpTarget* true_target,
591 JumpTarget* false_target) {
592 VirtualFrame::SpilledScope spilled_scope(this);
mads.s.ager31e71382008-08-13 09:32:07 +0000593 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000595 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000596
597 // Fast case checks
598
mads.s.ager31e71382008-08-13 09:32:07 +0000599 // Check if the value is 'false'.
600 __ cmp(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000601 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000602
mads.s.ager31e71382008-08-13 09:32:07 +0000603 // Check if the value is 'true'.
604 __ cmp(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000605 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000606
mads.s.ager31e71382008-08-13 09:32:07 +0000607 // Check if the value is 'undefined'.
608 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000609 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000610
mads.s.ager31e71382008-08-13 09:32:07 +0000611 // Check if the value is a smi.
612 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000613 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000614 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000615 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000616
617 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000618 frame_->EmitPush(r0);
619 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000620 // Convert the result (r0) to a condition code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000621 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622
623 cc_reg_ = ne;
624}
625
626
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627class GetPropertyStub : public CodeStub {
628 public:
629 GetPropertyStub() { }
630
631 private:
632 Major MajorKey() { return GetProperty; }
633 int MinorKey() { return 0; }
634 void Generate(MacroAssembler* masm);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000635};
636
637
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000638class SetPropertyStub : public CodeStub {
639 public:
640 SetPropertyStub() { }
641
642 private:
643 Major MajorKey() { return SetProperty; }
644 int MinorKey() { return 0; }
645 void Generate(MacroAssembler* masm);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000646};
647
648
kasper.lund7276f142008-07-30 08:49:36 +0000649class GenericBinaryOpStub : public CodeStub {
650 public:
651 explicit GenericBinaryOpStub(Token::Value op) : op_(op) { }
652
653 private:
654 Token::Value op_;
655
656 Major MajorKey() { return GenericBinaryOp; }
657 int MinorKey() { return static_cast<int>(op_); }
658 void Generate(MacroAssembler* masm);
659
660 const char* GetName() {
661 switch (op_) {
662 case Token::ADD: return "GenericBinaryOpStub_ADD";
663 case Token::SUB: return "GenericBinaryOpStub_SUB";
664 case Token::MUL: return "GenericBinaryOpStub_MUL";
665 case Token::DIV: return "GenericBinaryOpStub_DIV";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000666 case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR";
667 case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND";
668 case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR";
669 case Token::SAR: return "GenericBinaryOpStub_SAR";
670 case Token::SHL: return "GenericBinaryOpStub_SHL";
671 case Token::SHR: return "GenericBinaryOpStub_SHR";
kasper.lund7276f142008-07-30 08:49:36 +0000672 default: return "GenericBinaryOpStub";
673 }
674 }
675
676#ifdef DEBUG
677 void Print() { PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_)); }
678#endif
679};
680
681
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000682class InvokeBuiltinStub : public CodeStub {
683 public:
684 enum Kind { Inc, Dec, ToNumber };
685 InvokeBuiltinStub(Kind kind, int argc) : kind_(kind), argc_(argc) { }
686
687 private:
688 Kind kind_;
689 int argc_;
690
691 Major MajorKey() { return InvokeBuiltin; }
692 int MinorKey() { return (argc_ << 3) | static_cast<int>(kind_); }
693 void Generate(MacroAssembler* masm);
694
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000695#ifdef DEBUG
696 void Print() {
697 PrintF("InvokeBuiltinStub (kind %d, argc, %d)\n",
698 static_cast<int>(kind_),
699 argc_);
700 }
701#endif
702};
703
704
ager@chromium.org7c537e22008-10-16 08:43:32 +0000705void CodeGenerator::GenericBinaryOperation(Token::Value op) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000706 VirtualFrame::SpilledScope spilled_scope(this);
mads.s.ager31e71382008-08-13 09:32:07 +0000707 // sp[0] : y
708 // sp[1] : x
709 // result : r0
710
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000711 // Stub is entered with a call: 'return address' is in lr.
712 switch (op) {
713 case Token::ADD: // fall through.
714 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000715 case Token::MUL:
716 case Token::BIT_OR:
717 case Token::BIT_AND:
718 case Token::BIT_XOR:
719 case Token::SHL:
720 case Token::SHR:
721 case Token::SAR: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000722 frame_->EmitPop(r0); // r0 : y
723 frame_->EmitPop(r1); // r1 : x
kasper.lund7276f142008-07-30 08:49:36 +0000724 GenericBinaryOpStub stub(op);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000725 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000726 break;
727 }
728
729 case Token::DIV: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000730 Result arg_count = allocator_->Allocate(r0);
731 ASSERT(arg_count.is_valid());
732 __ mov(arg_count.reg(), Operand(1));
733 frame_->InvokeBuiltin(Builtins::DIV, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000734 break;
735 }
736
737 case Token::MOD: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000738 Result arg_count = allocator_->Allocate(r0);
739 ASSERT(arg_count.is_valid());
740 __ mov(arg_count.reg(), Operand(1));
741 frame_->InvokeBuiltin(Builtins::MOD, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000742 break;
743 }
744
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000745 case Token::COMMA:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000746 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000747 // simply discard left value
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000748 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000749 break;
750
751 default:
752 // Other cases should have been handled before this point.
753 UNREACHABLE();
754 break;
755 }
756}
757
758
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000759class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000760 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000761 DeferredInlineSmiOperation(CodeGenerator* generator,
762 Token::Value op,
763 int value,
764 bool reversed)
765 : DeferredCode(generator),
766 op_(op),
767 value_(value),
768 reversed_(reversed) {
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_;
778};
779
780
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000781void DeferredInlineSmiOperation::Generate() {
782 enter()->Bind();
783 VirtualFrame::SpilledScope spilled_scope(generator());
784
785 switch (op_) {
786 case Token::ADD: {
787 if (reversed_) {
788 // revert optimistic add
789 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
790 __ mov(r1, Operand(Smi::FromInt(value_)));
791 } else {
792 // revert optimistic add
793 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
794 __ mov(r0, Operand(Smi::FromInt(value_)));
795 }
796 break;
797 }
798
799 case Token::SUB: {
800 if (reversed_) {
801 // revert optimistic sub
802 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
803 __ mov(r1, Operand(Smi::FromInt(value_)));
804 } else {
805 __ add(r1, r0, Operand(Smi::FromInt(value_)));
806 __ mov(r0, Operand(Smi::FromInt(value_)));
807 }
808 break;
809 }
810
811 case Token::BIT_OR:
812 case Token::BIT_XOR:
813 case Token::BIT_AND: {
814 if (reversed_) {
815 __ mov(r1, Operand(Smi::FromInt(value_)));
816 } else {
817 __ mov(r1, Operand(r0));
818 __ mov(r0, Operand(Smi::FromInt(value_)));
819 }
820 break;
821 }
822
823 case Token::SHL:
824 case Token::SHR:
825 case Token::SAR: {
826 if (!reversed_) {
827 __ mov(r1, Operand(r0));
828 __ mov(r0, Operand(Smi::FromInt(value_)));
829 } else {
830 UNREACHABLE(); // should have been handled in SmiOperation
831 }
832 break;
833 }
834
835 default:
836 // other cases should have been handled before this point.
837 UNREACHABLE();
838 break;
839 }
840
841 GenericBinaryOpStub igostub(op_);
842 Result arg0 = generator()->allocator()->Allocate(r0);
843 ASSERT(arg0.is_valid());
844 Result arg1 = generator()->allocator()->Allocate(r1);
845 ASSERT(arg1.is_valid());
846 generator()->frame()->CallStub(&igostub, &arg0, &arg1, 0);
847 exit_.Jump();
848}
849
850
ager@chromium.org7c537e22008-10-16 08:43:32 +0000851void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000852 Handle<Object> value,
853 bool reversed) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000854 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855 // NOTE: This is an attempt to inline (a bit) more of the code for
856 // some possible smi operations (like + and -) when (at least) one
857 // of the operands is a literal smi. With this optimization, the
858 // performance of the system is increased by ~15%, and the generated
859 // code size is increased by ~1% (measured on a combination of
860 // different benchmarks).
861
mads.s.ager31e71382008-08-13 09:32:07 +0000862 // sp[0] : operand
863
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000864 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000865
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000866 JumpTarget exit(this);
867 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868
869 switch (op) {
870 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000871 DeferredCode* deferred =
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000872 new DeferredInlineSmiOperation(this, op, int_value, reversed);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000874 __ add(r0, r0, Operand(value), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000875 deferred->enter()->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000876 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000877 deferred->enter()->Branch(ne);
878 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000879 break;
880 }
881
882 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000883 DeferredCode* deferred =
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000884 new DeferredInlineSmiOperation(this, op, int_value, reversed);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000885
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886 if (!reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000887 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000888 } else {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000889 __ rsb(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000890 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000891 deferred->enter()->Branch(vs);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000892 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000893 deferred->enter()->Branch(ne);
894 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000895 break;
896 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000898 case Token::BIT_OR:
899 case Token::BIT_XOR:
900 case Token::BIT_AND: {
901 DeferredCode* deferred =
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000902 new DeferredInlineSmiOperation(this, op, int_value, reversed);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000903 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000904 deferred->enter()->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000905 switch (op) {
906 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
907 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
908 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
909 default: UNREACHABLE();
910 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000911 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000912 break;
913 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000914
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000915 case Token::SHL:
916 case Token::SHR:
917 case Token::SAR: {
918 if (reversed) {
919 __ mov(ip, Operand(value));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000920 frame_->EmitPush(ip);
921 frame_->EmitPush(r0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000922 GenericBinaryOperation(op);
923
924 } else {
925 int shift_value = int_value & 0x1f; // least significant 5 bits
926 DeferredCode* deferred =
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000927 new DeferredInlineSmiOperation(this, op, shift_value, false);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000928 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000929 deferred->enter()->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000930 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
931 switch (op) {
932 case Token::SHL: {
933 __ mov(r2, Operand(r2, LSL, shift_value));
934 // check that the *unsigned* result fits in a smi
935 __ add(r3, r2, Operand(0x40000000), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000936 deferred->enter()->Branch(mi);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000937 break;
938 }
939 case Token::SHR: {
940 // LSR by immediate 0 means shifting 32 bits.
941 if (shift_value != 0) {
942 __ mov(r2, Operand(r2, LSR, shift_value));
943 }
944 // check that the *unsigned* result fits in a smi
945 // neither of the two high-order bits can be set:
946 // - 0x80000000: high bit would be lost when smi tagging
947 // - 0x40000000: this number would convert to negative when
948 // smi tagging these two cases can only happen with shifts
949 // by 0 or 1 when handed a valid smi
950 __ and_(r3, r2, Operand(0xc0000000), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000951 deferred->enter()->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000952 break;
953 }
954 case Token::SAR: {
955 if (shift_value != 0) {
956 // ASR by immediate 0 means shifting 32 bits.
957 __ mov(r2, Operand(r2, ASR, shift_value));
958 }
959 break;
960 }
961 default: UNREACHABLE();
962 }
963 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000964 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000965 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000966 break;
967 }
968
969 default:
970 if (!reversed) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000971 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +0000972 __ mov(r0, Operand(value));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000973 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000974 } else {
975 __ mov(ip, Operand(value));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000976 frame_->EmitPush(ip);
977 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000978 }
kasper.lund7276f142008-07-30 08:49:36 +0000979 GenericBinaryOperation(op);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000980 break;
981 }
982
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000983 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984}
985
986
ager@chromium.org7c537e22008-10-16 08:43:32 +0000987void CodeGenerator::Comparison(Condition cc, bool strict) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000988 VirtualFrame::SpilledScope spilled_scope(this);
mads.s.ager31e71382008-08-13 09:32:07 +0000989 // sp[0] : y
990 // sp[1] : x
991 // result : cc register
992
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000993 // Strict only makes sense for equality comparisons.
994 ASSERT(!strict || cc == eq);
995
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000996 JumpTarget exit(this);
997 JumpTarget smi(this);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000998 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
999 if (cc == gt || cc == le) {
1000 cc = ReverseCondition(cc);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001001 frame_->EmitPop(r1);
1002 frame_->EmitPop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001003 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001004 frame_->EmitPop(r0);
1005 frame_->EmitPop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001006 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007 __ orr(r2, r0, Operand(r1));
1008 __ tst(r2, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001009 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001010
1011 // Perform non-smi comparison by runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001012 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001013
1014 // Figure out which native to call and setup the arguments.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001015 Builtins::JavaScript native;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001016 int arg_count = 1;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001017 if (cc == eq) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001018 native = strict ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001019 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001020 native = Builtins::COMPARE;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001021 int ncr; // NaN compare result
1022 if (cc == lt || cc == le) {
1023 ncr = GREATER;
1024 } else {
1025 ASSERT(cc == gt || cc == ge); // remaining cases
1026 ncr = LESS;
1027 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001028 frame_->EmitPush(r0);
1029 arg_count++;
mads.s.ager31e71382008-08-13 09:32:07 +00001030 __ mov(r0, Operand(Smi::FromInt(ncr)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001031 }
1032
1033 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1034 // tagged as a small integer.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001035 frame_->EmitPush(r0);
1036 Result arg_count_register = allocator_->Allocate(r0);
1037 ASSERT(arg_count_register.is_valid());
1038 __ mov(arg_count_register.reg(), Operand(arg_count));
1039 Result result = frame_->InvokeBuiltin(native,
1040 CALL_JS,
1041 &arg_count_register,
1042 arg_count + 1);
1043 __ cmp(result.reg(), Operand(0));
1044 result.Unuse();
1045 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046
1047 // test smi equality by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001048 smi.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001049 __ cmp(r1, Operand(r0));
1050
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001051 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001052 cc_reg_ = cc;
1053}
1054
1055
kasper.lund7276f142008-07-30 08:49:36 +00001056class CallFunctionStub: public CodeStub {
1057 public:
1058 explicit CallFunctionStub(int argc) : argc_(argc) {}
1059
1060 void Generate(MacroAssembler* masm);
1061
1062 private:
1063 int argc_;
1064
kasper.lund7276f142008-07-30 08:49:36 +00001065#if defined(DEBUG)
1066 void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); }
1067#endif // defined(DEBUG)
1068
1069 Major MajorKey() { return CallFunction; }
1070 int MinorKey() { return argc_; }
1071};
1072
1073
mads.s.ager31e71382008-08-13 09:32:07 +00001074// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001075void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001076 int position) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001077 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001078 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001079 int arg_count = args->length();
1080 for (int i = 0; i < arg_count; i++) {
1081 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001082 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001083
kasper.lund7276f142008-07-30 08:49:36 +00001084 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001085 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001086
kasper.lund7276f142008-07-30 08:49:36 +00001087 // Use the shared code stub to call the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001088 CallFunctionStub call_function(arg_count);
1089 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090
1091 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001092 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001093 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001094}
1095
1096
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001097void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
1098 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001099 ASSERT(has_cc());
1100 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001101 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001102 cc_reg_ = al;
1103}
1104
1105
ager@chromium.org7c537e22008-10-16 08:43:32 +00001106void CodeGenerator::CheckStack() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001107 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001108 if (FLAG_check_stack) {
1109 Comment cmnt(masm_, "[ check stack");
1110 StackCheckStub stub;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001111 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001112 }
1113}
1114
1115
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001116void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1117#ifdef DEBUG
1118 int original_height = frame_->height();
1119#endif
1120 VirtualFrame::SpilledScope spilled_scope(this);
1121 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1122 VisitAndSpill(statements->at(i));
1123 }
1124 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1125}
1126
1127
ager@chromium.org7c537e22008-10-16 08:43:32 +00001128void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001129#ifdef DEBUG
1130 int original_height = frame_->height();
1131#endif
1132 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001134 CodeForStatementPosition(node);
1135 node->break_target()->Initialize(this);
1136 VisitStatementsAndSpill(node->statements());
1137 if (node->break_target()->is_linked()) {
1138 node->break_target()->Bind();
1139 }
1140 node->break_target()->Unuse();
1141 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142}
1143
1144
ager@chromium.org7c537e22008-10-16 08:43:32 +00001145void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001146 VirtualFrame::SpilledScope spilled_scope(this);
mads.s.ager31e71382008-08-13 09:32:07 +00001147 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001148 frame_->EmitPush(r0);
1149 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001150 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001151 frame_->EmitPush(r0);
1152 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001153 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001154}
1155
1156
ager@chromium.org7c537e22008-10-16 08:43:32 +00001157void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001158#ifdef DEBUG
1159 int original_height = frame_->height();
1160#endif
1161 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001162 Comment cmnt(masm_, "[ Declaration");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001163 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001164 Variable* var = node->proxy()->var();
1165 ASSERT(var != NULL); // must have been resolved
1166 Slot* slot = var->slot();
1167
1168 // If it was not possible to allocate the variable at compile time,
1169 // we need to "declare" it at runtime to make sure it actually
1170 // exists in the local context.
1171 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1172 // Variables with a "LOOKUP" slot were introduced as non-locals
1173 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001174 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001175 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001176 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001177 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001178 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001179 // Declaration nodes are always declared in only two modes.
1180 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1181 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001182 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001183 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001184 // Push initial value, if any.
1185 // Note: For variables we must not push an initial value (such as
1186 // 'undefined') because we may have a (legal) redeclaration and we
1187 // must not destroy the current value.
1188 if (node->mode() == Variable::CONST) {
mads.s.ager31e71382008-08-13 09:32:07 +00001189 __ mov(r0, Operand(Factory::the_hole_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001190 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001191 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001192 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001193 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001194 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001195 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001196 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001197 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001198 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001199 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001200 return;
1201 }
1202
1203 ASSERT(!var->is_global());
1204
1205 // If we have a function or a constant, we need to initialize the variable.
1206 Expression* val = NULL;
1207 if (node->mode() == Variable::CONST) {
1208 val = new Literal(Factory::the_hole_value());
1209 } else {
1210 val = node->fun(); // NULL if we don't have a function
1211 }
1212
1213 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001214 {
1215 // Set initial value.
1216 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001217 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001218 target.SetValue(NOT_CONST_INIT);
1219 // The reference is removed from the stack (preserving TOS) when
1220 // it goes out of scope.
1221 }
1222 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001223 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001224 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001225 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001226}
1227
1228
ager@chromium.org7c537e22008-10-16 08:43:32 +00001229void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001230#ifdef DEBUG
1231 int original_height = frame_->height();
1232#endif
1233 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001234 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001235 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001236 Expression* expression = node->expression();
1237 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001238 LoadAndSpill(expression);
1239 frame_->Drop();
1240 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001241}
1242
1243
ager@chromium.org7c537e22008-10-16 08:43:32 +00001244void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001245#ifdef DEBUG
1246 int original_height = frame_->height();
1247#endif
1248 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001249 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001250 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001251 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001252 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001253}
1254
1255
ager@chromium.org7c537e22008-10-16 08:43:32 +00001256void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001257#ifdef DEBUG
1258 int original_height = frame_->height();
1259#endif
1260 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001262 // Generate different code depending on which parts of the if statement
1263 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264 bool has_then_stm = node->HasThenStatement();
1265 bool has_else_stm = node->HasElseStatement();
1266
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001267 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001268
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001269 JumpTarget exit(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001270 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001271 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001272 JumpTarget then(this);
1273 JumpTarget else_(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001274 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001275 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1276 &then, &else_, true);
1277 if (frame_ != NULL) {
1278 Branch(false, &else_);
1279 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001281 if (frame_ != NULL || then.is_linked()) {
1282 then.Bind();
1283 VisitAndSpill(node->then_statement());
1284 }
1285 if (frame_ != NULL) {
1286 exit.Jump();
1287 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001288 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001289 if (else_.is_linked()) {
1290 else_.Bind();
1291 VisitAndSpill(node->else_statement());
1292 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001293
1294 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001295 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296 ASSERT(!has_else_stm);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001297 JumpTarget then(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, &exit, true);
1301 if (frame_ != NULL) {
1302 Branch(false, &exit);
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 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309
1310 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001311 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312 ASSERT(!has_then_stm);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001313 JumpTarget else_(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001314 // if (!cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001315 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1316 &exit, &else_, true);
1317 if (frame_ != NULL) {
1318 Branch(true, &exit);
1319 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001320 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001321 if (frame_ != NULL || else_.is_linked()) {
1322 else_.Bind();
1323 VisitAndSpill(node->else_statement());
1324 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001325
1326 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001327 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328 ASSERT(!has_then_stm && !has_else_stm);
1329 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001330 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1331 &exit, &exit, false);
1332 if (frame_ != NULL) {
1333 if (has_cc()) {
1334 cc_reg_ = al;
1335 } else {
1336 frame_->Drop();
1337 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338 }
1339 }
1340
1341 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001342 if (exit.is_linked()) {
1343 exit.Bind();
1344 }
1345 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001346}
1347
1348
ager@chromium.org7c537e22008-10-16 08:43:32 +00001349void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001350 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001352 CodeForStatementPosition(node);
1353 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001354}
1355
1356
ager@chromium.org7c537e22008-10-16 08:43:32 +00001357void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001358 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001359 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001360 CodeForStatementPosition(node);
1361 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001362}
1363
1364
ager@chromium.org7c537e22008-10-16 08:43:32 +00001365void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001366 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001367 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001368
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001369 if (function_return_is_shadowed_) {
1370 CodeForStatementPosition(node);
1371 LoadAndSpill(node->expression());
1372 frame_->EmitPop(r0);
1373 function_return_.Jump();
1374 } else {
1375 // Load the returned value.
1376 CodeForStatementPosition(node);
1377 LoadAndSpill(node->expression());
1378
1379 // Pop the result from the frame and prepare the frame for
1380 // returning thus making it easier to merge.
1381 frame_->EmitPop(r0);
1382 frame_->PrepareForReturn();
1383
1384 function_return_.Jump();
1385 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001386}
1387
1388
ager@chromium.org7c537e22008-10-16 08:43:32 +00001389void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001390#ifdef DEBUG
1391 int original_height = frame_->height();
1392#endif
1393 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001395 CodeForStatementPosition(node);
1396 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001397 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001398 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001399 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001400 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001401 }
kasper.lund7276f142008-07-30 08:49:36 +00001402 if (kDebug) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001403 JumpTarget verified_true(this);
kasper.lund7276f142008-07-30 08:49:36 +00001404 __ cmp(r0, Operand(cp));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001405 verified_true.Branch(eq);
kasper.lund7276f142008-07-30 08:49:36 +00001406 __ stop("PushContext: r0 is expected to be the same as cp");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001407 verified_true.Bind();
kasper.lund7276f142008-07-30 08:49:36 +00001408 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001409 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001410 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001411 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001412}
1413
1414
ager@chromium.org7c537e22008-10-16 08:43:32 +00001415void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001416#ifdef DEBUG
1417 int original_height = frame_->height();
1418#endif
1419 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001420 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001421 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001422 // Pop context.
1423 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1424 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001425 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001426 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001427}
1428
1429
ager@chromium.org7c537e22008-10-16 08:43:32 +00001430int CodeGenerator::FastCaseSwitchMaxOverheadFactor() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001431 return kFastSwitchMaxOverheadFactor;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001432}
1433
ager@chromium.org7c537e22008-10-16 08:43:32 +00001434int CodeGenerator::FastCaseSwitchMinCaseCount() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001435 return kFastSwitchMinCaseCount;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001436}
1437
1438
ager@chromium.org7c537e22008-10-16 08:43:32 +00001439void CodeGenerator::GenerateFastCaseSwitchJumpTable(
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001440 SwitchStatement* node,
1441 int min_index,
1442 int range,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001443 Label* default_label,
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001444 Vector<Label*> case_targets,
1445 Vector<Label> case_labels) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001446 VirtualFrame::SpilledScope spilled_scope(this);
1447 JumpTarget setup_default(this);
1448 JumpTarget is_smi(this);
1449
1450 // A non-null default label pointer indicates a default case among
1451 // the case labels. Otherwise we use the break target as a
1452 // "default" for failure to hit the jump table.
1453 JumpTarget* default_target =
1454 (default_label == NULL) ? node->break_target() : &setup_default;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001455
1456 ASSERT(kSmiTag == 0 && kSmiTagSize <= 2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001457 frame_->EmitPop(r0);
ager@chromium.org870a0b62008-11-04 11:43:05 +00001458
1459 // Test for a Smi value in a HeapNumber.
ager@chromium.org870a0b62008-11-04 11:43:05 +00001460 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001461 is_smi.Branch(eq);
ager@chromium.org870a0b62008-11-04 11:43:05 +00001462 __ ldr(r1, MemOperand(r0, HeapObject::kMapOffset - kHeapObjectTag));
1463 __ ldrb(r1, MemOperand(r1, Map::kInstanceTypeOffset - kHeapObjectTag));
1464 __ cmp(r1, Operand(HEAP_NUMBER_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001465 default_target->Branch(ne);
1466 frame_->EmitPush(r0);
1467 frame_->CallRuntime(Runtime::kNumberToSmi, 1);
1468 is_smi.Bind();
ager@chromium.org870a0b62008-11-04 11:43:05 +00001469
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001470 if (min_index != 0) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001471 // Small positive numbers can be immediate operands.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001472 if (min_index < 0) {
ager@chromium.org870a0b62008-11-04 11:43:05 +00001473 // If min_index is Smi::kMinValue, -min_index is not a Smi.
1474 if (Smi::IsValid(-min_index)) {
1475 __ add(r0, r0, Operand(Smi::FromInt(-min_index)));
1476 } else {
1477 __ add(r0, r0, Operand(Smi::FromInt(-min_index - 1)));
1478 __ add(r0, r0, Operand(Smi::FromInt(1)));
1479 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001480 } else {
1481 __ sub(r0, r0, Operand(Smi::FromInt(min_index)));
1482 }
1483 }
1484 __ tst(r0, Operand(0x80000000 | kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001485 default_target->Branch(ne);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001486 __ cmp(r0, Operand(Smi::FromInt(range)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001487 default_target->Branch(ge);
1488 VirtualFrame* start_frame = new VirtualFrame(frame_);
ager@chromium.org8bb60582008-12-11 12:02:20 +00001489 __ SmiJumpTable(r0, case_targets);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001490
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001491 GenerateFastCaseSwitchCases(node, case_labels, start_frame);
1492
1493 // If there was a default case among the case labels, we need to
1494 // emit code to jump to it from the default target used for failure
1495 // to hit the jump table.
1496 if (default_label != NULL) {
1497 if (has_valid_frame()) {
1498 node->break_target()->Jump();
1499 }
1500 setup_default.Bind();
1501 frame_->MergeTo(start_frame);
1502 __ b(default_label);
1503 DeleteFrame();
1504 }
1505 if (node->break_target()->is_linked()) {
1506 node->break_target()->Bind();
1507 }
1508
1509 delete start_frame;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001510}
1511
1512
ager@chromium.org7c537e22008-10-16 08:43:32 +00001513void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001514#ifdef DEBUG
1515 int original_height = frame_->height();
1516#endif
1517 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001518 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001519 CodeForStatementPosition(node);
1520 node->break_target()->Initialize(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001521
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001522 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001523 if (TryGenerateFastCaseSwitchStatement(node)) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001524 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1525 return;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001526 }
1527
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001528 JumpTarget next_test(this);
1529 JumpTarget fall_through(this);
1530 JumpTarget default_entry(this);
1531 JumpTarget default_exit(this, JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001532 ZoneList<CaseClause*>* cases = node->cases();
1533 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001534 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001535
1536 for (int i = 0; i < length; i++) {
1537 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001538 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001539 // Remember the default clause and compile it at the end.
1540 default_clause = clause;
1541 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001542 }
1543
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001544 Comment cmnt(masm_, "[ Case clause");
1545 // Compile the test.
1546 next_test.Bind();
1547 next_test.Unuse();
1548 // Duplicate TOS.
1549 __ ldr(r0, frame_->Top());
1550 frame_->EmitPush(r0);
1551 LoadAndSpill(clause->label());
1552 Comparison(eq, true);
1553 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001554
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001555 // Before entering the body from the test, remove the switch value from
1556 // the stack.
1557 frame_->Drop();
1558
1559 // Label the body so that fall through is enabled.
1560 if (i > 0 && cases->at(i - 1)->is_default()) {
1561 default_exit.Bind();
1562 } else {
1563 fall_through.Bind();
1564 fall_through.Unuse();
1565 }
1566 VisitStatementsAndSpill(clause->statements());
1567
1568 // If control flow can fall through from the body, jump to the next body
1569 // or the end of the statement.
1570 if (frame_ != NULL) {
1571 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1572 default_entry.Jump();
1573 } else {
1574 fall_through.Jump();
1575 }
1576 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001577 }
1578
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001579 // The final "test" removes the switch value.
1580 next_test.Bind();
1581 frame_->Drop();
1582
1583 // If there is a default clause, compile it.
1584 if (default_clause != NULL) {
1585 Comment cmnt(masm_, "[ Default clause");
1586 default_entry.Bind();
1587 VisitStatementsAndSpill(default_clause->statements());
1588 // If control flow can fall out of the default and there is a case after
1589 // it, jup to that case's body.
1590 if (frame_ != NULL && default_exit.is_bound()) {
1591 default_exit.Jump();
1592 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001593 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001594
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001595 if (fall_through.is_linked()) {
1596 fall_through.Bind();
1597 }
1598
1599 if (node->break_target()->is_linked()) {
1600 node->break_target()->Bind();
1601 }
1602 node->break_target()->Unuse();
1603 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001604}
1605
1606
ager@chromium.org7c537e22008-10-16 08:43:32 +00001607void CodeGenerator::VisitLoopStatement(LoopStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001608#ifdef DEBUG
1609 int original_height = frame_->height();
1610#endif
1611 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001612 Comment cmnt(masm_, "[ LoopStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001613 CodeForStatementPosition(node);
1614 node->break_target()->Initialize(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001615
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001616 // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a
1617 // known result for the test expression, with no side effects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW;
1619 if (node->cond() == NULL) {
1620 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1621 info = ALWAYS_TRUE;
1622 } else {
1623 Literal* lit = node->cond()->AsLiteral();
1624 if (lit != NULL) {
1625 if (lit->IsTrue()) {
1626 info = ALWAYS_TRUE;
1627 } else if (lit->IsFalse()) {
1628 info = ALWAYS_FALSE;
1629 }
1630 }
1631 }
1632
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001633 switch (node->type()) {
1634 case LoopStatement::DO_LOOP: {
1635 JumpTarget body(this, JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001637 // Label the top of the loop for the backward CFG edge. If the test
1638 // is always true we can use the continue target, and if the test is
1639 // always false there is no need.
1640 if (info == ALWAYS_TRUE) {
1641 node->continue_target()->Initialize(this, JumpTarget::BIDIRECTIONAL);
1642 node->continue_target()->Bind();
1643 } else if (info == ALWAYS_FALSE) {
1644 node->continue_target()->Initialize(this);
1645 } else {
1646 ASSERT(info == DONT_KNOW);
1647 node->continue_target()->Initialize(this);
1648 body.Bind();
1649 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001650
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001651 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001652 VisitAndSpill(node->body());
1653
1654 // Compile the test.
1655 if (info == ALWAYS_TRUE) {
1656 if (has_valid_frame()) {
1657 // If control can fall off the end of the body, jump back to the
1658 // top.
1659 node->continue_target()->Jump();
1660 }
1661 } else if (info == ALWAYS_FALSE) {
1662 // If we have a continue in the body, we only have to bind its jump
1663 // target.
1664 if (node->continue_target()->is_linked()) {
1665 node->continue_target()->Bind();
1666 }
1667 } else {
1668 ASSERT(info == DONT_KNOW);
1669 // We have to compile the test expression if it can be reached by
1670 // control flow falling out of the body or via continue.
1671 if (node->continue_target()->is_linked()) {
1672 node->continue_target()->Bind();
1673 }
1674 if (has_valid_frame()) {
1675 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1676 &body, node->break_target(), true);
1677 if (has_valid_frame()) {
1678 // A invalid frame here indicates that control did not
1679 // fall out of the test expression.
1680 Branch(true, &body);
1681 }
1682 }
1683 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001684 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001685 }
1686
1687 case LoopStatement::WHILE_LOOP: {
1688 // If the test is never true and has no side effects there is no need
1689 // to compile the test or body.
1690 if (info == ALWAYS_FALSE) break;
1691
1692 // Label the top of the loop with the continue target for the backward
1693 // CFG edge.
1694 node->continue_target()->Initialize(this, JumpTarget::BIDIRECTIONAL);
1695 node->continue_target()->Bind();
1696
1697 if (info == DONT_KNOW) {
1698 JumpTarget body(this);
1699 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1700 &body, node->break_target(), true);
1701 if (has_valid_frame()) {
1702 // A NULL frame indicates that control did not fall out of the
1703 // test expression.
1704 Branch(false, node->break_target());
1705 }
1706 if (has_valid_frame() || body.is_linked()) {
1707 body.Bind();
1708 }
1709 }
1710
1711 if (has_valid_frame()) {
1712 CheckStack(); // TODO(1222600): ignore if body contains calls.
1713 VisitAndSpill(node->body());
1714
1715 // If control flow can fall out of the body, jump back to the top.
1716 if (has_valid_frame()) {
1717 node->continue_target()->Jump();
1718 }
1719 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001720 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001721 }
1722
1723 case LoopStatement::FOR_LOOP: {
1724 JumpTarget loop(this, JumpTarget::BIDIRECTIONAL);
1725
1726 if (node->init() != NULL) {
1727 VisitAndSpill(node->init());
1728 }
1729
1730 // There is no need to compile the test or body.
1731 if (info == ALWAYS_FALSE) break;
1732
1733 // If there is no update statement, label the top of the loop with the
1734 // continue target, otherwise with the loop target.
1735 if (node->next() == NULL) {
1736 node->continue_target()->Initialize(this, JumpTarget::BIDIRECTIONAL);
1737 node->continue_target()->Bind();
1738 } else {
1739 node->continue_target()->Initialize(this);
1740 loop.Bind();
1741 }
1742
1743 // If the test is always true, there is no need to compile it.
1744 if (info == DONT_KNOW) {
1745 JumpTarget body(this);
1746 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1747 &body, node->break_target(), true);
1748 if (has_valid_frame()) {
1749 Branch(false, node->break_target());
1750 }
1751 if (has_valid_frame() || body.is_linked()) {
1752 body.Bind();
1753 }
1754 }
1755
1756 if (has_valid_frame()) {
1757 CheckStack(); // TODO(1222600): ignore if body contains calls.
1758 VisitAndSpill(node->body());
1759
1760 if (node->next() == NULL) {
1761 // If there is no update statement and control flow can fall out
1762 // of the loop, jump directly to the continue label.
1763 if (has_valid_frame()) {
1764 node->continue_target()->Jump();
1765 }
1766 } else {
1767 // If there is an update statement and control flow can reach it
1768 // via falling out of the body of the loop or continuing, we
1769 // compile the update statement.
1770 if (node->continue_target()->is_linked()) {
1771 node->continue_target()->Bind();
1772 }
1773 if (has_valid_frame()) {
1774 // Record source position of the statement as this code which is
1775 // after the code for the body actually belongs to the loop
1776 // statement and not the body.
1777 CodeForStatementPosition(node);
1778 VisitAndSpill(node->next());
1779 loop.Jump();
1780 }
1781 }
1782 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001783 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001784 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001785 }
1786
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001787 if (node->break_target()->is_linked()) {
1788 node->break_target()->Bind();
1789 }
1790 node->continue_target()->Unuse();
1791 node->break_target()->Unuse();
1792 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001793}
1794
1795
ager@chromium.org7c537e22008-10-16 08:43:32 +00001796void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001797#ifdef DEBUG
1798 int original_height = frame_->height();
1799#endif
1800 ASSERT(!in_spilled_code());
1801 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001802 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001803 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001804
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001805 JumpTarget primitive(this);
1806 JumpTarget jsobject(this);
1807 JumpTarget fixed_array(this);
1808 JumpTarget entry(this, JumpTarget::BIDIRECTIONAL);
1809 JumpTarget end_del_check(this);
1810 JumpTarget exit(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001811
1812 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001813 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814
1815 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1816 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001817 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001819 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820 __ cmp(r0, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001821 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822
1823 // Stack layout in body:
1824 // [iteration counter (Smi)]
1825 // [length of array]
1826 // [FixedArray]
1827 // [Map or 0]
1828 // [Object]
1829
1830 // Check if enumerable is already a JSObject
1831 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001832 primitive.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001833 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
1834 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00001835 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001836 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001837
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001838 primitive.Bind();
1839 frame_->EmitPush(r0);
1840 Result arg_count = allocator_->Allocate(r0);
1841 ASSERT(arg_count.is_valid());
1842 __ mov(arg_count.reg(), Operand(0));
1843 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001845 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001846 // Get the set of properties (as a FixedArray or Map).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001847 frame_->EmitPush(r0); // duplicate the object being enumerated
1848 frame_->EmitPush(r0);
1849 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001850
1851 // If we got a Map, we can do a fast modification check.
1852 // Otherwise, we got a FixedArray, and we have to do a slow check.
1853 __ mov(r2, Operand(r0));
1854 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
1855 __ cmp(r1, Operand(Factory::meta_map()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001856 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857
1858 // Get enum cache
1859 __ mov(r1, Operand(r0));
1860 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1861 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1862 __ ldr(r2,
1863 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1864
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001865 frame_->EmitPush(r0); // map
1866 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001867 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001869 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001870 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001871 frame_->EmitPush(r0);
1872 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001873
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001874 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001875 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001876 frame_->EmitPush(r1); // insert 0 in place of Map
1877 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001878
1879 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001880 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001881 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001882 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001883 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001884 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001885
1886 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001887 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00001888 // sp[0] : index
1889 // sp[1] : array/enum cache length
1890 // sp[2] : array or enum cache
1891 // sp[3] : 0 or map
1892 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001893 // Grab the current frame's height for the break and continue
1894 // targets only after all the state is pushed on the frame.
1895 node->break_target()->Initialize(this);
1896 node->continue_target()->Initialize(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001897
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001898 __ ldr(r0, frame_->ElementAt(0)); // load the current count
1899 __ ldr(r1, frame_->ElementAt(1)); // load the length
1900 __ cmp(r0, Operand(r1)); // compare to the array length
1901 node->break_target()->Branch(hs);
1902
1903 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001904
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001905 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001906 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1908 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1909
1910 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001911 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001912 // Check if this (still) matches the map of the enumerable.
1913 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001914 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001915 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1916 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001917 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001918
1919 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001920 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
1921 frame_->EmitPush(r0);
1922 frame_->EmitPush(r3); // push entry
1923 Result arg_count_register = allocator_->Allocate(r0);
1924 ASSERT(arg_count_register.is_valid());
1925 __ mov(arg_count_register.reg(), Operand(1));
1926 Result result = frame_->InvokeBuiltin(Builtins::FILTER_KEY,
1927 CALL_JS,
1928 &arg_count_register,
1929 2);
1930 __ mov(r3, Operand(result.reg()));
1931 result.Unuse();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001932
1933 // If the property has been removed while iterating, we just skip it.
1934 __ cmp(r3, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001935 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001937 end_del_check.Bind();
1938 // Store the entry in the 'each' expression and take another spin in the
1939 // loop. r3: i'th entry of the enum cache (or string there of)
1940 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001941 { Reference each(this, node->each());
1942 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001943 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001944 __ ldr(r0, frame_->ElementAt(each.size()));
1945 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001946 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001947 // If the reference was to a slot we rely on the convenient property
1948 // that it doesn't matter whether a value (eg, r3 pushed above) is
1949 // right on top of or right underneath a zero-sized reference.
1950 each.SetValue(NOT_CONST_INIT);
mads.s.ager31e71382008-08-13 09:32:07 +00001951 if (each.size() > 0) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00001952 // It's safe to pop the value lying on top of the reference before
1953 // unloading the reference itself (which preserves the top of stack,
1954 // ie, now the topmost value of the non-zero sized reference), since
1955 // we will discard the top of stack after unloading the reference
1956 // anyway.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001957 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001958 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959 }
1960 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001961 // Discard the i'th entry pushed above or else the remainder of the
1962 // reference, whichever is currently on top of the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001963 frame_->Drop();
1964
1965 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001966 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001967 VisitAndSpill(node->body());
1968
1969 // Next.
1970 node->continue_target()->Bind();
1971 frame_->EmitPop(r0);
1972 __ add(r0, r0, Operand(Smi::FromInt(1)));
1973 frame_->EmitPush(r0);
1974 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001975
1976 // Cleanup.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001977 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001978 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001979
1980 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001981 exit.Bind();
1982 node->continue_target()->Unuse();
1983 node->break_target()->Unuse();
1984 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001985}
1986
1987
ager@chromium.org7c537e22008-10-16 08:43:32 +00001988void CodeGenerator::VisitTryCatch(TryCatch* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001989#ifdef DEBUG
1990 int original_height = frame_->height();
1991#endif
1992 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993 Comment cmnt(masm_, "[ TryCatch");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001994 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001995
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001996 JumpTarget try_block(this);
1997 JumpTarget exit(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001998
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001999 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002000 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002001 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002002
2003 // Store the caught exception in the catch variable.
2004 { Reference ref(this, node->catch_var());
ager@chromium.org7c537e22008-10-16 08:43:32 +00002005 ASSERT(ref.is_slot());
2006 // Here we make use of the convenient property that it doesn't matter
2007 // whether a value is immediately on top of or underneath a zero-sized
2008 // reference.
2009 ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010 }
2011
2012 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002013 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002014
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002015 VisitStatementsAndSpill(node->catch_block()->statements());
2016 if (frame_ != NULL) {
2017 exit.Jump();
2018 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019
2020
2021 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002022 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002023
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002024 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2025 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002026
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002027 // Shadow the labels for all escapes from the try block, including
2028 // returns. During shadowing, the original label is hidden as the
2029 // LabelShadow and operations on the original actually affect the
2030 // shadowing label.
2031 //
2032 // We should probably try to unify the escaping labels and the return
2033 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002034 int nof_escapes = node->escaping_targets()->length();
2035 List<ShadowTarget*> shadows(1 + nof_escapes);
2036
2037 // Add the shadow target for the function return.
2038 static const int kReturnShadowIndex = 0;
2039 shadows.Add(new ShadowTarget(&function_return_));
2040 bool function_return_was_shadowed = function_return_is_shadowed_;
2041 function_return_is_shadowed_ = true;
2042 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2043
2044 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002046 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 }
2048
2049 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002050 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051
2052 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002053 // After shadowing stops, the original labels are unshadowed and the
2054 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002055 bool has_unlinks = false;
2056 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002057 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002058 has_unlinks = has_unlinks || shadows[i]->is_linked();
2059 }
2060 function_return_is_shadowed_ = function_return_was_shadowed;
2061
2062 // Get an external reference to the handler address.
2063 ExternalReference handler_address(Top::k_handler_address);
2064
2065 // The next handler address is at kNextIndex in the stack.
2066 const int kNextIndex = StackHandlerConstants::kNextOffset / kPointerSize;
2067 // If we can fall off the end of the try block, unlink from try chain.
2068 if (has_valid_frame()) {
2069 __ ldr(r1, frame_->ElementAt(kNextIndex));
2070 __ mov(r3, Operand(handler_address));
2071 __ str(r1, MemOperand(r3));
2072 frame_->Drop(StackHandlerConstants::kSize / kPointerSize);
2073 if (has_unlinks) {
2074 exit.Jump();
2075 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076 }
2077
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002078 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002079 // jumped to. Deallocate each shadow target.
2080 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002082 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002083 shadows[i]->Bind();
2084 // Because we can be jumping here (to spilled code) from unspilled
2085 // code, we need to reestablish a spilled frame at this block.
2086 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002087
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002088 // Reload sp from the top handler, because some statements that we
2089 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002090 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002092 // The stack pointer was restored to just below the code slot
2093 // (the topmost slot) in the handler.
2094 frame_->Forget(frame_->height() - handler_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002096 // kNextIndex is off by one because the code slot has already
2097 // been dropped.
2098 __ ldr(r1, frame_->ElementAt(kNextIndex - 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002099 __ str(r1, MemOperand(r3));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002100 // The code slot has already been dropped from the handler.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002101 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002102
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002103 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2104 frame_->PrepareForReturn();
2105 }
2106 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002107 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002108 delete shadows[i];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109 }
2110
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002111 exit.Bind();
2112 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002113}
2114
2115
ager@chromium.org7c537e22008-10-16 08:43:32 +00002116void CodeGenerator::VisitTryFinally(TryFinally* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002117#ifdef DEBUG
2118 int original_height = frame_->height();
2119#endif
2120 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121 Comment cmnt(masm_, "[ TryFinally");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002122 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123
2124 // State: Used to keep track of reason for entering the finally
2125 // block. Should probably be extended to hold information for
2126 // break/continue from within the try block.
2127 enum { FALLING, THROWING, JUMPING };
2128
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002129 JumpTarget try_block(this);
2130 JumpTarget finally_block(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002131
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002132 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002133
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002134 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002135 // In case of thrown exceptions, this is where we continue.
2136 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002137 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002138
2139 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002140 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002141
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002142 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2143 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002144
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002145 // Shadow the labels for all escapes from the try block, including
2146 // returns. Shadowing hides the original label as the LabelShadow and
2147 // operations on the original actually affect the shadowing label.
2148 //
2149 // We should probably try to unify the escaping labels and the return
2150 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002151 int nof_escapes = node->escaping_targets()->length();
2152 List<ShadowTarget*> shadows(1 + nof_escapes);
2153
2154 // Add the shadow target for the function return.
2155 static const int kReturnShadowIndex = 0;
2156 shadows.Add(new ShadowTarget(&function_return_));
2157 bool function_return_was_shadowed = function_return_is_shadowed_;
2158 function_return_is_shadowed_ = true;
2159 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2160
2161 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002162 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002163 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002164 }
2165
2166 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002167 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002168
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002169 // Stop the introduced shadowing and count the number of required unlinks.
2170 // After shadowing stops, the original labels are unshadowed and the
2171 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002172 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002173 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002174 shadows[i]->StopShadowing();
2175 if (shadows[i]->is_linked()) nof_unlinks++;
2176 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002177 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002178
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002179 // Get an external reference to the handler address.
2180 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002181
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002182 // The next handler address is at kNextIndex in the stack.
2183 const int kNextIndex = StackHandlerConstants::kNextOffset / kPointerSize;
2184 // If we can fall off the end of the try block, unlink from the try
2185 // chain and set the state on the frame to FALLING.
2186 if (has_valid_frame()) {
2187 __ ldr(r1, frame_->ElementAt(kNextIndex));
2188 __ mov(r3, Operand(handler_address));
2189 __ str(r1, MemOperand(r3));
2190 frame_->Drop(StackHandlerConstants::kSize / kPointerSize);
2191
2192 // Fake a top of stack value (unneeded when FALLING) and set the
2193 // state in r2, then jump around the unlink blocks if any.
2194 __ mov(r0, Operand(Factory::undefined_value()));
2195 frame_->EmitPush(r0);
2196 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2197 if (nof_unlinks > 0) {
2198 finally_block.Jump();
2199 }
2200 }
2201
2202 // Generate code to unlink and set the state for the (formerly)
2203 // shadowing targets that have been jumped to.
2204 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002205 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002206 // If we have come from the shadowed return, the return value is
2207 // in (a non-refcounted reference to) r0. We must preserve it
2208 // until it is pushed.
2209 //
2210 // Because we can be jumping here (to spilled code) from
2211 // unspilled code, we need to reestablish a spilled frame at
2212 // this block.
2213 shadows[i]->Bind();
2214 frame_->SpillAll();
2215
2216 // Reload sp from the top handler, because some statements that
2217 // we break from (eg, for...in) may have left stuff on the
2218 // stack.
2219 __ mov(r3, Operand(handler_address));
2220 __ ldr(sp, MemOperand(r3));
2221 // The stack pointer was restored to the address slot in the handler.
2222 ASSERT(StackHandlerConstants::kNextOffset == 1 * kPointerSize);
2223 frame_->Forget(frame_->height() - handler_height + 1);
2224
2225 // Unlink this handler and drop it from the frame. The next
2226 // handler address is now on top of the frame.
2227 frame_->EmitPop(r1);
2228 __ str(r1, MemOperand(r3));
2229 // The top (code) and the second (handler) slot have both been
2230 // dropped already.
2231 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 2);
2232
2233 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002234 // If this label shadowed the function return, materialize the
2235 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002236 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002237 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002238 // Fake TOS for targets that shadowed breaks and continues.
mads.s.ager31e71382008-08-13 09:32:07 +00002239 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002240 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002241 }
2242 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002243 if (--nof_unlinks > 0) {
2244 // If this is not the last unlink block, jump around the next.
2245 finally_block.Jump();
2246 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247 }
2248 }
2249
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002250 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002251 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002252
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002253 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002254 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002255
2256 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002257 // and the state - while evaluating the finally block.
2258 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002259 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002260 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002261
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002262 if (has_valid_frame()) {
2263 // Restore state and return value or faked TOS.
2264 frame_->EmitPop(r2);
2265 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002266 }
2267
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002268 // Generate code to jump to the right destination for all used
2269 // formerly shadowing targets. Deallocate each shadow target.
2270 for (int i = 0; i < shadows.length(); i++) {
2271 if (has_valid_frame() && shadows[i]->is_bound()) {
2272 JumpTarget* original = shadows[i]->other_target();
2273 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2274 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2275 JumpTarget skip(this);
2276 skip.Branch(ne);
2277 frame_->PrepareForReturn();
2278 original->Jump();
2279 skip.Bind();
2280 } else {
2281 original->Branch(eq);
2282 }
2283 }
2284 delete shadows[i];
2285 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002287 if (has_valid_frame()) {
2288 // Check if we need to rethrow the exception.
2289 JumpTarget exit(this);
2290 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2291 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002292
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002293 // Rethrow exception.
2294 frame_->EmitPush(r0);
2295 frame_->CallRuntime(Runtime::kReThrow, 1);
2296
2297 // Done.
2298 exit.Bind();
2299 }
2300 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301}
2302
2303
ager@chromium.org7c537e22008-10-16 08:43:32 +00002304void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002305#ifdef DEBUG
2306 int original_height = frame_->height();
2307#endif
2308 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002309 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002310 CodeForStatementPosition(node);
2311 frame_->CallRuntime(Runtime::kDebugBreak, 0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002312 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002313 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002314}
2315
2316
ager@chromium.org7c537e22008-10-16 08:43:32 +00002317void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002318 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002319 ASSERT(boilerplate->IsBoilerplate());
2320
2321 // Push the boilerplate on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002322 __ mov(r0, Operand(boilerplate));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002323 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002324
2325 // Create a new closure.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002326 frame_->EmitPush(cp);
2327 frame_->CallRuntime(Runtime::kNewClosure, 2);
2328 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002329}
2330
2331
ager@chromium.org7c537e22008-10-16 08:43:32 +00002332void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002333#ifdef DEBUG
2334 int original_height = frame_->height();
2335#endif
2336 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002337 Comment cmnt(masm_, "[ FunctionLiteral");
2338
2339 // Build the function boilerplate and instantiate it.
2340 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00002341 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002342 if (HasStackOverflow()) {
2343 ASSERT(frame_->height() == original_height);
2344 return;
2345 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002346 InstantiateBoilerplate(boilerplate);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002347 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002348}
2349
2350
ager@chromium.org7c537e22008-10-16 08:43:32 +00002351void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002352 FunctionBoilerplateLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002353#ifdef DEBUG
2354 int original_height = frame_->height();
2355#endif
2356 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002357 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2358 InstantiateBoilerplate(node->boilerplate());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002359 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002360}
2361
2362
ager@chromium.org7c537e22008-10-16 08:43:32 +00002363void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002364#ifdef DEBUG
2365 int original_height = frame_->height();
2366#endif
2367 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002368 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002369 JumpTarget then(this);
2370 JumpTarget else_(this);
2371 JumpTarget exit(this);
2372 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
2373 &then, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002374 Branch(false, &else_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002375 then.Bind();
2376 LoadAndSpill(node->then_expression(), typeof_state());
2377 exit.Jump();
2378 else_.Bind();
2379 LoadAndSpill(node->else_expression(), typeof_state());
2380 exit.Bind();
2381 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002382}
2383
2384
ager@chromium.org7c537e22008-10-16 08:43:32 +00002385void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002386 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002387 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002388 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002389
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002390 JumpTarget slow(this);
2391 JumpTarget done(this);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002392
2393 // Generate fast-case code for variables that might be shadowed by
2394 // eval-introduced variables. Eval is used a lot without
2395 // introducing variables. In those cases, we do not want to
2396 // perform a runtime call for all variables in the scope
2397 // containing the eval.
2398 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2399 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002400 // If there was no control flow to slow, we can exit early.
2401 if (!slow.is_linked()) {
2402 frame_->EmitPush(r0);
2403 return;
2404 }
2405
2406 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002407
2408 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2409 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2410 // Only generate the fast case for locals that rewrite to slots.
2411 // This rules out argument loads.
2412 if (potential_slot != NULL) {
2413 __ ldr(r0,
2414 ContextSlotOperandCheckExtensions(potential_slot,
2415 r1,
2416 r2,
2417 &slow));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002418 // There is always control flow to slow from
2419 // ContextSlotOperandCheckExtensions.
2420 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002421 }
2422 }
2423
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002424 slow.Bind();
2425 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002426 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002427 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002428
ager@chromium.org7c537e22008-10-16 08:43:32 +00002429 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002430 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002431 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002432 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002433 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002434
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002435 done.Bind();
2436 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002437
2438 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002439 // Note: We would like to keep the assert below, but it fires because of
2440 // some nasty code in LoadTypeofExpression() which should be removed...
ager@chromium.org381abbb2009-02-25 13:23:22 +00002441 // ASSERT(!slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002442
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002443 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002444 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002445 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002446 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002447 // Const slots may contain 'the hole' value (the constant hasn't been
2448 // initialized yet) which needs to be converted into the 'undefined'
2449 // value.
2450 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002451 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002452 __ cmp(r0, Operand(Factory::the_hole_value()));
2453 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002454 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002455 }
2456 }
2457}
2458
2459
ager@chromium.org381abbb2009-02-25 13:23:22 +00002460void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2461 TypeofState typeof_state,
2462 Register tmp,
2463 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002464 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002465 // Check that no extension objects have been created by calls to
2466 // eval from the current scope to the global scope.
2467 Register context = cp;
2468 Scope* s = scope();
2469 while (s != NULL) {
2470 if (s->num_heap_slots() > 0) {
2471 if (s->calls_eval()) {
2472 // Check that extension is NULL.
2473 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2474 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002475 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002476 }
2477 // Load next context in chain.
2478 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2479 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2480 context = tmp;
2481 }
2482 // If no outer scope calls eval, we do not need to check more
2483 // context extensions.
2484 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2485 s = s->outer_scope();
2486 }
2487
2488 if (s->is_eval_scope()) {
2489 Label next, fast;
2490 if (!context.is(tmp)) __ mov(tmp, Operand(context));
2491 __ bind(&next);
2492 // Terminate at global context.
2493 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
2494 __ cmp(tmp2, Operand(Factory::global_context_map()));
2495 __ b(eq, &fast);
2496 // Check that extension is NULL.
2497 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2498 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002499 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002500 // Load next context in chain.
2501 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2502 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2503 __ b(&next);
2504 __ bind(&fast);
2505 }
2506
2507 // All extension objects were empty and it is safe to use a global
2508 // load IC call.
2509 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2510 // Load the global object.
2511 LoadGlobal();
2512 // Setup the name register.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002513 Result name = allocator_->Allocate(r2);
2514 ASSERT(name.is_valid()); // We are in spilled code.
2515 __ mov(name.reg(), Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002516 // Call IC stub.
2517 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002518 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002519 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002520 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002521 }
2522
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002523 // Drop the global object. The result is in r0.
2524 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002525}
2526
2527
ager@chromium.org7c537e22008-10-16 08:43:32 +00002528void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002529#ifdef DEBUG
2530 int original_height = frame_->height();
2531#endif
2532 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002533 Comment cmnt(masm_, "[ Slot");
2534 LoadFromSlot(node, typeof_state());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002535 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002536}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002537
ager@chromium.org7c537e22008-10-16 08:43:32 +00002538
2539void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002540#ifdef DEBUG
2541 int original_height = frame_->height();
2542#endif
2543 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002544 Comment cmnt(masm_, "[ VariableProxy");
2545
2546 Variable* var = node->var();
2547 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002548 if (expr != NULL) {
2549 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002550 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002551 ASSERT(var->is_global());
2552 Reference ref(this, node);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002553 ref.GetValueAndSpill(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002554 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002555 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002556}
2557
2558
ager@chromium.org7c537e22008-10-16 08:43:32 +00002559void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002560#ifdef DEBUG
2561 int original_height = frame_->height();
2562#endif
2563 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002564 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002565 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002566 frame_->EmitPush(r0);
2567 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002568}
2569
2570
ager@chromium.org7c537e22008-10-16 08:43:32 +00002571void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002572#ifdef DEBUG
2573 int original_height = frame_->height();
2574#endif
2575 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002576 Comment cmnt(masm_, "[ RexExp Literal");
2577
2578 // Retrieve the literal array and check the allocated entry.
2579
2580 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002581 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002582
2583 // Load the literals array of the function.
2584 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2585
2586 // Load the literal at the ast saved index.
2587 int literal_offset =
2588 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2589 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2590
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002591 JumpTarget done(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002592 __ cmp(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002593 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002594
2595 // If the entry is undefined we call the runtime system to computed
2596 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002597 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002598 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002599 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002600 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002601 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002602 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002603 frame_->EmitPush(r0);
2604 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002605 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002606
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002607 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002608 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002609 frame_->EmitPush(r2);
2610 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002611}
2612
2613
2614// This deferred code stub will be used for creating the boilerplate
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002615// by calling Runtime_CreateObjectLiteralBoilerplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002616// Each created boilerplate is stored in the JSFunction and they are
2617// therefore context dependent.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002618class DeferredObjectLiteral: public DeferredCode {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002619 public:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002620 DeferredObjectLiteral(CodeGenerator* generator, ObjectLiteral* node)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002621 : DeferredCode(generator), node_(node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002622 set_comment("[ DeferredObjectLiteral");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002623 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002624
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002625 virtual void Generate();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002626
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002627 private:
2628 ObjectLiteral* node_;
2629};
2630
2631
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002632void DeferredObjectLiteral::Generate() {
2633 // Argument is passed in r1.
2634 enter()->Bind();
2635 VirtualFrame::SpilledScope spilled_scope(generator());
2636
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002637 // If the entry is undefined we call the runtime system to computed
2638 // the literal.
2639
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002640 VirtualFrame* frame = generator()->frame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002641 // Literal array (0).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002642 frame->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002643 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00002644 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002645 frame->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002646 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00002647 __ mov(r0, Operand(node_->constant_properties()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002648 frame->EmitPush(r0);
2649 Result boilerplate =
2650 frame->CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
2651 __ mov(r2, Operand(boilerplate.reg()));
2652 // Result is returned in r2.
2653 exit_.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002654}
2655
2656
ager@chromium.org7c537e22008-10-16 08:43:32 +00002657void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002658#ifdef DEBUG
2659 int original_height = frame_->height();
2660#endif
2661 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002662 Comment cmnt(masm_, "[ ObjectLiteral");
2663
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002664 DeferredObjectLiteral* deferred = new DeferredObjectLiteral(this, node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002665
2666 // Retrieve the literal array and check the allocated entry.
2667
2668 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002669 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002670
2671 // Load the literals array of the function.
2672 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2673
2674 // Load the literal at the ast saved index.
2675 int literal_offset =
2676 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2677 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2678
2679 // Check whether we need to materialize the object literal boilerplate.
2680 // If so, jump to the deferred code.
2681 __ cmp(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002682 deferred->enter()->Branch(eq);
2683 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002684
2685 // Push the object literal boilerplate.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002686 frame_->EmitPush(r2);
mads.s.ager31e71382008-08-13 09:32:07 +00002687
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002688 // Clone the boilerplate object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002689 frame_->CallRuntime(Runtime::kCloneLiteralBoilerplate, 1);
2690 frame_->EmitPush(r0); // save the result
mads.s.ager31e71382008-08-13 09:32:07 +00002691 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002692
2693 for (int i = 0; i < node->properties()->length(); i++) {
2694 ObjectLiteral::Property* property = node->properties()->at(i);
2695 Literal* key = property->key();
2696 Expression* value = property->value();
2697 switch (property->kind()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002698 case ObjectLiteral::Property::CONSTANT:
2699 break;
2700 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2701 if (property->value()->AsMaterializedLiteral()->is_simple()) break;
2702 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002703 case ObjectLiteral::Property::COMPUTED: // fall through
2704 case ObjectLiteral::Property::PROTOTYPE: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002705 frame_->EmitPush(r0); // dup the result
2706 LoadAndSpill(key);
2707 LoadAndSpill(value);
2708 frame_->CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002709 // restore r0
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002710 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002711 break;
2712 }
2713 case ObjectLiteral::Property::SETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002714 frame_->EmitPush(r0);
2715 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002716 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002717 frame_->EmitPush(r0);
2718 LoadAndSpill(value);
2719 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002720 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002721 break;
2722 }
2723 case ObjectLiteral::Property::GETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002724 frame_->EmitPush(r0);
2725 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002726 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002727 frame_->EmitPush(r0);
2728 LoadAndSpill(value);
2729 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002730 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002731 break;
2732 }
2733 }
2734 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002735 ASSERT(frame_->height() == original_height + 1);
2736}
2737
2738
2739// This deferred code stub will be used for creating the boilerplate
2740// by calling Runtime_CreateArrayLiteralBoilerplate.
2741// Each created boilerplate is stored in the JSFunction and they are
2742// therefore context dependent.
2743class DeferredArrayLiteral: public DeferredCode {
2744 public:
2745 DeferredArrayLiteral(CodeGenerator* generator, ArrayLiteral* node)
2746 : DeferredCode(generator), node_(node) {
2747 set_comment("[ DeferredArrayLiteral");
2748 }
2749
2750 virtual void Generate();
2751
2752 private:
2753 ArrayLiteral* node_;
2754};
2755
2756
2757void DeferredArrayLiteral::Generate() {
2758 // Argument is passed in r1.
2759 enter()->Bind();
2760 VirtualFrame::SpilledScope spilled_scope(generator());
2761
2762 // If the entry is undefined we call the runtime system to computed
2763 // the literal.
2764
2765 VirtualFrame* frame = generator()->frame();
2766 // Literal array (0).
2767 frame->EmitPush(r1);
2768 // Literal index (1).
2769 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
2770 frame->EmitPush(r0);
2771 // Constant properties (2).
2772 __ mov(r0, Operand(node_->literals()));
2773 frame->EmitPush(r0);
2774 Result boilerplate =
2775 frame->CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3);
2776 __ mov(r2, Operand(boilerplate.reg()));
2777 // Result is returned in r2.
2778 exit_.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002779}
2780
2781
ager@chromium.org7c537e22008-10-16 08:43:32 +00002782void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002783#ifdef DEBUG
2784 int original_height = frame_->height();
2785#endif
2786 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002787 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002788
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002789 DeferredArrayLiteral* deferred = new DeferredArrayLiteral(this, node);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002790
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002791 // Retrieve the literal array and check the allocated entry.
2792
2793 // Load the function of this activation.
2794 __ ldr(r1, frame_->Function());
2795
2796 // Load the literals array of the function.
2797 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2798
2799 // Load the literal at the ast saved index.
2800 int literal_offset =
2801 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2802 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2803
2804 // Check whether we need to materialize the object literal boilerplate.
2805 // If so, jump to the deferred code.
2806 __ cmp(r2, Operand(Factory::undefined_value()));
2807 deferred->enter()->Branch(eq);
2808 deferred->BindExit();
2809
2810 // Push the object literal boilerplate.
2811 frame_->EmitPush(r2);
2812
2813 // Clone the boilerplate object.
2814 frame_->CallRuntime(Runtime::kCloneLiteralBoilerplate, 1);
2815 frame_->EmitPush(r0); // save the result
2816 // r0: cloned object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002817
2818 // Generate code to set the elements in the array that are not
2819 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002820 for (int i = 0; i < node->values()->length(); i++) {
2821 Expression* value = node->values()->at(i);
2822
2823 // If value is literal the property value is already
2824 // set in the boilerplate object.
2825 if (value->AsLiteral() == NULL) {
2826 // The property must be set by generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002827 LoadAndSpill(value);
2828 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002829
2830 // Fetch the object literal
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002831 __ ldr(r1, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002832 // Get the elements array.
2833 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
2834
2835 // Write to the indexed properties array.
2836 int offset = i * kPointerSize + Array::kHeaderSize;
2837 __ str(r0, FieldMemOperand(r1, offset));
2838
2839 // Update the write barrier for the array address.
2840 __ mov(r3, Operand(offset));
2841 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002842 }
2843 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002844 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002845}
2846
2847
ager@chromium.org32912102009-01-16 10:38:43 +00002848void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002849#ifdef DEBUG
2850 int original_height = frame_->height();
2851#endif
2852 ASSERT(!in_spilled_code());
2853 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org32912102009-01-16 10:38:43 +00002854 // Call runtime routine to allocate the catch extension object and
2855 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002856 Comment cmnt(masm_, "[ CatchExtensionObject");
2857 LoadAndSpill(node->key());
2858 LoadAndSpill(node->value());
2859 Result result =
2860 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2861 frame_->EmitPush(result.reg());
2862 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002863}
2864
2865
ager@chromium.org7c537e22008-10-16 08:43:32 +00002866void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002867#ifdef DEBUG
2868 int original_height = frame_->height();
2869#endif
2870 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002871 Comment cmnt(masm_, "[ Assignment");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002872 CodeForStatementPosition(node);
mads.s.ager31e71382008-08-13 09:32:07 +00002873
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002874 { Reference target(this, node->target());
2875 if (target.is_illegal()) {
2876 // Fool the virtual frame into thinking that we left the assignment's
2877 // value on the frame.
2878 __ mov(r0, Operand(Smi::FromInt(0)));
2879 frame_->EmitPush(r0);
2880 ASSERT(frame_->height() == original_height + 1);
2881 return;
2882 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002883
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002884 if (node->op() == Token::ASSIGN ||
2885 node->op() == Token::INIT_VAR ||
2886 node->op() == Token::INIT_CONST) {
2887 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002888
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002889 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002890 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
2891 Literal* literal = node->value()->AsLiteral();
2892 if (literal != NULL && literal->handle()->IsSmi()) {
2893 SmiOperation(node->binary_op(), literal->handle(), false);
2894 frame_->EmitPush(r0);
2895
2896 } else {
2897 LoadAndSpill(node->value());
2898 GenericBinaryOperation(node->binary_op());
2899 frame_->EmitPush(r0);
2900 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002901 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002902
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002903 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2904 if (var != NULL &&
2905 (var->mode() == Variable::CONST) &&
2906 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2907 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002908
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002909 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002910 CodeForSourcePosition(node->position());
2911 if (node->op() == Token::INIT_CONST) {
2912 // Dynamic constant initializations must use the function context
2913 // and initialize the actual constant declared. Dynamic variable
2914 // initializations are simply assignments and use SetValue.
2915 target.SetValue(CONST_INIT);
2916 } else {
2917 target.SetValue(NOT_CONST_INIT);
2918 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002919 }
2920 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002921 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002922}
2923
2924
ager@chromium.org7c537e22008-10-16 08:43:32 +00002925void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002926#ifdef DEBUG
2927 int original_height = frame_->height();
2928#endif
2929 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002930 Comment cmnt(masm_, "[ Throw");
2931
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002932 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002933 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002934 frame_->CallRuntime(Runtime::kThrow, 1);
2935 frame_->EmitPush(r0);
2936 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002937}
2938
2939
ager@chromium.org7c537e22008-10-16 08:43:32 +00002940void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002941#ifdef DEBUG
2942 int original_height = frame_->height();
2943#endif
2944 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002945 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002946
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002947 { Reference property(this, node);
2948 property.GetValueAndSpill(typeof_state());
2949 }
2950 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002951}
2952
2953
ager@chromium.org7c537e22008-10-16 08:43:32 +00002954void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002955#ifdef DEBUG
2956 int original_height = frame_->height();
2957#endif
2958 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002959 Comment cmnt(masm_, "[ Call");
2960
2961 ZoneList<Expression*>* args = node->arguments();
2962
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002963 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002964 // Standard function call.
2965
2966 // Check if the function is a variable or a property.
2967 Expression* function = node->expression();
2968 Variable* var = function->AsVariableProxy()->AsVariable();
2969 Property* property = function->AsProperty();
2970
2971 // ------------------------------------------------------------------------
2972 // Fast-case: Use inline caching.
2973 // ---
2974 // According to ECMA-262, section 11.2.3, page 44, the function to call
2975 // must be resolved after the arguments have been evaluated. The IC code
2976 // automatically handles this by loading the arguments before the function
2977 // is resolved in cache misses (this also holds for megamorphic calls).
2978 // ------------------------------------------------------------------------
2979
2980 if (var != NULL && !var->is_this() && var->is_global()) {
2981 // ----------------------------------
2982 // JavaScript example: 'foo(1, 2, 3)' // foo is global
2983 // ----------------------------------
2984
2985 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002986 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002987 frame_->EmitPush(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002988
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002989 // Pass the global object as the receiver and let the IC stub
2990 // patch the stack to use the global proxy as 'this' in the
2991 // invoked function.
2992 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002993
2994 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002995 int arg_count = args->length();
2996 for (int i = 0; i < arg_count; i++) {
2997 LoadAndSpill(args->at(i));
2998 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002999
3000 // Setup the receiver register and call the IC initialization code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003001 Handle<Code> stub = ComputeCallInitialize(arg_count);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003002 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003003 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3004 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003005 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003006 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003007 frame_->Drop();
3008 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003009
3010 } else if (var != NULL && var->slot() != NULL &&
3011 var->slot()->type() == Slot::LOOKUP) {
3012 // ----------------------------------
3013 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3014 // ----------------------------------
3015
3016 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003017 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003018 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003019 frame_->EmitPush(r0);
3020 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003021 // r0: slot value; r1: receiver
3022
3023 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003024 frame_->EmitPush(r0); // function
3025 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003026
3027 // Call the function.
3028 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003029 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003030
3031 } else if (property != NULL) {
3032 // Check if the key is a literal string.
3033 Literal* literal = property->key()->AsLiteral();
3034
3035 if (literal != NULL && literal->handle()->IsSymbol()) {
3036 // ------------------------------------------------------------------
3037 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3038 // ------------------------------------------------------------------
3039
3040 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003041 __ mov(r0, Operand(literal->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003042 frame_->EmitPush(r0);
3043 LoadAndSpill(property->obj());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003044
3045 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003046 int arg_count = args->length();
3047 for (int i = 0; i < arg_count; i++) {
3048 LoadAndSpill(args->at(i));
3049 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003050
3051 // Set the receiver register and call the IC initialization code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003052 Handle<Code> stub = ComputeCallInitialize(arg_count);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003053 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003054 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003055 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003056
3057 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003058 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00003059
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003060 frame_->EmitPush(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003061
3062 } else {
3063 // -------------------------------------------
3064 // JavaScript example: 'array[index](1, 2, 3)'
3065 // -------------------------------------------
3066
3067 // Load the function to call from the property through a reference.
3068 Reference ref(this, property);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003069 ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003070
3071 // Pass receiver to called function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003072 if (property->is_synthetic()) {
3073 LoadGlobalReceiver(r0);
3074 } else {
3075 __ ldr(r0, frame_->ElementAt(ref.size()));
3076 frame_->EmitPush(r0);
3077 }
3078
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003079 // Call the function.
3080 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003081 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003082 }
3083
3084 } else {
3085 // ----------------------------------
3086 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3087 // ----------------------------------
3088
3089 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003090 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003091
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003092 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003093 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003094
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003095 // Call the function.
3096 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003097 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003098 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003099 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003100}
3101
3102
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003103void CodeGenerator::VisitCallEval(CallEval* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003104#ifdef DEBUG
3105 int original_height = frame_->height();
3106#endif
3107 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003108 Comment cmnt(masm_, "[ CallEval");
3109
3110 // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve
3111 // the function we need to call and the receiver of the call.
3112 // Then we call the resolved function using the given arguments.
3113
3114 ZoneList<Expression*>* args = node->arguments();
3115 Expression* function = node->expression();
3116
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003117 CodeForStatementPosition(node);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003118
3119 // Prepare stack for call to resolved function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003120 LoadAndSpill(function);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003121 __ mov(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003122 frame_->EmitPush(r2); // Slot for receiver
3123 int arg_count = args->length();
3124 for (int i = 0; i < arg_count; i++) {
3125 LoadAndSpill(args->at(i));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003126 }
3127
3128 // Prepare stack for call to ResolvePossiblyDirectEval.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003129 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3130 frame_->EmitPush(r1);
3131 if (arg_count > 0) {
3132 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3133 frame_->EmitPush(r1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003134 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003135 frame_->EmitPush(r2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003136 }
3137
3138 // Resolve the call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003139 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003140
3141 // Touch up stack with the right values for the function and the receiver.
3142 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003143 __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003144 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003145 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003146
3147 // Call the function.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003148 CodeForSourcePosition(node->position());
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003149
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003150 CallFunctionStub call_function(arg_count);
3151 frame_->CallStub(&call_function, arg_count + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003152
3153 __ ldr(cp, frame_->Context());
3154 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003155 frame_->Drop();
3156 frame_->EmitPush(r0);
3157 ASSERT(frame_->height() == original_height + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003158}
3159
3160
ager@chromium.org7c537e22008-10-16 08:43:32 +00003161void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003162#ifdef DEBUG
3163 int original_height = frame_->height();
3164#endif
3165 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003166 Comment cmnt(masm_, "[ CallNew");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003167 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003168
3169 // According to ECMA-262, section 11.2.2, page 44, the function
3170 // expression in new calls must be evaluated before the
3171 // arguments. This is different from ordinary calls, where the
3172 // actual function to call is resolved after the arguments have been
3173 // evaluated.
3174
3175 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003176 // receiver. There is no need to use the global proxy here because
3177 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003178 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003179 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003180
3181 // Push the arguments ("left-to-right") on the stack.
3182 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003183 int arg_count = args->length();
3184 for (int i = 0; i < arg_count; i++) {
3185 LoadAndSpill(args->at(i));
3186 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003187
mads.s.ager31e71382008-08-13 09:32:07 +00003188 // r0: the number of arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003189 Result num_args = allocator_->Allocate(r0);
3190 ASSERT(num_args.is_valid());
3191 __ mov(num_args.reg(), Operand(arg_count));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003192
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003193 // Load the function into r1 as per calling convention.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003194 Result function = allocator_->Allocate(r1);
3195 ASSERT(function.is_valid());
3196 __ ldr(function.reg(), frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003197
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003198 // Call the construct call builtin that handles allocation and
3199 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003200 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003201 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
3202 Result result = frame_->CallCodeObject(ic,
3203 RelocInfo::CONSTRUCT_CALL,
3204 &num_args,
3205 &function,
3206 arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003207
3208 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003209 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003210 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003211}
3212
3213
ager@chromium.org7c537e22008-10-16 08:43:32 +00003214void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003215 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003216 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003217 JumpTarget leave(this);
3218 LoadAndSpill(args->at(0));
3219 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003220 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003221 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003222 leave.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003223 // It is a heap object - get map.
3224 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3225 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
mads.s.ager31e71382008-08-13 09:32:07 +00003226 // if (!object->IsJSValue()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003227 __ cmp(r1, Operand(JS_VALUE_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003228 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003229 // Load the value.
3230 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003231 leave.Bind();
3232 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003233}
3234
3235
ager@chromium.org7c537e22008-10-16 08:43:32 +00003236void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003237 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003238 ASSERT(args->length() == 2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003239 JumpTarget leave(this);
3240 LoadAndSpill(args->at(0)); // Load the object.
3241 LoadAndSpill(args->at(1)); // Load the value.
3242 frame_->EmitPop(r0); // r0 contains value
3243 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003244 // if (object->IsSmi()) return object.
3245 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003246 leave.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003247 // It is a heap object - get map.
3248 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3249 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3250 // if (!object->IsJSValue()) return object.
3251 __ cmp(r2, Operand(JS_VALUE_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003252 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003253 // Store the value.
3254 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3255 // Update the write barrier.
3256 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3257 __ RecordWrite(r1, r2, r3);
3258 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003259 leave.Bind();
3260 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003261}
3262
3263
ager@chromium.org7c537e22008-10-16 08:43:32 +00003264void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003265 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003266 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003267 LoadAndSpill(args->at(0));
3268 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003269 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003270 cc_reg_ = eq;
3271}
3272
3273
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003274void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003275 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003276 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3277 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003278#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003279 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003280 LoadAndSpill(args->at(1));
3281 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003282 __ CallRuntime(Runtime::kLog, 2);
3283 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003284#endif
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003285 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003286 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003287}
3288
3289
ager@chromium.org7c537e22008-10-16 08:43:32 +00003290void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003291 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003292 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003293 LoadAndSpill(args->at(0));
3294 frame_->EmitPop(r0);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003295 __ tst(r0, Operand(kSmiTagMask | 0x80000000));
3296 cc_reg_ = eq;
3297}
3298
3299
kasper.lund7276f142008-07-30 08:49:36 +00003300// This should generate code that performs a charCodeAt() call or returns
3301// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3302// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003303void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003304 VirtualFrame::SpilledScope spilled_scope(this);
kasper.lund7276f142008-07-30 08:49:36 +00003305 ASSERT(args->length() == 2);
kasper.lund7276f142008-07-30 08:49:36 +00003306 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003307 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003308}
3309
3310
ager@chromium.org7c537e22008-10-16 08:43:32 +00003311void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003312 VirtualFrame::SpilledScope spilled_scope(this);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003313 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003314 LoadAndSpill(args->at(0));
3315 JumpTarget answer(this);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003316 // We need the CC bits to come out as not_equal in the case where the
3317 // object is a smi. This can't be done with the usual test opcode so
3318 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003319 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003320 __ and_(r1, r0, Operand(kSmiTagMask));
3321 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003322 answer.Branch(ne);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003323 // It is a heap object - get the map.
3324 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3325 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3326 // Check if the object is a JS array or not.
3327 __ cmp(r1, Operand(JS_ARRAY_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003328 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003329 cc_reg_ = eq;
3330}
3331
3332
ager@chromium.org7c537e22008-10-16 08:43:32 +00003333void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003334 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003335 ASSERT(args->length() == 0);
3336
mads.s.ager31e71382008-08-13 09:32:07 +00003337 // Seed the result with the formal parameters count, which will be used
3338 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003339 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
3340
3341 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003342 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003343 frame_->CallStub(&stub, 0);
3344 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003345}
3346
3347
ager@chromium.org7c537e22008-10-16 08:43:32 +00003348void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003349 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003350 ASSERT(args->length() == 1);
3351
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003352 // Satisfy contract with ArgumentsAccessStub:
3353 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003354 LoadAndSpill(args->at(0));
3355 frame_->EmitPop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003356 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003357
3358 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003359 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003360 frame_->CallStub(&stub, 0);
3361 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003362}
3363
3364
ager@chromium.org7c537e22008-10-16 08:43:32 +00003365void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003366 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003367 ASSERT(args->length() == 2);
3368
3369 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003370 LoadAndSpill(args->at(0));
3371 LoadAndSpill(args->at(1));
3372 frame_->EmitPop(r0);
3373 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003374 __ cmp(r0, Operand(r1));
3375 cc_reg_ = eq;
3376}
3377
3378
ager@chromium.org7c537e22008-10-16 08:43:32 +00003379void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003380#ifdef DEBUG
3381 int original_height = frame_->height();
3382#endif
3383 VirtualFrame::SpilledScope spilled_scope(this);
3384 if (CheckForInlineRuntimeCall(node)) {
3385 ASSERT((has_cc() && frame_->height() == original_height) ||
3386 (!has_cc() && frame_->height() == original_height + 1));
3387 return;
3388 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003389
3390 ZoneList<Expression*>* args = node->arguments();
3391 Comment cmnt(masm_, "[ CallRuntime");
3392 Runtime::Function* function = node->function();
3393
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003394 if (function != NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003395 // Push the arguments ("left-to-right").
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003396 int arg_count = args->length();
3397 for (int i = 0; i < arg_count; i++) {
3398 LoadAndSpill(args->at(i));
3399 }
mads.s.ager31e71382008-08-13 09:32:07 +00003400
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003401 // Call the C runtime function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003402 frame_->CallRuntime(function, arg_count);
3403 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003404
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003405 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00003406 // Prepare stack for calling JS runtime function.
3407 __ mov(r0, Operand(node->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003408 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003409 // Push the builtins object found in the current global object.
3410 __ ldr(r1, GlobalObject());
3411 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003412 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003413
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003414 int arg_count = args->length();
3415 for (int i = 0; i < arg_count; i++) {
3416 LoadAndSpill(args->at(i));
3417 }
mads.s.ager31e71382008-08-13 09:32:07 +00003418
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003419 // Call the JS runtime function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003420 Handle<Code> stub = ComputeCallInitialize(args->length());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003421 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003422 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003423 frame_->Drop();
3424 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003425 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003426 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003427}
3428
3429
ager@chromium.org7c537e22008-10-16 08:43:32 +00003430void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003431#ifdef DEBUG
3432 int original_height = frame_->height();
3433#endif
3434 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003435 Comment cmnt(masm_, "[ UnaryOperation");
3436
3437 Token::Value op = node->op();
3438
3439 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003440 LoadConditionAndSpill(node->expression(),
3441 NOT_INSIDE_TYPEOF,
3442 false_target(),
3443 true_target(),
3444 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003445 cc_reg_ = NegateCondition(cc_reg_);
3446
3447 } else if (op == Token::DELETE) {
3448 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003449 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003450 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003451 LoadAndSpill(property->obj());
3452 LoadAndSpill(property->key());
3453 Result arg_count = allocator_->Allocate(r0);
3454 ASSERT(arg_count.is_valid());
3455 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3456 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003457
mads.s.ager31e71382008-08-13 09:32:07 +00003458 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003459 Slot* slot = variable->slot();
3460 if (variable->is_global()) {
3461 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003462 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003463 frame_->EmitPush(r0);
3464 Result arg_count = allocator_->Allocate(r0);
3465 ASSERT(arg_count.is_valid());
3466 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3467 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003468
3469 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3470 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003471 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003472 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003473 frame_->EmitPush(r0);
3474 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003475 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003476 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003477 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003478 frame_->EmitPush(r0);
3479 Result arg_count = allocator_->Allocate(r0);
3480 ASSERT(arg_count.is_valid());
3481 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3482 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003483
mads.s.ager31e71382008-08-13 09:32:07 +00003484 } else {
3485 // Default: Result of deleting non-global, not dynamically
3486 // introduced variables is false.
3487 __ mov(r0, Operand(Factory::false_value()));
3488 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003489
3490 } else {
3491 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003492 LoadAndSpill(node->expression()); // may have side-effects
3493 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003494 __ mov(r0, Operand(Factory::true_value()));
3495 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003496 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003497
3498 } else if (op == Token::TYPEOF) {
3499 // Special case for loading the typeof expression; see comment on
3500 // LoadTypeofExpression().
3501 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003502 frame_->CallRuntime(Runtime::kTypeof, 1);
3503 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003504
3505 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003506 LoadAndSpill(node->expression());
3507 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003508 switch (op) {
3509 case Token::NOT:
3510 case Token::DELETE:
3511 case Token::TYPEOF:
3512 UNREACHABLE(); // handled above
3513 break;
3514
3515 case Token::SUB: {
3516 UnarySubStub stub;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003517 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003518 break;
3519 }
3520
3521 case Token::BIT_NOT: {
3522 // smi check
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003523 JumpTarget smi_label(this);
3524 JumpTarget continue_label(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003525 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003526 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003527
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003528 frame_->EmitPush(r0);
3529 Result arg_count = allocator_->Allocate(r0);
3530 ASSERT(arg_count.is_valid());
3531 __ mov(arg_count.reg(), Operand(0)); // not counting receiver
3532 frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003533
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003534 continue_label.Jump();
3535 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003536 __ mvn(r0, Operand(r0));
3537 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003538 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003539 break;
3540 }
3541
3542 case Token::VOID:
3543 // since the stack top is cached in r0, popping and then
3544 // pushing a value can be done by just writing to r0.
3545 __ mov(r0, Operand(Factory::undefined_value()));
3546 break;
3547
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003548 case Token::ADD: {
3549 // Smi check.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003550 JumpTarget continue_label(this);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003551 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003552 continue_label.Branch(eq);
3553 frame_->EmitPush(r0);
3554 Result arg_count = allocator_->Allocate(r0);
3555 ASSERT(arg_count.is_valid());
3556 __ mov(arg_count.reg(), Operand(0)); // not counting receiver
3557 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3558 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003559 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003560 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003561 default:
3562 UNREACHABLE();
3563 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003564 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003565 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003566 ASSERT((has_cc() && frame_->height() == original_height) ||
3567 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003568}
3569
3570
ager@chromium.org7c537e22008-10-16 08:43:32 +00003571void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003572#ifdef DEBUG
3573 int original_height = frame_->height();
3574#endif
3575 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003576 Comment cmnt(masm_, "[ CountOperation");
3577
3578 bool is_postfix = node->is_postfix();
3579 bool is_increment = node->op() == Token::INC;
3580
3581 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3582 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3583
3584 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003585 if (is_postfix) {
3586 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003587 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003588 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003589
3590 { Reference target(this, node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003591 if (target.is_illegal()) {
3592 // Spoof the virtual frame to have the expected height (one higher
3593 // than on entry).
3594 if (!is_postfix) {
3595 __ mov(r0, Operand(Smi::FromInt(0)));
3596 frame_->EmitPush(r0);
3597 }
3598 ASSERT(frame_->height() == original_height + 1);
3599 return;
3600 }
3601 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
3602 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003603
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003604 JumpTarget slow(this);
3605 JumpTarget exit(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003606
3607 // Load the value (1) into register r1.
3608 __ mov(r1, Operand(Smi::FromInt(1)));
3609
3610 // Check for smi operand.
3611 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003612 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003613
3614 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003615 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003616 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003617 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003618
3619 // Perform optimistic increment/decrement.
3620 if (is_increment) {
3621 __ add(r0, r0, Operand(r1), SetCC);
3622 } else {
3623 __ sub(r0, r0, Operand(r1), SetCC);
3624 }
3625
3626 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003627 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003628
3629 // Revert optimistic increment/decrement.
3630 if (is_increment) {
3631 __ sub(r0, r0, Operand(r1));
3632 } else {
3633 __ add(r0, r0, Operand(r1));
3634 }
3635
3636 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003637 slow.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003638
3639 // Postfix: Convert the operand to a number and store it as the result.
3640 if (is_postfix) {
3641 InvokeBuiltinStub stub(InvokeBuiltinStub::ToNumber, 2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003642 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003643 // Store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003644 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003645 }
3646
3647 // Compute the new value by calling the right JavaScript native.
3648 if (is_increment) {
3649 InvokeBuiltinStub stub(InvokeBuiltinStub::Inc, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003650 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003651 } else {
3652 InvokeBuiltinStub stub(InvokeBuiltinStub::Dec, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003653 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003654 }
3655
3656 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003657 exit.Bind();
3658 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003659 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003660 }
3661
3662 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003663 if (is_postfix) frame_->EmitPop(r0);
3664 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003665}
3666
3667
ager@chromium.org7c537e22008-10-16 08:43:32 +00003668void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003669#ifdef DEBUG
3670 int original_height = frame_->height();
3671#endif
3672 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003673 Comment cmnt(masm_, "[ BinaryOperation");
3674 Token::Value op = node->op();
3675
3676 // According to ECMA-262 section 11.11, page 58, the binary logical
3677 // operators must yield the result of one of the two expressions
3678 // before any ToBoolean() conversions. This means that the value
3679 // produced by a && or || operator is not necessarily a boolean.
3680
3681 // NOTE: If the left hand side produces a materialized value (not in
3682 // the CC register), we force the right hand side to do the
3683 // same. This is necessary because we may have to branch to the exit
3684 // after evaluating the left hand side (due to the shortcut
3685 // semantics), but the compiler must (statically) know if the result
3686 // of compiling the binary operation is materialized or not.
3687
3688 if (op == Token::AND) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003689 JumpTarget is_true(this);
3690 LoadConditionAndSpill(node->left(),
3691 NOT_INSIDE_TYPEOF,
3692 &is_true,
3693 false_target(),
3694 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003695 if (has_cc()) {
3696 Branch(false, false_target());
3697
3698 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003699 is_true.Bind();
3700 LoadConditionAndSpill(node->right(),
3701 NOT_INSIDE_TYPEOF,
3702 true_target(),
3703 false_target(),
3704 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003705
3706 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003707 JumpTarget pop_and_continue(this);
3708 JumpTarget exit(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003709
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003710 __ ldr(r0, frame_->Top()); // dup the stack top
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003711 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003712 // Avoid popping the result if it converts to 'false' using the
3713 // standard ToBoolean() conversion as described in ECMA-262,
3714 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003715 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003716 Branch(false, &exit);
3717
3718 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003719 pop_and_continue.Bind();
3720 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003721
3722 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003723 is_true.Bind();
3724 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003725
3726 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003727 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003728 }
3729
3730 } else if (op == Token::OR) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003731 JumpTarget is_false(this);
3732 LoadConditionAndSpill(node->left(),
3733 NOT_INSIDE_TYPEOF,
3734 true_target(),
3735 &is_false,
3736 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003737 if (has_cc()) {
3738 Branch(true, true_target());
3739
3740 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003741 is_false.Bind();
3742 LoadConditionAndSpill(node->right(),
3743 NOT_INSIDE_TYPEOF,
3744 true_target(),
3745 false_target(),
3746 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003747
3748 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003749 JumpTarget pop_and_continue(this);
3750 JumpTarget exit(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003751
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003752 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003753 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003754 // Avoid popping the result if it converts to 'true' using the
3755 // standard ToBoolean() conversion as described in ECMA-262,
3756 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003757 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003758 Branch(true, &exit);
3759
3760 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003761 pop_and_continue.Bind();
3762 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003763
3764 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003765 is_false.Bind();
3766 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003767
3768 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003769 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003770 }
3771
3772 } else {
3773 // Optimize for the case where (at least) one of the expressions
3774 // is a literal small integer.
3775 Literal* lliteral = node->left()->AsLiteral();
3776 Literal* rliteral = node->right()->AsLiteral();
3777
3778 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003779 LoadAndSpill(node->left());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003780 SmiOperation(node->op(), rliteral->handle(), false);
3781
3782 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003783 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003784 SmiOperation(node->op(), lliteral->handle(), true);
3785
3786 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003787 LoadAndSpill(node->left());
3788 LoadAndSpill(node->right());
kasper.lund7276f142008-07-30 08:49:36 +00003789 GenericBinaryOperation(node->op());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003790 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003791 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003792 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003793 ASSERT((has_cc() && frame_->height() == original_height) ||
3794 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003795}
3796
3797
ager@chromium.org7c537e22008-10-16 08:43:32 +00003798void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003799#ifdef DEBUG
3800 int original_height = frame_->height();
3801#endif
3802 VirtualFrame::SpilledScope spilled_scope(this);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003803 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003804 frame_->EmitPush(r0);
3805 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003806}
3807
3808
ager@chromium.org7c537e22008-10-16 08:43:32 +00003809void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003810#ifdef DEBUG
3811 int original_height = frame_->height();
3812#endif
3813 VirtualFrame::SpilledScope spilled_scope(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003814 Comment cmnt(masm_, "[ CompareOperation");
3815
3816 // Get the expressions from the node.
3817 Expression* left = node->left();
3818 Expression* right = node->right();
3819 Token::Value op = node->op();
3820
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003821 // To make null checks efficient, we check if either left or right is the
3822 // literal 'null'. If so, we optimize the code by inlining a null check
3823 // instead of calling the (very) general runtime routine for checking
3824 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003825 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003826 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003827 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003828 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003829 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
3830 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003831 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003832 LoadAndSpill(left_is_null ? right : left);
3833 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003834 __ cmp(r0, Operand(Factory::null_value()));
3835
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003836 // The 'null' value is only equal to 'undefined' if using non-strict
3837 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003838 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003839 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003840
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003841 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003842 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003843
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003844 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003845 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003846
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003847 // It can be an undetectable object.
3848 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3849 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
3850 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3851 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003852 }
3853
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003854 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003855 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003856 return;
3857 }
3858 }
3859
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003860 // To make typeof testing for natives implemented in JavaScript really
3861 // efficient, we generate special code for expressions of the form:
3862 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003863 UnaryOperation* operation = left->AsUnaryOperation();
3864 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
3865 (operation != NULL && operation->op() == Token::TYPEOF) &&
3866 (right->AsLiteral() != NULL &&
3867 right->AsLiteral()->handle()->IsString())) {
3868 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
3869
mads.s.ager31e71382008-08-13 09:32:07 +00003870 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003871 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003872 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003873
3874 if (check->Equals(Heap::number_symbol())) {
3875 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003876 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003877 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3878 __ cmp(r1, Operand(Factory::heap_number_map()));
3879 cc_reg_ = eq;
3880
3881 } else if (check->Equals(Heap::string_symbol())) {
3882 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003883 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003884
3885 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3886
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003887 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003888 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3889 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3890 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003891 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003892
3893 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3894 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
3895 cc_reg_ = lt;
3896
3897 } else if (check->Equals(Heap::boolean_symbol())) {
3898 __ cmp(r1, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003899 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003900 __ cmp(r1, Operand(Factory::false_value()));
3901 cc_reg_ = eq;
3902
3903 } else if (check->Equals(Heap::undefined_symbol())) {
3904 __ cmp(r1, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003905 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003906
3907 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003908 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003909
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003910 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003911 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3912 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3913 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3914 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3915
3916 cc_reg_ = eq;
3917
3918 } else if (check->Equals(Heap::function_symbol())) {
3919 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003920 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003921 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3922 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3923 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3924 cc_reg_ = eq;
3925
3926 } else if (check->Equals(Heap::object_symbol())) {
3927 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003928 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003929
3930 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
3931 __ cmp(r1, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003932 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003933
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003934 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003935 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
3936 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3937 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003938 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003939
3940 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
3941 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003942 false_target()->Branch(lt);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003943 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
3944 cc_reg_ = le;
3945
3946 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003947 // Uncommon case: typeof testing against a string literal that is
3948 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003949 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003950 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003951 ASSERT(!has_valid_frame() ||
3952 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003953 return;
3954 }
3955
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003956 LoadAndSpill(left);
3957 LoadAndSpill(right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003958 switch (op) {
3959 case Token::EQ:
3960 Comparison(eq, false);
3961 break;
3962
3963 case Token::LT:
3964 Comparison(lt);
3965 break;
3966
3967 case Token::GT:
3968 Comparison(gt);
3969 break;
3970
3971 case Token::LTE:
3972 Comparison(le);
3973 break;
3974
3975 case Token::GTE:
3976 Comparison(ge);
3977 break;
3978
3979 case Token::EQ_STRICT:
3980 Comparison(eq, true);
3981 break;
3982
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003983 case Token::IN: {
3984 Result arg_count = allocator_->Allocate(r0);
3985 ASSERT(arg_count.is_valid());
3986 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3987 Result result = frame_->InvokeBuiltin(Builtins::IN,
3988 CALL_JS,
3989 &arg_count,
3990 2);
3991 frame_->EmitPush(result.reg());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003992 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003993 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003994
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003995 case Token::INSTANCEOF: {
3996 Result arg_count = allocator_->Allocate(r0);
3997 ASSERT(arg_count.is_valid());
3998 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3999 Result result = frame_->InvokeBuiltin(Builtins::INSTANCE_OF,
4000 CALL_JS,
4001 &arg_count,
4002 2);
4003 __ tst(result.reg(), Operand(result.reg()));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004004 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004005 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004006 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004007
4008 default:
4009 UNREACHABLE();
4010 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004011 ASSERT((has_cc() && frame_->height() == original_height) ||
4012 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004013}
4014
4015
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004016#ifdef DEBUG
4017bool CodeGenerator::HasValidEntryRegisters() { return true; }
4018#endif
4019
4020
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004021#undef __
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004022#define __ masm->
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004023
ager@chromium.org7c537e22008-10-16 08:43:32 +00004024Handle<String> Reference::GetName() {
4025 ASSERT(type_ == NAMED);
4026 Property* property = expression_->AsProperty();
4027 if (property == NULL) {
4028 // Global variable reference treated as a named property reference.
4029 VariableProxy* proxy = expression_->AsVariableProxy();
4030 ASSERT(proxy->AsVariable() != NULL);
4031 ASSERT(proxy->AsVariable()->is_global());
4032 return proxy->name();
4033 } else {
4034 Literal* raw_name = property->key()->AsLiteral();
4035 ASSERT(raw_name != NULL);
4036 return Handle<String>(String::cast(*raw_name->handle()));
4037 }
4038}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004039
ager@chromium.org7c537e22008-10-16 08:43:32 +00004040
4041void Reference::GetValue(TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004042 ASSERT(!cgen_->in_spilled_code());
4043 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004044 ASSERT(!is_illegal());
4045 ASSERT(!cgen_->has_cc());
4046 MacroAssembler* masm = cgen_->masm();
4047 Property* property = expression_->AsProperty();
4048 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004049 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004050 }
4051
4052 switch (type_) {
4053 case SLOT: {
4054 Comment cmnt(masm, "[ Load from Slot");
4055 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4056 ASSERT(slot != NULL);
4057 cgen_->LoadFromSlot(slot, typeof_state);
4058 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004059 }
4060
ager@chromium.org7c537e22008-10-16 08:43:32 +00004061 case NAMED: {
4062 // TODO(1241834): Make sure that this it is safe to ignore the
4063 // distinction between expressions in a typeof and not in a typeof. If
4064 // there is a chance that reference errors can be thrown below, we
4065 // must distinguish between the two kinds of loads (typeof expression
4066 // loads must not throw a reference error).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004067 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004068 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004069 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004070 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004071 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4072 // Setup the name register.
4073 Result name_reg = cgen_->allocator()->Allocate(r2);
4074 ASSERT(name_reg.is_valid());
4075 __ mov(name_reg.reg(), Operand(name));
4076 ASSERT(var == NULL || var->is_global());
4077 RelocInfo::Mode rmode = (var == NULL)
4078 ? RelocInfo::CODE_TARGET
4079 : RelocInfo::CODE_TARGET_CONTEXT;
4080 Result answer = frame->CallCodeObject(ic, rmode, &name_reg, 0);
4081 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004082 break;
4083 }
4084
4085 case KEYED: {
4086 // TODO(1241834): Make sure that this it is safe to ignore the
4087 // distinction between expressions in a typeof and not in a typeof.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004088
4089 // TODO(181): Implement inlined version of array indexing once
4090 // loop nesting is properly tracked on ARM.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004091 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004092 Comment cmnt(masm, "[ Load from keyed Property");
4093 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004094 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004095 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004096 ASSERT(var == NULL || var->is_global());
4097 RelocInfo::Mode rmode = (var == NULL)
4098 ? RelocInfo::CODE_TARGET
4099 : RelocInfo::CODE_TARGET_CONTEXT;
4100 Result answer = frame->CallCodeObject(ic, rmode, 0);
4101 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004102 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004103 }
4104
4105 default:
4106 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004107 }
4108}
4109
4110
ager@chromium.org7c537e22008-10-16 08:43:32 +00004111void Reference::SetValue(InitState init_state) {
4112 ASSERT(!is_illegal());
4113 ASSERT(!cgen_->has_cc());
4114 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004115 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004116 Property* property = expression_->AsProperty();
4117 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004118 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004119 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004120
ager@chromium.org7c537e22008-10-16 08:43:32 +00004121 switch (type_) {
4122 case SLOT: {
4123 Comment cmnt(masm, "[ Store to Slot");
4124 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4125 ASSERT(slot != NULL);
4126 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004127 ASSERT(slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004128
ager@chromium.org7c537e22008-10-16 08:43:32 +00004129 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004130 frame->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004131 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004132 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004133
ager@chromium.org7c537e22008-10-16 08:43:32 +00004134 if (init_state == CONST_INIT) {
4135 // Same as the case for a normal store, but ignores attribute
4136 // (e.g. READ_ONLY) of context slot so that we can initialize
4137 // const properties (introduced via eval("const foo = (some
4138 // expr);")). Also, uses the current function context instead of
4139 // the top context.
4140 //
4141 // Note that we must declare the foo upon entry of eval(), via a
4142 // context slot declaration, but we cannot initialize it at the
4143 // same time, because the const declaration may be at the end of
4144 // the eval code (sigh...) and the const variable may have been
4145 // used before (where its value is 'undefined'). Thus, we can only
4146 // do the initialization when we actually encounter the expression
4147 // and when the expression operands are defined and valid, and
4148 // thus we need the split into 2 operations: declaration of the
4149 // context slot followed by initialization.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004150 frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004151 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004152 frame->CallRuntime(Runtime::kStoreContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004153 }
4154 // Storing a variable must keep the (new) value on the expression
4155 // stack. This is necessary for compiling assignment expressions.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004156 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004157
ager@chromium.org7c537e22008-10-16 08:43:32 +00004158 } else {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004159 ASSERT(!slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004160
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004161 JumpTarget exit(cgen_);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004162 if (init_state == CONST_INIT) {
4163 ASSERT(slot->var()->mode() == Variable::CONST);
4164 // Only the first const initialization must be executed (the slot
4165 // still contains 'the hole' value). When the assignment is
4166 // executed, the code is identical to a normal store (see below).
4167 Comment cmnt(masm, "[ Init const");
4168 __ ldr(r2, cgen_->SlotOperand(slot, r2));
4169 __ cmp(r2, Operand(Factory::the_hole_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004170 exit.Branch(ne);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004171 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004172
ager@chromium.org7c537e22008-10-16 08:43:32 +00004173 // We must execute the store. Storing a variable must keep the
4174 // (new) value on the stack. This is necessary for compiling
4175 // assignment expressions.
4176 //
4177 // Note: We will reach here even with slot->var()->mode() ==
4178 // Variable::CONST because of const declarations which will
4179 // initialize consts to 'the hole' value and by doing so, end up
4180 // calling this code. r2 may be loaded with context; used below in
4181 // RecordWrite.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004182 frame->EmitPop(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004183 __ str(r0, cgen_->SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004184 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004185 if (slot->type() == Slot::CONTEXT) {
4186 // Skip write barrier if the written value is a smi.
4187 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004188 exit.Branch(eq);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004189 // r2 is loaded with context when calling SlotOperand above.
4190 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
4191 __ mov(r3, Operand(offset));
4192 __ RecordWrite(r2, r3, r1);
4193 }
4194 // If we definitely did not jump over the assignment, we do not need
4195 // to bind the exit label. Doing so can defeat peephole
4196 // optimization.
4197 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004198 exit.Bind();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004199 }
4200 }
4201 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004202 }
4203
ager@chromium.org7c537e22008-10-16 08:43:32 +00004204 case NAMED: {
4205 Comment cmnt(masm, "[ Store to named Property");
4206 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004207 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004208 Handle<String> name(GetName());
4209
4210 Result value = cgen_->allocator()->Allocate(r0);
4211 ASSERT(value.is_valid());
4212 frame->EmitPop(value.reg());
4213
4214 // Setup the name register.
4215 Result property_name = cgen_->allocator()->Allocate(r2);
4216 ASSERT(property_name.is_valid());
4217 __ mov(property_name.reg(), Operand(name));
4218 Result answer = frame->CallCodeObject(ic,
4219 RelocInfo::CODE_TARGET,
4220 &value,
4221 &property_name,
4222 0);
4223 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004224 break;
4225 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004226
ager@chromium.org7c537e22008-10-16 08:43:32 +00004227 case KEYED: {
4228 Comment cmnt(masm, "[ Store to keyed Property");
4229 Property* property = expression_->AsProperty();
4230 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004231 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004232
4233 // Call IC code.
4234 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
4235 // TODO(1222589): Make the IC grab the values from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004236 Result value = cgen_->allocator()->Allocate(r0);
4237 ASSERT(value.is_valid());
4238 frame->EmitPop(value.reg()); // value
4239 Result result =
4240 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0);
4241 frame->EmitPush(result.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004242 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004243 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004244
4245 default:
4246 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004247 }
4248}
4249
4250
4251void GetPropertyStub::Generate(MacroAssembler* masm) {
4252 // sp[0]: key
4253 // sp[1]: receiver
4254 Label slow, fast;
4255 // Get the key and receiver object from the stack.
4256 __ ldm(ia, sp, r0.bit() | r1.bit());
4257 // Check that the key is a smi.
4258 __ tst(r0, Operand(kSmiTagMask));
4259 __ b(ne, &slow);
4260 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
4261 // Check that the object isn't a smi.
4262 __ tst(r1, Operand(kSmiTagMask));
4263 __ b(eq, &slow);
4264
4265 // Check that the object is some kind of JS object EXCEPT JS Value type.
4266 // In the case that the object is a value-wrapper object,
4267 // we enter the runtime system to make sure that indexing into string
4268 // objects work as intended.
4269 ASSERT(JS_OBJECT_TYPE > JS_VALUE_TYPE);
4270 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4271 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4272 __ cmp(r2, Operand(JS_OBJECT_TYPE));
4273 __ b(lt, &slow);
4274
4275 // Get the elements array of the object.
4276 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
4277 // Check that the object is in fast mode (not dictionary).
4278 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
4279 __ cmp(r3, Operand(Factory::hash_table_map()));
4280 __ b(eq, &slow);
4281 // Check that the key (index) is within bounds.
4282 __ ldr(r3, FieldMemOperand(r1, Array::kLengthOffset));
4283 __ cmp(r0, Operand(r3));
4284 __ b(lo, &fast);
4285
4286 // Slow case: Push extra copies of the arguments (2).
4287 __ bind(&slow);
4288 __ ldm(ia, sp, r0.bit() | r1.bit());
4289 __ stm(db_w, sp, r0.bit() | r1.bit());
4290 // Do tail-call to runtime routine.
4291 __ TailCallRuntime(ExternalReference(Runtime::kGetProperty), 2);
4292
4293 // Fast case: Do the load.
4294 __ bind(&fast);
4295 __ add(r3, r1, Operand(Array::kHeaderSize - kHeapObjectTag));
4296 __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2));
4297 __ cmp(r0, Operand(Factory::the_hole_value()));
4298 // In case the loaded value is the_hole we have to consult GetProperty
4299 // to ensure the prototype chain is searched.
4300 __ b(eq, &slow);
4301
4302 __ StubReturn(1);
4303}
4304
4305
4306void SetPropertyStub::Generate(MacroAssembler* masm) {
4307 // r0 : value
4308 // sp[0] : key
4309 // sp[1] : receiver
4310
4311 Label slow, fast, array, extra, exit;
4312 // Get the key and the object from the stack.
4313 __ ldm(ia, sp, r1.bit() | r3.bit()); // r1 = key, r3 = receiver
4314 // Check that the key is a smi.
4315 __ tst(r1, Operand(kSmiTagMask));
4316 __ b(ne, &slow);
4317 // Check that the object isn't a smi.
4318 __ tst(r3, Operand(kSmiTagMask));
4319 __ b(eq, &slow);
4320 // Get the type of the object from its map.
4321 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
4322 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4323 // Check if the object is a JS array or not.
4324 __ cmp(r2, Operand(JS_ARRAY_TYPE));
4325 __ b(eq, &array);
4326 // Check that the object is some kind of JS object.
4327 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
4328 __ b(lt, &slow);
4329
4330
4331 // Object case: Check key against length in the elements array.
4332 __ ldr(r3, FieldMemOperand(r3, JSObject::kElementsOffset));
4333 // Check that the object is in fast mode (not dictionary).
4334 __ ldr(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
4335 __ cmp(r2, Operand(Factory::hash_table_map()));
4336 __ b(eq, &slow);
4337 // Untag the key (for checking against untagged length in the fixed array).
4338 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
4339 // Compute address to store into and check array bounds.
4340 __ add(r2, r3, Operand(Array::kHeaderSize - kHeapObjectTag));
4341 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2));
4342 __ ldr(ip, FieldMemOperand(r3, Array::kLengthOffset));
4343 __ cmp(r1, Operand(ip));
4344 __ b(lo, &fast);
4345
4346
4347 // Slow case: Push extra copies of the arguments (3).
4348 __ bind(&slow);
4349 __ ldm(ia, sp, r1.bit() | r3.bit()); // r0 == value, r1 == key, r3 == object
4350 __ stm(db_w, sp, r0.bit() | r1.bit() | r3.bit());
4351 // Do tail-call to runtime routine.
4352 __ TailCallRuntime(ExternalReference(Runtime::kSetProperty), 3);
4353
4354
4355 // Extra capacity case: Check if there is extra capacity to
4356 // perform the store and update the length. Used for adding one
4357 // element to the array by writing to array[array.length].
4358 // r0 == value, r1 == key, r2 == elements, r3 == object
4359 __ bind(&extra);
4360 __ b(ne, &slow); // do not leave holes in the array
4361 __ mov(r1, Operand(r1, ASR, kSmiTagSize)); // untag
4362 __ ldr(ip, FieldMemOperand(r2, Array::kLengthOffset));
4363 __ cmp(r1, Operand(ip));
4364 __ b(hs, &slow);
4365 __ mov(r1, Operand(r1, LSL, kSmiTagSize)); // restore tag
4366 __ add(r1, r1, Operand(1 << kSmiTagSize)); // and increment
4367 __ str(r1, FieldMemOperand(r3, JSArray::kLengthOffset));
4368 __ mov(r3, Operand(r2));
4369 // NOTE: Computing the address to store into must take the fact
4370 // that the key has been incremented into account.
4371 int displacement = Array::kHeaderSize - kHeapObjectTag -
4372 ((1 << kSmiTagSize) * 2);
4373 __ add(r2, r2, Operand(displacement));
4374 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
4375 __ b(&fast);
4376
4377
4378 // Array case: Get the length and the elements array from the JS
4379 // array. Check that the array is in fast mode; if it is the
4380 // length is always a smi.
4381 // r0 == value, r3 == object
4382 __ bind(&array);
4383 __ ldr(r2, FieldMemOperand(r3, JSObject::kElementsOffset));
4384 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
4385 __ cmp(r1, Operand(Factory::hash_table_map()));
4386 __ b(eq, &slow);
4387
4388 // Check the key against the length in the array, compute the
4389 // address to store into and fall through to fast case.
4390 __ ldr(r1, MemOperand(sp));
4391 // r0 == value, r1 == key, r2 == elements, r3 == object.
4392 __ ldr(ip, FieldMemOperand(r3, JSArray::kLengthOffset));
4393 __ cmp(r1, Operand(ip));
4394 __ b(hs, &extra);
4395 __ mov(r3, Operand(r2));
4396 __ add(r2, r2, Operand(Array::kHeaderSize - kHeapObjectTag));
4397 __ add(r2, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
4398
4399
4400 // Fast case: Do the store.
4401 // r0 == value, r2 == address to store into, r3 == elements
4402 __ bind(&fast);
4403 __ str(r0, MemOperand(r2));
4404 // Skip write barrier if the written value is a smi.
4405 __ tst(r0, Operand(kSmiTagMask));
4406 __ b(eq, &exit);
4407 // Update write barrier for the elements array address.
4408 __ sub(r1, r2, Operand(r3));
4409 __ RecordWrite(r3, r1, r2);
4410 __ bind(&exit);
4411 __ StubReturn(1);
4412}
4413
4414
4415void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
4416 // r1 : x
4417 // r0 : y
4418 // result : r0
4419
4420 switch (op_) {
4421 case Token::ADD: {
4422 Label slow, exit;
4423 // fast path
4424 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
4425 __ add(r0, r1, Operand(r0), SetCC); // add y optimistically
4426 // go slow-path in case of overflow
4427 __ b(vs, &slow);
4428 // go slow-path in case of non-smi operands
4429 ASSERT(kSmiTag == 0); // adjust code below
4430 __ tst(r2, Operand(kSmiTagMask));
4431 __ b(eq, &exit);
4432 // slow path
4433 __ bind(&slow);
4434 __ sub(r0, r0, Operand(r1)); // revert optimistic add
4435 __ push(r1);
4436 __ push(r0);
4437 __ mov(r0, Operand(1)); // set number of arguments
4438 __ InvokeBuiltin(Builtins::ADD, JUMP_JS);
4439 // done
4440 __ bind(&exit);
4441 break;
4442 }
4443
4444 case Token::SUB: {
4445 Label slow, exit;
4446 // fast path
4447 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
4448 __ sub(r3, r1, Operand(r0), SetCC); // subtract y optimistically
4449 // go slow-path in case of overflow
4450 __ b(vs, &slow);
4451 // go slow-path in case of non-smi operands
4452 ASSERT(kSmiTag == 0); // adjust code below
4453 __ tst(r2, Operand(kSmiTagMask));
4454 __ mov(r0, Operand(r3), LeaveCC, eq); // conditionally set r0 to result
4455 __ b(eq, &exit);
4456 // slow path
4457 __ bind(&slow);
4458 __ push(r1);
4459 __ push(r0);
4460 __ mov(r0, Operand(1)); // set number of arguments
4461 __ InvokeBuiltin(Builtins::SUB, JUMP_JS);
4462 // done
4463 __ bind(&exit);
4464 break;
4465 }
4466
4467 case Token::MUL: {
4468 Label slow, exit;
4469 // tag check
4470 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
4471 ASSERT(kSmiTag == 0); // adjust code below
4472 __ tst(r2, Operand(kSmiTagMask));
4473 __ b(ne, &slow);
4474 // remove tag from one operand (but keep sign), so that result is smi
4475 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
4476 // do multiplication
4477 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1
4478 // go slow on overflows (overflow bit is not set)
4479 __ mov(ip, Operand(r3, ASR, 31));
4480 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
4481 __ b(ne, &slow);
4482 // go slow on zero result to handle -0
4483 __ tst(r3, Operand(r3));
4484 __ mov(r0, Operand(r3), LeaveCC, ne);
4485 __ b(ne, &exit);
4486 // slow case
4487 __ bind(&slow);
4488 __ push(r1);
4489 __ push(r0);
4490 __ mov(r0, Operand(1)); // set number of arguments
4491 __ InvokeBuiltin(Builtins::MUL, JUMP_JS);
4492 // done
4493 __ bind(&exit);
4494 break;
4495 }
4496
4497 case Token::BIT_OR:
4498 case Token::BIT_AND:
4499 case Token::BIT_XOR: {
4500 Label slow, exit;
4501 // tag check
4502 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
4503 ASSERT(kSmiTag == 0); // adjust code below
4504 __ tst(r2, Operand(kSmiTagMask));
4505 __ b(ne, &slow);
4506 switch (op_) {
4507 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
4508 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
4509 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
4510 default: UNREACHABLE();
4511 }
4512 __ b(&exit);
4513 __ bind(&slow);
4514 __ push(r1); // restore stack
4515 __ push(r0);
4516 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
4517 switch (op_) {
4518 case Token::BIT_OR:
4519 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
4520 break;
4521 case Token::BIT_AND:
4522 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
4523 break;
4524 case Token::BIT_XOR:
4525 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
4526 break;
4527 default:
4528 UNREACHABLE();
4529 }
4530 __ bind(&exit);
4531 break;
4532 }
4533
4534 case Token::SHL:
4535 case Token::SHR:
4536 case Token::SAR: {
4537 Label slow, exit;
4538 // tag check
4539 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
4540 ASSERT(kSmiTag == 0); // adjust code below
4541 __ tst(r2, Operand(kSmiTagMask));
4542 __ b(ne, &slow);
4543 // remove tags from operands (but keep sign)
4544 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
4545 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
4546 // use only the 5 least significant bits of the shift count
4547 __ and_(r2, r2, Operand(0x1f));
4548 // perform operation
4549 switch (op_) {
4550 case Token::SAR:
4551 __ mov(r3, Operand(r3, ASR, r2));
4552 // no checks of result necessary
4553 break;
4554
4555 case Token::SHR:
4556 __ mov(r3, Operand(r3, LSR, r2));
4557 // check that the *unsigned* result fits in a smi
4558 // neither of the two high-order bits can be set:
4559 // - 0x80000000: high bit would be lost when smi tagging
4560 // - 0x40000000: this number would convert to negative when
4561 // smi tagging these two cases can only happen with shifts
4562 // by 0 or 1 when handed a valid smi
4563 __ and_(r2, r3, Operand(0xc0000000), SetCC);
4564 __ b(ne, &slow);
4565 break;
4566
4567 case Token::SHL:
4568 __ mov(r3, Operand(r3, LSL, r2));
4569 // check that the *signed* result fits in a smi
4570 __ add(r2, r3, Operand(0x40000000), SetCC);
4571 __ b(mi, &slow);
4572 break;
4573
4574 default: UNREACHABLE();
4575 }
4576 // tag result and store it in r0
4577 ASSERT(kSmiTag == 0); // adjust code below
4578 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
4579 __ b(&exit);
4580 // slow case
4581 __ bind(&slow);
4582 __ push(r1); // restore stack
4583 __ push(r0);
4584 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
4585 switch (op_) {
4586 case Token::SAR: __ InvokeBuiltin(Builtins::SAR, JUMP_JS); break;
4587 case Token::SHR: __ InvokeBuiltin(Builtins::SHR, JUMP_JS); break;
4588 case Token::SHL: __ InvokeBuiltin(Builtins::SHL, JUMP_JS); break;
4589 default: UNREACHABLE();
4590 }
4591 __ bind(&exit);
4592 break;
4593 }
4594
4595 default: UNREACHABLE();
4596 }
4597 __ Ret();
4598}
4599
4600
4601void StackCheckStub::Generate(MacroAssembler* masm) {
4602 Label within_limit;
4603 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
4604 __ ldr(ip, MemOperand(ip));
4605 __ cmp(sp, Operand(ip));
4606 __ b(hs, &within_limit);
4607 // Do tail-call to runtime routine.
4608 __ push(r0);
4609 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
4610 __ bind(&within_limit);
4611
4612 __ StubReturn(1);
4613}
4614
4615
4616void UnarySubStub::Generate(MacroAssembler* masm) {
4617 Label undo;
4618 Label slow;
4619 Label done;
4620
4621 // Enter runtime system if the value is not a smi.
4622 __ tst(r0, Operand(kSmiTagMask));
4623 __ b(ne, &slow);
4624
4625 // Enter runtime system if the value of the expression is zero
4626 // to make sure that we switch between 0 and -0.
4627 __ cmp(r0, Operand(0));
4628 __ b(eq, &slow);
4629
4630 // The value of the expression is a smi that is not zero. Try
4631 // optimistic subtraction '0 - value'.
4632 __ rsb(r1, r0, Operand(0), SetCC);
4633 __ b(vs, &slow);
4634
4635 // If result is a smi we are done.
4636 __ tst(r1, Operand(kSmiTagMask));
4637 __ mov(r0, Operand(r1), LeaveCC, eq); // conditionally set r0 to result
4638 __ b(eq, &done);
4639
4640 // Enter runtime system.
4641 __ bind(&slow);
4642 __ push(r0);
4643 __ mov(r0, Operand(0)); // set number of arguments
4644 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
4645
4646 __ bind(&done);
4647 __ StubReturn(1);
4648}
4649
4650
4651void InvokeBuiltinStub::Generate(MacroAssembler* masm) {
4652 __ push(r0);
4653 __ mov(r0, Operand(0)); // set number of arguments
4654 switch (kind_) {
4655 case ToNumber: __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_JS); break;
4656 case Inc: __ InvokeBuiltin(Builtins::INC, JUMP_JS); break;
4657 case Dec: __ InvokeBuiltin(Builtins::DEC, JUMP_JS); break;
4658 default: UNREACHABLE();
4659 }
4660 __ StubReturn(argc_);
4661}
4662
4663
4664void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
4665 // r0 holds exception
4666 ASSERT(StackHandlerConstants::kSize == 6 * kPointerSize); // adjust this code
4667 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
4668 __ ldr(sp, MemOperand(r3));
4669 __ pop(r2); // pop next in chain
4670 __ str(r2, MemOperand(r3));
4671 // restore parameter- and frame-pointer and pop state.
4672 __ ldm(ia_w, sp, r3.bit() | pp.bit() | fp.bit());
4673 // Before returning we restore the context from the frame pointer if not NULL.
4674 // The frame pointer is NULL in the exception handler of a JS entry frame.
4675 __ cmp(fp, Operand(0));
4676 // Set cp to NULL if fp is NULL.
4677 __ mov(cp, Operand(0), LeaveCC, eq);
4678 // Restore cp otherwise.
4679 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
4680 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
4681 __ pop(pc);
4682}
4683
4684
4685void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
4686 // Fetch top stack handler.
4687 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
4688 __ ldr(r3, MemOperand(r3));
4689
4690 // Unwind the handlers until the ENTRY handler is found.
4691 Label loop, done;
4692 __ bind(&loop);
4693 // Load the type of the current stack handler.
4694 const int kStateOffset = StackHandlerConstants::kAddressDisplacement +
4695 StackHandlerConstants::kStateOffset;
4696 __ ldr(r2, MemOperand(r3, kStateOffset));
4697 __ cmp(r2, Operand(StackHandler::ENTRY));
4698 __ b(eq, &done);
4699 // Fetch the next handler in the list.
4700 const int kNextOffset = StackHandlerConstants::kAddressDisplacement +
4701 StackHandlerConstants::kNextOffset;
4702 __ ldr(r3, MemOperand(r3, kNextOffset));
4703 __ jmp(&loop);
4704 __ bind(&done);
4705
4706 // Set the top handler address to next handler past the current ENTRY handler.
4707 __ ldr(r0, MemOperand(r3, kNextOffset));
4708 __ mov(r2, Operand(ExternalReference(Top::k_handler_address)));
4709 __ str(r0, MemOperand(r2));
4710
4711 // Set external caught exception to false.
4712 __ mov(r0, Operand(false));
4713 ExternalReference external_caught(Top::k_external_caught_exception_address);
4714 __ mov(r2, Operand(external_caught));
4715 __ str(r0, MemOperand(r2));
4716
4717 // Set pending exception and r0 to out of memory exception.
4718 Failure* out_of_memory = Failure::OutOfMemoryException();
4719 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
4720 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
4721 __ str(r0, MemOperand(r2));
4722
4723 // Restore the stack to the address of the ENTRY handler
4724 __ mov(sp, Operand(r3));
4725
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004726 // Stack layout at this point. See also PushTryHandler
4727 // r3, sp -> next handler
4728 // state (ENTRY)
4729 // pp
4730 // fp
4731 // lr
4732
4733 // Discard ENTRY state (r2 is not used), and restore parameter-
4734 // and frame-pointer and pop state.
4735 __ ldm(ia_w, sp, r2.bit() | r3.bit() | pp.bit() | fp.bit());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004736 // Before returning we restore the context from the frame pointer if not NULL.
4737 // The frame pointer is NULL in the exception handler of a JS entry frame.
4738 __ cmp(fp, Operand(0));
4739 // Set cp to NULL if fp is NULL.
4740 __ mov(cp, Operand(0), LeaveCC, eq);
4741 // Restore cp otherwise.
4742 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
4743 if (kDebug && FLAG_debug_code) __ mov(lr, Operand(pc));
4744 __ pop(pc);
4745}
4746
4747
4748void CEntryStub::GenerateCore(MacroAssembler* masm,
4749 Label* throw_normal_exception,
4750 Label* throw_out_of_memory_exception,
4751 StackFrame::Type frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004752 bool do_gc,
4753 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004754 // r0: result parameter for PerformGC, if any
4755 // r4: number of arguments including receiver (C callee-saved)
4756 // r5: pointer to builtin function (C callee-saved)
4757 // r6: pointer to the first argument (C callee-saved)
4758
4759 if (do_gc) {
4760 // Passing r0.
4761 __ Call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
4762 }
4763
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004764 ExternalReference scope_depth =
4765 ExternalReference::heap_always_allocate_scope_depth();
4766 if (always_allocate) {
4767 __ mov(r0, Operand(scope_depth));
4768 __ ldr(r1, MemOperand(r0));
4769 __ add(r1, r1, Operand(1));
4770 __ str(r1, MemOperand(r0));
4771 }
4772
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004773 // Call C built-in.
4774 // r0 = argc, r1 = argv
4775 __ mov(r0, Operand(r4));
4776 __ mov(r1, Operand(r6));
4777
4778 // TODO(1242173): To let the GC traverse the return address of the exit
4779 // frames, we need to know where the return address is. Right now,
4780 // we push it on the stack to be able to find it again, but we never
4781 // restore from it in case of changes, which makes it impossible to
4782 // support moving the C entry code stub. This should be fixed, but currently
4783 // this is OK because the CEntryStub gets generated so early in the V8 boot
4784 // sequence that it is not moving ever.
4785 __ add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
4786 __ push(lr);
4787#if !defined(__arm__)
4788 // Notify the simulator of the transition to C code.
4789 __ swi(assembler::arm::call_rt_r5);
4790#else /* !defined(__arm__) */
4791 __ mov(pc, Operand(r5));
4792#endif /* !defined(__arm__) */
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004793
4794 if (always_allocate) {
4795 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
4796 // though (contain the result).
4797 __ mov(r2, Operand(scope_depth));
4798 __ ldr(r3, MemOperand(r2));
4799 __ sub(r3, r3, Operand(1));
4800 __ str(r3, MemOperand(r2));
4801 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004802
4803 // check for failure result
4804 Label failure_returned;
4805 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4806 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
4807 __ add(r2, r0, Operand(1));
4808 __ tst(r2, Operand(kFailureTagMask));
4809 __ b(eq, &failure_returned);
4810
4811 // Exit C frame and return.
4812 // r0:r1: result
4813 // sp: stack pointer
4814 // fp: frame pointer
4815 // pp: caller's parameter pointer pp (restored as C callee-saved)
4816 __ LeaveExitFrame(frame_type);
4817
4818 // check if we should retry or throw exception
4819 Label retry;
4820 __ bind(&failure_returned);
4821 ASSERT(Failure::RETRY_AFTER_GC == 0);
4822 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
4823 __ b(eq, &retry);
4824
4825 Label continue_exception;
4826 // If the returned failure is EXCEPTION then promote Top::pending_exception().
4827 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
4828 __ b(ne, &continue_exception);
4829
4830 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00004831 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004832 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00004833 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004834 __ ldr(r0, MemOperand(ip));
4835 __ str(r3, MemOperand(ip));
4836
4837 __ bind(&continue_exception);
4838 // Special handling of out of memory exception.
4839 Failure* out_of_memory = Failure::OutOfMemoryException();
4840 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
4841 __ b(eq, throw_out_of_memory_exception);
4842
4843 // Handle normal exception.
4844 __ jmp(throw_normal_exception);
4845
4846 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
4847}
4848
4849
4850void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
4851 // Called from JavaScript; parameters are on stack as if calling JS function
4852 // r0: number of arguments including receiver
4853 // r1: pointer to builtin function
4854 // fp: frame pointer (restored after C call)
4855 // sp: stack pointer (restored as callee's pp after C call)
4856 // cp: current context (C callee-saved)
4857 // pp: caller's parameter pointer pp (C callee-saved)
4858
4859 // NOTE: Invocations of builtins may return failure objects
4860 // instead of a proper result. The builtin entry handles
4861 // this by performing a garbage collection and retrying the
4862 // builtin once.
4863
4864 StackFrame::Type frame_type = is_debug_break
4865 ? StackFrame::EXIT_DEBUG
4866 : StackFrame::EXIT;
4867
4868 // Enter the exit frame that transitions from JavaScript to C++.
4869 __ EnterExitFrame(frame_type);
4870
4871 // r4: number of arguments (C callee-saved)
4872 // r5: pointer to builtin function (C callee-saved)
4873 // r6: pointer to first argument (C callee-saved)
4874
4875 Label throw_out_of_memory_exception;
4876 Label throw_normal_exception;
4877
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004878 // Call into the runtime system. Collect garbage before the call if
4879 // running with --gc-greedy set.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004880 if (FLAG_gc_greedy) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004881 Failure* failure = Failure::RetryAfterGC(0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004882 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
4883 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004884 GenerateCore(masm, &throw_normal_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004885 &throw_out_of_memory_exception,
4886 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004887 FLAG_gc_greedy,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004888 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004889
4890 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004891 GenerateCore(masm,
4892 &throw_normal_exception,
4893 &throw_out_of_memory_exception,
4894 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00004895 true,
4896 false);
4897
4898 // Do full GC and retry runtime call one final time.
4899 Failure* failure = Failure::InternalError();
4900 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
4901 GenerateCore(masm,
4902 &throw_normal_exception,
4903 &throw_out_of_memory_exception,
4904 frame_type,
4905 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004906 true);
4907
4908 __ bind(&throw_out_of_memory_exception);
4909 GenerateThrowOutOfMemory(masm);
4910 // control flow for generated will not return.
4911
4912 __ bind(&throw_normal_exception);
4913 GenerateThrowTOS(masm);
4914}
4915
4916
4917void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4918 // r0: code entry
4919 // r1: function
4920 // r2: receiver
4921 // r3: argc
4922 // [sp+0]: argv
4923
4924 Label invoke, exit;
4925
4926 // Called from C, so do not pop argc and args on exit (preserve sp)
4927 // No need to save register-passed args
4928 // Save callee-saved registers (incl. cp, pp, and fp), sp, and lr
4929 __ stm(db_w, sp, kCalleeSaved | lr.bit());
4930
4931 // Get address of argv, see stm above.
4932 // r0: code entry
4933 // r1: function
4934 // r2: receiver
4935 // r3: argc
4936 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
4937 __ ldr(r4, MemOperand(r4)); // argv
4938
4939 // Push a frame with special values setup to mark it as an entry frame.
4940 // r0: code entry
4941 // r1: function
4942 // r2: receiver
4943 // r3: argc
4944 // r4: argv
4945 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4946 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
4947 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
4948 __ mov(r6, Operand(Smi::FromInt(marker)));
4949 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
4950 __ ldr(r5, MemOperand(r5));
4951 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
4952
4953 // Setup frame pointer for the frame to be pushed.
4954 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
4955
4956 // Call a faked try-block that does the invoke.
4957 __ bl(&invoke);
4958
4959 // Caught exception: Store result (exception) in the pending
4960 // exception field in the JSEnv and return a failure sentinel.
4961 // Coming in here the fp will be invalid because the PushTryHandler below
4962 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00004963 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004964 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004965 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004966 __ b(&exit);
4967
4968 // Invoke: Link this frame into the handler chain.
4969 __ bind(&invoke);
4970 // Must preserve r0-r4, r5-r7 are available.
4971 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4972 // If an exception not caught by another handler occurs, this handler returns
4973 // control to the code after the bl(&invoke) above, which restores all
4974 // kCalleeSaved registers (including cp, pp and fp) to their saved values
4975 // before returning a failure to C.
4976
4977 // Clear any pending exceptions.
4978 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
4979 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00004980 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004981 __ str(r5, MemOperand(ip));
4982
4983 // Invoke the function by calling through JS entry trampoline builtin.
4984 // Notice that we cannot store a reference to the trampoline code directly in
4985 // this stub, because runtime stubs are not traversed when doing GC.
4986
4987 // Expected registers by Builtins::JSEntryTrampoline
4988 // r0: code entry
4989 // r1: function
4990 // r2: receiver
4991 // r3: argc
4992 // r4: argv
4993 if (is_construct) {
4994 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
4995 __ mov(ip, Operand(construct_entry));
4996 } else {
4997 ExternalReference entry(Builtins::JSEntryTrampoline);
4998 __ mov(ip, Operand(entry));
4999 }
5000 __ ldr(ip, MemOperand(ip)); // deref address
5001
5002 // Branch and link to JSEntryTrampoline
5003 __ mov(lr, Operand(pc));
5004 __ add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
5005
5006 // Unlink this frame from the handler chain. When reading the
5007 // address of the next handler, there is no need to use the address
5008 // displacement since the current stack pointer (sp) points directly
5009 // to the stack handler.
5010 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
5011 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
5012 __ str(r3, MemOperand(ip));
5013 // No need to restore registers
5014 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
5015
5016 __ bind(&exit); // r0 holds result
5017 // Restore the top frame descriptors from the stack.
5018 __ pop(r3);
5019 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
5020 __ str(r3, MemOperand(ip));
5021
5022 // Reset the stack to the callee saved registers.
5023 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
5024
5025 // Restore callee-saved registers and return.
5026#ifdef DEBUG
5027 if (FLAG_debug_code) __ mov(lr, Operand(pc));
5028#endif
5029 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
5030}
5031
5032
ager@chromium.org7c537e22008-10-16 08:43:32 +00005033void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005034 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005035 Label adaptor;
5036 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5037 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5038 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
ager@chromium.org7c537e22008-10-16 08:43:32 +00005039 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005040
ager@chromium.org7c537e22008-10-16 08:43:32 +00005041 // Nothing to do: The formal number of parameters has already been
5042 // passed in register r0 by calling function. Just return it.
5043 __ mov(pc, lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005044
ager@chromium.org7c537e22008-10-16 08:43:32 +00005045 // Arguments adaptor case: Read the arguments length from the
5046 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005047 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005048 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +00005049 __ mov(pc, lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005050}
5051
5052
ager@chromium.org7c537e22008-10-16 08:43:32 +00005053void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
5054 // The displacement is the offset of the last parameter (if any)
5055 // relative to the frame pointer.
5056 static const int kDisplacement =
5057 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005058
ager@chromium.org7c537e22008-10-16 08:43:32 +00005059 // Check that the key is a smi.
5060 Label slow;
5061 __ tst(r1, Operand(kSmiTagMask));
5062 __ b(ne, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005063
ager@chromium.org7c537e22008-10-16 08:43:32 +00005064 // Check if the calling frame is an arguments adaptor frame.
5065 Label adaptor;
5066 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5067 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5068 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
5069 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005070
ager@chromium.org7c537e22008-10-16 08:43:32 +00005071 // Check index against formal parameters count limit passed in
5072 // through register eax. Use unsigned comparison to get negative
5073 // check for free.
5074 __ cmp(r1, r0);
5075 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005076
ager@chromium.org7c537e22008-10-16 08:43:32 +00005077 // Read the argument from the stack and return it.
5078 __ sub(r3, r0, r1);
5079 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
5080 __ ldr(r0, MemOperand(r3, kDisplacement));
5081 __ mov(pc, lr);
5082
5083 // Arguments adaptor case: Check index against actual arguments
5084 // limit found in the arguments adaptor frame. Use unsigned
5085 // comparison to get negative check for free.
5086 __ bind(&adaptor);
5087 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
5088 __ cmp(r1, r0);
5089 __ b(cs, &slow);
5090
5091 // Read the argument from the adaptor frame and return it.
5092 __ sub(r3, r0, r1);
5093 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
5094 __ ldr(r0, MemOperand(r3, kDisplacement));
5095 __ mov(pc, lr);
5096
5097 // Slow-case: Handle non-smi or out-of-bounds access to arguments
5098 // by calling the runtime system.
5099 __ bind(&slow);
5100 __ push(r1);
5101 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
5102}
5103
5104
5105void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
5106 // Check if the calling frame is an arguments adaptor frame.
5107 Label runtime;
5108 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5109 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
5110 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
5111 __ b(ne, &runtime);
5112
5113 // Patch the arguments.length and the parameters pointer.
5114 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
5115 __ str(r0, MemOperand(sp, 0 * kPointerSize));
5116 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
5117 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
5118 __ str(r3, MemOperand(sp, 1 * kPointerSize));
5119
5120 // Do the runtime call to allocate the arguments object.
5121 __ bind(&runtime);
5122 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005123}
5124
5125
5126void CallFunctionStub::Generate(MacroAssembler* masm) {
5127 Label slow;
5128 // Get the function to call from the stack.
5129 // function, receiver [, arguments]
5130 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
5131
5132 // Check that the function is really a JavaScript function.
5133 // r1: pushed function (to be verified)
5134 __ tst(r1, Operand(kSmiTagMask));
5135 __ b(eq, &slow);
5136 // Get the map of the function object.
5137 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
5138 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
5139 __ cmp(r2, Operand(JS_FUNCTION_TYPE));
5140 __ b(ne, &slow);
5141
5142 // Fast-case: Invoke the function now.
5143 // r1: pushed function
5144 ParameterCount actual(argc_);
5145 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
5146
5147 // Slow-case: Non-function called.
5148 __ bind(&slow);
5149 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005150 __ mov(r2, Operand(0));
5151 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
5152 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
5153 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005154}
5155
5156
5157#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005158
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00005159} } // namespace v8::internal