blob: 5f8149e2eaa514308bdce58ea10e3980d9ece3db [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_;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002424 LoadConditionAndSpill(node->condition(), NOT_INSIDE_TYPEOF,
2425 &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002426 if (has_valid_frame()) {
2427 Branch(false, &else_);
2428 }
2429 if (has_valid_frame() || then.is_linked()) {
2430 then.Bind();
2431 LoadAndSpill(node->then_expression(), typeof_state());
2432 }
2433 if (else_.is_linked()) {
2434 JumpTarget exit;
2435 if (has_valid_frame()) exit.Jump();
2436 else_.Bind();
2437 LoadAndSpill(node->else_expression(), typeof_state());
2438 if (exit.is_linked()) exit.Bind();
2439 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002440 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002441}
2442
2443
ager@chromium.org7c537e22008-10-16 08:43:32 +00002444void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002445 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002446 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002447 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002448
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002449 JumpTarget slow;
2450 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002451
2452 // Generate fast-case code for variables that might be shadowed by
2453 // eval-introduced variables. Eval is used a lot without
2454 // introducing variables. In those cases, we do not want to
2455 // perform a runtime call for all variables in the scope
2456 // containing the eval.
2457 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2458 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002459 // If there was no control flow to slow, we can exit early.
2460 if (!slow.is_linked()) {
2461 frame_->EmitPush(r0);
2462 return;
2463 }
2464
2465 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002466
2467 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2468 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2469 // Only generate the fast case for locals that rewrite to slots.
2470 // This rules out argument loads.
2471 if (potential_slot != NULL) {
2472 __ ldr(r0,
2473 ContextSlotOperandCheckExtensions(potential_slot,
2474 r1,
2475 r2,
2476 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002477 if (potential_slot->var()->mode() == Variable::CONST) {
2478 __ cmp(r0, Operand(Factory::the_hole_value()));
2479 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
2480 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002481 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002482 // ContextSlotOperandCheckExtensions so we have to jump around
2483 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002484 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002485 }
2486 }
2487
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002488 slow.Bind();
2489 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002490 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002491 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002492
ager@chromium.org7c537e22008-10-16 08:43:32 +00002493 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002494 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002495 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002496 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002497 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002498
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002499 done.Bind();
2500 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002501
2502 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002503 // Note: We would like to keep the assert below, but it fires because of
2504 // some nasty code in LoadTypeofExpression() which should be removed...
ager@chromium.org381abbb2009-02-25 13:23:22 +00002505 // ASSERT(!slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002506
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002507 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002508 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002509 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002510 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002511 // Const slots may contain 'the hole' value (the constant hasn't been
2512 // initialized yet) which needs to be converted into the 'undefined'
2513 // value.
2514 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002515 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002516 __ cmp(r0, Operand(Factory::the_hole_value()));
2517 __ mov(r0, Operand(Factory::undefined_value()), LeaveCC, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002518 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002519 }
2520 }
2521}
2522
2523
ager@chromium.org381abbb2009-02-25 13:23:22 +00002524void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2525 TypeofState typeof_state,
2526 Register tmp,
2527 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002528 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002529 // Check that no extension objects have been created by calls to
2530 // eval from the current scope to the global scope.
2531 Register context = cp;
2532 Scope* s = scope();
2533 while (s != NULL) {
2534 if (s->num_heap_slots() > 0) {
2535 if (s->calls_eval()) {
2536 // Check that extension is NULL.
2537 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2538 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002539 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002540 }
2541 // Load next context in chain.
2542 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2543 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2544 context = tmp;
2545 }
2546 // If no outer scope calls eval, we do not need to check more
2547 // context extensions.
2548 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2549 s = s->outer_scope();
2550 }
2551
2552 if (s->is_eval_scope()) {
2553 Label next, fast;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002554 if (!context.is(tmp)) {
2555 __ mov(tmp, Operand(context));
2556 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002557 __ bind(&next);
2558 // Terminate at global context.
2559 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
2560 __ cmp(tmp2, Operand(Factory::global_context_map()));
2561 __ b(eq, &fast);
2562 // Check that extension is NULL.
2563 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2564 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002565 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002566 // Load next context in chain.
2567 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2568 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2569 __ b(&next);
2570 __ bind(&fast);
2571 }
2572
2573 // All extension objects were empty and it is safe to use a global
2574 // load IC call.
2575 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2576 // Load the global object.
2577 LoadGlobal();
2578 // Setup the name register.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002579 Result name = allocator_->Allocate(r2);
2580 ASSERT(name.is_valid()); // We are in spilled code.
2581 __ mov(name.reg(), Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002582 // Call IC stub.
2583 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002584 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002585 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002586 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, &name, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002587 }
2588
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002589 // Drop the global object. The result is in r0.
2590 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002591}
2592
2593
ager@chromium.org7c537e22008-10-16 08:43:32 +00002594void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002595#ifdef DEBUG
2596 int original_height = frame_->height();
2597#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002598 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002599 Comment cmnt(masm_, "[ Slot");
2600 LoadFromSlot(node, typeof_state());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002601 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002602}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002603
ager@chromium.org7c537e22008-10-16 08:43:32 +00002604
2605void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002606#ifdef DEBUG
2607 int original_height = frame_->height();
2608#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002609 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002610 Comment cmnt(masm_, "[ VariableProxy");
2611
2612 Variable* var = node->var();
2613 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002614 if (expr != NULL) {
2615 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002616 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002617 ASSERT(var->is_global());
2618 Reference ref(this, node);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002619 ref.GetValueAndSpill(typeof_state());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002620 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002621 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002622}
2623
2624
ager@chromium.org7c537e22008-10-16 08:43:32 +00002625void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002626#ifdef DEBUG
2627 int original_height = frame_->height();
2628#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002629 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002630 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002631 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002632 frame_->EmitPush(r0);
2633 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002634}
2635
2636
ager@chromium.org7c537e22008-10-16 08:43:32 +00002637void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002638#ifdef DEBUG
2639 int original_height = frame_->height();
2640#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002641 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002642 Comment cmnt(masm_, "[ RexExp Literal");
2643
2644 // Retrieve the literal array and check the allocated entry.
2645
2646 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002647 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002648
2649 // Load the literals array of the function.
2650 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2651
2652 // Load the literal at the ast saved index.
2653 int literal_offset =
2654 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2655 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2656
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002657 JumpTarget done;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002658 __ cmp(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002659 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002660
2661 // If the entry is undefined we call the runtime system to computed
2662 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002663 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002664 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002665 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002666 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002667 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002668 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002669 frame_->EmitPush(r0);
2670 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002671 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002672
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002673 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002674 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002675 frame_->EmitPush(r2);
2676 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002677}
2678
2679
2680// This deferred code stub will be used for creating the boilerplate
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002681// by calling Runtime_CreateObjectLiteralBoilerplate.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002682// Each created boilerplate is stored in the JSFunction and they are
2683// therefore context dependent.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002684class DeferredObjectLiteral: public DeferredCode {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002685 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002686 explicit DeferredObjectLiteral(ObjectLiteral* node) : node_(node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002687 set_comment("[ DeferredObjectLiteral");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002688 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002689
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002690 virtual void Generate();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002691
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002692 private:
2693 ObjectLiteral* node_;
2694};
2695
2696
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002697void DeferredObjectLiteral::Generate() {
2698 // Argument is passed in r1.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002699
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002700 // If the entry is undefined we call the runtime system to compute
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002701 // the literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002702 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002703 __ push(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002704 // Literal index (1).
mads.s.ager31e71382008-08-13 09:32:07 +00002705 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002706 __ push(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002707 // Constant properties (2).
mads.s.ager31e71382008-08-13 09:32:07 +00002708 __ mov(r0, Operand(node_->constant_properties()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002709 __ push(r0);
2710 __ CallRuntime(Runtime::kCreateObjectLiteralBoilerplate, 3);
2711 __ mov(r2, Operand(r0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002712 // Result is returned in r2.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002713}
2714
2715
ager@chromium.org7c537e22008-10-16 08:43:32 +00002716void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002717#ifdef DEBUG
2718 int original_height = frame_->height();
2719#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002720 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002721 Comment cmnt(masm_, "[ ObjectLiteral");
2722
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002723 DeferredObjectLiteral* deferred = new DeferredObjectLiteral(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002724
2725 // Retrieve the literal array and check the allocated entry.
2726
2727 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002728 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002729
2730 // Load the literals array of the function.
2731 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2732
2733 // Load the literal at the ast saved index.
2734 int literal_offset =
2735 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2736 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2737
2738 // Check whether we need to materialize the object literal boilerplate.
2739 // If so, jump to the deferred code.
2740 __ cmp(r2, Operand(Factory::undefined_value()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002741 deferred->Branch(eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002742 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002743
2744 // Push the object literal boilerplate.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002745 frame_->EmitPush(r2);
mads.s.ager31e71382008-08-13 09:32:07 +00002746
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002747 // Clone the boilerplate object.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002748 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2749 if (node->depth() == 1) {
2750 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2751 }
2752 frame_->CallRuntime(clone_function_id, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002753 frame_->EmitPush(r0); // save the result
mads.s.ager31e71382008-08-13 09:32:07 +00002754 // r0: cloned object literal
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002755
2756 for (int i = 0; i < node->properties()->length(); i++) {
2757 ObjectLiteral::Property* property = node->properties()->at(i);
2758 Literal* key = property->key();
2759 Expression* value = property->value();
2760 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002761 case ObjectLiteral::Property::CONSTANT:
2762 break;
2763 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2764 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
2765 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002766 case ObjectLiteral::Property::COMPUTED: // fall through
2767 case ObjectLiteral::Property::PROTOTYPE: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002768 frame_->EmitPush(r0); // dup the result
2769 LoadAndSpill(key);
2770 LoadAndSpill(value);
2771 frame_->CallRuntime(Runtime::kSetProperty, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00002772 // restore r0
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002773 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002774 break;
2775 }
2776 case ObjectLiteral::Property::SETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002777 frame_->EmitPush(r0);
2778 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002779 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002780 frame_->EmitPush(r0);
2781 LoadAndSpill(value);
2782 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002783 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002784 break;
2785 }
2786 case ObjectLiteral::Property::GETTER: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002787 frame_->EmitPush(r0);
2788 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002789 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002790 frame_->EmitPush(r0);
2791 LoadAndSpill(value);
2792 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002793 __ ldr(r0, frame_->Top());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002794 break;
2795 }
2796 }
2797 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002798 ASSERT(frame_->height() == original_height + 1);
2799}
2800
2801
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002802// This deferred code stub will be used for creating the boilerplate
2803// by calling Runtime_CreateArrayLiteralBoilerplate.
2804// Each created boilerplate is stored in the JSFunction and they are
2805// therefore context dependent.
2806class DeferredArrayLiteral: public DeferredCode {
2807 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002808 explicit DeferredArrayLiteral(ArrayLiteral* node) : node_(node) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002809 set_comment("[ DeferredArrayLiteral");
2810 }
2811
2812 virtual void Generate();
2813
2814 private:
2815 ArrayLiteral* node_;
2816};
2817
2818
2819void DeferredArrayLiteral::Generate() {
2820 // Argument is passed in r1.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002821
2822 // If the entry is undefined we call the runtime system to computed
2823 // the literal.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002824 // Literal array (0).
ager@chromium.orge2902be2009-06-08 12:21:35 +00002825 __ push(r1);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002826 // Literal index (1).
2827 __ mov(r0, Operand(Smi::FromInt(node_->literal_index())));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002828 __ push(r0);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002829 // Constant properties (2).
2830 __ mov(r0, Operand(node_->literals()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002831 __ push(r0);
2832 __ CallRuntime(Runtime::kCreateArrayLiteralBoilerplate, 3);
2833 __ mov(r2, Operand(r0));
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002834 // Result is returned in r2.
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002835}
2836
2837
ager@chromium.org7c537e22008-10-16 08:43:32 +00002838void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002839#ifdef DEBUG
2840 int original_height = frame_->height();
2841#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002842 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002843 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002844
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +00002845 DeferredArrayLiteral* deferred = new DeferredArrayLiteral(node);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002846
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002847 // Retrieve the literal array and check the allocated entry.
2848
2849 // Load the function of this activation.
2850 __ ldr(r1, frame_->Function());
2851
2852 // Load the literals array of the function.
2853 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2854
2855 // Load the literal at the ast saved index.
2856 int literal_offset =
2857 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2858 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2859
2860 // Check whether we need to materialize the object literal boilerplate.
2861 // If so, jump to the deferred code.
2862 __ cmp(r2, Operand(Factory::undefined_value()));
ager@chromium.orge2902be2009-06-08 12:21:35 +00002863 deferred->Branch(eq);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002864 deferred->BindExit();
2865
2866 // Push the object literal boilerplate.
2867 frame_->EmitPush(r2);
2868
2869 // Clone the boilerplate object.
2870 Runtime::FunctionId clone_function_id = Runtime::kCloneLiteralBoilerplate;
2871 if (node->depth() == 1) {
2872 clone_function_id = Runtime::kCloneShallowLiteralBoilerplate;
2873 }
2874 frame_->CallRuntime(clone_function_id, 1);
2875 frame_->EmitPush(r0); // save the result
2876 // r0: cloned object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002877
2878 // Generate code to set the elements in the array that are not
2879 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002880 for (int i = 0; i < node->values()->length(); i++) {
2881 Expression* value = node->values()->at(i);
2882
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002883 // If value is a literal the property value is already set in the
2884 // boilerplate object.
2885 if (value->AsLiteral() != NULL) continue;
2886 // If value is a materialized literal the property value is already set
2887 // in the boilerplate object if it is simple.
2888 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002889
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002890 // The property must be set by generated code.
2891 LoadAndSpill(value);
2892 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002893
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002894 // Fetch the object literal.
2895 __ ldr(r1, frame_->Top());
2896 // Get the elements array.
2897 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002898
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002899 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002900 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002901 __ str(r0, FieldMemOperand(r1, offset));
2902
2903 // Update the write barrier for the array address.
2904 __ mov(r3, Operand(offset));
2905 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002906 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002907 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002908}
2909
2910
ager@chromium.org32912102009-01-16 10:38:43 +00002911void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002912#ifdef DEBUG
2913 int original_height = frame_->height();
2914#endif
2915 ASSERT(!in_spilled_code());
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002916 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org32912102009-01-16 10:38:43 +00002917 // Call runtime routine to allocate the catch extension object and
2918 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002919 Comment cmnt(masm_, "[ CatchExtensionObject");
2920 LoadAndSpill(node->key());
2921 LoadAndSpill(node->value());
2922 Result result =
2923 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2924 frame_->EmitPush(result.reg());
2925 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002926}
2927
2928
ager@chromium.org7c537e22008-10-16 08:43:32 +00002929void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002930#ifdef DEBUG
2931 int original_height = frame_->height();
2932#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002933 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002934 Comment cmnt(masm_, "[ Assignment");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002935 CodeForStatementPosition(node);
mads.s.ager31e71382008-08-13 09:32:07 +00002936
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002937 { Reference target(this, node->target());
2938 if (target.is_illegal()) {
2939 // Fool the virtual frame into thinking that we left the assignment's
2940 // value on the frame.
2941 __ mov(r0, Operand(Smi::FromInt(0)));
2942 frame_->EmitPush(r0);
2943 ASSERT(frame_->height() == original_height + 1);
2944 return;
2945 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002946
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002947 if (node->op() == Token::ASSIGN ||
2948 node->op() == Token::INIT_VAR ||
2949 node->op() == Token::INIT_CONST) {
2950 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002951
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002952 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002953 // +=, *= and similar binary assignments.
2954 // Get the old value of the lhs.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002955 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
2956 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002957 bool overwrite =
2958 (node->value()->AsBinaryOperation() != NULL &&
2959 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002960 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002961 SmiOperation(node->binary_op(),
2962 literal->handle(),
2963 false,
2964 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002965 frame_->EmitPush(r0);
2966
2967 } else {
2968 LoadAndSpill(node->value());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002969 GenericBinaryOperation(node->binary_op(),
2970 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002971 frame_->EmitPush(r0);
2972 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002973 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002974
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002975 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2976 if (var != NULL &&
2977 (var->mode() == Variable::CONST) &&
2978 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2979 // Assignment ignored - leave the value on the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002980
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002981 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002982 CodeForSourcePosition(node->position());
2983 if (node->op() == Token::INIT_CONST) {
2984 // Dynamic constant initializations must use the function context
2985 // and initialize the actual constant declared. Dynamic variable
2986 // initializations are simply assignments and use SetValue.
2987 target.SetValue(CONST_INIT);
2988 } else {
2989 target.SetValue(NOT_CONST_INIT);
2990 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002991 }
2992 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002993 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002994}
2995
2996
ager@chromium.org7c537e22008-10-16 08:43:32 +00002997void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002998#ifdef DEBUG
2999 int original_height = frame_->height();
3000#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003001 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003002 Comment cmnt(masm_, "[ Throw");
3003
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003004 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003005 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003006 frame_->CallRuntime(Runtime::kThrow, 1);
3007 frame_->EmitPush(r0);
3008 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003009}
3010
3011
ager@chromium.org7c537e22008-10-16 08:43:32 +00003012void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003013#ifdef DEBUG
3014 int original_height = frame_->height();
3015#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003016 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003017 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003018
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003019 { Reference property(this, node);
3020 property.GetValueAndSpill(typeof_state());
3021 }
3022 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003023}
3024
3025
ager@chromium.org7c537e22008-10-16 08:43:32 +00003026void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003027#ifdef DEBUG
3028 int original_height = frame_->height();
3029#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003030 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003031 Comment cmnt(masm_, "[ Call");
3032
3033 ZoneList<Expression*>* args = node->arguments();
3034
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003035 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003036 // Standard function call.
3037
3038 // Check if the function is a variable or a property.
3039 Expression* function = node->expression();
3040 Variable* var = function->AsVariableProxy()->AsVariable();
3041 Property* property = function->AsProperty();
3042
3043 // ------------------------------------------------------------------------
3044 // Fast-case: Use inline caching.
3045 // ---
3046 // According to ECMA-262, section 11.2.3, page 44, the function to call
3047 // must be resolved after the arguments have been evaluated. The IC code
3048 // automatically handles this by loading the arguments before the function
3049 // is resolved in cache misses (this also holds for megamorphic calls).
3050 // ------------------------------------------------------------------------
3051
3052 if (var != NULL && !var->is_this() && var->is_global()) {
3053 // ----------------------------------
3054 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3055 // ----------------------------------
3056
3057 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003058 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003059 frame_->EmitPush(r0);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003060
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003061 // Pass the global object as the receiver and let the IC stub
3062 // patch the stack to use the global proxy as 'this' in the
3063 // invoked function.
3064 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003065
3066 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003067 int arg_count = args->length();
3068 for (int i = 0; i < arg_count; i++) {
3069 LoadAndSpill(args->at(i));
3070 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003071
3072 // Setup the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003073 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3074 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003075 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003076 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3077 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003078 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003079 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003080 frame_->Drop();
3081 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003082
3083 } else if (var != NULL && var->slot() != NULL &&
3084 var->slot()->type() == Slot::LOOKUP) {
3085 // ----------------------------------
3086 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3087 // ----------------------------------
3088
3089 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003090 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003091 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003092 frame_->EmitPush(r0);
3093 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003094 // r0: slot value; r1: receiver
3095
3096 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003097 frame_->EmitPush(r0); // function
3098 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003099
3100 // Call the function.
3101 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003102 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003103
3104 } else if (property != NULL) {
3105 // Check if the key is a literal string.
3106 Literal* literal = property->key()->AsLiteral();
3107
3108 if (literal != NULL && literal->handle()->IsSymbol()) {
3109 // ------------------------------------------------------------------
3110 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3111 // ------------------------------------------------------------------
3112
3113 // Push the name of the function and the receiver onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00003114 __ mov(r0, Operand(literal->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003115 frame_->EmitPush(r0);
3116 LoadAndSpill(property->obj());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003117
3118 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003119 int arg_count = args->length();
3120 for (int i = 0; i < arg_count; i++) {
3121 LoadAndSpill(args->at(i));
3122 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003123
3124 // Set the receiver register and call the IC initialization code.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003125 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3126 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003127 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003128 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003129 __ ldr(cp, frame_->Context());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003130
3131 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003132 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00003133
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003134 frame_->EmitPush(r0); // push after get rid of function from the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003135
3136 } else {
3137 // -------------------------------------------
3138 // JavaScript example: 'array[index](1, 2, 3)'
3139 // -------------------------------------------
3140
3141 // Load the function to call from the property through a reference.
3142 Reference ref(this, property);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003143 ref.GetValueAndSpill(NOT_INSIDE_TYPEOF); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003144
3145 // Pass receiver to called function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003146 if (property->is_synthetic()) {
3147 LoadGlobalReceiver(r0);
3148 } else {
3149 __ ldr(r0, frame_->ElementAt(ref.size()));
3150 frame_->EmitPush(r0);
3151 }
3152
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003153 // Call the function.
3154 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003155 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003156 }
3157
3158 } else {
3159 // ----------------------------------
3160 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3161 // ----------------------------------
3162
3163 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003164 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003165
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003166 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003167 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003168
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003169 // Call the function.
3170 CallWithArguments(args, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003171 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003172 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003173 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003174}
3175
3176
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003177void CodeGenerator::VisitCallEval(CallEval* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003178#ifdef DEBUG
3179 int original_height = frame_->height();
3180#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003181 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003182 Comment cmnt(masm_, "[ CallEval");
3183
3184 // In a call to eval, we first call %ResolvePossiblyDirectEval to resolve
3185 // the function we need to call and the receiver of the call.
3186 // Then we call the resolved function using the given arguments.
3187
3188 ZoneList<Expression*>* args = node->arguments();
3189 Expression* function = node->expression();
3190
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003191 CodeForStatementPosition(node);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003192
3193 // Prepare stack for call to resolved function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003194 LoadAndSpill(function);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003195 __ mov(r2, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003196 frame_->EmitPush(r2); // Slot for receiver
3197 int arg_count = args->length();
3198 for (int i = 0; i < arg_count; i++) {
3199 LoadAndSpill(args->at(i));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003200 }
3201
3202 // Prepare stack for call to ResolvePossiblyDirectEval.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003203 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3204 frame_->EmitPush(r1);
3205 if (arg_count > 0) {
3206 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3207 frame_->EmitPush(r1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003208 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003209 frame_->EmitPush(r2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003210 }
3211
3212 // Resolve the call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003213 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 2);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003214
3215 // Touch up stack with the right values for the function and the receiver.
3216 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003217 __ str(r1, MemOperand(sp, (arg_count + 1) * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003218 __ ldr(r1, FieldMemOperand(r0, FixedArray::kHeaderSize + kPointerSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003219 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003220
3221 // Call the function.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003222 CodeForSourcePosition(node->position());
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003223
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003224 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3225 CallFunctionStub call_function(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003226 frame_->CallStub(&call_function, arg_count + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003227
3228 __ ldr(cp, frame_->Context());
3229 // Remove the function from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003230 frame_->Drop();
3231 frame_->EmitPush(r0);
3232 ASSERT(frame_->height() == original_height + 1);
ager@chromium.orga74f0da2008-12-03 16:05:52 +00003233}
3234
3235
ager@chromium.org7c537e22008-10-16 08:43:32 +00003236void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003237#ifdef DEBUG
3238 int original_height = frame_->height();
3239#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003240 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003241 Comment cmnt(masm_, "[ CallNew");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003242 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003243
3244 // According to ECMA-262, section 11.2.2, page 44, the function
3245 // expression in new calls must be evaluated before the
3246 // arguments. This is different from ordinary calls, where the
3247 // actual function to call is resolved after the arguments have been
3248 // evaluated.
3249
3250 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003251 // receiver. There is no need to use the global proxy here because
3252 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003253 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003254 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003255
3256 // Push the arguments ("left-to-right") on the stack.
3257 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003258 int arg_count = args->length();
3259 for (int i = 0; i < arg_count; i++) {
3260 LoadAndSpill(args->at(i));
3261 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003262
mads.s.ager31e71382008-08-13 09:32:07 +00003263 // r0: the number of arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003264 Result num_args = allocator_->Allocate(r0);
3265 ASSERT(num_args.is_valid());
3266 __ mov(num_args.reg(), Operand(arg_count));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003267
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003268 // Load the function into r1 as per calling convention.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003269 Result function = allocator_->Allocate(r1);
3270 ASSERT(function.is_valid());
3271 __ ldr(function.reg(), frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003272
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003273 // Call the construct call builtin that handles allocation and
3274 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003275 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003276 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
3277 Result result = frame_->CallCodeObject(ic,
3278 RelocInfo::CONSTRUCT_CALL,
3279 &num_args,
3280 &function,
3281 arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003282
3283 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003284 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003285 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003286}
3287
3288
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003289void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
3290 VirtualFrame::SpilledScope spilled_scope;
3291 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003292 JumpTarget leave, null, function, non_function_constructor;
3293
3294 // Load the object into r0.
3295 LoadAndSpill(args->at(0));
3296 frame_->EmitPop(r0);
3297
3298 // If the object is a smi, we return null.
3299 __ tst(r0, Operand(kSmiTagMask));
3300 null.Branch(eq);
3301
3302 // Check that the object is a JS object but take special care of JS
3303 // functions to make sure they have 'Function' as their class.
3304 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
3305 null.Branch(lt);
3306
3307 // As long as JS_FUNCTION_TYPE is the last instance type and it is
3308 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
3309 // LAST_JS_OBJECT_TYPE.
3310 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3311 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
3312 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3313 function.Branch(eq);
3314
3315 // Check if the constructor in the map is a function.
3316 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
3317 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
3318 non_function_constructor.Branch(ne);
3319
3320 // The r0 register now contains the constructor function. Grab the
3321 // instance class name from there.
3322 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
3323 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003324 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003325 leave.Jump();
3326
3327 // Functions have class 'Function'.
3328 function.Bind();
3329 __ mov(r0, Operand(Factory::function_class_symbol()));
3330 frame_->EmitPush(r0);
3331 leave.Jump();
3332
3333 // Objects with a non-function constructor have class 'Object'.
3334 non_function_constructor.Bind();
3335 __ mov(r0, Operand(Factory::Object_symbol()));
3336 frame_->EmitPush(r0);
3337 leave.Jump();
3338
3339 // Non-JS objects have class null.
3340 null.Bind();
3341 __ mov(r0, Operand(Factory::null_value()));
3342 frame_->EmitPush(r0);
3343
3344 // All done.
3345 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003346}
3347
3348
ager@chromium.org7c537e22008-10-16 08:43:32 +00003349void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003350 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003351 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003352 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003353 LoadAndSpill(args->at(0));
3354 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003355 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003356 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003357 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003358 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3359 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003360 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003361 // Load the value.
3362 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003363 leave.Bind();
3364 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003365}
3366
3367
ager@chromium.org7c537e22008-10-16 08:43:32 +00003368void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003369 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003370 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003371 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003372 LoadAndSpill(args->at(0)); // Load the object.
3373 LoadAndSpill(args->at(1)); // Load the value.
3374 frame_->EmitPop(r0); // r0 contains value
3375 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003376 // if (object->IsSmi()) return object.
3377 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003378 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003379 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3380 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003381 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003382 // Store the value.
3383 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3384 // Update the write barrier.
3385 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3386 __ RecordWrite(r1, r2, r3);
3387 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003388 leave.Bind();
3389 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003390}
3391
3392
ager@chromium.org7c537e22008-10-16 08:43:32 +00003393void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003394 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003395 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003396 LoadAndSpill(args->at(0));
3397 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003398 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003399 cc_reg_ = eq;
3400}
3401
3402
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003403void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003404 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003405 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3406 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003407#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003408 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003409 LoadAndSpill(args->at(1));
3410 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003411 __ CallRuntime(Runtime::kLog, 2);
3412 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003413#endif
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003414 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003415 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003416}
3417
3418
ager@chromium.org7c537e22008-10-16 08:43:32 +00003419void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003420 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003421 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003422 LoadAndSpill(args->at(0));
3423 frame_->EmitPop(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003424 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003425 cc_reg_ = eq;
3426}
3427
3428
kasper.lund7276f142008-07-30 08:49:36 +00003429// This should generate code that performs a charCodeAt() call or returns
3430// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3431// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003432void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003433 VirtualFrame::SpilledScope spilled_scope;
kasper.lund7276f142008-07-30 08:49:36 +00003434 ASSERT(args->length() == 2);
kasper.lund7276f142008-07-30 08:49:36 +00003435 __ mov(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003436 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003437}
3438
3439
ager@chromium.org7c537e22008-10-16 08:43:32 +00003440void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003441 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003442 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003443 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003444 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003445 // We need the CC bits to come out as not_equal in the case where the
3446 // object is a smi. This can't be done with the usual test opcode so
3447 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003448 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003449 __ and_(r1, r0, Operand(kSmiTagMask));
3450 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003451 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003452 // It is a heap object - get the map. Check if the object is a JS array.
3453 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003454 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003455 cc_reg_ = eq;
3456}
3457
3458
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003459void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
3460 VirtualFrame::SpilledScope spilled_scope;
3461 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003462
3463 // Get the frame pointer for the calling frame.
3464 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3465
3466 // Skip the arguments adaptor frame if it exists.
3467 Label check_frame_marker;
3468 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
3469 __ cmp(r1, Operand(ArgumentsAdaptorFrame::SENTINEL));
3470 __ b(ne, &check_frame_marker);
3471 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
3472
3473 // Check the marker in the calling frame.
3474 __ bind(&check_frame_marker);
3475 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
3476 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
3477 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003478}
3479
3480
ager@chromium.org7c537e22008-10-16 08:43:32 +00003481void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003482 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003483 ASSERT(args->length() == 0);
3484
mads.s.ager31e71382008-08-13 09:32:07 +00003485 // Seed the result with the formal parameters count, which will be used
3486 // in case no arguments adaptor frame is found below the current frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003487 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
3488
3489 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003490 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003491 frame_->CallStub(&stub, 0);
3492 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003493}
3494
3495
ager@chromium.org7c537e22008-10-16 08:43:32 +00003496void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003497 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003498 ASSERT(args->length() == 1);
3499
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003500 // Satisfy contract with ArgumentsAccessStub:
3501 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003502 LoadAndSpill(args->at(0));
3503 frame_->EmitPop(r1);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003504 __ mov(r0, Operand(Smi::FromInt(scope_->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003505
3506 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003507 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003508 frame_->CallStub(&stub, 0);
3509 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003510}
3511
3512
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003513void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) {
3514 VirtualFrame::SpilledScope spilled_scope;
3515 ASSERT(args->length() == 0);
3516 __ Call(ExternalReference::random_positive_smi_function().address(),
3517 RelocInfo::RUNTIME_ENTRY);
3518 frame_->EmitPush(r0);
3519}
3520
3521
3522void CodeGenerator::GenerateFastMathOp(MathOp op, ZoneList<Expression*>* args) {
3523 VirtualFrame::SpilledScope spilled_scope;
3524 LoadAndSpill(args->at(0));
3525 switch (op) {
3526 case SIN:
3527 frame_->CallRuntime(Runtime::kMath_sin, 1);
3528 break;
3529 case COS:
3530 frame_->CallRuntime(Runtime::kMath_cos, 1);
3531 break;
3532 }
3533 frame_->EmitPush(r0);
3534}
3535
3536
ager@chromium.org7c537e22008-10-16 08:43:32 +00003537void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003538 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003539 ASSERT(args->length() == 2);
3540
3541 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003542 LoadAndSpill(args->at(0));
3543 LoadAndSpill(args->at(1));
3544 frame_->EmitPop(r0);
3545 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003546 __ cmp(r0, Operand(r1));
3547 cc_reg_ = eq;
3548}
3549
3550
ager@chromium.org7c537e22008-10-16 08:43:32 +00003551void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003552#ifdef DEBUG
3553 int original_height = frame_->height();
3554#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003555 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003556 if (CheckForInlineRuntimeCall(node)) {
3557 ASSERT((has_cc() && frame_->height() == original_height) ||
3558 (!has_cc() && frame_->height() == original_height + 1));
3559 return;
3560 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003561
3562 ZoneList<Expression*>* args = node->arguments();
3563 Comment cmnt(masm_, "[ CallRuntime");
3564 Runtime::Function* function = node->function();
3565
ager@chromium.org41826e72009-03-30 13:30:57 +00003566 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003567 // Prepare stack for calling JS runtime function.
3568 __ mov(r0, Operand(node->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003569 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003570 // Push the builtins object found in the current global object.
3571 __ ldr(r1, GlobalObject());
3572 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003573 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003574 }
mads.s.ager31e71382008-08-13 09:32:07 +00003575
ager@chromium.org41826e72009-03-30 13:30:57 +00003576 // Push the arguments ("left-to-right").
3577 int arg_count = args->length();
3578 for (int i = 0; i < arg_count; i++) {
3579 LoadAndSpill(args->at(i));
3580 }
mads.s.ager31e71382008-08-13 09:32:07 +00003581
ager@chromium.org41826e72009-03-30 13:30:57 +00003582 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003583 // Call the JS runtime function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003584 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3585 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003586 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003587 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003588 frame_->Drop();
3589 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003590 } else {
3591 // Call the C runtime function.
3592 frame_->CallRuntime(function, arg_count);
3593 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003594 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003595 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003596}
3597
3598
ager@chromium.org7c537e22008-10-16 08:43:32 +00003599void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003600#ifdef DEBUG
3601 int original_height = frame_->height();
3602#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003603 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003604 Comment cmnt(masm_, "[ UnaryOperation");
3605
3606 Token::Value op = node->op();
3607
3608 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003609 LoadConditionAndSpill(node->expression(),
3610 NOT_INSIDE_TYPEOF,
3611 false_target(),
3612 true_target(),
3613 true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003614 // LoadCondition may (and usually does) leave a test and branch to
3615 // be emitted by the caller. In that case, negate the condition.
3616 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003617
3618 } else if (op == Token::DELETE) {
3619 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003620 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003621 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003622 LoadAndSpill(property->obj());
3623 LoadAndSpill(property->key());
3624 Result arg_count = allocator_->Allocate(r0);
3625 ASSERT(arg_count.is_valid());
3626 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3627 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003628
mads.s.ager31e71382008-08-13 09:32:07 +00003629 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003630 Slot* slot = variable->slot();
3631 if (variable->is_global()) {
3632 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003633 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003634 frame_->EmitPush(r0);
3635 Result arg_count = allocator_->Allocate(r0);
3636 ASSERT(arg_count.is_valid());
3637 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3638 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003639
3640 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3641 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003642 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003643 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003644 frame_->EmitPush(r0);
3645 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003646 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003647 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003648 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003649 frame_->EmitPush(r0);
3650 Result arg_count = allocator_->Allocate(r0);
3651 ASSERT(arg_count.is_valid());
3652 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
3653 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, &arg_count, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003654
mads.s.ager31e71382008-08-13 09:32:07 +00003655 } else {
3656 // Default: Result of deleting non-global, not dynamically
3657 // introduced variables is false.
3658 __ mov(r0, Operand(Factory::false_value()));
3659 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003660
3661 } else {
3662 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003663 LoadAndSpill(node->expression()); // may have side-effects
3664 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003665 __ mov(r0, Operand(Factory::true_value()));
3666 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003667 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003668
3669 } else if (op == Token::TYPEOF) {
3670 // Special case for loading the typeof expression; see comment on
3671 // LoadTypeofExpression().
3672 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003673 frame_->CallRuntime(Runtime::kTypeof, 1);
3674 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003675
3676 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003677 LoadAndSpill(node->expression());
3678 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003679 switch (op) {
3680 case Token::NOT:
3681 case Token::DELETE:
3682 case Token::TYPEOF:
3683 UNREACHABLE(); // handled above
3684 break;
3685
3686 case Token::SUB: {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003687 bool overwrite =
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003688 (node->expression()->AsBinaryOperation() != NULL &&
3689 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003690 UnarySubStub stub(overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003691 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003692 break;
3693 }
3694
3695 case Token::BIT_NOT: {
3696 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003697 JumpTarget smi_label;
3698 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003699 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003700 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003701
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003702 frame_->EmitPush(r0);
3703 Result arg_count = allocator_->Allocate(r0);
3704 ASSERT(arg_count.is_valid());
3705 __ mov(arg_count.reg(), Operand(0)); // not counting receiver
3706 frame_->InvokeBuiltin(Builtins::BIT_NOT, CALL_JS, &arg_count, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003707
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003708 continue_label.Jump();
3709 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003710 __ mvn(r0, Operand(r0));
3711 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003712 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003713 break;
3714 }
3715
3716 case Token::VOID:
3717 // since the stack top is cached in r0, popping and then
3718 // pushing a value can be done by just writing to r0.
3719 __ mov(r0, Operand(Factory::undefined_value()));
3720 break;
3721
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003722 case Token::ADD: {
3723 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003724 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003725 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003726 continue_label.Branch(eq);
3727 frame_->EmitPush(r0);
3728 Result arg_count = allocator_->Allocate(r0);
3729 ASSERT(arg_count.is_valid());
3730 __ mov(arg_count.reg(), Operand(0)); // not counting receiver
3731 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3732 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003733 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003734 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003735 default:
3736 UNREACHABLE();
3737 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003738 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003739 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003740 ASSERT(!has_valid_frame() ||
3741 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003742 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003743}
3744
3745
ager@chromium.org7c537e22008-10-16 08:43:32 +00003746void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003747#ifdef DEBUG
3748 int original_height = frame_->height();
3749#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003750 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003751 Comment cmnt(masm_, "[ CountOperation");
3752
3753 bool is_postfix = node->is_postfix();
3754 bool is_increment = node->op() == Token::INC;
3755
3756 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3757 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3758
3759 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003760 if (is_postfix) {
3761 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003762 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003763 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003764
3765 { Reference target(this, node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003766 if (target.is_illegal()) {
3767 // Spoof the virtual frame to have the expected height (one higher
3768 // than on entry).
3769 if (!is_postfix) {
3770 __ mov(r0, Operand(Smi::FromInt(0)));
3771 frame_->EmitPush(r0);
3772 }
3773 ASSERT(frame_->height() == original_height + 1);
3774 return;
3775 }
3776 target.GetValueAndSpill(NOT_INSIDE_TYPEOF);
3777 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003778
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003779 JumpTarget slow;
3780 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003781
3782 // Load the value (1) into register r1.
3783 __ mov(r1, Operand(Smi::FromInt(1)));
3784
3785 // Check for smi operand.
3786 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003787 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003788
3789 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003790 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003791 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003792 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003793
3794 // Perform optimistic increment/decrement.
3795 if (is_increment) {
3796 __ add(r0, r0, Operand(r1), SetCC);
3797 } else {
3798 __ sub(r0, r0, Operand(r1), SetCC);
3799 }
3800
3801 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003802 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003803
3804 // Revert optimistic increment/decrement.
3805 if (is_increment) {
3806 __ sub(r0, r0, Operand(r1));
3807 } else {
3808 __ add(r0, r0, Operand(r1));
3809 }
3810
3811 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003812 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003813 {
3814 // Convert the operand to a number.
3815 frame_->EmitPush(r0);
3816 Result arg_count = allocator_->Allocate(r0);
3817 ASSERT(arg_count.is_valid());
3818 __ mov(arg_count.reg(), Operand(0));
3819 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, &arg_count, 1);
3820 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003821 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003822 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003823 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003824 }
3825
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003826 // Compute the new value.
3827 __ mov(r1, Operand(Smi::FromInt(1)));
3828 frame_->EmitPush(r0);
3829 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003830 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003831 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003832 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003833 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003834 }
3835
3836 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003837 exit.Bind();
3838 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003839 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003840 }
3841
3842 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003843 if (is_postfix) frame_->EmitPop(r0);
3844 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003845}
3846
3847
ager@chromium.org7c537e22008-10-16 08:43:32 +00003848void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003849#ifdef DEBUG
3850 int original_height = frame_->height();
3851#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003852 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003853 Comment cmnt(masm_, "[ BinaryOperation");
3854 Token::Value op = node->op();
3855
3856 // According to ECMA-262 section 11.11, page 58, the binary logical
3857 // operators must yield the result of one of the two expressions
3858 // before any ToBoolean() conversions. This means that the value
3859 // produced by a && or || operator is not necessarily a boolean.
3860
3861 // NOTE: If the left hand side produces a materialized value (not in
3862 // the CC register), we force the right hand side to do the
3863 // same. This is necessary because we may have to branch to the exit
3864 // after evaluating the left hand side (due to the shortcut
3865 // semantics), but the compiler must (statically) know if the result
3866 // of compiling the binary operation is materialized or not.
3867
3868 if (op == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003869 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003870 LoadConditionAndSpill(node->left(),
3871 NOT_INSIDE_TYPEOF,
3872 &is_true,
3873 false_target(),
3874 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003875 if (has_valid_frame() && !has_cc()) {
3876 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003877 JumpTarget pop_and_continue;
3878 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003879
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003880 __ ldr(r0, frame_->Top()); // Duplicate the stack top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003881 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003882 // Avoid popping the result if it converts to 'false' using the
3883 // standard ToBoolean() conversion as described in ECMA-262,
3884 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003885 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003886 Branch(false, &exit);
3887
3888 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003889 pop_and_continue.Bind();
3890 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003891
3892 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003893 is_true.Bind();
3894 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003895
3896 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003897 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003898 } else if (has_cc() || is_true.is_linked()) {
3899 // The left-hand side is either (a) partially compiled to
3900 // control flow with a final branch left to emit or (b) fully
3901 // compiled to control flow and possibly true.
3902 if (has_cc()) {
3903 Branch(false, false_target());
3904 }
3905 is_true.Bind();
3906 LoadConditionAndSpill(node->right(),
3907 NOT_INSIDE_TYPEOF,
3908 true_target(),
3909 false_target(),
3910 false);
3911 } else {
3912 // Nothing to do.
3913 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003914 }
3915
3916 } else if (op == Token::OR) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003917 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003918 LoadConditionAndSpill(node->left(),
3919 NOT_INSIDE_TYPEOF,
3920 true_target(),
3921 &is_false,
3922 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003923 if (has_valid_frame() && !has_cc()) {
3924 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003925 JumpTarget pop_and_continue;
3926 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003927
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003928 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003929 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003930 // Avoid popping the result if it converts to 'true' using the
3931 // standard ToBoolean() conversion as described in ECMA-262,
3932 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003933 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003934 Branch(true, &exit);
3935
3936 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003937 pop_and_continue.Bind();
3938 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003939
3940 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003941 is_false.Bind();
3942 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003943
3944 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003945 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003946 } else if (has_cc() || is_false.is_linked()) {
3947 // The left-hand side is either (a) partially compiled to
3948 // control flow with a final branch left to emit or (b) fully
3949 // compiled to control flow and possibly false.
3950 if (has_cc()) {
3951 Branch(true, true_target());
3952 }
3953 is_false.Bind();
3954 LoadConditionAndSpill(node->right(),
3955 NOT_INSIDE_TYPEOF,
3956 true_target(),
3957 false_target(),
3958 false);
3959 } else {
3960 // Nothing to do.
3961 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003962 }
3963
3964 } else {
3965 // Optimize for the case where (at least) one of the expressions
3966 // is a literal small integer.
3967 Literal* lliteral = node->left()->AsLiteral();
3968 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003969 // NOTE: The code below assumes that the slow cases (calls to runtime)
3970 // never return a constant/immutable object.
3971 bool overwrite_left =
3972 (node->left()->AsBinaryOperation() != NULL &&
3973 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
3974 bool overwrite_right =
3975 (node->right()->AsBinaryOperation() != NULL &&
3976 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003977
3978 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003979 LoadAndSpill(node->left());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003980 SmiOperation(node->op(),
3981 rliteral->handle(),
3982 false,
3983 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003984
3985 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003986 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003987 SmiOperation(node->op(),
3988 lliteral->handle(),
3989 true,
3990 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003991
3992 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003993 OverwriteMode overwrite_mode = NO_OVERWRITE;
3994 if (overwrite_left) {
3995 overwrite_mode = OVERWRITE_LEFT;
3996 } else if (overwrite_right) {
3997 overwrite_mode = OVERWRITE_RIGHT;
3998 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003999 LoadAndSpill(node->left());
4000 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004001 GenericBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004002 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004003 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004004 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004005 ASSERT(!has_valid_frame() ||
4006 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004007 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004008}
4009
4010
ager@chromium.org7c537e22008-10-16 08:43:32 +00004011void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004012#ifdef DEBUG
4013 int original_height = frame_->height();
4014#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004015 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004016 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004017 frame_->EmitPush(r0);
4018 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004019}
4020
4021
ager@chromium.org7c537e22008-10-16 08:43:32 +00004022void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004023#ifdef DEBUG
4024 int original_height = frame_->height();
4025#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004026 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004027 Comment cmnt(masm_, "[ CompareOperation");
4028
4029 // Get the expressions from the node.
4030 Expression* left = node->left();
4031 Expression* right = node->right();
4032 Token::Value op = node->op();
4033
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004034 // To make null checks efficient, we check if either left or right is the
4035 // literal 'null'. If so, we optimize the code by inlining a null check
4036 // instead of calling the (very) general runtime routine for checking
4037 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004038 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004039 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004040 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004041 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004042 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
4043 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004044 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004045 LoadAndSpill(left_is_null ? right : left);
4046 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004047 __ cmp(r0, Operand(Factory::null_value()));
4048
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004049 // The 'null' value is only equal to 'undefined' if using non-strict
4050 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004051 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004052 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004053
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004054 __ cmp(r0, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004055 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004056
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004057 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004058 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004059
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004060 // It can be an undetectable object.
4061 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
4062 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
4063 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
4064 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004065 }
4066
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004067 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004068 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004069 return;
4070 }
4071 }
4072
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004073 // To make typeof testing for natives implemented in JavaScript really
4074 // efficient, we generate special code for expressions of the form:
4075 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004076 UnaryOperation* operation = left->AsUnaryOperation();
4077 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
4078 (operation != NULL && operation->op() == Token::TYPEOF) &&
4079 (right->AsLiteral() != NULL &&
4080 right->AsLiteral()->handle()->IsString())) {
4081 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
4082
mads.s.ager31e71382008-08-13 09:32:07 +00004083 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004084 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004085 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004086
4087 if (check->Equals(Heap::number_symbol())) {
4088 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004089 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004090 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4091 __ cmp(r1, Operand(Factory::heap_number_map()));
4092 cc_reg_ = eq;
4093
4094 } else if (check->Equals(Heap::string_symbol())) {
4095 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004096 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004097
4098 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4099
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004100 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004101 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4102 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4103 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004104 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004105
4106 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
4107 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
4108 cc_reg_ = lt;
4109
4110 } else if (check->Equals(Heap::boolean_symbol())) {
4111 __ cmp(r1, Operand(Factory::true_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004112 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004113 __ cmp(r1, Operand(Factory::false_value()));
4114 cc_reg_ = eq;
4115
4116 } else if (check->Equals(Heap::undefined_symbol())) {
4117 __ cmp(r1, Operand(Factory::undefined_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004118 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004119
4120 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004121 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004122
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004123 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004124 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4125 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4126 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4127 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4128
4129 cc_reg_ = eq;
4130
4131 } else if (check->Equals(Heap::function_symbol())) {
4132 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004133 false_target()->Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004134 __ CompareObjectType(r1, r1, r1, JS_FUNCTION_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004135 cc_reg_ = eq;
4136
4137 } else if (check->Equals(Heap::object_symbol())) {
4138 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004139 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004140
4141 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
4142 __ cmp(r1, Operand(Factory::null_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004143 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004144
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004145 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004146 __ ldrb(r1, FieldMemOperand(r2, Map::kBitFieldOffset));
4147 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4148 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004149 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004150
4151 __ ldrb(r2, FieldMemOperand(r2, Map::kInstanceTypeOffset));
4152 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004153 false_target()->Branch(lt);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004154 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
4155 cc_reg_ = le;
4156
4157 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004158 // Uncommon case: typeof testing against a string literal that is
4159 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004160 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004161 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004162 ASSERT(!has_valid_frame() ||
4163 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004164 return;
4165 }
4166
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004167 switch (op) {
4168 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004169 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004170 break;
4171
4172 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004173 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004174 break;
4175
4176 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004177 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004178 break;
4179
4180 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004181 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004182 break;
4183
4184 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004185 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004186 break;
4187
4188 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004189 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004190 break;
4191
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004192 case Token::IN: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004193 LoadAndSpill(left);
4194 LoadAndSpill(right);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004195 Result arg_count = allocator_->Allocate(r0);
4196 ASSERT(arg_count.is_valid());
4197 __ mov(arg_count.reg(), Operand(1)); // not counting receiver
4198 Result result = frame_->InvokeBuiltin(Builtins::IN,
4199 CALL_JS,
4200 &arg_count,
4201 2);
4202 frame_->EmitPush(result.reg());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004203 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004204 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004205
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004206 case Token::INSTANCEOF: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004207 LoadAndSpill(left);
4208 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004209 InstanceofStub stub;
4210 Result result = frame_->CallStub(&stub, 2);
4211 // At this point if instanceof succeeded then r0 == 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004212 __ tst(result.reg(), Operand(result.reg()));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004213 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004214 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004215 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004216
4217 default:
4218 UNREACHABLE();
4219 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004220 ASSERT((has_cc() && frame_->height() == original_height) ||
4221 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004222}
4223
4224
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004225#ifdef DEBUG
4226bool CodeGenerator::HasValidEntryRegisters() { return true; }
4227#endif
4228
4229
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004230#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004231#define __ ACCESS_MASM(masm)
4232
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004233
ager@chromium.org7c537e22008-10-16 08:43:32 +00004234Handle<String> Reference::GetName() {
4235 ASSERT(type_ == NAMED);
4236 Property* property = expression_->AsProperty();
4237 if (property == NULL) {
4238 // Global variable reference treated as a named property reference.
4239 VariableProxy* proxy = expression_->AsVariableProxy();
4240 ASSERT(proxy->AsVariable() != NULL);
4241 ASSERT(proxy->AsVariable()->is_global());
4242 return proxy->name();
4243 } else {
4244 Literal* raw_name = property->key()->AsLiteral();
4245 ASSERT(raw_name != NULL);
4246 return Handle<String>(String::cast(*raw_name->handle()));
4247 }
4248}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004249
ager@chromium.org7c537e22008-10-16 08:43:32 +00004250
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00004251void Reference::GetValueAndSpill(TypeofState typeof_state) {
4252 ASSERT(cgen_->in_spilled_code());
4253 cgen_->set_in_spilled_code(false);
4254 GetValue(typeof_state);
4255 cgen_->frame()->SpillAll();
4256 cgen_->set_in_spilled_code(true);
4257}
4258
4259
ager@chromium.org7c537e22008-10-16 08:43:32 +00004260void Reference::GetValue(TypeofState typeof_state) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004261 ASSERT(!cgen_->in_spilled_code());
4262 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004263 ASSERT(!is_illegal());
4264 ASSERT(!cgen_->has_cc());
4265 MacroAssembler* masm = cgen_->masm();
4266 Property* property = expression_->AsProperty();
4267 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004268 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004269 }
4270
4271 switch (type_) {
4272 case SLOT: {
4273 Comment cmnt(masm, "[ Load from Slot");
4274 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4275 ASSERT(slot != NULL);
4276 cgen_->LoadFromSlot(slot, typeof_state);
4277 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004278 }
4279
ager@chromium.org7c537e22008-10-16 08:43:32 +00004280 case NAMED: {
4281 // TODO(1241834): Make sure that this it is safe to ignore the
4282 // distinction between expressions in a typeof and not in a typeof. If
4283 // there is a chance that reference errors can be thrown below, we
4284 // must distinguish between the two kinds of loads (typeof expression
4285 // loads must not throw a reference error).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004286 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004287 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004288 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004289 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004290 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4291 // Setup the name register.
4292 Result name_reg = cgen_->allocator()->Allocate(r2);
4293 ASSERT(name_reg.is_valid());
4294 __ mov(name_reg.reg(), Operand(name));
4295 ASSERT(var == NULL || var->is_global());
4296 RelocInfo::Mode rmode = (var == NULL)
4297 ? RelocInfo::CODE_TARGET
4298 : RelocInfo::CODE_TARGET_CONTEXT;
4299 Result answer = frame->CallCodeObject(ic, rmode, &name_reg, 0);
4300 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004301 break;
4302 }
4303
4304 case KEYED: {
4305 // TODO(1241834): Make sure that this it is safe to ignore the
4306 // distinction between expressions in a typeof and not in a typeof.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004307
4308 // TODO(181): Implement inlined version of array indexing once
4309 // loop nesting is properly tracked on ARM.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004310 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004311 Comment cmnt(masm, "[ Load from keyed Property");
4312 ASSERT(property != NULL);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004313 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004314 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004315 ASSERT(var == NULL || var->is_global());
4316 RelocInfo::Mode rmode = (var == NULL)
4317 ? RelocInfo::CODE_TARGET
4318 : RelocInfo::CODE_TARGET_CONTEXT;
4319 Result answer = frame->CallCodeObject(ic, rmode, 0);
4320 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004321 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004322 }
4323
4324 default:
4325 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004326 }
4327}
4328
4329
ager@chromium.org7c537e22008-10-16 08:43:32 +00004330void Reference::SetValue(InitState init_state) {
4331 ASSERT(!is_illegal());
4332 ASSERT(!cgen_->has_cc());
4333 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004334 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004335 Property* property = expression_->AsProperty();
4336 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004337 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004338 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004339
ager@chromium.org7c537e22008-10-16 08:43:32 +00004340 switch (type_) {
4341 case SLOT: {
4342 Comment cmnt(masm, "[ Store to Slot");
4343 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4344 ASSERT(slot != NULL);
4345 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004346 ASSERT(slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004347
ager@chromium.org7c537e22008-10-16 08:43:32 +00004348 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004349 frame->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004350 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004351 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004352
ager@chromium.org7c537e22008-10-16 08:43:32 +00004353 if (init_state == CONST_INIT) {
4354 // Same as the case for a normal store, but ignores attribute
4355 // (e.g. READ_ONLY) of context slot so that we can initialize
4356 // const properties (introduced via eval("const foo = (some
4357 // expr);")). Also, uses the current function context instead of
4358 // the top context.
4359 //
4360 // Note that we must declare the foo upon entry of eval(), via a
4361 // context slot declaration, but we cannot initialize it at the
4362 // same time, because the const declaration may be at the end of
4363 // the eval code (sigh...) and the const variable may have been
4364 // used before (where its value is 'undefined'). Thus, we can only
4365 // do the initialization when we actually encounter the expression
4366 // and when the expression operands are defined and valid, and
4367 // thus we need the split into 2 operations: declaration of the
4368 // context slot followed by initialization.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004369 frame->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004370 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004371 frame->CallRuntime(Runtime::kStoreContextSlot, 3);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004372 }
4373 // Storing a variable must keep the (new) value on the expression
4374 // stack. This is necessary for compiling assignment expressions.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004375 frame->EmitPush(r0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004376
ager@chromium.org7c537e22008-10-16 08:43:32 +00004377 } else {
ager@chromium.org381abbb2009-02-25 13:23:22 +00004378 ASSERT(!slot->var()->is_dynamic());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004379
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004380 JumpTarget exit;
ager@chromium.org7c537e22008-10-16 08:43:32 +00004381 if (init_state == CONST_INIT) {
4382 ASSERT(slot->var()->mode() == Variable::CONST);
4383 // Only the first const initialization must be executed (the slot
4384 // still contains 'the hole' value). When the assignment is
4385 // executed, the code is identical to a normal store (see below).
4386 Comment cmnt(masm, "[ Init const");
4387 __ ldr(r2, cgen_->SlotOperand(slot, r2));
4388 __ cmp(r2, Operand(Factory::the_hole_value()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004389 exit.Branch(ne);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004390 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004391
ager@chromium.org7c537e22008-10-16 08:43:32 +00004392 // We must execute the store. Storing a variable must keep the
4393 // (new) value on the stack. This is necessary for compiling
4394 // assignment expressions.
4395 //
4396 // Note: We will reach here even with slot->var()->mode() ==
4397 // Variable::CONST because of const declarations which will
4398 // initialize consts to 'the hole' value and by doing so, end up
4399 // calling this code. r2 may be loaded with context; used below in
4400 // RecordWrite.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004401 frame->EmitPop(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004402 __ str(r0, cgen_->SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004403 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004404 if (slot->type() == Slot::CONTEXT) {
4405 // Skip write barrier if the written value is a smi.
4406 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004407 exit.Branch(eq);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004408 // r2 is loaded with context when calling SlotOperand above.
4409 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
4410 __ mov(r3, Operand(offset));
4411 __ RecordWrite(r2, r3, r1);
4412 }
4413 // If we definitely did not jump over the assignment, we do not need
4414 // to bind the exit label. Doing so can defeat peephole
4415 // optimization.
4416 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004417 exit.Bind();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004418 }
4419 }
4420 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004421 }
4422
ager@chromium.org7c537e22008-10-16 08:43:32 +00004423 case NAMED: {
4424 Comment cmnt(masm, "[ Store to named Property");
4425 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004426 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004427 Handle<String> name(GetName());
4428
4429 Result value = cgen_->allocator()->Allocate(r0);
4430 ASSERT(value.is_valid());
4431 frame->EmitPop(value.reg());
4432
4433 // Setup the name register.
4434 Result property_name = cgen_->allocator()->Allocate(r2);
4435 ASSERT(property_name.is_valid());
4436 __ mov(property_name.reg(), Operand(name));
4437 Result answer = frame->CallCodeObject(ic,
4438 RelocInfo::CODE_TARGET,
4439 &value,
4440 &property_name,
4441 0);
4442 frame->EmitPush(answer.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004443 break;
4444 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004445
ager@chromium.org7c537e22008-10-16 08:43:32 +00004446 case KEYED: {
4447 Comment cmnt(masm, "[ Store to keyed Property");
4448 Property* property = expression_->AsProperty();
4449 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004450 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004451
4452 // Call IC code.
4453 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
4454 // TODO(1222589): Make the IC grab the values from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004455 Result value = cgen_->allocator()->Allocate(r0);
4456 ASSERT(value.is_valid());
4457 frame->EmitPop(value.reg()); // value
4458 Result result =
4459 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, &value, 0);
4460 frame->EmitPush(result.reg());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004461 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004462 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004463
4464 default:
4465 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004466 }
4467}
4468
4469
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004470// Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz
4471// instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0
4472// (31 instead of 32).
4473static void CountLeadingZeros(
4474 MacroAssembler* masm,
4475 Register source,
4476 Register scratch,
4477 Register zeros) {
4478#ifdef __ARM_ARCH_5__
4479 __ clz(zeros, source); // This instruction is only supported after ARM5.
4480#else
4481 __ mov(zeros, Operand(0));
4482 __ mov(scratch, source);
4483 // Top 16.
4484 __ tst(scratch, Operand(0xffff0000));
4485 __ add(zeros, zeros, Operand(16), LeaveCC, eq);
4486 __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
4487 // Top 8.
4488 __ tst(scratch, Operand(0xff000000));
4489 __ add(zeros, zeros, Operand(8), LeaveCC, eq);
4490 __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
4491 // Top 4.
4492 __ tst(scratch, Operand(0xf0000000));
4493 __ add(zeros, zeros, Operand(4), LeaveCC, eq);
4494 __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
4495 // Top 2.
4496 __ tst(scratch, Operand(0xc0000000));
4497 __ add(zeros, zeros, Operand(2), LeaveCC, eq);
4498 __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
4499 // Top bit.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004500 __ tst(scratch, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004501 __ add(zeros, zeros, Operand(1), LeaveCC, eq);
4502#endif
4503}
4504
4505
4506// Takes a Smi and converts to an IEEE 64 bit floating point value in two
4507// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
4508// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
4509// scratch register. Destroys the source register. No GC occurs during this
4510// stub so you don't have to set up the frame.
4511class ConvertToDoubleStub : public CodeStub {
4512 public:
4513 ConvertToDoubleStub(Register result_reg_1,
4514 Register result_reg_2,
4515 Register source_reg,
4516 Register scratch_reg)
4517 : result1_(result_reg_1),
4518 result2_(result_reg_2),
4519 source_(source_reg),
4520 zeros_(scratch_reg) { }
4521
4522 private:
4523 Register result1_;
4524 Register result2_;
4525 Register source_;
4526 Register zeros_;
4527
4528 // Minor key encoding in 16 bits.
4529 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4530 class OpBits: public BitField<Token::Value, 2, 14> {};
4531
4532 Major MajorKey() { return ConvertToDouble; }
4533 int MinorKey() {
4534 // Encode the parameters in a unique 16 bit value.
4535 return result1_.code() +
4536 (result2_.code() << 4) +
4537 (source_.code() << 8) +
4538 (zeros_.code() << 12);
4539 }
4540
4541 void Generate(MacroAssembler* masm);
4542
4543 const char* GetName() { return "ConvertToDoubleStub"; }
4544
4545#ifdef DEBUG
4546 void Print() { PrintF("ConvertToDoubleStub\n"); }
4547#endif
4548};
4549
4550
4551void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
4552#ifndef BIG_ENDIAN_FLOATING_POINT
4553 Register exponent = result1_;
4554 Register mantissa = result2_;
4555#else
4556 Register exponent = result2_;
4557 Register mantissa = result1_;
4558#endif
4559 Label not_special;
4560 // Convert from Smi to integer.
4561 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
4562 // Move sign bit from source to destination. This works because the sign bit
4563 // in the exponent word of the double has the same position and polarity as
4564 // the 2's complement sign bit in a Smi.
4565 ASSERT(HeapNumber::kSignMask == 0x80000000u);
4566 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
4567 // Subtract from 0 if source was negative.
4568 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
4569 __ cmp(source_, Operand(1));
4570 __ b(gt, &not_special);
4571
4572 // We have -1, 0 or 1, which we treat specially.
4573 __ cmp(source_, Operand(0));
4574 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
4575 static const uint32_t exponent_word_for_1 =
4576 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
4577 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne);
4578 // 1, 0 and -1 all have 0 for the second word.
4579 __ mov(mantissa, Operand(0));
4580 __ Ret();
4581
4582 __ bind(&not_special);
4583 // Count leading zeros. Uses result2 for a scratch register on pre-ARM5.
4584 // Gets the wrong answer for 0, but we already checked for that case above.
4585 CountLeadingZeros(masm, source_, mantissa, zeros_);
4586 // Compute exponent and or it into the exponent register.
4587 // We use result2 as a scratch register here.
4588 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
4589 __ orr(exponent,
4590 exponent,
4591 Operand(mantissa, LSL, HeapNumber::kExponentShift));
4592 // Shift up the source chopping the top bit off.
4593 __ add(zeros_, zeros_, Operand(1));
4594 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
4595 __ mov(source_, Operand(source_, LSL, zeros_));
4596 // Compute lower part of fraction (last 12 bits).
4597 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
4598 // And the top (top 20 bits).
4599 __ orr(exponent,
4600 exponent,
4601 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
4602 __ Ret();
4603}
4604
4605
4606// This stub can convert a signed int32 to a heap number (double). It does
4607// not work for int32s that are in Smi range! No GC occurs during this stub
4608// so you don't have to set up the frame.
4609class WriteInt32ToHeapNumberStub : public CodeStub {
4610 public:
4611 WriteInt32ToHeapNumberStub(Register the_int,
4612 Register the_heap_number,
4613 Register scratch)
4614 : the_int_(the_int),
4615 the_heap_number_(the_heap_number),
4616 scratch_(scratch) { }
4617
4618 private:
4619 Register the_int_;
4620 Register the_heap_number_;
4621 Register scratch_;
4622
4623 // Minor key encoding in 16 bits.
4624 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4625 class OpBits: public BitField<Token::Value, 2, 14> {};
4626
4627 Major MajorKey() { return WriteInt32ToHeapNumber; }
4628 int MinorKey() {
4629 // Encode the parameters in a unique 16 bit value.
4630 return the_int_.code() +
4631 (the_heap_number_.code() << 4) +
4632 (scratch_.code() << 8);
4633 }
4634
4635 void Generate(MacroAssembler* masm);
4636
4637 const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
4638
4639#ifdef DEBUG
4640 void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
4641#endif
4642};
4643
4644
4645// See comment for class.
4646void WriteInt32ToHeapNumberStub::Generate(MacroAssembler *masm) {
4647 Label max_negative_int;
4648 // the_int_ has the answer which is a signed int32 but not a Smi.
4649 // We test for the special value that has a different exponent. This test
4650 // has the neat side effect of setting the flags according to the sign.
4651 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004652 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004653 __ b(eq, &max_negative_int);
4654 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
4655 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
4656 uint32_t non_smi_exponent =
4657 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
4658 __ mov(scratch_, Operand(non_smi_exponent));
4659 // Set the sign bit in scratch_ if the value was negative.
4660 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
4661 // Subtract from 0 if the value was negative.
4662 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
4663 // We should be masking the implict first digit of the mantissa away here,
4664 // but it just ends up combining harmlessly with the last digit of the
4665 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
4666 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
4667 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
4668 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
4669 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
4670 __ str(scratch_, FieldMemOperand(the_heap_number_,
4671 HeapNumber::kExponentOffset));
4672 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
4673 __ str(scratch_, FieldMemOperand(the_heap_number_,
4674 HeapNumber::kMantissaOffset));
4675 __ Ret();
4676
4677 __ bind(&max_negative_int);
4678 // The max negative int32 is stored as a positive number in the mantissa of
4679 // a double because it uses a sign bit instead of using two's complement.
4680 // The actual mantissa bits stored are all 0 because the implicit most
4681 // significant 1 bit is not stored.
4682 non_smi_exponent += 1 << HeapNumber::kExponentShift;
4683 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
4684 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
4685 __ mov(ip, Operand(0));
4686 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
4687 __ Ret();
4688}
4689
4690
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004691// Handle the case where the lhs and rhs are the same object.
4692// Equality is almost reflexive (everything but NaN), so this is a test
4693// for "identity and not NaN".
4694static void EmitIdenticalObjectComparison(MacroAssembler* masm,
4695 Label* slow,
4696 Condition cc) {
4697 Label not_identical;
4698 __ cmp(r0, Operand(r1));
4699 __ b(ne, &not_identical);
4700
4701 Register exp_mask_reg = r5;
4702 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4703
4704 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
4705 // so we do the second best thing - test it ourselves.
4706 Label heap_number, return_equal;
4707 // They are both equal and they are not both Smis so both of them are not
4708 // Smis. If it's not a heap number, then return equal.
4709 if (cc == lt || cc == gt) {
4710 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
4711 __ b(ge, slow);
4712 } else {
4713 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4714 __ b(eq, &heap_number);
4715 // Comparing JS objects with <=, >= is complicated.
4716 if (cc != eq) {
4717 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
4718 __ b(ge, slow);
4719 }
4720 }
4721 __ bind(&return_equal);
4722 if (cc == lt) {
4723 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
4724 } else if (cc == gt) {
4725 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
4726 } else {
4727 __ mov(r0, Operand(0)); // Things are <=, >=, ==, === themselves.
4728 }
4729 __ mov(pc, Operand(lr)); // Return.
4730
4731 // For less and greater we don't have to check for NaN since the result of
4732 // x < x is false regardless. For the others here is some code to check
4733 // for NaN.
4734 if (cc != lt && cc != gt) {
4735 __ bind(&heap_number);
4736 // It is a heap number, so return non-equal if it's NaN and equal if it's
4737 // not NaN.
4738 // The representation of NaN values has all exponent bits (52..62) set,
4739 // and not all mantissa bits (0..51) clear.
4740 // Read top bits of double representation (second word of value).
4741 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
4742 // Test that exponent bits are all set.
4743 __ and_(r3, r2, Operand(exp_mask_reg));
4744 __ cmp(r3, Operand(exp_mask_reg));
4745 __ b(ne, &return_equal);
4746
4747 // Shift out flag and all exponent bits, retaining only mantissa.
4748 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
4749 // Or with all low-bits of mantissa.
4750 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
4751 __ orr(r0, r3, Operand(r2), SetCC);
4752 // For equal we already have the right value in r0: Return zero (equal)
4753 // if all bits in mantissa are zero (it's an Infinity) and non-zero if not
4754 // (it's a NaN). For <= and >= we need to load r0 with the failing value
4755 // if it's a NaN.
4756 if (cc != eq) {
4757 // All-zero means Infinity means equal.
4758 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
4759 if (cc == le) {
4760 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
4761 } else {
4762 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
4763 }
4764 }
4765 __ mov(pc, Operand(lr)); // Return.
4766 }
4767 // No fall through here.
4768
4769 __ bind(&not_identical);
4770}
4771
4772
4773// See comment at call site.
4774static void EmitSmiNonsmiComparison(MacroAssembler* masm,
4775 Label* rhs_not_nan,
4776 Label* slow,
4777 bool strict) {
4778 Label lhs_is_smi;
4779 __ tst(r0, Operand(kSmiTagMask));
4780 __ b(eq, &lhs_is_smi);
4781
4782 // Rhs is a Smi. Check whether the non-smi is a heap number.
4783 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4784 if (strict) {
4785 // If lhs was not a number and rhs was a Smi then strict equality cannot
4786 // succeed. Return non-equal (r0 is already not zero)
4787 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4788 } else {
4789 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4790 // the runtime.
4791 __ b(ne, slow);
4792 }
4793
4794 // Rhs is a smi, lhs is a number.
4795 __ push(lr);
4796 __ mov(r7, Operand(r1));
4797 ConvertToDoubleStub stub1(r3, r2, r7, r6);
4798 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
4799 // r3 and r2 are rhs as double.
4800 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4801 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4802 // We now have both loaded as doubles but we can skip the lhs nan check
4803 // since it's a Smi.
4804 __ pop(lr);
4805 __ jmp(rhs_not_nan);
4806
4807 __ bind(&lhs_is_smi);
4808 // Lhs is a Smi. Check whether the non-smi is a heap number.
4809 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
4810 if (strict) {
4811 // If lhs was not a number and rhs was a Smi then strict equality cannot
4812 // succeed. Return non-equal.
4813 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
4814 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4815 } else {
4816 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4817 // the runtime.
4818 __ b(ne, slow);
4819 }
4820
4821 // Lhs is a smi, rhs is a number.
4822 // r0 is Smi and r1 is heap number.
4823 __ push(lr);
4824 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
4825 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4826 __ mov(r7, Operand(r0));
4827 ConvertToDoubleStub stub2(r1, r0, r7, r6);
4828 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
4829 __ pop(lr);
4830 // Fall through to both_loaded_as_doubles.
4831}
4832
4833
4834void EmitNanCheck(MacroAssembler* masm, Label* rhs_not_nan, Condition cc) {
4835 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
4836 Register lhs_exponent = exp_first ? r0 : r1;
4837 Register rhs_exponent = exp_first ? r2 : r3;
4838 Register lhs_mantissa = exp_first ? r1 : r0;
4839 Register rhs_mantissa = exp_first ? r3 : r2;
4840 Label one_is_nan, neither_is_nan;
4841
4842 Register exp_mask_reg = r5;
4843
4844 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4845 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
4846 __ cmp(r4, Operand(exp_mask_reg));
4847 __ b(ne, rhs_not_nan);
4848 __ mov(r4,
4849 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4850 SetCC);
4851 __ b(ne, &one_is_nan);
4852 __ cmp(rhs_mantissa, Operand(0));
4853 __ b(ne, &one_is_nan);
4854
4855 __ bind(rhs_not_nan);
4856 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
4857 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
4858 __ cmp(r4, Operand(exp_mask_reg));
4859 __ b(ne, &neither_is_nan);
4860 __ mov(r4,
4861 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
4862 SetCC);
4863 __ b(ne, &one_is_nan);
4864 __ cmp(lhs_mantissa, Operand(0));
4865 __ b(eq, &neither_is_nan);
4866
4867 __ bind(&one_is_nan);
4868 // NaN comparisons always fail.
4869 // Load whatever we need in r0 to make the comparison fail.
4870 if (cc == lt || cc == le) {
4871 __ mov(r0, Operand(GREATER));
4872 } else {
4873 __ mov(r0, Operand(LESS));
4874 }
4875 __ mov(pc, Operand(lr)); // Return.
4876
4877 __ bind(&neither_is_nan);
4878}
4879
4880
4881// See comment at call site.
4882static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
4883 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
4884 Register lhs_exponent = exp_first ? r0 : r1;
4885 Register rhs_exponent = exp_first ? r2 : r3;
4886 Register lhs_mantissa = exp_first ? r1 : r0;
4887 Register rhs_mantissa = exp_first ? r3 : r2;
4888
4889 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
4890 if (cc == eq) {
4891 // Doubles are not equal unless they have the same bit pattern.
4892 // Exception: 0 and -0.
4893 __ cmp(lhs_mantissa, Operand(rhs_mantissa));
4894 __ orr(r0, lhs_mantissa, Operand(rhs_mantissa), LeaveCC, ne);
4895 // Return non-zero if the numbers are unequal.
4896 __ mov(pc, Operand(lr), LeaveCC, ne);
4897
4898 __ sub(r0, lhs_exponent, Operand(rhs_exponent), SetCC);
4899 // If exponents are equal then return 0.
4900 __ mov(pc, Operand(lr), LeaveCC, eq);
4901
4902 // Exponents are unequal. The only way we can return that the numbers
4903 // are equal is if one is -0 and the other is 0. We already dealt
4904 // with the case where both are -0 or both are 0.
4905 // We start by seeing if the mantissas (that are equal) or the bottom
4906 // 31 bits of the rhs exponent are non-zero. If so we return not
4907 // equal.
4908 __ orr(r4, rhs_mantissa, Operand(rhs_exponent, LSL, kSmiTagSize), SetCC);
4909 __ mov(r0, Operand(r4), LeaveCC, ne);
4910 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
4911 // Now they are equal if and only if the lhs exponent is zero in its
4912 // low 31 bits.
4913 __ mov(r0, Operand(lhs_exponent, LSL, kSmiTagSize));
4914 __ mov(pc, Operand(lr));
4915 } else {
4916 // Call a native function to do a comparison between two non-NaNs.
4917 // Call C routine that may not cause GC or other trouble.
4918 __ mov(r5, Operand(ExternalReference::compare_doubles()));
4919 __ Jump(r5); // Tail call.
4920 }
4921}
4922
4923
4924// See comment at call site.
4925static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
4926 // If either operand is a JSObject or an oddball value, then they are
4927 // not equal since their pointers are different.
4928 // There is no test for undetectability in strict equality.
4929 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4930 Label first_non_object;
4931 // Get the type of the first operand into r2 and compare it with
4932 // FIRST_JS_OBJECT_TYPE.
4933 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
4934 __ b(lt, &first_non_object);
4935
4936 // Return non-zero (r0 is not zero)
4937 Label return_not_equal;
4938 __ bind(&return_not_equal);
4939 __ mov(pc, Operand(lr)); // Return.
4940
4941 __ bind(&first_non_object);
4942 // Check for oddballs: true, false, null, undefined.
4943 __ cmp(r2, Operand(ODDBALL_TYPE));
4944 __ b(eq, &return_not_equal);
4945
4946 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
4947 __ b(ge, &return_not_equal);
4948
4949 // Check for oddballs: true, false, null, undefined.
4950 __ cmp(r3, Operand(ODDBALL_TYPE));
4951 __ b(eq, &return_not_equal);
4952}
4953
4954
4955// See comment at call site.
4956static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
4957 Label* both_loaded_as_doubles,
4958 Label* not_heap_numbers,
4959 Label* slow) {
4960 __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE);
4961 __ b(ne, not_heap_numbers);
4962 __ CompareObjectType(r1, r3, r3, HEAP_NUMBER_TYPE);
4963 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
4964
4965 // Both are heap numbers. Load them up then jump to the code we have
4966 // for that.
4967 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
4968 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4969 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4970 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4971 __ jmp(both_loaded_as_doubles);
4972}
4973
4974
4975// Fast negative check for symbol-to-symbol equality.
4976static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
4977 // r2 is object type of r0.
4978 __ tst(r2, Operand(kIsNotStringMask));
4979 __ b(ne, slow);
4980 __ tst(r2, Operand(kIsSymbolMask));
4981 __ b(eq, slow);
4982 __ CompareObjectType(r1, r3, r3, FIRST_NONSTRING_TYPE);
4983 __ b(ge, slow);
4984 __ tst(r3, Operand(kIsSymbolMask));
4985 __ b(eq, slow);
4986
4987 // Both are symbols. We already checked they weren't the same pointer
4988 // so they are not equal.
4989 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
4990 __ mov(pc, Operand(lr)); // Return.
4991}
4992
4993
4994// On entry r0 and r1 are the things to be compared. On exit r0 is 0,
4995// positive or negative to indicate the result of the comparison.
4996void CompareStub::Generate(MacroAssembler* masm) {
4997 Label slow; // Call builtin.
4998 Label not_smis, both_loaded_as_doubles, rhs_not_nan;
4999
5000 // NOTICE! This code is only reached after a smi-fast-case check, so
5001 // it is certain that at least one operand isn't a smi.
5002
5003 // Handle the case where the objects are identical. Either returns the answer
5004 // or goes to slow. Only falls through if the objects were not identical.
5005 EmitIdenticalObjectComparison(masm, &slow, cc_);
5006
5007 // If either is a Smi (we know that not both are), then they can only
5008 // be strictly equal if the other is a HeapNumber.
5009 ASSERT_EQ(0, kSmiTag);
5010 ASSERT_EQ(0, Smi::FromInt(0));
5011 __ and_(r2, r0, Operand(r1));
5012 __ tst(r2, Operand(kSmiTagMask));
5013 __ b(ne, &not_smis);
5014 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
5015 // 1) Return the answer.
5016 // 2) Go to slow.
5017 // 3) Fall through to both_loaded_as_doubles.
5018 // 4) Jump to rhs_not_nan.
5019 // In cases 3 and 4 we have found out we were dealing with a number-number
5020 // comparison and the numbers have been loaded into r0, r1, r2, r3 as doubles.
5021 EmitSmiNonsmiComparison(masm, &rhs_not_nan, &slow, strict_);
5022
5023 __ bind(&both_loaded_as_doubles);
5024 // r0, r1, r2, r3 are the double representations of the left hand side
5025 // and the right hand side.
5026
5027 // Checks for NaN in the doubles we have loaded. Can return the answer or
5028 // fall through if neither is a NaN. Also binds rhs_not_nan.
5029 EmitNanCheck(masm, &rhs_not_nan, cc_);
5030
5031 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
5032 // answer. Never falls through.
5033 EmitTwoNonNanDoubleComparison(masm, cc_);
5034
5035 __ bind(&not_smis);
5036 // At this point we know we are dealing with two different objects,
5037 // and neither of them is a Smi. The objects are in r0 and r1.
5038 if (strict_) {
5039 // This returns non-equal for some object types, or falls through if it
5040 // was not lucky.
5041 EmitStrictTwoHeapObjectCompare(masm);
5042 }
5043
5044 Label check_for_symbols;
5045 // Check for heap-number-heap-number comparison. Can jump to slow case,
5046 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
5047 // that case. If the inputs are not doubles then jumps to check_for_symbols.
5048 // In this case r2 will contain the type of r0.
5049 EmitCheckForTwoHeapNumbers(masm,
5050 &both_loaded_as_doubles,
5051 &check_for_symbols,
5052 &slow);
5053
5054 __ bind(&check_for_symbols);
5055 if (cc_ == eq) {
5056 // Either jumps to slow or returns the answer. Assumes that r2 is the type
5057 // of r0 on entry.
5058 EmitCheckForSymbols(masm, &slow);
5059 }
5060
5061 __ bind(&slow);
5062 __ push(lr);
5063 __ push(r1);
5064 __ push(r0);
5065 // Figure out which native to call and setup the arguments.
5066 Builtins::JavaScript native;
5067 int arg_count = 1; // Not counting receiver.
5068 if (cc_ == eq) {
5069 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
5070 } else {
5071 native = Builtins::COMPARE;
5072 int ncr; // NaN compare result
5073 if (cc_ == lt || cc_ == le) {
5074 ncr = GREATER;
5075 } else {
5076 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
5077 ncr = LESS;
5078 }
5079 arg_count++;
5080 __ mov(r0, Operand(Smi::FromInt(ncr)));
5081 __ push(r0);
5082 }
5083
5084 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
5085 // tagged as a small integer.
5086 __ mov(r0, Operand(arg_count));
5087 __ InvokeBuiltin(native, CALL_JS);
5088 __ cmp(r0, Operand(0));
5089 __ pop(pc);
5090}
5091
5092
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005093// Allocates a heap number or jumps to the label if the young space is full and
5094// a scavenge is needed.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005095static void AllocateHeapNumber(
5096 MacroAssembler* masm,
5097 Label* need_gc, // Jump here if young space is full.
5098 Register result_reg, // The tagged address of the new heap number.
5099 Register allocation_top_addr_reg, // A scratch register.
5100 Register scratch2) { // Another scratch register.
5101 ExternalReference allocation_top =
5102 ExternalReference::new_space_allocation_top_address();
5103 ExternalReference allocation_limit =
5104 ExternalReference::new_space_allocation_limit_address();
5105
5106 // allocat := the address of the allocation top variable.
5107 __ mov(allocation_top_addr_reg, Operand(allocation_top));
5108 // result_reg := the old allocation top.
5109 __ ldr(result_reg, MemOperand(allocation_top_addr_reg));
5110 // scratch2 := the address of the allocation limit.
5111 __ mov(scratch2, Operand(allocation_limit));
5112 // scratch2 := the allocation limit.
5113 __ ldr(scratch2, MemOperand(scratch2));
5114 // result_reg := the new allocation top.
5115 __ add(result_reg, result_reg, Operand(HeapNumber::kSize));
5116 // Compare new new allocation top and limit.
5117 __ cmp(result_reg, Operand(scratch2));
5118 // Branch if out of space in young generation.
5119 __ b(hi, need_gc);
5120 // Store new allocation top.
5121 __ str(result_reg, MemOperand(allocation_top_addr_reg)); // store new top
5122 // Tag and adjust back to start of new object.
5123 __ sub(result_reg, result_reg, Operand(HeapNumber::kSize - kHeapObjectTag));
5124 // Get heap number map into scratch2.
5125 __ mov(scratch2, Operand(Factory::heap_number_map()));
5126 // Store heap number map in new object.
5127 __ str(scratch2, FieldMemOperand(result_reg, HeapObject::kMapOffset));
5128}
5129
5130
5131// We fall into this code if the operands were Smis, but the result was
5132// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005133// the operands were not both Smi. The operands are in r0 and r1. In order
5134// to call the C-implemented binary fp operation routines we need to end up
5135// with the double precision floating point operands in r0 and r1 (for the
5136// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005137static void HandleBinaryOpSlowCases(MacroAssembler* masm,
5138 Label* not_smi,
5139 const Builtins::JavaScript& builtin,
5140 Token::Value operation,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005141 OverwriteMode mode) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005142 Label slow, slow_pop_2_first, do_the_call;
5143 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
5144 // Smi-smi case (overflow).
5145 // Since both are Smis there is no heap number to overwrite, so allocate.
5146 // The new heap number is in r5. r6 and r7 are scratch.
5147 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5148 // Write Smi from r0 to r3 and r2 in double format. r6 is scratch.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005149 __ mov(r7, Operand(r0));
5150 ConvertToDoubleStub stub1(r3, r2, r7, r6);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005151 __ push(lr);
5152 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
5153 // Write Smi from r1 to r1 and r0 in double format. r6 is scratch.
5154 __ mov(r7, Operand(r1));
5155 ConvertToDoubleStub stub2(r1, r0, r7, r6);
5156 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
5157 __ pop(lr);
5158 __ jmp(&do_the_call); // Tail call. No return.
5159
5160 // We jump to here if something goes wrong (one param is not a number of any
5161 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005162 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005163 __ push(r1);
5164 __ push(r0);
5165 __ mov(r0, Operand(1)); // Set number of arguments.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005166 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005167
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005168 // We branch here if at least one of r0 and r1 is not a Smi.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005169 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005170 if (mode == NO_OVERWRITE) {
5171 // In the case where there is no chance of an overwritable float we may as
5172 // well do the allocation immediately while r0 and r1 are untouched.
5173 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5174 }
5175
5176 // Move r0 to a double in r2-r3.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005177 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005178 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5179 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005180 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005181 if (mode == OVERWRITE_RIGHT) {
5182 __ mov(r5, Operand(r0)); // Overwrite this heap number.
5183 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005184 // Calling convention says that second double is in r2 and r3.
5185 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005186 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
5187 __ jmp(&finished_loading_r0);
5188 __ bind(&r0_is_smi);
5189 if (mode == OVERWRITE_RIGHT) {
5190 // We can't overwrite a Smi so get address of new heap number into r5.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005191 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005192 }
5193 // Write Smi from r0 to r3 and r2 in double format.
5194 __ mov(r7, Operand(r0));
5195 ConvertToDoubleStub stub3(r3, r2, r7, r6);
5196 __ push(lr);
5197 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
5198 __ pop(lr);
5199 __ bind(&finished_loading_r0);
5200
5201 // Move r1 to a double in r0-r1.
5202 __ tst(r1, Operand(kSmiTagMask));
5203 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5204 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5205 __ b(ne, &slow);
5206 if (mode == OVERWRITE_LEFT) {
5207 __ mov(r5, Operand(r1)); // Overwrite this heap number.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005208 }
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005209 // Calling convention says that first double is in r0 and r1.
5210 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005211 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
5212 __ jmp(&finished_loading_r1);
5213 __ bind(&r1_is_smi);
5214 if (mode == OVERWRITE_LEFT) {
5215 // We can't overwrite a Smi so get address of new heap number into r5.
5216 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5217 }
5218 // Write Smi from r1 to r1 and r0 in double format.
5219 __ mov(r7, Operand(r1));
5220 ConvertToDoubleStub stub4(r1, r0, r7, r6);
5221 __ push(lr);
5222 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
5223 __ pop(lr);
5224 __ bind(&finished_loading_r1);
5225
5226 __ bind(&do_the_call);
5227 // r0: Left value (least significant part of mantissa).
5228 // r1: Left value (sign, exponent, top of mantissa).
5229 // r2: Right value (least significant part of mantissa).
5230 // r3: Right value (sign, exponent, top of mantissa).
5231 // r5: Address of heap number for result.
5232 __ push(lr); // For later.
5233 __ push(r5); // Address of heap number that is answer.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005234 // Call C routine that may not cause GC or other trouble.
5235 __ mov(r5, Operand(ExternalReference::double_fp_operation(operation)));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005236 __ Call(r5);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005237 // Store answer in the overwritable heap number.
5238 __ pop(r4);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005239#if !defined(USE_ARM_EABI)
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005240 // Double returned in fp coprocessor register 0 and 1, encoded as register
5241 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
5242 // substract the tag from r4.
5243 __ sub(r5, r4, Operand(kHeapObjectTag));
5244 __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset));
5245#else
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005246 // Double returned in registers 0 and 1.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005247 __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005248 __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005249#endif
5250 __ mov(r0, Operand(r4));
5251 // And we are done.
5252 __ pop(pc);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005253}
5254
5255
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005256// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005257// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005258// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
5259// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005260// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
5261// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005262static void GetInt32(MacroAssembler* masm,
5263 Register source,
5264 Register dest,
5265 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005266 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005267 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005268 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005269 // Get exponent word.
5270 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
5271 // Get exponent alone in scratch2.
5272 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005273 // Load dest with zero. We use this either for the final shift or
5274 // for the answer.
5275 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005276 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005277 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
5278 // the exponent that we are fastest at and also the highest exponent we can
5279 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005280 const uint32_t non_smi_exponent =
5281 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
5282 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005283 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
5284 __ b(eq, &right_exponent);
5285 // If the exponent is higher than that then go to slow case. This catches
5286 // numbers that don't fit in a signed int32, infinities and NaNs.
5287 __ b(gt, slow);
5288
5289 // We know the exponent is smaller than 30 (biased). If it is less than
5290 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
5291 // it rounds to zero.
5292 const uint32_t zero_exponent =
5293 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
5294 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
5295 // Dest already has a Smi zero.
5296 __ b(lt, &done);
5297 // We have a shifted exponent between 0 and 30 in scratch2.
5298 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
5299 // We now have the exponent in dest. Subtract from 30 to get
5300 // how much to shift down.
5301 __ rsb(dest, dest, Operand(30));
5302
5303 __ bind(&right_exponent);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005304 // Get the top bits of the mantissa.
5305 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
5306 // Put back the implicit 1.
5307 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
5308 // Shift up the mantissa bits to take up the space the exponent used to take.
5309 // We just orred in the implicit bit so that took care of one and we want to
5310 // leave the sign bit 0 so we subtract 2 bits from the shift distance.
5311 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
5312 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
5313 // Put sign in zero flag.
5314 __ tst(scratch, Operand(HeapNumber::kSignMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005315 // Get the second half of the double. For some exponents we don't actually
5316 // need this because the bits get shifted out again, but it's probably slower
5317 // to test than just to do it.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005318 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
5319 // Shift down 22 bits to get the last 10 bits.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005320 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
5321 // Move down according to the exponent.
5322 __ mov(dest, Operand(scratch, LSR, dest));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005323 // Fix sign if sign bit was set.
5324 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005325 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005326}
5327
5328
5329// For bitwise ops where the inputs are not both Smis we here try to determine
5330// whether both inputs are either Smis or at least heap numbers that can be
5331// represented by a 32 bit signed value. We truncate towards zero as required
5332// by the ES spec. If this is the case we do the bitwise op and see if the
5333// result is a Smi. If so, great, otherwise we try to find a heap number to
5334// write the answer into (either by allocating or by overwriting).
5335// On entry the operands are in r0 and r1. On exit the answer is in r0.
5336void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) {
5337 Label slow, result_not_a_smi;
5338 Label r0_is_smi, r1_is_smi;
5339 Label done_checking_r0, done_checking_r1;
5340
5341 __ tst(r1, Operand(kSmiTagMask));
5342 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5343 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5344 __ b(ne, &slow);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005345 GetInt32(masm, r1, r3, r4, r5, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005346 __ jmp(&done_checking_r1);
5347 __ bind(&r1_is_smi);
5348 __ mov(r3, Operand(r1, ASR, 1));
5349 __ bind(&done_checking_r1);
5350
5351 __ tst(r0, Operand(kSmiTagMask));
5352 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5353 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5354 __ b(ne, &slow);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005355 GetInt32(masm, r0, r2, r4, r5, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005356 __ jmp(&done_checking_r0);
5357 __ bind(&r0_is_smi);
5358 __ mov(r2, Operand(r0, ASR, 1));
5359 __ bind(&done_checking_r0);
5360
5361 // r0 and r1: Original operands (Smi or heap numbers).
5362 // r2 and r3: Signed int32 operands.
5363 switch (op_) {
5364 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
5365 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
5366 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
5367 case Token::SAR:
5368 // Use only the 5 least significant bits of the shift count.
5369 __ and_(r2, r2, Operand(0x1f));
5370 __ mov(r2, Operand(r3, ASR, r2));
5371 break;
5372 case Token::SHR:
5373 // Use only the 5 least significant bits of the shift count.
5374 __ and_(r2, r2, Operand(0x1f));
5375 __ mov(r2, Operand(r3, LSR, r2), SetCC);
5376 // SHR is special because it is required to produce a positive answer.
5377 // The code below for writing into heap numbers isn't capable of writing
5378 // the register as an unsigned int so we go to slow case if we hit this
5379 // case.
5380 __ b(mi, &slow);
5381 break;
5382 case Token::SHL:
5383 // Use only the 5 least significant bits of the shift count.
5384 __ and_(r2, r2, Operand(0x1f));
5385 __ mov(r2, Operand(r3, LSL, r2));
5386 break;
5387 default: UNREACHABLE();
5388 }
5389 // check that the *signed* result fits in a smi
5390 __ add(r3, r2, Operand(0x40000000), SetCC);
5391 __ b(mi, &result_not_a_smi);
5392 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5393 __ Ret();
5394
5395 Label have_to_allocate, got_a_heap_number;
5396 __ bind(&result_not_a_smi);
5397 switch (mode_) {
5398 case OVERWRITE_RIGHT: {
5399 __ tst(r0, Operand(kSmiTagMask));
5400 __ b(eq, &have_to_allocate);
5401 __ mov(r5, Operand(r0));
5402 break;
5403 }
5404 case OVERWRITE_LEFT: {
5405 __ tst(r1, Operand(kSmiTagMask));
5406 __ b(eq, &have_to_allocate);
5407 __ mov(r5, Operand(r1));
5408 break;
5409 }
5410 case NO_OVERWRITE: {
5411 // Get a new heap number in r5. r6 and r7 are scratch.
5412 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5413 }
5414 default: break;
5415 }
5416 __ bind(&got_a_heap_number);
5417 // r2: Answer as signed int32.
5418 // r5: Heap number to write answer into.
5419
5420 // Nothing can go wrong now, so move the heap number to r0, which is the
5421 // result.
5422 __ mov(r0, Operand(r5));
5423
5424 // Tail call that writes the int32 in r2 to the heap number in r0, using
5425 // r3 as scratch. r0 is preserved and returned.
5426 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
5427 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
5428
5429 if (mode_ != NO_OVERWRITE) {
5430 __ bind(&have_to_allocate);
5431 // Get a new heap number in r5. r6 and r7 are scratch.
5432 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5433 __ jmp(&got_a_heap_number);
5434 }
5435
5436 // If all else failed then we go to the runtime system.
5437 __ bind(&slow);
5438 __ push(r1); // restore stack
5439 __ push(r0);
5440 __ mov(r0, Operand(1)); // 1 argument (not counting receiver).
5441 switch (op_) {
5442 case Token::BIT_OR:
5443 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
5444 break;
5445 case Token::BIT_AND:
5446 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
5447 break;
5448 case Token::BIT_XOR:
5449 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
5450 break;
5451 case Token::SAR:
5452 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
5453 break;
5454 case Token::SHR:
5455 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
5456 break;
5457 case Token::SHL:
5458 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
5459 break;
5460 default:
5461 UNREACHABLE();
5462 }
5463}
5464
5465
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005466// Can we multiply by x with max two shifts and an add.
5467// This answers yes to all integers from 2 to 10.
5468static bool IsEasyToMultiplyBy(int x) {
5469 if (x < 2) return false; // Avoid special cases.
5470 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
5471 if (IsPowerOf2(x)) return true; // Simple shift.
5472 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
5473 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
5474 return false;
5475}
5476
5477
5478// Can multiply by anything that IsEasyToMultiplyBy returns true for.
5479// Source and destination may be the same register. This routine does
5480// not set carry and overflow the way a mul instruction would.
5481static void MultiplyByKnownInt(MacroAssembler* masm,
5482 Register source,
5483 Register destination,
5484 int known_int) {
5485 if (IsPowerOf2(known_int)) {
5486 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
5487 } else if (PopCountLessThanEqual2(known_int)) {
5488 int first_bit = BitPosition(known_int);
5489 int second_bit = BitPosition(known_int ^ (1 << first_bit));
5490 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
5491 if (first_bit != 0) {
5492 __ mov(destination, Operand(destination, LSL, first_bit));
5493 }
5494 } else {
5495 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
5496 int the_bit = BitPosition(known_int + 1);
5497 __ rsb(destination, source, Operand(source, LSL, the_bit));
5498 }
5499}
5500
5501
5502// This function (as opposed to MultiplyByKnownInt) takes the known int in a
5503// a register for the cases where it doesn't know a good trick, and may deliver
5504// a result that needs shifting.
5505static void MultiplyByKnownInt2(
5506 MacroAssembler* masm,
5507 Register result,
5508 Register source,
5509 Register known_int_register, // Smi tagged.
5510 int known_int,
5511 int* required_shift) { // Including Smi tag shift
5512 switch (known_int) {
5513 case 3:
5514 __ add(result, source, Operand(source, LSL, 1));
5515 *required_shift = 1;
5516 break;
5517 case 5:
5518 __ add(result, source, Operand(source, LSL, 2));
5519 *required_shift = 1;
5520 break;
5521 case 6:
5522 __ add(result, source, Operand(source, LSL, 1));
5523 *required_shift = 2;
5524 break;
5525 case 7:
5526 __ rsb(result, source, Operand(source, LSL, 3));
5527 *required_shift = 1;
5528 break;
5529 case 9:
5530 __ add(result, source, Operand(source, LSL, 3));
5531 *required_shift = 1;
5532 break;
5533 case 10:
5534 __ add(result, source, Operand(source, LSL, 2));
5535 *required_shift = 2;
5536 break;
5537 default:
5538 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
5539 __ mul(result, source, known_int_register);
5540 *required_shift = 0;
5541 }
5542}
5543
5544
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005545void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
5546 // r1 : x
5547 // r0 : y
5548 // result : r0
5549
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005550 // All ops need to know whether we are dealing with two Smis. Set up r2 to
5551 // tell us that.
5552 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
5553
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005554 switch (op_) {
5555 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005556 Label not_smi;
5557 // Fast path.
5558 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005559 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005560 __ b(ne, &not_smi);
5561 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
5562 // Return if no overflow.
5563 __ Ret(vc);
5564 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
5565
5566 HandleBinaryOpSlowCases(masm,
5567 &not_smi,
5568 Builtins::ADD,
5569 Token::ADD,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005570 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005571 break;
5572 }
5573
5574 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005575 Label not_smi;
5576 // Fast path.
5577 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005578 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005579 __ b(ne, &not_smi);
5580 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
5581 // Return if no overflow.
5582 __ Ret(vc);
5583 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
5584
5585 HandleBinaryOpSlowCases(masm,
5586 &not_smi,
5587 Builtins::SUB,
5588 Token::SUB,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005589 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005590 break;
5591 }
5592
5593 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005594 Label not_smi, slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005595 ASSERT(kSmiTag == 0); // adjust code below
5596 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005597 __ b(ne, &not_smi);
5598 // Remove tag from one operand (but keep sign), so that result is Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005599 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005600 // Do multiplication
5601 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1.
5602 // Go slow on overflows (overflow bit is not set).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005603 __ mov(ip, Operand(r3, ASR, 31));
5604 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
5605 __ b(ne, &slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005606 // Go slow on zero result to handle -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005607 __ tst(r3, Operand(r3));
5608 __ mov(r0, Operand(r3), LeaveCC, ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005609 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005610 // We need -0 if we were multiplying a negative number with 0 to get 0.
5611 // We know one of them was zero.
5612 __ add(r2, r0, Operand(r1), SetCC);
5613 __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl);
5614 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
5615 // Slow case. We fall through here if we multiplied a negative number
5616 // with 0, because that would mean we should produce -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005617 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005618
5619 HandleBinaryOpSlowCases(masm,
5620 &not_smi,
5621 Builtins::MUL,
5622 Token::MUL,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005623 mode_);
5624 break;
5625 }
5626
5627 case Token::DIV:
5628 case Token::MOD: {
5629 Label not_smi;
5630 if (specialized_on_rhs_) {
5631 Label smi_is_unsuitable;
5632 __ BranchOnNotSmi(r1, &not_smi);
5633 if (IsPowerOf2(constant_rhs_)) {
5634 if (op_ == Token::MOD) {
5635 __ and_(r0,
5636 r1,
5637 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
5638 SetCC);
5639 // We now have the answer, but if the input was negative we also
5640 // have the sign bit. Our work is done if the result is
5641 // positive or zero:
5642 __ Ret(pl);
5643 // A mod of a negative left hand side must return a negative number.
5644 // Unfortunately if the answer is 0 then we must return -0. And we
5645 // already optimistically trashed r0 so we may need to restore it.
5646 __ eor(r0, r0, Operand(0x80000000u), SetCC);
5647 // Next two instructions are conditional on the answer being -0.
5648 __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
5649 __ b(eq, &smi_is_unsuitable);
5650 // We need to subtract the dividend. Eg. -3 % 4 == -3.
5651 __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_)));
5652 } else {
5653 ASSERT(op_ == Token::DIV);
5654 __ tst(r1,
5655 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
5656 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
5657 int shift = 0;
5658 int d = constant_rhs_;
5659 while ((d & 1) == 0) {
5660 d >>= 1;
5661 shift++;
5662 }
5663 __ mov(r0, Operand(r1, LSR, shift));
5664 __ bic(r0, r0, Operand(kSmiTagMask));
5665 }
5666 } else {
5667 // Not a power of 2.
5668 __ tst(r1, Operand(0x80000000u));
5669 __ b(ne, &smi_is_unsuitable);
5670 // Find a fixed point reciprocal of the divisor so we can divide by
5671 // multiplying.
5672 double divisor = 1.0 / constant_rhs_;
5673 int shift = 32;
5674 double scale = 4294967296.0; // 1 << 32.
5675 uint32_t mul;
5676 // Maximise the precision of the fixed point reciprocal.
5677 while (true) {
5678 mul = static_cast<uint32_t>(scale * divisor);
5679 if (mul >= 0x7fffffff) break;
5680 scale *= 2.0;
5681 shift++;
5682 }
5683 mul++;
5684 __ mov(r2, Operand(mul));
5685 __ umull(r3, r2, r2, r1);
5686 __ mov(r2, Operand(r2, LSR, shift - 31));
5687 // r2 is r1 / rhs. r2 is not Smi tagged.
5688 // r0 is still the known rhs. r0 is Smi tagged.
5689 // r1 is still the unkown lhs. r1 is Smi tagged.
5690 int required_r4_shift = 0; // Including the Smi tag shift of 1.
5691 // r4 = r2 * r0.
5692 MultiplyByKnownInt2(masm,
5693 r4,
5694 r2,
5695 r0,
5696 constant_rhs_,
5697 &required_r4_shift);
5698 // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs).
5699 if (op_ == Token::DIV) {
5700 __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC);
5701 __ b(ne, &smi_is_unsuitable); // There was a remainder.
5702 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5703 } else {
5704 ASSERT(op_ == Token::MOD);
5705 __ sub(r0, r1, Operand(r4, LSL, required_r4_shift));
5706 }
5707 }
5708 __ Ret();
5709 __ bind(&smi_is_unsuitable);
5710 } else {
5711 __ jmp(&not_smi);
5712 }
5713 HandleBinaryOpSlowCases(masm,
5714 &not_smi,
5715 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV,
5716 op_,
5717 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005718 break;
5719 }
5720
5721 case Token::BIT_OR:
5722 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005723 case Token::BIT_XOR:
5724 case Token::SAR:
5725 case Token::SHR:
5726 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005727 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005728 ASSERT(kSmiTag == 0); // adjust code below
5729 __ tst(r2, Operand(kSmiTagMask));
5730 __ b(ne, &slow);
5731 switch (op_) {
5732 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
5733 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
5734 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005735 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005736 // Remove tags from right operand.
5737 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5738 // Use only the 5 least significant bits of the shift count.
5739 __ and_(r2, r2, Operand(0x1f));
5740 __ mov(r0, Operand(r1, ASR, r2));
5741 // Smi tag result.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005742 __ bic(r0, r0, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005743 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005744 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005745 // Remove tags from operands. We can't do this on a 31 bit number
5746 // because then the 0s get shifted into bit 30 instead of bit 31.
5747 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5748 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5749 // Use only the 5 least significant bits of the shift count.
5750 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005751 __ mov(r3, Operand(r3, LSR, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005752 // Unsigned shift is not allowed to produce a negative number, so
5753 // check the sign bit and the sign bit after Smi tagging.
5754 __ tst(r3, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005755 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005756 // Smi tag result.
5757 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005758 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005759 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005760 // Remove tags from operands.
5761 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
5762 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // y
5763 // Use only the 5 least significant bits of the shift count.
5764 __ and_(r2, r2, Operand(0x1f));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005765 __ mov(r3, Operand(r3, LSL, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005766 // Check that the signed result fits in a Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005767 __ add(r2, r3, Operand(0x40000000), SetCC);
5768 __ b(mi, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005769 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005770 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005771 default: UNREACHABLE();
5772 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005773 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005774 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005775 HandleNonSmiBitwiseOp(masm);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005776 break;
5777 }
5778
5779 default: UNREACHABLE();
5780 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005781 // This code should be unreachable.
5782 __ stop("Unreachable");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005783}
5784
5785
5786void StackCheckStub::Generate(MacroAssembler* masm) {
5787 Label within_limit;
5788 __ mov(ip, Operand(ExternalReference::address_of_stack_guard_limit()));
5789 __ ldr(ip, MemOperand(ip));
5790 __ cmp(sp, Operand(ip));
5791 __ b(hs, &within_limit);
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00005792 // Do tail-call to runtime routine. Runtime routines expect at least one
5793 // argument, so give it a Smi.
5794 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005795 __ push(r0);
5796 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1);
5797 __ bind(&within_limit);
5798
5799 __ StubReturn(1);
5800}
5801
5802
5803void UnarySubStub::Generate(MacroAssembler* masm) {
5804 Label undo;
5805 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005806 Label not_smi;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005807
5808 // Enter runtime system if the value is not a smi.
5809 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005810 __ b(ne, &not_smi);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005811
5812 // Enter runtime system if the value of the expression is zero
5813 // to make sure that we switch between 0 and -0.
5814 __ cmp(r0, Operand(0));
5815 __ b(eq, &slow);
5816
5817 // The value of the expression is a smi that is not zero. Try
5818 // optimistic subtraction '0 - value'.
5819 __ rsb(r1, r0, Operand(0), SetCC);
5820 __ b(vs, &slow);
5821
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005822 __ mov(r0, Operand(r1)); // Set r0 to result.
5823 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005824
5825 // Enter runtime system.
5826 __ bind(&slow);
5827 __ push(r0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005828 __ mov(r0, Operand(0)); // Set number of arguments.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005829 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
5830
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005831 __ bind(&not_smi);
5832 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
5833 __ b(ne, &slow);
5834 // r0 is a heap number. Get a new heap number in r1.
5835 if (overwrite_) {
5836 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5837 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
5838 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5839 } else {
5840 AllocateHeapNumber(masm, &slow, r1, r2, r3);
5841 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
5842 __ str(r2, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
5843 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5844 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
5845 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
5846 __ mov(r0, Operand(r1));
5847 }
5848 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005849}
5850
5851
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005852void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005853 // r0 holds the exception.
5854
5855 // Adjust this code if not the case.
5856 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
5857
5858 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005859 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
5860 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005861
5862 // Restore the next handler and frame pointer, discard handler state.
5863 ASSERT(StackHandlerConstants::kNextOffset == 0);
5864 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005865 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005866 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
5867 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
5868
5869 // Before returning we restore the context from the frame pointer if
5870 // not NULL. The frame pointer is NULL in the exception handler of a
5871 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005872 __ cmp(fp, Operand(0));
5873 // Set cp to NULL if fp is NULL.
5874 __ mov(cp, Operand(0), LeaveCC, eq);
5875 // Restore cp otherwise.
5876 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005877#ifdef DEBUG
5878 if (FLAG_debug_code) {
5879 __ mov(lr, Operand(pc));
5880 }
5881#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005882 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005883 __ pop(pc);
5884}
5885
5886
5887void CEntryStub::GenerateThrowOutOfMemory(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005888 // Adjust this code if not the case.
5889 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
5890
5891 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005892 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005893 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005894
5895 // Unwind the handlers until the ENTRY handler is found.
5896 Label loop, done;
5897 __ bind(&loop);
5898 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005899 const int kStateOffset = StackHandlerConstants::kStateOffset;
5900 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005901 __ cmp(r2, Operand(StackHandler::ENTRY));
5902 __ b(eq, &done);
5903 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005904 const int kNextOffset = StackHandlerConstants::kNextOffset;
5905 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005906 __ jmp(&loop);
5907 __ bind(&done);
5908
5909 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005910 ASSERT(StackHandlerConstants::kNextOffset == 0);
5911 __ pop(r0);
5912 __ str(r0, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005913
5914 // Set external caught exception to false.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005915 ExternalReference external_caught(Top::k_external_caught_exception_address);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005916 __ mov(r0, Operand(false));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005917 __ mov(r2, Operand(external_caught));
5918 __ str(r0, MemOperand(r2));
5919
5920 // Set pending exception and r0 to out of memory exception.
5921 Failure* out_of_memory = Failure::OutOfMemoryException();
5922 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
5923 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
5924 __ str(r0, MemOperand(r2));
5925
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005926 // Stack layout at this point. See also StackHandlerConstants.
5927 // sp -> state (ENTRY)
5928 // fp
5929 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005930
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005931 // Discard handler state (r2 is not used) and restore frame pointer.
5932 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
5933 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
5934 // Before returning we restore the context from the frame pointer if
5935 // not NULL. The frame pointer is NULL in the exception handler of a
5936 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005937 __ cmp(fp, Operand(0));
5938 // Set cp to NULL if fp is NULL.
5939 __ mov(cp, Operand(0), LeaveCC, eq);
5940 // Restore cp otherwise.
5941 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005942#ifdef DEBUG
5943 if (FLAG_debug_code) {
5944 __ mov(lr, Operand(pc));
5945 }
5946#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005947 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005948 __ pop(pc);
5949}
5950
5951
5952void CEntryStub::GenerateCore(MacroAssembler* masm,
5953 Label* throw_normal_exception,
5954 Label* throw_out_of_memory_exception,
5955 StackFrame::Type frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005956 bool do_gc,
5957 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005958 // r0: result parameter for PerformGC, if any
5959 // r4: number of arguments including receiver (C callee-saved)
5960 // r5: pointer to builtin function (C callee-saved)
5961 // r6: pointer to the first argument (C callee-saved)
5962
5963 if (do_gc) {
5964 // Passing r0.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005965 ExternalReference gc_reference = ExternalReference::perform_gc_function();
5966 __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005967 }
5968
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005969 ExternalReference scope_depth =
5970 ExternalReference::heap_always_allocate_scope_depth();
5971 if (always_allocate) {
5972 __ mov(r0, Operand(scope_depth));
5973 __ ldr(r1, MemOperand(r0));
5974 __ add(r1, r1, Operand(1));
5975 __ str(r1, MemOperand(r0));
5976 }
5977
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005978 // Call C built-in.
5979 // r0 = argc, r1 = argv
5980 __ mov(r0, Operand(r4));
5981 __ mov(r1, Operand(r6));
5982
5983 // TODO(1242173): To let the GC traverse the return address of the exit
5984 // frames, we need to know where the return address is. Right now,
5985 // we push it on the stack to be able to find it again, but we never
5986 // restore from it in case of changes, which makes it impossible to
5987 // support moving the C entry code stub. This should be fixed, but currently
5988 // this is OK because the CEntryStub gets generated so early in the V8 boot
5989 // sequence that it is not moving ever.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005990 masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
5991 masm->push(lr);
5992 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00005993
5994 if (always_allocate) {
5995 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
5996 // though (contain the result).
5997 __ mov(r2, Operand(scope_depth));
5998 __ ldr(r3, MemOperand(r2));
5999 __ sub(r3, r3, Operand(1));
6000 __ str(r3, MemOperand(r2));
6001 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006002
6003 // check for failure result
6004 Label failure_returned;
6005 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
6006 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
6007 __ add(r2, r0, Operand(1));
6008 __ tst(r2, Operand(kFailureTagMask));
6009 __ b(eq, &failure_returned);
6010
6011 // Exit C frame and return.
6012 // r0:r1: result
6013 // sp: stack pointer
6014 // fp: frame pointer
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006015 __ LeaveExitFrame(frame_type);
6016
6017 // check if we should retry or throw exception
6018 Label retry;
6019 __ bind(&failure_returned);
6020 ASSERT(Failure::RETRY_AFTER_GC == 0);
6021 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
6022 __ b(eq, &retry);
6023
6024 Label continue_exception;
6025 // If the returned failure is EXCEPTION then promote Top::pending_exception().
6026 __ cmp(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
6027 __ b(ne, &continue_exception);
6028
6029 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00006030 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006031 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00006032 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006033 __ ldr(r0, MemOperand(ip));
6034 __ str(r3, MemOperand(ip));
6035
6036 __ bind(&continue_exception);
6037 // Special handling of out of memory exception.
6038 Failure* out_of_memory = Failure::OutOfMemoryException();
6039 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
6040 __ b(eq, throw_out_of_memory_exception);
6041
6042 // Handle normal exception.
6043 __ jmp(throw_normal_exception);
6044
6045 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
6046}
6047
6048
6049void CEntryStub::GenerateBody(MacroAssembler* masm, bool is_debug_break) {
6050 // Called from JavaScript; parameters are on stack as if calling JS function
6051 // r0: number of arguments including receiver
6052 // r1: pointer to builtin function
6053 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006054 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006055 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006056
6057 // NOTE: Invocations of builtins may return failure objects
6058 // instead of a proper result. The builtin entry handles
6059 // this by performing a garbage collection and retrying the
6060 // builtin once.
6061
6062 StackFrame::Type frame_type = is_debug_break
6063 ? StackFrame::EXIT_DEBUG
6064 : StackFrame::EXIT;
6065
6066 // Enter the exit frame that transitions from JavaScript to C++.
6067 __ EnterExitFrame(frame_type);
6068
6069 // r4: number of arguments (C callee-saved)
6070 // r5: pointer to builtin function (C callee-saved)
6071 // r6: pointer to first argument (C callee-saved)
6072
6073 Label throw_out_of_memory_exception;
6074 Label throw_normal_exception;
6075
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006076 // Call into the runtime system. Collect garbage before the call if
6077 // running with --gc-greedy set.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006078 if (FLAG_gc_greedy) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00006079 Failure* failure = Failure::RetryAfterGC(0);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006080 __ mov(r0, Operand(reinterpret_cast<intptr_t>(failure)));
6081 }
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006082 GenerateCore(masm, &throw_normal_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006083 &throw_out_of_memory_exception,
6084 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006085 FLAG_gc_greedy,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006086 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006087
6088 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006089 GenerateCore(masm,
6090 &throw_normal_exception,
6091 &throw_out_of_memory_exception,
6092 frame_type,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006093 true,
6094 false);
6095
6096 // Do full GC and retry runtime call one final time.
6097 Failure* failure = Failure::InternalError();
6098 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
6099 GenerateCore(masm,
6100 &throw_normal_exception,
6101 &throw_out_of_memory_exception,
6102 frame_type,
6103 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006104 true);
6105
6106 __ bind(&throw_out_of_memory_exception);
6107 GenerateThrowOutOfMemory(masm);
6108 // control flow for generated will not return.
6109
6110 __ bind(&throw_normal_exception);
6111 GenerateThrowTOS(masm);
6112}
6113
6114
6115void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
6116 // r0: code entry
6117 // r1: function
6118 // r2: receiver
6119 // r3: argc
6120 // [sp+0]: argv
6121
6122 Label invoke, exit;
6123
6124 // Called from C, so do not pop argc and args on exit (preserve sp)
6125 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006126 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006127 __ stm(db_w, sp, kCalleeSaved | lr.bit());
6128
6129 // Get address of argv, see stm above.
6130 // r0: code entry
6131 // r1: function
6132 // r2: receiver
6133 // r3: argc
6134 __ add(r4, sp, Operand((kNumCalleeSaved + 1)*kPointerSize));
6135 __ ldr(r4, MemOperand(r4)); // argv
6136
6137 // Push a frame with special values setup to mark it as an entry frame.
6138 // r0: code entry
6139 // r1: function
6140 // r2: receiver
6141 // r3: argc
6142 // r4: argv
6143 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
6144 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
6145 __ mov(r7, Operand(~ArgumentsAdaptorFrame::SENTINEL));
6146 __ mov(r6, Operand(Smi::FromInt(marker)));
6147 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6148 __ ldr(r5, MemOperand(r5));
6149 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
6150
6151 // Setup frame pointer for the frame to be pushed.
6152 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6153
6154 // Call a faked try-block that does the invoke.
6155 __ bl(&invoke);
6156
6157 // Caught exception: Store result (exception) in the pending
6158 // exception field in the JSEnv and return a failure sentinel.
6159 // Coming in here the fp will be invalid because the PushTryHandler below
6160 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00006161 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006162 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006163 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006164 __ b(&exit);
6165
6166 // Invoke: Link this frame into the handler chain.
6167 __ bind(&invoke);
6168 // Must preserve r0-r4, r5-r7 are available.
6169 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006170 // If an exception not caught by another handler occurs, this handler
6171 // returns control to the code after the bl(&invoke) above, which
6172 // restores all kCalleeSaved registers (including cp and fp) to their
6173 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006174
6175 // Clear any pending exceptions.
6176 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
6177 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00006178 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006179 __ str(r5, MemOperand(ip));
6180
6181 // Invoke the function by calling through JS entry trampoline builtin.
6182 // Notice that we cannot store a reference to the trampoline code directly in
6183 // this stub, because runtime stubs are not traversed when doing GC.
6184
6185 // Expected registers by Builtins::JSEntryTrampoline
6186 // r0: code entry
6187 // r1: function
6188 // r2: receiver
6189 // r3: argc
6190 // r4: argv
6191 if (is_construct) {
6192 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
6193 __ mov(ip, Operand(construct_entry));
6194 } else {
6195 ExternalReference entry(Builtins::JSEntryTrampoline);
6196 __ mov(ip, Operand(entry));
6197 }
6198 __ ldr(ip, MemOperand(ip)); // deref address
6199
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006200 // Branch and link to JSEntryTrampoline. We don't use the double underscore
6201 // macro for the add instruction because we don't want the coverage tool
6202 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006203 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006204 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006205
6206 // Unlink this frame from the handler chain. When reading the
6207 // address of the next handler, there is no need to use the address
6208 // displacement since the current stack pointer (sp) points directly
6209 // to the stack handler.
6210 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
6211 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
6212 __ str(r3, MemOperand(ip));
6213 // No need to restore registers
6214 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
6215
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006216
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006217 __ bind(&exit); // r0 holds result
6218 // Restore the top frame descriptors from the stack.
6219 __ pop(r3);
6220 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6221 __ str(r3, MemOperand(ip));
6222
6223 // Reset the stack to the callee saved registers.
6224 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6225
6226 // Restore callee-saved registers and return.
6227#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006228 if (FLAG_debug_code) {
6229 __ mov(lr, Operand(pc));
6230 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006231#endif
6232 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
6233}
6234
6235
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006236// This stub performs an instanceof, calling the builtin function if
6237// necessary. Uses r1 for the object, r0 for the function that it may
6238// be an instance of (these are fetched from the stack).
6239void InstanceofStub::Generate(MacroAssembler* masm) {
6240 // Get the object - slow case for smis (we may need to throw an exception
6241 // depending on the rhs).
6242 Label slow, loop, is_instance, is_not_instance;
6243 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
6244 __ BranchOnSmi(r0, &slow);
6245
6246 // Check that the left hand is a JS object and put map in r3.
6247 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
6248 __ b(lt, &slow);
6249 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
6250 __ b(gt, &slow);
6251
6252 // Get the prototype of the function (r4 is result, r2 is scratch).
6253 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
6254 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
6255
6256 // Check that the function prototype is a JS object.
6257 __ BranchOnSmi(r4, &slow);
6258 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
6259 __ b(lt, &slow);
6260 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
6261 __ b(gt, &slow);
6262
6263 // Register mapping: r3 is object map and r4 is function prototype.
6264 // Get prototype of object into r2.
6265 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
6266
6267 // Loop through the prototype chain looking for the function prototype.
6268 __ bind(&loop);
6269 __ cmp(r2, Operand(r4));
6270 __ b(eq, &is_instance);
6271 __ cmp(r2, Operand(Factory::null_value()));
6272 __ b(eq, &is_not_instance);
6273 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
6274 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
6275 __ jmp(&loop);
6276
6277 __ bind(&is_instance);
6278 __ mov(r0, Operand(Smi::FromInt(0)));
6279 __ pop();
6280 __ pop();
6281 __ mov(pc, Operand(lr)); // Return.
6282
6283 __ bind(&is_not_instance);
6284 __ mov(r0, Operand(Smi::FromInt(1)));
6285 __ pop();
6286 __ pop();
6287 __ mov(pc, Operand(lr)); // Return.
6288
6289 // Slow-case. Tail call builtin.
6290 __ bind(&slow);
6291 __ mov(r0, Operand(1)); // Arg count without receiver.
6292 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
6293}
6294
6295
ager@chromium.org7c537e22008-10-16 08:43:32 +00006296void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006297 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006298 Label adaptor;
6299 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6300 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
6301 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006302 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006303
ager@chromium.org7c537e22008-10-16 08:43:32 +00006304 // Nothing to do: The formal number of parameters has already been
6305 // passed in register r0 by calling function. Just return it.
ager@chromium.org9085a012009-05-11 19:22:57 +00006306 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006307
ager@chromium.org7c537e22008-10-16 08:43:32 +00006308 // Arguments adaptor case: Read the arguments length from the
6309 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006310 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006311 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org9085a012009-05-11 19:22:57 +00006312 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006313}
6314
6315
ager@chromium.org7c537e22008-10-16 08:43:32 +00006316void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
6317 // The displacement is the offset of the last parameter (if any)
6318 // relative to the frame pointer.
6319 static const int kDisplacement =
6320 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006321
ager@chromium.org7c537e22008-10-16 08:43:32 +00006322 // Check that the key is a smi.
6323 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006324 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006325
ager@chromium.org7c537e22008-10-16 08:43:32 +00006326 // Check if the calling frame is an arguments adaptor frame.
6327 Label adaptor;
6328 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6329 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
6330 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
6331 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006332
ager@chromium.org7c537e22008-10-16 08:43:32 +00006333 // Check index against formal parameters count limit passed in
6334 // through register eax. Use unsigned comparison to get negative
6335 // check for free.
6336 __ cmp(r1, r0);
6337 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006338
ager@chromium.org7c537e22008-10-16 08:43:32 +00006339 // Read the argument from the stack and return it.
6340 __ sub(r3, r0, r1);
6341 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6342 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006343 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006344
6345 // Arguments adaptor case: Check index against actual arguments
6346 // limit found in the arguments adaptor frame. Use unsigned
6347 // comparison to get negative check for free.
6348 __ bind(&adaptor);
6349 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6350 __ cmp(r1, r0);
6351 __ b(cs, &slow);
6352
6353 // Read the argument from the adaptor frame and return it.
6354 __ sub(r3, r0, r1);
6355 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6356 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006357 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006358
6359 // Slow-case: Handle non-smi or out-of-bounds access to arguments
6360 // by calling the runtime system.
6361 __ bind(&slow);
6362 __ push(r1);
6363 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1);
6364}
6365
6366
6367void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
6368 // Check if the calling frame is an arguments adaptor frame.
6369 Label runtime;
6370 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6371 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
6372 __ cmp(r3, Operand(ArgumentsAdaptorFrame::SENTINEL));
6373 __ b(ne, &runtime);
6374
6375 // Patch the arguments.length and the parameters pointer.
6376 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6377 __ str(r0, MemOperand(sp, 0 * kPointerSize));
6378 __ add(r3, r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
6379 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
6380 __ str(r3, MemOperand(sp, 1 * kPointerSize));
6381
6382 // Do the runtime call to allocate the arguments object.
6383 __ bind(&runtime);
6384 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006385}
6386
6387
6388void CallFunctionStub::Generate(MacroAssembler* masm) {
6389 Label slow;
6390 // Get the function to call from the stack.
6391 // function, receiver [, arguments]
6392 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
6393
6394 // Check that the function is really a JavaScript function.
6395 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006396 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006397 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006398 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006399 __ b(ne, &slow);
6400
6401 // Fast-case: Invoke the function now.
6402 // r1: pushed function
6403 ParameterCount actual(argc_);
6404 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
6405
6406 // Slow-case: Non-function called.
6407 __ bind(&slow);
6408 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006409 __ mov(r2, Operand(0));
6410 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
6411 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
6412 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006413}
6414
6415
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006416int CompareStub::MinorKey() {
6417 // Encode the two parameters in a unique 16 bit value.
6418 ASSERT(static_cast<unsigned>(cc_) >> 28 < (1 << 15));
6419 return (static_cast<unsigned>(cc_) >> 27) | (strict_ ? 1 : 0);
6420}
6421
6422
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006423#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006424
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00006425} } // namespace v8::internal