blob: a3176c211cacac25beb87fcb81eef445ae4f93c5 [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),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000136 function_return_is_shadowed_(false),
137 in_spilled_code_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138}
139
140
141// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000142// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000144// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145// cp: callee's context
146
ager@chromium.org7c537e22008-10-16 08:43:32 +0000147void CodeGenerator::GenCode(FunctionLiteral* fun) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148 ZoneList<Statement*>* body = fun->body();
149
150 // Initialize state.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000151 ASSERT(scope_ == NULL);
152 scope_ = fun->scope();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000153 ASSERT(allocator_ == NULL);
154 RegisterAllocator register_allocator(this);
155 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000156 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000157 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000158 cc_reg_ = al;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000159 set_in_spilled_code(false);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000160 {
161 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000162
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000163 // Entry:
164 // Stack: receiver, arguments
165 // lr: return address
166 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000168 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000170 allocator_->Initialize();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000171 frame_->Enter();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172 // tos: code slot
173#ifdef DEBUG
174 if (strlen(FLAG_stop_at) > 0 &&
175 fun->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000176 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000177 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000178 }
179#endif
180
181 // Allocate space for locals and initialize them.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000182 frame_->AllocateStackSlots();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000183 // Initialize the function return target after the locals are set
184 // up, because it needs the expected frame height from the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000185 function_return_.set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000186 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000187
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000188 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000189 if (scope_->num_heap_slots() > 0) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000190 // Allocate local context.
191 // Get outer context and create a new context based on it.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000192 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000193 frame_->EmitPush(r0);
194 frame_->CallRuntime(Runtime::kNewContext, 1); // r0 holds the result
kasper.lund7276f142008-07-30 08:49:36 +0000195
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000196#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000197 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000198 __ cmp(r0, Operand(cp));
199 verified_true.Branch(eq);
200 __ stop("NewContext: r0 is expected to be the same as cp");
201 verified_true.Bind();
202#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000204 __ str(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205 }
206
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000207 // TODO(1241774): Improve this code:
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208 // 1) only needed if we have a context
209 // 2) no need to recompute context ptr every single time
210 // 3) don't copy parameter operand code from SlotOperand!
211 {
212 Comment cmnt2(masm_, "[ copy context parameters into .context");
213
214 // Note that iteration order is relevant here! If we have the same
215 // parameter twice (e.g., function (x, y, x)), and that parameter
216 // needs to be copied into the context, it must be the last argument
217 // passed to the parameter that needs to be copied. This is a rare
218 // case so we don't check for it, instead we rely on the copying
219 // order: such a parameter is copied repeatedly into the same
220 // context location and thus the last value is what is seen inside
221 // the function.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000222 for (int i = 0; i < scope_->num_parameters(); i++) {
223 Variable* par = scope_->parameter(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 Slot* slot = par->slot();
225 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000226 ASSERT(!scope_->is_global_scope()); // no parameters in global scope
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000227 __ ldr(r1, frame_->ParameterAt(i));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000228 // Loads r2 with context; used below in RecordWrite.
229 __ str(r1, SlotOperand(slot, r2));
230 // Load the offset into r3.
231 int slot_offset =
232 FixedArray::kHeaderSize + slot->index() * kPointerSize;
233 __ mov(r3, Operand(slot_offset));
234 __ RecordWrite(r2, r3, r1);
235 }
236 }
237 }
238
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000239 // Store the arguments object. This must happen after context
240 // initialization because the arguments object may be stored in the
241 // context.
242 if (scope_->arguments() != NULL) {
243 ASSERT(scope_->arguments_shadow() != NULL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244 Comment cmnt(masm_, "[ allocate arguments object");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000245 { Reference shadow_ref(this, scope_->arguments_shadow());
246 { Reference arguments_ref(this, scope_->arguments());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000247 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000248 __ ldr(r2, frame_->Function());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000249 // The receiver is below the arguments, the return address,
250 // and the frame pointer on the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000251 const int kReceiverDisplacement = 2 + scope_->num_parameters();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000252 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000253 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000254 frame_->Adjust(3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000255 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000256 frame_->CallStub(&stub, 3);
257 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000258 arguments_ref.SetValue(NOT_CONST_INIT);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000259 }
ager@chromium.org7c537e22008-10-16 08:43:32 +0000260 shadow_ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000261 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000262 frame_->Drop(); // Value is no longer needed.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000263 }
264
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000265 // Generate code to 'execute' declarations and initialize functions
266 // (source elements). In case of an illegal redeclaration we need to
267 // handle that instead of processing the declarations.
268 if (scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000270 scope_->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271 } else {
272 Comment cmnt(masm_, "[ declarations");
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000273 ProcessDeclarations(scope_->declarations());
274 // Bail out if a stack-overflow exception occurred when processing
275 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000276 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000277 }
278
mads.s.ager31e71382008-08-13 09:32:07 +0000279 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000280 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000281 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000282 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283 CheckStack();
284
285 // Compile the body of the function in a vanilla state. Don't
286 // bother compiling all the code if the scope has an illegal
287 // redeclaration.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000288 if (!scope_->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289 Comment cmnt(masm_, "[ function body");
290#ifdef DEBUG
291 bool is_builtin = Bootstrapper::IsActive();
292 bool should_trace =
293 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000294 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000295 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000296 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000297 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298#endif
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000299 VisitStatementsAndSpill(body);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000301 }
302
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000303 // Generate the return sequence if necessary.
304 if (frame_ != NULL || function_return_.is_linked()) {
305 // exit
306 // r0: result
307 // sp: stack pointer
308 // fp: frame pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000309 // cp: callee's context
310 __ mov(r0, Operand(Factory::undefined_value()));
mads.s.ager31e71382008-08-13 09:32:07 +0000311
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000312 function_return_.Bind();
313 if (FLAG_trace) {
314 // Push the return value on the stack as the parameter.
315 // Runtime::TraceExit returns the parameter as it is.
316 frame_->EmitPush(r0);
317 frame_->CallRuntime(Runtime::kTraceExit, 1);
318 }
319
320 // Tear down the frame which will restore the caller's frame pointer and
321 // the link register.
322 frame_->Exit();
323
324 __ add(sp, sp, Operand((scope_->num_parameters() + 1) * kPointerSize));
ager@chromium.org9085a012009-05-11 19:22:57 +0000325 __ Jump(lr);
mads.s.ager31e71382008-08-13 09:32:07 +0000326 }
327
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329 ASSERT(!has_cc());
330 ASSERT(state_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000331 ASSERT(!function_return_is_shadowed_);
332 function_return_.Unuse();
333 DeleteFrame();
334
335 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000336 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000337 ProcessDeferred();
338 }
339
340 allocator_ = NULL;
341 scope_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000342}
343
344
ager@chromium.org7c537e22008-10-16 08:43:32 +0000345MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
346 // Currently, this assertion will fail if we try to assign to
347 // a constant variable that is constant because it is read-only
348 // (such as the variable referring to a named function expression).
349 // We need to implement assignments to read-only variables.
350 // Ideally, we should do this during AST generation (by converting
351 // such assignments into expression statements); however, in general
352 // we may not be able to make the decision until past AST generation,
353 // that is when the entire program is known.
354 ASSERT(slot != NULL);
355 int index = slot->index();
356 switch (slot->type()) {
357 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000358 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000359
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000360 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000361 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000362
363 case Slot::CONTEXT: {
364 // Follow the context chain if necessary.
365 ASSERT(!tmp.is(cp)); // do not overwrite context register
366 Register context = cp;
367 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000368 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000369 // Load the closure.
370 // (All contexts, even 'with' contexts, have a closure,
371 // and it is the same for all contexts inside a function.
372 // There is no need to go to the function context first.)
373 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
374 // Load the function context (which is the incoming, outer context).
375 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
376 context = tmp;
377 }
378 // We may have a 'with' context now. Get the function context.
379 // (In fact this mov may never be the needed, since the scope analysis
380 // may not permit a direct context access in this case and thus we are
381 // always at a function context. However it is safe to dereference be-
382 // cause the function context of a function context is itself. Before
383 // deleting this mov we should try to create a counter-example first,
384 // though...)
385 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
386 return ContextOperand(tmp, index);
387 }
388
389 default:
390 UNREACHABLE();
391 return MemOperand(r0, 0);
392 }
393}
394
395
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000396MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
397 Slot* slot,
398 Register tmp,
399 Register tmp2,
400 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000401 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000402 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000403
ager@chromium.org381abbb2009-02-25 13:23:22 +0000404 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
405 if (s->num_heap_slots() > 0) {
406 if (s->calls_eval()) {
407 // Check that extension is NULL.
408 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
409 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000410 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000411 }
412 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
413 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
414 context = tmp;
415 }
416 }
417 // Check that last extension is NULL.
418 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
419 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000420 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000421 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000422 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000423}
424
425
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000426void CodeGenerator::LoadConditionAndSpill(Expression* expression,
427 TypeofState typeof_state,
428 JumpTarget* true_target,
429 JumpTarget* false_target,
430 bool force_control) {
431 ASSERT(in_spilled_code());
432 set_in_spilled_code(false);
433 LoadCondition(expression, typeof_state, true_target, false_target,
434 force_control);
435 if (frame_ != NULL) {
436 frame_->SpillAll();
437 }
438 set_in_spilled_code(true);
439}
440
441
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000442// Loads a value on TOS. If it is a boolean value, the result may have been
443// (partially) translated into branches, or it may have set the condition
444// code register. If force_cc is set, the value is forced to set the
445// condition code register and no value is pushed. If the condition code
446// register was set, has_cc() is true and cc_reg_ contains the condition to
447// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000448void CodeGenerator::LoadCondition(Expression* x,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000449 TypeofState typeof_state,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000450 JumpTarget* true_target,
451 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000452 bool force_cc) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000453 ASSERT(!in_spilled_code());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000454 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000455 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000456
ager@chromium.org7c537e22008-10-16 08:43:32 +0000457 { CodeGenState new_state(this, typeof_state, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000458 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000459
460 // If we hit a stack overflow, we may not have actually visited
461 // the expression. In that case, we ensure that we have a
462 // valid-looking frame state because we will continue to generate
463 // code as we unwind the C++ stack.
464 //
465 // It's possible to have both a stack overflow and a valid frame
466 // state (eg, a subexpression overflowed, visiting it returned
467 // with a dummied frame state, and visiting this expression
468 // returned with a normal-looking state).
469 if (HasStackOverflow() &&
470 has_valid_frame() &&
471 !has_cc() &&
472 frame_->height() == original_height) {
473 true_target->Jump();
474 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000475 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000476 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000477 // Convert the TOS value to a boolean in the condition code register.
478 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000479 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000480 ASSERT(!force_cc || !has_valid_frame() || has_cc());
481 ASSERT(!has_valid_frame() ||
482 (has_cc() && frame_->height() == original_height) ||
483 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000484}
485
486
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000487void CodeGenerator::LoadAndSpill(Expression* expression,
488 TypeofState typeof_state) {
489 ASSERT(in_spilled_code());
490 set_in_spilled_code(false);
491 Load(expression, typeof_state);
492 frame_->SpillAll();
493 set_in_spilled_code(true);
494}
495
496
ager@chromium.org7c537e22008-10-16 08:43:32 +0000497void CodeGenerator::Load(Expression* x, TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000498#ifdef DEBUG
499 int original_height = frame_->height();
500#endif
501 ASSERT(!in_spilled_code());
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000502 JumpTarget true_target;
503 JumpTarget false_target;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000504 LoadCondition(x, typeof_state, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000505
506 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000507 // Convert cc_reg_ into a boolean value.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000508 JumpTarget loaded;
509 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000510 materialize_true.Branch(cc_reg_);
mads.s.ager31e71382008-08-13 09:32:07 +0000511 __ mov(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000512 frame_->EmitPush(r0);
513 loaded.Jump();
514 materialize_true.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000515 __ mov(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000516 frame_->EmitPush(r0);
517 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000518 cc_reg_ = al;
519 }
520
521 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000522 // We have at least one condition value that has been "translated"
523 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000524 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000525 if (frame_ != NULL) {
526 loaded.Jump(); // Don't lose the current TOS.
527 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000528 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000529 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000530 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000531 true_target.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000532 __ mov(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000533 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000534 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000535 // If both "true" and "false" need to be loaded jump across the code for
536 // "false".
537 if (both) {
538 loaded.Jump();
539 }
540 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000541 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000542 false_target.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +0000543 __ mov(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000544 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000545 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000546 // A value is loaded on all paths reaching this point.
547 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000548 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000549 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000550 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000551 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552}
553
554
ager@chromium.org7c537e22008-10-16 08:43:32 +0000555void CodeGenerator::LoadGlobal() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000556 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000557 __ ldr(r0, GlobalObject());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000558 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559}
560
561
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000562void CodeGenerator::LoadGlobalReceiver(Register scratch) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000563 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000564 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
565 __ ldr(scratch,
566 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000567 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000568}
569
570
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571// TODO(1241834): Get rid of this function in favor of just using Load, now
ager@chromium.org7c537e22008-10-16 08:43:32 +0000572// that we have the INSIDE_TYPEOF typeof state. => Need to handle global
573// variables w/o reference errors elsewhere.
574void CodeGenerator::LoadTypeofExpression(Expression* x) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000575 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000576 Variable* variable = x->AsVariableProxy()->AsVariable();
577 if (variable != NULL && !variable->is_this() && variable->is_global()) {
578 // NOTE: This is somewhat nasty. We force the compiler to load
579 // the variable as if through '<global>.<variable>' to make sure we
580 // do not get reference errors.
581 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
582 Literal key(variable->name());
583 // TODO(1241834): Fetch the position from the variable instead of using
584 // no position.
ager@chromium.org236ad962008-09-25 09:45:57 +0000585 Property property(&global, &key, RelocInfo::kNoPosition);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000586 LoadAndSpill(&property);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000587 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000588 LoadAndSpill(x, INSIDE_TYPEOF);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000589 }
590}
591
592
ager@chromium.org7c537e22008-10-16 08:43:32 +0000593Reference::Reference(CodeGenerator* cgen, Expression* expression)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594 : cgen_(cgen), expression_(expression), type_(ILLEGAL) {
595 cgen->LoadReference(this);
596}
597
598
599Reference::~Reference() {
600 cgen_->UnloadReference(this);
601}
602
603
ager@chromium.org7c537e22008-10-16 08:43:32 +0000604void CodeGenerator::LoadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000605 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000606 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000607 Expression* e = ref->expression();
608 Property* property = e->AsProperty();
609 Variable* var = e->AsVariableProxy()->AsVariable();
610
611 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000612 // The expression is either a property or a variable proxy that rewrites
613 // to a property.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000614 LoadAndSpill(property->obj());
ager@chromium.org7c537e22008-10-16 08:43:32 +0000615 // We use a named reference if the key is a literal symbol, unless it is
616 // a string that can be legally parsed as an integer. This is because
617 // otherwise we will not get into the slow case code that handles [] on
618 // String objects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000619 Literal* literal = property->key()->AsLiteral();
620 uint32_t dummy;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000621 if (literal != NULL &&
622 literal->handle()->IsSymbol() &&
623 !String::cast(*(literal->handle()))->AsArrayIndex(&dummy)) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000624 ref->set_type(Reference::NAMED);
625 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000626 LoadAndSpill(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627 ref->set_type(Reference::KEYED);
628 }
629 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000630 // The expression is a variable proxy that does not rewrite to a
631 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000632 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000633 LoadGlobal();
634 ref->set_type(Reference::NAMED);
635 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000636 ASSERT(var->slot() != NULL);
637 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000638 }
639 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000640 // Anything else is a runtime error.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000641 LoadAndSpill(e);
642 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000643 }
644}
645
646
ager@chromium.org7c537e22008-10-16 08:43:32 +0000647void CodeGenerator::UnloadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000648 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000649 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000650 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000651 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000652 if (size > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000653 frame_->EmitPop(r0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000654 frame_->Drop(size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000655 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656 }
657}
658
659
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000660// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
661// register to a boolean in the condition code register. The code
662// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000663void CodeGenerator::ToBoolean(JumpTarget* true_target,
664 JumpTarget* false_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000665 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000666 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000667 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000668 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000669
670 // Fast case checks
671
mads.s.ager31e71382008-08-13 09:32:07 +0000672 // Check if the value is 'false'.
673 __ cmp(r0, Operand(Factory::false_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000674 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000675
mads.s.ager31e71382008-08-13 09:32:07 +0000676 // Check if the value is 'true'.
677 __ cmp(r0, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000678 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000679
mads.s.ager31e71382008-08-13 09:32:07 +0000680 // Check if the value is 'undefined'.
681 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000682 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000683
mads.s.ager31e71382008-08-13 09:32:07 +0000684 // Check if the value is a smi.
685 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000686 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000687 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000688 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689
690 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000691 frame_->EmitPush(r0);
692 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000693 // Convert the result (r0) to a condition code.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000694 __ cmp(r0, Operand(Factory::false_value()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000695
696 cc_reg_ = ne;
697}
698
699
kasper.lund7276f142008-07-30 08:49:36 +0000700class GenericBinaryOpStub : public CodeStub {
701 public:
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000702 GenericBinaryOpStub(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000703 OverwriteMode mode,
704 int constant_rhs = CodeGenerator::kUnknownIntValue)
705 : op_(op),
706 mode_(mode),
707 constant_rhs_(constant_rhs),
708 specialized_on_rhs_(RhsIsOneWeWantToOptimizeFor(op, constant_rhs)) { }
kasper.lund7276f142008-07-30 08:49:36 +0000709
710 private:
711 Token::Value op_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000712 OverwriteMode mode_;
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000713 int constant_rhs_;
714 bool specialized_on_rhs_;
715
716 static const int kMaxKnownRhs = 0x40000000;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000717
718 // Minor key encoding in 16 bits.
719 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000720 class OpBits: public BitField<Token::Value, 2, 6> {};
721 class KnownIntBits: public BitField<int, 8, 8> {};
kasper.lund7276f142008-07-30 08:49:36 +0000722
723 Major MajorKey() { return GenericBinaryOp; }
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000724 int MinorKey() {
725 // Encode the parameters in a unique 16 bit value.
726 return OpBits::encode(op_)
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000727 | ModeBits::encode(mode_)
728 | KnownIntBits::encode(MinorKeyForKnownInt());
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000729 }
730
kasper.lund7276f142008-07-30 08:49:36 +0000731 void Generate(MacroAssembler* masm);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000732 void HandleNonSmiBitwiseOp(MacroAssembler* masm);
kasper.lund7276f142008-07-30 08:49:36 +0000733
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000734 static bool RhsIsOneWeWantToOptimizeFor(Token::Value op, int constant_rhs) {
735 if (constant_rhs == CodeGenerator::kUnknownIntValue) return false;
736 if (op == Token::DIV) return constant_rhs >= 2 && constant_rhs <= 3;
737 if (op == Token::MOD) {
738 if (constant_rhs <= 1) return false;
739 if (constant_rhs <= 10) return true;
740 if (constant_rhs <= kMaxKnownRhs && IsPowerOf2(constant_rhs)) return true;
741 return false;
742 }
743 return false;
744 }
745
746 int MinorKeyForKnownInt() {
747 if (!specialized_on_rhs_) return 0;
748 if (constant_rhs_ <= 10) return constant_rhs_ + 1;
749 ASSERT(IsPowerOf2(constant_rhs_));
750 int key = 12;
751 int d = constant_rhs_;
752 while ((d & 1) == 0) {
753 key++;
754 d >>= 1;
755 }
756 return key;
757 }
758
kasper.lund7276f142008-07-30 08:49:36 +0000759 const char* GetName() {
760 switch (op_) {
761 case Token::ADD: return "GenericBinaryOpStub_ADD";
762 case Token::SUB: return "GenericBinaryOpStub_SUB";
763 case Token::MUL: return "GenericBinaryOpStub_MUL";
764 case Token::DIV: return "GenericBinaryOpStub_DIV";
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000765 case Token::MOD: return "GenericBinaryOpStub_MOD";
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000766 case Token::BIT_OR: return "GenericBinaryOpStub_BIT_OR";
767 case Token::BIT_AND: return "GenericBinaryOpStub_BIT_AND";
768 case Token::BIT_XOR: return "GenericBinaryOpStub_BIT_XOR";
769 case Token::SAR: return "GenericBinaryOpStub_SAR";
770 case Token::SHL: return "GenericBinaryOpStub_SHL";
771 case Token::SHR: return "GenericBinaryOpStub_SHR";
kasper.lund7276f142008-07-30 08:49:36 +0000772 default: return "GenericBinaryOpStub";
773 }
774 }
775
776#ifdef DEBUG
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000777 void Print() {
778 if (!specialized_on_rhs_) {
779 PrintF("GenericBinaryOpStub (%s)\n", Token::String(op_));
780 } else {
781 PrintF("GenericBinaryOpStub (%s by %d)\n",
782 Token::String(op_),
783 constant_rhs_);
784 }
785 }
kasper.lund7276f142008-07-30 08:49:36 +0000786#endif
787};
788
789
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000790void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000791 OverwriteMode overwrite_mode,
792 int constant_rhs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000793 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000794 // sp[0] : y
795 // sp[1] : x
796 // result : r0
797
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000798 // Stub is entered with a call: 'return address' is in lr.
799 switch (op) {
800 case Token::ADD: // fall through.
801 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000802 case Token::MUL:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000803 case Token::DIV:
804 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000805 case Token::BIT_OR:
806 case Token::BIT_AND:
807 case Token::BIT_XOR:
808 case Token::SHL:
809 case Token::SHR:
810 case Token::SAR: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000811 frame_->EmitPop(r0); // r0 : y
812 frame_->EmitPop(r1); // r1 : x
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000813 GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000814 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815 break;
816 }
817
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000818 case Token::COMMA:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000819 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000820 // simply discard left value
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000821 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000822 break;
823
824 default:
825 // Other cases should have been handled before this point.
826 UNREACHABLE();
827 break;
828 }
829}
830
831
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000832class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000833 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000834 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000835 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000836 bool reversed,
837 OverwriteMode overwrite_mode)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000838 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000839 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000840 reversed_(reversed),
841 overwrite_mode_(overwrite_mode) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000842 set_comment("[ DeferredInlinedSmiOperation");
843 }
844
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000845 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000846
847 private:
848 Token::Value op_;
849 int value_;
850 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000851 OverwriteMode overwrite_mode_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000852};
853
854
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000855void DeferredInlineSmiOperation::Generate() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000856 switch (op_) {
857 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000858 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000859 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000860 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
861 __ mov(r1, Operand(Smi::FromInt(value_)));
862 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000863 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
864 __ mov(r0, Operand(Smi::FromInt(value_)));
865 }
866 break;
867 }
868
869 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000870 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000871 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000872 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
873 __ mov(r1, Operand(Smi::FromInt(value_)));
874 } else {
875 __ add(r1, r0, Operand(Smi::FromInt(value_)));
876 __ mov(r0, Operand(Smi::FromInt(value_)));
877 }
878 break;
879 }
880
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000881 // For these operations there is no optimistic operation that needs to be
882 // reverted.
883 case Token::MUL:
884 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000885 case Token::BIT_OR:
886 case Token::BIT_XOR:
887 case Token::BIT_AND: {
888 if (reversed_) {
889 __ mov(r1, Operand(Smi::FromInt(value_)));
890 } else {
891 __ mov(r1, Operand(r0));
892 __ mov(r0, Operand(Smi::FromInt(value_)));
893 }
894 break;
895 }
896
897 case Token::SHL:
898 case Token::SHR:
899 case Token::SAR: {
900 if (!reversed_) {
901 __ mov(r1, Operand(r0));
902 __ mov(r0, Operand(Smi::FromInt(value_)));
903 } else {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000904 UNREACHABLE(); // Should have been handled in SmiOperation.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000905 }
906 break;
907 }
908
909 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +0000910 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000911 UNREACHABLE();
912 break;
913 }
914
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000915 GenericBinaryOpStub stub(op_, overwrite_mode_, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000916 __ CallStub(&stub);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000917}
918
919
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000920static bool PopCountLessThanEqual2(unsigned int x) {
921 x &= x - 1;
922 return (x & (x - 1)) == 0;
923}
924
925
926// Returns the index of the lowest bit set.
927static int BitPosition(unsigned x) {
928 int bit_posn = 0;
929 while ((x & 0xf) == 0) {
930 bit_posn += 4;
931 x >>= 4;
932 }
933 while ((x & 1) == 0) {
934 bit_posn++;
935 x >>= 1;
936 }
937 return bit_posn;
938}
939
940
ager@chromium.org7c537e22008-10-16 08:43:32 +0000941void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000942 Handle<Object> value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000943 bool reversed,
944 OverwriteMode mode) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000945 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000946 // NOTE: This is an attempt to inline (a bit) more of the code for
947 // some possible smi operations (like + and -) when (at least) one
948 // of the operands is a literal smi. With this optimization, the
949 // performance of the system is increased by ~15%, and the generated
950 // code size is increased by ~1% (measured on a combination of
951 // different benchmarks).
952
mads.s.ager31e71382008-08-13 09:32:07 +0000953 // sp[0] : operand
954
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000955 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000957 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000958 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000959
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000960 bool something_to_inline = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000961 switch (op) {
962 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000963 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000964 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000966 __ add(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000967 deferred->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000968 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000969 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000970 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971 break;
972 }
973
974 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000975 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000976 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000977
ager@chromium.orge2902be2009-06-08 12:21:35 +0000978 if (reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000979 __ rsb(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000980 } else {
981 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000982 }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000983 deferred->Branch(vs);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000984 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000985 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000986 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000987 break;
988 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000989
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000990
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000991 case Token::BIT_OR:
992 case Token::BIT_XOR:
993 case Token::BIT_AND: {
994 DeferredCode* deferred =
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000995 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000996 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000997 deferred->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000998 switch (op) {
999 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
1000 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
1001 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
1002 default: UNREACHABLE();
1003 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001004 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001005 break;
1006 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001007
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001008 case Token::SHL:
1009 case Token::SHR:
1010 case Token::SAR: {
1011 if (reversed) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001012 something_to_inline = false;
1013 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001014 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001015 int shift_value = int_value & 0x1f; // least significant 5 bits
1016 DeferredCode* deferred =
1017 new DeferredInlineSmiOperation(op, shift_value, false, mode);
1018 __ tst(r0, Operand(kSmiTagMask));
1019 deferred->Branch(ne);
1020 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
1021 switch (op) {
1022 case Token::SHL: {
1023 if (shift_value != 0) {
1024 __ mov(r2, Operand(r2, LSL, shift_value));
1025 }
1026 // check that the *unsigned* result fits in a smi
1027 __ add(r3, r2, Operand(0x40000000), SetCC);
1028 deferred->Branch(mi);
1029 break;
1030 }
1031 case Token::SHR: {
1032 // LSR by immediate 0 means shifting 32 bits.
1033 if (shift_value != 0) {
1034 __ mov(r2, Operand(r2, LSR, shift_value));
1035 }
1036 // check that the *unsigned* result fits in a smi
1037 // neither of the two high-order bits can be set:
1038 // - 0x80000000: high bit would be lost when smi tagging
1039 // - 0x40000000: this number would convert to negative when
1040 // smi tagging these two cases can only happen with shifts
1041 // by 0 or 1 when handed a valid smi
1042 __ and_(r3, r2, Operand(0xc0000000), SetCC);
1043 deferred->Branch(ne);
1044 break;
1045 }
1046 case Token::SAR: {
1047 if (shift_value != 0) {
1048 // ASR by immediate 0 means shifting 32 bits.
1049 __ mov(r2, Operand(r2, ASR, shift_value));
1050 }
1051 break;
1052 }
1053 default: UNREACHABLE();
1054 }
1055 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
1056 deferred->BindExit();
1057 break;
1058 }
1059
1060 case Token::MOD: {
1061 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
1062 something_to_inline = false;
1063 break;
1064 }
1065 DeferredCode* deferred =
1066 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
1067 unsigned mask = (0x80000000u | kSmiTagMask);
1068 __ tst(r0, Operand(mask));
1069 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1070 mask = (int_value << kSmiTagSize) - 1;
1071 __ and_(r0, r0, Operand(mask));
1072 deferred->BindExit();
1073 break;
1074 }
1075
1076 case Token::MUL: {
1077 if (!IsEasyToMultiplyBy(int_value)) {
1078 something_to_inline = false;
1079 break;
1080 }
1081 DeferredCode* deferred =
1082 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
1083 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1084 max_smi_that_wont_overflow <<= kSmiTagSize;
1085 unsigned mask = 0x80000000u;
1086 while ((mask & max_smi_that_wont_overflow) == 0) {
1087 mask |= mask >> 1;
1088 }
1089 mask |= kSmiTagMask;
1090 // This does a single mask that checks for a too high value in a
1091 // conservative way and for a non-Smi. It also filters out negative
1092 // numbers, unfortunately, but since this code is inline we prefer
1093 // brevity to comprehensiveness.
1094 __ tst(r0, Operand(mask));
1095 deferred->Branch(ne);
1096 MultiplyByKnownInt(masm_, r0, r0, int_value);
1097 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098 break;
1099 }
1100
1101 default:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001102 something_to_inline = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001103 break;
1104 }
1105
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001106 if (!something_to_inline) {
1107 if (!reversed) {
1108 frame_->EmitPush(r0);
1109 __ mov(r0, Operand(value));
1110 frame_->EmitPush(r0);
1111 GenericBinaryOperation(op, mode, int_value);
1112 } else {
1113 __ mov(ip, Operand(value));
1114 frame_->EmitPush(ip);
1115 frame_->EmitPush(r0);
1116 GenericBinaryOperation(op, mode, kUnknownIntValue);
1117 }
1118 }
1119
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001120 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001121}
1122
1123
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001124void CodeGenerator::Comparison(Condition cc,
1125 Expression* left,
1126 Expression* right,
1127 bool strict) {
1128 if (left != NULL) LoadAndSpill(left);
1129 if (right != NULL) LoadAndSpill(right);
1130
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001131 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001132 // sp[0] : y
1133 // sp[1] : x
1134 // result : cc register
1135
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001136 // Strict only makes sense for equality comparisons.
1137 ASSERT(!strict || cc == eq);
1138
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001139 JumpTarget exit;
1140 JumpTarget smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001141 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1142 if (cc == gt || cc == le) {
1143 cc = ReverseCondition(cc);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001144 frame_->EmitPop(r1);
1145 frame_->EmitPop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001146 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001147 frame_->EmitPop(r0);
1148 frame_->EmitPop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001149 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001150 __ orr(r2, r0, Operand(r1));
1151 __ tst(r2, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001152 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001153
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001154 // Perform non-smi comparison by stub.
1155 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1156 // We call with 0 args because there are 0 on the stack.
1157 CompareStub stub(cc, strict);
1158 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001160 Result result = allocator_->Allocate(r0);
1161 ASSERT(result.is_valid());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001162 __ cmp(result.reg(), Operand(0));
1163 result.Unuse();
1164 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001165
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001166 // Do smi comparisons by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001167 smi.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001168 __ cmp(r1, Operand(r0));
1169
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001170 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001171 cc_reg_ = cc;
1172}
1173
1174
kasper.lund7276f142008-07-30 08:49:36 +00001175class CallFunctionStub: public CodeStub {
1176 public:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001177 CallFunctionStub(int argc, InLoopFlag in_loop)
1178 : argc_(argc), in_loop_(in_loop) {}
kasper.lund7276f142008-07-30 08:49:36 +00001179
1180 void Generate(MacroAssembler* masm);
1181
1182 private:
1183 int argc_;
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001184 InLoopFlag in_loop_;
kasper.lund7276f142008-07-30 08:49:36 +00001185
kasper.lund7276f142008-07-30 08:49:36 +00001186#if defined(DEBUG)
1187 void Print() { PrintF("CallFunctionStub (argc %d)\n", argc_); }
1188#endif // defined(DEBUG)
1189
1190 Major MajorKey() { return CallFunction; }
1191 int MinorKey() { return argc_; }
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001192 InLoopFlag InLoop() { return in_loop_; }
kasper.lund7276f142008-07-30 08:49:36 +00001193};
1194
1195
mads.s.ager31e71382008-08-13 09:32:07 +00001196// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001197void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001198 int position) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001199 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001200 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001201 int arg_count = args->length();
1202 for (int i = 0; i < arg_count; i++) {
1203 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001204 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001205
kasper.lund7276f142008-07-30 08:49:36 +00001206 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001207 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208
kasper.lund7276f142008-07-30 08:49:36 +00001209 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001210 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
1211 CallFunctionStub call_function(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001212 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001213
1214 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001215 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001216 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001217}
1218
1219
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001220void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001221 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001222 ASSERT(has_cc());
1223 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001224 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001225 cc_reg_ = al;
1226}
1227
1228
ager@chromium.org7c537e22008-10-16 08:43:32 +00001229void CodeGenerator::CheckStack() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001230 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001231 if (FLAG_check_stack) {
1232 Comment cmnt(masm_, "[ check stack");
1233 StackCheckStub stub;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001234 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001235 }
1236}
1237
1238
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001239void CodeGenerator::VisitAndSpill(Statement* statement) {
1240 ASSERT(in_spilled_code());
1241 set_in_spilled_code(false);
1242 Visit(statement);
1243 if (frame_ != NULL) {
1244 frame_->SpillAll();
1245 }
1246 set_in_spilled_code(true);
1247}
1248
1249
1250void CodeGenerator::VisitStatementsAndSpill(ZoneList<Statement*>* statements) {
1251 ASSERT(in_spilled_code());
1252 set_in_spilled_code(false);
1253 VisitStatements(statements);
1254 if (frame_ != NULL) {
1255 frame_->SpillAll();
1256 }
1257 set_in_spilled_code(true);
1258}
1259
1260
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001261void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1262#ifdef DEBUG
1263 int original_height = frame_->height();
1264#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001265 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001266 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1267 VisitAndSpill(statements->at(i));
1268 }
1269 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1270}
1271
1272
ager@chromium.org7c537e22008-10-16 08:43:32 +00001273void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001274#ifdef DEBUG
1275 int original_height = frame_->height();
1276#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001277 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001279 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001280 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001281 VisitStatementsAndSpill(node->statements());
1282 if (node->break_target()->is_linked()) {
1283 node->break_target()->Bind();
1284 }
1285 node->break_target()->Unuse();
1286 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287}
1288
1289
ager@chromium.org7c537e22008-10-16 08:43:32 +00001290void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001291 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001292 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001293 frame_->EmitPush(r0);
1294 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001295 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001296 frame_->EmitPush(r0);
1297 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001298 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001299}
1300
1301
ager@chromium.org7c537e22008-10-16 08:43:32 +00001302void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001303#ifdef DEBUG
1304 int original_height = frame_->height();
1305#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001306 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307 Comment cmnt(masm_, "[ Declaration");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001308 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309 Variable* var = node->proxy()->var();
1310 ASSERT(var != NULL); // must have been resolved
1311 Slot* slot = var->slot();
1312
1313 // If it was not possible to allocate the variable at compile time,
1314 // we need to "declare" it at runtime to make sure it actually
1315 // exists in the local context.
1316 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1317 // Variables with a "LOOKUP" slot were introduced as non-locals
1318 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001319 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001320 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001321 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001322 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001323 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001324 // Declaration nodes are always declared in only two modes.
1325 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1326 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001327 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001328 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001329 // Push initial value, if any.
1330 // Note: For variables we must not push an initial value (such as
1331 // 'undefined') because we may have a (legal) redeclaration and we
1332 // must not destroy the current value.
1333 if (node->mode() == Variable::CONST) {
mads.s.ager31e71382008-08-13 09:32:07 +00001334 __ mov(r0, Operand(Factory::the_hole_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001335 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001337 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001338 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001339 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001340 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001342 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001343 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001344 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001345 return;
1346 }
1347
1348 ASSERT(!var->is_global());
1349
1350 // If we have a function or a constant, we need to initialize the variable.
1351 Expression* val = NULL;
1352 if (node->mode() == Variable::CONST) {
1353 val = new Literal(Factory::the_hole_value());
1354 } else {
1355 val = node->fun(); // NULL if we don't have a function
1356 }
1357
1358 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001359 {
1360 // Set initial value.
1361 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001362 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001363 target.SetValue(NOT_CONST_INIT);
1364 // The reference is removed from the stack (preserving TOS) when
1365 // it goes out of scope.
1366 }
1367 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001368 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001369 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001370 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001371}
1372
1373
ager@chromium.org7c537e22008-10-16 08:43:32 +00001374void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001375#ifdef DEBUG
1376 int original_height = frame_->height();
1377#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001378 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001379 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001380 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381 Expression* expression = node->expression();
1382 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001383 LoadAndSpill(expression);
1384 frame_->Drop();
1385 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001386}
1387
1388
ager@chromium.org7c537e22008-10-16 08:43:32 +00001389void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001390#ifdef DEBUG
1391 int original_height = frame_->height();
1392#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001393 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001395 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001396 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001397 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001398}
1399
1400
ager@chromium.org7c537e22008-10-16 08:43:32 +00001401void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001402#ifdef DEBUG
1403 int original_height = frame_->height();
1404#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001405 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001406 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001407 // Generate different code depending on which parts of the if statement
1408 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001409 bool has_then_stm = node->HasThenStatement();
1410 bool has_else_stm = node->HasElseStatement();
1411
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001412 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001413
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001414 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001415 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001416 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001417 JumpTarget then;
1418 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001419 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001420 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1421 &then, &else_, true);
1422 if (frame_ != NULL) {
1423 Branch(false, &else_);
1424 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001425 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001426 if (frame_ != NULL || then.is_linked()) {
1427 then.Bind();
1428 VisitAndSpill(node->then_statement());
1429 }
1430 if (frame_ != NULL) {
1431 exit.Jump();
1432 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001434 if (else_.is_linked()) {
1435 else_.Bind();
1436 VisitAndSpill(node->else_statement());
1437 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438
1439 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001440 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001442 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001444 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1445 &then, &exit, true);
1446 if (frame_ != NULL) {
1447 Branch(false, &exit);
1448 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001450 if (frame_ != NULL || then.is_linked()) {
1451 then.Bind();
1452 VisitAndSpill(node->then_statement());
1453 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001454
1455 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001456 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001457 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001458 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001459 // if (!cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001460 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1461 &exit, &else_, true);
1462 if (frame_ != NULL) {
1463 Branch(true, &exit);
1464 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001465 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001466 if (frame_ != NULL || else_.is_linked()) {
1467 else_.Bind();
1468 VisitAndSpill(node->else_statement());
1469 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001470
1471 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001472 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001473 ASSERT(!has_then_stm && !has_else_stm);
1474 // if (cond)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001475 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
1476 &exit, &exit, false);
1477 if (frame_ != NULL) {
1478 if (has_cc()) {
1479 cc_reg_ = al;
1480 } else {
1481 frame_->Drop();
1482 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001483 }
1484 }
1485
1486 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001487 if (exit.is_linked()) {
1488 exit.Bind();
1489 }
1490 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001491}
1492
1493
ager@chromium.org7c537e22008-10-16 08:43:32 +00001494void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001495 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001496 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001497 CodeForStatementPosition(node);
1498 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001499}
1500
1501
ager@chromium.org7c537e22008-10-16 08:43:32 +00001502void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001503 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001504 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001505 CodeForStatementPosition(node);
1506 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001507}
1508
1509
ager@chromium.org7c537e22008-10-16 08:43:32 +00001510void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001511 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001512 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001513
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001514 if (function_return_is_shadowed_) {
1515 CodeForStatementPosition(node);
1516 LoadAndSpill(node->expression());
1517 frame_->EmitPop(r0);
1518 function_return_.Jump();
1519 } else {
1520 // Load the returned value.
1521 CodeForStatementPosition(node);
1522 LoadAndSpill(node->expression());
1523
1524 // Pop the result from the frame and prepare the frame for
1525 // returning thus making it easier to merge.
1526 frame_->EmitPop(r0);
1527 frame_->PrepareForReturn();
1528
1529 function_return_.Jump();
1530 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001531}
1532
1533
ager@chromium.org7c537e22008-10-16 08:43:32 +00001534void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001535#ifdef DEBUG
1536 int original_height = frame_->height();
1537#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001538 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001539 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001540 CodeForStatementPosition(node);
1541 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001542 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001543 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001544 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001545 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001546 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001547#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001548 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001549 __ cmp(r0, Operand(cp));
1550 verified_true.Branch(eq);
1551 __ stop("PushContext: r0 is expected to be the same as cp");
1552 verified_true.Bind();
1553#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001554 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001555 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001556 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001557}
1558
1559
ager@chromium.org7c537e22008-10-16 08:43:32 +00001560void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001561#ifdef DEBUG
1562 int original_height = frame_->height();
1563#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001564 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001565 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001566 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001567 // Pop context.
1568 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1569 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001570 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001571 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001572}
1573
1574
ager@chromium.org7c537e22008-10-16 08:43:32 +00001575void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001576#ifdef DEBUG
1577 int original_height = frame_->height();
1578#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001579 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001581 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001582 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001583
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001584 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001585
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001586 JumpTarget next_test;
1587 JumpTarget fall_through;
1588 JumpTarget default_entry;
1589 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001590 ZoneList<CaseClause*>* cases = node->cases();
1591 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001592 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001593
1594 for (int i = 0; i < length; i++) {
1595 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001596 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001597 // Remember the default clause and compile it at the end.
1598 default_clause = clause;
1599 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001600 }
1601
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001602 Comment cmnt(masm_, "[ Case clause");
1603 // Compile the test.
1604 next_test.Bind();
1605 next_test.Unuse();
1606 // Duplicate TOS.
1607 __ ldr(r0, frame_->Top());
1608 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001609 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001610 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001611
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001612 // Before entering the body from the test, remove the switch value from
1613 // the stack.
1614 frame_->Drop();
1615
1616 // Label the body so that fall through is enabled.
1617 if (i > 0 && cases->at(i - 1)->is_default()) {
1618 default_exit.Bind();
1619 } else {
1620 fall_through.Bind();
1621 fall_through.Unuse();
1622 }
1623 VisitStatementsAndSpill(clause->statements());
1624
1625 // If control flow can fall through from the body, jump to the next body
1626 // or the end of the statement.
1627 if (frame_ != NULL) {
1628 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1629 default_entry.Jump();
1630 } else {
1631 fall_through.Jump();
1632 }
1633 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001634 }
1635
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001636 // The final "test" removes the switch value.
1637 next_test.Bind();
1638 frame_->Drop();
1639
1640 // If there is a default clause, compile it.
1641 if (default_clause != NULL) {
1642 Comment cmnt(masm_, "[ Default clause");
1643 default_entry.Bind();
1644 VisitStatementsAndSpill(default_clause->statements());
1645 // If control flow can fall out of the default and there is a case after
1646 // it, jup to that case's body.
1647 if (frame_ != NULL && default_exit.is_bound()) {
1648 default_exit.Jump();
1649 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001650 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001651
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001652 if (fall_through.is_linked()) {
1653 fall_through.Bind();
1654 }
1655
1656 if (node->break_target()->is_linked()) {
1657 node->break_target()->Bind();
1658 }
1659 node->break_target()->Unuse();
1660 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001661}
1662
1663
ager@chromium.org7c537e22008-10-16 08:43:32 +00001664void CodeGenerator::VisitLoopStatement(LoopStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001665#ifdef DEBUG
1666 int original_height = frame_->height();
1667#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001668 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001669 Comment cmnt(masm_, "[ LoopStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001670 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001671 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001672
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001673 // Simple condition analysis. ALWAYS_TRUE and ALWAYS_FALSE represent a
1674 // known result for the test expression, with no side effects.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001675 enum { ALWAYS_TRUE, ALWAYS_FALSE, DONT_KNOW } info = DONT_KNOW;
1676 if (node->cond() == NULL) {
1677 ASSERT(node->type() == LoopStatement::FOR_LOOP);
1678 info = ALWAYS_TRUE;
1679 } else {
1680 Literal* lit = node->cond()->AsLiteral();
1681 if (lit != NULL) {
1682 if (lit->IsTrue()) {
1683 info = ALWAYS_TRUE;
1684 } else if (lit->IsFalse()) {
1685 info = ALWAYS_FALSE;
1686 }
1687 }
1688 }
1689
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001690 switch (node->type()) {
1691 case LoopStatement::DO_LOOP: {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001692 JumpTarget body(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001693
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001694 // Label the top of the loop for the backward CFG edge. If the test
1695 // is always true we can use the continue target, and if the test is
1696 // always false there is no need.
1697 if (info == ALWAYS_TRUE) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001698 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001699 node->continue_target()->Bind();
1700 } else if (info == ALWAYS_FALSE) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001701 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001702 } else {
1703 ASSERT(info == DONT_KNOW);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001704 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001705 body.Bind();
1706 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001707
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001708 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001709 VisitAndSpill(node->body());
1710
1711 // Compile the test.
1712 if (info == ALWAYS_TRUE) {
1713 if (has_valid_frame()) {
1714 // If control can fall off the end of the body, jump back to the
1715 // top.
1716 node->continue_target()->Jump();
1717 }
1718 } else if (info == ALWAYS_FALSE) {
1719 // If we have a continue in the body, we only have to bind its jump
1720 // target.
1721 if (node->continue_target()->is_linked()) {
1722 node->continue_target()->Bind();
1723 }
1724 } else {
1725 ASSERT(info == DONT_KNOW);
1726 // We have to compile the test expression if it can be reached by
1727 // control flow falling out of the body or via continue.
1728 if (node->continue_target()->is_linked()) {
1729 node->continue_target()->Bind();
1730 }
1731 if (has_valid_frame()) {
1732 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1733 &body, node->break_target(), true);
1734 if (has_valid_frame()) {
1735 // A invalid frame here indicates that control did not
1736 // fall out of the test expression.
1737 Branch(true, &body);
1738 }
1739 }
1740 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001741 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001742 }
1743
1744 case LoopStatement::WHILE_LOOP: {
1745 // If the test is never true and has no side effects there is no need
1746 // to compile the test or body.
1747 if (info == ALWAYS_FALSE) break;
1748
1749 // Label the top of the loop with the continue target for the backward
1750 // CFG edge.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001751 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001752 node->continue_target()->Bind();
1753
1754 if (info == DONT_KNOW) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001755 JumpTarget body;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001756 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1757 &body, node->break_target(), true);
1758 if (has_valid_frame()) {
1759 // A NULL frame indicates that control did not fall out of the
1760 // test expression.
1761 Branch(false, node->break_target());
1762 }
1763 if (has_valid_frame() || body.is_linked()) {
1764 body.Bind();
1765 }
1766 }
1767
1768 if (has_valid_frame()) {
1769 CheckStack(); // TODO(1222600): ignore if body contains calls.
1770 VisitAndSpill(node->body());
1771
1772 // If control flow can fall out of the body, jump back to the top.
1773 if (has_valid_frame()) {
1774 node->continue_target()->Jump();
1775 }
1776 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001777 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001778 }
1779
1780 case LoopStatement::FOR_LOOP: {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001781 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001782
1783 if (node->init() != NULL) {
1784 VisitAndSpill(node->init());
1785 }
1786
1787 // There is no need to compile the test or body.
1788 if (info == ALWAYS_FALSE) break;
1789
1790 // If there is no update statement, label the top of the loop with the
1791 // continue target, otherwise with the loop target.
1792 if (node->next() == NULL) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001793 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001794 node->continue_target()->Bind();
1795 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001796 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001797 loop.Bind();
1798 }
1799
1800 // If the test is always true, there is no need to compile it.
1801 if (info == DONT_KNOW) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001802 JumpTarget body;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001803 LoadConditionAndSpill(node->cond(), NOT_INSIDE_TYPEOF,
1804 &body, node->break_target(), true);
1805 if (has_valid_frame()) {
1806 Branch(false, node->break_target());
1807 }
1808 if (has_valid_frame() || body.is_linked()) {
1809 body.Bind();
1810 }
1811 }
1812
1813 if (has_valid_frame()) {
1814 CheckStack(); // TODO(1222600): ignore if body contains calls.
1815 VisitAndSpill(node->body());
1816
1817 if (node->next() == NULL) {
1818 // If there is no update statement and control flow can fall out
1819 // of the loop, jump directly to the continue label.
1820 if (has_valid_frame()) {
1821 node->continue_target()->Jump();
1822 }
1823 } else {
1824 // If there is an update statement and control flow can reach it
1825 // via falling out of the body of the loop or continuing, we
1826 // compile the update statement.
1827 if (node->continue_target()->is_linked()) {
1828 node->continue_target()->Bind();
1829 }
1830 if (has_valid_frame()) {
1831 // Record source position of the statement as this code which is
1832 // after the code for the body actually belongs to the loop
1833 // statement and not the body.
1834 CodeForStatementPosition(node);
1835 VisitAndSpill(node->next());
1836 loop.Jump();
1837 }
1838 }
1839 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001840 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001841 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001842 }
1843
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001844 if (node->break_target()->is_linked()) {
1845 node->break_target()->Bind();
1846 }
1847 node->continue_target()->Unuse();
1848 node->break_target()->Unuse();
1849 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001850}
1851
1852
ager@chromium.org7c537e22008-10-16 08:43:32 +00001853void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001854#ifdef DEBUG
1855 int original_height = frame_->height();
1856#endif
1857 ASSERT(!in_spilled_code());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001858 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001859 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001860 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001861
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001862 JumpTarget primitive;
1863 JumpTarget jsobject;
1864 JumpTarget fixed_array;
1865 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
1866 JumpTarget end_del_check;
1867 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868
1869 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001870 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001871
1872 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1873 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001874 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001875 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001876 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001877 __ cmp(r0, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001878 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001879
1880 // Stack layout in body:
1881 // [iteration counter (Smi)]
1882 // [length of array]
1883 // [FixedArray]
1884 // [Map or 0]
1885 // [Object]
1886
1887 // Check if enumerable is already a JSObject
1888 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001889 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001890 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001891 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001892
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001893 primitive.Bind();
1894 frame_->EmitPush(r0);
1895 Result arg_count = allocator_->Allocate(r0);
1896 ASSERT(arg_count.is_valid());
1897 __ mov(arg_count.reg(), Operand(0));
1898 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001899
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001900 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001901 // Get the set of properties (as a FixedArray or Map).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001902 frame_->EmitPush(r0); // duplicate the object being enumerated
1903 frame_->EmitPush(r0);
1904 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001905
1906 // If we got a Map, we can do a fast modification check.
1907 // Otherwise, we got a FixedArray, and we have to do a slow check.
1908 __ mov(r2, Operand(r0));
1909 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
1910 __ cmp(r1, Operand(Factory::meta_map()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001911 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001912
1913 // Get enum cache
1914 __ mov(r1, Operand(r0));
1915 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1916 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1917 __ ldr(r2,
1918 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1919
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001920 frame_->EmitPush(r0); // map
1921 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001922 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001923 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001924 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001925 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001926 frame_->EmitPush(r0);
1927 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001928
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001929 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001930 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001931 frame_->EmitPush(r1); // insert 0 in place of Map
1932 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001933
1934 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001935 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001937 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001938 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001939 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001940
1941 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001942 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00001943 // sp[0] : index
1944 // sp[1] : array/enum cache length
1945 // sp[2] : array or enum cache
1946 // sp[3] : 0 or map
1947 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001948 // Grab the current frame's height for the break and continue
1949 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001950 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1951 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001952
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001953 __ ldr(r0, frame_->ElementAt(0)); // load the current count
1954 __ ldr(r1, frame_->ElementAt(1)); // load the length
1955 __ cmp(r0, Operand(r1)); // compare to the array length
1956 node->break_target()->Branch(hs);
1957
1958 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001959
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001960 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001961 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001962 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1963 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1964
1965 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001966 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001967 // Check if this (still) matches the map of the enumerable.
1968 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001969 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1971 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001972 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001973
1974 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001975 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
1976 frame_->EmitPush(r0);
1977 frame_->EmitPush(r3); // push entry
1978 Result arg_count_register = allocator_->Allocate(r0);
1979 ASSERT(arg_count_register.is_valid());
1980 __ mov(arg_count_register.reg(), Operand(1));
1981 Result result = frame_->InvokeBuiltin(Builtins::FILTER_KEY,
1982 CALL_JS,
1983 &arg_count_register,
1984 2);
1985 __ mov(r3, Operand(result.reg()));
1986 result.Unuse();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001987
1988 // If the property has been removed while iterating, we just skip it.
1989 __ cmp(r3, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001990 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001991
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001992 end_del_check.Bind();
1993 // Store the entry in the 'each' expression and take another spin in the
1994 // loop. r3: i'th entry of the enum cache (or string there of)
1995 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001996 { Reference each(this, node->each());
1997 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001998 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001999 __ ldr(r0, frame_->ElementAt(each.size()));
2000 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002001 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00002002 // If the reference was to a slot we rely on the convenient property
2003 // that it doesn't matter whether a value (eg, r3 pushed above) is
2004 // right on top of or right underneath a zero-sized reference.
2005 each.SetValue(NOT_CONST_INIT);
mads.s.ager31e71382008-08-13 09:32:07 +00002006 if (each.size() > 0) {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002007 // It's safe to pop the value lying on top of the reference before
2008 // unloading the reference itself (which preserves the top of stack,
2009 // ie, now the topmost value of the non-zero sized reference), since
2010 // we will discard the top of stack after unloading the reference
2011 // anyway.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002012 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002013 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002014 }
2015 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00002016 // Discard the i'th entry pushed above or else the remainder of the
2017 // reference, whichever is currently on top of the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002018 frame_->Drop();
2019
2020 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002021 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002022 VisitAndSpill(node->body());
2023
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002024 // Next. Reestablish a spilled frame in case we are coming here via
2025 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002026 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002027 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002028 frame_->EmitPop(r0);
2029 __ add(r0, r0, Operand(Smi::FromInt(1)));
2030 frame_->EmitPush(r0);
2031 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002033 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2034 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002035 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002036 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002037
2038 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002039 exit.Bind();
2040 node->continue_target()->Unuse();
2041 node->break_target()->Unuse();
2042 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043}
2044
2045
ager@chromium.org7c537e22008-10-16 08:43:32 +00002046void CodeGenerator::VisitTryCatch(TryCatch* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002047#ifdef DEBUG
2048 int original_height = frame_->height();
2049#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002050 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051 Comment cmnt(masm_, "[ TryCatch");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002052 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002053
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002054 JumpTarget try_block;
2055 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002057 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002058 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002059 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002060
2061 // Store the caught exception in the catch variable.
2062 { Reference ref(this, node->catch_var());
ager@chromium.org7c537e22008-10-16 08:43:32 +00002063 ASSERT(ref.is_slot());
2064 // Here we make use of the convenient property that it doesn't matter
2065 // whether a value is immediately on top of or underneath a zero-sized
2066 // reference.
2067 ref.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002068 }
2069
2070 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002071 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002073 VisitStatementsAndSpill(node->catch_block()->statements());
2074 if (frame_ != NULL) {
2075 exit.Jump();
2076 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002077
2078
2079 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002080 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002082 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2083 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002084
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002085 // Shadow the labels for all escapes from the try block, including
2086 // returns. During shadowing, the original label is hidden as the
2087 // LabelShadow and operations on the original actually affect the
2088 // shadowing label.
2089 //
2090 // We should probably try to unify the escaping labels and the return
2091 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002092 int nof_escapes = node->escaping_targets()->length();
2093 List<ShadowTarget*> shadows(1 + nof_escapes);
2094
2095 // Add the shadow target for the function return.
2096 static const int kReturnShadowIndex = 0;
2097 shadows.Add(new ShadowTarget(&function_return_));
2098 bool function_return_was_shadowed = function_return_is_shadowed_;
2099 function_return_is_shadowed_ = true;
2100 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2101
2102 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002104 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002105 }
2106
2107 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002108 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109
2110 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002111 // After shadowing stops, the original labels are unshadowed and the
2112 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002113 bool has_unlinks = false;
2114 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002115 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002116 has_unlinks = has_unlinks || shadows[i]->is_linked();
2117 }
2118 function_return_is_shadowed_ = function_return_was_shadowed;
2119
2120 // Get an external reference to the handler address.
2121 ExternalReference handler_address(Top::k_handler_address);
2122
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002123 // If we can fall off the end of the try block, unlink from try chain.
2124 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002125 // The next handler address is on top of the frame. Unlink from
2126 // the handler list and drop the rest of this handler from the
2127 // frame.
2128 ASSERT(StackHandlerConstants::kNextOffset == 0);
2129 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002130 __ mov(r3, Operand(handler_address));
2131 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002132 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002133 if (has_unlinks) {
2134 exit.Jump();
2135 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002136 }
2137
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002138 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002139 // jumped to. Deallocate each shadow target.
2140 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002141 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002142 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002143 shadows[i]->Bind();
2144 // Because we can be jumping here (to spilled code) from unspilled
2145 // code, we need to reestablish a spilled frame at this block.
2146 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002147
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148 // Reload sp from the top handler, because some statements that we
2149 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002150 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002151 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002152 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002153
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002154 ASSERT(StackHandlerConstants::kNextOffset == 0);
2155 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002156 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002157 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002158
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002159 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2160 frame_->PrepareForReturn();
2161 }
2162 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002163 }
2164 }
2165
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002166 exit.Bind();
2167 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002168}
2169
2170
ager@chromium.org7c537e22008-10-16 08:43:32 +00002171void CodeGenerator::VisitTryFinally(TryFinally* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002172#ifdef DEBUG
2173 int original_height = frame_->height();
2174#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002175 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002176 Comment cmnt(masm_, "[ TryFinally");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002177 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002178
2179 // State: Used to keep track of reason for entering the finally
2180 // block. Should probably be extended to hold information for
2181 // break/continue from within the try block.
2182 enum { FALLING, THROWING, JUMPING };
2183
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002184 JumpTarget try_block;
2185 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002186
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002187 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002188
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002189 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002190 // In case of thrown exceptions, this is where we continue.
2191 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002192 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002193
2194 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002195 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002196
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002197 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2198 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002199
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002200 // Shadow the labels for all escapes from the try block, including
2201 // returns. Shadowing hides the original label as the LabelShadow and
2202 // operations on the original actually affect the shadowing label.
2203 //
2204 // We should probably try to unify the escaping labels and the return
2205 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002206 int nof_escapes = node->escaping_targets()->length();
2207 List<ShadowTarget*> shadows(1 + nof_escapes);
2208
2209 // Add the shadow target for the function return.
2210 static const int kReturnShadowIndex = 0;
2211 shadows.Add(new ShadowTarget(&function_return_));
2212 bool function_return_was_shadowed = function_return_is_shadowed_;
2213 function_return_is_shadowed_ = true;
2214 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2215
2216 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002217 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002218 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002219 }
2220
2221 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002222 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002223
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002224 // Stop the introduced shadowing and count the number of required unlinks.
2225 // After shadowing stops, the original labels are unshadowed and the
2226 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002227 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002228 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002229 shadows[i]->StopShadowing();
2230 if (shadows[i]->is_linked()) nof_unlinks++;
2231 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002232 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002233
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002234 // Get an external reference to the handler address.
2235 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002237 // If we can fall off the end of the try block, unlink from the try
2238 // chain and set the state on the frame to FALLING.
2239 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002240 // The next handler address is on top of the frame.
2241 ASSERT(StackHandlerConstants::kNextOffset == 0);
2242 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002243 __ mov(r3, Operand(handler_address));
2244 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002245 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002246
2247 // Fake a top of stack value (unneeded when FALLING) and set the
2248 // state in r2, then jump around the unlink blocks if any.
2249 __ mov(r0, Operand(Factory::undefined_value()));
2250 frame_->EmitPush(r0);
2251 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2252 if (nof_unlinks > 0) {
2253 finally_block.Jump();
2254 }
2255 }
2256
2257 // Generate code to unlink and set the state for the (formerly)
2258 // shadowing targets that have been jumped to.
2259 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002260 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002261 // If we have come from the shadowed return, the return value is
2262 // in (a non-refcounted reference to) r0. We must preserve it
2263 // until it is pushed.
2264 //
2265 // Because we can be jumping here (to spilled code) from
2266 // unspilled code, we need to reestablish a spilled frame at
2267 // this block.
2268 shadows[i]->Bind();
2269 frame_->SpillAll();
2270
2271 // Reload sp from the top handler, because some statements that
2272 // we break from (eg, for...in) may have left stuff on the
2273 // stack.
2274 __ mov(r3, Operand(handler_address));
2275 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002276 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002277
2278 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002279 // handler address is currently on top of the frame.
2280 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002281 frame_->EmitPop(r1);
2282 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002283 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002284
2285 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002286 // If this label shadowed the function return, materialize the
2287 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002288 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002289 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002290 // Fake TOS for targets that shadowed breaks and continues.
mads.s.ager31e71382008-08-13 09:32:07 +00002291 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002292 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002293 }
2294 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002295 if (--nof_unlinks > 0) {
2296 // If this is not the last unlink block, jump around the next.
2297 finally_block.Jump();
2298 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002299 }
2300 }
2301
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002303 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002304
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002305 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002306 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002307
2308 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002309 // and the state - while evaluating the finally block.
2310 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002311 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002312 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002313
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002314 if (has_valid_frame()) {
2315 // Restore state and return value or faked TOS.
2316 frame_->EmitPop(r2);
2317 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002318 }
2319
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002320 // Generate code to jump to the right destination for all used
2321 // formerly shadowing targets. Deallocate each shadow target.
2322 for (int i = 0; i < shadows.length(); i++) {
2323 if (has_valid_frame() && shadows[i]->is_bound()) {
2324 JumpTarget* original = shadows[i]->other_target();
2325 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2326 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002327 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002328 skip.Branch(ne);
2329 frame_->PrepareForReturn();
2330 original->Jump();
2331 skip.Bind();
2332 } else {
2333 original->Branch(eq);
2334 }
2335 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002336 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002337
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002338 if (has_valid_frame()) {
2339 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002340 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002341 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2342 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002343
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002344 // Rethrow exception.
2345 frame_->EmitPush(r0);
2346 frame_->CallRuntime(Runtime::kReThrow, 1);
2347
2348 // Done.
2349 exit.Bind();
2350 }
2351 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002352}
2353
2354
ager@chromium.org7c537e22008-10-16 08:43:32 +00002355void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002356#ifdef DEBUG
2357 int original_height = frame_->height();
2358#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002359 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002360 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002361 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002362#ifdef ENABLE_DEBUGGER_SUPPORT
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002363 frame_->CallRuntime(Runtime::kDebugBreak, 0);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002364#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002365 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002366 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002367}
2368
2369
ager@chromium.org7c537e22008-10-16 08:43:32 +00002370void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002371 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002372 ASSERT(boilerplate->IsBoilerplate());
2373
2374 // Push the boilerplate on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002375 __ mov(r0, Operand(boilerplate));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002376 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002377
2378 // Create a new closure.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002379 frame_->EmitPush(cp);
2380 frame_->CallRuntime(Runtime::kNewClosure, 2);
2381 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002382}
2383
2384
ager@chromium.org7c537e22008-10-16 08:43:32 +00002385void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002386#ifdef DEBUG
2387 int original_height = frame_->height();
2388#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002389 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002390 Comment cmnt(masm_, "[ FunctionLiteral");
2391
2392 // Build the function boilerplate and instantiate it.
2393 Handle<JSFunction> boilerplate = BuildBoilerplate(node);
kasper.lund212ac232008-07-16 07:07:30 +00002394 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002395 if (HasStackOverflow()) {
2396 ASSERT(frame_->height() == original_height);
2397 return;
2398 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002399 InstantiateBoilerplate(boilerplate);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002400 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002401}
2402
2403
ager@chromium.org7c537e22008-10-16 08:43:32 +00002404void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002405 FunctionBoilerplateLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002406#ifdef DEBUG
2407 int original_height = frame_->height();
2408#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002409 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002410 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2411 InstantiateBoilerplate(node->boilerplate());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002412 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002413}
2414
2415
ager@chromium.org7c537e22008-10-16 08:43:32 +00002416void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002417#ifdef DEBUG
2418 int original_height = frame_->height();
2419#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002420 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002421 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002422 JumpTarget then;
2423 JumpTarget else_;
2424 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002425 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
2426 &then, &else_, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002427 Branch(false, &else_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002428 then.Bind();
2429 LoadAndSpill(node->then_expression(), typeof_state());
2430 exit.Jump();
2431 else_.Bind();
2432 LoadAndSpill(node->else_expression(), typeof_state());
2433 exit.Bind();
2434 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002435}
2436
2437
ager@chromium.org7c537e22008-10-16 08:43:32 +00002438void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002439 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002440 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002441 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002442
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002443 JumpTarget slow;
2444 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002445
2446 // Generate fast-case code for variables that might be shadowed by
2447 // eval-introduced variables. Eval is used a lot without
2448 // introducing variables. In those cases, we do not want to
2449 // perform a runtime call for all variables in the scope
2450 // containing the eval.
2451 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2452 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002453 // If there was no control flow to slow, we can exit early.
2454 if (!slow.is_linked()) {
2455 frame_->EmitPush(r0);
2456 return;
2457 }
2458
2459 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002460
2461 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2462 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2463 // Only generate the fast case for locals that rewrite to slots.
2464 // This rules out argument loads.
2465 if (potential_slot != NULL) {
2466 __ ldr(r0,
2467 ContextSlotOperandCheckExtensions(potential_slot,
2468 r1,
2469 r2,
2470 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002471 if (potential_slot->var()->mode() == Variable::CONST) {
2472 __ cmp(r0, Operand(Factory::the_hole_value()));
2473 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
2474 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002475 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002476 // ContextSlotOperandCheckExtensions so we have to jump around
2477 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002478 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002479 }
2480 }
2481
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002482 slow.Bind();
2483 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002484 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002485 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002486
ager@chromium.org7c537e22008-10-16 08:43:32 +00002487 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002488 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002489 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002490 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002491 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002492
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002493 done.Bind();
2494 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002495
2496 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002497 // Note: We would like to keep the assert below, but it fires because of
2498 // some nasty code in LoadTypeofExpression() which should be removed...
ager@chromium.org381abbb2009-02-25 13:23:22 +00002499 // ASSERT(!slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002500
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002501 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002502 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002503 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002504 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002505 // Const slots may contain 'the hole' value (the constant hasn't been
2506 // initialized yet) which needs to be converted into the 'undefined'
2507 // value.
2508 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002509 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002510 __ cmp(r0, Operand(Factory::the_hole_value()));
2511 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002512 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002513 }
2514 }
2515}
2516
2517
ager@chromium.org381abbb2009-02-25 13:23:22 +00002518void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2519 TypeofState typeof_state,
2520 Register tmp,
2521 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002522 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002523 // Check that no extension objects have been created by calls to
2524 // eval from the current scope to the global scope.
2525 Register context = cp;
2526 Scope* s = scope();
2527 while (s != NULL) {
2528 if (s->num_heap_slots() > 0) {
2529 if (s->calls_eval()) {
2530 // Check that extension is NULL.
2531 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2532 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002533 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002534 }
2535 // Load next context in chain.
2536 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2537 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2538 context = tmp;
2539 }
2540 // If no outer scope calls eval, we do not need to check more
2541 // context extensions.
2542 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2543 s = s->outer_scope();
2544 }
2545
2546 if (s->is_eval_scope()) {
2547 Label next, fast;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002548 if (!context.is(tmp)) {
2549 __ mov(tmp, Operand(context));
2550 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002551 __ bind(&next);
2552 // Terminate at global context.
2553 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
2554 __ cmp(tmp2, Operand(Factory::global_context_map()));
2555 __ b(eq, &fast);
2556 // Check that extension is NULL.
2557 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2558 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002559 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002560 // Load next context in chain.
2561 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2562 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2563 __ b(&next);
2564 __ bind(&fast);
2565 }
2566
2567 // All extension objects were empty and it is safe to use a global
2568 // load IC call.
2569 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2570 // Load the global object.
2571 LoadGlobal();
2572 // Setup the name register.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002573 Result name = allocator_->Allocate(r2);
2574 ASSERT(name.is_valid()); // We are in spilled code.
2575 __ mov(name.reg(), Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002576 // Call IC stub.
2577 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002578 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002579 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002580 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002581 }
2582
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002583 // Drop the global object. The result is in r0.
2584 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002585}
2586
2587
ager@chromium.org7c537e22008-10-16 08:43:32 +00002588void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002589#ifdef DEBUG
2590 int original_height = frame_->height();
2591#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002592 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002593 Comment cmnt(masm_, "[ Slot");
2594 LoadFromSlot(node, typeof_state());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002595 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002596}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002597
ager@chromium.org7c537e22008-10-16 08:43:32 +00002598
2599void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002600#ifdef DEBUG
2601 int original_height = frame_->height();
2602#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002603 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002604 Comment cmnt(masm_, "[ VariableProxy");
2605
2606 Variable* var = node->var();
2607 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002608 if (expr != NULL) {
2609 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002610 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002611 ASSERT(var->is_global());
2612 Reference ref(this, node);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002613 ref.GetValueAndSpill(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002614 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002615 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002616}
2617
2618
ager@chromium.org7c537e22008-10-16 08:43:32 +00002619void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002620#ifdef DEBUG
2621 int original_height = frame_->height();
2622#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002623 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002624 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002625 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002626 frame_->EmitPush(r0);
2627 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002628}
2629
2630
ager@chromium.org7c537e22008-10-16 08:43:32 +00002631void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002632#ifdef DEBUG
2633 int original_height = frame_->height();
2634#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002635 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002636 Comment cmnt(masm_, "[ RexExp Literal");
2637
2638 // Retrieve the literal array and check the allocated entry.
2639
2640 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002641 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002642
2643 // Load the literals array of the function.
2644 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2645
2646 // Load the literal at the ast saved index.
2647 int literal_offset =
2648 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2649 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2650
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002651 JumpTarget done;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002652 __ cmp(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002653 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002654
2655 // If the entry is undefined we call the runtime system to computed
2656 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002657 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002658 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002659 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002660 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002661 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002662 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002663 frame_->EmitPush(r0);
2664 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002665 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002666
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002667 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002668 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002669 frame_->EmitPush(r2);
2670 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002671}
2672
2673
2674// This deferred code stub will be used for creating the boilerplate
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002675// by calling Runtime_CreateObjectLiteralBoilerplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002676// Each created boilerplate is stored in the JSFunction and they are
2677// therefore context dependent.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002678class DeferredObjectLiteral: public DeferredCode {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002679 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002680 explicit DeferredObjectLiteral(ObjectLiteral* node) : node_(node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002681 set_comment("[ DeferredObjectLiteral");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002682 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002683
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002684 virtual void Generate();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002685
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002686 private:
2687 ObjectLiteral* node_;
2688};
2689
2690
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002691void DeferredObjectLiteral::Generate() {
2692 // Argument is passed in r1.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002693
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002694 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002695 // the literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002696 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002697 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002698 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00002699 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002700 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002701 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00002702 __ mov(r0, Operand(node_->constant_properties()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002703 __ push(r0);
2704 __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
2705 __ mov(r2, Operand(r0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002706 // Result is returned in r2.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002707}
2708
2709
ager@chromium.org7c537e22008-10-16 08:43:32 +00002710void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002711#ifdef DEBUG
2712 int original_height = frame_->height();
2713#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002714 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002715 Comment cmnt(masm_, "[ ObjectLiteral");
2716
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002717 DeferredObjectLiteral* deferred = new DeferredObjectLiteral(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002718
2719 // Retrieve the literal array and check the allocated entry.
2720
2721 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002722 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002723
2724 // Load the literals array of the function.
2725 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2726
2727 // Load the literal at the ast saved index.
2728 int literal_offset =
2729 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2730 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2731
2732 // Check whether we need to materialize the object literal boilerplate.
2733 // If so, jump to the deferred code.
2734 __ cmp(r2, Operand(Factory::undefined_value()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002735 deferred->Branch(eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002736 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002737
2738 // Push the object literal boilerplate.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002739 frame_->EmitPush(r2);
mads.s.ager31e71382008-08-13 09:32:07 +00002740
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002741 // Clone the boilerplate object.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002742 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2743 if (node->depth() == 1) {
2744 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2745 }
2746 frame_->CallRuntime(clone_function_id, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002747 frame_->EmitPush(r0); // save the result
mads.s.ager31e71382008-08-13 09:32:07 +00002748 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002749
2750 for (int i = 0; i < node->properties()->length(); i++) {
2751 ObjectLiteral::Property* property = node->properties()->at(i);
2752 Literal* key = property->key();
2753 Expression* value = property->value();
2754 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002755 case ObjectLiteral::Property::CONSTANT:
2756 break;
2757 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2758 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
2759 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002760 case ObjectLiteral::Property::COMPUTED: // fall through
2761 case ObjectLiteral::Property::PROTOTYPE: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002762 frame_->EmitPush(r0); // dup the result
2763 LoadAndSpill(key);
2764 LoadAndSpill(value);
2765 frame_->CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002766 // restore r0
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002767 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002768 break;
2769 }
2770 case ObjectLiteral::Property::SETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002771 frame_->EmitPush(r0);
2772 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002773 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002774 frame_->EmitPush(r0);
2775 LoadAndSpill(value);
2776 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002777 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002778 break;
2779 }
2780 case ObjectLiteral::Property::GETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002781 frame_->EmitPush(r0);
2782 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002783 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002784 frame_->EmitPush(r0);
2785 LoadAndSpill(value);
2786 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002787 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002788 break;
2789 }
2790 }
2791 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002792 ASSERT(frame_->height() == original_height + 1);
2793}
2794
2795
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002796// This deferred code stub will be used for creating the boilerplate
2797// by calling Runtime_CreateArrayLiteralBoilerplate.
2798// Each created boilerplate is stored in the JSFunction and they are
2799// therefore context dependent.
2800class DeferredArrayLiteral: public DeferredCode {
2801 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002802 explicit DeferredArrayLiteral(ArrayLiteral* node) : node_(node) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002803 set_comment("[ DeferredArrayLiteral");
2804 }
2805
2806 virtual void Generate();
2807
2808 private:
2809 ArrayLiteral* node_;
2810};
2811
2812
2813void DeferredArrayLiteral::Generate() {
2814 // Argument is passed in r1.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002815
2816 // If the entry is undefined we call the runtime system to computed
2817 // the literal.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002818 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002819 __ push(r1);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002820 // Literal index (1).
2821 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002822 __ push(r0);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002823 // Constant properties (2).
2824 __ mov(r0, Operand(node_->literals()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002825 __ push(r0);
2826 __ CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3);
2827 __ mov(r2, Operand(r0));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002828 // Result is returned in r2.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002829}
2830
2831
ager@chromium.org7c537e22008-10-16 08:43:32 +00002832void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002833#ifdef DEBUG
2834 int original_height = frame_->height();
2835#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002836 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002837 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002838
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002839 DeferredArrayLiteral* deferred = new DeferredArrayLiteral(node);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002840
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002841 // Retrieve the literal array and check the allocated entry.
2842
2843 // Load the function of this activation.
2844 __ ldr(r1, frame_->Function());
2845
2846 // Load the literals array of the function.
2847 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2848
2849 // Load the literal at the ast saved index.
2850 int literal_offset =
2851 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2852 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2853
2854 // Check whether we need to materialize the object literal boilerplate.
2855 // If so, jump to the deferred code.
2856 __ cmp(r2, Operand(Factory::undefined_value()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002857 deferred->Branch(eq);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002858 deferred->BindExit();
2859
2860 // Push the object literal boilerplate.
2861 frame_->EmitPush(r2);
2862
2863 // Clone the boilerplate object.
2864 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2865 if (node->depth() == 1) {
2866 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2867 }
2868 frame_->CallRuntime(clone_function_id, 1);
2869 frame_->EmitPush(r0); // save the result
2870 // r0: cloned object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002871
2872 // Generate code to set the elements in the array that are not
2873 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002874 for (int i = 0; i < node->values()->length(); i++) {
2875 Expression* value = node->values()->at(i);
2876
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002877 // If value is a literal the property value is already set in the
2878 // boilerplate object.
2879 if (value->AsLiteral() != NULL) continue;
2880 // If value is a materialized literal the property value is already set
2881 // in the boilerplate object if it is simple.
2882 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002883
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002884 // The property must be set by generated code.
2885 LoadAndSpill(value);
2886 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002887
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002888 // Fetch the object literal.
2889 __ ldr(r1, frame_->Top());
2890 // Get the elements array.
2891 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002892
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002893 // Write to the indexed properties array.
2894 int offset = i * kPointerSize + Array::kHeaderSize;
2895 __ str(r0, FieldMemOperand(r1, offset));
2896
2897 // Update the write barrier for the array address.
2898 __ mov(r3, Operand(offset));
2899 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002900 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002901 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002902}
2903
2904
ager@chromium.org32912102009-01-16 10:38:43 +00002905void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002906#ifdef DEBUG
2907 int original_height = frame_->height();
2908#endif
2909 ASSERT(!in_spilled_code());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002910 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org32912102009-01-16 10:38:43 +00002911 // Call runtime routine to allocate the catch extension object and
2912 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002913 Comment cmnt(masm_, "[ CatchExtensionObject");
2914 LoadAndSpill(node->key());
2915 LoadAndSpill(node->value());
2916 Result result =
2917 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2918 frame_->EmitPush(result.reg());
2919 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002920}
2921
2922
ager@chromium.org7c537e22008-10-16 08:43:32 +00002923void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002924#ifdef DEBUG
2925 int original_height = frame_->height();
2926#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002927 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002928 Comment cmnt(masm_, "[ Assignment");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002929 CodeForStatementPosition(node);
mads.s.ager31e71382008-08-13 09:32:07 +00002930
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002931 { Reference target(this, node->target());
2932 if (target.is_illegal()) {
2933 // Fool the virtual frame into thinking that we left the assignment's
2934 // value on the frame.
2935 __ mov(r0, Operand(Smi::FromInt(0)));
2936 frame_->EmitPush(r0);
2937 ASSERT(frame_->height() == original_height + 1);
2938 return;
2939 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002940
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002941 if (node->op() == Token::ASSIGN ||
2942 node->op() == Token::INIT_VAR ||
2943 node->op() == Token::INIT_CONST) {
2944 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002945
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002946 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002947 // +=, *= and similar binary assignments.
2948 // Get the old value of the lhs.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002949 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
2950 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002951 bool overwrite =
2952 (node->value()->AsBinaryOperation() != NULL &&
2953 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002954 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002955 SmiOperation(node->binary_op(),
2956 literal->handle(),
2957 false,
2958 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002959 frame_->EmitPush(r0);
2960
2961 } else {
2962 LoadAndSpill(node->value());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002963 GenericBinaryOperation(node->binary_op(),
2964 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002965 frame_->EmitPush(r0);
2966 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002967 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002968
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002969 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2970 if (var != NULL &&
2971 (var->mode() == Variable::CONST) &&
2972 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2973 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002974
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002975 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002976 CodeForSourcePosition(node->position());
2977 if (node->op() == Token::INIT_CONST) {
2978 // Dynamic constant initializations must use the function context
2979 // and initialize the actual constant declared. Dynamic variable
2980 // initializations are simply assignments and use SetValue.
2981 target.SetValue(CONST_INIT);
2982 } else {
2983 target.SetValue(NOT_CONST_INIT);
2984 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002985 }
2986 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002987 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002988}
2989
2990
ager@chromium.org7c537e22008-10-16 08:43:32 +00002991void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002992#ifdef DEBUG
2993 int original_height = frame_->height();
2994#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002995 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002996 Comment cmnt(masm_, "[ Throw");
2997
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002998 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002999 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003000 frame_->CallRuntime(Runtime::kThrow, 1);
3001 frame_->EmitPush(r0);
3002 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003003}
3004
3005
ager@chromium.org7c537e22008-10-16 08:43:32 +00003006void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003007#ifdef DEBUG
3008 int original_height = frame_->height();
3009#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003010 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003011 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003012
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003013 { Reference property(this, node);
3014 property.GetValueAndSpill(typeof_state());
3015 }
3016 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003017}
3018
3019
ager@chromium.org7c537e22008-10-16 08:43:32 +00003020void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003021#ifdef DEBUG
3022 int original_height = frame_->height();
3023#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003024 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003025 Comment cmnt(masm_, "[ Call");
3026
3027 ZoneList<Expression*>* args = node->arguments();
3028
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003029 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003030 // Standard function call.
3031
3032 // Check if the function is a variable or a property.
3033 Expression* function = node->expression();
3034 Variable* var = function->AsVariableProxy()->AsVariable();
3035 Property* property = function->AsProperty();
3036
3037 // ------------------------------------------------------------------------
3038 // Fast-case: Use inline caching.
3039 // ---
3040 // According to ECMA-262, section 11.2.3, page 44, the function to call
3041 // must be resolved after the arguments have been evaluated. The IC code
3042 // automatically handles this by loading the arguments before the function
3043 // is resolved in cache misses (this also holds for megamorphic calls).
3044 // ------------------------------------------------------------------------
3045
3046 if (var != NULL && !var->is_this() && var->is_global()) {
3047 // ----------------------------------
3048 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3049 // ----------------------------------
3050
3051 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003052 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003053 frame_->EmitPush(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003054
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003055 // Pass the global object as the receiver and let the IC stub
3056 // patch the stack to use the global proxy as 'this' in the
3057 // invoked function.
3058 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003059
3060 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003061 int arg_count = args->length();
3062 for (int i = 0; i < arg_count; i++) {
3063 LoadAndSpill(args->at(i));
3064 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003065
3066 // Setup the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003067 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3068 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003069 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003070 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3071 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003072 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003073 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003074 frame_->Drop();
3075 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003076
3077 } else if (var != NULL && var->slot() != NULL &&
3078 var->slot()->type() == Slot::LOOKUP) {
3079 // ----------------------------------
3080 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3081 // ----------------------------------
3082
3083 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003084 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003085 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003086 frame_->EmitPush(r0);
3087 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003088 // r0: slot value; r1: receiver
3089
3090 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003091 frame_->EmitPush(r0); // function
3092 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003093
3094 // Call the function.
3095 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003096 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003097
3098 } else if (property != NULL) {
3099 // Check if the key is a literal string.
3100 Literal* literal = property->key()->AsLiteral();
3101
3102 if (literal != NULL && literal->handle()->IsSymbol()) {
3103 // ------------------------------------------------------------------
3104 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3105 // ------------------------------------------------------------------
3106
3107 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003108 __ mov(r0, Operand(literal->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003109 frame_->EmitPush(r0);
3110 LoadAndSpill(property->obj());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003111
3112 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003113 int arg_count = args->length();
3114 for (int i = 0; i < arg_count; i++) {
3115 LoadAndSpill(args->at(i));
3116 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003117
3118 // Set the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003119 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3120 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003121 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003122 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003123 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003124
3125 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003126 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00003127
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003128 frame_->EmitPush(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003129
3130 } else {
3131 // -------------------------------------------
3132 // JavaScript example: 'array[index](1, 2, 3)'
3133 // -------------------------------------------
3134
3135 // Load the function to call from the property through a reference.
3136 Reference ref(this, property);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003137 ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003138
3139 // Pass receiver to called function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003140 if (property->is_synthetic()) {
3141 LoadGlobalReceiver(r0);
3142 } else {
3143 __ ldr(r0, frame_->ElementAt(ref.size()));
3144 frame_->EmitPush(r0);
3145 }
3146
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003147 // Call the function.
3148 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003149 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003150 }
3151
3152 } else {
3153 // ----------------------------------
3154 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3155 // ----------------------------------
3156
3157 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003158 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003159
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003160 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003161 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003162
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003163 // Call the function.
3164 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003165 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003166 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003167 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003168}
3169
3170
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003171void CodeGenerator::VisitCallEval(CallEval* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003172#ifdef DEBUG
3173 int original_height = frame_->height();
3174#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003175 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003176 Comment cmnt(masm_, "[ CallEval");
3177
3178 // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve
3179 // the function we need to call and the receiver of the call.
3180 // Then we call the resolved function using the given arguments.
3181
3182 ZoneList<Expression*>* args = node->arguments();
3183 Expression* function = node->expression();
3184
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003185 CodeForStatementPosition(node);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003186
3187 // Prepare stack for call to resolved function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003188 LoadAndSpill(function);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003189 __ mov(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003190 frame_->EmitPush(r2); // Slot for receiver
3191 int arg_count = args->length();
3192 for (int i = 0; i < arg_count; i++) {
3193 LoadAndSpill(args->at(i));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003194 }
3195
3196 // Prepare stack for call to ResolvePossiblyDirectEval.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003197 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3198 frame_->EmitPush(r1);
3199 if (arg_count > 0) {
3200 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3201 frame_->EmitPush(r1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003202 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003203 frame_->EmitPush(r2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003204 }
3205
3206 // Resolve the call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003207 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003208
3209 // Touch up stack with the right values for the function and the receiver.
3210 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003211 __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003212 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003213 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003214
3215 // Call the function.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003216 CodeForSourcePosition(node->position());
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003217
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003218 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3219 CallFunctionStub call_function(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003220 frame_->CallStub(&call_function, arg_count + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003221
3222 __ ldr(cp, frame_->Context());
3223 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003224 frame_->Drop();
3225 frame_->EmitPush(r0);
3226 ASSERT(frame_->height() == original_height + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003227}
3228
3229
ager@chromium.org7c537e22008-10-16 08:43:32 +00003230void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003231#ifdef DEBUG
3232 int original_height = frame_->height();
3233#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003234 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003235 Comment cmnt(masm_, "[ CallNew");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003236 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003237
3238 // According to ECMA-262, section 11.2.2, page 44, the function
3239 // expression in new calls must be evaluated before the
3240 // arguments. This is different from ordinary calls, where the
3241 // actual function to call is resolved after the arguments have been
3242 // evaluated.
3243
3244 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003245 // receiver. There is no need to use the global proxy here because
3246 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003247 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003248 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003249
3250 // Push the arguments ("left-to-right") on the stack.
3251 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003252 int arg_count = args->length();
3253 for (int i = 0; i < arg_count; i++) {
3254 LoadAndSpill(args->at(i));
3255 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003256
mads.s.ager31e71382008-08-13 09:32:07 +00003257 // r0: the number of arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003258 Result num_args = allocator_->Allocate(r0);
3259 ASSERT(num_args.is_valid());
3260 __ mov(num_args.reg(), Operand(arg_count));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003261
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003262 // Load the function into r1 as per calling convention.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003263 Result function = allocator_->Allocate(r1);
3264 ASSERT(function.is_valid());
3265 __ ldr(function.reg(), frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003266
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003267 // Call the construct call builtin that handles allocation and
3268 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003269 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003270 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
3271 Result result = frame_->CallCodeObject(ic,
3272 RelocInfo::CONSTRUCT_CALL,
3273 &num_args,
3274 &function,
3275 arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003276
3277 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003278 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003279 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003280}
3281
3282
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003283void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
3284 VirtualFrame::SpilledScope spilled_scope;
3285 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003286 JumpTarget leave, null, function, non_function_constructor;
3287
3288 // Load the object into r0.
3289 LoadAndSpill(args->at(0));
3290 frame_->EmitPop(r0);
3291
3292 // If the object is a smi, we return null.
3293 __ tst(r0, Operand(kSmiTagMask));
3294 null.Branch(eq);
3295
3296 // Check that the object is a JS object but take special care of JS
3297 // functions to make sure they have 'Function' as their class.
3298 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
3299 null.Branch(lt);
3300
3301 // As long as JS_FUNCTION_TYPE is the last instance type and it is
3302 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
3303 // LAST_JS_OBJECT_TYPE.
3304 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3305 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
3306 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3307 function.Branch(eq);
3308
3309 // Check if the constructor in the map is a function.
3310 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
3311 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
3312 non_function_constructor.Branch(ne);
3313
3314 // The r0 register now contains the constructor function. Grab the
3315 // instance class name from there.
3316 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
3317 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003318 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003319 leave.Jump();
3320
3321 // Functions have class 'Function'.
3322 function.Bind();
3323 __ mov(r0, Operand(Factory::function_class_symbol()));
3324 frame_->EmitPush(r0);
3325 leave.Jump();
3326
3327 // Objects with a non-function constructor have class 'Object'.
3328 non_function_constructor.Bind();
3329 __ mov(r0, Operand(Factory::Object_symbol()));
3330 frame_->EmitPush(r0);
3331 leave.Jump();
3332
3333 // Non-JS objects have class null.
3334 null.Bind();
3335 __ mov(r0, Operand(Factory::null_value()));
3336 frame_->EmitPush(r0);
3337
3338 // All done.
3339 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003340}
3341
3342
ager@chromium.org7c537e22008-10-16 08:43:32 +00003343void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003344 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003345 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003346 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003347 LoadAndSpill(args->at(0));
3348 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003349 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003350 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003351 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003352 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3353 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003354 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003355 // Load the value.
3356 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003357 leave.Bind();
3358 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003359}
3360
3361
ager@chromium.org7c537e22008-10-16 08:43:32 +00003362void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003363 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003364 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003365 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003366 LoadAndSpill(args->at(0)); // Load the object.
3367 LoadAndSpill(args->at(1)); // Load the value.
3368 frame_->EmitPop(r0); // r0 contains value
3369 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003370 // if (object->IsSmi()) return object.
3371 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003372 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003373 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3374 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003375 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003376 // Store the value.
3377 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3378 // Update the write barrier.
3379 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3380 __ RecordWrite(r1, r2, r3);
3381 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003382 leave.Bind();
3383 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003384}
3385
3386
ager@chromium.org7c537e22008-10-16 08:43:32 +00003387void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003388 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003389 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003390 LoadAndSpill(args->at(0));
3391 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003392 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003393 cc_reg_ = eq;
3394}
3395
3396
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003397void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003398 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003399 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3400 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003401#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003402 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003403 LoadAndSpill(args->at(1));
3404 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003405 __ CallRuntime(Runtime::kLog, 2);
3406 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003407#endif
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003408 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003409 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003410}
3411
3412
ager@chromium.org7c537e22008-10-16 08:43:32 +00003413void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003414 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003415 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003416 LoadAndSpill(args->at(0));
3417 frame_->EmitPop(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003418 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003419 cc_reg_ = eq;
3420}
3421
3422
kasper.lund7276f142008-07-30 08:49:36 +00003423// This should generate code that performs a charCodeAt() call or returns
3424// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3425// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003426void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003427 VirtualFrame::SpilledScope spilled_scope;
kasper.lund7276f142008-07-30 08:49:36 +00003428 ASSERT(args->length() == 2);
kasper.lund7276f142008-07-30 08:49:36 +00003429 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003430 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003431}
3432
3433
ager@chromium.org7c537e22008-10-16 08:43:32 +00003434void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003435 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003436 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003437 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003438 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003439 // We need the CC bits to come out as not_equal in the case where the
3440 // object is a smi. This can't be done with the usual test opcode so
3441 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003442 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003443 __ and_(r1, r0, Operand(kSmiTagMask));
3444 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003445 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003446 // It is a heap object - get the map. Check if the object is a JS array.
3447 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003448 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003449 cc_reg_ = eq;
3450}
3451
3452
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003453void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
3454 VirtualFrame::SpilledScope spilled_scope;
3455 ASSERT(args->length() == 0);
3456 frame_->CallRuntime(Runtime::kIsConstructCall, 0);
3457 frame_->EmitPush(r0);
3458}
3459
3460
ager@chromium.org7c537e22008-10-16 08:43:32 +00003461void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003462 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003463 ASSERT(args->length() == 0);
3464
mads.s.ager31e71382008-08-13 09:32:07 +00003465 // Seed the result with the formal parameters count, which will be used
3466 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003467 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
3468
3469 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003470 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003471 frame_->CallStub(&stub, 0);
3472 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003473}
3474
3475
ager@chromium.org7c537e22008-10-16 08:43:32 +00003476void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003477 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003478 ASSERT(args->length() == 1);
3479
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003480 // Satisfy contract with ArgumentsAccessStub:
3481 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003482 LoadAndSpill(args->at(0));
3483 frame_->EmitPop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003484 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003485
3486 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003487 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003488 frame_->CallStub(&stub, 0);
3489 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003490}
3491
3492
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003493void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) {
3494 VirtualFrame::SpilledScope spilled_scope;
3495 ASSERT(args->length() == 0);
3496 __ Call(ExternalReference::random_positive_smi_function().address(),
3497 RelocInfo::RUNTIME_ENTRY);
3498 frame_->EmitPush(r0);
3499}
3500
3501
3502void CodeGenerator::GenerateFastMathOp(MathOp op, ZoneList<Expression*>* args) {
3503 VirtualFrame::SpilledScope spilled_scope;
3504 LoadAndSpill(args->at(0));
3505 switch (op) {
3506 case SIN:
3507 frame_->CallRuntime(Runtime::kMath_sin, 1);
3508 break;
3509 case COS:
3510 frame_->CallRuntime(Runtime::kMath_cos, 1);
3511 break;
3512 }
3513 frame_->EmitPush(r0);
3514}
3515
3516
ager@chromium.org7c537e22008-10-16 08:43:32 +00003517void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003518 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003519 ASSERT(args->length() == 2);
3520
3521 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003522 LoadAndSpill(args->at(0));
3523 LoadAndSpill(args->at(1));
3524 frame_->EmitPop(r0);
3525 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003526 __ cmp(r0, Operand(r1));
3527 cc_reg_ = eq;
3528}
3529
3530
ager@chromium.org7c537e22008-10-16 08:43:32 +00003531void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003532#ifdef DEBUG
3533 int original_height = frame_->height();
3534#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003535 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003536 if (CheckForInlineRuntimeCall(node)) {
3537 ASSERT((has_cc() && frame_->height() == original_height) ||
3538 (!has_cc() && frame_->height() == original_height + 1));
3539 return;
3540 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003541
3542 ZoneList<Expression*>* args = node->arguments();
3543 Comment cmnt(masm_, "[ CallRuntime");
3544 Runtime::Function* function = node->function();
3545
ager@chromium.org41826e72009-03-30 13:30:57 +00003546 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003547 // Prepare stack for calling JS runtime function.
3548 __ mov(r0, Operand(node->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003549 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003550 // Push the builtins object found in the current global object.
3551 __ ldr(r1, GlobalObject());
3552 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003553 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003554 }
mads.s.ager31e71382008-08-13 09:32:07 +00003555
ager@chromium.org41826e72009-03-30 13:30:57 +00003556 // Push the arguments ("left-to-right").
3557 int arg_count = args->length();
3558 for (int i = 0; i < arg_count; i++) {
3559 LoadAndSpill(args->at(i));
3560 }
mads.s.ager31e71382008-08-13 09:32:07 +00003561
ager@chromium.org41826e72009-03-30 13:30:57 +00003562 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003563 // Call the JS runtime function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003564 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3565 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003566 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003567 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003568 frame_->Drop();
3569 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003570 } else {
3571 // Call the C runtime function.
3572 frame_->CallRuntime(function, arg_count);
3573 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003574 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003575 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003576}
3577
3578
ager@chromium.org7c537e22008-10-16 08:43:32 +00003579void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003580#ifdef DEBUG
3581 int original_height = frame_->height();
3582#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003583 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003584 Comment cmnt(masm_, "[ UnaryOperation");
3585
3586 Token::Value op = node->op();
3587
3588 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003589 LoadConditionAndSpill(node->expression(),
3590 NOT_INSIDE_TYPEOF,
3591 false_target(),
3592 true_target(),
3593 true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003594 cc_reg_ = NegateCondition(cc_reg_);
3595
3596 } else if (op == Token::DELETE) {
3597 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003598 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003599 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003600 LoadAndSpill(property->obj());
3601 LoadAndSpill(property->key());
3602 Result arg_count = allocator_->Allocate(r0);
3603 ASSERT(arg_count.is_valid());
3604 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3605 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003606
mads.s.ager31e71382008-08-13 09:32:07 +00003607 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003608 Slot* slot = variable->slot();
3609 if (variable->is_global()) {
3610 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003611 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003612 frame_->EmitPush(r0);
3613 Result arg_count = allocator_->Allocate(r0);
3614 ASSERT(arg_count.is_valid());
3615 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3616 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003617
3618 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3619 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003620 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003621 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003622 frame_->EmitPush(r0);
3623 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003624 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003625 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003626 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003627 frame_->EmitPush(r0);
3628 Result arg_count = allocator_->Allocate(r0);
3629 ASSERT(arg_count.is_valid());
3630 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3631 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003632
mads.s.ager31e71382008-08-13 09:32:07 +00003633 } else {
3634 // Default: Result of deleting non-global, not dynamically
3635 // introduced variables is false.
3636 __ mov(r0, Operand(Factory::false_value()));
3637 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003638
3639 } else {
3640 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003641 LoadAndSpill(node->expression()); // may have side-effects
3642 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003643 __ mov(r0, Operand(Factory::true_value()));
3644 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003645 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003646
3647 } else if (op == Token::TYPEOF) {
3648 // Special case for loading the typeof expression; see comment on
3649 // LoadTypeofExpression().
3650 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003651 frame_->CallRuntime(Runtime::kTypeof, 1);
3652 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003653
3654 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003655 LoadAndSpill(node->expression());
3656 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003657 switch (op) {
3658 case Token::NOT:
3659 case Token::DELETE:
3660 case Token::TYPEOF:
3661 UNREACHABLE(); // handled above
3662 break;
3663
3664 case Token::SUB: {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003665 bool overwrite =
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003666 (node->expression()->AsBinaryOperation() != NULL &&
3667 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003668 UnarySubStub stub(overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003669 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003670 break;
3671 }
3672
3673 case Token::BIT_NOT: {
3674 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003675 JumpTarget smi_label;
3676 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003677 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003678 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003679
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003680 frame_->EmitPush(r0);
3681 Result arg_count = allocator_->Allocate(r0);
3682 ASSERT(arg_count.is_valid());
3683 __ mov(arg_count.reg(), Operand(0)); // not counting receiver
3684 frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003685
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003686 continue_label.Jump();
3687 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003688 __ mvn(r0, Operand(r0));
3689 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003690 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003691 break;
3692 }
3693
3694 case Token::VOID:
3695 // since the stack top is cached in r0, popping and then
3696 // pushing a value can be done by just writing to r0.
3697 __ mov(r0, Operand(Factory::undefined_value()));
3698 break;
3699
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003700 case Token::ADD: {
3701 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003702 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003703 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003704 continue_label.Branch(eq);
3705 frame_->EmitPush(r0);
3706 Result arg_count = allocator_->Allocate(r0);
3707 ASSERT(arg_count.is_valid());
3708 __ mov(arg_count.reg(), Operand(0)); // not counting receiver
3709 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3710 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003711 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003712 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003713 default:
3714 UNREACHABLE();
3715 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003716 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003717 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003718 ASSERT((has_cc() && frame_->height() == original_height) ||
3719 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003720}
3721
3722
ager@chromium.org7c537e22008-10-16 08:43:32 +00003723void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003724#ifdef DEBUG
3725 int original_height = frame_->height();
3726#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003727 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003728 Comment cmnt(masm_, "[ CountOperation");
3729
3730 bool is_postfix = node->is_postfix();
3731 bool is_increment = node->op() == Token::INC;
3732
3733 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3734 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3735
3736 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003737 if (is_postfix) {
3738 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003739 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003740 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003741
3742 { Reference target(this, node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003743 if (target.is_illegal()) {
3744 // Spoof the virtual frame to have the expected height (one higher
3745 // than on entry).
3746 if (!is_postfix) {
3747 __ mov(r0, Operand(Smi::FromInt(0)));
3748 frame_->EmitPush(r0);
3749 }
3750 ASSERT(frame_->height() == original_height + 1);
3751 return;
3752 }
3753 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
3754 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003755
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003756 JumpTarget slow;
3757 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003758
3759 // Load the value (1) into register r1.
3760 __ mov(r1, Operand(Smi::FromInt(1)));
3761
3762 // Check for smi operand.
3763 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003764 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003765
3766 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003767 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003768 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003769 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003770
3771 // Perform optimistic increment/decrement.
3772 if (is_increment) {
3773 __ add(r0, r0, Operand(r1), SetCC);
3774 } else {
3775 __ sub(r0, r0, Operand(r1), SetCC);
3776 }
3777
3778 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003779 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003780
3781 // Revert optimistic increment/decrement.
3782 if (is_increment) {
3783 __ sub(r0, r0, Operand(r1));
3784 } else {
3785 __ add(r0, r0, Operand(r1));
3786 }
3787
3788 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003789 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003790 {
3791 // Convert the operand to a number.
3792 frame_->EmitPush(r0);
3793 Result arg_count = allocator_->Allocate(r0);
3794 ASSERT(arg_count.is_valid());
3795 __ mov(arg_count.reg(), Operand(0));
3796 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3797 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003798 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003799 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003800 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003801 }
3802
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003803 // Compute the new value.
3804 __ mov(r1, Operand(Smi::FromInt(1)));
3805 frame_->EmitPush(r0);
3806 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003807 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003808 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003809 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003810 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003811 }
3812
3813 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003814 exit.Bind();
3815 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003816 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003817 }
3818
3819 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003820 if (is_postfix) frame_->EmitPop(r0);
3821 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003822}
3823
3824
ager@chromium.org7c537e22008-10-16 08:43:32 +00003825void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003826#ifdef DEBUG
3827 int original_height = frame_->height();
3828#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003829 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003830 Comment cmnt(masm_, "[ BinaryOperation");
3831 Token::Value op = node->op();
3832
3833 // According to ECMA-262 section 11.11, page 58, the binary logical
3834 // operators must yield the result of one of the two expressions
3835 // before any ToBoolean() conversions. This means that the value
3836 // produced by a && or || operator is not necessarily a boolean.
3837
3838 // NOTE: If the left hand side produces a materialized value (not in
3839 // the CC register), we force the right hand side to do the
3840 // same. This is necessary because we may have to branch to the exit
3841 // after evaluating the left hand side (due to the shortcut
3842 // semantics), but the compiler must (statically) know if the result
3843 // of compiling the binary operation is materialized or not.
3844
3845 if (op == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003846 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003847 LoadConditionAndSpill(node->left(),
3848 NOT_INSIDE_TYPEOF,
3849 &is_true,
3850 false_target(),
3851 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003852 if (has_cc()) {
3853 Branch(false, false_target());
3854
3855 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003856 is_true.Bind();
3857 LoadConditionAndSpill(node->right(),
3858 NOT_INSIDE_TYPEOF,
3859 true_target(),
3860 false_target(),
3861 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003862
3863 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003864 JumpTarget pop_and_continue;
3865 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003866
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003867 __ ldr(r0, frame_->Top()); // dup the stack top
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003868 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003869 // Avoid popping the result if it converts to 'false' using the
3870 // standard ToBoolean() conversion as described in ECMA-262,
3871 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003872 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003873 Branch(false, &exit);
3874
3875 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003876 pop_and_continue.Bind();
3877 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003878
3879 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003880 is_true.Bind();
3881 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003882
3883 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003884 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003885 }
3886
3887 } else if (op == Token::OR) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003888 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003889 LoadConditionAndSpill(node->left(),
3890 NOT_INSIDE_TYPEOF,
3891 true_target(),
3892 &is_false,
3893 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003894 if (has_cc()) {
3895 Branch(true, true_target());
3896
3897 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003898 is_false.Bind();
3899 LoadConditionAndSpill(node->right(),
3900 NOT_INSIDE_TYPEOF,
3901 true_target(),
3902 false_target(),
3903 false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003904
3905 } else {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003906 JumpTarget pop_and_continue;
3907 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003908
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003909 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003910 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003911 // Avoid popping the result if it converts to 'true' using the
3912 // standard ToBoolean() conversion as described in ECMA-262,
3913 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003914 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003915 Branch(true, &exit);
3916
3917 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003918 pop_and_continue.Bind();
3919 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003920
3921 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003922 is_false.Bind();
3923 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003924
3925 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003926 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003927 }
3928
3929 } else {
3930 // Optimize for the case where (at least) one of the expressions
3931 // is a literal small integer.
3932 Literal* lliteral = node->left()->AsLiteral();
3933 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003934 // NOTE: The code below assumes that the slow cases (calls to runtime)
3935 // never return a constant/immutable object.
3936 bool overwrite_left =
3937 (node->left()->AsBinaryOperation() != NULL &&
3938 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
3939 bool overwrite_right =
3940 (node->right()->AsBinaryOperation() != NULL &&
3941 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003942
3943 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003944 LoadAndSpill(node->left());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003945 SmiOperation(node->op(),
3946 rliteral->handle(),
3947 false,
3948 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003949
3950 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003951 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003952 SmiOperation(node->op(),
3953 lliteral->handle(),
3954 true,
3955 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003956
3957 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003958 OverwriteMode overwrite_mode = NO_OVERWRITE;
3959 if (overwrite_left) {
3960 overwrite_mode = OVERWRITE_LEFT;
3961 } else if (overwrite_right) {
3962 overwrite_mode = OVERWRITE_RIGHT;
3963 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003964 LoadAndSpill(node->left());
3965 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003966 GenericBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003967 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003968 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003969 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003970 ASSERT((has_cc() && frame_->height() == original_height) ||
3971 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003972}
3973
3974
ager@chromium.org7c537e22008-10-16 08:43:32 +00003975void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003976#ifdef DEBUG
3977 int original_height = frame_->height();
3978#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003979 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003980 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003981 frame_->EmitPush(r0);
3982 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003983}
3984
3985
ager@chromium.org7c537e22008-10-16 08:43:32 +00003986void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003987#ifdef DEBUG
3988 int original_height = frame_->height();
3989#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003990 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003991 Comment cmnt(masm_, "[ CompareOperation");
3992
3993 // Get the expressions from the node.
3994 Expression* left = node->left();
3995 Expression* right = node->right();
3996 Token::Value op = node->op();
3997
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003998 // To make null checks efficient, we check if either left or right is the
3999 // literal 'null'. If so, we optimize the code by inlining a null check
4000 // instead of calling the (very) general runtime routine for checking
4001 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004002 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004003 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004004 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004005 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004006 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
4007 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004008 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004009 LoadAndSpill(left_is_null ? right : left);
4010 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004011 __ cmp(r0, Operand(Factory::null_value()));
4012
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004013 // The 'null' value is only equal to 'undefined' if using non-strict
4014 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004015 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004016 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004017
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004018 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004019 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004020
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004021 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004022 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004023
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004024 // It can be an undetectable object.
4025 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
4026 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
4027 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
4028 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004029 }
4030
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004031 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004032 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004033 return;
4034 }
4035 }
4036
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004037 // To make typeof testing for natives implemented in JavaScript really
4038 // efficient, we generate special code for expressions of the form:
4039 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004040 UnaryOperation* operation = left->AsUnaryOperation();
4041 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
4042 (operation != NULL && operation->op() == Token::TYPEOF) &&
4043 (right->AsLiteral() != NULL &&
4044 right->AsLiteral()->handle()->IsString())) {
4045 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
4046
mads.s.ager31e71382008-08-13 09:32:07 +00004047 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004048 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004049 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004050
4051 if (check->Equals(Heap::number_symbol())) {
4052 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004053 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004054 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4055 __ cmp(r1, Operand(Factory::heap_number_map()));
4056 cc_reg_ = eq;
4057
4058 } else if (check->Equals(Heap::string_symbol())) {
4059 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004060 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004061
4062 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4063
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004064 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004065 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4066 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4067 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004068 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004069
4070 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
4071 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
4072 cc_reg_ = lt;
4073
4074 } else if (check->Equals(Heap::boolean_symbol())) {
4075 __ cmp(r1, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004076 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004077 __ cmp(r1, Operand(Factory::false_value()));
4078 cc_reg_ = eq;
4079
4080 } else if (check->Equals(Heap::undefined_symbol())) {
4081 __ cmp(r1, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004082 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004083
4084 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004085 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004086
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004087 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004088 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4089 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4090 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4091 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4092
4093 cc_reg_ = eq;
4094
4095 } else if (check->Equals(Heap::function_symbol())) {
4096 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004097 false_target()->Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004098 __ CompareObjectType(r1, r1, r1, JS_FUNCTION_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004099 cc_reg_ = eq;
4100
4101 } else if (check->Equals(Heap::object_symbol())) {
4102 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004103 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004104
4105 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4106 __ cmp(r1, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004107 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004108
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004109 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004110 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
4111 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4112 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004113 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004114
4115 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4116 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004117 false_target()->Branch(lt);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004118 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
4119 cc_reg_ = le;
4120
4121 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004122 // Uncommon case: typeof testing against a string literal that is
4123 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004124 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004125 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004126 ASSERT(!has_valid_frame() ||
4127 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004128 return;
4129 }
4130
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004131 switch (op) {
4132 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004133 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004134 break;
4135
4136 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004137 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004138 break;
4139
4140 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004141 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004142 break;
4143
4144 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004145 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004146 break;
4147
4148 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004149 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004150 break;
4151
4152 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004153 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004154 break;
4155
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004156 case Token::IN: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004157 LoadAndSpill(left);
4158 LoadAndSpill(right);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004159 Result arg_count = allocator_->Allocate(r0);
4160 ASSERT(arg_count.is_valid());
4161 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
4162 Result result = frame_->InvokeBuiltin(Builtins::IN,
4163 CALL_JS,
4164 &arg_count,
4165 2);
4166 frame_->EmitPush(result.reg());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004167 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004168 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004169
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004170 case Token::INSTANCEOF: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004171 LoadAndSpill(left);
4172 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004173 InstanceofStub stub;
4174 Result result = frame_->CallStub(&stub, 2);
4175 // At this point if instanceof succeeded then r0 == 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004176 __ tst(result.reg(), Operand(result.reg()));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004177 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004178 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004179 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004180
4181 default:
4182 UNREACHABLE();
4183 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004184 ASSERT((has_cc() && frame_->height() == original_height) ||
4185 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004186}
4187
4188
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004189#ifdef DEBUG
4190bool CodeGenerator::HasValidEntryRegisters() { return true; }
4191#endif
4192
4193
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004194#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004195#define __ ACCESS_MASM(masm)
4196
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004197
ager@chromium.org7c537e22008-10-16 08:43:32 +00004198Handle<String> Reference::GetName() {
4199 ASSERT(type_ == NAMED);
4200 Property* property = expression_->AsProperty();
4201 if (property == NULL) {
4202 // Global variable reference treated as a named property reference.
4203 VariableProxy* proxy = expression_->AsVariableProxy();
4204 ASSERT(proxy->AsVariable() != NULL);
4205 ASSERT(proxy->AsVariable()->is_global());
4206 return proxy->name();
4207 } else {
4208 Literal* raw_name = property->key()->AsLiteral();
4209 ASSERT(raw_name != NULL);
4210 return Handle<String>(String::cast(*raw_name->handle()));
4211 }
4212}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004213
ager@chromium.org7c537e22008-10-16 08:43:32 +00004214
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004215void Reference::GetValueAndSpill(TypeofState typeof_state) {
4216 ASSERT(cgen_->in_spilled_code());
4217 cgen_->set_in_spilled_code(false);
4218 GetValue(typeof_state);
4219 cgen_->frame()->SpillAll();
4220 cgen_->set_in_spilled_code(true);
4221}
4222
4223
ager@chromium.org7c537e22008-10-16 08:43:32 +00004224void Reference::GetValue(TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004225 ASSERT(!cgen_->in_spilled_code());
4226 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004227 ASSERT(!is_illegal());
4228 ASSERT(!cgen_->has_cc());
4229 MacroAssembler* masm = cgen_->masm();
4230 Property* property = expression_->AsProperty();
4231 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004232 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004233 }
4234
4235 switch (type_) {
4236 case SLOT: {
4237 Comment cmnt(masm, "[ Load from Slot");
4238 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4239 ASSERT(slot != NULL);
4240 cgen_->LoadFromSlot(slot, typeof_state);
4241 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004242 }
4243
ager@chromium.org7c537e22008-10-16 08:43:32 +00004244 case NAMED: {
4245 // TODO(1241834): Make sure that this it is safe to ignore the
4246 // distinction between expressions in a typeof and not in a typeof. If
4247 // there is a chance that reference errors can be thrown below, we
4248 // must distinguish between the two kinds of loads (typeof expression
4249 // loads must not throw a reference error).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004250 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004251 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004252 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004253 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004254 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4255 // Setup the name register.
4256 Result name_reg = cgen_->allocator()->Allocate(r2);
4257 ASSERT(name_reg.is_valid());
4258 __ mov(name_reg.reg(), Operand(name));
4259 ASSERT(var == NULL || var->is_global());
4260 RelocInfo::Mode rmode = (var == NULL)
4261 ? RelocInfo::CODE_TARGET
4262 : RelocInfo::CODE_TARGET_CONTEXT;
4263 Result answer = frame->CallCodeObject(ic, rmode, &name_reg, 0);
4264 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004265 break;
4266 }
4267
4268 case KEYED: {
4269 // TODO(1241834): Make sure that this it is safe to ignore the
4270 // distinction between expressions in a typeof and not in a typeof.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004271
4272 // TODO(181): Implement inlined version of array indexing once
4273 // loop nesting is properly tracked on ARM.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004274 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004275 Comment cmnt(masm, "[ Load from keyed Property");
4276 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004277 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004278 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004279 ASSERT(var == NULL || var->is_global());
4280 RelocInfo::Mode rmode = (var == NULL)
4281 ? RelocInfo::CODE_TARGET
4282 : RelocInfo::CODE_TARGET_CONTEXT;
4283 Result answer = frame->CallCodeObject(ic, rmode, 0);
4284 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004285 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004286 }
4287
4288 default:
4289 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004290 }
4291}
4292
4293
ager@chromium.org7c537e22008-10-16 08:43:32 +00004294void Reference::SetValue(InitState init_state) {
4295 ASSERT(!is_illegal());
4296 ASSERT(!cgen_->has_cc());
4297 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004298 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004299 Property* property = expression_->AsProperty();
4300 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004301 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004302 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004303
ager@chromium.org7c537e22008-10-16 08:43:32 +00004304 switch (type_) {
4305 case SLOT: {
4306 Comment cmnt(masm, "[ Store to Slot");
4307 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4308 ASSERT(slot != NULL);
4309 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004310 ASSERT(slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004311
ager@chromium.org7c537e22008-10-16 08:43:32 +00004312 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004313 frame->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004314 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004315 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004316
ager@chromium.org7c537e22008-10-16 08:43:32 +00004317 if (init_state == CONST_INIT) {
4318 // Same as the case for a normal store, but ignores attribute
4319 // (e.g. READ_ONLY) of context slot so that we can initialize
4320 // const properties (introduced via eval("const foo = (some
4321 // expr);")). Also, uses the current function context instead of
4322 // the top context.
4323 //
4324 // Note that we must declare the foo upon entry of eval(), via a
4325 // context slot declaration, but we cannot initialize it at the
4326 // same time, because the const declaration may be at the end of
4327 // the eval code (sigh...) and the const variable may have been
4328 // used before (where its value is 'undefined'). Thus, we can only
4329 // do the initialization when we actually encounter the expression
4330 // and when the expression operands are defined and valid, and
4331 // thus we need the split into 2 operations: declaration of the
4332 // context slot followed by initialization.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004333 frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004334 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004335 frame->CallRuntime(Runtime::kStoreContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004336 }
4337 // Storing a variable must keep the (new) value on the expression
4338 // stack. This is necessary for compiling assignment expressions.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004339 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004340
ager@chromium.org7c537e22008-10-16 08:43:32 +00004341 } else {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004342 ASSERT(!slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004343
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004344 JumpTarget exit;
ager@chromium.org7c537e22008-10-16 08:43:32 +00004345 if (init_state == CONST_INIT) {
4346 ASSERT(slot->var()->mode() == Variable::CONST);
4347 // Only the first const initialization must be executed (the slot
4348 // still contains 'the hole' value). When the assignment is
4349 // executed, the code is identical to a normal store (see below).
4350 Comment cmnt(masm, "[ Init const");
4351 __ ldr(r2, cgen_->SlotOperand(slot, r2));
4352 __ cmp(r2, Operand(Factory::the_hole_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004353 exit.Branch(ne);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004354 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004355
ager@chromium.org7c537e22008-10-16 08:43:32 +00004356 // We must execute the store. Storing a variable must keep the
4357 // (new) value on the stack. This is necessary for compiling
4358 // assignment expressions.
4359 //
4360 // Note: We will reach here even with slot->var()->mode() ==
4361 // Variable::CONST because of const declarations which will
4362 // initialize consts to 'the hole' value and by doing so, end up
4363 // calling this code. r2 may be loaded with context; used below in
4364 // RecordWrite.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004365 frame->EmitPop(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004366 __ str(r0, cgen_->SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004367 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004368 if (slot->type() == Slot::CONTEXT) {
4369 // Skip write barrier if the written value is a smi.
4370 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004371 exit.Branch(eq);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004372 // r2 is loaded with context when calling SlotOperand above.
4373 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
4374 __ mov(r3, Operand(offset));
4375 __ RecordWrite(r2, r3, r1);
4376 }
4377 // If we definitely did not jump over the assignment, we do not need
4378 // to bind the exit label. Doing so can defeat peephole
4379 // optimization.
4380 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004381 exit.Bind();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004382 }
4383 }
4384 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004385 }
4386
ager@chromium.org7c537e22008-10-16 08:43:32 +00004387 case NAMED: {
4388 Comment cmnt(masm, "[ Store to named Property");
4389 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004390 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004391 Handle<String> name(GetName());
4392
4393 Result value = cgen_->allocator()->Allocate(r0);
4394 ASSERT(value.is_valid());
4395 frame->EmitPop(value.reg());
4396
4397 // Setup the name register.
4398 Result property_name = cgen_->allocator()->Allocate(r2);
4399 ASSERT(property_name.is_valid());
4400 __ mov(property_name.reg(), Operand(name));
4401 Result answer = frame->CallCodeObject(ic,
4402 RelocInfo::CODE_TARGET,
4403 &value,
4404 &property_name,
4405 0);
4406 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004407 break;
4408 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004409
ager@chromium.org7c537e22008-10-16 08:43:32 +00004410 case KEYED: {
4411 Comment cmnt(masm, "[ Store to keyed Property");
4412 Property* property = expression_->AsProperty();
4413 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004414 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004415
4416 // Call IC code.
4417 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
4418 // TODO(1222589): Make the IC grab the values from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004419 Result value = cgen_->allocator()->Allocate(r0);
4420 ASSERT(value.is_valid());
4421 frame->EmitPop(value.reg()); // value
4422 Result result =
4423 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0);
4424 frame->EmitPush(result.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004425 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004426 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004427
4428 default:
4429 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004430 }
4431}
4432
4433
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004434// Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz
4435// instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0
4436// (31 instead of 32).
4437static void CountLeadingZeros(
4438 MacroAssembler* masm,
4439 Register source,
4440 Register scratch,
4441 Register zeros) {
4442#ifdef __ARM_ARCH_5__
4443 __ clz(zeros, source); // This instruction is only supported after ARM5.
4444#else
4445 __ mov(zeros, Operand(0));
4446 __ mov(scratch, source);
4447 // Top 16.
4448 __ tst(scratch, Operand(0xffff0000));
4449 __ add(zeros, zeros, Operand(16), LeaveCC, eq);
4450 __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
4451 // Top 8.
4452 __ tst(scratch, Operand(0xff000000));
4453 __ add(zeros, zeros, Operand(8), LeaveCC, eq);
4454 __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
4455 // Top 4.
4456 __ tst(scratch, Operand(0xf0000000));
4457 __ add(zeros, zeros, Operand(4), LeaveCC, eq);
4458 __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
4459 // Top 2.
4460 __ tst(scratch, Operand(0xc0000000));
4461 __ add(zeros, zeros, Operand(2), LeaveCC, eq);
4462 __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
4463 // Top bit.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004464 __ tst(scratch, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004465 __ add(zeros, zeros, Operand(1), LeaveCC, eq);
4466#endif
4467}
4468
4469
4470// Takes a Smi and converts to an IEEE 64 bit floating point value in two
4471// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
4472// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
4473// scratch register. Destroys the source register. No GC occurs during this
4474// stub so you don't have to set up the frame.
4475class ConvertToDoubleStub : public CodeStub {
4476 public:
4477 ConvertToDoubleStub(Register result_reg_1,
4478 Register result_reg_2,
4479 Register source_reg,
4480 Register scratch_reg)
4481 : result1_(result_reg_1),
4482 result2_(result_reg_2),
4483 source_(source_reg),
4484 zeros_(scratch_reg) { }
4485
4486 private:
4487 Register result1_;
4488 Register result2_;
4489 Register source_;
4490 Register zeros_;
4491
4492 // Minor key encoding in 16 bits.
4493 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4494 class OpBits: public BitField<Token::Value, 2, 14> {};
4495
4496 Major MajorKey() { return ConvertToDouble; }
4497 int MinorKey() {
4498 // Encode the parameters in a unique 16 bit value.
4499 return result1_.code() +
4500 (result2_.code() << 4) +
4501 (source_.code() << 8) +
4502 (zeros_.code() << 12);
4503 }
4504
4505 void Generate(MacroAssembler* masm);
4506
4507 const char* GetName() { return "ConvertToDoubleStub"; }
4508
4509#ifdef DEBUG
4510 void Print() { PrintF("ConvertToDoubleStub\n"); }
4511#endif
4512};
4513
4514
4515void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
4516#ifndef BIG_ENDIAN_FLOATING_POINT
4517 Register exponent = result1_;
4518 Register mantissa = result2_;
4519#else
4520 Register exponent = result2_;
4521 Register mantissa = result1_;
4522#endif
4523 Label not_special;
4524 // Convert from Smi to integer.
4525 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
4526 // Move sign bit from source to destination. This works because the sign bit
4527 // in the exponent word of the double has the same position and polarity as
4528 // the 2's complement sign bit in a Smi.
4529 ASSERT(HeapNumber::kSignMask == 0x80000000u);
4530 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
4531 // Subtract from 0 if source was negative.
4532 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
4533 __ cmp(source_, Operand(1));
4534 __ b(gt, &not_special);
4535
4536 // We have -1, 0 or 1, which we treat specially.
4537 __ cmp(source_, Operand(0));
4538 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
4539 static const uint32_t exponent_word_for_1 =
4540 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
4541 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne);
4542 // 1, 0 and -1 all have 0 for the second word.
4543 __ mov(mantissa, Operand(0));
4544 __ Ret();
4545
4546 __ bind(&not_special);
4547 // Count leading zeros. Uses result2 for a scratch register on pre-ARM5.
4548 // Gets the wrong answer for 0, but we already checked for that case above.
4549 CountLeadingZeros(masm, source_, mantissa, zeros_);
4550 // Compute exponent and or it into the exponent register.
4551 // We use result2 as a scratch register here.
4552 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
4553 __ orr(exponent,
4554 exponent,
4555 Operand(mantissa, LSL, HeapNumber::kExponentShift));
4556 // Shift up the source chopping the top bit off.
4557 __ add(zeros_, zeros_, Operand(1));
4558 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
4559 __ mov(source_, Operand(source_, LSL, zeros_));
4560 // Compute lower part of fraction (last 12 bits).
4561 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
4562 // And the top (top 20 bits).
4563 __ orr(exponent,
4564 exponent,
4565 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
4566 __ Ret();
4567}
4568
4569
4570// This stub can convert a signed int32 to a heap number (double). It does
4571// not work for int32s that are in Smi range! No GC occurs during this stub
4572// so you don't have to set up the frame.
4573class WriteInt32ToHeapNumberStub : public CodeStub {
4574 public:
4575 WriteInt32ToHeapNumberStub(Register the_int,
4576 Register the_heap_number,
4577 Register scratch)
4578 : the_int_(the_int),
4579 the_heap_number_(the_heap_number),
4580 scratch_(scratch) { }
4581
4582 private:
4583 Register the_int_;
4584 Register the_heap_number_;
4585 Register scratch_;
4586
4587 // Minor key encoding in 16 bits.
4588 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4589 class OpBits: public BitField<Token::Value, 2, 14> {};
4590
4591 Major MajorKey() { return WriteInt32ToHeapNumber; }
4592 int MinorKey() {
4593 // Encode the parameters in a unique 16 bit value.
4594 return the_int_.code() +
4595 (the_heap_number_.code() << 4) +
4596 (scratch_.code() << 8);
4597 }
4598
4599 void Generate(MacroAssembler* masm);
4600
4601 const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
4602
4603#ifdef DEBUG
4604 void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
4605#endif
4606};
4607
4608
4609// See comment for class.
4610void WriteInt32ToHeapNumberStub::Generate(MacroAssembler *masm) {
4611 Label max_negative_int;
4612 // the_int_ has the answer which is a signed int32 but not a Smi.
4613 // We test for the special value that has a different exponent. This test
4614 // has the neat side effect of setting the flags according to the sign.
4615 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004616 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004617 __ b(eq, &max_negative_int);
4618 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
4619 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
4620 uint32_t non_smi_exponent =
4621 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
4622 __ mov(scratch_, Operand(non_smi_exponent));
4623 // Set the sign bit in scratch_ if the value was negative.
4624 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
4625 // Subtract from 0 if the value was negative.
4626 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
4627 // We should be masking the implict first digit of the mantissa away here,
4628 // but it just ends up combining harmlessly with the last digit of the
4629 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
4630 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
4631 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
4632 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
4633 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
4634 __ str(scratch_, FieldMemOperand(the_heap_number_,
4635 HeapNumber::kExponentOffset));
4636 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
4637 __ str(scratch_, FieldMemOperand(the_heap_number_,
4638 HeapNumber::kMantissaOffset));
4639 __ Ret();
4640
4641 __ bind(&max_negative_int);
4642 // The max negative int32 is stored as a positive number in the mantissa of
4643 // a double because it uses a sign bit instead of using two's complement.
4644 // The actual mantissa bits stored are all 0 because the implicit most
4645 // significant 1 bit is not stored.
4646 non_smi_exponent += 1 << HeapNumber::kExponentShift;
4647 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
4648 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
4649 __ mov(ip, Operand(0));
4650 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
4651 __ Ret();
4652}
4653
4654
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004655// Handle the case where the lhs and rhs are the same object.
4656// Equality is almost reflexive (everything but NaN), so this is a test
4657// for "identity and not NaN".
4658static void EmitIdenticalObjectComparison(MacroAssembler* masm,
4659 Label* slow,
4660 Condition cc) {
4661 Label not_identical;
4662 __ cmp(r0, Operand(r1));
4663 __ b(ne, &not_identical);
4664
4665 Register exp_mask_reg = r5;
4666 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4667
4668 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
4669 // so we do the second best thing - test it ourselves.
4670 Label heap_number, return_equal;
4671 // They are both equal and they are not both Smis so both of them are not
4672 // Smis. If it's not a heap number, then return equal.
4673 if (cc == lt || cc == gt) {
4674 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
4675 __ b(ge, slow);
4676 } else {
4677 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4678 __ b(eq, &heap_number);
4679 // Comparing JS objects with <=, >= is complicated.
4680 if (cc != eq) {
4681 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
4682 __ b(ge, slow);
4683 }
4684 }
4685 __ bind(&return_equal);
4686 if (cc == lt) {
4687 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
4688 } else if (cc == gt) {
4689 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
4690 } else {
4691 __ mov(r0, Operand(0)); // Things are <=, >=, ==, === themselves.
4692 }
4693 __ mov(pc, Operand(lr)); // Return.
4694
4695 // For less and greater we don't have to check for NaN since the result of
4696 // x < x is false regardless. For the others here is some code to check
4697 // for NaN.
4698 if (cc != lt && cc != gt) {
4699 __ bind(&heap_number);
4700 // It is a heap number, so return non-equal if it's NaN and equal if it's
4701 // not NaN.
4702 // The representation of NaN values has all exponent bits (52..62) set,
4703 // and not all mantissa bits (0..51) clear.
4704 // Read top bits of double representation (second word of value).
4705 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
4706 // Test that exponent bits are all set.
4707 __ and_(r3, r2, Operand(exp_mask_reg));
4708 __ cmp(r3, Operand(exp_mask_reg));
4709 __ b(ne, &return_equal);
4710
4711 // Shift out flag and all exponent bits, retaining only mantissa.
4712 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
4713 // Or with all low-bits of mantissa.
4714 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
4715 __ orr(r0, r3, Operand(r2), SetCC);
4716 // For equal we already have the right value in r0: Return zero (equal)
4717 // if all bits in mantissa are zero (it's an Infinity) and non-zero if not
4718 // (it's a NaN). For <= and >= we need to load r0 with the failing value
4719 // if it's a NaN.
4720 if (cc != eq) {
4721 // All-zero means Infinity means equal.
4722 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
4723 if (cc == le) {
4724 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
4725 } else {
4726 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
4727 }
4728 }
4729 __ mov(pc, Operand(lr)); // Return.
4730 }
4731 // No fall through here.
4732
4733 __ bind(&not_identical);
4734}
4735
4736
4737// See comment at call site.
4738static void EmitSmiNonsmiComparison(MacroAssembler* masm,
4739 Label* rhs_not_nan,
4740 Label* slow,
4741 bool strict) {
4742 Label lhs_is_smi;
4743 __ tst(r0, Operand(kSmiTagMask));
4744 __ b(eq, &lhs_is_smi);
4745
4746 // Rhs is a Smi. Check whether the non-smi is a heap number.
4747 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4748 if (strict) {
4749 // If lhs was not a number and rhs was a Smi then strict equality cannot
4750 // succeed. Return non-equal (r0 is already not zero)
4751 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4752 } else {
4753 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4754 // the runtime.
4755 __ b(ne, slow);
4756 }
4757
4758 // Rhs is a smi, lhs is a number.
4759 __ push(lr);
4760 __ mov(r7, Operand(r1));
4761 ConvertToDoubleStub stub1(r3, r2, r7, r6);
4762 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
4763 // r3 and r2 are rhs as double.
4764 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4765 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4766 // We now have both loaded as doubles but we can skip the lhs nan check
4767 // since it's a Smi.
4768 __ pop(lr);
4769 __ jmp(rhs_not_nan);
4770
4771 __ bind(&lhs_is_smi);
4772 // Lhs is a Smi. Check whether the non-smi is a heap number.
4773 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
4774 if (strict) {
4775 // If lhs was not a number and rhs was a Smi then strict equality cannot
4776 // succeed. Return non-equal.
4777 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
4778 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4779 } else {
4780 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4781 // the runtime.
4782 __ b(ne, slow);
4783 }
4784
4785 // Lhs is a smi, rhs is a number.
4786 // r0 is Smi and r1 is heap number.
4787 __ push(lr);
4788 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
4789 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4790 __ mov(r7, Operand(r0));
4791 ConvertToDoubleStub stub2(r1, r0, r7, r6);
4792 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
4793 __ pop(lr);
4794 // Fall through to both_loaded_as_doubles.
4795}
4796
4797
4798void EmitNanCheck(MacroAssembler* masm, Label* rhs_not_nan, Condition cc) {
4799 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
4800 Register lhs_exponent = exp_first ? r0 : r1;
4801 Register rhs_exponent = exp_first ? r2 : r3;
4802 Register lhs_mantissa = exp_first ? r1 : r0;
4803 Register rhs_mantissa = exp_first ? r3 : r2;
4804 Label one_is_nan, neither_is_nan;
4805
4806 Register exp_mask_reg = r5;
4807
4808 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4809 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
4810 __ cmp(r4, Operand(exp_mask_reg));
4811 __ b(ne, rhs_not_nan);
4812 __ mov(r4,
4813 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4814 SetCC);
4815 __ b(ne, &one_is_nan);
4816 __ cmp(rhs_mantissa, Operand(0));
4817 __ b(ne, &one_is_nan);
4818
4819 __ bind(rhs_not_nan);
4820 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4821 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
4822 __ cmp(r4, Operand(exp_mask_reg));
4823 __ b(ne, &neither_is_nan);
4824 __ mov(r4,
4825 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4826 SetCC);
4827 __ b(ne, &one_is_nan);
4828 __ cmp(lhs_mantissa, Operand(0));
4829 __ b(eq, &neither_is_nan);
4830
4831 __ bind(&one_is_nan);
4832 // NaN comparisons always fail.
4833 // Load whatever we need in r0 to make the comparison fail.
4834 if (cc == lt || cc == le) {
4835 __ mov(r0, Operand(GREATER));
4836 } else {
4837 __ mov(r0, Operand(LESS));
4838 }
4839 __ mov(pc, Operand(lr)); // Return.
4840
4841 __ bind(&neither_is_nan);
4842}
4843
4844
4845// See comment at call site.
4846static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
4847 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
4848 Register lhs_exponent = exp_first ? r0 : r1;
4849 Register rhs_exponent = exp_first ? r2 : r3;
4850 Register lhs_mantissa = exp_first ? r1 : r0;
4851 Register rhs_mantissa = exp_first ? r3 : r2;
4852
4853 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
4854 if (cc == eq) {
4855 // Doubles are not equal unless they have the same bit pattern.
4856 // Exception: 0 and -0.
4857 __ cmp(lhs_mantissa, Operand(rhs_mantissa));
4858 __ orr(r0, lhs_mantissa, Operand(rhs_mantissa), LeaveCC, ne);
4859 // Return non-zero if the numbers are unequal.
4860 __ mov(pc, Operand(lr), LeaveCC, ne);
4861
4862 __ sub(r0, lhs_exponent, Operand(rhs_exponent), SetCC);
4863 // If exponents are equal then return 0.
4864 __ mov(pc, Operand(lr), LeaveCC, eq);
4865
4866 // Exponents are unequal. The only way we can return that the numbers
4867 // are equal is if one is -0 and the other is 0. We already dealt
4868 // with the case where both are -0 or both are 0.
4869 // We start by seeing if the mantissas (that are equal) or the bottom
4870 // 31 bits of the rhs exponent are non-zero. If so we return not
4871 // equal.
4872 __ orr(r4, rhs_mantissa, Operand(rhs_exponent, LSL, kSmiTagSize), SetCC);
4873 __ mov(r0, Operand(r4), LeaveCC, ne);
4874 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
4875 // Now they are equal if and only if the lhs exponent is zero in its
4876 // low 31 bits.
4877 __ mov(r0, Operand(lhs_exponent, LSL, kSmiTagSize));
4878 __ mov(pc, Operand(lr));
4879 } else {
4880 // Call a native function to do a comparison between two non-NaNs.
4881 // Call C routine that may not cause GC or other trouble.
4882 __ mov(r5, Operand(ExternalReference::compare_doubles()));
4883 __ Jump(r5); // Tail call.
4884 }
4885}
4886
4887
4888// See comment at call site.
4889static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
4890 // If either operand is a JSObject or an oddball value, then they are
4891 // not equal since their pointers are different.
4892 // There is no test for undetectability in strict equality.
4893 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4894 Label first_non_object;
4895 // Get the type of the first operand into r2 and compare it with
4896 // FIRST_JS_OBJECT_TYPE.
4897 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
4898 __ b(lt, &first_non_object);
4899
4900 // Return non-zero (r0 is not zero)
4901 Label return_not_equal;
4902 __ bind(&return_not_equal);
4903 __ mov(pc, Operand(lr)); // Return.
4904
4905 __ bind(&first_non_object);
4906 // Check for oddballs: true, false, null, undefined.
4907 __ cmp(r2, Operand(ODDBALL_TYPE));
4908 __ b(eq, &return_not_equal);
4909
4910 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
4911 __ b(ge, &return_not_equal);
4912
4913 // Check for oddballs: true, false, null, undefined.
4914 __ cmp(r3, Operand(ODDBALL_TYPE));
4915 __ b(eq, &return_not_equal);
4916}
4917
4918
4919// See comment at call site.
4920static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
4921 Label* both_loaded_as_doubles,
4922 Label* not_heap_numbers,
4923 Label* slow) {
4924 __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE);
4925 __ b(ne, not_heap_numbers);
4926 __ CompareObjectType(r1, r3, r3, HEAP_NUMBER_TYPE);
4927 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
4928
4929 // Both are heap numbers. Load them up then jump to the code we have
4930 // for that.
4931 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
4932 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4933 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4934 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4935 __ jmp(both_loaded_as_doubles);
4936}
4937
4938
4939// Fast negative check for symbol-to-symbol equality.
4940static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
4941 // r2 is object type of r0.
4942 __ tst(r2, Operand(kIsNotStringMask));
4943 __ b(ne, slow);
4944 __ tst(r2, Operand(kIsSymbolMask));
4945 __ b(eq, slow);
4946 __ CompareObjectType(r1, r3, r3, FIRST_NONSTRING_TYPE);
4947 __ b(ge, slow);
4948 __ tst(r3, Operand(kIsSymbolMask));
4949 __ b(eq, slow);
4950
4951 // Both are symbols. We already checked they weren't the same pointer
4952 // so they are not equal.
4953 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
4954 __ mov(pc, Operand(lr)); // Return.
4955}
4956
4957
4958// On entry r0 and r1 are the things to be compared. On exit r0 is 0,
4959// positive or negative to indicate the result of the comparison.
4960void CompareStub::Generate(MacroAssembler* masm) {
4961 Label slow; // Call builtin.
4962 Label not_smis, both_loaded_as_doubles, rhs_not_nan;
4963
4964 // NOTICE! This code is only reached after a smi-fast-case check, so
4965 // it is certain that at least one operand isn't a smi.
4966
4967 // Handle the case where the objects are identical. Either returns the answer
4968 // or goes to slow. Only falls through if the objects were not identical.
4969 EmitIdenticalObjectComparison(masm, &slow, cc_);
4970
4971 // If either is a Smi (we know that not both are), then they can only
4972 // be strictly equal if the other is a HeapNumber.
4973 ASSERT_EQ(0, kSmiTag);
4974 ASSERT_EQ(0, Smi::FromInt(0));
4975 __ and_(r2, r0, Operand(r1));
4976 __ tst(r2, Operand(kSmiTagMask));
4977 __ b(ne, &not_smis);
4978 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
4979 // 1) Return the answer.
4980 // 2) Go to slow.
4981 // 3) Fall through to both_loaded_as_doubles.
4982 // 4) Jump to rhs_not_nan.
4983 // In cases 3 and 4 we have found out we were dealing with a number-number
4984 // comparison and the numbers have been loaded into r0, r1, r2, r3 as doubles.
4985 EmitSmiNonsmiComparison(masm, &rhs_not_nan, &slow, strict_);
4986
4987 __ bind(&both_loaded_as_doubles);
4988 // r0, r1, r2, r3 are the double representations of the left hand side
4989 // and the right hand side.
4990
4991 // Checks for NaN in the doubles we have loaded. Can return the answer or
4992 // fall through if neither is a NaN. Also binds rhs_not_nan.
4993 EmitNanCheck(masm, &rhs_not_nan, cc_);
4994
4995 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
4996 // answer. Never falls through.
4997 EmitTwoNonNanDoubleComparison(masm, cc_);
4998
4999 __ bind(&not_smis);
5000 // At this point we know we are dealing with two different objects,
5001 // and neither of them is a Smi. The objects are in r0 and r1.
5002 if (strict_) {
5003 // This returns non-equal for some object types, or falls through if it
5004 // was not lucky.
5005 EmitStrictTwoHeapObjectCompare(masm);
5006 }
5007
5008 Label check_for_symbols;
5009 // Check for heap-number-heap-number comparison. Can jump to slow case,
5010 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
5011 // that case. If the inputs are not doubles then jumps to check_for_symbols.
5012 // In this case r2 will contain the type of r0.
5013 EmitCheckForTwoHeapNumbers(masm,
5014 &both_loaded_as_doubles,
5015 &check_for_symbols,
5016 &slow);
5017
5018 __ bind(&check_for_symbols);
5019 if (cc_ == eq) {
5020 // Either jumps to slow or returns the answer. Assumes that r2 is the type
5021 // of r0 on entry.
5022 EmitCheckForSymbols(masm, &slow);
5023 }
5024
5025 __ bind(&slow);
5026 __ push(lr);
5027 __ push(r1);
5028 __ push(r0);
5029 // Figure out which native to call and setup the arguments.
5030 Builtins::JavaScript native;
5031 int arg_count = 1; // Not counting receiver.
5032 if (cc_ == eq) {
5033 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
5034 } else {
5035 native = Builtins::COMPARE;
5036 int ncr; // NaN compare result
5037 if (cc_ == lt || cc_ == le) {
5038 ncr = GREATER;
5039 } else {
5040 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
5041 ncr = LESS;
5042 }
5043 arg_count++;
5044 __ mov(r0, Operand(Smi::FromInt(ncr)));
5045 __ push(r0);
5046 }
5047
5048 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
5049 // tagged as a small integer.
5050 __ mov(r0, Operand(arg_count));
5051 __ InvokeBuiltin(native, CALL_JS);
5052 __ cmp(r0, Operand(0));
5053 __ pop(pc);
5054}
5055
5056
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005057// Allocates a heap number or jumps to the label if the young space is full and
5058// a scavenge is needed.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005059static void AllocateHeapNumber(
5060 MacroAssembler* masm,
5061 Label* need_gc, // Jump here if young space is full.
5062 Register result_reg, // The tagged address of the new heap number.
5063 Register allocation_top_addr_reg, // A scratch register.
5064 Register scratch2) { // Another scratch register.
5065 ExternalReference allocation_top =
5066 ExternalReference::new_space_allocation_top_address();
5067 ExternalReference allocation_limit =
5068 ExternalReference::new_space_allocation_limit_address();
5069
5070 // allocat := the address of the allocation top variable.
5071 __ mov(allocation_top_addr_reg, Operand(allocation_top));
5072 // result_reg := the old allocation top.
5073 __ ldr(result_reg, MemOperand(allocation_top_addr_reg));
5074 // scratch2 := the address of the allocation limit.
5075 __ mov(scratch2, Operand(allocation_limit));
5076 // scratch2 := the allocation limit.
5077 __ ldr(scratch2, MemOperand(scratch2));
5078 // result_reg := the new allocation top.
5079 __ add(result_reg, result_reg, Operand(HeapNumber::kSize));
5080 // Compare new new allocation top and limit.
5081 __ cmp(result_reg, Operand(scratch2));
5082 // Branch if out of space in young generation.
5083 __ b(hi, need_gc);
5084 // Store new allocation top.
5085 __ str(result_reg, MemOperand(allocation_top_addr_reg)); // store new top
5086 // Tag and adjust back to start of new object.
5087 __ sub(result_reg, result_reg, Operand(HeapNumber::kSize - kHeapObjectTag));
5088 // Get heap number map into scratch2.
5089 __ mov(scratch2, Operand(Factory::heap_number_map()));
5090 // Store heap number map in new object.
5091 __ str(scratch2, FieldMemOperand(result_reg, HeapObject::kMapOffset));
5092}
5093
5094
5095// We fall into this code if the operands were Smis, but the result was
5096// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005097// the operands were not both Smi. The operands are in r0 and r1. In order
5098// to call the C-implemented binary fp operation routines we need to end up
5099// with the double precision floating point operands in r0 and r1 (for the
5100// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005101static void HandleBinaryOpSlowCases(MacroAssembler* masm,
5102 Label* not_smi,
5103 const Builtins::JavaScript& builtin,
5104 Token::Value operation,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005105 OverwriteMode mode) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005106 Label slow, slow_pop_2_first, do_the_call;
5107 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
5108 // Smi-smi case (overflow).
5109 // Since both are Smis there is no heap number to overwrite, so allocate.
5110 // The new heap number is in r5. r6 and r7 are scratch.
5111 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5112 // Write Smi from r0 to r3 and r2 in double format. r6 is scratch.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005113 __ mov(r7, Operand(r0));
5114 ConvertToDoubleStub stub1(r3, r2, r7, r6);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005115 __ push(lr);
5116 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
5117 // Write Smi from r1 to r1 and r0 in double format. r6 is scratch.
5118 __ mov(r7, Operand(r1));
5119 ConvertToDoubleStub stub2(r1, r0, r7, r6);
5120 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
5121 __ pop(lr);
5122 __ jmp(&do_the_call); // Tail call. No return.
5123
5124 // We jump to here if something goes wrong (one param is not a number of any
5125 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005126 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005127 __ push(r1);
5128 __ push(r0);
5129 __ mov(r0, Operand(1)); // Set number of arguments.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005130 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005131
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005132 // We branch here if at least one of r0 and r1 is not a Smi.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005133 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005134 if (mode == NO_OVERWRITE) {
5135 // In the case where there is no chance of an overwritable float we may as
5136 // well do the allocation immediately while r0 and r1 are untouched.
5137 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5138 }
5139
5140 // Move r0 to a double in r2-r3.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005141 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005142 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5143 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005144 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005145 if (mode == OVERWRITE_RIGHT) {
5146 __ mov(r5, Operand(r0)); // Overwrite this heap number.
5147 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005148 // Calling convention says that second double is in r2 and r3.
5149 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005150 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
5151 __ jmp(&finished_loading_r0);
5152 __ bind(&r0_is_smi);
5153 if (mode == OVERWRITE_RIGHT) {
5154 // We can't overwrite a Smi so get address of new heap number into r5.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005155 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005156 }
5157 // Write Smi from r0 to r3 and r2 in double format.
5158 __ mov(r7, Operand(r0));
5159 ConvertToDoubleStub stub3(r3, r2, r7, r6);
5160 __ push(lr);
5161 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
5162 __ pop(lr);
5163 __ bind(&finished_loading_r0);
5164
5165 // Move r1 to a double in r0-r1.
5166 __ tst(r1, Operand(kSmiTagMask));
5167 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5168 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5169 __ b(ne, &slow);
5170 if (mode == OVERWRITE_LEFT) {
5171 __ mov(r5, Operand(r1)); // Overwrite this heap number.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005172 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005173 // Calling convention says that first double is in r0 and r1.
5174 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005175 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
5176 __ jmp(&finished_loading_r1);
5177 __ bind(&r1_is_smi);
5178 if (mode == OVERWRITE_LEFT) {
5179 // We can't overwrite a Smi so get address of new heap number into r5.
5180 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5181 }
5182 // Write Smi from r1 to r1 and r0 in double format.
5183 __ mov(r7, Operand(r1));
5184 ConvertToDoubleStub stub4(r1, r0, r7, r6);
5185 __ push(lr);
5186 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
5187 __ pop(lr);
5188 __ bind(&finished_loading_r1);
5189
5190 __ bind(&do_the_call);
5191 // r0: Left value (least significant part of mantissa).
5192 // r1: Left value (sign, exponent, top of mantissa).
5193 // r2: Right value (least significant part of mantissa).
5194 // r3: Right value (sign, exponent, top of mantissa).
5195 // r5: Address of heap number for result.
5196 __ push(lr); // For later.
5197 __ push(r5); // Address of heap number that is answer.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005198 // Call C routine that may not cause GC or other trouble.
5199 __ mov(r5, Operand(ExternalReference::double_fp_operation(operation)));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005200 __ Call(r5);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005201 // Store answer in the overwritable heap number.
5202 __ pop(r4);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005203#if !defined(USE_ARM_EABI)
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005204 // Double returned in fp coprocessor register 0 and 1, encoded as register
5205 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
5206 // substract the tag from r4.
5207 __ sub(r5, r4, Operand(kHeapObjectTag));
5208 __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset));
5209#else
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005210 // Double returned in registers 0 and 1.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005211 __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005212 __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005213#endif
5214 __ mov(r0, Operand(r4));
5215 // And we are done.
5216 __ pop(pc);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005217}
5218
5219
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005220// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005221// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005222// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
5223// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005224// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
5225// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005226static void GetInt32(MacroAssembler* masm,
5227 Register source,
5228 Register dest,
5229 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005230 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005231 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005232 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005233 // Get exponent word.
5234 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
5235 // Get exponent alone in scratch2.
5236 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005237 // Load dest with zero. We use this either for the final shift or
5238 // for the answer.
5239 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005240 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005241 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
5242 // the exponent that we are fastest at and also the highest exponent we can
5243 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005244 const uint32_t non_smi_exponent =
5245 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
5246 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005247 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
5248 __ b(eq, &right_exponent);
5249 // If the exponent is higher than that then go to slow case. This catches
5250 // numbers that don't fit in a signed int32, infinities and NaNs.
5251 __ b(gt, slow);
5252
5253 // We know the exponent is smaller than 30 (biased). If it is less than
5254 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
5255 // it rounds to zero.
5256 const uint32_t zero_exponent =
5257 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
5258 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
5259 // Dest already has a Smi zero.
5260 __ b(lt, &done);
5261 // We have a shifted exponent between 0 and 30 in scratch2.
5262 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
5263 // We now have the exponent in dest. Subtract from 30 to get
5264 // how much to shift down.
5265 __ rsb(dest, dest, Operand(30));
5266
5267 __ bind(&right_exponent);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005268 // Get the top bits of the mantissa.
5269 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
5270 // Put back the implicit 1.
5271 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
5272 // Shift up the mantissa bits to take up the space the exponent used to take.
5273 // We just orred in the implicit bit so that took care of one and we want to
5274 // leave the sign bit 0 so we subtract 2 bits from the shift distance.
5275 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
5276 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
5277 // Put sign in zero flag.
5278 __ tst(scratch, Operand(HeapNumber::kSignMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005279 // Get the second half of the double. For some exponents we don't actually
5280 // need this because the bits get shifted out again, but it's probably slower
5281 // to test than just to do it.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005282 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
5283 // Shift down 22 bits to get the last 10 bits.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005284 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
5285 // Move down according to the exponent.
5286 __ mov(dest, Operand(scratch, LSR, dest));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005287 // Fix sign if sign bit was set.
5288 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005289 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005290}
5291
5292
5293// For bitwise ops where the inputs are not both Smis we here try to determine
5294// whether both inputs are either Smis or at least heap numbers that can be
5295// represented by a 32 bit signed value. We truncate towards zero as required
5296// by the ES spec. If this is the case we do the bitwise op and see if the
5297// result is a Smi. If so, great, otherwise we try to find a heap number to
5298// write the answer into (either by allocating or by overwriting).
5299// On entry the operands are in r0 and r1. On exit the answer is in r0.
5300void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) {
5301 Label slow, result_not_a_smi;
5302 Label r0_is_smi, r1_is_smi;
5303 Label done_checking_r0, done_checking_r1;
5304
5305 __ tst(r1, Operand(kSmiTagMask));
5306 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5307 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5308 __ b(ne, &slow);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005309 GetInt32(masm, r1, r3, r4, r5, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005310 __ jmp(&done_checking_r1);
5311 __ bind(&r1_is_smi);
5312 __ mov(r3, Operand(r1, ASR, 1));
5313 __ bind(&done_checking_r1);
5314
5315 __ tst(r0, Operand(kSmiTagMask));
5316 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5317 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5318 __ b(ne, &slow);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005319 GetInt32(masm, r0, r2, r4, r5, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005320 __ jmp(&done_checking_r0);
5321 __ bind(&r0_is_smi);
5322 __ mov(r2, Operand(r0, ASR, 1));
5323 __ bind(&done_checking_r0);
5324
5325 // r0 and r1: Original operands (Smi or heap numbers).
5326 // r2 and r3: Signed int32 operands.
5327 switch (op_) {
5328 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
5329 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
5330 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
5331 case Token::SAR:
5332 // Use only the 5 least significant bits of the shift count.
5333 __ and_(r2, r2, Operand(0x1f));
5334 __ mov(r2, Operand(r3, ASR, r2));
5335 break;
5336 case Token::SHR:
5337 // Use only the 5 least significant bits of the shift count.
5338 __ and_(r2, r2, Operand(0x1f));
5339 __ mov(r2, Operand(r3, LSR, r2), SetCC);
5340 // SHR is special because it is required to produce a positive answer.
5341 // The code below for writing into heap numbers isn't capable of writing
5342 // the register as an unsigned int so we go to slow case if we hit this
5343 // case.
5344 __ b(mi, &slow);
5345 break;
5346 case Token::SHL:
5347 // Use only the 5 least significant bits of the shift count.
5348 __ and_(r2, r2, Operand(0x1f));
5349 __ mov(r2, Operand(r3, LSL, r2));
5350 break;
5351 default: UNREACHABLE();
5352 }
5353 // check that the *signed* result fits in a smi
5354 __ add(r3, r2, Operand(0x40000000), SetCC);
5355 __ b(mi, &result_not_a_smi);
5356 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5357 __ Ret();
5358
5359 Label have_to_allocate, got_a_heap_number;
5360 __ bind(&result_not_a_smi);
5361 switch (mode_) {
5362 case OVERWRITE_RIGHT: {
5363 __ tst(r0, Operand(kSmiTagMask));
5364 __ b(eq, &have_to_allocate);
5365 __ mov(r5, Operand(r0));
5366 break;
5367 }
5368 case OVERWRITE_LEFT: {
5369 __ tst(r1, Operand(kSmiTagMask));
5370 __ b(eq, &have_to_allocate);
5371 __ mov(r5, Operand(r1));
5372 break;
5373 }
5374 case NO_OVERWRITE: {
5375 // Get a new heap number in r5. r6 and r7 are scratch.
5376 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5377 }
5378 default: break;
5379 }
5380 __ bind(&got_a_heap_number);
5381 // r2: Answer as signed int32.
5382 // r5: Heap number to write answer into.
5383
5384 // Nothing can go wrong now, so move the heap number to r0, which is the
5385 // result.
5386 __ mov(r0, Operand(r5));
5387
5388 // Tail call that writes the int32 in r2 to the heap number in r0, using
5389 // r3 as scratch. r0 is preserved and returned.
5390 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
5391 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
5392
5393 if (mode_ != NO_OVERWRITE) {
5394 __ bind(&have_to_allocate);
5395 // Get a new heap number in r5. r6 and r7 are scratch.
5396 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5397 __ jmp(&got_a_heap_number);
5398 }
5399
5400 // If all else failed then we go to the runtime system.
5401 __ bind(&slow);
5402 __ push(r1); // restore stack
5403 __ push(r0);
5404 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
5405 switch (op_) {
5406 case Token::BIT_OR:
5407 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
5408 break;
5409 case Token::BIT_AND:
5410 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
5411 break;
5412 case Token::BIT_XOR:
5413 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
5414 break;
5415 case Token::SAR:
5416 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
5417 break;
5418 case Token::SHR:
5419 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
5420 break;
5421 case Token::SHL:
5422 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
5423 break;
5424 default:
5425 UNREACHABLE();
5426 }
5427}
5428
5429
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005430// Can we multiply by x with max two shifts and an add.
5431// This answers yes to all integers from 2 to 10.
5432static bool IsEasyToMultiplyBy(int x) {
5433 if (x < 2) return false; // Avoid special cases.
5434 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
5435 if (IsPowerOf2(x)) return true; // Simple shift.
5436 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
5437 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
5438 return false;
5439}
5440
5441
5442// Can multiply by anything that IsEasyToMultiplyBy returns true for.
5443// Source and destination may be the same register. This routine does
5444// not set carry and overflow the way a mul instruction would.
5445static void MultiplyByKnownInt(MacroAssembler* masm,
5446 Register source,
5447 Register destination,
5448 int known_int) {
5449 if (IsPowerOf2(known_int)) {
5450 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
5451 } else if (PopCountLessThanEqual2(known_int)) {
5452 int first_bit = BitPosition(known_int);
5453 int second_bit = BitPosition(known_int ^ (1 << first_bit));
5454 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
5455 if (first_bit != 0) {
5456 __ mov(destination, Operand(destination, LSL, first_bit));
5457 }
5458 } else {
5459 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
5460 int the_bit = BitPosition(known_int + 1);
5461 __ rsb(destination, source, Operand(source, LSL, the_bit));
5462 }
5463}
5464
5465
5466// This function (as opposed to MultiplyByKnownInt) takes the known int in a
5467// a register for the cases where it doesn't know a good trick, and may deliver
5468// a result that needs shifting.
5469static void MultiplyByKnownInt2(
5470 MacroAssembler* masm,
5471 Register result,
5472 Register source,
5473 Register known_int_register, // Smi tagged.
5474 int known_int,
5475 int* required_shift) { // Including Smi tag shift
5476 switch (known_int) {
5477 case 3:
5478 __ add(result, source, Operand(source, LSL, 1));
5479 *required_shift = 1;
5480 break;
5481 case 5:
5482 __ add(result, source, Operand(source, LSL, 2));
5483 *required_shift = 1;
5484 break;
5485 case 6:
5486 __ add(result, source, Operand(source, LSL, 1));
5487 *required_shift = 2;
5488 break;
5489 case 7:
5490 __ rsb(result, source, Operand(source, LSL, 3));
5491 *required_shift = 1;
5492 break;
5493 case 9:
5494 __ add(result, source, Operand(source, LSL, 3));
5495 *required_shift = 1;
5496 break;
5497 case 10:
5498 __ add(result, source, Operand(source, LSL, 2));
5499 *required_shift = 2;
5500 break;
5501 default:
5502 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
5503 __ mul(result, source, known_int_register);
5504 *required_shift = 0;
5505 }
5506}
5507
5508
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005509void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
5510 // r1 : x
5511 // r0 : y
5512 // result : r0
5513
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005514 // All ops need to know whether we are dealing with two Smis. Set up r2 to
5515 // tell us that.
5516 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
5517
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005518 switch (op_) {
5519 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005520 Label not_smi;
5521 // Fast path.
5522 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005523 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005524 __ b(ne, &not_smi);
5525 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
5526 // Return if no overflow.
5527 __ Ret(vc);
5528 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
5529
5530 HandleBinaryOpSlowCases(masm,
5531 &not_smi,
5532 Builtins::ADD,
5533 Token::ADD,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005534 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005535 break;
5536 }
5537
5538 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005539 Label not_smi;
5540 // Fast path.
5541 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005542 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005543 __ b(ne, &not_smi);
5544 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
5545 // Return if no overflow.
5546 __ Ret(vc);
5547 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
5548
5549 HandleBinaryOpSlowCases(masm,
5550 &not_smi,
5551 Builtins::SUB,
5552 Token::SUB,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005553 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005554 break;
5555 }
5556
5557 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005558 Label not_smi, slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005559 ASSERT(kSmiTag == 0); // adjust code below
5560 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005561 __ b(ne, &not_smi);
5562 // Remove tag from one operand (but keep sign), so that result is Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005563 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005564 // Do multiplication
5565 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1.
5566 // Go slow on overflows (overflow bit is not set).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005567 __ mov(ip, Operand(r3, ASR, 31));
5568 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
5569 __ b(ne, &slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005570 // Go slow on zero result to handle -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005571 __ tst(r3, Operand(r3));
5572 __ mov(r0, Operand(r3), LeaveCC, ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005573 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005574 // We need -0 if we were multiplying a negative number with 0 to get 0.
5575 // We know one of them was zero.
5576 __ add(r2, r0, Operand(r1), SetCC);
5577 __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl);
5578 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
5579 // Slow case. We fall through here if we multiplied a negative number
5580 // with 0, because that would mean we should produce -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005581 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005582
5583 HandleBinaryOpSlowCases(masm,
5584 &not_smi,
5585 Builtins::MUL,
5586 Token::MUL,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005587 mode_);
5588 break;
5589 }
5590
5591 case Token::DIV:
5592 case Token::MOD: {
5593 Label not_smi;
5594 if (specialized_on_rhs_) {
5595 Label smi_is_unsuitable;
5596 __ BranchOnNotSmi(r1, &not_smi);
5597 if (IsPowerOf2(constant_rhs_)) {
5598 if (op_ == Token::MOD) {
5599 __ and_(r0,
5600 r1,
5601 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
5602 SetCC);
5603 // We now have the answer, but if the input was negative we also
5604 // have the sign bit. Our work is done if the result is
5605 // positive or zero:
5606 __ Ret(pl);
5607 // A mod of a negative left hand side must return a negative number.
5608 // Unfortunately if the answer is 0 then we must return -0. And we
5609 // already optimistically trashed r0 so we may need to restore it.
5610 __ eor(r0, r0, Operand(0x80000000u), SetCC);
5611 // Next two instructions are conditional on the answer being -0.
5612 __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
5613 __ b(eq, &smi_is_unsuitable);
5614 // We need to subtract the dividend. Eg. -3 % 4 == -3.
5615 __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_)));
5616 } else {
5617 ASSERT(op_ == Token::DIV);
5618 __ tst(r1,
5619 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
5620 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
5621 int shift = 0;
5622 int d = constant_rhs_;
5623 while ((d & 1) == 0) {
5624 d >>= 1;
5625 shift++;
5626 }
5627 __ mov(r0, Operand(r1, LSR, shift));
5628 __ bic(r0, r0, Operand(kSmiTagMask));
5629 }
5630 } else {
5631 // Not a power of 2.
5632 __ tst(r1, Operand(0x80000000u));
5633 __ b(ne, &smi_is_unsuitable);
5634 // Find a fixed point reciprocal of the divisor so we can divide by
5635 // multiplying.
5636 double divisor = 1.0 / constant_rhs_;
5637 int shift = 32;
5638 double scale = 4294967296.0; // 1 << 32.
5639 uint32_t mul;
5640 // Maximise the precision of the fixed point reciprocal.
5641 while (true) {
5642 mul = static_cast<uint32_t>(scale * divisor);
5643 if (mul >= 0x7fffffff) break;
5644 scale *= 2.0;
5645 shift++;
5646 }
5647 mul++;
5648 __ mov(r2, Operand(mul));
5649 __ umull(r3, r2, r2, r1);
5650 __ mov(r2, Operand(r2, LSR, shift - 31));
5651 // r2 is r1 / rhs. r2 is not Smi tagged.
5652 // r0 is still the known rhs. r0 is Smi tagged.
5653 // r1 is still the unkown lhs. r1 is Smi tagged.
5654 int required_r4_shift = 0; // Including the Smi tag shift of 1.
5655 // r4 = r2 * r0.
5656 MultiplyByKnownInt2(masm,
5657 r4,
5658 r2,
5659 r0,
5660 constant_rhs_,
5661 &required_r4_shift);
5662 // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs).
5663 if (op_ == Token::DIV) {
5664 __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC);
5665 __ b(ne, &smi_is_unsuitable); // There was a remainder.
5666 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5667 } else {
5668 ASSERT(op_ == Token::MOD);
5669 __ sub(r0, r1, Operand(r4, LSL, required_r4_shift));
5670 }
5671 }
5672 __ Ret();
5673 __ bind(&smi_is_unsuitable);
5674 } else {
5675 __ jmp(&not_smi);
5676 }
5677 HandleBinaryOpSlowCases(masm,
5678 &not_smi,
5679 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV,
5680 op_,
5681 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005682 break;
5683 }
5684
5685 case Token::BIT_OR:
5686 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005687 case Token::BIT_XOR:
5688 case Token::SAR:
5689 case Token::SHR:
5690 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005691 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005692 ASSERT(kSmiTag == 0); // adjust code below
5693 __ tst(r2, Operand(kSmiTagMask));
5694 __ b(ne, &slow);
5695 switch (op_) {
5696 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
5697 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
5698 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005699 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005700 // Remove tags from right operand.
5701 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5702 // Use only the 5 least significant bits of the shift count.
5703 __ and_(r2, r2, Operand(0x1f));
5704 __ mov(r0, Operand(r1, ASR, r2));
5705 // Smi tag result.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005706 __ bic(r0, r0, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005707 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005708 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005709 // Remove tags from operands. We can't do this on a 31 bit number
5710 // because then the 0s get shifted into bit 30 instead of bit 31.
5711 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5712 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5713 // Use only the 5 least significant bits of the shift count.
5714 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005715 __ mov(r3, Operand(r3, LSR, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005716 // Unsigned shift is not allowed to produce a negative number, so
5717 // check the sign bit and the sign bit after Smi tagging.
5718 __ tst(r3, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005719 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005720 // Smi tag result.
5721 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005722 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005723 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005724 // Remove tags from operands.
5725 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5726 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5727 // Use only the 5 least significant bits of the shift count.
5728 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005729 __ mov(r3, Operand(r3, LSL, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005730 // Check that the signed result fits in a Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005731 __ add(r2, r3, Operand(0x40000000), SetCC);
5732 __ b(mi, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005733 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005734 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005735 default: UNREACHABLE();
5736 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005737 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005738 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005739 HandleNonSmiBitwiseOp(masm);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005740 break;
5741 }
5742
5743 default: UNREACHABLE();
5744 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005745 // This code should be unreachable.
5746 __ stop("Unreachable");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005747}
5748
5749
5750void StackCheckStub::Generate(MacroAssembler* masm) {
5751 Label within_limit;
5752 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
5753 __ ldr(ip, MemOperand(ip));
5754 __ cmp(sp, Operand(ip));
5755 __ b(hs, &within_limit);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00005756 // Do tail-call to runtime routine. Runtime routines expect at least one
5757 // argument, so give it a Smi.
5758 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005759 __ push(r0);
5760 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
5761 __ bind(&within_limit);
5762
5763 __ StubReturn(1);
5764}
5765
5766
5767void UnarySubStub::Generate(MacroAssembler* masm) {
5768 Label undo;
5769 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005770 Label not_smi;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005771
5772 // Enter runtime system if the value is not a smi.
5773 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005774 __ b(ne, &not_smi);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005775
5776 // Enter runtime system if the value of the expression is zero
5777 // to make sure that we switch between 0 and -0.
5778 __ cmp(r0, Operand(0));
5779 __ b(eq, &slow);
5780
5781 // The value of the expression is a smi that is not zero. Try
5782 // optimistic subtraction '0 - value'.
5783 __ rsb(r1, r0, Operand(0), SetCC);
5784 __ b(vs, &slow);
5785
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005786 __ mov(r0, Operand(r1)); // Set r0 to result.
5787 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005788
5789 // Enter runtime system.
5790 __ bind(&slow);
5791 __ push(r0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005792 __ mov(r0, Operand(0)); // Set number of arguments.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005793 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
5794
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005795 __ bind(&not_smi);
5796 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
5797 __ b(ne, &slow);
5798 // r0 is a heap number. Get a new heap number in r1.
5799 if (overwrite_) {
5800 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5801 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
5802 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5803 } else {
5804 AllocateHeapNumber(masm, &slow, r1, r2, r3);
5805 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
5806 __ str(r2, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
5807 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5808 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
5809 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
5810 __ mov(r0, Operand(r1));
5811 }
5812 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005813}
5814
5815
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005816void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005817 // r0 holds the exception.
5818
5819 // Adjust this code if not the case.
5820 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
5821
5822 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005823 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
5824 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005825
5826 // Restore the next handler and frame pointer, discard handler state.
5827 ASSERT(StackHandlerConstants::kNextOffset == 0);
5828 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005829 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005830 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
5831 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
5832
5833 // Before returning we restore the context from the frame pointer if
5834 // not NULL. The frame pointer is NULL in the exception handler of a
5835 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005836 __ cmp(fp, Operand(0));
5837 // Set cp to NULL if fp is NULL.
5838 __ mov(cp, Operand(0), LeaveCC, eq);
5839 // Restore cp otherwise.
5840 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005841#ifdef DEBUG
5842 if (FLAG_debug_code) {
5843 __ mov(lr, Operand(pc));
5844 }
5845#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005846 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005847 __ pop(pc);
5848}
5849
5850
5851void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005852 // Adjust this code if not the case.
5853 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
5854
5855 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005856 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005857 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005858
5859 // Unwind the handlers until the ENTRY handler is found.
5860 Label loop, done;
5861 __ bind(&loop);
5862 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005863 const int kStateOffset = StackHandlerConstants::kStateOffset;
5864 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005865 __ cmp(r2, Operand(StackHandler::ENTRY));
5866 __ b(eq, &done);
5867 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005868 const int kNextOffset = StackHandlerConstants::kNextOffset;
5869 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005870 __ jmp(&loop);
5871 __ bind(&done);
5872
5873 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005874 ASSERT(StackHandlerConstants::kNextOffset == 0);
5875 __ pop(r0);
5876 __ str(r0, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005877
5878 // Set external caught exception to false.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005879 ExternalReference external_caught(Top::k_external_caught_exception_address);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005880 __ mov(r0, Operand(false));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005881 __ mov(r2, Operand(external_caught));
5882 __ str(r0, MemOperand(r2));
5883
5884 // Set pending exception and r0 to out of memory exception.
5885 Failure* out_of_memory = Failure::OutOfMemoryException();
5886 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
5887 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
5888 __ str(r0, MemOperand(r2));
5889
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005890 // Stack layout at this point. See also StackHandlerConstants.
5891 // sp -> state (ENTRY)
5892 // fp
5893 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005894
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005895 // Discard handler state (r2 is not used) and restore frame pointer.
5896 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
5897 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
5898 // Before returning we restore the context from the frame pointer if
5899 // not NULL. The frame pointer is NULL in the exception handler of a
5900 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005901 __ cmp(fp, Operand(0));
5902 // Set cp to NULL if fp is NULL.
5903 __ mov(cp, Operand(0), LeaveCC, eq);
5904 // Restore cp otherwise.
5905 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005906#ifdef DEBUG
5907 if (FLAG_debug_code) {
5908 __ mov(lr, Operand(pc));
5909 }
5910#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005911 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005912 __ pop(pc);
5913}
5914
5915
5916void CEntryStub::GenerateCore(MacroAssembler* masm,
5917 Label* throw_normal_exception,
5918 Label* throw_out_of_memory_exception,
5919 StackFrame::Type frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005920 bool do_gc,
5921 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005922 // r0: result parameter for PerformGC, if any
5923 // r4: number of arguments including receiver (C callee-saved)
5924 // r5: pointer to builtin function (C callee-saved)
5925 // r6: pointer to the first argument (C callee-saved)
5926
5927 if (do_gc) {
5928 // Passing r0.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005929 ExternalReference gc_reference = ExternalReference::perform_gc_function();
5930 __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005931 }
5932
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005933 ExternalReference scope_depth =
5934 ExternalReference::heap_always_allocate_scope_depth();
5935 if (always_allocate) {
5936 __ mov(r0, Operand(scope_depth));
5937 __ ldr(r1, MemOperand(r0));
5938 __ add(r1, r1, Operand(1));
5939 __ str(r1, MemOperand(r0));
5940 }
5941
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005942 // Call C built-in.
5943 // r0 = argc, r1 = argv
5944 __ mov(r0, Operand(r4));
5945 __ mov(r1, Operand(r6));
5946
5947 // TODO(1242173): To let the GC traverse the return address of the exit
5948 // frames, we need to know where the return address is. Right now,
5949 // we push it on the stack to be able to find it again, but we never
5950 // restore from it in case of changes, which makes it impossible to
5951 // support moving the C entry code stub. This should be fixed, but currently
5952 // this is OK because the CEntryStub gets generated so early in the V8 boot
5953 // sequence that it is not moving ever.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005954 masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
5955 masm->push(lr);
5956 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005957
5958 if (always_allocate) {
5959 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
5960 // though (contain the result).
5961 __ mov(r2, Operand(scope_depth));
5962 __ ldr(r3, MemOperand(r2));
5963 __ sub(r3, r3, Operand(1));
5964 __ str(r3, MemOperand(r2));
5965 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005966
5967 // check for failure result
5968 Label failure_returned;
5969 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
5970 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
5971 __ add(r2, r0, Operand(1));
5972 __ tst(r2, Operand(kFailureTagMask));
5973 __ b(eq, &failure_returned);
5974
5975 // Exit C frame and return.
5976 // r0:r1: result
5977 // sp: stack pointer
5978 // fp: frame pointer
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005979 __ LeaveExitFrame(frame_type);
5980
5981 // check if we should retry or throw exception
5982 Label retry;
5983 __ bind(&failure_returned);
5984 ASSERT(Failure::RETRY_AFTER_GC == 0);
5985 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
5986 __ b(eq, &retry);
5987
5988 Label continue_exception;
5989 // If the returned failure is EXCEPTION then promote Top::pending_exception().
5990 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
5991 __ b(ne, &continue_exception);
5992
5993 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00005994 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005995 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00005996 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005997 __ ldr(r0, MemOperand(ip));
5998 __ str(r3, MemOperand(ip));
5999
6000 __ bind(&continue_exception);
6001 // Special handling of out of memory exception.
6002 Failure* out_of_memory = Failure::OutOfMemoryException();
6003 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
6004 __ b(eq, throw_out_of_memory_exception);
6005
6006 // Handle normal exception.
6007 __ jmp(throw_normal_exception);
6008
6009 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
6010}
6011
6012
6013void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
6014 // Called from JavaScript; parameters are on stack as if calling JS function
6015 // r0: number of arguments including receiver
6016 // r1: pointer to builtin function
6017 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006018 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006019 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006020
6021 // NOTE: Invocations of builtins may return failure objects
6022 // instead of a proper result. The builtin entry handles
6023 // this by performing a garbage collection and retrying the
6024 // builtin once.
6025
6026 StackFrame::Type frame_type = is_debug_break
6027 ? StackFrame::EXIT_DEBUG
6028 : StackFrame::EXIT;
6029
6030 // Enter the exit frame that transitions from JavaScript to C++.
6031 __ EnterExitFrame(frame_type);
6032
6033 // r4: number of arguments (C callee-saved)
6034 // r5: pointer to builtin function (C callee-saved)
6035 // r6: pointer to first argument (C callee-saved)
6036
6037 Label throw_out_of_memory_exception;
6038 Label throw_normal_exception;
6039
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006040 // Call into the runtime system. Collect garbage before the call if
6041 // running with --gc-greedy set.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006042 if (FLAG_gc_greedy) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00006043 Failure* failure = Failure::RetryAfterGC(0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006044 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
6045 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006046 GenerateCore(masm, &throw_normal_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006047 &throw_out_of_memory_exception,
6048 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006049 FLAG_gc_greedy,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006050 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006051
6052 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006053 GenerateCore(masm,
6054 &throw_normal_exception,
6055 &throw_out_of_memory_exception,
6056 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006057 true,
6058 false);
6059
6060 // Do full GC and retry runtime call one final time.
6061 Failure* failure = Failure::InternalError();
6062 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
6063 GenerateCore(masm,
6064 &throw_normal_exception,
6065 &throw_out_of_memory_exception,
6066 frame_type,
6067 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006068 true);
6069
6070 __ bind(&throw_out_of_memory_exception);
6071 GenerateThrowOutOfMemory(masm);
6072 // control flow for generated will not return.
6073
6074 __ bind(&throw_normal_exception);
6075 GenerateThrowTOS(masm);
6076}
6077
6078
6079void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
6080 // r0: code entry
6081 // r1: function
6082 // r2: receiver
6083 // r3: argc
6084 // [sp+0]: argv
6085
6086 Label invoke, exit;
6087
6088 // Called from C, so do not pop argc and args on exit (preserve sp)
6089 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006090 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006091 __ stm(db_w, sp, kCalleeSaved | lr.bit());
6092
6093 // Get address of argv, see stm above.
6094 // r0: code entry
6095 // r1: function
6096 // r2: receiver
6097 // r3: argc
6098 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
6099 __ ldr(r4, MemOperand(r4)); // argv
6100
6101 // Push a frame with special values setup to mark it as an entry frame.
6102 // r0: code entry
6103 // r1: function
6104 // r2: receiver
6105 // r3: argc
6106 // r4: argv
6107 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
6108 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
6109 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
6110 __ mov(r6, Operand(Smi::FromInt(marker)));
6111 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6112 __ ldr(r5, MemOperand(r5));
6113 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
6114
6115 // Setup frame pointer for the frame to be pushed.
6116 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6117
6118 // Call a faked try-block that does the invoke.
6119 __ bl(&invoke);
6120
6121 // Caught exception: Store result (exception) in the pending
6122 // exception field in the JSEnv and return a failure sentinel.
6123 // Coming in here the fp will be invalid because the PushTryHandler below
6124 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00006125 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006126 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006127 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006128 __ b(&exit);
6129
6130 // Invoke: Link this frame into the handler chain.
6131 __ bind(&invoke);
6132 // Must preserve r0-r4, r5-r7 are available.
6133 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006134 // If an exception not caught by another handler occurs, this handler
6135 // returns control to the code after the bl(&invoke) above, which
6136 // restores all kCalleeSaved registers (including cp and fp) to their
6137 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006138
6139 // Clear any pending exceptions.
6140 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
6141 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00006142 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006143 __ str(r5, MemOperand(ip));
6144
6145 // Invoke the function by calling through JS entry trampoline builtin.
6146 // Notice that we cannot store a reference to the trampoline code directly in
6147 // this stub, because runtime stubs are not traversed when doing GC.
6148
6149 // Expected registers by Builtins::JSEntryTrampoline
6150 // r0: code entry
6151 // r1: function
6152 // r2: receiver
6153 // r3: argc
6154 // r4: argv
6155 if (is_construct) {
6156 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
6157 __ mov(ip, Operand(construct_entry));
6158 } else {
6159 ExternalReference entry(Builtins::JSEntryTrampoline);
6160 __ mov(ip, Operand(entry));
6161 }
6162 __ ldr(ip, MemOperand(ip)); // deref address
6163
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006164 // Branch and link to JSEntryTrampoline. We don't use the double underscore
6165 // macro for the add instruction because we don't want the coverage tool
6166 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006167 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006168 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006169
6170 // Unlink this frame from the handler chain. When reading the
6171 // address of the next handler, there is no need to use the address
6172 // displacement since the current stack pointer (sp) points directly
6173 // to the stack handler.
6174 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
6175 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
6176 __ str(r3, MemOperand(ip));
6177 // No need to restore registers
6178 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
6179
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006180
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006181 __ bind(&exit); // r0 holds result
6182 // Restore the top frame descriptors from the stack.
6183 __ pop(r3);
6184 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6185 __ str(r3, MemOperand(ip));
6186
6187 // Reset the stack to the callee saved registers.
6188 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6189
6190 // Restore callee-saved registers and return.
6191#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006192 if (FLAG_debug_code) {
6193 __ mov(lr, Operand(pc));
6194 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006195#endif
6196 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
6197}
6198
6199
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006200// This stub performs an instanceof, calling the builtin function if
6201// necessary. Uses r1 for the object, r0 for the function that it may
6202// be an instance of (these are fetched from the stack).
6203void InstanceofStub::Generate(MacroAssembler* masm) {
6204 // Get the object - slow case for smis (we may need to throw an exception
6205 // depending on the rhs).
6206 Label slow, loop, is_instance, is_not_instance;
6207 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
6208 __ BranchOnSmi(r0, &slow);
6209
6210 // Check that the left hand is a JS object and put map in r3.
6211 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
6212 __ b(lt, &slow);
6213 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
6214 __ b(gt, &slow);
6215
6216 // Get the prototype of the function (r4 is result, r2 is scratch).
6217 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
6218 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
6219
6220 // Check that the function prototype is a JS object.
6221 __ BranchOnSmi(r4, &slow);
6222 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
6223 __ b(lt, &slow);
6224 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
6225 __ b(gt, &slow);
6226
6227 // Register mapping: r3 is object map and r4 is function prototype.
6228 // Get prototype of object into r2.
6229 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
6230
6231 // Loop through the prototype chain looking for the function prototype.
6232 __ bind(&loop);
6233 __ cmp(r2, Operand(r4));
6234 __ b(eq, &is_instance);
6235 __ cmp(r2, Operand(Factory::null_value()));
6236 __ b(eq, &is_not_instance);
6237 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
6238 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
6239 __ jmp(&loop);
6240
6241 __ bind(&is_instance);
6242 __ mov(r0, Operand(Smi::FromInt(0)));
6243 __ pop();
6244 __ pop();
6245 __ mov(pc, Operand(lr)); // Return.
6246
6247 __ bind(&is_not_instance);
6248 __ mov(r0, Operand(Smi::FromInt(1)));
6249 __ pop();
6250 __ pop();
6251 __ mov(pc, Operand(lr)); // Return.
6252
6253 // Slow-case. Tail call builtin.
6254 __ bind(&slow);
6255 __ mov(r0, Operand(1)); // Arg count without receiver.
6256 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
6257}
6258
6259
ager@chromium.org7c537e22008-10-16 08:43:32 +00006260void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006261 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006262 Label adaptor;
6263 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6264 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
6265 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006266 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006267
ager@chromium.org7c537e22008-10-16 08:43:32 +00006268 // Nothing to do: The formal number of parameters has already been
6269 // passed in register r0 by calling function. Just return it.
ager@chromium.org9085a012009-05-11 19:22:57 +00006270 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006271
ager@chromium.org7c537e22008-10-16 08:43:32 +00006272 // Arguments adaptor case: Read the arguments length from the
6273 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006274 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006275 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org9085a012009-05-11 19:22:57 +00006276 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006277}
6278
6279
ager@chromium.org7c537e22008-10-16 08:43:32 +00006280void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
6281 // The displacement is the offset of the last parameter (if any)
6282 // relative to the frame pointer.
6283 static const int kDisplacement =
6284 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006285
ager@chromium.org7c537e22008-10-16 08:43:32 +00006286 // Check that the key is a smi.
6287 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006288 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006289
ager@chromium.org7c537e22008-10-16 08:43:32 +00006290 // Check if the calling frame is an arguments adaptor frame.
6291 Label adaptor;
6292 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6293 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
6294 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
6295 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006296
ager@chromium.org7c537e22008-10-16 08:43:32 +00006297 // Check index against formal parameters count limit passed in
6298 // through register eax. Use unsigned comparison to get negative
6299 // check for free.
6300 __ cmp(r1, r0);
6301 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006302
ager@chromium.org7c537e22008-10-16 08:43:32 +00006303 // Read the argument from the stack and return it.
6304 __ sub(r3, r0, r1);
6305 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6306 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006307 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006308
6309 // Arguments adaptor case: Check index against actual arguments
6310 // limit found in the arguments adaptor frame. Use unsigned
6311 // comparison to get negative check for free.
6312 __ bind(&adaptor);
6313 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6314 __ cmp(r1, r0);
6315 __ b(cs, &slow);
6316
6317 // Read the argument from the adaptor frame and return it.
6318 __ sub(r3, r0, r1);
6319 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6320 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006321 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006322
6323 // Slow-case: Handle non-smi or out-of-bounds access to arguments
6324 // by calling the runtime system.
6325 __ bind(&slow);
6326 __ push(r1);
6327 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
6328}
6329
6330
6331void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
6332 // Check if the calling frame is an arguments adaptor frame.
6333 Label runtime;
6334 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6335 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
6336 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
6337 __ b(ne, &runtime);
6338
6339 // Patch the arguments.length and the parameters pointer.
6340 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6341 __ str(r0, MemOperand(sp, 0 * kPointerSize));
6342 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
6343 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
6344 __ str(r3, MemOperand(sp, 1 * kPointerSize));
6345
6346 // Do the runtime call to allocate the arguments object.
6347 __ bind(&runtime);
6348 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006349}
6350
6351
6352void CallFunctionStub::Generate(MacroAssembler* masm) {
6353 Label slow;
6354 // Get the function to call from the stack.
6355 // function, receiver [, arguments]
6356 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
6357
6358 // Check that the function is really a JavaScript function.
6359 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006360 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006361 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006362 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006363 __ b(ne, &slow);
6364
6365 // Fast-case: Invoke the function now.
6366 // r1: pushed function
6367 ParameterCount actual(argc_);
6368 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
6369
6370 // Slow-case: Non-function called.
6371 __ bind(&slow);
6372 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006373 __ mov(r2, Operand(0));
6374 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
6375 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
6376 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006377}
6378
6379
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006380int CompareStub::MinorKey() {
6381 // Encode the two parameters in a unique 16 bit value.
6382 ASSERT(static_cast<unsigned>(cc_) >> 28 < (1 << 15));
6383 return (static_cast<unsigned>(cc_) >> 27) | (strict_ ? 1 : 0);
6384}
6385
6386
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006387#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006388
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006389} } // namespace v8::internal