blob: 12d828d7ee21dd50ca057262675681a2865c73f1 [file] [log] [blame]
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "bootstrapper.h"
31#include "codegen-inl.h"
32#include "debug.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000033#include "parser.h"
34#include "register-allocator-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035#include "runtime.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000036#include "scopes.h"
37
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038
kasperl@chromium.org71affb52009-05-26 05:44:31 +000039namespace v8 {
40namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041
ager@chromium.org65dad4b2009-04-23 08:48:43 +000042#define __ ACCESS_MASM(masm_)
43
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000044static void EmitIdenticalObjectComparison(MacroAssembler* masm,
45 Label* slow,
46 Condition cc);
47static void EmitSmiNonsmiComparison(MacroAssembler* masm,
48 Label* rhs_not_nan,
49 Label* slow,
50 bool strict);
51static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc);
52static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000053static void MultiplyByKnownInt(MacroAssembler* masm,
54 Register source,
55 Register destination,
56 int known_int);
57static bool IsEasyToMultiplyBy(int x);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000058
59
60
ager@chromium.orge2902be2009-06-08 12:21:35 +000061// -------------------------------------------------------------------------
62// Platform-specific DeferredCode functions.
63
64void DeferredCode::SaveRegisters() {
65 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) {
66 int action = registers_[i];
67 if (action == kPush) {
68 __ push(RegisterAllocator::ToRegister(i));
69 } else if (action != kIgnore && (action & kSyncedFlag) == 0) {
70 __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
71 }
72 }
73}
74
75
76void DeferredCode::RestoreRegisters() {
77 // Restore registers in reverse order due to the stack.
78 for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) {
79 int action = registers_[i];
80 if (action == kPush) {
81 __ pop(RegisterAllocator::ToRegister(i));
82 } else if (action != kIgnore) {
83 action &= ~kSyncedFlag;
84 __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
85 }
86 }
87}
88
ager@chromium.org3bf7b912008-11-17 09:09:45 +000089
90// -------------------------------------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000091// CodeGenState implementation.
92
ager@chromium.org7c537e22008-10-16 08:43:32 +000093CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000094 : owner_(owner),
ager@chromium.org7c537e22008-10-16 08:43:32 +000095 typeof_state_(NOT_INSIDE_TYPEOF),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000096 true_target_(NULL),
97 false_target_(NULL),
98 previous_(NULL) {
99 owner_->set_state(this);
100}
101
102
ager@chromium.org7c537e22008-10-16 08:43:32 +0000103CodeGenState::CodeGenState(CodeGenerator* owner,
104 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000105 JumpTarget* true_target,
106 JumpTarget* false_target)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000107 : owner_(owner),
ager@chromium.org7c537e22008-10-16 08:43:32 +0000108 typeof_state_(typeof_state),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000109 true_target_(true_target),
110 false_target_(false_target),
111 previous_(owner->state()) {
112 owner_->set_state(this);
113}
114
115
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000116CodeGenState::~CodeGenState() {
117 ASSERT(owner_->state() == this);
118 owner_->set_state(previous_);
119}
120
121
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000122// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +0000123// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124
ager@chromium.org7c537e22008-10-16 08:43:32 +0000125CodeGenerator::CodeGenerator(int buffer_size, Handle<Script> script,
126 bool is_eval)
127 : is_eval_(is_eval),
128 script_(script),
129 deferred_(8),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000130 masm_(new MacroAssembler(NULL, buffer_size)),
131 scope_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000132 frame_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000133 allocator_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134 cc_reg_(al),
135 state_(NULL),
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000136 function_return_is_shadowed_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137}
138
139
140// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000141// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000143// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144// cp: callee's context
145
ager@chromium.org7c537e22008-10-16 08:43:32 +0000146void CodeGenerator::GenCode(FunctionLiteral* fun) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000147 ZoneList<Statement*>* body = fun->body();
148
149 // Initialize state.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000150 ASSERT(scope_ == NULL);
151 scope_ = fun->scope();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000152 ASSERT(allocator_ == NULL);
153 RegisterAllocator register_allocator(this);
154 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000155 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000156 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000157 cc_reg_ = al;
158 {
159 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000160
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000161 // Entry:
162 // Stack: receiver, arguments
163 // lr: return address
164 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000165 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000166 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000168 allocator_->Initialize();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000169 frame_->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000170 // tos: code slot
171#ifdef DEBUG
172 if (strlen(FLAG_stop_at) > 0 &&
173 fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000174 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000175 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000176 }
177#endif
178
179 // Allocate space for locals and initialize them.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000180 frame_->AllocateStackSlots();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000181 // Initialize the function return target after the locals are set
182 // up, because it needs the expected frame height from the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000183 function_return_.set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000184 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000186 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000187 if (scope_->num_heap_slots() > 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000188 // Allocate local context.
189 // Get outer context and create a new context based on it.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000190 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000191 frame_->EmitPush(r0);
192 frame_->CallRuntime(Runtime::kNewContext, 1); // r0 holds the result
kasper.lund7276f142008-07-30 08:49:36 +0000193
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000194#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000195 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000196 __ cmp(r0, Operand(cp));
197 verified_true.Branch(eq);
198 __ stop("NewContext: r0 is expected to be the same as cp");
199 verified_true.Bind();
200#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000202 __ str(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203 }
204
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000205 // TODO(1241774): Improve this code:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000206 // 1) only needed if we have a context
207 // 2) no need to recompute context ptr every single time
208 // 3) don't copy parameter operand code from SlotOperand!
209 {
210 Comment cmnt2(masm_, "[ copy context parameters into .context");
211
212 // Note that iteration order is relevant here! If we have the same
213 // parameter twice (e.g., function (x, y, x)), and that parameter
214 // needs to be copied into the context, it must be the last argument
215 // passed to the parameter that needs to be copied. This is a rare
216 // case so we don't check for it, instead we rely on the copying
217 // order: such a parameter is copied repeatedly into the same
218 // context location and thus the last value is what is seen inside
219 // the function.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000220 for (int i = 0; i < scope_->num_parameters(); i++) {
221 Variable* par = scope_->parameter(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222 Slot* slot = par->slot();
223 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000224 ASSERT(!scope_->is_global_scope()); // no parameters in global scope
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000225 __ ldr(r1, frame_->ParameterAt(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000226 // Loads r2 with context; used below in RecordWrite.
227 __ str(r1, SlotOperand(slot, r2));
228 // Load the offset into r3.
229 int slot_offset =
230 FixedArray::kHeaderSize + slot->index() * kPointerSize;
231 __ mov(r3, Operand(slot_offset));
232 __ RecordWrite(r2, r3, r1);
233 }
234 }
235 }
236
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000237 // Store the arguments object. This must happen after context
238 // initialization because the arguments object may be stored in the
239 // context.
240 if (scope_->arguments() != NULL) {
241 ASSERT(scope_->arguments_shadow() != NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000242 Comment cmnt(masm_, "[ allocate arguments object");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000243 { Reference shadow_ref(this, scope_->arguments_shadow());
244 { Reference arguments_ref(this, scope_->arguments());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000245 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000246 __ ldr(r2, frame_->Function());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000247 // The receiver is below the arguments, the return address,
248 // and the frame pointer on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000249 const int kReceiverDisplacement = 2 + scope_->num_parameters();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000250 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000251 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000252 frame_->Adjust(3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000253 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000254 frame_->CallStub(&stub, 3);
255 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000256 arguments_ref.SetValue(NOT_CONST_INIT);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000257 }
ager@chromium.org7c537e22008-10-16 08:43:32 +0000258 shadow_ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000260 frame_->Drop(); // Value is no longer needed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261 }
262
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000263 // Generate code to 'execute' declarations and initialize functions
264 // (source elements). In case of an illegal redeclaration we need to
265 // handle that instead of processing the declarations.
266 if (scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000267 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000268 scope_->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269 } else {
270 Comment cmnt(masm_, "[ declarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000271 ProcessDeclarations(scope_->declarations());
272 // Bail out if a stack-overflow exception occurred when processing
273 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000274 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275 }
276
mads.s.ager31e71382008-08-13 09:32:07 +0000277 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000278 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000279 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000280 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000281 CheckStack();
282
283 // Compile the body of the function in a vanilla state. Don't
284 // bother compiling all the code if the scope has an illegal
285 // redeclaration.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000286 if (!scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287 Comment cmnt(masm_, "[ function body");
288#ifdef DEBUG
289 bool is_builtin = Bootstrapper::IsActive();
290 bool should_trace =
291 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000292 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000293 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000294 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000295 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000297 VisitStatementsAndSpill(body);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299 }
300
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000301 // Generate the return sequence if necessary.
302 if (frame_ != NULL || function_return_.is_linked()) {
303 // exit
304 // r0: result
305 // sp: stack pointer
306 // fp: frame pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000307 // cp: callee's context
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000308 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +0000309
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000310 function_return_.Bind();
311 if (FLAG_trace) {
312 // Push the return value on the stack as the parameter.
313 // Runtime::TraceExit returns the parameter as it is.
314 frame_->EmitPush(r0);
315 frame_->CallRuntime(Runtime::kTraceExit, 1);
316 }
317
318 // Tear down the frame which will restore the caller's frame pointer and
319 // the link register.
320 frame_->Exit();
321
322 __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize));
ager@chromium.org9085a012009-05-11 19:22:57 +0000323 __ Jump(lr);
mads.s.ager31e71382008-08-13 09:32:07 +0000324 }
325
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327 ASSERT(!has_cc());
328 ASSERT(state_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000329 ASSERT(!function_return_is_shadowed_);
330 function_return_.Unuse();
331 DeleteFrame();
332
333 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000334 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000335 ProcessDeferred();
336 }
337
338 allocator_ = NULL;
339 scope_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000340}
341
342
ager@chromium.org7c537e22008-10-16 08:43:32 +0000343MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
344 // Currently, this assertion will fail if we try to assign to
345 // a constant variable that is constant because it is read-only
346 // (such as the variable referring to a named function expression).
347 // We need to implement assignments to read-only variables.
348 // Ideally, we should do this during AST generation (by converting
349 // such assignments into expression statements); however, in general
350 // we may not be able to make the decision until past AST generation,
351 // that is when the entire program is known.
352 ASSERT(slot != NULL);
353 int index = slot->index();
354 switch (slot->type()) {
355 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000356 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000357
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000358 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000359 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000360
361 case Slot::CONTEXT: {
362 // Follow the context chain if necessary.
363 ASSERT(!tmp.is(cp)); // do not overwrite context register
364 Register context = cp;
365 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000366 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000367 // Load the closure.
368 // (All contexts, even 'with' contexts, have a closure,
369 // and it is the same for all contexts inside a function.
370 // There is no need to go to the function context first.)
371 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
372 // Load the function context (which is the incoming, outer context).
373 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
374 context = tmp;
375 }
376 // We may have a 'with' context now. Get the function context.
377 // (In fact this mov may never be the needed, since the scope analysis
378 // may not permit a direct context access in this case and thus we are
379 // always at a function context. However it is safe to dereference be-
380 // cause the function context of a function context is itself. Before
381 // deleting this mov we should try to create a counter-example first,
382 // though...)
383 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
384 return ContextOperand(tmp, index);
385 }
386
387 default:
388 UNREACHABLE();
389 return MemOperand(r0, 0);
390 }
391}
392
393
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000394MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
395 Slot* slot,
396 Register tmp,
397 Register tmp2,
398 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000399 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000400 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000401
ager@chromium.org381abbb2009-02-25 13:23:22 +0000402 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
403 if (s->num_heap_slots() > 0) {
404 if (s->calls_eval()) {
405 // Check that extension is NULL.
406 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
407 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000408 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000409 }
410 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
411 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
412 context = tmp;
413 }
414 }
415 // Check that last extension is NULL.
416 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
417 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000418 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000419 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000420 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000421}
422
423
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000424// Loads a value on TOS. If it is a boolean value, the result may have been
425// (partially) translated into branches, or it may have set the condition
426// code register. If force_cc is set, the value is forced to set the
427// condition code register and no value is pushed. If the condition code
428// register was set, has_cc() is true and cc_reg_ contains the condition to
429// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000430void CodeGenerator::LoadCondition(Expression* x,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000431 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000432 JumpTarget* true_target,
433 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000434 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000435 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000436 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000437
ager@chromium.org7c537e22008-10-16 08:43:32 +0000438 { CodeGenState new_state(this, typeof_state, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000439 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000440
441 // If we hit a stack overflow, we may not have actually visited
442 // the expression. In that case, we ensure that we have a
443 // valid-looking frame state because we will continue to generate
444 // code as we unwind the C++ stack.
445 //
446 // It's possible to have both a stack overflow and a valid frame
447 // state (eg, a subexpression overflowed, visiting it returned
448 // with a dummied frame state, and visiting this expression
449 // returned with a normal-looking state).
450 if (HasStackOverflow() &&
451 has_valid_frame() &&
452 !has_cc() &&
453 frame_->height() == original_height) {
454 true_target->Jump();
455 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000456 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000457 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000458 // Convert the TOS value to a boolean in the condition code register.
459 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000460 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000461 ASSERT(!force_cc || !has_valid_frame() || has_cc());
462 ASSERT(!has_valid_frame() ||
463 (has_cc() && frame_->height() == original_height) ||
464 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000465}
466
467
ager@chromium.org7c537e22008-10-16 08:43:32 +0000468void CodeGenerator::Load(Expression* x, TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000469#ifdef DEBUG
470 int original_height = frame_->height();
471#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000472 JumpTarget true_target;
473 JumpTarget false_target;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000474 LoadCondition(x, typeof_state, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000475
476 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000477 // Convert cc_reg_ into a boolean value.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000478 JumpTarget loaded;
479 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000480 materialize_true.Branch(cc_reg_);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000481 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000482 frame_->EmitPush(r0);
483 loaded.Jump();
484 materialize_true.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000485 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000486 frame_->EmitPush(r0);
487 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000488 cc_reg_ = al;
489 }
490
491 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000492 // We have at least one condition value that has been "translated"
493 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000494 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000495 if (frame_ != NULL) {
496 loaded.Jump(); // Don't lose the current TOS.
497 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000498 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000499 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000500 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000501 true_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000502 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000503 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000504 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000505 // If both "true" and "false" need to be loaded jump across the code for
506 // "false".
507 if (both) {
508 loaded.Jump();
509 }
510 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000511 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000512 false_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000513 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000514 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000516 // A value is loaded on all paths reaching this point.
517 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000519 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000521 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000522}
523
524
ager@chromium.org7c537e22008-10-16 08:43:32 +0000525void CodeGenerator::LoadGlobal() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000526 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000527 __ ldr(r0, GlobalObject());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000528 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000529}
530
531
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000532void CodeGenerator::LoadGlobalReceiver(Register scratch) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000533 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000534 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
535 __ ldr(scratch,
536 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000537 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000538}
539
540
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000541// TODO(1241834): Get rid of this function in favor of just using Load, now
ager@chromium.org7c537e22008-10-16 08:43:32 +0000542// that we have the INSIDE_TYPEOF typeof state. => Need to handle global
543// variables w/o reference errors elsewhere.
544void CodeGenerator::LoadTypeofExpression(Expression* x) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000545 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000546 Variable* variable = x->AsVariableProxy()->AsVariable();
547 if (variable != NULL && !variable->is_this() && variable->is_global()) {
548 // NOTE: This is somewhat nasty. We force the compiler to load
549 // the variable as if through '<global>.<variable>' to make sure we
550 // do not get reference errors.
551 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
552 Literal key(variable->name());
553 // TODO(1241834): Fetch the position from the variable instead of using
554 // no position.
ager@chromium.org236ad962008-09-25 09:45:57 +0000555 Property property(&global, &key, RelocInfo::kNoPosition);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000556 LoadAndSpill(&property);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000557 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000558 LoadAndSpill(x, INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559 }
560}
561
562
ager@chromium.org7c537e22008-10-16 08:43:32 +0000563Reference::Reference(CodeGenerator* cgen, Expression* expression)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
565 cgen->LoadReference(this);
566}
567
568
569Reference::~Reference() {
570 cgen_->UnloadReference(this);
571}
572
573
ager@chromium.org7c537e22008-10-16 08:43:32 +0000574void CodeGenerator::LoadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000575 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000576 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577 Expression* e = ref->expression();
578 Property* property = e->AsProperty();
579 Variable* var = e->AsVariableProxy()->AsVariable();
580
581 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000582 // The expression is either a property or a variable proxy that rewrites
583 // to a property.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000584 LoadAndSpill(property->obj());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000585 // We use a named reference if the key is a literal symbol, unless it is
586 // a string that can be legally parsed as an integer. This is because
587 // otherwise we will not get into the slow case code that handles [] on
588 // String objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000589 Literal* literal = property->key()->AsLiteral();
590 uint32_t dummy;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000591 if (literal != NULL &&
592 literal->handle()->IsSymbol() &&
593 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594 ref->set_type(Reference::NAMED);
595 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000596 LoadAndSpill(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000597 ref->set_type(Reference::KEYED);
598 }
599 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000600 // The expression is a variable proxy that does not rewrite to a
601 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000602 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000603 LoadGlobal();
604 ref->set_type(Reference::NAMED);
605 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000606 ASSERT(var->slot() != NULL);
607 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608 }
609 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000610 // Anything else is a runtime error.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000611 LoadAndSpill(e);
612 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000613 }
614}
615
616
ager@chromium.org7c537e22008-10-16 08:43:32 +0000617void CodeGenerator::UnloadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000618 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000619 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000620 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000621 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000622 if (size > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000623 frame_->EmitPop(r0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000624 frame_->Drop(size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000625 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000626 }
627}
628
629
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
631// register to a boolean in the condition code register. The code
632// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000633void CodeGenerator::ToBoolean(JumpTarget* true_target,
634 JumpTarget* false_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000635 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000636 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000638 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000639
640 // Fast case checks
641
mads.s.ager31e71382008-08-13 09:32:07 +0000642 // Check if the value is 'false'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000643 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
644 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000645 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000646
mads.s.ager31e71382008-08-13 09:32:07 +0000647 // Check if the value is 'true'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000648 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
649 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000650 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000651
mads.s.ager31e71382008-08-13 09:32:07 +0000652 // Check if the value is 'undefined'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000653 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
654 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000655 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656
mads.s.ager31e71382008-08-13 09:32:07 +0000657 // Check if the value is a smi.
658 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000659 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000660 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000661 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662
663 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000664 frame_->EmitPush(r0);
665 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000666 // Convert the result (r0) to a condition code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000667 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
668 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000669
670 cc_reg_ = ne;
671}
672
673
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000674void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000675 OverwriteMode overwrite_mode,
676 int constant_rhs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000677 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000678 // sp[0] : y
679 // sp[1] : x
680 // result : r0
681
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000682 // Stub is entered with a call: 'return address' is in lr.
683 switch (op) {
684 case Token::ADD: // fall through.
685 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000686 case Token::MUL:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000687 case Token::DIV:
688 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000689 case Token::BIT_OR:
690 case Token::BIT_AND:
691 case Token::BIT_XOR:
692 case Token::SHL:
693 case Token::SHR:
694 case Token::SAR: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000695 frame_->EmitPop(r0); // r0 : y
696 frame_->EmitPop(r1); // r1 : x
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000697 GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000698 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000699 break;
700 }
701
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702 case Token::COMMA:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000703 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000704 // simply discard left value
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000705 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706 break;
707
708 default:
709 // Other cases should have been handled before this point.
710 UNREACHABLE();
711 break;
712 }
713}
714
715
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000716class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000717 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000718 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000719 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000720 bool reversed,
721 OverwriteMode overwrite_mode)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000722 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000723 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000724 reversed_(reversed),
725 overwrite_mode_(overwrite_mode) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000726 set_comment("[ DeferredInlinedSmiOperation");
727 }
728
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000729 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000730
731 private:
732 Token::Value op_;
733 int value_;
734 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000735 OverwriteMode overwrite_mode_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000736};
737
738
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000739void DeferredInlineSmiOperation::Generate() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000740 switch (op_) {
741 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000742 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000743 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000744 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
745 __ mov(r1, Operand(Smi::FromInt(value_)));
746 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000747 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
748 __ mov(r0, Operand(Smi::FromInt(value_)));
749 }
750 break;
751 }
752
753 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000754 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000755 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000756 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
757 __ mov(r1, Operand(Smi::FromInt(value_)));
758 } else {
759 __ add(r1, r0, Operand(Smi::FromInt(value_)));
760 __ mov(r0, Operand(Smi::FromInt(value_)));
761 }
762 break;
763 }
764
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000765 // For these operations there is no optimistic operation that needs to be
766 // reverted.
767 case Token::MUL:
768 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000769 case Token::BIT_OR:
770 case Token::BIT_XOR:
771 case Token::BIT_AND: {
772 if (reversed_) {
773 __ mov(r1, Operand(Smi::FromInt(value_)));
774 } else {
775 __ mov(r1, Operand(r0));
776 __ mov(r0, Operand(Smi::FromInt(value_)));
777 }
778 break;
779 }
780
781 case Token::SHL:
782 case Token::SHR:
783 case Token::SAR: {
784 if (!reversed_) {
785 __ mov(r1, Operand(r0));
786 __ mov(r0, Operand(Smi::FromInt(value_)));
787 } else {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000788 UNREACHABLE(); // Should have been handled in SmiOperation.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000789 }
790 break;
791 }
792
793 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +0000794 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000795 UNREACHABLE();
796 break;
797 }
798
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000799 GenericBinaryOpStub stub(op_, overwrite_mode_, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000800 __ CallStub(&stub);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000801}
802
803
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000804static bool PopCountLessThanEqual2(unsigned int x) {
805 x &= x - 1;
806 return (x & (x - 1)) == 0;
807}
808
809
810// Returns the index of the lowest bit set.
811static int BitPosition(unsigned x) {
812 int bit_posn = 0;
813 while ((x & 0xf) == 0) {
814 bit_posn += 4;
815 x >>= 4;
816 }
817 while ((x & 1) == 0) {
818 bit_posn++;
819 x >>= 1;
820 }
821 return bit_posn;
822}
823
824
ager@chromium.org7c537e22008-10-16 08:43:32 +0000825void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000826 Handle<Object> value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000827 bool reversed,
828 OverwriteMode mode) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000829 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000830 // NOTE: This is an attempt to inline (a bit) more of the code for
831 // some possible smi operations (like + and -) when (at least) one
832 // of the operands is a literal smi. With this optimization, the
833 // performance of the system is increased by ~15%, and the generated
834 // code size is increased by ~1% (measured on a combination of
835 // different benchmarks).
836
mads.s.ager31e71382008-08-13 09:32:07 +0000837 // sp[0] : operand
838
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000839 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000840
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000841 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000842 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000843
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000844 bool something_to_inline = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000845 switch (op) {
846 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000847 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000848 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000849
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000850 __ add(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000851 deferred->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000852 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000853 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000854 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000855 break;
856 }
857
858 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000859 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000860 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000861
ager@chromium.orge2902be2009-06-08 12:21:35 +0000862 if (reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000863 __ rsb(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000864 } else {
865 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000866 }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000867 deferred->Branch(vs);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000868 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000869 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000870 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000871 break;
872 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000874
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000875 case Token::BIT_OR:
876 case Token::BIT_XOR:
877 case Token::BIT_AND: {
878 DeferredCode* deferred =
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000879 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000880 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000881 deferred->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000882 switch (op) {
883 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
884 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
885 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
886 default: UNREACHABLE();
887 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000888 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000889 break;
890 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000891
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000892 case Token::SHL:
893 case Token::SHR:
894 case Token::SAR: {
895 if (reversed) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000896 something_to_inline = false;
897 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000898 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000899 int shift_value = int_value & 0x1f; // least significant 5 bits
900 DeferredCode* deferred =
901 new DeferredInlineSmiOperation(op, shift_value, false, mode);
902 __ tst(r0, Operand(kSmiTagMask));
903 deferred->Branch(ne);
904 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
905 switch (op) {
906 case Token::SHL: {
907 if (shift_value != 0) {
908 __ mov(r2, Operand(r2, LSL, shift_value));
909 }
910 // check that the *unsigned* result fits in a smi
911 __ add(r3, r2, Operand(0x40000000), SetCC);
912 deferred->Branch(mi);
913 break;
914 }
915 case Token::SHR: {
916 // LSR by immediate 0 means shifting 32 bits.
917 if (shift_value != 0) {
918 __ mov(r2, Operand(r2, LSR, shift_value));
919 }
920 // check that the *unsigned* result fits in a smi
921 // neither of the two high-order bits can be set:
922 // - 0x80000000: high bit would be lost when smi tagging
923 // - 0x40000000: this number would convert to negative when
924 // smi tagging these two cases can only happen with shifts
925 // by 0 or 1 when handed a valid smi
926 __ and_(r3, r2, Operand(0xc0000000), SetCC);
927 deferred->Branch(ne);
928 break;
929 }
930 case Token::SAR: {
931 if (shift_value != 0) {
932 // ASR by immediate 0 means shifting 32 bits.
933 __ mov(r2, Operand(r2, ASR, shift_value));
934 }
935 break;
936 }
937 default: UNREACHABLE();
938 }
939 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
940 deferred->BindExit();
941 break;
942 }
943
944 case Token::MOD: {
945 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
946 something_to_inline = false;
947 break;
948 }
949 DeferredCode* deferred =
950 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
951 unsigned mask = (0x80000000u | kSmiTagMask);
952 __ tst(r0, Operand(mask));
953 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
954 mask = (int_value << kSmiTagSize) - 1;
955 __ and_(r0, r0, Operand(mask));
956 deferred->BindExit();
957 break;
958 }
959
960 case Token::MUL: {
961 if (!IsEasyToMultiplyBy(int_value)) {
962 something_to_inline = false;
963 break;
964 }
965 DeferredCode* deferred =
966 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
967 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
968 max_smi_that_wont_overflow <<= kSmiTagSize;
969 unsigned mask = 0x80000000u;
970 while ((mask & max_smi_that_wont_overflow) == 0) {
971 mask |= mask >> 1;
972 }
973 mask |= kSmiTagMask;
974 // This does a single mask that checks for a too high value in a
975 // conservative way and for a non-Smi. It also filters out negative
976 // numbers, unfortunately, but since this code is inline we prefer
977 // brevity to comprehensiveness.
978 __ tst(r0, Operand(mask));
979 deferred->Branch(ne);
980 MultiplyByKnownInt(masm_, r0, r0, int_value);
981 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982 break;
983 }
984
985 default:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000986 something_to_inline = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000987 break;
988 }
989
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000990 if (!something_to_inline) {
991 if (!reversed) {
992 frame_->EmitPush(r0);
993 __ mov(r0, Operand(value));
994 frame_->EmitPush(r0);
995 GenericBinaryOperation(op, mode, int_value);
996 } else {
997 __ mov(ip, Operand(value));
998 frame_->EmitPush(ip);
999 frame_->EmitPush(r0);
1000 GenericBinaryOperation(op, mode, kUnknownIntValue);
1001 }
1002 }
1003
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001004 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001005}
1006
1007
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001008void CodeGenerator::Comparison(Condition cc,
1009 Expression* left,
1010 Expression* right,
1011 bool strict) {
1012 if (left != NULL) LoadAndSpill(left);
1013 if (right != NULL) LoadAndSpill(right);
1014
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001015 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001016 // sp[0] : y
1017 // sp[1] : x
1018 // result : cc register
1019
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001020 // Strict only makes sense for equality comparisons.
1021 ASSERT(!strict || cc == eq);
1022
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001023 JumpTarget exit;
1024 JumpTarget smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001025 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1026 if (cc == gt || cc == le) {
1027 cc = ReverseCondition(cc);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001028 frame_->EmitPop(r1);
1029 frame_->EmitPop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001030 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001031 frame_->EmitPop(r0);
1032 frame_->EmitPop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001033 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001034 __ orr(r2, r0, Operand(r1));
1035 __ tst(r2, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001036 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001037
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001038 // Perform non-smi comparison by stub.
1039 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1040 // We call with 0 args because there are 0 on the stack.
1041 CompareStub stub(cc, strict);
1042 frame_->CallStub(&stub, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001043 __ cmp(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001044 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001045
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001046 // Do smi comparisons by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001047 smi.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001048 __ cmp(r1, Operand(r0));
1049
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001050 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051 cc_reg_ = cc;
1052}
1053
1054
kasper.lund7276f142008-07-30 08:49:36 +00001055class CallFunctionStub: public CodeStub {
1056 public:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001057 CallFunctionStub(int argc, InLoopFlag in_loop)
1058 : argc_(argc), in_loop_(in_loop) {}
kasper.lund7276f142008-07-30 08:49:36 +00001059
1060 void Generate(MacroAssembler* masm);
1061
1062 private:
1063 int argc_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001064 InLoopFlag in_loop_;
kasper.lund7276f142008-07-30 08:49:36 +00001065
kasper.lund7276f142008-07-30 08:49:36 +00001066#if defined(DEBUG)
1067 void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); }
1068#endif // defined(DEBUG)
1069
1070 Major MajorKey() { return CallFunction; }
1071 int MinorKey() { return argc_; }
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001072 InLoopFlag InLoop() { return in_loop_; }
kasper.lund7276f142008-07-30 08:49:36 +00001073};
1074
1075
mads.s.ager31e71382008-08-13 09:32:07 +00001076// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001077void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001078 int position) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001079 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001080 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001081 int arg_count = args->length();
1082 for (int i = 0; i < arg_count; i++) {
1083 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001084 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085
kasper.lund7276f142008-07-30 08:49:36 +00001086 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001087 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001088
kasper.lund7276f142008-07-30 08:49:36 +00001089 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001090 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
1091 CallFunctionStub call_function(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001092 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093
1094 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001095 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001096 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001097}
1098
1099
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001100void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001101 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001102 ASSERT(has_cc());
1103 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001104 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001105 cc_reg_ = al;
1106}
1107
1108
ager@chromium.org7c537e22008-10-16 08:43:32 +00001109void CodeGenerator::CheckStack() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001110 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001111 if (FLAG_check_stack) {
1112 Comment cmnt(masm_, "[ check stack");
1113 StackCheckStub stub;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001114 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001115 }
1116}
1117
1118
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001119void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1120#ifdef DEBUG
1121 int original_height = frame_->height();
1122#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001123 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001124 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1125 VisitAndSpill(statements->at(i));
1126 }
1127 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1128}
1129
1130
ager@chromium.org7c537e22008-10-16 08:43:32 +00001131void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001132#ifdef DEBUG
1133 int original_height = frame_->height();
1134#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001135 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001136 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001137 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001138 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001139 VisitStatementsAndSpill(node->statements());
1140 if (node->break_target()->is_linked()) {
1141 node->break_target()->Bind();
1142 }
1143 node->break_target()->Unuse();
1144 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001145}
1146
1147
ager@chromium.org7c537e22008-10-16 08:43:32 +00001148void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001149 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001150 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001151 frame_->EmitPush(r0);
1152 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001153 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001154 frame_->EmitPush(r0);
1155 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001156 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001157}
1158
1159
ager@chromium.org7c537e22008-10-16 08:43:32 +00001160void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001161#ifdef DEBUG
1162 int original_height = frame_->height();
1163#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001164 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001165 Comment cmnt(masm_, "[ Declaration");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001166 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167 Variable* var = node->proxy()->var();
1168 ASSERT(var != NULL); // must have been resolved
1169 Slot* slot = var->slot();
1170
1171 // If it was not possible to allocate the variable at compile time,
1172 // we need to "declare" it at runtime to make sure it actually
1173 // exists in the local context.
1174 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1175 // Variables with a "LOOKUP" slot were introduced as non-locals
1176 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001177 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001178 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001179 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001180 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001181 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001182 // Declaration nodes are always declared in only two modes.
1183 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1184 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001185 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001186 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001187 // Push initial value, if any.
1188 // Note: For variables we must not push an initial value (such as
1189 // 'undefined') because we may have a (legal) redeclaration and we
1190 // must not destroy the current value.
1191 if (node->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001192 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001193 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001194 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001195 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001196 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001197 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001198 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001200 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001201 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001202 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001203 return;
1204 }
1205
1206 ASSERT(!var->is_global());
1207
1208 // If we have a function or a constant, we need to initialize the variable.
1209 Expression* val = NULL;
1210 if (node->mode() == Variable::CONST) {
1211 val = new Literal(Factory::the_hole_value());
1212 } else {
1213 val = node->fun(); // NULL if we don't have a function
1214 }
1215
1216 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001217 {
1218 // Set initial value.
1219 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001220 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001221 target.SetValue(NOT_CONST_INIT);
1222 // The reference is removed from the stack (preserving TOS) when
1223 // it goes out of scope.
1224 }
1225 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001226 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001227 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001228 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001229}
1230
1231
ager@chromium.org7c537e22008-10-16 08:43:32 +00001232void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001233#ifdef DEBUG
1234 int original_height = frame_->height();
1235#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001236 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001237 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001238 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001239 Expression* expression = node->expression();
1240 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001241 LoadAndSpill(expression);
1242 frame_->Drop();
1243 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001244}
1245
1246
ager@chromium.org7c537e22008-10-16 08:43:32 +00001247void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001248#ifdef DEBUG
1249 int original_height = frame_->height();
1250#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001251 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001252 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001253 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001254 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001255 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001256}
1257
1258
ager@chromium.org7c537e22008-10-16 08:43:32 +00001259void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001260#ifdef DEBUG
1261 int original_height = frame_->height();
1262#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001263 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001265 // Generate different code depending on which parts of the if statement
1266 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001267 bool has_then_stm = node->HasThenStatement();
1268 bool has_else_stm = node->HasElseStatement();
1269
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001270 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001271
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001272 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001274 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001275 JumpTarget then;
1276 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001277 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001278 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1279 &then, &else_, true);
1280 if (frame_ != NULL) {
1281 Branch(false, &else_);
1282 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001283 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001284 if (frame_ != NULL || then.is_linked()) {
1285 then.Bind();
1286 VisitAndSpill(node->then_statement());
1287 }
1288 if (frame_ != NULL) {
1289 exit.Jump();
1290 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001292 if (else_.is_linked()) {
1293 else_.Bind();
1294 VisitAndSpill(node->else_statement());
1295 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296
1297 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001298 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001299 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001300 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001302 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1303 &then, &exit, true);
1304 if (frame_ != NULL) {
1305 Branch(false, &exit);
1306 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001308 if (frame_ != NULL || then.is_linked()) {
1309 then.Bind();
1310 VisitAndSpill(node->then_statement());
1311 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312
1313 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001314 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001316 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001317 // if (!cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001318 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1319 &exit, &else_, true);
1320 if (frame_ != NULL) {
1321 Branch(true, &exit);
1322 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001324 if (frame_ != NULL || else_.is_linked()) {
1325 else_.Bind();
1326 VisitAndSpill(node->else_statement());
1327 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328
1329 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001330 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001331 ASSERT(!has_then_stm && !has_else_stm);
1332 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001333 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1334 &exit, &exit, false);
1335 if (frame_ != NULL) {
1336 if (has_cc()) {
1337 cc_reg_ = al;
1338 } else {
1339 frame_->Drop();
1340 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341 }
1342 }
1343
1344 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001345 if (exit.is_linked()) {
1346 exit.Bind();
1347 }
1348 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349}
1350
1351
ager@chromium.org7c537e22008-10-16 08:43:32 +00001352void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001353 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001354 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001355 CodeForStatementPosition(node);
1356 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001357}
1358
1359
ager@chromium.org7c537e22008-10-16 08:43:32 +00001360void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001361 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001362 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001363 CodeForStatementPosition(node);
1364 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001365}
1366
1367
ager@chromium.org7c537e22008-10-16 08:43:32 +00001368void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001369 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001371
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001372 if (function_return_is_shadowed_) {
1373 CodeForStatementPosition(node);
1374 LoadAndSpill(node->expression());
1375 frame_->EmitPop(r0);
1376 function_return_.Jump();
1377 } else {
1378 // Load the returned value.
1379 CodeForStatementPosition(node);
1380 LoadAndSpill(node->expression());
1381
1382 // Pop the result from the frame and prepare the frame for
1383 // returning thus making it easier to merge.
1384 frame_->EmitPop(r0);
1385 frame_->PrepareForReturn();
1386
1387 function_return_.Jump();
1388 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001389}
1390
1391
ager@chromium.org7c537e22008-10-16 08:43:32 +00001392void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001393#ifdef DEBUG
1394 int original_height = frame_->height();
1395#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001396 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001397 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001398 CodeForStatementPosition(node);
1399 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001400 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001401 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001402 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001403 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001404 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001405#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001406 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001407 __ cmp(r0, Operand(cp));
1408 verified_true.Branch(eq);
1409 __ stop("PushContext: r0 is expected to be the same as cp");
1410 verified_true.Bind();
1411#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001412 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001413 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001414 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001415}
1416
1417
ager@chromium.org7c537e22008-10-16 08:43:32 +00001418void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001419#ifdef DEBUG
1420 int original_height = frame_->height();
1421#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001422 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001423 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001424 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001425 // Pop context.
1426 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1427 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001428 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001429 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001430}
1431
1432
ager@chromium.org7c537e22008-10-16 08:43:32 +00001433void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001434#ifdef DEBUG
1435 int original_height = frame_->height();
1436#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001437 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001439 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001440 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001442 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001443
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001444 JumpTarget next_test;
1445 JumpTarget fall_through;
1446 JumpTarget default_entry;
1447 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001448 ZoneList<CaseClause*>* cases = node->cases();
1449 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001450 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001451
1452 for (int i = 0; i < length; i++) {
1453 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001454 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001455 // Remember the default clause and compile it at the end.
1456 default_clause = clause;
1457 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001458 }
1459
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001460 Comment cmnt(masm_, "[ Case clause");
1461 // Compile the test.
1462 next_test.Bind();
1463 next_test.Unuse();
1464 // Duplicate TOS.
1465 __ ldr(r0, frame_->Top());
1466 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001467 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001468 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001469
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001470 // Before entering the body from the test, remove the switch value from
1471 // the stack.
1472 frame_->Drop();
1473
1474 // Label the body so that fall through is enabled.
1475 if (i > 0 && cases->at(i - 1)->is_default()) {
1476 default_exit.Bind();
1477 } else {
1478 fall_through.Bind();
1479 fall_through.Unuse();
1480 }
1481 VisitStatementsAndSpill(clause->statements());
1482
1483 // If control flow can fall through from the body, jump to the next body
1484 // or the end of the statement.
1485 if (frame_ != NULL) {
1486 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1487 default_entry.Jump();
1488 } else {
1489 fall_through.Jump();
1490 }
1491 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001492 }
1493
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001494 // The final "test" removes the switch value.
1495 next_test.Bind();
1496 frame_->Drop();
1497
1498 // If there is a default clause, compile it.
1499 if (default_clause != NULL) {
1500 Comment cmnt(masm_, "[ Default clause");
1501 default_entry.Bind();
1502 VisitStatementsAndSpill(default_clause->statements());
1503 // If control flow can fall out of the default and there is a case after
1504 // it, jup to that case's body.
1505 if (frame_ != NULL && default_exit.is_bound()) {
1506 default_exit.Jump();
1507 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001508 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001509
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001510 if (fall_through.is_linked()) {
1511 fall_through.Bind();
1512 }
1513
1514 if (node->break_target()->is_linked()) {
1515 node->break_target()->Bind();
1516 }
1517 node->break_target()->Unuse();
1518 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001519}
1520
1521
ager@chromium.org7c537e22008-10-16 08:43:32 +00001522void CodeGenerator::VisitLoopStatement(LoopStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001523#ifdef DEBUG
1524 int original_height = frame_->height();
1525#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001526 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001527 Comment cmnt(masm_, "[ LoopStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001528 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001529 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001530
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001531 // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a
1532 // known result for the test expression, with no side effects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001533 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW;
1534 if (node->cond() == NULL) {
1535 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1536 info = ALWAYS_TRUE;
1537 } else {
1538 Literal* lit = node->cond()->AsLiteral();
1539 if (lit != NULL) {
1540 if (lit->IsTrue()) {
1541 info = ALWAYS_TRUE;
1542 } else if (lit->IsFalse()) {
1543 info = ALWAYS_FALSE;
1544 }
1545 }
1546 }
1547
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001548 switch (node->type()) {
1549 case LoopStatement::DO_LOOP: {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001550 JumpTarget body(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001551
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001552 // Label the top of the loop for the backward CFG edge. If the test
1553 // is always true we can use the continue target, and if the test is
1554 // always false there is no need.
1555 if (info == ALWAYS_TRUE) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001556 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001557 node->continue_target()->Bind();
1558 } else if (info == ALWAYS_FALSE) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001559 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001560 } else {
1561 ASSERT(info == DONT_KNOW);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001562 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001563 body.Bind();
1564 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001565
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001566 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001567 VisitAndSpill(node->body());
1568
1569 // Compile the test.
1570 if (info == ALWAYS_TRUE) {
1571 if (has_valid_frame()) {
1572 // If control can fall off the end of the body, jump back to the
1573 // top.
1574 node->continue_target()->Jump();
1575 }
1576 } else if (info == ALWAYS_FALSE) {
1577 // If we have a continue in the body, we only have to bind its jump
1578 // target.
1579 if (node->continue_target()->is_linked()) {
1580 node->continue_target()->Bind();
1581 }
1582 } else {
1583 ASSERT(info == DONT_KNOW);
1584 // We have to compile the test expression if it can be reached by
1585 // control flow falling out of the body or via continue.
1586 if (node->continue_target()->is_linked()) {
1587 node->continue_target()->Bind();
1588 }
1589 if (has_valid_frame()) {
1590 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1591 &body, node->break_target(), true);
1592 if (has_valid_frame()) {
1593 // A invalid frame here indicates that control did not
1594 // fall out of the test expression.
1595 Branch(true, &body);
1596 }
1597 }
1598 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001599 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001600 }
1601
1602 case LoopStatement::WHILE_LOOP: {
1603 // If the test is never true and has no side effects there is no need
1604 // to compile the test or body.
1605 if (info == ALWAYS_FALSE) break;
1606
1607 // Label the top of the loop with the continue target for the backward
1608 // CFG edge.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001609 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001610 node->continue_target()->Bind();
1611
1612 if (info == DONT_KNOW) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001613 JumpTarget body;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001614 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1615 &body, node->break_target(), true);
1616 if (has_valid_frame()) {
1617 // A NULL frame indicates that control did not fall out of the
1618 // test expression.
1619 Branch(false, node->break_target());
1620 }
1621 if (has_valid_frame() || body.is_linked()) {
1622 body.Bind();
1623 }
1624 }
1625
1626 if (has_valid_frame()) {
1627 CheckStack(); // TODO(1222600): ignore if body contains calls.
1628 VisitAndSpill(node->body());
1629
1630 // If control flow can fall out of the body, jump back to the top.
1631 if (has_valid_frame()) {
1632 node->continue_target()->Jump();
1633 }
1634 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001635 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001636 }
1637
1638 case LoopStatement::FOR_LOOP: {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001639 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001640
1641 if (node->init() != NULL) {
1642 VisitAndSpill(node->init());
1643 }
1644
1645 // There is no need to compile the test or body.
1646 if (info == ALWAYS_FALSE) break;
1647
1648 // If there is no update statement, label the top of the loop with the
1649 // continue target, otherwise with the loop target.
1650 if (node->next() == NULL) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001651 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001652 node->continue_target()->Bind();
1653 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001654 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001655 loop.Bind();
1656 }
1657
1658 // If the test is always true, there is no need to compile it.
1659 if (info == DONT_KNOW) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001660 JumpTarget body;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001661 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1662 &body, node->break_target(), true);
1663 if (has_valid_frame()) {
1664 Branch(false, node->break_target());
1665 }
1666 if (has_valid_frame() || body.is_linked()) {
1667 body.Bind();
1668 }
1669 }
1670
1671 if (has_valid_frame()) {
1672 CheckStack(); // TODO(1222600): ignore if body contains calls.
1673 VisitAndSpill(node->body());
1674
1675 if (node->next() == NULL) {
1676 // If there is no update statement and control flow can fall out
1677 // of the loop, jump directly to the continue label.
1678 if (has_valid_frame()) {
1679 node->continue_target()->Jump();
1680 }
1681 } else {
1682 // If there is an update statement and control flow can reach it
1683 // via falling out of the body of the loop or continuing, we
1684 // compile the update statement.
1685 if (node->continue_target()->is_linked()) {
1686 node->continue_target()->Bind();
1687 }
1688 if (has_valid_frame()) {
1689 // Record source position of the statement as this code which is
1690 // after the code for the body actually belongs to the loop
1691 // statement and not the body.
1692 CodeForStatementPosition(node);
1693 VisitAndSpill(node->next());
1694 loop.Jump();
1695 }
1696 }
1697 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001699 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700 }
1701
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001702 if (node->break_target()->is_linked()) {
1703 node->break_target()->Bind();
1704 }
1705 node->continue_target()->Unuse();
1706 node->break_target()->Unuse();
1707 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001708}
1709
1710
ager@chromium.org7c537e22008-10-16 08:43:32 +00001711void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001712#ifdef DEBUG
1713 int original_height = frame_->height();
1714#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001715 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001716 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001717 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001718
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001719 JumpTarget primitive;
1720 JumpTarget jsobject;
1721 JumpTarget fixed_array;
1722 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
1723 JumpTarget end_del_check;
1724 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001725
1726 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001727 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001728
1729 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1730 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001731 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001732 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1733 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001734 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001735 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1736 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001737 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001738
1739 // Stack layout in body:
1740 // [iteration counter (Smi)]
1741 // [length of array]
1742 // [FixedArray]
1743 // [Map or 0]
1744 // [Object]
1745
1746 // Check if enumerable is already a JSObject
1747 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001748 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001749 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001750 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001751
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001752 primitive.Bind();
1753 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001754 Result arg_count(r0);
1755 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001756 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001757
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001758 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759 // Get the set of properties (as a FixedArray or Map).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001760 frame_->EmitPush(r0); // duplicate the object being enumerated
1761 frame_->EmitPush(r0);
1762 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001763
1764 // If we got a Map, we can do a fast modification check.
1765 // Otherwise, we got a FixedArray, and we have to do a slow check.
1766 __ mov(r2, Operand(r0));
1767 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001768 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
1769 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001770 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001771
1772 // Get enum cache
1773 __ mov(r1, Operand(r0));
1774 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1775 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1776 __ ldr(r2,
1777 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1778
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001779 frame_->EmitPush(r0); // map
1780 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001781 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001782 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001783 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001784 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001785 frame_->EmitPush(r0);
1786 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001787
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001788 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001790 frame_->EmitPush(r1); // insert 0 in place of Map
1791 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792
1793 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001794 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001795 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001796 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001797 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001798 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001799
1800 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001801 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00001802 // sp[0] : index
1803 // sp[1] : array/enum cache length
1804 // sp[2] : array or enum cache
1805 // sp[3] : 0 or map
1806 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001807 // Grab the current frame's height for the break and continue
1808 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001809 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1810 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001811
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001812 __ ldr(r0, frame_->ElementAt(0)); // load the current count
1813 __ ldr(r1, frame_->ElementAt(1)); // load the length
1814 __ cmp(r0, Operand(r1)); // compare to the array length
1815 node->break_target()->Branch(hs);
1816
1817 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001818
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001819 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001820 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001821 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1822 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1823
1824 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001825 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001826 // Check if this (still) matches the map of the enumerable.
1827 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001828 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1830 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001831 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001832
1833 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001834 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
1835 frame_->EmitPush(r0);
1836 frame_->EmitPush(r3); // push entry
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001837 Result arg_count_reg(r0);
1838 __ mov(r0, Operand(1));
1839 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, &arg_count_reg, 2);
1840 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001841
1842 // If the property has been removed while iterating, we just skip it.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001843 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1844 __ cmp(r3, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001845 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001846
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001847 end_del_check.Bind();
1848 // Store the entry in the 'each' expression and take another spin in the
1849 // loop. r3: i'th entry of the enum cache (or string there of)
1850 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851 { Reference each(this, node->each());
1852 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001853 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001854 __ ldr(r0, frame_->ElementAt(each.size()));
1855 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001856 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001857 // If the reference was to a slot we rely on the convenient property
1858 // that it doesn't matter whether a value (eg, r3 pushed above) is
1859 // right on top of or right underneath a zero-sized reference.
1860 each.SetValue(NOT_CONST_INIT);
mads.s.ager31e71382008-08-13 09:32:07 +00001861 if (each.size() > 0) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00001862 // It's safe to pop the value lying on top of the reference before
1863 // unloading the reference itself (which preserves the top of stack,
1864 // ie, now the topmost value of the non-zero sized reference), since
1865 // we will discard the top of stack after unloading the reference
1866 // anyway.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001867 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001868 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001869 }
1870 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00001871 // Discard the i'th entry pushed above or else the remainder of the
1872 // reference, whichever is currently on top of the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001873 frame_->Drop();
1874
1875 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001876 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001877 VisitAndSpill(node->body());
1878
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001879 // Next. Reestablish a spilled frame in case we are coming here via
1880 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001881 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001882 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001883 frame_->EmitPop(r0);
1884 __ add(r0, r0, Operand(Smi::FromInt(1)));
1885 frame_->EmitPush(r0);
1886 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001888 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
1889 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001890 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001891 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001892
1893 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001894 exit.Bind();
1895 node->continue_target()->Unuse();
1896 node->break_target()->Unuse();
1897 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001898}
1899
1900
ager@chromium.org7c537e22008-10-16 08:43:32 +00001901void CodeGenerator::VisitTryCatch(TryCatch* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001902#ifdef DEBUG
1903 int original_height = frame_->height();
1904#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001905 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001906 Comment cmnt(masm_, "[ TryCatch");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001907 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001908
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001909 JumpTarget try_block;
1910 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001911
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001912 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001914 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001915
1916 // Store the caught exception in the catch variable.
1917 { Reference ref(this, node->catch_var());
ager@chromium.org7c537e22008-10-16 08:43:32 +00001918 ASSERT(ref.is_slot());
1919 // Here we make use of the convenient property that it doesn't matter
1920 // whether a value is immediately on top of or underneath a zero-sized
1921 // reference.
1922 ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001923 }
1924
1925 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001926 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001928 VisitStatementsAndSpill(node->catch_block()->statements());
1929 if (frame_ != NULL) {
1930 exit.Jump();
1931 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001932
1933
1934 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001935 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001937 frame_->PushTryHandler(TRY_CATCH_HANDLER);
1938 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001939
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001940 // Shadow the labels for all escapes from the try block, including
1941 // returns. During shadowing, the original label is hidden as the
1942 // LabelShadow and operations on the original actually affect the
1943 // shadowing label.
1944 //
1945 // We should probably try to unify the escaping labels and the return
1946 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001947 int nof_escapes = node->escaping_targets()->length();
1948 List<ShadowTarget*> shadows(1 + nof_escapes);
1949
1950 // Add the shadow target for the function return.
1951 static const int kReturnShadowIndex = 0;
1952 shadows.Add(new ShadowTarget(&function_return_));
1953 bool function_return_was_shadowed = function_return_is_shadowed_;
1954 function_return_is_shadowed_ = true;
1955 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
1956
1957 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001958 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001959 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001960 }
1961
1962 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001963 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001964
1965 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001966 // After shadowing stops, the original labels are unshadowed and the
1967 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001968 bool has_unlinks = false;
1969 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001971 has_unlinks = has_unlinks || shadows[i]->is_linked();
1972 }
1973 function_return_is_shadowed_ = function_return_was_shadowed;
1974
1975 // Get an external reference to the handler address.
1976 ExternalReference handler_address(Top::k_handler_address);
1977
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001978 // If we can fall off the end of the try block, unlink from try chain.
1979 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001980 // The next handler address is on top of the frame. Unlink from
1981 // the handler list and drop the rest of this handler from the
1982 // frame.
1983 ASSERT(StackHandlerConstants::kNextOffset == 0);
1984 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001985 __ mov(r3, Operand(handler_address));
1986 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001987 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001988 if (has_unlinks) {
1989 exit.Jump();
1990 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001991 }
1992
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001993 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001994 // jumped to. Deallocate each shadow target.
1995 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001996 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001997 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001998 shadows[i]->Bind();
1999 // Because we can be jumping here (to spilled code) from unspilled
2000 // code, we need to reestablish a spilled frame at this block.
2001 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002002
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002003 // Reload sp from the top handler, because some statements that we
2004 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002005 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002006 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002007 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002008
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002009 ASSERT(StackHandlerConstants::kNextOffset == 0);
2010 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002012 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002013
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002014 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2015 frame_->PrepareForReturn();
2016 }
2017 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002018 }
2019 }
2020
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002021 exit.Bind();
2022 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002023}
2024
2025
ager@chromium.org7c537e22008-10-16 08:43:32 +00002026void CodeGenerator::VisitTryFinally(TryFinally* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002027#ifdef DEBUG
2028 int original_height = frame_->height();
2029#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002030 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 Comment cmnt(masm_, "[ TryFinally");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002032 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002033
2034 // State: Used to keep track of reason for entering the finally
2035 // block. Should probably be extended to hold information for
2036 // break/continue from within the try block.
2037 enum { FALLING, THROWING, JUMPING };
2038
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002039 JumpTarget try_block;
2040 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002042 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002044 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045 // In case of thrown exceptions, this is where we continue.
2046 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002047 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048
2049 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002050 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002052 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2053 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002055 // Shadow the labels for all escapes from the try block, including
2056 // returns. Shadowing hides the original label as the LabelShadow and
2057 // operations on the original actually affect the shadowing label.
2058 //
2059 // We should probably try to unify the escaping labels and the return
2060 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002061 int nof_escapes = node->escaping_targets()->length();
2062 List<ShadowTarget*> shadows(1 + nof_escapes);
2063
2064 // Add the shadow target for the function return.
2065 static const int kReturnShadowIndex = 0;
2066 shadows.Add(new ShadowTarget(&function_return_));
2067 bool function_return_was_shadowed = function_return_is_shadowed_;
2068 function_return_is_shadowed_ = true;
2069 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2070
2071 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002073 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002074 }
2075
2076 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002077 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002079 // Stop the introduced shadowing and count the number of required unlinks.
2080 // After shadowing stops, the original labels are unshadowed and the
2081 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002082 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002083 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002084 shadows[i]->StopShadowing();
2085 if (shadows[i]->is_linked()) nof_unlinks++;
2086 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002087 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002088
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002089 // Get an external reference to the handler address.
2090 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002092 // If we can fall off the end of the try block, unlink from the try
2093 // chain and set the state on the frame to FALLING.
2094 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002095 // The next handler address is on top of the frame.
2096 ASSERT(StackHandlerConstants::kNextOffset == 0);
2097 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002098 __ mov(r3, Operand(handler_address));
2099 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002100 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002101
2102 // Fake a top of stack value (unneeded when FALLING) and set the
2103 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002104 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002105 frame_->EmitPush(r0);
2106 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2107 if (nof_unlinks > 0) {
2108 finally_block.Jump();
2109 }
2110 }
2111
2112 // Generate code to unlink and set the state for the (formerly)
2113 // shadowing targets that have been jumped to.
2114 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002115 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002116 // If we have come from the shadowed return, the return value is
2117 // in (a non-refcounted reference to) r0. We must preserve it
2118 // until it is pushed.
2119 //
2120 // Because we can be jumping here (to spilled code) from
2121 // unspilled code, we need to reestablish a spilled frame at
2122 // this block.
2123 shadows[i]->Bind();
2124 frame_->SpillAll();
2125
2126 // Reload sp from the top handler, because some statements that
2127 // we break from (eg, for...in) may have left stuff on the
2128 // stack.
2129 __ mov(r3, Operand(handler_address));
2130 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002131 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002132
2133 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002134 // handler address is currently on top of the frame.
2135 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002136 frame_->EmitPop(r1);
2137 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002138 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002139
2140 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002141 // If this label shadowed the function return, materialize the
2142 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002143 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002144 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002145 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002146 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002147 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148 }
2149 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002150 if (--nof_unlinks > 0) {
2151 // If this is not the last unlink block, jump around the next.
2152 finally_block.Jump();
2153 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002154 }
2155 }
2156
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002157 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002158 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002159
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002160 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002161 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002162
2163 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002164 // and the state - while evaluating the finally block.
2165 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002166 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002167 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002168
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002169 if (has_valid_frame()) {
2170 // Restore state and return value or faked TOS.
2171 frame_->EmitPop(r2);
2172 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002173 }
2174
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002175 // Generate code to jump to the right destination for all used
2176 // formerly shadowing targets. Deallocate each shadow target.
2177 for (int i = 0; i < shadows.length(); i++) {
2178 if (has_valid_frame() && shadows[i]->is_bound()) {
2179 JumpTarget* original = shadows[i]->other_target();
2180 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2181 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002182 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002183 skip.Branch(ne);
2184 frame_->PrepareForReturn();
2185 original->Jump();
2186 skip.Bind();
2187 } else {
2188 original->Branch(eq);
2189 }
2190 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002191 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002192
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002193 if (has_valid_frame()) {
2194 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002195 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002196 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2197 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002198
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002199 // Rethrow exception.
2200 frame_->EmitPush(r0);
2201 frame_->CallRuntime(Runtime::kReThrow, 1);
2202
2203 // Done.
2204 exit.Bind();
2205 }
2206 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002207}
2208
2209
ager@chromium.org7c537e22008-10-16 08:43:32 +00002210void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002211#ifdef DEBUG
2212 int original_height = frame_->height();
2213#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002214 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002215 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002216 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002217#ifdef ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002218 frame_->CallRuntime(Runtime::kDebugBreak, 0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002219#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002220 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002221 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002222}
2223
2224
ager@chromium.org7c537e22008-10-16 08:43:32 +00002225void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002226 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002227 ASSERT(boilerplate->IsBoilerplate());
2228
2229 // Push the boilerplate on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002230 __ mov(r0, Operand(boilerplate));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002231 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002232
2233 // Create a new closure.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002234 frame_->EmitPush(cp);
2235 frame_->CallRuntime(Runtime::kNewClosure, 2);
2236 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237}
2238
2239
ager@chromium.org7c537e22008-10-16 08:43:32 +00002240void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002241#ifdef DEBUG
2242 int original_height = frame_->height();
2243#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002244 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002245 Comment cmnt(masm_, "[ FunctionLiteral");
2246
2247 // Build the function boilerplate and instantiate it.
2248 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00002249 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002250 if (HasStackOverflow()) {
2251 ASSERT(frame_->height() == original_height);
2252 return;
2253 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002254 InstantiateBoilerplate(boilerplate);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002255 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256}
2257
2258
ager@chromium.org7c537e22008-10-16 08:43:32 +00002259void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002260 FunctionBoilerplateLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002261#ifdef DEBUG
2262 int original_height = frame_->height();
2263#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002264 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002265 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2266 InstantiateBoilerplate(node->boilerplate());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002267 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002268}
2269
2270
ager@chromium.org7c537e22008-10-16 08:43:32 +00002271void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002272#ifdef DEBUG
2273 int original_height = frame_->height();
2274#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002275 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002277 JumpTarget then;
2278 JumpTarget else_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002279 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
2280 &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002281 if (has_valid_frame()) {
2282 Branch(false, &else_);
2283 }
2284 if (has_valid_frame() || then.is_linked()) {
2285 then.Bind();
2286 LoadAndSpill(node->then_expression(), typeof_state());
2287 }
2288 if (else_.is_linked()) {
2289 JumpTarget exit;
2290 if (has_valid_frame()) exit.Jump();
2291 else_.Bind();
2292 LoadAndSpill(node->else_expression(), typeof_state());
2293 if (exit.is_linked()) exit.Bind();
2294 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002295 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002296}
2297
2298
ager@chromium.org7c537e22008-10-16 08:43:32 +00002299void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002300 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002301 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002302 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002304 JumpTarget slow;
2305 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002306
2307 // Generate fast-case code for variables that might be shadowed by
2308 // eval-introduced variables. Eval is used a lot without
2309 // introducing variables. In those cases, we do not want to
2310 // perform a runtime call for all variables in the scope
2311 // containing the eval.
2312 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2313 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002314 // If there was no control flow to slow, we can exit early.
2315 if (!slow.is_linked()) {
2316 frame_->EmitPush(r0);
2317 return;
2318 }
2319
2320 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002321
2322 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2323 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2324 // Only generate the fast case for locals that rewrite to slots.
2325 // This rules out argument loads.
2326 if (potential_slot != NULL) {
2327 __ ldr(r0,
2328 ContextSlotOperandCheckExtensions(potential_slot,
2329 r1,
2330 r2,
2331 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002332 if (potential_slot->var()->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002333 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2334 __ cmp(r0, ip);
2335 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002336 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002337 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002338 // ContextSlotOperandCheckExtensions so we have to jump around
2339 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002340 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002341 }
2342 }
2343
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002344 slow.Bind();
2345 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002346 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002347 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002348
ager@chromium.org7c537e22008-10-16 08:43:32 +00002349 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002350 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002351 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002352 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002353 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002354
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002355 done.Bind();
2356 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002357
2358 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002359 // Note: We would like to keep the assert below, but it fires because of
2360 // some nasty code in LoadTypeofExpression() which should be removed...
ager@chromium.org381abbb2009-02-25 13:23:22 +00002361 // ASSERT(!slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002362
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002363 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002364 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002365 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002366 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002367 // Const slots may contain 'the hole' value (the constant hasn't been
2368 // initialized yet) which needs to be converted into the 'undefined'
2369 // value.
2370 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002371 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002372 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2373 __ cmp(r0, ip);
2374 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002375 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002376 }
2377 }
2378}
2379
2380
ager@chromium.org381abbb2009-02-25 13:23:22 +00002381void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2382 TypeofState typeof_state,
2383 Register tmp,
2384 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002385 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002386 // Check that no extension objects have been created by calls to
2387 // eval from the current scope to the global scope.
2388 Register context = cp;
2389 Scope* s = scope();
2390 while (s != NULL) {
2391 if (s->num_heap_slots() > 0) {
2392 if (s->calls_eval()) {
2393 // Check that extension is NULL.
2394 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2395 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002396 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002397 }
2398 // Load next context in chain.
2399 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2400 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2401 context = tmp;
2402 }
2403 // If no outer scope calls eval, we do not need to check more
2404 // context extensions.
2405 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2406 s = s->outer_scope();
2407 }
2408
2409 if (s->is_eval_scope()) {
2410 Label next, fast;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002411 if (!context.is(tmp)) {
2412 __ mov(tmp, Operand(context));
2413 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002414 __ bind(&next);
2415 // Terminate at global context.
2416 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002417 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
2418 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002419 __ b(eq, &fast);
2420 // Check that extension is NULL.
2421 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2422 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002423 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002424 // Load next context in chain.
2425 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2426 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2427 __ b(&next);
2428 __ bind(&fast);
2429 }
2430
2431 // All extension objects were empty and it is safe to use a global
2432 // load IC call.
2433 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2434 // Load the global object.
2435 LoadGlobal();
2436 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002437 Result name(r2);
2438 __ mov(r2, Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002439 // Call IC stub.
2440 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002441 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002442 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002443 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002444 }
2445
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002446 // Drop the global object. The result is in r0.
2447 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002448}
2449
2450
ager@chromium.org7c537e22008-10-16 08:43:32 +00002451void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002452#ifdef DEBUG
2453 int original_height = frame_->height();
2454#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002455 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002456 Comment cmnt(masm_, "[ Slot");
2457 LoadFromSlot(node, typeof_state());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002458 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002459}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002460
ager@chromium.org7c537e22008-10-16 08:43:32 +00002461
2462void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002463#ifdef DEBUG
2464 int original_height = frame_->height();
2465#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002466 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002467 Comment cmnt(masm_, "[ VariableProxy");
2468
2469 Variable* var = node->var();
2470 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002471 if (expr != NULL) {
2472 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002473 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002474 ASSERT(var->is_global());
2475 Reference ref(this, node);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002476 ref.GetValueAndSpill(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002477 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002478 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002479}
2480
2481
ager@chromium.org7c537e22008-10-16 08:43:32 +00002482void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002483#ifdef DEBUG
2484 int original_height = frame_->height();
2485#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002486 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002487 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002488 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002489 frame_->EmitPush(r0);
2490 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002491}
2492
2493
ager@chromium.org7c537e22008-10-16 08:43:32 +00002494void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002495#ifdef DEBUG
2496 int original_height = frame_->height();
2497#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002498 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002499 Comment cmnt(masm_, "[ RexExp Literal");
2500
2501 // Retrieve the literal array and check the allocated entry.
2502
2503 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002504 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002505
2506 // Load the literals array of the function.
2507 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2508
2509 // Load the literal at the ast saved index.
2510 int literal_offset =
2511 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2512 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2513
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002514 JumpTarget done;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002515 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2516 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002517 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002518
2519 // If the entry is undefined we call the runtime system to computed
2520 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002521 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002522 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002523 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002524 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002525 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002526 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002527 frame_->EmitPush(r0);
2528 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002529 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002530
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002531 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002532 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002533 frame_->EmitPush(r2);
2534 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002535}
2536
2537
2538// This deferred code stub will be used for creating the boilerplate
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002539// by calling Runtime_CreateObjectLiteralBoilerplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002540// Each created boilerplate is stored in the JSFunction and they are
2541// therefore context dependent.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002542class DeferredObjectLiteral: public DeferredCode {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002543 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002544 explicit DeferredObjectLiteral(ObjectLiteral* node) : node_(node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002545 set_comment("[ DeferredObjectLiteral");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002546 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002547
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002548 virtual void Generate();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002549
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002550 private:
2551 ObjectLiteral* node_;
2552};
2553
2554
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002555void DeferredObjectLiteral::Generate() {
2556 // Argument is passed in r1.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002557
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002558 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002559 // the literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002560 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002561 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002562 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00002563 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002564 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002565 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00002566 __ mov(r0, Operand(node_->constant_properties()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002567 __ push(r0);
2568 __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
2569 __ mov(r2, Operand(r0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002570 // Result is returned in r2.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571}
2572
2573
ager@chromium.org7c537e22008-10-16 08:43:32 +00002574void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002575#ifdef DEBUG
2576 int original_height = frame_->height();
2577#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002578 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002579 Comment cmnt(masm_, "[ ObjectLiteral");
2580
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002581 DeferredObjectLiteral* deferred = new DeferredObjectLiteral(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002582
2583 // Retrieve the literal array and check the allocated entry.
2584
2585 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002586 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002587
2588 // Load the literals array of the function.
2589 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2590
2591 // Load the literal at the ast saved index.
2592 int literal_offset =
2593 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2594 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2595
2596 // Check whether we need to materialize the object literal boilerplate.
2597 // If so, jump to the deferred code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002598 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2599 __ cmp(r2, Operand(ip));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002600 deferred->Branch(eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002601 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002602
2603 // Push the object literal boilerplate.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002604 frame_->EmitPush(r2);
mads.s.ager31e71382008-08-13 09:32:07 +00002605
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002606 // Clone the boilerplate object.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002607 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2608 if (node->depth() == 1) {
2609 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2610 }
2611 frame_->CallRuntime(clone_function_id, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002612 frame_->EmitPush(r0); // save the result
mads.s.ager31e71382008-08-13 09:32:07 +00002613 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002614
2615 for (int i = 0; i < node->properties()->length(); i++) {
2616 ObjectLiteral::Property* property = node->properties()->at(i);
2617 Literal* key = property->key();
2618 Expression* value = property->value();
2619 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002620 case ObjectLiteral::Property::CONSTANT:
2621 break;
2622 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2623 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
2624 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002625 case ObjectLiteral::Property::COMPUTED: // fall through
2626 case ObjectLiteral::Property::PROTOTYPE: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002627 frame_->EmitPush(r0); // dup the result
2628 LoadAndSpill(key);
2629 LoadAndSpill(value);
2630 frame_->CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002631 // restore r0
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002632 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002633 break;
2634 }
2635 case ObjectLiteral::Property::SETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002636 frame_->EmitPush(r0);
2637 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002638 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002639 frame_->EmitPush(r0);
2640 LoadAndSpill(value);
2641 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002642 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002643 break;
2644 }
2645 case ObjectLiteral::Property::GETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002646 frame_->EmitPush(r0);
2647 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002648 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002649 frame_->EmitPush(r0);
2650 LoadAndSpill(value);
2651 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002652 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002653 break;
2654 }
2655 }
2656 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002657 ASSERT(frame_->height() == original_height + 1);
2658}
2659
2660
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002661// This deferred code stub will be used for creating the boilerplate
2662// by calling Runtime_CreateArrayLiteralBoilerplate.
2663// Each created boilerplate is stored in the JSFunction and they are
2664// therefore context dependent.
2665class DeferredArrayLiteral: public DeferredCode {
2666 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002667 explicit DeferredArrayLiteral(ArrayLiteral* node) : node_(node) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002668 set_comment("[ DeferredArrayLiteral");
2669 }
2670
2671 virtual void Generate();
2672
2673 private:
2674 ArrayLiteral* node_;
2675};
2676
2677
2678void DeferredArrayLiteral::Generate() {
2679 // Argument is passed in r1.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002680
2681 // If the entry is undefined we call the runtime system to computed
2682 // the literal.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002683 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002684 __ push(r1);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002685 // Literal index (1).
2686 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002687 __ push(r0);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002688 // Constant properties (2).
2689 __ mov(r0, Operand(node_->literals()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002690 __ push(r0);
2691 __ CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3);
2692 __ mov(r2, Operand(r0));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002693 // Result is returned in r2.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002694}
2695
2696
ager@chromium.org7c537e22008-10-16 08:43:32 +00002697void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002698#ifdef DEBUG
2699 int original_height = frame_->height();
2700#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002701 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002702 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002703
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002704 DeferredArrayLiteral* deferred = new DeferredArrayLiteral(node);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002705
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002706 // Retrieve the literal array and check the allocated entry.
2707
2708 // Load the function of this activation.
2709 __ ldr(r1, frame_->Function());
2710
2711 // Load the literals array of the function.
2712 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2713
2714 // Load the literal at the ast saved index.
2715 int literal_offset =
2716 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2717 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2718
2719 // Check whether we need to materialize the object literal boilerplate.
2720 // If so, jump to the deferred code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002721 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2722 __ cmp(r2, Operand(ip));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002723 deferred->Branch(eq);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002724 deferred->BindExit();
2725
2726 // Push the object literal boilerplate.
2727 frame_->EmitPush(r2);
2728
2729 // Clone the boilerplate object.
2730 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2731 if (node->depth() == 1) {
2732 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2733 }
2734 frame_->CallRuntime(clone_function_id, 1);
2735 frame_->EmitPush(r0); // save the result
2736 // r0: cloned object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002737
2738 // Generate code to set the elements in the array that are not
2739 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002740 for (int i = 0; i < node->values()->length(); i++) {
2741 Expression* value = node->values()->at(i);
2742
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002743 // If value is a literal the property value is already set in the
2744 // boilerplate object.
2745 if (value->AsLiteral() != NULL) continue;
2746 // If value is a materialized literal the property value is already set
2747 // in the boilerplate object if it is simple.
2748 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002749
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002750 // The property must be set by generated code.
2751 LoadAndSpill(value);
2752 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002753
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002754 // Fetch the object literal.
2755 __ ldr(r1, frame_->Top());
2756 // Get the elements array.
2757 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002758
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002759 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002760 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002761 __ str(r0, FieldMemOperand(r1, offset));
2762
2763 // Update the write barrier for the array address.
2764 __ mov(r3, Operand(offset));
2765 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002766 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002767 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002768}
2769
2770
ager@chromium.org32912102009-01-16 10:38:43 +00002771void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002772#ifdef DEBUG
2773 int original_height = frame_->height();
2774#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002775 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org32912102009-01-16 10:38:43 +00002776 // Call runtime routine to allocate the catch extension object and
2777 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002778 Comment cmnt(masm_, "[ CatchExtensionObject");
2779 LoadAndSpill(node->key());
2780 LoadAndSpill(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002781 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2782 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002783 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002784}
2785
2786
ager@chromium.org7c537e22008-10-16 08:43:32 +00002787void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002788#ifdef DEBUG
2789 int original_height = frame_->height();
2790#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002791 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002792 Comment cmnt(masm_, "[ Assignment");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002793 CodeForStatementPosition(node);
mads.s.ager31e71382008-08-13 09:32:07 +00002794
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002795 { Reference target(this, node->target());
2796 if (target.is_illegal()) {
2797 // Fool the virtual frame into thinking that we left the assignment's
2798 // value on the frame.
2799 __ mov(r0, Operand(Smi::FromInt(0)));
2800 frame_->EmitPush(r0);
2801 ASSERT(frame_->height() == original_height + 1);
2802 return;
2803 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002804
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002805 if (node->op() == Token::ASSIGN ||
2806 node->op() == Token::INIT_VAR ||
2807 node->op() == Token::INIT_CONST) {
2808 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002809
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002810 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002811 // +=, *= and similar binary assignments.
2812 // Get the old value of the lhs.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002813 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
2814 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002815 bool overwrite =
2816 (node->value()->AsBinaryOperation() != NULL &&
2817 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002818 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002819 SmiOperation(node->binary_op(),
2820 literal->handle(),
2821 false,
2822 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002823 frame_->EmitPush(r0);
2824
2825 } else {
2826 LoadAndSpill(node->value());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002827 GenericBinaryOperation(node->binary_op(),
2828 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002829 frame_->EmitPush(r0);
2830 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002831 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002832
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002833 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2834 if (var != NULL &&
2835 (var->mode() == Variable::CONST) &&
2836 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2837 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002838
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002839 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002840 CodeForSourcePosition(node->position());
2841 if (node->op() == Token::INIT_CONST) {
2842 // Dynamic constant initializations must use the function context
2843 // and initialize the actual constant declared. Dynamic variable
2844 // initializations are simply assignments and use SetValue.
2845 target.SetValue(CONST_INIT);
2846 } else {
2847 target.SetValue(NOT_CONST_INIT);
2848 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002849 }
2850 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002851 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002852}
2853
2854
ager@chromium.org7c537e22008-10-16 08:43:32 +00002855void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002856#ifdef DEBUG
2857 int original_height = frame_->height();
2858#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002859 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002860 Comment cmnt(masm_, "[ Throw");
2861
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002862 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002863 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002864 frame_->CallRuntime(Runtime::kThrow, 1);
2865 frame_->EmitPush(r0);
2866 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002867}
2868
2869
ager@chromium.org7c537e22008-10-16 08:43:32 +00002870void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002871#ifdef DEBUG
2872 int original_height = frame_->height();
2873#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002874 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002875 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002876
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002877 { Reference property(this, node);
2878 property.GetValueAndSpill(typeof_state());
2879 }
2880 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002881}
2882
2883
ager@chromium.org7c537e22008-10-16 08:43:32 +00002884void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002885#ifdef DEBUG
2886 int original_height = frame_->height();
2887#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002888 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002889 Comment cmnt(masm_, "[ Call");
2890
2891 ZoneList<Expression*>* args = node->arguments();
2892
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002893 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002894 // Standard function call.
2895
2896 // Check if the function is a variable or a property.
2897 Expression* function = node->expression();
2898 Variable* var = function->AsVariableProxy()->AsVariable();
2899 Property* property = function->AsProperty();
2900
2901 // ------------------------------------------------------------------------
2902 // Fast-case: Use inline caching.
2903 // ---
2904 // According to ECMA-262, section 11.2.3, page 44, the function to call
2905 // must be resolved after the arguments have been evaluated. The IC code
2906 // automatically handles this by loading the arguments before the function
2907 // is resolved in cache misses (this also holds for megamorphic calls).
2908 // ------------------------------------------------------------------------
2909
2910 if (var != NULL && !var->is_this() && var->is_global()) {
2911 // ----------------------------------
2912 // JavaScript example: 'foo(1, 2, 3)' // foo is global
2913 // ----------------------------------
2914
2915 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002916 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002917 frame_->EmitPush(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002918
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002919 // Pass the global object as the receiver and let the IC stub
2920 // patch the stack to use the global proxy as 'this' in the
2921 // invoked function.
2922 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002923
2924 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002925 int arg_count = args->length();
2926 for (int i = 0; i < arg_count; i++) {
2927 LoadAndSpill(args->at(i));
2928 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002929
2930 // Setup the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002931 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
2932 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002933 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002934 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
2935 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002936 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002937 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002938 frame_->Drop();
2939 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002940
2941 } else if (var != NULL && var->slot() != NULL &&
2942 var->slot()->type() == Slot::LOOKUP) {
2943 // ----------------------------------
2944 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
2945 // ----------------------------------
2946
2947 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002948 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00002949 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002950 frame_->EmitPush(r0);
2951 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002952 // r0: slot value; r1: receiver
2953
2954 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002955 frame_->EmitPush(r0); // function
2956 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002957
2958 // Call the function.
2959 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002960 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002961
2962 } else if (property != NULL) {
2963 // Check if the key is a literal string.
2964 Literal* literal = property->key()->AsLiteral();
2965
2966 if (literal != NULL && literal->handle()->IsSymbol()) {
2967 // ------------------------------------------------------------------
2968 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
2969 // ------------------------------------------------------------------
2970
2971 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002972 __ mov(r0, Operand(literal->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002973 frame_->EmitPush(r0);
2974 LoadAndSpill(property->obj());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002975
2976 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002977 int arg_count = args->length();
2978 for (int i = 0; i < arg_count; i++) {
2979 LoadAndSpill(args->at(i));
2980 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002981
2982 // Set the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002983 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
2984 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002985 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002986 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002987 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002988
2989 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002990 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00002991
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002992 frame_->EmitPush(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002993
2994 } else {
2995 // -------------------------------------------
2996 // JavaScript example: 'array[index](1, 2, 3)'
2997 // -------------------------------------------
2998
2999 // Load the function to call from the property through a reference.
3000 Reference ref(this, property);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003001 ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003002
3003 // Pass receiver to called function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003004 if (property->is_synthetic()) {
3005 LoadGlobalReceiver(r0);
3006 } else {
3007 __ ldr(r0, frame_->ElementAt(ref.size()));
3008 frame_->EmitPush(r0);
3009 }
3010
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003011 // Call the function.
3012 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003013 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003014 }
3015
3016 } else {
3017 // ----------------------------------
3018 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3019 // ----------------------------------
3020
3021 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003022 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003023
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003024 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003025 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003026
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003027 // 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 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003031 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003032}
3033
3034
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003035void CodeGenerator::VisitCallEval(CallEval* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003036#ifdef DEBUG
3037 int original_height = frame_->height();
3038#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003039 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003040 Comment cmnt(masm_, "[ CallEval");
3041
3042 // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve
3043 // the function we need to call and the receiver of the call.
3044 // Then we call the resolved function using the given arguments.
3045
3046 ZoneList<Expression*>* args = node->arguments();
3047 Expression* function = node->expression();
3048
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003049 CodeForStatementPosition(node);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003050
3051 // Prepare stack for call to resolved function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003052 LoadAndSpill(function);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003053 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003054 frame_->EmitPush(r2); // Slot for receiver
3055 int arg_count = args->length();
3056 for (int i = 0; i < arg_count; i++) {
3057 LoadAndSpill(args->at(i));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003058 }
3059
3060 // Prepare stack for call to ResolvePossiblyDirectEval.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003061 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3062 frame_->EmitPush(r1);
3063 if (arg_count > 0) {
3064 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3065 frame_->EmitPush(r1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003066 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003067 frame_->EmitPush(r2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003068 }
3069
3070 // Resolve the call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003071 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003072
3073 // Touch up stack with the right values for the function and the receiver.
3074 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003075 __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003076 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003077 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003078
3079 // Call the function.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003080 CodeForSourcePosition(node->position());
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003081
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003082 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3083 CallFunctionStub call_function(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003084 frame_->CallStub(&call_function, arg_count + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003085
3086 __ ldr(cp, frame_->Context());
3087 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003088 frame_->Drop();
3089 frame_->EmitPush(r0);
3090 ASSERT(frame_->height() == original_height + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003091}
3092
3093
ager@chromium.org7c537e22008-10-16 08:43:32 +00003094void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003095#ifdef DEBUG
3096 int original_height = frame_->height();
3097#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003098 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003099 Comment cmnt(masm_, "[ CallNew");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003100 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003101
3102 // According to ECMA-262, section 11.2.2, page 44, the function
3103 // expression in new calls must be evaluated before the
3104 // arguments. This is different from ordinary calls, where the
3105 // actual function to call is resolved after the arguments have been
3106 // evaluated.
3107
3108 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003109 // receiver. There is no need to use the global proxy here because
3110 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003111 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003112 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003113
3114 // Push the arguments ("left-to-right") on the stack.
3115 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003116 int arg_count = args->length();
3117 for (int i = 0; i < arg_count; i++) {
3118 LoadAndSpill(args->at(i));
3119 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003120
mads.s.ager31e71382008-08-13 09:32:07 +00003121 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003122 Result num_args(r0);
3123 __ mov(r0, Operand(arg_count));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003124
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003125 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003126 Result function(r1);
3127 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003128
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003129 // Call the construct call builtin that handles allocation and
3130 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003131 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003132 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003133 frame_->CallCodeObject(ic,
3134 RelocInfo::CONSTRUCT_CALL,
3135 &num_args,
3136 &function,
3137 arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003138
3139 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003140 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003141 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003142}
3143
3144
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003145void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
3146 VirtualFrame::SpilledScope spilled_scope;
3147 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003148 JumpTarget leave, null, function, non_function_constructor;
3149
3150 // Load the object into r0.
3151 LoadAndSpill(args->at(0));
3152 frame_->EmitPop(r0);
3153
3154 // If the object is a smi, we return null.
3155 __ tst(r0, Operand(kSmiTagMask));
3156 null.Branch(eq);
3157
3158 // Check that the object is a JS object but take special care of JS
3159 // functions to make sure they have 'Function' as their class.
3160 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
3161 null.Branch(lt);
3162
3163 // As long as JS_FUNCTION_TYPE is the last instance type and it is
3164 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
3165 // LAST_JS_OBJECT_TYPE.
3166 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3167 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
3168 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3169 function.Branch(eq);
3170
3171 // Check if the constructor in the map is a function.
3172 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
3173 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
3174 non_function_constructor.Branch(ne);
3175
3176 // The r0 register now contains the constructor function. Grab the
3177 // instance class name from there.
3178 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
3179 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003180 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003181 leave.Jump();
3182
3183 // Functions have class 'Function'.
3184 function.Bind();
3185 __ mov(r0, Operand(Factory::function_class_symbol()));
3186 frame_->EmitPush(r0);
3187 leave.Jump();
3188
3189 // Objects with a non-function constructor have class 'Object'.
3190 non_function_constructor.Bind();
3191 __ mov(r0, Operand(Factory::Object_symbol()));
3192 frame_->EmitPush(r0);
3193 leave.Jump();
3194
3195 // Non-JS objects have class null.
3196 null.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003197 __ LoadRoot(r0, Heap::kNullValueRootIndex);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003198 frame_->EmitPush(r0);
3199
3200 // All done.
3201 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003202}
3203
3204
ager@chromium.org7c537e22008-10-16 08:43:32 +00003205void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003206 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003207 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003208 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003209 LoadAndSpill(args->at(0));
3210 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003211 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003212 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003213 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003214 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3215 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003216 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003217 // Load the value.
3218 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003219 leave.Bind();
3220 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003221}
3222
3223
ager@chromium.org7c537e22008-10-16 08:43:32 +00003224void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003225 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003226 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003227 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003228 LoadAndSpill(args->at(0)); // Load the object.
3229 LoadAndSpill(args->at(1)); // Load the value.
3230 frame_->EmitPop(r0); // r0 contains value
3231 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003232 // if (object->IsSmi()) return object.
3233 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003234 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003235 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3236 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003237 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003238 // Store the value.
3239 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3240 // Update the write barrier.
3241 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3242 __ RecordWrite(r1, r2, r3);
3243 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003244 leave.Bind();
3245 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003246}
3247
3248
ager@chromium.org7c537e22008-10-16 08:43:32 +00003249void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003250 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003251 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003252 LoadAndSpill(args->at(0));
3253 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003254 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003255 cc_reg_ = eq;
3256}
3257
3258
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003259void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003260 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003261 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3262 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003263#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003264 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003265 LoadAndSpill(args->at(1));
3266 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003267 __ CallRuntime(Runtime::kLog, 2);
3268 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003269#endif
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003270 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003271 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003272}
3273
3274
ager@chromium.org7c537e22008-10-16 08:43:32 +00003275void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003276 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003277 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003278 LoadAndSpill(args->at(0));
3279 frame_->EmitPop(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003280 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003281 cc_reg_ = eq;
3282}
3283
3284
kasper.lund7276f142008-07-30 08:49:36 +00003285// This should generate code that performs a charCodeAt() call or returns
3286// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3287// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003288void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003289 VirtualFrame::SpilledScope spilled_scope;
kasper.lund7276f142008-07-30 08:49:36 +00003290 ASSERT(args->length() == 2);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003291 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003292 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003293}
3294
3295
ager@chromium.org7c537e22008-10-16 08:43:32 +00003296void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003297 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003298 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003299 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003300 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003301 // We need the CC bits to come out as not_equal in the case where the
3302 // object is a smi. This can't be done with the usual test opcode so
3303 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003304 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003305 __ and_(r1, r0, Operand(kSmiTagMask));
3306 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003307 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003308 // It is a heap object - get the map. Check if the object is a JS array.
3309 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003310 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003311 cc_reg_ = eq;
3312}
3313
3314
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003315void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
3316 VirtualFrame::SpilledScope spilled_scope;
3317 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003318
3319 // Get the frame pointer for the calling frame.
3320 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3321
3322 // Skip the arguments adaptor frame if it exists.
3323 Label check_frame_marker;
3324 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
3325 __ cmp(r1, Operand(ArgumentsAdaptorFrame::SENTINEL));
3326 __ b(ne, &check_frame_marker);
3327 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
3328
3329 // Check the marker in the calling frame.
3330 __ bind(&check_frame_marker);
3331 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
3332 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
3333 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003334}
3335
3336
ager@chromium.org7c537e22008-10-16 08:43:32 +00003337void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003338 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003339 ASSERT(args->length() == 0);
3340
mads.s.ager31e71382008-08-13 09:32:07 +00003341 // Seed the result with the formal parameters count, which will be used
3342 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003343 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
3344
3345 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003346 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003347 frame_->CallStub(&stub, 0);
3348 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003349}
3350
3351
ager@chromium.org7c537e22008-10-16 08:43:32 +00003352void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003353 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003354 ASSERT(args->length() == 1);
3355
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003356 // Satisfy contract with ArgumentsAccessStub:
3357 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003358 LoadAndSpill(args->at(0));
3359 frame_->EmitPop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003360 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003361
3362 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003363 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003364 frame_->CallStub(&stub, 0);
3365 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003366}
3367
3368
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003369void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) {
3370 VirtualFrame::SpilledScope spilled_scope;
3371 ASSERT(args->length() == 0);
3372 __ Call(ExternalReference::random_positive_smi_function().address(),
3373 RelocInfo::RUNTIME_ENTRY);
3374 frame_->EmitPush(r0);
3375}
3376
3377
3378void CodeGenerator::GenerateFastMathOp(MathOp op, ZoneList<Expression*>* args) {
3379 VirtualFrame::SpilledScope spilled_scope;
3380 LoadAndSpill(args->at(0));
3381 switch (op) {
3382 case SIN:
3383 frame_->CallRuntime(Runtime::kMath_sin, 1);
3384 break;
3385 case COS:
3386 frame_->CallRuntime(Runtime::kMath_cos, 1);
3387 break;
3388 }
3389 frame_->EmitPush(r0);
3390}
3391
3392
ager@chromium.org7c537e22008-10-16 08:43:32 +00003393void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003394 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003395 ASSERT(args->length() == 2);
3396
3397 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003398 LoadAndSpill(args->at(0));
3399 LoadAndSpill(args->at(1));
3400 frame_->EmitPop(r0);
3401 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003402 __ cmp(r0, Operand(r1));
3403 cc_reg_ = eq;
3404}
3405
3406
ager@chromium.org7c537e22008-10-16 08:43:32 +00003407void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003408#ifdef DEBUG
3409 int original_height = frame_->height();
3410#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003411 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003412 if (CheckForInlineRuntimeCall(node)) {
3413 ASSERT((has_cc() && frame_->height() == original_height) ||
3414 (!has_cc() && frame_->height() == original_height + 1));
3415 return;
3416 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003417
3418 ZoneList<Expression*>* args = node->arguments();
3419 Comment cmnt(masm_, "[ CallRuntime");
3420 Runtime::Function* function = node->function();
3421
ager@chromium.org41826e72009-03-30 13:30:57 +00003422 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003423 // Prepare stack for calling JS runtime function.
3424 __ mov(r0, Operand(node->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003425 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003426 // Push the builtins object found in the current global object.
3427 __ ldr(r1, GlobalObject());
3428 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003429 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003430 }
mads.s.ager31e71382008-08-13 09:32:07 +00003431
ager@chromium.org41826e72009-03-30 13:30:57 +00003432 // Push the arguments ("left-to-right").
3433 int arg_count = args->length();
3434 for (int i = 0; i < arg_count; i++) {
3435 LoadAndSpill(args->at(i));
3436 }
mads.s.ager31e71382008-08-13 09:32:07 +00003437
ager@chromium.org41826e72009-03-30 13:30:57 +00003438 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003439 // Call the JS runtime function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003440 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3441 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003442 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003443 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003444 frame_->Drop();
3445 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003446 } else {
3447 // Call the C runtime function.
3448 frame_->CallRuntime(function, arg_count);
3449 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003450 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003451 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003452}
3453
3454
ager@chromium.org7c537e22008-10-16 08:43:32 +00003455void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003456#ifdef DEBUG
3457 int original_height = frame_->height();
3458#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003459 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003460 Comment cmnt(masm_, "[ UnaryOperation");
3461
3462 Token::Value op = node->op();
3463
3464 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003465 LoadConditionAndSpill(node->expression(),
3466 NOT_INSIDE_TYPEOF,
3467 false_target(),
3468 true_target(),
3469 true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003470 // LoadCondition may (and usually does) leave a test and branch to
3471 // be emitted by the caller. In that case, negate the condition.
3472 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003473
3474 } else if (op == Token::DELETE) {
3475 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003476 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003477 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003478 LoadAndSpill(property->obj());
3479 LoadAndSpill(property->key());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003480 Result arg_count(r0);
3481 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003482 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 if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003485 Slot* slot = variable->slot();
3486 if (variable->is_global()) {
3487 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003488 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003489 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003490 Result arg_count(r0);
3491 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003492 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003493
3494 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3495 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003496 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003497 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003498 frame_->EmitPush(r0);
3499 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003500 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003501 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003502 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003503 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003504 Result arg_count(r0);
3505 __ mov(r0, Operand(1)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003506 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003507
mads.s.ager31e71382008-08-13 09:32:07 +00003508 } else {
3509 // Default: Result of deleting non-global, not dynamically
3510 // introduced variables is false.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003511 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00003512 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003513
3514 } else {
3515 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003516 LoadAndSpill(node->expression()); // may have side-effects
3517 frame_->Drop();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003518 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003519 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003520 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003521
3522 } else if (op == Token::TYPEOF) {
3523 // Special case for loading the typeof expression; see comment on
3524 // LoadTypeofExpression().
3525 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003526 frame_->CallRuntime(Runtime::kTypeof, 1);
3527 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003528
3529 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003530 LoadAndSpill(node->expression());
3531 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003532 switch (op) {
3533 case Token::NOT:
3534 case Token::DELETE:
3535 case Token::TYPEOF:
3536 UNREACHABLE(); // handled above
3537 break;
3538
3539 case Token::SUB: {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003540 bool overwrite =
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003541 (node->expression()->AsBinaryOperation() != NULL &&
3542 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003543 UnarySubStub stub(overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003544 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003545 break;
3546 }
3547
3548 case Token::BIT_NOT: {
3549 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003550 JumpTarget smi_label;
3551 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003552 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003553 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003554
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003555 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003556 Result arg_count(r0);
3557 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003558 frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003559
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003560 continue_label.Jump();
3561 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003562 __ mvn(r0, Operand(r0));
3563 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003564 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003565 break;
3566 }
3567
3568 case Token::VOID:
3569 // since the stack top is cached in r0, popping and then
3570 // pushing a value can be done by just writing to r0.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003571 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003572 break;
3573
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003574 case Token::ADD: {
3575 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003576 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003577 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003578 continue_label.Branch(eq);
3579 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003580 Result arg_count(r0);
3581 __ mov(r0, Operand(0)); // not counting receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003582 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3583 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003584 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003585 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003586 default:
3587 UNREACHABLE();
3588 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003589 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003590 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003591 ASSERT(!has_valid_frame() ||
3592 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003593 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003594}
3595
3596
ager@chromium.org7c537e22008-10-16 08:43:32 +00003597void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003598#ifdef DEBUG
3599 int original_height = frame_->height();
3600#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003601 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003602 Comment cmnt(masm_, "[ CountOperation");
3603
3604 bool is_postfix = node->is_postfix();
3605 bool is_increment = node->op() == Token::INC;
3606
3607 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3608 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3609
3610 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003611 if (is_postfix) {
3612 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003613 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003614 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003615
3616 { Reference target(this, node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003617 if (target.is_illegal()) {
3618 // Spoof the virtual frame to have the expected height (one higher
3619 // than on entry).
3620 if (!is_postfix) {
3621 __ mov(r0, Operand(Smi::FromInt(0)));
3622 frame_->EmitPush(r0);
3623 }
3624 ASSERT(frame_->height() == original_height + 1);
3625 return;
3626 }
3627 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
3628 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003629
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003630 JumpTarget slow;
3631 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003632
3633 // Load the value (1) into register r1.
3634 __ mov(r1, Operand(Smi::FromInt(1)));
3635
3636 // Check for smi operand.
3637 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003638 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003639
3640 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003641 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003642 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003643 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003644
3645 // Perform optimistic increment/decrement.
3646 if (is_increment) {
3647 __ add(r0, r0, Operand(r1), SetCC);
3648 } else {
3649 __ sub(r0, r0, Operand(r1), SetCC);
3650 }
3651
3652 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003653 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003654
3655 // Revert optimistic increment/decrement.
3656 if (is_increment) {
3657 __ sub(r0, r0, Operand(r1));
3658 } else {
3659 __ add(r0, r0, Operand(r1));
3660 }
3661
3662 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003663 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003664 {
3665 // Convert the operand to a number.
3666 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003667 Result arg_count(r0);
3668 __ mov(r0, Operand(0));
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003669 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3670 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003671 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003672 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003673 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003674 }
3675
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003676 // Compute the new value.
3677 __ mov(r1, Operand(Smi::FromInt(1)));
3678 frame_->EmitPush(r0);
3679 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003680 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003681 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003682 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003683 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003684 }
3685
3686 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003687 exit.Bind();
3688 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003689 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003690 }
3691
3692 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003693 if (is_postfix) frame_->EmitPop(r0);
3694 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003695}
3696
3697
ager@chromium.org7c537e22008-10-16 08:43:32 +00003698void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003699#ifdef DEBUG
3700 int original_height = frame_->height();
3701#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003702 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003703 Comment cmnt(masm_, "[ BinaryOperation");
3704 Token::Value op = node->op();
3705
3706 // According to ECMA-262 section 11.11, page 58, the binary logical
3707 // operators must yield the result of one of the two expressions
3708 // before any ToBoolean() conversions. This means that the value
3709 // produced by a && or || operator is not necessarily a boolean.
3710
3711 // NOTE: If the left hand side produces a materialized value (not in
3712 // the CC register), we force the right hand side to do the
3713 // same. This is necessary because we may have to branch to the exit
3714 // after evaluating the left hand side (due to the shortcut
3715 // semantics), but the compiler must (statically) know if the result
3716 // of compiling the binary operation is materialized or not.
3717
3718 if (op == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003719 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003720 LoadConditionAndSpill(node->left(),
3721 NOT_INSIDE_TYPEOF,
3722 &is_true,
3723 false_target(),
3724 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003725 if (has_valid_frame() && !has_cc()) {
3726 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003727 JumpTarget pop_and_continue;
3728 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003729
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003730 __ ldr(r0, frame_->Top()); // Duplicate the stack top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003731 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003732 // Avoid popping the result if it converts to 'false' using the
3733 // standard ToBoolean() conversion as described in ECMA-262,
3734 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003735 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003736 Branch(false, &exit);
3737
3738 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003739 pop_and_continue.Bind();
3740 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003741
3742 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003743 is_true.Bind();
3744 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003745
3746 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003747 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003748 } else if (has_cc() || is_true.is_linked()) {
3749 // The left-hand side is either (a) partially compiled to
3750 // control flow with a final branch left to emit or (b) fully
3751 // compiled to control flow and possibly true.
3752 if (has_cc()) {
3753 Branch(false, false_target());
3754 }
3755 is_true.Bind();
3756 LoadConditionAndSpill(node->right(),
3757 NOT_INSIDE_TYPEOF,
3758 true_target(),
3759 false_target(),
3760 false);
3761 } else {
3762 // Nothing to do.
3763 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003764 }
3765
3766 } else if (op == Token::OR) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003767 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003768 LoadConditionAndSpill(node->left(),
3769 NOT_INSIDE_TYPEOF,
3770 true_target(),
3771 &is_false,
3772 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003773 if (has_valid_frame() && !has_cc()) {
3774 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003775 JumpTarget pop_and_continue;
3776 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003777
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003778 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003779 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003780 // Avoid popping the result if it converts to 'true' using the
3781 // standard ToBoolean() conversion as described in ECMA-262,
3782 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003783 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003784 Branch(true, &exit);
3785
3786 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003787 pop_and_continue.Bind();
3788 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003789
3790 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003791 is_false.Bind();
3792 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003793
3794 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003795 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003796 } else if (has_cc() || is_false.is_linked()) {
3797 // The left-hand side is either (a) partially compiled to
3798 // control flow with a final branch left to emit or (b) fully
3799 // compiled to control flow and possibly false.
3800 if (has_cc()) {
3801 Branch(true, true_target());
3802 }
3803 is_false.Bind();
3804 LoadConditionAndSpill(node->right(),
3805 NOT_INSIDE_TYPEOF,
3806 true_target(),
3807 false_target(),
3808 false);
3809 } else {
3810 // Nothing to do.
3811 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003812 }
3813
3814 } else {
3815 // Optimize for the case where (at least) one of the expressions
3816 // is a literal small integer.
3817 Literal* lliteral = node->left()->AsLiteral();
3818 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003819 // NOTE: The code below assumes that the slow cases (calls to runtime)
3820 // never return a constant/immutable object.
3821 bool overwrite_left =
3822 (node->left()->AsBinaryOperation() != NULL &&
3823 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
3824 bool overwrite_right =
3825 (node->right()->AsBinaryOperation() != NULL &&
3826 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003827
3828 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003829 LoadAndSpill(node->left());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003830 SmiOperation(node->op(),
3831 rliteral->handle(),
3832 false,
3833 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003834
3835 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003836 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003837 SmiOperation(node->op(),
3838 lliteral->handle(),
3839 true,
3840 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003841
3842 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003843 OverwriteMode overwrite_mode = NO_OVERWRITE;
3844 if (overwrite_left) {
3845 overwrite_mode = OVERWRITE_LEFT;
3846 } else if (overwrite_right) {
3847 overwrite_mode = OVERWRITE_RIGHT;
3848 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003849 LoadAndSpill(node->left());
3850 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003851 GenericBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003852 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003853 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003854 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003855 ASSERT(!has_valid_frame() ||
3856 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003857 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003858}
3859
3860
ager@chromium.org7c537e22008-10-16 08:43:32 +00003861void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003862#ifdef DEBUG
3863 int original_height = frame_->height();
3864#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003865 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003866 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003867 frame_->EmitPush(r0);
3868 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003869}
3870
3871
ager@chromium.org7c537e22008-10-16 08:43:32 +00003872void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003873#ifdef DEBUG
3874 int original_height = frame_->height();
3875#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003876 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003877 Comment cmnt(masm_, "[ CompareOperation");
3878
3879 // Get the expressions from the node.
3880 Expression* left = node->left();
3881 Expression* right = node->right();
3882 Token::Value op = node->op();
3883
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003884 // To make null checks efficient, we check if either left or right is the
3885 // literal 'null'. If so, we optimize the code by inlining a null check
3886 // instead of calling the (very) general runtime routine for checking
3887 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003888 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003889 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003890 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003891 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003892 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
3893 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003894 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003895 LoadAndSpill(left_is_null ? right : left);
3896 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003897 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3898 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003899
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003900 // The 'null' value is only equal to 'undefined' if using non-strict
3901 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003902 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003903 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003904
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003905 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3906 __ cmp(r0, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003907 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003908
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003909 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003910 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003911
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003912 // It can be an undetectable object.
3913 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
3914 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
3915 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
3916 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003917 }
3918
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003919 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003920 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003921 return;
3922 }
3923 }
3924
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003925 // To make typeof testing for natives implemented in JavaScript really
3926 // efficient, we generate special code for expressions of the form:
3927 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003928 UnaryOperation* operation = left->AsUnaryOperation();
3929 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
3930 (operation != NULL && operation->op() == Token::TYPEOF) &&
3931 (right->AsLiteral() != NULL &&
3932 right->AsLiteral()->handle()->IsString())) {
3933 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
3934
mads.s.ager31e71382008-08-13 09:32:07 +00003935 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003936 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003937 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003938
3939 if (check->Equals(Heap::number_symbol())) {
3940 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003941 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003942 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003943 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
3944 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003945 cc_reg_ = eq;
3946
3947 } else if (check->Equals(Heap::string_symbol())) {
3948 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003949 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003950
3951 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3952
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003953 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003954 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3955 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3956 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003957 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003958
3959 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
3960 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
3961 cc_reg_ = lt;
3962
3963 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003964 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
3965 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003966 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003967 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
3968 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003969 cc_reg_ = eq;
3970
3971 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003972 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3973 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003974 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003975
3976 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003977 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003978
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003979 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003980 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
3981 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
3982 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
3983 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
3984
3985 cc_reg_ = eq;
3986
3987 } else if (check->Equals(Heap::function_symbol())) {
3988 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003989 false_target()->Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003990 __ CompareObjectType(r1, r1, r1, JS_FUNCTION_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003991 cc_reg_ = eq;
3992
3993 } else if (check->Equals(Heap::object_symbol())) {
3994 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003995 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003996
3997 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003998 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3999 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004000 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004001
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004002 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004003 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
4004 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4005 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004006 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004007
4008 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4009 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004010 false_target()->Branch(lt);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004011 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
4012 cc_reg_ = le;
4013
4014 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004015 // Uncommon case: typeof testing against a string literal that is
4016 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004017 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004018 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004019 ASSERT(!has_valid_frame() ||
4020 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004021 return;
4022 }
4023
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004024 switch (op) {
4025 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004026 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004027 break;
4028
4029 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004030 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004031 break;
4032
4033 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004034 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004035 break;
4036
4037 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004038 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004039 break;
4040
4041 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004042 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004043 break;
4044
4045 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004046 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004047 break;
4048
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004049 case Token::IN: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004050 LoadAndSpill(left);
4051 LoadAndSpill(right);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004052 Result arg_count(r0);
4053 __ mov(r0, Operand(1)); // not counting receiver
4054 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, &arg_count, 2);
4055 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004056 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004057 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004058
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004059 case Token::INSTANCEOF: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004060 LoadAndSpill(left);
4061 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004062 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004063 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004064 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004065 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004066 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004067 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004068 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004069
4070 default:
4071 UNREACHABLE();
4072 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004073 ASSERT((has_cc() && frame_->height() == original_height) ||
4074 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004075}
4076
4077
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004078#ifdef DEBUG
4079bool CodeGenerator::HasValidEntryRegisters() { return true; }
4080#endif
4081
4082
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004083#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004084#define __ ACCESS_MASM(masm)
4085
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004086
ager@chromium.org7c537e22008-10-16 08:43:32 +00004087Handle<String> Reference::GetName() {
4088 ASSERT(type_ == NAMED);
4089 Property* property = expression_->AsProperty();
4090 if (property == NULL) {
4091 // Global variable reference treated as a named property reference.
4092 VariableProxy* proxy = expression_->AsVariableProxy();
4093 ASSERT(proxy->AsVariable() != NULL);
4094 ASSERT(proxy->AsVariable()->is_global());
4095 return proxy->name();
4096 } else {
4097 Literal* raw_name = property->key()->AsLiteral();
4098 ASSERT(raw_name != NULL);
4099 return Handle<String>(String::cast(*raw_name->handle()));
4100 }
4101}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004102
ager@chromium.org7c537e22008-10-16 08:43:32 +00004103
4104void Reference::GetValue(TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004105 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004106 ASSERT(!is_illegal());
4107 ASSERT(!cgen_->has_cc());
4108 MacroAssembler* masm = cgen_->masm();
4109 Property* property = expression_->AsProperty();
4110 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004111 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004112 }
4113
4114 switch (type_) {
4115 case SLOT: {
4116 Comment cmnt(masm, "[ Load from Slot");
4117 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4118 ASSERT(slot != NULL);
4119 cgen_->LoadFromSlot(slot, typeof_state);
4120 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004121 }
4122
ager@chromium.org7c537e22008-10-16 08:43:32 +00004123 case NAMED: {
4124 // TODO(1241834): Make sure that this it is safe to ignore the
4125 // distinction between expressions in a typeof and not in a typeof. If
4126 // there is a chance that reference errors can be thrown below, we
4127 // must distinguish between the two kinds of loads (typeof expression
4128 // loads must not throw a reference error).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004129 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004130 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004131 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004132 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004133 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4134 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004135 Result name_reg(r2);
4136 __ mov(r2, Operand(name));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004137 ASSERT(var == NULL || var->is_global());
4138 RelocInfo::Mode rmode = (var == NULL)
4139 ? RelocInfo::CODE_TARGET
4140 : RelocInfo::CODE_TARGET_CONTEXT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004141 frame->CallCodeObject(ic, rmode, &name_reg, 0);
4142 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004143 break;
4144 }
4145
4146 case KEYED: {
4147 // TODO(1241834): Make sure that this it is safe to ignore the
4148 // distinction between expressions in a typeof and not in a typeof.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004149
4150 // TODO(181): Implement inlined version of array indexing once
4151 // loop nesting is properly tracked on ARM.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004152 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004153 Comment cmnt(masm, "[ Load from keyed Property");
4154 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004155 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004156 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004157 ASSERT(var == NULL || var->is_global());
4158 RelocInfo::Mode rmode = (var == NULL)
4159 ? RelocInfo::CODE_TARGET
4160 : RelocInfo::CODE_TARGET_CONTEXT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004161 frame->CallCodeObject(ic, rmode, 0);
4162 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004163 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004164 }
4165
4166 default:
4167 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004168 }
4169}
4170
4171
ager@chromium.org7c537e22008-10-16 08:43:32 +00004172void Reference::SetValue(InitState init_state) {
4173 ASSERT(!is_illegal());
4174 ASSERT(!cgen_->has_cc());
4175 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004176 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004177 Property* property = expression_->AsProperty();
4178 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004179 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004180 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004181
ager@chromium.org7c537e22008-10-16 08:43:32 +00004182 switch (type_) {
4183 case SLOT: {
4184 Comment cmnt(masm, "[ Store to Slot");
4185 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4186 ASSERT(slot != NULL);
4187 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004188 ASSERT(slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004189
ager@chromium.org7c537e22008-10-16 08:43:32 +00004190 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004191 frame->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004192 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004193 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004194
ager@chromium.org7c537e22008-10-16 08:43:32 +00004195 if (init_state == CONST_INIT) {
4196 // Same as the case for a normal store, but ignores attribute
4197 // (e.g. READ_ONLY) of context slot so that we can initialize
4198 // const properties (introduced via eval("const foo = (some
4199 // expr);")). Also, uses the current function context instead of
4200 // the top context.
4201 //
4202 // Note that we must declare the foo upon entry of eval(), via a
4203 // context slot declaration, but we cannot initialize it at the
4204 // same time, because the const declaration may be at the end of
4205 // the eval code (sigh...) and the const variable may have been
4206 // used before (where its value is 'undefined'). Thus, we can only
4207 // do the initialization when we actually encounter the expression
4208 // and when the expression operands are defined and valid, and
4209 // thus we need the split into 2 operations: declaration of the
4210 // context slot followed by initialization.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004211 frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004212 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004213 frame->CallRuntime(Runtime::kStoreContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004214 }
4215 // Storing a variable must keep the (new) value on the expression
4216 // stack. This is necessary for compiling assignment expressions.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004217 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004218
ager@chromium.org7c537e22008-10-16 08:43:32 +00004219 } else {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004220 ASSERT(!slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004221
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004222 JumpTarget exit;
ager@chromium.org7c537e22008-10-16 08:43:32 +00004223 if (init_state == CONST_INIT) {
4224 ASSERT(slot->var()->mode() == Variable::CONST);
4225 // Only the first const initialization must be executed (the slot
4226 // still contains 'the hole' value). When the assignment is
4227 // executed, the code is identical to a normal store (see below).
4228 Comment cmnt(masm, "[ Init const");
4229 __ ldr(r2, cgen_->SlotOperand(slot, r2));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004230 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
4231 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004232 exit.Branch(ne);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004233 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004234
ager@chromium.org7c537e22008-10-16 08:43:32 +00004235 // We must execute the store. Storing a variable must keep the
4236 // (new) value on the stack. This is necessary for compiling
4237 // assignment expressions.
4238 //
4239 // Note: We will reach here even with slot->var()->mode() ==
4240 // Variable::CONST because of const declarations which will
4241 // initialize consts to 'the hole' value and by doing so, end up
4242 // calling this code. r2 may be loaded with context; used below in
4243 // RecordWrite.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004244 frame->EmitPop(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004245 __ str(r0, cgen_->SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004246 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004247 if (slot->type() == Slot::CONTEXT) {
4248 // Skip write barrier if the written value is a smi.
4249 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004250 exit.Branch(eq);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004251 // r2 is loaded with context when calling SlotOperand above.
4252 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
4253 __ mov(r3, Operand(offset));
4254 __ RecordWrite(r2, r3, r1);
4255 }
4256 // If we definitely did not jump over the assignment, we do not need
4257 // to bind the exit label. Doing so can defeat peephole
4258 // optimization.
4259 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004260 exit.Bind();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004261 }
4262 }
4263 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004264 }
4265
ager@chromium.org7c537e22008-10-16 08:43:32 +00004266 case NAMED: {
4267 Comment cmnt(masm, "[ Store to named Property");
4268 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004269 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004270 Handle<String> name(GetName());
4271
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004272 Result value(r0);
4273 frame->EmitPop(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004274
4275 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004276 Result property_name(r2);
4277 __ mov(r2, Operand(name));
4278 frame->CallCodeObject(ic,
4279 RelocInfo::CODE_TARGET,
4280 &value,
4281 &property_name,
4282 0);
4283 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004284 break;
4285 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004286
ager@chromium.org7c537e22008-10-16 08:43:32 +00004287 case KEYED: {
4288 Comment cmnt(masm, "[ Store to keyed Property");
4289 Property* property = expression_->AsProperty();
4290 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004291 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004292
4293 // Call IC code.
4294 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
4295 // TODO(1222589): Make the IC grab the values from the stack.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004296 Result value(r0);
4297 frame->EmitPop(r0); // value
4298 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0);
4299 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004300 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004301 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004302
4303 default:
4304 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004305 }
4306}
4307
4308
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004309// Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz
4310// instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0
4311// (31 instead of 32).
4312static void CountLeadingZeros(
4313 MacroAssembler* masm,
4314 Register source,
4315 Register scratch,
4316 Register zeros) {
4317#ifdef __ARM_ARCH_5__
4318 __ clz(zeros, source); // This instruction is only supported after ARM5.
4319#else
4320 __ mov(zeros, Operand(0));
4321 __ mov(scratch, source);
4322 // Top 16.
4323 __ tst(scratch, Operand(0xffff0000));
4324 __ add(zeros, zeros, Operand(16), LeaveCC, eq);
4325 __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
4326 // Top 8.
4327 __ tst(scratch, Operand(0xff000000));
4328 __ add(zeros, zeros, Operand(8), LeaveCC, eq);
4329 __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
4330 // Top 4.
4331 __ tst(scratch, Operand(0xf0000000));
4332 __ add(zeros, zeros, Operand(4), LeaveCC, eq);
4333 __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
4334 // Top 2.
4335 __ tst(scratch, Operand(0xc0000000));
4336 __ add(zeros, zeros, Operand(2), LeaveCC, eq);
4337 __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
4338 // Top bit.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004339 __ tst(scratch, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004340 __ add(zeros, zeros, Operand(1), LeaveCC, eq);
4341#endif
4342}
4343
4344
4345// Takes a Smi and converts to an IEEE 64 bit floating point value in two
4346// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
4347// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
4348// scratch register. Destroys the source register. No GC occurs during this
4349// stub so you don't have to set up the frame.
4350class ConvertToDoubleStub : public CodeStub {
4351 public:
4352 ConvertToDoubleStub(Register result_reg_1,
4353 Register result_reg_2,
4354 Register source_reg,
4355 Register scratch_reg)
4356 : result1_(result_reg_1),
4357 result2_(result_reg_2),
4358 source_(source_reg),
4359 zeros_(scratch_reg) { }
4360
4361 private:
4362 Register result1_;
4363 Register result2_;
4364 Register source_;
4365 Register zeros_;
4366
4367 // Minor key encoding in 16 bits.
4368 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4369 class OpBits: public BitField<Token::Value, 2, 14> {};
4370
4371 Major MajorKey() { return ConvertToDouble; }
4372 int MinorKey() {
4373 // Encode the parameters in a unique 16 bit value.
4374 return result1_.code() +
4375 (result2_.code() << 4) +
4376 (source_.code() << 8) +
4377 (zeros_.code() << 12);
4378 }
4379
4380 void Generate(MacroAssembler* masm);
4381
4382 const char* GetName() { return "ConvertToDoubleStub"; }
4383
4384#ifdef DEBUG
4385 void Print() { PrintF("ConvertToDoubleStub\n"); }
4386#endif
4387};
4388
4389
4390void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
4391#ifndef BIG_ENDIAN_FLOATING_POINT
4392 Register exponent = result1_;
4393 Register mantissa = result2_;
4394#else
4395 Register exponent = result2_;
4396 Register mantissa = result1_;
4397#endif
4398 Label not_special;
4399 // Convert from Smi to integer.
4400 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
4401 // Move sign bit from source to destination. This works because the sign bit
4402 // in the exponent word of the double has the same position and polarity as
4403 // the 2's complement sign bit in a Smi.
4404 ASSERT(HeapNumber::kSignMask == 0x80000000u);
4405 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
4406 // Subtract from 0 if source was negative.
4407 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
4408 __ cmp(source_, Operand(1));
4409 __ b(gt, &not_special);
4410
4411 // We have -1, 0 or 1, which we treat specially.
4412 __ cmp(source_, Operand(0));
4413 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
4414 static const uint32_t exponent_word_for_1 =
4415 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
4416 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne);
4417 // 1, 0 and -1 all have 0 for the second word.
4418 __ mov(mantissa, Operand(0));
4419 __ Ret();
4420
4421 __ bind(&not_special);
4422 // Count leading zeros. Uses result2 for a scratch register on pre-ARM5.
4423 // Gets the wrong answer for 0, but we already checked for that case above.
4424 CountLeadingZeros(masm, source_, mantissa, zeros_);
4425 // Compute exponent and or it into the exponent register.
4426 // We use result2 as a scratch register here.
4427 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
4428 __ orr(exponent,
4429 exponent,
4430 Operand(mantissa, LSL, HeapNumber::kExponentShift));
4431 // Shift up the source chopping the top bit off.
4432 __ add(zeros_, zeros_, Operand(1));
4433 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
4434 __ mov(source_, Operand(source_, LSL, zeros_));
4435 // Compute lower part of fraction (last 12 bits).
4436 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
4437 // And the top (top 20 bits).
4438 __ orr(exponent,
4439 exponent,
4440 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
4441 __ Ret();
4442}
4443
4444
4445// This stub can convert a signed int32 to a heap number (double). It does
4446// not work for int32s that are in Smi range! No GC occurs during this stub
4447// so you don't have to set up the frame.
4448class WriteInt32ToHeapNumberStub : public CodeStub {
4449 public:
4450 WriteInt32ToHeapNumberStub(Register the_int,
4451 Register the_heap_number,
4452 Register scratch)
4453 : the_int_(the_int),
4454 the_heap_number_(the_heap_number),
4455 scratch_(scratch) { }
4456
4457 private:
4458 Register the_int_;
4459 Register the_heap_number_;
4460 Register scratch_;
4461
4462 // Minor key encoding in 16 bits.
4463 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4464 class OpBits: public BitField<Token::Value, 2, 14> {};
4465
4466 Major MajorKey() { return WriteInt32ToHeapNumber; }
4467 int MinorKey() {
4468 // Encode the parameters in a unique 16 bit value.
4469 return the_int_.code() +
4470 (the_heap_number_.code() << 4) +
4471 (scratch_.code() << 8);
4472 }
4473
4474 void Generate(MacroAssembler* masm);
4475
4476 const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
4477
4478#ifdef DEBUG
4479 void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
4480#endif
4481};
4482
4483
4484// See comment for class.
4485void WriteInt32ToHeapNumberStub::Generate(MacroAssembler *masm) {
4486 Label max_negative_int;
4487 // the_int_ has the answer which is a signed int32 but not a Smi.
4488 // We test for the special value that has a different exponent. This test
4489 // has the neat side effect of setting the flags according to the sign.
4490 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004491 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004492 __ b(eq, &max_negative_int);
4493 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
4494 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
4495 uint32_t non_smi_exponent =
4496 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
4497 __ mov(scratch_, Operand(non_smi_exponent));
4498 // Set the sign bit in scratch_ if the value was negative.
4499 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
4500 // Subtract from 0 if the value was negative.
4501 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
4502 // We should be masking the implict first digit of the mantissa away here,
4503 // but it just ends up combining harmlessly with the last digit of the
4504 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
4505 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
4506 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
4507 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
4508 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
4509 __ str(scratch_, FieldMemOperand(the_heap_number_,
4510 HeapNumber::kExponentOffset));
4511 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
4512 __ str(scratch_, FieldMemOperand(the_heap_number_,
4513 HeapNumber::kMantissaOffset));
4514 __ Ret();
4515
4516 __ bind(&max_negative_int);
4517 // The max negative int32 is stored as a positive number in the mantissa of
4518 // a double because it uses a sign bit instead of using two's complement.
4519 // The actual mantissa bits stored are all 0 because the implicit most
4520 // significant 1 bit is not stored.
4521 non_smi_exponent += 1 << HeapNumber::kExponentShift;
4522 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
4523 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
4524 __ mov(ip, Operand(0));
4525 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
4526 __ Ret();
4527}
4528
4529
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004530// Handle the case where the lhs and rhs are the same object.
4531// Equality is almost reflexive (everything but NaN), so this is a test
4532// for "identity and not NaN".
4533static void EmitIdenticalObjectComparison(MacroAssembler* masm,
4534 Label* slow,
4535 Condition cc) {
4536 Label not_identical;
4537 __ cmp(r0, Operand(r1));
4538 __ b(ne, &not_identical);
4539
4540 Register exp_mask_reg = r5;
4541 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4542
4543 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
4544 // so we do the second best thing - test it ourselves.
4545 Label heap_number, return_equal;
4546 // They are both equal and they are not both Smis so both of them are not
4547 // Smis. If it's not a heap number, then return equal.
4548 if (cc == lt || cc == gt) {
4549 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
4550 __ b(ge, slow);
4551 } else {
4552 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4553 __ b(eq, &heap_number);
4554 // Comparing JS objects with <=, >= is complicated.
4555 if (cc != eq) {
4556 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
4557 __ b(ge, slow);
4558 }
4559 }
4560 __ bind(&return_equal);
4561 if (cc == lt) {
4562 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
4563 } else if (cc == gt) {
4564 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
4565 } else {
4566 __ mov(r0, Operand(0)); // Things are <=, >=, ==, === themselves.
4567 }
4568 __ mov(pc, Operand(lr)); // Return.
4569
4570 // For less and greater we don't have to check for NaN since the result of
4571 // x < x is false regardless. For the others here is some code to check
4572 // for NaN.
4573 if (cc != lt && cc != gt) {
4574 __ bind(&heap_number);
4575 // It is a heap number, so return non-equal if it's NaN and equal if it's
4576 // not NaN.
4577 // The representation of NaN values has all exponent bits (52..62) set,
4578 // and not all mantissa bits (0..51) clear.
4579 // Read top bits of double representation (second word of value).
4580 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
4581 // Test that exponent bits are all set.
4582 __ and_(r3, r2, Operand(exp_mask_reg));
4583 __ cmp(r3, Operand(exp_mask_reg));
4584 __ b(ne, &return_equal);
4585
4586 // Shift out flag and all exponent bits, retaining only mantissa.
4587 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
4588 // Or with all low-bits of mantissa.
4589 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
4590 __ orr(r0, r3, Operand(r2), SetCC);
4591 // For equal we already have the right value in r0: Return zero (equal)
4592 // if all bits in mantissa are zero (it's an Infinity) and non-zero if not
4593 // (it's a NaN). For <= and >= we need to load r0 with the failing value
4594 // if it's a NaN.
4595 if (cc != eq) {
4596 // All-zero means Infinity means equal.
4597 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
4598 if (cc == le) {
4599 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
4600 } else {
4601 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
4602 }
4603 }
4604 __ mov(pc, Operand(lr)); // Return.
4605 }
4606 // No fall through here.
4607
4608 __ bind(&not_identical);
4609}
4610
4611
4612// See comment at call site.
4613static void EmitSmiNonsmiComparison(MacroAssembler* masm,
4614 Label* rhs_not_nan,
4615 Label* slow,
4616 bool strict) {
4617 Label lhs_is_smi;
4618 __ tst(r0, Operand(kSmiTagMask));
4619 __ b(eq, &lhs_is_smi);
4620
4621 // Rhs is a Smi. Check whether the non-smi is a heap number.
4622 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4623 if (strict) {
4624 // If lhs was not a number and rhs was a Smi then strict equality cannot
4625 // succeed. Return non-equal (r0 is already not zero)
4626 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4627 } else {
4628 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4629 // the runtime.
4630 __ b(ne, slow);
4631 }
4632
4633 // Rhs is a smi, lhs is a number.
4634 __ push(lr);
4635 __ mov(r7, Operand(r1));
4636 ConvertToDoubleStub stub1(r3, r2, r7, r6);
4637 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
4638 // r3 and r2 are rhs as double.
4639 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4640 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4641 // We now have both loaded as doubles but we can skip the lhs nan check
4642 // since it's a Smi.
4643 __ pop(lr);
4644 __ jmp(rhs_not_nan);
4645
4646 __ bind(&lhs_is_smi);
4647 // Lhs is a Smi. Check whether the non-smi is a heap number.
4648 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
4649 if (strict) {
4650 // If lhs was not a number and rhs was a Smi then strict equality cannot
4651 // succeed. Return non-equal.
4652 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
4653 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4654 } else {
4655 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4656 // the runtime.
4657 __ b(ne, slow);
4658 }
4659
4660 // Lhs is a smi, rhs is a number.
4661 // r0 is Smi and r1 is heap number.
4662 __ push(lr);
4663 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
4664 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4665 __ mov(r7, Operand(r0));
4666 ConvertToDoubleStub stub2(r1, r0, r7, r6);
4667 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
4668 __ pop(lr);
4669 // Fall through to both_loaded_as_doubles.
4670}
4671
4672
4673void EmitNanCheck(MacroAssembler* masm, Label* rhs_not_nan, Condition cc) {
4674 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
4675 Register lhs_exponent = exp_first ? r0 : r1;
4676 Register rhs_exponent = exp_first ? r2 : r3;
4677 Register lhs_mantissa = exp_first ? r1 : r0;
4678 Register rhs_mantissa = exp_first ? r3 : r2;
4679 Label one_is_nan, neither_is_nan;
4680
4681 Register exp_mask_reg = r5;
4682
4683 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4684 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
4685 __ cmp(r4, Operand(exp_mask_reg));
4686 __ b(ne, rhs_not_nan);
4687 __ mov(r4,
4688 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4689 SetCC);
4690 __ b(ne, &one_is_nan);
4691 __ cmp(rhs_mantissa, Operand(0));
4692 __ b(ne, &one_is_nan);
4693
4694 __ bind(rhs_not_nan);
4695 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4696 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
4697 __ cmp(r4, Operand(exp_mask_reg));
4698 __ b(ne, &neither_is_nan);
4699 __ mov(r4,
4700 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4701 SetCC);
4702 __ b(ne, &one_is_nan);
4703 __ cmp(lhs_mantissa, Operand(0));
4704 __ b(eq, &neither_is_nan);
4705
4706 __ bind(&one_is_nan);
4707 // NaN comparisons always fail.
4708 // Load whatever we need in r0 to make the comparison fail.
4709 if (cc == lt || cc == le) {
4710 __ mov(r0, Operand(GREATER));
4711 } else {
4712 __ mov(r0, Operand(LESS));
4713 }
4714 __ mov(pc, Operand(lr)); // Return.
4715
4716 __ bind(&neither_is_nan);
4717}
4718
4719
4720// See comment at call site.
4721static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
4722 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
4723 Register lhs_exponent = exp_first ? r0 : r1;
4724 Register rhs_exponent = exp_first ? r2 : r3;
4725 Register lhs_mantissa = exp_first ? r1 : r0;
4726 Register rhs_mantissa = exp_first ? r3 : r2;
4727
4728 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
4729 if (cc == eq) {
4730 // Doubles are not equal unless they have the same bit pattern.
4731 // Exception: 0 and -0.
4732 __ cmp(lhs_mantissa, Operand(rhs_mantissa));
4733 __ orr(r0, lhs_mantissa, Operand(rhs_mantissa), LeaveCC, ne);
4734 // Return non-zero if the numbers are unequal.
4735 __ mov(pc, Operand(lr), LeaveCC, ne);
4736
4737 __ sub(r0, lhs_exponent, Operand(rhs_exponent), SetCC);
4738 // If exponents are equal then return 0.
4739 __ mov(pc, Operand(lr), LeaveCC, eq);
4740
4741 // Exponents are unequal. The only way we can return that the numbers
4742 // are equal is if one is -0 and the other is 0. We already dealt
4743 // with the case where both are -0 or both are 0.
4744 // We start by seeing if the mantissas (that are equal) or the bottom
4745 // 31 bits of the rhs exponent are non-zero. If so we return not
4746 // equal.
4747 __ orr(r4, rhs_mantissa, Operand(rhs_exponent, LSL, kSmiTagSize), SetCC);
4748 __ mov(r0, Operand(r4), LeaveCC, ne);
4749 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
4750 // Now they are equal if and only if the lhs exponent is zero in its
4751 // low 31 bits.
4752 __ mov(r0, Operand(lhs_exponent, LSL, kSmiTagSize));
4753 __ mov(pc, Operand(lr));
4754 } else {
4755 // Call a native function to do a comparison between two non-NaNs.
4756 // Call C routine that may not cause GC or other trouble.
4757 __ mov(r5, Operand(ExternalReference::compare_doubles()));
4758 __ Jump(r5); // Tail call.
4759 }
4760}
4761
4762
4763// See comment at call site.
4764static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
4765 // If either operand is a JSObject or an oddball value, then they are
4766 // not equal since their pointers are different.
4767 // There is no test for undetectability in strict equality.
4768 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4769 Label first_non_object;
4770 // Get the type of the first operand into r2 and compare it with
4771 // FIRST_JS_OBJECT_TYPE.
4772 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
4773 __ b(lt, &first_non_object);
4774
4775 // Return non-zero (r0 is not zero)
4776 Label return_not_equal;
4777 __ bind(&return_not_equal);
4778 __ mov(pc, Operand(lr)); // Return.
4779
4780 __ bind(&first_non_object);
4781 // Check for oddballs: true, false, null, undefined.
4782 __ cmp(r2, Operand(ODDBALL_TYPE));
4783 __ b(eq, &return_not_equal);
4784
4785 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
4786 __ b(ge, &return_not_equal);
4787
4788 // Check for oddballs: true, false, null, undefined.
4789 __ cmp(r3, Operand(ODDBALL_TYPE));
4790 __ b(eq, &return_not_equal);
4791}
4792
4793
4794// See comment at call site.
4795static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
4796 Label* both_loaded_as_doubles,
4797 Label* not_heap_numbers,
4798 Label* slow) {
4799 __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE);
4800 __ b(ne, not_heap_numbers);
4801 __ CompareObjectType(r1, r3, r3, HEAP_NUMBER_TYPE);
4802 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
4803
4804 // Both are heap numbers. Load them up then jump to the code we have
4805 // for that.
4806 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
4807 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4808 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4809 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4810 __ jmp(both_loaded_as_doubles);
4811}
4812
4813
4814// Fast negative check for symbol-to-symbol equality.
4815static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
4816 // r2 is object type of r0.
4817 __ tst(r2, Operand(kIsNotStringMask));
4818 __ b(ne, slow);
4819 __ tst(r2, Operand(kIsSymbolMask));
4820 __ b(eq, slow);
4821 __ CompareObjectType(r1, r3, r3, FIRST_NONSTRING_TYPE);
4822 __ b(ge, slow);
4823 __ tst(r3, Operand(kIsSymbolMask));
4824 __ b(eq, slow);
4825
4826 // Both are symbols. We already checked they weren't the same pointer
4827 // so they are not equal.
4828 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
4829 __ mov(pc, Operand(lr)); // Return.
4830}
4831
4832
4833// On entry r0 and r1 are the things to be compared. On exit r0 is 0,
4834// positive or negative to indicate the result of the comparison.
4835void CompareStub::Generate(MacroAssembler* masm) {
4836 Label slow; // Call builtin.
4837 Label not_smis, both_loaded_as_doubles, rhs_not_nan;
4838
4839 // NOTICE! This code is only reached after a smi-fast-case check, so
4840 // it is certain that at least one operand isn't a smi.
4841
4842 // Handle the case where the objects are identical. Either returns the answer
4843 // or goes to slow. Only falls through if the objects were not identical.
4844 EmitIdenticalObjectComparison(masm, &slow, cc_);
4845
4846 // If either is a Smi (we know that not both are), then they can only
4847 // be strictly equal if the other is a HeapNumber.
4848 ASSERT_EQ(0, kSmiTag);
4849 ASSERT_EQ(0, Smi::FromInt(0));
4850 __ and_(r2, r0, Operand(r1));
4851 __ tst(r2, Operand(kSmiTagMask));
4852 __ b(ne, &not_smis);
4853 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
4854 // 1) Return the answer.
4855 // 2) Go to slow.
4856 // 3) Fall through to both_loaded_as_doubles.
4857 // 4) Jump to rhs_not_nan.
4858 // In cases 3 and 4 we have found out we were dealing with a number-number
4859 // comparison and the numbers have been loaded into r0, r1, r2, r3 as doubles.
4860 EmitSmiNonsmiComparison(masm, &rhs_not_nan, &slow, strict_);
4861
4862 __ bind(&both_loaded_as_doubles);
4863 // r0, r1, r2, r3 are the double representations of the left hand side
4864 // and the right hand side.
4865
4866 // Checks for NaN in the doubles we have loaded. Can return the answer or
4867 // fall through if neither is a NaN. Also binds rhs_not_nan.
4868 EmitNanCheck(masm, &rhs_not_nan, cc_);
4869
4870 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
4871 // answer. Never falls through.
4872 EmitTwoNonNanDoubleComparison(masm, cc_);
4873
4874 __ bind(&not_smis);
4875 // At this point we know we are dealing with two different objects,
4876 // and neither of them is a Smi. The objects are in r0 and r1.
4877 if (strict_) {
4878 // This returns non-equal for some object types, or falls through if it
4879 // was not lucky.
4880 EmitStrictTwoHeapObjectCompare(masm);
4881 }
4882
4883 Label check_for_symbols;
4884 // Check for heap-number-heap-number comparison. Can jump to slow case,
4885 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
4886 // that case. If the inputs are not doubles then jumps to check_for_symbols.
4887 // In this case r2 will contain the type of r0.
4888 EmitCheckForTwoHeapNumbers(masm,
4889 &both_loaded_as_doubles,
4890 &check_for_symbols,
4891 &slow);
4892
4893 __ bind(&check_for_symbols);
4894 if (cc_ == eq) {
4895 // Either jumps to slow or returns the answer. Assumes that r2 is the type
4896 // of r0 on entry.
4897 EmitCheckForSymbols(masm, &slow);
4898 }
4899
4900 __ bind(&slow);
4901 __ push(lr);
4902 __ push(r1);
4903 __ push(r0);
4904 // Figure out which native to call and setup the arguments.
4905 Builtins::JavaScript native;
4906 int arg_count = 1; // Not counting receiver.
4907 if (cc_ == eq) {
4908 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
4909 } else {
4910 native = Builtins::COMPARE;
4911 int ncr; // NaN compare result
4912 if (cc_ == lt || cc_ == le) {
4913 ncr = GREATER;
4914 } else {
4915 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
4916 ncr = LESS;
4917 }
4918 arg_count++;
4919 __ mov(r0, Operand(Smi::FromInt(ncr)));
4920 __ push(r0);
4921 }
4922
4923 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
4924 // tagged as a small integer.
4925 __ mov(r0, Operand(arg_count));
4926 __ InvokeBuiltin(native, CALL_JS);
4927 __ cmp(r0, Operand(0));
4928 __ pop(pc);
4929}
4930
4931
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004932// Allocates a heap number or jumps to the label if the young space is full and
4933// a scavenge is needed.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004934static void AllocateHeapNumber(
4935 MacroAssembler* masm,
4936 Label* need_gc, // Jump here if young space is full.
4937 Register result_reg, // The tagged address of the new heap number.
4938 Register allocation_top_addr_reg, // A scratch register.
4939 Register scratch2) { // Another scratch register.
4940 ExternalReference allocation_top =
4941 ExternalReference::new_space_allocation_top_address();
4942 ExternalReference allocation_limit =
4943 ExternalReference::new_space_allocation_limit_address();
4944
4945 // allocat := the address of the allocation top variable.
4946 __ mov(allocation_top_addr_reg, Operand(allocation_top));
4947 // result_reg := the old allocation top.
4948 __ ldr(result_reg, MemOperand(allocation_top_addr_reg));
4949 // scratch2 := the address of the allocation limit.
4950 __ mov(scratch2, Operand(allocation_limit));
4951 // scratch2 := the allocation limit.
4952 __ ldr(scratch2, MemOperand(scratch2));
4953 // result_reg := the new allocation top.
4954 __ add(result_reg, result_reg, Operand(HeapNumber::kSize));
4955 // Compare new new allocation top and limit.
4956 __ cmp(result_reg, Operand(scratch2));
4957 // Branch if out of space in young generation.
4958 __ b(hi, need_gc);
4959 // Store new allocation top.
4960 __ str(result_reg, MemOperand(allocation_top_addr_reg)); // store new top
4961 // Tag and adjust back to start of new object.
4962 __ sub(result_reg, result_reg, Operand(HeapNumber::kSize - kHeapObjectTag));
4963 // Get heap number map into scratch2.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004964 __ LoadRoot(scratch2, Heap::kHeapNumberMapRootIndex);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00004965 // Store heap number map in new object.
4966 __ str(scratch2, FieldMemOperand(result_reg, HeapObject::kMapOffset));
4967}
4968
4969
4970// We fall into this code if the operands were Smis, but the result was
4971// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004972// the operands were not both Smi. The operands are in r0 and r1. In order
4973// to call the C-implemented binary fp operation routines we need to end up
4974// with the double precision floating point operands in r0 and r1 (for the
4975// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004976static void HandleBinaryOpSlowCases(MacroAssembler* masm,
4977 Label* not_smi,
4978 const Builtins::JavaScript& builtin,
4979 Token::Value operation,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004980 OverwriteMode mode) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004981 Label slow, slow_pop_2_first, do_the_call;
4982 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
4983 // Smi-smi case (overflow).
4984 // Since both are Smis there is no heap number to overwrite, so allocate.
4985 // The new heap number is in r5. r6 and r7 are scratch.
4986 AllocateHeapNumber(masm, &slow, r5, r6, r7);
4987 // Write Smi from r0 to r3 and r2 in double format. r6 is scratch.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004988 __ mov(r7, Operand(r0));
4989 ConvertToDoubleStub stub1(r3, r2, r7, r6);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004990 __ push(lr);
4991 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
4992 // Write Smi from r1 to r1 and r0 in double format. r6 is scratch.
4993 __ mov(r7, Operand(r1));
4994 ConvertToDoubleStub stub2(r1, r0, r7, r6);
4995 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
4996 __ pop(lr);
4997 __ jmp(&do_the_call); // Tail call. No return.
4998
4999 // We jump to here if something goes wrong (one param is not a number of any
5000 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005001 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005002 __ push(r1);
5003 __ push(r0);
5004 __ mov(r0, Operand(1)); // Set number of arguments.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005005 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005006
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005007 // We branch here if at least one of r0 and r1 is not a Smi.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005008 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005009 if (mode == NO_OVERWRITE) {
5010 // In the case where there is no chance of an overwritable float we may as
5011 // well do the allocation immediately while r0 and r1 are untouched.
5012 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5013 }
5014
5015 // Move r0 to a double in r2-r3.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005016 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005017 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5018 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005019 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005020 if (mode == OVERWRITE_RIGHT) {
5021 __ mov(r5, Operand(r0)); // Overwrite this heap number.
5022 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005023 // Calling convention says that second double is in r2 and r3.
5024 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005025 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
5026 __ jmp(&finished_loading_r0);
5027 __ bind(&r0_is_smi);
5028 if (mode == OVERWRITE_RIGHT) {
5029 // We can't overwrite a Smi so get address of new heap number into r5.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005030 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005031 }
5032 // Write Smi from r0 to r3 and r2 in double format.
5033 __ mov(r7, Operand(r0));
5034 ConvertToDoubleStub stub3(r3, r2, r7, r6);
5035 __ push(lr);
5036 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
5037 __ pop(lr);
5038 __ bind(&finished_loading_r0);
5039
5040 // Move r1 to a double in r0-r1.
5041 __ tst(r1, Operand(kSmiTagMask));
5042 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5043 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5044 __ b(ne, &slow);
5045 if (mode == OVERWRITE_LEFT) {
5046 __ mov(r5, Operand(r1)); // Overwrite this heap number.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005047 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005048 // Calling convention says that first double is in r0 and r1.
5049 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005050 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
5051 __ jmp(&finished_loading_r1);
5052 __ bind(&r1_is_smi);
5053 if (mode == OVERWRITE_LEFT) {
5054 // We can't overwrite a Smi so get address of new heap number into r5.
5055 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5056 }
5057 // Write Smi from r1 to r1 and r0 in double format.
5058 __ mov(r7, Operand(r1));
5059 ConvertToDoubleStub stub4(r1, r0, r7, r6);
5060 __ push(lr);
5061 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
5062 __ pop(lr);
5063 __ bind(&finished_loading_r1);
5064
5065 __ bind(&do_the_call);
5066 // r0: Left value (least significant part of mantissa).
5067 // r1: Left value (sign, exponent, top of mantissa).
5068 // r2: Right value (least significant part of mantissa).
5069 // r3: Right value (sign, exponent, top of mantissa).
5070 // r5: Address of heap number for result.
5071 __ push(lr); // For later.
5072 __ push(r5); // Address of heap number that is answer.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005073 // Call C routine that may not cause GC or other trouble.
5074 __ mov(r5, Operand(ExternalReference::double_fp_operation(operation)));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005075 __ Call(r5);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005076 // Store answer in the overwritable heap number.
5077 __ pop(r4);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005078#if !defined(USE_ARM_EABI)
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005079 // Double returned in fp coprocessor register 0 and 1, encoded as register
5080 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
5081 // substract the tag from r4.
5082 __ sub(r5, r4, Operand(kHeapObjectTag));
5083 __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset));
5084#else
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005085 // Double returned in registers 0 and 1.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005086 __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005087 __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005088#endif
5089 __ mov(r0, Operand(r4));
5090 // And we are done.
5091 __ pop(pc);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005092}
5093
5094
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005095// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005096// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005097// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
5098// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005099// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
5100// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005101static void GetInt32(MacroAssembler* masm,
5102 Register source,
5103 Register dest,
5104 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005105 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005106 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005107 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005108 // Get exponent word.
5109 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
5110 // Get exponent alone in scratch2.
5111 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005112 // Load dest with zero. We use this either for the final shift or
5113 // for the answer.
5114 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005115 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005116 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
5117 // the exponent that we are fastest at and also the highest exponent we can
5118 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005119 const uint32_t non_smi_exponent =
5120 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
5121 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005122 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
5123 __ b(eq, &right_exponent);
5124 // If the exponent is higher than that then go to slow case. This catches
5125 // numbers that don't fit in a signed int32, infinities and NaNs.
5126 __ b(gt, slow);
5127
5128 // We know the exponent is smaller than 30 (biased). If it is less than
5129 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
5130 // it rounds to zero.
5131 const uint32_t zero_exponent =
5132 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
5133 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
5134 // Dest already has a Smi zero.
5135 __ b(lt, &done);
5136 // We have a shifted exponent between 0 and 30 in scratch2.
5137 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
5138 // We now have the exponent in dest. Subtract from 30 to get
5139 // how much to shift down.
5140 __ rsb(dest, dest, Operand(30));
5141
5142 __ bind(&right_exponent);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005143 // Get the top bits of the mantissa.
5144 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
5145 // Put back the implicit 1.
5146 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
5147 // Shift up the mantissa bits to take up the space the exponent used to take.
5148 // We just orred in the implicit bit so that took care of one and we want to
5149 // leave the sign bit 0 so we subtract 2 bits from the shift distance.
5150 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
5151 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
5152 // Put sign in zero flag.
5153 __ tst(scratch, Operand(HeapNumber::kSignMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005154 // Get the second half of the double. For some exponents we don't actually
5155 // need this because the bits get shifted out again, but it's probably slower
5156 // to test than just to do it.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005157 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
5158 // Shift down 22 bits to get the last 10 bits.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005159 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
5160 // Move down according to the exponent.
5161 __ mov(dest, Operand(scratch, LSR, dest));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005162 // Fix sign if sign bit was set.
5163 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005164 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005165}
5166
5167
5168// For bitwise ops where the inputs are not both Smis we here try to determine
5169// whether both inputs are either Smis or at least heap numbers that can be
5170// represented by a 32 bit signed value. We truncate towards zero as required
5171// by the ES spec. If this is the case we do the bitwise op and see if the
5172// result is a Smi. If so, great, otherwise we try to find a heap number to
5173// write the answer into (either by allocating or by overwriting).
5174// On entry the operands are in r0 and r1. On exit the answer is in r0.
5175void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) {
5176 Label slow, result_not_a_smi;
5177 Label r0_is_smi, r1_is_smi;
5178 Label done_checking_r0, done_checking_r1;
5179
5180 __ tst(r1, Operand(kSmiTagMask));
5181 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5182 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5183 __ b(ne, &slow);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005184 GetInt32(masm, r1, r3, r4, r5, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005185 __ jmp(&done_checking_r1);
5186 __ bind(&r1_is_smi);
5187 __ mov(r3, Operand(r1, ASR, 1));
5188 __ bind(&done_checking_r1);
5189
5190 __ tst(r0, Operand(kSmiTagMask));
5191 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5192 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5193 __ b(ne, &slow);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005194 GetInt32(masm, r0, r2, r4, r5, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005195 __ jmp(&done_checking_r0);
5196 __ bind(&r0_is_smi);
5197 __ mov(r2, Operand(r0, ASR, 1));
5198 __ bind(&done_checking_r0);
5199
5200 // r0 and r1: Original operands (Smi or heap numbers).
5201 // r2 and r3: Signed int32 operands.
5202 switch (op_) {
5203 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
5204 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
5205 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
5206 case Token::SAR:
5207 // Use only the 5 least significant bits of the shift count.
5208 __ and_(r2, r2, Operand(0x1f));
5209 __ mov(r2, Operand(r3, ASR, r2));
5210 break;
5211 case Token::SHR:
5212 // Use only the 5 least significant bits of the shift count.
5213 __ and_(r2, r2, Operand(0x1f));
5214 __ mov(r2, Operand(r3, LSR, r2), SetCC);
5215 // SHR is special because it is required to produce a positive answer.
5216 // The code below for writing into heap numbers isn't capable of writing
5217 // the register as an unsigned int so we go to slow case if we hit this
5218 // case.
5219 __ b(mi, &slow);
5220 break;
5221 case Token::SHL:
5222 // Use only the 5 least significant bits of the shift count.
5223 __ and_(r2, r2, Operand(0x1f));
5224 __ mov(r2, Operand(r3, LSL, r2));
5225 break;
5226 default: UNREACHABLE();
5227 }
5228 // check that the *signed* result fits in a smi
5229 __ add(r3, r2, Operand(0x40000000), SetCC);
5230 __ b(mi, &result_not_a_smi);
5231 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5232 __ Ret();
5233
5234 Label have_to_allocate, got_a_heap_number;
5235 __ bind(&result_not_a_smi);
5236 switch (mode_) {
5237 case OVERWRITE_RIGHT: {
5238 __ tst(r0, Operand(kSmiTagMask));
5239 __ b(eq, &have_to_allocate);
5240 __ mov(r5, Operand(r0));
5241 break;
5242 }
5243 case OVERWRITE_LEFT: {
5244 __ tst(r1, Operand(kSmiTagMask));
5245 __ b(eq, &have_to_allocate);
5246 __ mov(r5, Operand(r1));
5247 break;
5248 }
5249 case NO_OVERWRITE: {
5250 // Get a new heap number in r5. r6 and r7 are scratch.
5251 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5252 }
5253 default: break;
5254 }
5255 __ bind(&got_a_heap_number);
5256 // r2: Answer as signed int32.
5257 // r5: Heap number to write answer into.
5258
5259 // Nothing can go wrong now, so move the heap number to r0, which is the
5260 // result.
5261 __ mov(r0, Operand(r5));
5262
5263 // Tail call that writes the int32 in r2 to the heap number in r0, using
5264 // r3 as scratch. r0 is preserved and returned.
5265 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
5266 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
5267
5268 if (mode_ != NO_OVERWRITE) {
5269 __ bind(&have_to_allocate);
5270 // Get a new heap number in r5. r6 and r7 are scratch.
5271 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5272 __ jmp(&got_a_heap_number);
5273 }
5274
5275 // If all else failed then we go to the runtime system.
5276 __ bind(&slow);
5277 __ push(r1); // restore stack
5278 __ push(r0);
5279 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
5280 switch (op_) {
5281 case Token::BIT_OR:
5282 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
5283 break;
5284 case Token::BIT_AND:
5285 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
5286 break;
5287 case Token::BIT_XOR:
5288 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
5289 break;
5290 case Token::SAR:
5291 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
5292 break;
5293 case Token::SHR:
5294 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
5295 break;
5296 case Token::SHL:
5297 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
5298 break;
5299 default:
5300 UNREACHABLE();
5301 }
5302}
5303
5304
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005305// Can we multiply by x with max two shifts and an add.
5306// This answers yes to all integers from 2 to 10.
5307static bool IsEasyToMultiplyBy(int x) {
5308 if (x < 2) return false; // Avoid special cases.
5309 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
5310 if (IsPowerOf2(x)) return true; // Simple shift.
5311 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
5312 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
5313 return false;
5314}
5315
5316
5317// Can multiply by anything that IsEasyToMultiplyBy returns true for.
5318// Source and destination may be the same register. This routine does
5319// not set carry and overflow the way a mul instruction would.
5320static void MultiplyByKnownInt(MacroAssembler* masm,
5321 Register source,
5322 Register destination,
5323 int known_int) {
5324 if (IsPowerOf2(known_int)) {
5325 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
5326 } else if (PopCountLessThanEqual2(known_int)) {
5327 int first_bit = BitPosition(known_int);
5328 int second_bit = BitPosition(known_int ^ (1 << first_bit));
5329 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
5330 if (first_bit != 0) {
5331 __ mov(destination, Operand(destination, LSL, first_bit));
5332 }
5333 } else {
5334 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
5335 int the_bit = BitPosition(known_int + 1);
5336 __ rsb(destination, source, Operand(source, LSL, the_bit));
5337 }
5338}
5339
5340
5341// This function (as opposed to MultiplyByKnownInt) takes the known int in a
5342// a register for the cases where it doesn't know a good trick, and may deliver
5343// a result that needs shifting.
5344static void MultiplyByKnownInt2(
5345 MacroAssembler* masm,
5346 Register result,
5347 Register source,
5348 Register known_int_register, // Smi tagged.
5349 int known_int,
5350 int* required_shift) { // Including Smi tag shift
5351 switch (known_int) {
5352 case 3:
5353 __ add(result, source, Operand(source, LSL, 1));
5354 *required_shift = 1;
5355 break;
5356 case 5:
5357 __ add(result, source, Operand(source, LSL, 2));
5358 *required_shift = 1;
5359 break;
5360 case 6:
5361 __ add(result, source, Operand(source, LSL, 1));
5362 *required_shift = 2;
5363 break;
5364 case 7:
5365 __ rsb(result, source, Operand(source, LSL, 3));
5366 *required_shift = 1;
5367 break;
5368 case 9:
5369 __ add(result, source, Operand(source, LSL, 3));
5370 *required_shift = 1;
5371 break;
5372 case 10:
5373 __ add(result, source, Operand(source, LSL, 2));
5374 *required_shift = 2;
5375 break;
5376 default:
5377 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
5378 __ mul(result, source, known_int_register);
5379 *required_shift = 0;
5380 }
5381}
5382
5383
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005384void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
5385 // r1 : x
5386 // r0 : y
5387 // result : r0
5388
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005389 // All ops need to know whether we are dealing with two Smis. Set up r2 to
5390 // tell us that.
5391 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
5392
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005393 switch (op_) {
5394 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005395 Label not_smi;
5396 // Fast path.
5397 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005398 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005399 __ b(ne, &not_smi);
5400 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
5401 // Return if no overflow.
5402 __ Ret(vc);
5403 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
5404
5405 HandleBinaryOpSlowCases(masm,
5406 &not_smi,
5407 Builtins::ADD,
5408 Token::ADD,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005409 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005410 break;
5411 }
5412
5413 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005414 Label not_smi;
5415 // Fast path.
5416 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005417 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005418 __ b(ne, &not_smi);
5419 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
5420 // Return if no overflow.
5421 __ Ret(vc);
5422 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
5423
5424 HandleBinaryOpSlowCases(masm,
5425 &not_smi,
5426 Builtins::SUB,
5427 Token::SUB,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005428 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005429 break;
5430 }
5431
5432 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005433 Label not_smi, slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005434 ASSERT(kSmiTag == 0); // adjust code below
5435 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005436 __ b(ne, &not_smi);
5437 // Remove tag from one operand (but keep sign), so that result is Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005438 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005439 // Do multiplication
5440 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1.
5441 // Go slow on overflows (overflow bit is not set).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005442 __ mov(ip, Operand(r3, ASR, 31));
5443 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
5444 __ b(ne, &slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005445 // Go slow on zero result to handle -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005446 __ tst(r3, Operand(r3));
5447 __ mov(r0, Operand(r3), LeaveCC, ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005448 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005449 // We need -0 if we were multiplying a negative number with 0 to get 0.
5450 // We know one of them was zero.
5451 __ add(r2, r0, Operand(r1), SetCC);
5452 __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl);
5453 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
5454 // Slow case. We fall through here if we multiplied a negative number
5455 // with 0, because that would mean we should produce -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005456 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005457
5458 HandleBinaryOpSlowCases(masm,
5459 &not_smi,
5460 Builtins::MUL,
5461 Token::MUL,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005462 mode_);
5463 break;
5464 }
5465
5466 case Token::DIV:
5467 case Token::MOD: {
5468 Label not_smi;
5469 if (specialized_on_rhs_) {
5470 Label smi_is_unsuitable;
5471 __ BranchOnNotSmi(r1, &not_smi);
5472 if (IsPowerOf2(constant_rhs_)) {
5473 if (op_ == Token::MOD) {
5474 __ and_(r0,
5475 r1,
5476 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
5477 SetCC);
5478 // We now have the answer, but if the input was negative we also
5479 // have the sign bit. Our work is done if the result is
5480 // positive or zero:
5481 __ Ret(pl);
5482 // A mod of a negative left hand side must return a negative number.
5483 // Unfortunately if the answer is 0 then we must return -0. And we
5484 // already optimistically trashed r0 so we may need to restore it.
5485 __ eor(r0, r0, Operand(0x80000000u), SetCC);
5486 // Next two instructions are conditional on the answer being -0.
5487 __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
5488 __ b(eq, &smi_is_unsuitable);
5489 // We need to subtract the dividend. Eg. -3 % 4 == -3.
5490 __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_)));
5491 } else {
5492 ASSERT(op_ == Token::DIV);
5493 __ tst(r1,
5494 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
5495 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
5496 int shift = 0;
5497 int d = constant_rhs_;
5498 while ((d & 1) == 0) {
5499 d >>= 1;
5500 shift++;
5501 }
5502 __ mov(r0, Operand(r1, LSR, shift));
5503 __ bic(r0, r0, Operand(kSmiTagMask));
5504 }
5505 } else {
5506 // Not a power of 2.
5507 __ tst(r1, Operand(0x80000000u));
5508 __ b(ne, &smi_is_unsuitable);
5509 // Find a fixed point reciprocal of the divisor so we can divide by
5510 // multiplying.
5511 double divisor = 1.0 / constant_rhs_;
5512 int shift = 32;
5513 double scale = 4294967296.0; // 1 << 32.
5514 uint32_t mul;
5515 // Maximise the precision of the fixed point reciprocal.
5516 while (true) {
5517 mul = static_cast<uint32_t>(scale * divisor);
5518 if (mul >= 0x7fffffff) break;
5519 scale *= 2.0;
5520 shift++;
5521 }
5522 mul++;
5523 __ mov(r2, Operand(mul));
5524 __ umull(r3, r2, r2, r1);
5525 __ mov(r2, Operand(r2, LSR, shift - 31));
5526 // r2 is r1 / rhs. r2 is not Smi tagged.
5527 // r0 is still the known rhs. r0 is Smi tagged.
5528 // r1 is still the unkown lhs. r1 is Smi tagged.
5529 int required_r4_shift = 0; // Including the Smi tag shift of 1.
5530 // r4 = r2 * r0.
5531 MultiplyByKnownInt2(masm,
5532 r4,
5533 r2,
5534 r0,
5535 constant_rhs_,
5536 &required_r4_shift);
5537 // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs).
5538 if (op_ == Token::DIV) {
5539 __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC);
5540 __ b(ne, &smi_is_unsuitable); // There was a remainder.
5541 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5542 } else {
5543 ASSERT(op_ == Token::MOD);
5544 __ sub(r0, r1, Operand(r4, LSL, required_r4_shift));
5545 }
5546 }
5547 __ Ret();
5548 __ bind(&smi_is_unsuitable);
5549 } else {
5550 __ jmp(&not_smi);
5551 }
5552 HandleBinaryOpSlowCases(masm,
5553 &not_smi,
5554 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV,
5555 op_,
5556 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005557 break;
5558 }
5559
5560 case Token::BIT_OR:
5561 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005562 case Token::BIT_XOR:
5563 case Token::SAR:
5564 case Token::SHR:
5565 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005566 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005567 ASSERT(kSmiTag == 0); // adjust code below
5568 __ tst(r2, Operand(kSmiTagMask));
5569 __ b(ne, &slow);
5570 switch (op_) {
5571 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
5572 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
5573 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005574 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005575 // Remove tags from right operand.
5576 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5577 // Use only the 5 least significant bits of the shift count.
5578 __ and_(r2, r2, Operand(0x1f));
5579 __ mov(r0, Operand(r1, ASR, r2));
5580 // Smi tag result.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005581 __ bic(r0, r0, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005582 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005583 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005584 // Remove tags from operands. We can't do this on a 31 bit number
5585 // because then the 0s get shifted into bit 30 instead of bit 31.
5586 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5587 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5588 // Use only the 5 least significant bits of the shift count.
5589 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005590 __ mov(r3, Operand(r3, LSR, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005591 // Unsigned shift is not allowed to produce a negative number, so
5592 // check the sign bit and the sign bit after Smi tagging.
5593 __ tst(r3, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005594 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005595 // Smi tag result.
5596 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005597 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005598 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005599 // Remove tags from operands.
5600 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5601 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5602 // Use only the 5 least significant bits of the shift count.
5603 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005604 __ mov(r3, Operand(r3, LSL, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005605 // Check that the signed result fits in a Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005606 __ add(r2, r3, Operand(0x40000000), SetCC);
5607 __ b(mi, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005608 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005609 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005610 default: UNREACHABLE();
5611 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005612 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005613 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005614 HandleNonSmiBitwiseOp(masm);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005615 break;
5616 }
5617
5618 default: UNREACHABLE();
5619 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005620 // This code should be unreachable.
5621 __ stop("Unreachable");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005622}
5623
5624
5625void StackCheckStub::Generate(MacroAssembler* masm) {
5626 Label within_limit;
5627 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
5628 __ ldr(ip, MemOperand(ip));
5629 __ cmp(sp, Operand(ip));
5630 __ b(hs, &within_limit);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00005631 // Do tail-call to runtime routine. Runtime routines expect at least one
5632 // argument, so give it a Smi.
5633 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005634 __ push(r0);
5635 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
5636 __ bind(&within_limit);
5637
5638 __ StubReturn(1);
5639}
5640
5641
5642void UnarySubStub::Generate(MacroAssembler* masm) {
5643 Label undo;
5644 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005645 Label not_smi;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005646
5647 // Enter runtime system if the value is not a smi.
5648 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005649 __ b(ne, &not_smi);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005650
5651 // Enter runtime system if the value of the expression is zero
5652 // to make sure that we switch between 0 and -0.
5653 __ cmp(r0, Operand(0));
5654 __ b(eq, &slow);
5655
5656 // The value of the expression is a smi that is not zero. Try
5657 // optimistic subtraction '0 - value'.
5658 __ rsb(r1, r0, Operand(0), SetCC);
5659 __ b(vs, &slow);
5660
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005661 __ mov(r0, Operand(r1)); // Set r0 to result.
5662 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005663
5664 // Enter runtime system.
5665 __ bind(&slow);
5666 __ push(r0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005667 __ mov(r0, Operand(0)); // Set number of arguments.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005668 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
5669
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005670 __ bind(&not_smi);
5671 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
5672 __ b(ne, &slow);
5673 // r0 is a heap number. Get a new heap number in r1.
5674 if (overwrite_) {
5675 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5676 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
5677 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5678 } else {
5679 AllocateHeapNumber(masm, &slow, r1, r2, r3);
5680 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
5681 __ str(r2, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
5682 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5683 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
5684 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
5685 __ mov(r0, Operand(r1));
5686 }
5687 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005688}
5689
5690
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005691void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005692 // r0 holds the exception.
5693
5694 // Adjust this code if not the case.
5695 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
5696
5697 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005698 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
5699 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005700
5701 // Restore the next handler and frame pointer, discard handler state.
5702 ASSERT(StackHandlerConstants::kNextOffset == 0);
5703 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005704 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005705 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
5706 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
5707
5708 // Before returning we restore the context from the frame pointer if
5709 // not NULL. The frame pointer is NULL in the exception handler of a
5710 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005711 __ cmp(fp, Operand(0));
5712 // Set cp to NULL if fp is NULL.
5713 __ mov(cp, Operand(0), LeaveCC, eq);
5714 // Restore cp otherwise.
5715 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005716#ifdef DEBUG
5717 if (FLAG_debug_code) {
5718 __ mov(lr, Operand(pc));
5719 }
5720#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005721 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005722 __ pop(pc);
5723}
5724
5725
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005726void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
5727 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005728 // Adjust this code if not the case.
5729 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
5730
5731 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005732 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005733 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005734
5735 // Unwind the handlers until the ENTRY handler is found.
5736 Label loop, done;
5737 __ bind(&loop);
5738 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005739 const int kStateOffset = StackHandlerConstants::kStateOffset;
5740 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005741 __ cmp(r2, Operand(StackHandler::ENTRY));
5742 __ b(eq, &done);
5743 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005744 const int kNextOffset = StackHandlerConstants::kNextOffset;
5745 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005746 __ jmp(&loop);
5747 __ bind(&done);
5748
5749 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005750 ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005751 __ pop(r2);
5752 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005753
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005754 if (type == OUT_OF_MEMORY) {
5755 // Set external caught exception to false.
5756 ExternalReference external_caught(Top::k_external_caught_exception_address);
5757 __ mov(r0, Operand(false));
5758 __ mov(r2, Operand(external_caught));
5759 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005760
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005761 // Set pending exception and r0 to out of memory exception.
5762 Failure* out_of_memory = Failure::OutOfMemoryException();
5763 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
5764 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
5765 __ str(r0, MemOperand(r2));
5766 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005767
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005768 // Stack layout at this point. See also StackHandlerConstants.
5769 // sp -> state (ENTRY)
5770 // fp
5771 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005772
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005773 // Discard handler state (r2 is not used) and restore frame pointer.
5774 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
5775 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
5776 // Before returning we restore the context from the frame pointer if
5777 // not NULL. The frame pointer is NULL in the exception handler of a
5778 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005779 __ cmp(fp, Operand(0));
5780 // Set cp to NULL if fp is NULL.
5781 __ mov(cp, Operand(0), LeaveCC, eq);
5782 // Restore cp otherwise.
5783 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005784#ifdef DEBUG
5785 if (FLAG_debug_code) {
5786 __ mov(lr, Operand(pc));
5787 }
5788#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005789 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005790 __ pop(pc);
5791}
5792
5793
5794void CEntryStub::GenerateCore(MacroAssembler* masm,
5795 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005796 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005797 Label* throw_out_of_memory_exception,
5798 StackFrame::Type frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005799 bool do_gc,
5800 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005801 // r0: result parameter for PerformGC, if any
5802 // r4: number of arguments including receiver (C callee-saved)
5803 // r5: pointer to builtin function (C callee-saved)
5804 // r6: pointer to the first argument (C callee-saved)
5805
5806 if (do_gc) {
5807 // Passing r0.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005808 ExternalReference gc_reference = ExternalReference::perform_gc_function();
5809 __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005810 }
5811
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005812 ExternalReference scope_depth =
5813 ExternalReference::heap_always_allocate_scope_depth();
5814 if (always_allocate) {
5815 __ mov(r0, Operand(scope_depth));
5816 __ ldr(r1, MemOperand(r0));
5817 __ add(r1, r1, Operand(1));
5818 __ str(r1, MemOperand(r0));
5819 }
5820
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005821 // Call C built-in.
5822 // r0 = argc, r1 = argv
5823 __ mov(r0, Operand(r4));
5824 __ mov(r1, Operand(r6));
5825
5826 // TODO(1242173): To let the GC traverse the return address of the exit
5827 // frames, we need to know where the return address is. Right now,
5828 // we push it on the stack to be able to find it again, but we never
5829 // restore from it in case of changes, which makes it impossible to
5830 // support moving the C entry code stub. This should be fixed, but currently
5831 // this is OK because the CEntryStub gets generated so early in the V8 boot
5832 // sequence that it is not moving ever.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005833 masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
5834 masm->push(lr);
5835 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005836
5837 if (always_allocate) {
5838 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
5839 // though (contain the result).
5840 __ mov(r2, Operand(scope_depth));
5841 __ ldr(r3, MemOperand(r2));
5842 __ sub(r3, r3, Operand(1));
5843 __ str(r3, MemOperand(r2));
5844 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005845
5846 // check for failure result
5847 Label failure_returned;
5848 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
5849 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
5850 __ add(r2, r0, Operand(1));
5851 __ tst(r2, Operand(kFailureTagMask));
5852 __ b(eq, &failure_returned);
5853
5854 // Exit C frame and return.
5855 // r0:r1: result
5856 // sp: stack pointer
5857 // fp: frame pointer
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005858 __ LeaveExitFrame(frame_type);
5859
5860 // check if we should retry or throw exception
5861 Label retry;
5862 __ bind(&failure_returned);
5863 ASSERT(Failure::RETRY_AFTER_GC == 0);
5864 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
5865 __ b(eq, &retry);
5866
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005867 // Special handling of out of memory exceptions.
5868 Failure* out_of_memory = Failure::OutOfMemoryException();
5869 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
5870 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005871
5872 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00005873 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005874 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00005875 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005876 __ ldr(r0, MemOperand(ip));
5877 __ str(r3, MemOperand(ip));
5878
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005879 // Special handling of termination exceptions which are uncatchable
5880 // by javascript code.
5881 __ cmp(r0, Operand(Factory::termination_exception()));
5882 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005883
5884 // Handle normal exception.
5885 __ jmp(throw_normal_exception);
5886
5887 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
5888}
5889
5890
5891void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
5892 // Called from JavaScript; parameters are on stack as if calling JS function
5893 // r0: number of arguments including receiver
5894 // r1: pointer to builtin function
5895 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005896 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005897 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005898
5899 // NOTE: Invocations of builtins may return failure objects
5900 // instead of a proper result. The builtin entry handles
5901 // this by performing a garbage collection and retrying the
5902 // builtin once.
5903
5904 StackFrame::Type frame_type = is_debug_break
5905 ? StackFrame::EXIT_DEBUG
5906 : StackFrame::EXIT;
5907
5908 // Enter the exit frame that transitions from JavaScript to C++.
5909 __ EnterExitFrame(frame_type);
5910
5911 // r4: number of arguments (C callee-saved)
5912 // r5: pointer to builtin function (C callee-saved)
5913 // r6: pointer to first argument (C callee-saved)
5914
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005915 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005916 Label throw_termination_exception;
5917 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005918
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00005919 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005920 GenerateCore(masm,
5921 &throw_normal_exception,
5922 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005923 &throw_out_of_memory_exception,
5924 frame_type,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00005925 false,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005926 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005927
5928 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005929 GenerateCore(masm,
5930 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005931 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005932 &throw_out_of_memory_exception,
5933 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005934 true,
5935 false);
5936
5937 // Do full GC and retry runtime call one final time.
5938 Failure* failure = Failure::InternalError();
5939 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
5940 GenerateCore(masm,
5941 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005942 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005943 &throw_out_of_memory_exception,
5944 frame_type,
5945 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005946 true);
5947
5948 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00005949 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
5950
5951 __ bind(&throw_termination_exception);
5952 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005953
5954 __ bind(&throw_normal_exception);
5955 GenerateThrowTOS(masm);
5956}
5957
5958
5959void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
5960 // r0: code entry
5961 // r1: function
5962 // r2: receiver
5963 // r3: argc
5964 // [sp+0]: argv
5965
5966 Label invoke, exit;
5967
5968 // Called from C, so do not pop argc and args on exit (preserve sp)
5969 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005970 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005971 __ stm(db_w, sp, kCalleeSaved | lr.bit());
5972
5973 // Get address of argv, see stm above.
5974 // r0: code entry
5975 // r1: function
5976 // r2: receiver
5977 // r3: argc
5978 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
5979 __ ldr(r4, MemOperand(r4)); // argv
5980
5981 // Push a frame with special values setup to mark it as an entry frame.
5982 // r0: code entry
5983 // r1: function
5984 // r2: receiver
5985 // r3: argc
5986 // r4: argv
5987 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
5988 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
5989 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
5990 __ mov(r6, Operand(Smi::FromInt(marker)));
5991 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
5992 __ ldr(r5, MemOperand(r5));
5993 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
5994
5995 // Setup frame pointer for the frame to be pushed.
5996 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
5997
5998 // Call a faked try-block that does the invoke.
5999 __ bl(&invoke);
6000
6001 // Caught exception: Store result (exception) in the pending
6002 // exception field in the JSEnv and return a failure sentinel.
6003 // Coming in here the fp will be invalid because the PushTryHandler below
6004 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00006005 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006006 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006007 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006008 __ b(&exit);
6009
6010 // Invoke: Link this frame into the handler chain.
6011 __ bind(&invoke);
6012 // Must preserve r0-r4, r5-r7 are available.
6013 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006014 // If an exception not caught by another handler occurs, this handler
6015 // returns control to the code after the bl(&invoke) above, which
6016 // restores all kCalleeSaved registers (including cp and fp) to their
6017 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006018
6019 // Clear any pending exceptions.
6020 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
6021 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00006022 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006023 __ str(r5, MemOperand(ip));
6024
6025 // Invoke the function by calling through JS entry trampoline builtin.
6026 // Notice that we cannot store a reference to the trampoline code directly in
6027 // this stub, because runtime stubs are not traversed when doing GC.
6028
6029 // Expected registers by Builtins::JSEntryTrampoline
6030 // r0: code entry
6031 // r1: function
6032 // r2: receiver
6033 // r3: argc
6034 // r4: argv
6035 if (is_construct) {
6036 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
6037 __ mov(ip, Operand(construct_entry));
6038 } else {
6039 ExternalReference entry(Builtins::JSEntryTrampoline);
6040 __ mov(ip, Operand(entry));
6041 }
6042 __ ldr(ip, MemOperand(ip)); // deref address
6043
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006044 // Branch and link to JSEntryTrampoline. We don't use the double underscore
6045 // macro for the add instruction because we don't want the coverage tool
6046 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006047 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006048 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006049
6050 // Unlink this frame from the handler chain. When reading the
6051 // address of the next handler, there is no need to use the address
6052 // displacement since the current stack pointer (sp) points directly
6053 // to the stack handler.
6054 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
6055 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
6056 __ str(r3, MemOperand(ip));
6057 // No need to restore registers
6058 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
6059
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006060
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006061 __ bind(&exit); // r0 holds result
6062 // Restore the top frame descriptors from the stack.
6063 __ pop(r3);
6064 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6065 __ str(r3, MemOperand(ip));
6066
6067 // Reset the stack to the callee saved registers.
6068 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6069
6070 // Restore callee-saved registers and return.
6071#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006072 if (FLAG_debug_code) {
6073 __ mov(lr, Operand(pc));
6074 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006075#endif
6076 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
6077}
6078
6079
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006080// This stub performs an instanceof, calling the builtin function if
6081// necessary. Uses r1 for the object, r0 for the function that it may
6082// be an instance of (these are fetched from the stack).
6083void InstanceofStub::Generate(MacroAssembler* masm) {
6084 // Get the object - slow case for smis (we may need to throw an exception
6085 // depending on the rhs).
6086 Label slow, loop, is_instance, is_not_instance;
6087 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
6088 __ BranchOnSmi(r0, &slow);
6089
6090 // Check that the left hand is a JS object and put map in r3.
6091 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
6092 __ b(lt, &slow);
6093 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
6094 __ b(gt, &slow);
6095
6096 // Get the prototype of the function (r4 is result, r2 is scratch).
6097 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
6098 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
6099
6100 // Check that the function prototype is a JS object.
6101 __ BranchOnSmi(r4, &slow);
6102 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
6103 __ b(lt, &slow);
6104 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
6105 __ b(gt, &slow);
6106
6107 // Register mapping: r3 is object map and r4 is function prototype.
6108 // Get prototype of object into r2.
6109 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
6110
6111 // Loop through the prototype chain looking for the function prototype.
6112 __ bind(&loop);
6113 __ cmp(r2, Operand(r4));
6114 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006115 __ LoadRoot(ip, Heap::kNullValueRootIndex);
6116 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006117 __ b(eq, &is_not_instance);
6118 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
6119 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
6120 __ jmp(&loop);
6121
6122 __ bind(&is_instance);
6123 __ mov(r0, Operand(Smi::FromInt(0)));
6124 __ pop();
6125 __ pop();
6126 __ mov(pc, Operand(lr)); // Return.
6127
6128 __ bind(&is_not_instance);
6129 __ mov(r0, Operand(Smi::FromInt(1)));
6130 __ pop();
6131 __ pop();
6132 __ mov(pc, Operand(lr)); // Return.
6133
6134 // Slow-case. Tail call builtin.
6135 __ bind(&slow);
6136 __ mov(r0, Operand(1)); // Arg count without receiver.
6137 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
6138}
6139
6140
ager@chromium.org7c537e22008-10-16 08:43:32 +00006141void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006142 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006143 Label adaptor;
6144 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6145 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
6146 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006147 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006148
ager@chromium.org7c537e22008-10-16 08:43:32 +00006149 // Nothing to do: The formal number of parameters has already been
6150 // passed in register r0 by calling function. Just return it.
ager@chromium.org9085a012009-05-11 19:22:57 +00006151 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006152
ager@chromium.org7c537e22008-10-16 08:43:32 +00006153 // Arguments adaptor case: Read the arguments length from the
6154 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006155 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006156 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org9085a012009-05-11 19:22:57 +00006157 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006158}
6159
6160
ager@chromium.org7c537e22008-10-16 08:43:32 +00006161void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
6162 // The displacement is the offset of the last parameter (if any)
6163 // relative to the frame pointer.
6164 static const int kDisplacement =
6165 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006166
ager@chromium.org7c537e22008-10-16 08:43:32 +00006167 // Check that the key is a smi.
6168 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006169 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006170
ager@chromium.org7c537e22008-10-16 08:43:32 +00006171 // Check if the calling frame is an arguments adaptor frame.
6172 Label adaptor;
6173 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6174 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
6175 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
6176 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006177
ager@chromium.org7c537e22008-10-16 08:43:32 +00006178 // Check index against formal parameters count limit passed in
6179 // through register eax. Use unsigned comparison to get negative
6180 // check for free.
6181 __ cmp(r1, r0);
6182 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006183
ager@chromium.org7c537e22008-10-16 08:43:32 +00006184 // Read the argument from the stack and return it.
6185 __ sub(r3, r0, r1);
6186 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6187 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006188 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006189
6190 // Arguments adaptor case: Check index against actual arguments
6191 // limit found in the arguments adaptor frame. Use unsigned
6192 // comparison to get negative check for free.
6193 __ bind(&adaptor);
6194 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6195 __ cmp(r1, r0);
6196 __ b(cs, &slow);
6197
6198 // Read the argument from the adaptor frame and return it.
6199 __ sub(r3, r0, r1);
6200 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6201 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006202 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006203
6204 // Slow-case: Handle non-smi or out-of-bounds access to arguments
6205 // by calling the runtime system.
6206 __ bind(&slow);
6207 __ push(r1);
6208 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
6209}
6210
6211
6212void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
6213 // Check if the calling frame is an arguments adaptor frame.
6214 Label runtime;
6215 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6216 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
6217 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
6218 __ b(ne, &runtime);
6219
6220 // Patch the arguments.length and the parameters pointer.
6221 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6222 __ str(r0, MemOperand(sp, 0 * kPointerSize));
6223 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
6224 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
6225 __ str(r3, MemOperand(sp, 1 * kPointerSize));
6226
6227 // Do the runtime call to allocate the arguments object.
6228 __ bind(&runtime);
6229 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006230}
6231
6232
6233void CallFunctionStub::Generate(MacroAssembler* masm) {
6234 Label slow;
6235 // Get the function to call from the stack.
6236 // function, receiver [, arguments]
6237 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
6238
6239 // Check that the function is really a JavaScript function.
6240 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006241 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006242 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006243 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006244 __ b(ne, &slow);
6245
6246 // Fast-case: Invoke the function now.
6247 // r1: pushed function
6248 ParameterCount actual(argc_);
6249 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
6250
6251 // Slow-case: Non-function called.
6252 __ bind(&slow);
6253 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006254 __ mov(r2, Operand(0));
6255 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
6256 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
6257 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006258}
6259
6260
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006261int CompareStub::MinorKey() {
6262 // Encode the two parameters in a unique 16 bit value.
6263 ASSERT(static_cast<unsigned>(cc_) >> 28 < (1 << 15));
6264 return (static_cast<unsigned>(cc_) >> 27) | (strict_ ? 1 : 0);
6265}
6266
6267
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006268#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006269
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006270} } // namespace v8::internal