blob: 95c50f262f0da4318287d2057ac301c24aef25fb [file] [log] [blame]
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001// Copyright 2010 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"
ager@chromium.orgc4c92722009-11-18 14:12:51 +000032#include "compiler.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033#include "debug.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000034#include "parser.h"
35#include "register-allocator-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036#include "runtime.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000037#include "scopes.h"
38
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039
kasperl@chromium.org71affb52009-05-26 05:44:31 +000040namespace v8 {
41namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
ager@chromium.org65dad4b2009-04-23 08:48:43 +000043#define __ ACCESS_MASM(masm_)
44
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000045static void EmitIdenticalObjectComparison(MacroAssembler* masm,
46 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000047 Condition cc,
48 bool never_nan_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000049static void EmitSmiNonsmiComparison(MacroAssembler* masm,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000050 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000051 Label* slow,
52 bool strict);
53static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc);
54static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000055static void MultiplyByKnownInt(MacroAssembler* masm,
56 Register source,
57 Register destination,
58 int known_int);
59static bool IsEasyToMultiplyBy(int x);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000060
61
62
ager@chromium.orge2902be2009-06-08 12:21:35 +000063// -------------------------------------------------------------------------
64// Platform-specific DeferredCode functions.
65
66void DeferredCode::SaveRegisters() {
67 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) {
68 int action = registers_[i];
69 if (action == kPush) {
70 __ push(RegisterAllocator::ToRegister(i));
71 } else if (action != kIgnore && (action & kSyncedFlag) == 0) {
72 __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
73 }
74 }
75}
76
77
78void DeferredCode::RestoreRegisters() {
79 // Restore registers in reverse order due to the stack.
80 for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) {
81 int action = registers_[i];
82 if (action == kPush) {
83 __ pop(RegisterAllocator::ToRegister(i));
84 } else if (action != kIgnore) {
85 action &= ~kSyncedFlag;
86 __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
87 }
88 }
89}
90
ager@chromium.org3bf7b912008-11-17 09:09:45 +000091
92// -------------------------------------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000093// CodeGenState implementation.
94
ager@chromium.org7c537e22008-10-16 08:43:32 +000095CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000096 : owner_(owner),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000097 true_target_(NULL),
98 false_target_(NULL),
99 previous_(NULL) {
100 owner_->set_state(this);
101}
102
103
ager@chromium.org7c537e22008-10-16 08:43:32 +0000104CodeGenState::CodeGenState(CodeGenerator* owner,
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),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000108 true_target_(true_target),
109 false_target_(false_target),
110 previous_(owner->state()) {
111 owner_->set_state(this);
112}
113
114
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000115CodeGenState::~CodeGenState() {
116 ASSERT(owner_->state() == this);
117 owner_->set_state(previous_);
118}
119
120
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000121// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +0000122// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123
ager@chromium.org5c838252010-02-19 08:53:10 +0000124CodeGenerator::CodeGenerator(MacroAssembler* masm)
125 : deferred_(8),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000126 masm_(masm),
ager@chromium.org5c838252010-02-19 08:53:10 +0000127 info_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000128 frame_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000129 allocator_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000130 cc_reg_(al),
131 state_(NULL),
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000132 function_return_is_shadowed_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133}
134
135
ager@chromium.org5c838252010-02-19 08:53:10 +0000136Scope* CodeGenerator::scope() { return info_->function()->scope(); }
137
138
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000140// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000142// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143// cp: callee's context
144
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000145void CodeGenerator::Generate(CompilationInfo* info) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000146 // Record the position for debugging purposes.
ager@chromium.org5c838252010-02-19 08:53:10 +0000147 CodeForFunctionPosition(info->function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148
149 // Initialize state.
ager@chromium.org5c838252010-02-19 08:53:10 +0000150 info_ = info;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000151 ASSERT(allocator_ == NULL);
152 RegisterAllocator register_allocator(this);
153 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000154 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000155 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000156 cc_reg_ = al;
157 {
158 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000160 // Entry:
161 // Stack: receiver, arguments
162 // lr: return address
163 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000165 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000167 allocator_->Initialize();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000168
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169#ifdef DEBUG
170 if (strlen(FLAG_stop_at) > 0 &&
ager@chromium.org5c838252010-02-19 08:53:10 +0000171 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000172 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000173 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174 }
175#endif
176
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000177 if (info->mode() == CompilationInfo::PRIMARY) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000178 frame_->Enter();
179 // tos: code slot
180
181 // Allocate space for locals and initialize them. This also checks
182 // for stack overflow.
183 frame_->AllocateStackSlots();
184
185 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org5c838252010-02-19 08:53:10 +0000186 int heap_slots = scope()->num_heap_slots();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000187 if (heap_slots > 0) {
188 // Allocate local context.
189 // Get outer context and create a new context based on it.
190 __ ldr(r0, frame_->Function());
191 frame_->EmitPush(r0);
192 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
193 FastNewContextStub stub(heap_slots);
194 frame_->CallStub(&stub, 1);
195 } else {
196 frame_->CallRuntime(Runtime::kNewContext, 1);
197 }
198
199#ifdef DEBUG
200 JumpTarget verified_true;
201 __ cmp(r0, Operand(cp));
202 verified_true.Branch(eq);
203 __ stop("NewContext: r0 is expected to be the same as cp");
204 verified_true.Bind();
205#endif
206 // Update context local.
207 __ str(cp, frame_->Context());
208 }
209
210 // TODO(1241774): Improve this code:
211 // 1) only needed if we have a context
212 // 2) no need to recompute context ptr every single time
213 // 3) don't copy parameter operand code from SlotOperand!
214 {
215 Comment cmnt2(masm_, "[ copy context parameters into .context");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000216 // Note that iteration order is relevant here! If we have the same
217 // parameter twice (e.g., function (x, y, x)), and that parameter
218 // needs to be copied into the context, it must be the last argument
219 // passed to the parameter that needs to be copied. This is a rare
220 // case so we don't check for it, instead we rely on the copying
221 // order: such a parameter is copied repeatedly into the same
222 // context location and thus the last value is what is seen inside
223 // the function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000224 for (int i = 0; i < scope()->num_parameters(); i++) {
225 Variable* par = scope()->parameter(i);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000226 Slot* slot = par->slot();
227 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000228 ASSERT(!scope()->is_global_scope()); // No params in global scope.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000229 __ ldr(r1, frame_->ParameterAt(i));
230 // Loads r2 with context; used below in RecordWrite.
231 __ str(r1, SlotOperand(slot, r2));
232 // Load the offset into r3.
233 int slot_offset =
234 FixedArray::kHeaderSize + slot->index() * kPointerSize;
235 __ mov(r3, Operand(slot_offset));
236 __ RecordWrite(r2, r3, r1);
237 }
238 }
239 }
240
241 // Store the arguments object. This must happen after context
242 // initialization because the arguments object may be stored in the
243 // context.
ager@chromium.org5c838252010-02-19 08:53:10 +0000244 if (scope()->arguments() != NULL) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000245 Comment cmnt(masm_, "[ allocate arguments object");
ager@chromium.org5c838252010-02-19 08:53:10 +0000246 ASSERT(scope()->arguments_shadow() != NULL);
247 Variable* arguments = scope()->arguments()->var();
248 Variable* shadow = scope()->arguments_shadow()->var();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000249 ASSERT(arguments != NULL && arguments->slot() != NULL);
250 ASSERT(shadow != NULL && shadow->slot() != NULL);
251 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
252 __ ldr(r2, frame_->Function());
253 // The receiver is below the arguments, the return address, and the
254 // frame pointer on the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +0000255 const int kReceiverDisplacement = 2 + scope()->num_parameters();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000256 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
ager@chromium.org5c838252010-02-19 08:53:10 +0000257 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000258 frame_->Adjust(3);
259 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
260 frame_->CallStub(&stub, 3);
261 frame_->EmitPush(r0);
262 StoreToSlot(arguments->slot(), NOT_CONST_INIT);
263 StoreToSlot(shadow->slot(), NOT_CONST_INIT);
264 frame_->Drop(); // Value is no longer needed.
265 }
266
267 // Initialize ThisFunction reference if present.
ager@chromium.org5c838252010-02-19 08:53:10 +0000268 if (scope()->is_function_scope() && scope()->function() != NULL) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000269 __ mov(ip, Operand(Factory::the_hole_value()));
270 frame_->EmitPush(ip);
ager@chromium.org5c838252010-02-19 08:53:10 +0000271 StoreToSlot(scope()->function()->slot(), NOT_CONST_INIT);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000272 }
273 } else {
274 // When used as the secondary compiler for splitting, r1, cp,
275 // fp, and lr have been pushed on the stack. Adjust the virtual
276 // frame to match this state.
277 frame_->Adjust(4);
278 allocator_->Unuse(r1);
279 allocator_->Unuse(lr);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000280
281 // Bind all the bailout labels to the beginning of the function.
282 List<CompilationInfo::Bailout*>* bailouts = info->bailouts();
283 for (int i = 0; i < bailouts->length(); i++) {
284 __ bind(bailouts->at(i)->label());
285 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000286 }
287
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000288 // Initialize the function return target after the locals are set
289 // up, because it needs the expected frame height from the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000290 function_return_.set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000291 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000292
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000293 // Generate code to 'execute' declarations and initialize functions
294 // (source elements). In case of an illegal redeclaration we need to
295 // handle that instead of processing the declarations.
ager@chromium.org5c838252010-02-19 08:53:10 +0000296 if (scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000297 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000298 scope()->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299 } else {
300 Comment cmnt(masm_, "[ declarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000301 ProcessDeclarations(scope()->declarations());
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000302 // Bail out if a stack-overflow exception occurred when processing
303 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000304 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305 }
306
mads.s.ager31e71382008-08-13 09:32:07 +0000307 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000308 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000309 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000310 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311
312 // Compile the body of the function in a vanilla state. Don't
313 // bother compiling all the code if the scope has an illegal
314 // redeclaration.
ager@chromium.org5c838252010-02-19 08:53:10 +0000315 if (!scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316 Comment cmnt(masm_, "[ function body");
317#ifdef DEBUG
318 bool is_builtin = Bootstrapper::IsActive();
319 bool should_trace =
320 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000321 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000322 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000323 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000324 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325#endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000326 VisitStatementsAndSpill(info->function()->body());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328 }
329
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000330 // Generate the return sequence if necessary.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000331 if (has_valid_frame() || function_return_.is_linked()) {
332 if (!function_return_.is_linked()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000333 CodeForReturnPosition(info->function());
ager@chromium.org4af710e2009-09-15 12:20:11 +0000334 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000335 // exit
336 // r0: result
337 // sp: stack pointer
338 // fp: frame pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000339 // cp: callee's context
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000340 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +0000341
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000342 function_return_.Bind();
343 if (FLAG_trace) {
344 // Push the return value on the stack as the parameter.
345 // Runtime::TraceExit returns the parameter as it is.
346 frame_->EmitPush(r0);
347 frame_->CallRuntime(Runtime::kTraceExit, 1);
348 }
349
ager@chromium.org4af710e2009-09-15 12:20:11 +0000350 // Add a label for checking the size of the code used for returning.
351 Label check_exit_codesize;
352 masm_->bind(&check_exit_codesize);
353
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000354 // Calculate the exact length of the return sequence and make sure that
355 // the constant pool is not emitted inside of the return sequence.
ager@chromium.org5c838252010-02-19 08:53:10 +0000356 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000357 int return_sequence_length = Assembler::kJSReturnSequenceLength;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000358 if (!masm_->ImmediateFitsAddrMode1Instruction(sp_delta)) {
359 // Additional mov instruction generated.
360 return_sequence_length++;
361 }
362 masm_->BlockConstPoolFor(return_sequence_length);
363
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000364 // Tear down the frame which will restore the caller's frame pointer and
365 // the link register.
366 frame_->Exit();
367
ager@chromium.org4af710e2009-09-15 12:20:11 +0000368 // Here we use masm_-> instead of the __ macro to avoid the code coverage
369 // tool from instrumenting as we rely on the code size here.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000370 masm_->add(sp, sp, Operand(sp_delta));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000371 masm_->Jump(lr);
372
373 // Check that the size of the code used for returning matches what is
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000374 // expected by the debugger. The add instruction above is an addressing
375 // mode 1 instruction where there are restrictions on which immediate values
376 // can be encoded in the instruction and which immediate values requires
377 // use of an additional instruction for moving the immediate to a temporary
378 // register.
379 ASSERT_EQ(return_sequence_length,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000380 masm_->InstructionsGeneratedSince(&check_exit_codesize));
mads.s.ager31e71382008-08-13 09:32:07 +0000381 }
382
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000383 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000384 ASSERT(!has_cc());
385 ASSERT(state_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000386 ASSERT(!function_return_is_shadowed_);
387 function_return_.Unuse();
388 DeleteFrame();
389
390 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000391 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000392 ProcessDeferred();
393 }
394
395 allocator_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000396}
397
398
ager@chromium.org7c537e22008-10-16 08:43:32 +0000399MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
400 // Currently, this assertion will fail if we try to assign to
401 // a constant variable that is constant because it is read-only
402 // (such as the variable referring to a named function expression).
403 // We need to implement assignments to read-only variables.
404 // Ideally, we should do this during AST generation (by converting
405 // such assignments into expression statements); however, in general
406 // we may not be able to make the decision until past AST generation,
407 // that is when the entire program is known.
408 ASSERT(slot != NULL);
409 int index = slot->index();
410 switch (slot->type()) {
411 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000412 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000413
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000414 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000415 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000416
417 case Slot::CONTEXT: {
418 // Follow the context chain if necessary.
419 ASSERT(!tmp.is(cp)); // do not overwrite context register
420 Register context = cp;
421 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000422 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000423 // Load the closure.
424 // (All contexts, even 'with' contexts, have a closure,
425 // and it is the same for all contexts inside a function.
426 // There is no need to go to the function context first.)
427 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
428 // Load the function context (which is the incoming, outer context).
429 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
430 context = tmp;
431 }
432 // We may have a 'with' context now. Get the function context.
433 // (In fact this mov may never be the needed, since the scope analysis
434 // may not permit a direct context access in this case and thus we are
435 // always at a function context. However it is safe to dereference be-
436 // cause the function context of a function context is itself. Before
437 // deleting this mov we should try to create a counter-example first,
438 // though...)
439 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
440 return ContextOperand(tmp, index);
441 }
442
443 default:
444 UNREACHABLE();
445 return MemOperand(r0, 0);
446 }
447}
448
449
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000450MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
451 Slot* slot,
452 Register tmp,
453 Register tmp2,
454 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000455 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000456 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000457
ager@chromium.org381abbb2009-02-25 13:23:22 +0000458 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
459 if (s->num_heap_slots() > 0) {
460 if (s->calls_eval()) {
461 // Check that extension is NULL.
462 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
463 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000464 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000465 }
466 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
467 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
468 context = tmp;
469 }
470 }
471 // Check that last extension is NULL.
472 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
473 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000474 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000475 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000476 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000477}
478
479
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000480// Loads a value on TOS. If it is a boolean value, the result may have been
481// (partially) translated into branches, or it may have set the condition
482// code register. If force_cc is set, the value is forced to set the
483// condition code register and no value is pushed. If the condition code
484// register was set, has_cc() is true and cc_reg_ contains the condition to
485// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000486void CodeGenerator::LoadCondition(Expression* x,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000487 JumpTarget* true_target,
488 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000489 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000490 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000491 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000492
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000493 { CodeGenState new_state(this, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000494 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000495
496 // If we hit a stack overflow, we may not have actually visited
497 // the expression. In that case, we ensure that we have a
498 // valid-looking frame state because we will continue to generate
499 // code as we unwind the C++ stack.
500 //
501 // It's possible to have both a stack overflow and a valid frame
502 // state (eg, a subexpression overflowed, visiting it returned
503 // with a dummied frame state, and visiting this expression
504 // returned with a normal-looking state).
505 if (HasStackOverflow() &&
506 has_valid_frame() &&
507 !has_cc() &&
508 frame_->height() == original_height) {
509 true_target->Jump();
510 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000511 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000512 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000513 // Convert the TOS value to a boolean in the condition code register.
514 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000515 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000516 ASSERT(!force_cc || !has_valid_frame() || has_cc());
517 ASSERT(!has_valid_frame() ||
518 (has_cc() && frame_->height() == original_height) ||
519 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000520}
521
522
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000523void CodeGenerator::Load(Expression* expr) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000524#ifdef DEBUG
525 int original_height = frame_->height();
526#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000527 JumpTarget true_target;
528 JumpTarget false_target;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000529 LoadCondition(expr, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000530
531 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000532 // Convert cc_reg_ into a boolean value.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000533 JumpTarget loaded;
534 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000535 materialize_true.Branch(cc_reg_);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000536 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000537 frame_->EmitPush(r0);
538 loaded.Jump();
539 materialize_true.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000540 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000541 frame_->EmitPush(r0);
542 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000543 cc_reg_ = al;
544 }
545
546 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000547 // We have at least one condition value that has been "translated"
548 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000549 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000550 if (frame_ != NULL) {
551 loaded.Jump(); // Don't lose the current TOS.
552 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000554 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000556 true_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000557 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000558 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000559 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000560 // If both "true" and "false" need to be loaded jump across the code for
561 // "false".
562 if (both) {
563 loaded.Jump();
564 }
565 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000567 false_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000568 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000569 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000571 // A value is loaded on all paths reaching this point.
572 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000573 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000574 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000575 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000576 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000577}
578
579
ager@chromium.org7c537e22008-10-16 08:43:32 +0000580void CodeGenerator::LoadGlobal() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000581 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000582 __ ldr(r0, GlobalObject());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000583 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000584}
585
586
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000587void CodeGenerator::LoadGlobalReceiver(Register scratch) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000588 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000589 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
590 __ ldr(scratch,
591 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000592 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000593}
594
595
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000596void CodeGenerator::LoadTypeofExpression(Expression* expr) {
597 // Special handling of identifiers as subexpressions of typeof.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000598 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000599 Variable* variable = expr->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600 if (variable != NULL && !variable->is_this() && variable->is_global()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000601 // For a global variable we build the property reference
602 // <global>.<variable> and perform a (regular non-contextual) property
603 // load to make sure we do not get reference errors.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000604 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
605 Literal key(variable->name());
ager@chromium.org236ad962008-09-25 09:45:57 +0000606 Property property(&global, &key, RelocInfo::kNoPosition);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000607 Reference ref(this, &property);
608 ref.GetValueAndSpill();
609 } else if (variable != NULL && variable->slot() != NULL) {
610 // For a variable that rewrites to a slot, we signal it is the immediate
611 // subexpression of a typeof.
612 LoadFromSlot(variable->slot(), INSIDE_TYPEOF);
613 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000614 } else {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000615 // Anything else can be handled normally.
616 LoadAndSpill(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617 }
618}
619
620
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000621Reference::Reference(CodeGenerator* cgen,
622 Expression* expression,
623 bool persist_after_get)
624 : cgen_(cgen),
625 expression_(expression),
626 type_(ILLEGAL),
627 persist_after_get_(persist_after_get) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000628 cgen->LoadReference(this);
629}
630
631
632Reference::~Reference() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000633 ASSERT(is_unloaded() || is_illegal());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000634}
635
636
ager@chromium.org7c537e22008-10-16 08:43:32 +0000637void CodeGenerator::LoadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000638 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000639 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000640 Expression* e = ref->expression();
641 Property* property = e->AsProperty();
642 Variable* var = e->AsVariableProxy()->AsVariable();
643
644 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000645 // The expression is either a property or a variable proxy that rewrites
646 // to a property.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000647 LoadAndSpill(property->obj());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000648 if (property->key()->IsPropertyName()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000649 ref->set_type(Reference::NAMED);
650 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000651 LoadAndSpill(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 ref->set_type(Reference::KEYED);
653 }
654 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000655 // The expression is a variable proxy that does not rewrite to a
656 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000657 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000658 LoadGlobal();
659 ref->set_type(Reference::NAMED);
660 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000661 ASSERT(var->slot() != NULL);
662 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000663 }
664 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000665 // Anything else is a runtime error.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000666 LoadAndSpill(e);
667 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000668 }
669}
670
671
ager@chromium.org7c537e22008-10-16 08:43:32 +0000672void CodeGenerator::UnloadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000673 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000674 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000675 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000676 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000677 if (size > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000678 frame_->EmitPop(r0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000679 frame_->Drop(size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000680 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000681 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000682 ref->set_unloaded();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000683}
684
685
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000686// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
687// register to a boolean in the condition code register. The code
688// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000689void CodeGenerator::ToBoolean(JumpTarget* true_target,
690 JumpTarget* false_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000691 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000692 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000693 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000694 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000695
696 // Fast case checks
697
mads.s.ager31e71382008-08-13 09:32:07 +0000698 // Check if the value is 'false'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000699 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
700 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000701 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000702
mads.s.ager31e71382008-08-13 09:32:07 +0000703 // Check if the value is 'true'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000704 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
705 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000706 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000707
mads.s.ager31e71382008-08-13 09:32:07 +0000708 // Check if the value is 'undefined'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000709 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
710 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000711 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000712
mads.s.ager31e71382008-08-13 09:32:07 +0000713 // Check if the value is a smi.
714 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000715 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000716 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000717 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000718
719 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000720 frame_->EmitPush(r0);
721 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000722 // Convert the result (r0) to a condition code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000723 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
724 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000725
726 cc_reg_ = ne;
727}
728
729
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000730void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000731 OverwriteMode overwrite_mode,
732 int constant_rhs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000733 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000734 // sp[0] : y
735 // sp[1] : x
736 // result : r0
737
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000738 // Stub is entered with a call: 'return address' is in lr.
739 switch (op) {
740 case Token::ADD: // fall through.
741 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000742 case Token::MUL:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000743 case Token::DIV:
744 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000745 case Token::BIT_OR:
746 case Token::BIT_AND:
747 case Token::BIT_XOR:
748 case Token::SHL:
749 case Token::SHR:
750 case Token::SAR: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000751 frame_->EmitPop(r0); // r0 : y
752 frame_->EmitPop(r1); // r1 : x
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000753 GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000754 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755 break;
756 }
757
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758 case Token::COMMA:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000759 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000760 // simply discard left value
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000761 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000762 break;
763
764 default:
765 // Other cases should have been handled before this point.
766 UNREACHABLE();
767 break;
768 }
769}
770
771
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000772class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000773 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000774 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000775 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000776 bool reversed,
777 OverwriteMode overwrite_mode)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000778 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000779 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000780 reversed_(reversed),
781 overwrite_mode_(overwrite_mode) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000782 set_comment("[ DeferredInlinedSmiOperation");
783 }
784
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000785 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000786
787 private:
788 Token::Value op_;
789 int value_;
790 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000791 OverwriteMode overwrite_mode_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000792};
793
794
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000795void DeferredInlineSmiOperation::Generate() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000796 switch (op_) {
797 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000798 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000799 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000800 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
801 __ mov(r1, Operand(Smi::FromInt(value_)));
802 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000803 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
804 __ mov(r0, Operand(Smi::FromInt(value_)));
805 }
806 break;
807 }
808
809 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000810 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000811 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000812 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
813 __ mov(r1, Operand(Smi::FromInt(value_)));
814 } else {
815 __ add(r1, r0, Operand(Smi::FromInt(value_)));
816 __ mov(r0, Operand(Smi::FromInt(value_)));
817 }
818 break;
819 }
820
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000821 // For these operations there is no optimistic operation that needs to be
822 // reverted.
823 case Token::MUL:
824 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000825 case Token::BIT_OR:
826 case Token::BIT_XOR:
827 case Token::BIT_AND: {
828 if (reversed_) {
829 __ mov(r1, Operand(Smi::FromInt(value_)));
830 } else {
831 __ mov(r1, Operand(r0));
832 __ mov(r0, Operand(Smi::FromInt(value_)));
833 }
834 break;
835 }
836
837 case Token::SHL:
838 case Token::SHR:
839 case Token::SAR: {
840 if (!reversed_) {
841 __ mov(r1, Operand(r0));
842 __ mov(r0, Operand(Smi::FromInt(value_)));
843 } else {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000844 UNREACHABLE(); // Should have been handled in SmiOperation.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000845 }
846 break;
847 }
848
849 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +0000850 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000851 UNREACHABLE();
852 break;
853 }
854
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000855 GenericBinaryOpStub stub(op_, overwrite_mode_, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000856 __ CallStub(&stub);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000857}
858
859
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000860static bool PopCountLessThanEqual2(unsigned int x) {
861 x &= x - 1;
862 return (x & (x - 1)) == 0;
863}
864
865
866// Returns the index of the lowest bit set.
867static int BitPosition(unsigned x) {
868 int bit_posn = 0;
869 while ((x & 0xf) == 0) {
870 bit_posn += 4;
871 x >>= 4;
872 }
873 while ((x & 1) == 0) {
874 bit_posn++;
875 x >>= 1;
876 }
877 return bit_posn;
878}
879
880
ager@chromium.org7c537e22008-10-16 08:43:32 +0000881void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000882 Handle<Object> value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000883 bool reversed,
884 OverwriteMode mode) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000885 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000886 // NOTE: This is an attempt to inline (a bit) more of the code for
887 // some possible smi operations (like + and -) when (at least) one
888 // of the operands is a literal smi. With this optimization, the
889 // performance of the system is increased by ~15%, and the generated
890 // code size is increased by ~1% (measured on a combination of
891 // different benchmarks).
892
mads.s.ager31e71382008-08-13 09:32:07 +0000893 // sp[0] : operand
894
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000895 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000897 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000898 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000900 bool something_to_inline = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901 switch (op) {
902 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000903 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000904 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000906 __ add(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000907 deferred->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000909 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000910 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911 break;
912 }
913
914 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000915 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000916 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917
ager@chromium.orge2902be2009-06-08 12:21:35 +0000918 if (reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000919 __ rsb(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000920 } else {
921 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000922 }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000923 deferred->Branch(vs);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000924 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000925 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000926 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000927 break;
928 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000930
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000931 case Token::BIT_OR:
932 case Token::BIT_XOR:
933 case Token::BIT_AND: {
934 DeferredCode* deferred =
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000935 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000936 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000937 deferred->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000938 switch (op) {
939 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
940 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
941 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
942 default: UNREACHABLE();
943 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000944 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000945 break;
946 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000948 case Token::SHL:
949 case Token::SHR:
950 case Token::SAR: {
951 if (reversed) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000952 something_to_inline = false;
953 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000954 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000955 int shift_value = int_value & 0x1f; // least significant 5 bits
956 DeferredCode* deferred =
957 new DeferredInlineSmiOperation(op, shift_value, false, mode);
958 __ tst(r0, Operand(kSmiTagMask));
959 deferred->Branch(ne);
960 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
961 switch (op) {
962 case Token::SHL: {
963 if (shift_value != 0) {
964 __ mov(r2, Operand(r2, LSL, shift_value));
965 }
966 // check that the *unsigned* result fits in a smi
967 __ add(r3, r2, Operand(0x40000000), SetCC);
968 deferred->Branch(mi);
969 break;
970 }
971 case Token::SHR: {
972 // LSR by immediate 0 means shifting 32 bits.
973 if (shift_value != 0) {
974 __ mov(r2, Operand(r2, LSR, shift_value));
975 }
976 // check that the *unsigned* result fits in a smi
977 // neither of the two high-order bits can be set:
978 // - 0x80000000: high bit would be lost when smi tagging
979 // - 0x40000000: this number would convert to negative when
980 // smi tagging these two cases can only happen with shifts
981 // by 0 or 1 when handed a valid smi
982 __ and_(r3, r2, Operand(0xc0000000), SetCC);
983 deferred->Branch(ne);
984 break;
985 }
986 case Token::SAR: {
987 if (shift_value != 0) {
988 // ASR by immediate 0 means shifting 32 bits.
989 __ mov(r2, Operand(r2, ASR, shift_value));
990 }
991 break;
992 }
993 default: UNREACHABLE();
994 }
995 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
996 deferred->BindExit();
997 break;
998 }
999
1000 case Token::MOD: {
1001 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
1002 something_to_inline = false;
1003 break;
1004 }
1005 DeferredCode* deferred =
1006 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
1007 unsigned mask = (0x80000000u | kSmiTagMask);
1008 __ tst(r0, Operand(mask));
1009 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1010 mask = (int_value << kSmiTagSize) - 1;
1011 __ and_(r0, r0, Operand(mask));
1012 deferred->BindExit();
1013 break;
1014 }
1015
1016 case Token::MUL: {
1017 if (!IsEasyToMultiplyBy(int_value)) {
1018 something_to_inline = false;
1019 break;
1020 }
1021 DeferredCode* deferred =
1022 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
1023 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1024 max_smi_that_wont_overflow <<= kSmiTagSize;
1025 unsigned mask = 0x80000000u;
1026 while ((mask & max_smi_that_wont_overflow) == 0) {
1027 mask |= mask >> 1;
1028 }
1029 mask |= kSmiTagMask;
1030 // This does a single mask that checks for a too high value in a
1031 // conservative way and for a non-Smi. It also filters out negative
1032 // numbers, unfortunately, but since this code is inline we prefer
1033 // brevity to comprehensiveness.
1034 __ tst(r0, Operand(mask));
1035 deferred->Branch(ne);
1036 MultiplyByKnownInt(masm_, r0, r0, int_value);
1037 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001038 break;
1039 }
1040
1041 default:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001042 something_to_inline = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001043 break;
1044 }
1045
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001046 if (!something_to_inline) {
1047 if (!reversed) {
1048 frame_->EmitPush(r0);
1049 __ mov(r0, Operand(value));
1050 frame_->EmitPush(r0);
1051 GenericBinaryOperation(op, mode, int_value);
1052 } else {
1053 __ mov(ip, Operand(value));
1054 frame_->EmitPush(ip);
1055 frame_->EmitPush(r0);
1056 GenericBinaryOperation(op, mode, kUnknownIntValue);
1057 }
1058 }
1059
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001060 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001061}
1062
1063
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001064void CodeGenerator::Comparison(Condition cc,
1065 Expression* left,
1066 Expression* right,
1067 bool strict) {
1068 if (left != NULL) LoadAndSpill(left);
1069 if (right != NULL) LoadAndSpill(right);
1070
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001071 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001072 // sp[0] : y
1073 // sp[1] : x
1074 // result : cc register
1075
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001076 // Strict only makes sense for equality comparisons.
1077 ASSERT(!strict || cc == eq);
1078
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001079 JumpTarget exit;
1080 JumpTarget smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001081 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1082 if (cc == gt || cc == le) {
1083 cc = ReverseCondition(cc);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001084 frame_->EmitPop(r1);
1085 frame_->EmitPop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001086 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001087 frame_->EmitPop(r0);
1088 frame_->EmitPop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001089 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090 __ orr(r2, r0, Operand(r1));
1091 __ tst(r2, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001092 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001093
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001094 // Perform non-smi comparison by stub.
1095 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1096 // We call with 0 args because there are 0 on the stack.
1097 CompareStub stub(cc, strict);
1098 frame_->CallStub(&stub, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001099 __ cmp(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001100 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001102 // Do smi comparisons by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001103 smi.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104 __ cmp(r1, Operand(r0));
1105
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001106 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107 cc_reg_ = cc;
1108}
1109
1110
mads.s.ager31e71382008-08-13 09:32:07 +00001111// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001112void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001113 CallFunctionFlags flags,
1114 int position) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001115 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001116 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001117 int arg_count = args->length();
1118 for (int i = 0; i < arg_count; i++) {
1119 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001120 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001121
kasper.lund7276f142008-07-30 08:49:36 +00001122 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001123 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001124
kasper.lund7276f142008-07-30 08:49:36 +00001125 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001126 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001127 CallFunctionStub call_function(arg_count, in_loop, flags);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001128 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001129
1130 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001131 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001132 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133}
1134
1135
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001136void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001137 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001138 ASSERT(has_cc());
1139 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001140 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001141 cc_reg_ = al;
1142}
1143
1144
ager@chromium.org7c537e22008-10-16 08:43:32 +00001145void CodeGenerator::CheckStack() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001146 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3811b432009-10-28 14:53:37 +00001147 Comment cmnt(masm_, "[ check stack");
1148 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1149 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1150 // the implicit 8 byte offset that always applies to operations with pc and
1151 // gives a return address 12 bytes down.
1152 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1153 masm_->cmp(sp, Operand(ip));
1154 StackCheckStub stub;
1155 // Call the stub if lower.
1156 masm_->mov(pc,
1157 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1158 RelocInfo::CODE_TARGET),
1159 LeaveCC,
1160 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001161}
1162
1163
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001164void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1165#ifdef DEBUG
1166 int original_height = frame_->height();
1167#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001168 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001169 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1170 VisitAndSpill(statements->at(i));
1171 }
1172 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1173}
1174
1175
ager@chromium.org7c537e22008-10-16 08:43:32 +00001176void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001177#ifdef DEBUG
1178 int original_height = frame_->height();
1179#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001180 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001181 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001182 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001183 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001184 VisitStatementsAndSpill(node->statements());
1185 if (node->break_target()->is_linked()) {
1186 node->break_target()->Bind();
1187 }
1188 node->break_target()->Unuse();
1189 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001190}
1191
1192
ager@chromium.org7c537e22008-10-16 08:43:32 +00001193void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001194 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3811b432009-10-28 14:53:37 +00001195 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001196 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001197 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001198 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001199 frame_->EmitPush(r0);
1200 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001201 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001202}
1203
1204
ager@chromium.org7c537e22008-10-16 08:43:32 +00001205void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001206#ifdef DEBUG
1207 int original_height = frame_->height();
1208#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001209 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001210 Comment cmnt(masm_, "[ Declaration");
1211 Variable* var = node->proxy()->var();
1212 ASSERT(var != NULL); // must have been resolved
1213 Slot* slot = var->slot();
1214
1215 // If it was not possible to allocate the variable at compile time,
1216 // we need to "declare" it at runtime to make sure it actually
1217 // exists in the local context.
1218 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1219 // Variables with a "LOOKUP" slot were introduced as non-locals
1220 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001221 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001222 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001223 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001224 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001225 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001226 // Declaration nodes are always declared in only two modes.
1227 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1228 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001229 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001230 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001231 // Push initial value, if any.
1232 // Note: For variables we must not push an initial value (such as
1233 // 'undefined') because we may have a (legal) redeclaration and we
1234 // must not destroy the current value.
1235 if (node->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001236 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001237 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001238 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001239 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001240 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001241 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001242 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001243 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001244 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001245 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001246 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001247 return;
1248 }
1249
1250 ASSERT(!var->is_global());
1251
1252 // If we have a function or a constant, we need to initialize the variable.
1253 Expression* val = NULL;
1254 if (node->mode() == Variable::CONST) {
1255 val = new Literal(Factory::the_hole_value());
1256 } else {
1257 val = node->fun(); // NULL if we don't have a function
1258 }
1259
1260 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001261 {
1262 // Set initial value.
1263 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001264 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001265 target.SetValue(NOT_CONST_INIT);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001266 }
1267 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001268 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001269 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001270 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001271}
1272
1273
ager@chromium.org7c537e22008-10-16 08:43:32 +00001274void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001275#ifdef DEBUG
1276 int original_height = frame_->height();
1277#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001278 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001279 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001280 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001281 Expression* expression = node->expression();
1282 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001283 LoadAndSpill(expression);
1284 frame_->Drop();
1285 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001286}
1287
1288
ager@chromium.org7c537e22008-10-16 08:43:32 +00001289void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001290#ifdef DEBUG
1291 int original_height = frame_->height();
1292#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001293 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001295 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001296 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001297 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001298}
1299
1300
ager@chromium.org7c537e22008-10-16 08:43:32 +00001301void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001302#ifdef DEBUG
1303 int original_height = frame_->height();
1304#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001305 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001306 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001307 // Generate different code depending on which parts of the if statement
1308 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309 bool has_then_stm = node->HasThenStatement();
1310 bool has_else_stm = node->HasElseStatement();
1311
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001312 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001314 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001316 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001317 JumpTarget then;
1318 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001319 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001320 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001321 if (frame_ != NULL) {
1322 Branch(false, &else_);
1323 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001324 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001325 if (frame_ != NULL || then.is_linked()) {
1326 then.Bind();
1327 VisitAndSpill(node->then_statement());
1328 }
1329 if (frame_ != NULL) {
1330 exit.Jump();
1331 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001332 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001333 if (else_.is_linked()) {
1334 else_.Bind();
1335 VisitAndSpill(node->else_statement());
1336 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001337
1338 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001339 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001341 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001342 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001343 LoadConditionAndSpill(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001344 if (frame_ != NULL) {
1345 Branch(false, &exit);
1346 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001347 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001348 if (frame_ != NULL || then.is_linked()) {
1349 then.Bind();
1350 VisitAndSpill(node->then_statement());
1351 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352
1353 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001354 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001355 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001356 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001357 // if (!cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001358 LoadConditionAndSpill(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001359 if (frame_ != NULL) {
1360 Branch(true, &exit);
1361 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001362 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001363 if (frame_ != NULL || else_.is_linked()) {
1364 else_.Bind();
1365 VisitAndSpill(node->else_statement());
1366 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001367
1368 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001369 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370 ASSERT(!has_then_stm && !has_else_stm);
1371 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001372 LoadConditionAndSpill(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001373 if (frame_ != NULL) {
1374 if (has_cc()) {
1375 cc_reg_ = al;
1376 } else {
1377 frame_->Drop();
1378 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001379 }
1380 }
1381
1382 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001383 if (exit.is_linked()) {
1384 exit.Bind();
1385 }
1386 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001387}
1388
1389
ager@chromium.org7c537e22008-10-16 08:43:32 +00001390void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001391 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001393 CodeForStatementPosition(node);
1394 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001395}
1396
1397
ager@chromium.org7c537e22008-10-16 08:43:32 +00001398void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001399 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001400 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001401 CodeForStatementPosition(node);
1402 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001403}
1404
1405
ager@chromium.org7c537e22008-10-16 08:43:32 +00001406void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001407 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001408 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001409
ager@chromium.org4af710e2009-09-15 12:20:11 +00001410 CodeForStatementPosition(node);
1411 LoadAndSpill(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001412 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001413 frame_->EmitPop(r0);
1414 function_return_.Jump();
1415 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001416 // Pop the result from the frame and prepare the frame for
1417 // returning thus making it easier to merge.
1418 frame_->EmitPop(r0);
1419 frame_->PrepareForReturn();
1420
1421 function_return_.Jump();
1422 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001423}
1424
1425
ager@chromium.org7c537e22008-10-16 08:43:32 +00001426void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001427#ifdef DEBUG
1428 int original_height = frame_->height();
1429#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001430 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001431 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001432 CodeForStatementPosition(node);
1433 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001434 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001435 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001436 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001437 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001438 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001439#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001440 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001441 __ cmp(r0, Operand(cp));
1442 verified_true.Branch(eq);
1443 __ stop("PushContext: r0 is expected to be the same as cp");
1444 verified_true.Bind();
1445#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001446 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001447 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001448 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449}
1450
1451
ager@chromium.org7c537e22008-10-16 08:43:32 +00001452void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001453#ifdef DEBUG
1454 int original_height = frame_->height();
1455#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001456 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001457 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001458 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001459 // Pop context.
1460 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1461 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001462 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001463 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001464}
1465
1466
ager@chromium.org7c537e22008-10-16 08:43:32 +00001467void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001468#ifdef DEBUG
1469 int original_height = frame_->height();
1470#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001471 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001472 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001473 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001474 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001475
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001476 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001477
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001478 JumpTarget next_test;
1479 JumpTarget fall_through;
1480 JumpTarget default_entry;
1481 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482 ZoneList<CaseClause*>* cases = node->cases();
1483 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001484 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001485
1486 for (int i = 0; i < length; i++) {
1487 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001489 // Remember the default clause and compile it at the end.
1490 default_clause = clause;
1491 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001492 }
1493
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001494 Comment cmnt(masm_, "[ Case clause");
1495 // Compile the test.
1496 next_test.Bind();
1497 next_test.Unuse();
1498 // Duplicate TOS.
1499 __ ldr(r0, frame_->Top());
1500 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001501 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001502 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001503
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001504 // Before entering the body from the test, remove the switch value from
1505 // the stack.
1506 frame_->Drop();
1507
1508 // Label the body so that fall through is enabled.
1509 if (i > 0 && cases->at(i - 1)->is_default()) {
1510 default_exit.Bind();
1511 } else {
1512 fall_through.Bind();
1513 fall_through.Unuse();
1514 }
1515 VisitStatementsAndSpill(clause->statements());
1516
1517 // If control flow can fall through from the body, jump to the next body
1518 // or the end of the statement.
1519 if (frame_ != NULL) {
1520 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1521 default_entry.Jump();
1522 } else {
1523 fall_through.Jump();
1524 }
1525 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001526 }
1527
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001528 // The final "test" removes the switch value.
1529 next_test.Bind();
1530 frame_->Drop();
1531
1532 // If there is a default clause, compile it.
1533 if (default_clause != NULL) {
1534 Comment cmnt(masm_, "[ Default clause");
1535 default_entry.Bind();
1536 VisitStatementsAndSpill(default_clause->statements());
1537 // If control flow can fall out of the default and there is a case after
1538 // it, jup to that case's body.
1539 if (frame_ != NULL && default_exit.is_bound()) {
1540 default_exit.Jump();
1541 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001542 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001543
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001544 if (fall_through.is_linked()) {
1545 fall_through.Bind();
1546 }
1547
1548 if (node->break_target()->is_linked()) {
1549 node->break_target()->Bind();
1550 }
1551 node->break_target()->Unuse();
1552 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001553}
1554
1555
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001556void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001557#ifdef DEBUG
1558 int original_height = frame_->height();
1559#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001560 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001561 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001562 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001563 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001564 JumpTarget body(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001565
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001566 // Label the top of the loop for the backward CFG edge. If the test
1567 // is always true we can use the continue target, and if the test is
1568 // always false there is no need.
1569 ConditionAnalysis info = AnalyzeCondition(node->cond());
1570 switch (info) {
1571 case ALWAYS_TRUE:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001572 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001573 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001574 break;
1575 case ALWAYS_FALSE:
1576 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1577 break;
1578 case DONT_KNOW:
1579 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1580 body.Bind();
1581 break;
1582 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001583
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001584 CheckStack(); // TODO(1222600): ignore if body contains calls.
1585 VisitAndSpill(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001586
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001587 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001588 switch (info) {
1589 case ALWAYS_TRUE:
1590 // If control can fall off the end of the body, jump back to the
1591 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001592 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001593 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001594 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001595 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001596 case ALWAYS_FALSE:
1597 // If we have a continue in the body, we only have to bind its
1598 // jump target.
1599 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001600 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001601 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001602 break;
1603 case DONT_KNOW:
1604 // We have to compile the test expression if it can be reached by
1605 // control flow falling out of the body or via continue.
1606 if (node->continue_target()->is_linked()) {
1607 node->continue_target()->Bind();
1608 }
1609 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001610 Comment cmnt(masm_, "[ DoWhileCondition");
1611 CodeForDoWhileConditionPosition(node);
1612 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001613 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001614 // A invalid frame here indicates that control did not
1615 // fall out of the test expression.
1616 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001617 }
1618 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001619 break;
1620 }
1621
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001622 if (node->break_target()->is_linked()) {
1623 node->break_target()->Bind();
1624 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001625 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1626}
1627
1628
1629void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
1630#ifdef DEBUG
1631 int original_height = frame_->height();
1632#endif
1633 VirtualFrame::SpilledScope spilled_scope;
1634 Comment cmnt(masm_, "[ WhileStatement");
1635 CodeForStatementPosition(node);
1636
1637 // If the test is never true and has no side effects there is no need
1638 // to compile the test or body.
1639 ConditionAnalysis info = AnalyzeCondition(node->cond());
1640 if (info == ALWAYS_FALSE) return;
1641
1642 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1643
1644 // Label the top of the loop with the continue target for the backward
1645 // CFG edge.
1646 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
1647 node->continue_target()->Bind();
1648
1649 if (info == DONT_KNOW) {
1650 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001651 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001652 if (has_valid_frame()) {
1653 // A NULL frame indicates that control did not fall out of the
1654 // test expression.
1655 Branch(false, node->break_target());
1656 }
1657 if (has_valid_frame() || body.is_linked()) {
1658 body.Bind();
1659 }
1660 }
1661
1662 if (has_valid_frame()) {
1663 CheckStack(); // TODO(1222600): ignore if body contains calls.
1664 VisitAndSpill(node->body());
1665
1666 // If control flow can fall out of the body, jump back to the top.
1667 if (has_valid_frame()) {
1668 node->continue_target()->Jump();
1669 }
1670 }
1671 if (node->break_target()->is_linked()) {
1672 node->break_target()->Bind();
1673 }
1674 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1675}
1676
1677
1678void CodeGenerator::VisitForStatement(ForStatement* node) {
1679#ifdef DEBUG
1680 int original_height = frame_->height();
1681#endif
1682 VirtualFrame::SpilledScope spilled_scope;
1683 Comment cmnt(masm_, "[ ForStatement");
1684 CodeForStatementPosition(node);
1685 if (node->init() != NULL) {
1686 VisitAndSpill(node->init());
1687 }
1688
1689 // If the test is never true there is no need to compile the test or
1690 // body.
1691 ConditionAnalysis info = AnalyzeCondition(node->cond());
1692 if (info == ALWAYS_FALSE) return;
1693
1694 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1695
1696 // If there is no update statement, label the top of the loop with the
1697 // continue target, otherwise with the loop target.
1698 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
1699 if (node->next() == NULL) {
1700 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
1701 node->continue_target()->Bind();
1702 } else {
1703 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1704 loop.Bind();
1705 }
1706
1707 // If the test is always true, there is no need to compile it.
1708 if (info == DONT_KNOW) {
1709 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001710 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001711 if (has_valid_frame()) {
1712 Branch(false, node->break_target());
1713 }
1714 if (has_valid_frame() || body.is_linked()) {
1715 body.Bind();
1716 }
1717 }
1718
1719 if (has_valid_frame()) {
1720 CheckStack(); // TODO(1222600): ignore if body contains calls.
1721 VisitAndSpill(node->body());
1722
1723 if (node->next() == NULL) {
1724 // If there is no update statement and control flow can fall out
1725 // of the loop, jump directly to the continue label.
1726 if (has_valid_frame()) {
1727 node->continue_target()->Jump();
1728 }
1729 } else {
1730 // If there is an update statement and control flow can reach it
1731 // via falling out of the body of the loop or continuing, we
1732 // compile the update statement.
1733 if (node->continue_target()->is_linked()) {
1734 node->continue_target()->Bind();
1735 }
1736 if (has_valid_frame()) {
1737 // Record source position of the statement as this code which is
1738 // after the code for the body actually belongs to the loop
1739 // statement and not the body.
1740 CodeForStatementPosition(node);
1741 VisitAndSpill(node->next());
1742 loop.Jump();
1743 }
1744 }
1745 }
1746 if (node->break_target()->is_linked()) {
1747 node->break_target()->Bind();
1748 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001749 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001750}
1751
1752
ager@chromium.org7c537e22008-10-16 08:43:32 +00001753void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001754#ifdef DEBUG
1755 int original_height = frame_->height();
1756#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001757 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001758 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001759 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001760
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001761 JumpTarget primitive;
1762 JumpTarget jsobject;
1763 JumpTarget fixed_array;
1764 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
1765 JumpTarget end_del_check;
1766 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767
1768 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001769 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001770
1771 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1772 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001773 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001774 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1775 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001776 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001777 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1778 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001779 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001780
1781 // Stack layout in body:
1782 // [iteration counter (Smi)]
1783 // [length of array]
1784 // [FixedArray]
1785 // [Map or 0]
1786 // [Object]
1787
1788 // Check if enumerable is already a JSObject
1789 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001790 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001791 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001792 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001793
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001794 primitive.Bind();
1795 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001796 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001797
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001798 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001799 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001800 // r0: value to be iterated over
1801 frame_->EmitPush(r0); // Push the object being iterated over.
1802
1803 // Check cache validity in generated code. This is a fast case for
1804 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1805 // guarantee cache validity, call the runtime system to check cache
1806 // validity or get the property names in a fixed array.
1807 JumpTarget call_runtime;
1808 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
1809 JumpTarget check_prototype;
1810 JumpTarget use_cache;
1811 __ mov(r1, Operand(r0));
1812 loop.Bind();
1813 // Check that there are no elements.
1814 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
1815 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
1816 __ cmp(r2, r4);
1817 call_runtime.Branch(ne);
1818 // Check that instance descriptors are not empty so that we can
1819 // check for an enum cache. Leave the map in r3 for the subsequent
1820 // prototype load.
1821 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
1822 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
1823 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
1824 __ cmp(r2, ip);
1825 call_runtime.Branch(eq);
1826 // Check that there in an enum cache in the non-empty instance
1827 // descriptors. This is the case if the next enumeration index
1828 // field does not contain a smi.
1829 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
1830 __ tst(r2, Operand(kSmiTagMask));
1831 call_runtime.Branch(eq);
1832 // For all objects but the receiver, check that the cache is empty.
1833 // r4: empty fixed array root.
1834 __ cmp(r1, r0);
1835 check_prototype.Branch(eq);
1836 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
1837 __ cmp(r2, r4);
1838 call_runtime.Branch(ne);
1839 check_prototype.Bind();
1840 // Load the prototype from the map and loop if non-null.
1841 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
1842 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1843 __ cmp(r1, ip);
1844 loop.Branch(ne);
1845 // The enum cache is valid. Load the map of the object being
1846 // iterated over and use the cache for the iteration.
1847 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
1848 use_cache.Jump();
1849
1850 call_runtime.Bind();
1851 // Call the runtime to get the property names for the object.
1852 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001853 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001854
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001855 // If we got a map from the runtime call, we can do a fast
1856 // modification check. Otherwise, we got a fixed array, and we have
1857 // to do a slow check.
1858 // r0: map or fixed array (result from call to
1859 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001860 __ mov(r2, Operand(r0));
1861 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001862 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
1863 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001864 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001865
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001866 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001867 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001868 // r0: map (either the result from a call to
1869 // Runtime::kGetPropertyNamesFast or has been fetched directly from
1870 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001871 __ mov(r1, Operand(r0));
1872 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1873 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1874 __ ldr(r2,
1875 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1876
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001877 frame_->EmitPush(r0); // map
1878 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001879 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001880 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001881 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001882 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001883 frame_->EmitPush(r0);
1884 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001885
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001886 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001888 frame_->EmitPush(r1); // insert 0 in place of Map
1889 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001890
1891 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001892 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001893 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001894 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001895 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001896 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001897
1898 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001899 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00001900 // sp[0] : index
1901 // sp[1] : array/enum cache length
1902 // sp[2] : array or enum cache
1903 // sp[3] : 0 or map
1904 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001905 // Grab the current frame's height for the break and continue
1906 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001907 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1908 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001909
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001910 __ ldr(r0, frame_->ElementAt(0)); // load the current count
1911 __ ldr(r1, frame_->ElementAt(1)); // load the length
1912 __ cmp(r0, Operand(r1)); // compare to the array length
1913 node->break_target()->Branch(hs);
1914
1915 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001916
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001917 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001918 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001919 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1920 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1921
1922 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001923 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924 // Check if this (still) matches the map of the enumerable.
1925 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001926 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1928 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001929 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001930
1931 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001932 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
1933 frame_->EmitPush(r0);
1934 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001935 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001936 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001937
1938 // If the property has been removed while iterating, we just skip it.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001939 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1940 __ cmp(r3, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001941 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001942
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001943 end_del_check.Bind();
1944 // Store the entry in the 'each' expression and take another spin in the
1945 // loop. r3: i'th entry of the enum cache (or string there of)
1946 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001947 { Reference each(this, node->each());
1948 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001949 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001950 __ ldr(r0, frame_->ElementAt(each.size()));
1951 frame_->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001952 each.SetValue(NOT_CONST_INIT);
1953 frame_->Drop(2);
1954 } else {
1955 // If the reference was to a slot we rely on the convenient property
1956 // that it doesn't matter whether a value (eg, r3 pushed above) is
1957 // right on top of or right underneath a zero-sized reference.
1958 each.SetValue(NOT_CONST_INIT);
1959 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00001960 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001961 }
1962 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001963 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001964 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001965 VisitAndSpill(node->body());
1966
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001967 // Next. Reestablish a spilled frame in case we are coming here via
1968 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001969 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001970 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001971 frame_->EmitPop(r0);
1972 __ add(r0, r0, Operand(Smi::FromInt(1)));
1973 frame_->EmitPush(r0);
1974 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001975
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001976 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
1977 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001978 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001979 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001980
1981 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001982 exit.Bind();
1983 node->continue_target()->Unuse();
1984 node->break_target()->Unuse();
1985 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001986}
1987
1988
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001989void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001990#ifdef DEBUG
1991 int original_height = frame_->height();
1992#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001993 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001994 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001995 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001996
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001997 JumpTarget try_block;
1998 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001999
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002000 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002001 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002002 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002003
2004 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002005 Variable* catch_var = node->catch_var()->var();
2006 ASSERT(catch_var != NULL && catch_var->slot() != NULL);
2007 StoreToSlot(catch_var->slot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002008
2009 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002010 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002012 VisitStatementsAndSpill(node->catch_block()->statements());
2013 if (frame_ != NULL) {
2014 exit.Jump();
2015 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002016
2017
2018 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002019 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002021 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2022 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002023
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002024 // Shadow the labels for all escapes from the try block, including
2025 // returns. During shadowing, the original label is hidden as the
2026 // LabelShadow and operations on the original actually affect the
2027 // shadowing label.
2028 //
2029 // We should probably try to unify the escaping labels and the return
2030 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002031 int nof_escapes = node->escaping_targets()->length();
2032 List<ShadowTarget*> shadows(1 + nof_escapes);
2033
2034 // Add the shadow target for the function return.
2035 static const int kReturnShadowIndex = 0;
2036 shadows.Add(new ShadowTarget(&function_return_));
2037 bool function_return_was_shadowed = function_return_is_shadowed_;
2038 function_return_is_shadowed_ = true;
2039 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2040
2041 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002043 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002044 }
2045
2046 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002047 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048
2049 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002050 // After shadowing stops, the original labels are unshadowed and the
2051 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002052 bool has_unlinks = false;
2053 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002055 has_unlinks = has_unlinks || shadows[i]->is_linked();
2056 }
2057 function_return_is_shadowed_ = function_return_was_shadowed;
2058
2059 // Get an external reference to the handler address.
2060 ExternalReference handler_address(Top::k_handler_address);
2061
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002062 // If we can fall off the end of the try block, unlink from try chain.
2063 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002064 // The next handler address is on top of the frame. Unlink from
2065 // the handler list and drop the rest of this handler from the
2066 // frame.
2067 ASSERT(StackHandlerConstants::kNextOffset == 0);
2068 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002069 __ mov(r3, Operand(handler_address));
2070 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002071 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002072 if (has_unlinks) {
2073 exit.Jump();
2074 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075 }
2076
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002077 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002078 // jumped to. Deallocate each shadow target.
2079 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002080 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002081 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002082 shadows[i]->Bind();
2083 // Because we can be jumping here (to spilled code) from unspilled
2084 // code, we need to reestablish a spilled frame at this block.
2085 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002087 // Reload sp from the top handler, because some statements that we
2088 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002089 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002090 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002091 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002093 ASSERT(StackHandlerConstants::kNextOffset == 0);
2094 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002096 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002098 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2099 frame_->PrepareForReturn();
2100 }
2101 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002102 }
2103 }
2104
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002105 exit.Bind();
2106 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002107}
2108
2109
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002110void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002111#ifdef DEBUG
2112 int original_height = frame_->height();
2113#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002114 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002115 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002116 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002117
2118 // State: Used to keep track of reason for entering the finally
2119 // block. Should probably be extended to hold information for
2120 // break/continue from within the try block.
2121 enum { FALLING, THROWING, JUMPING };
2122
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002123 JumpTarget try_block;
2124 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002125
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002126 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002127
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002128 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002129 // In case of thrown exceptions, this is where we continue.
2130 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002131 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002132
2133 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002134 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002135
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002136 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2137 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002138
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002139 // Shadow the labels for all escapes from the try block, including
2140 // returns. Shadowing hides the original label as the LabelShadow and
2141 // operations on the original actually affect the shadowing label.
2142 //
2143 // We should probably try to unify the escaping labels and the return
2144 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002145 int nof_escapes = node->escaping_targets()->length();
2146 List<ShadowTarget*> shadows(1 + nof_escapes);
2147
2148 // Add the shadow target for the function return.
2149 static const int kReturnShadowIndex = 0;
2150 shadows.Add(new ShadowTarget(&function_return_));
2151 bool function_return_was_shadowed = function_return_is_shadowed_;
2152 function_return_is_shadowed_ = true;
2153 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2154
2155 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002156 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002157 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002158 }
2159
2160 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002161 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002162
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002163 // Stop the introduced shadowing and count the number of required unlinks.
2164 // After shadowing stops, the original labels are unshadowed and the
2165 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002166 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002167 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002168 shadows[i]->StopShadowing();
2169 if (shadows[i]->is_linked()) nof_unlinks++;
2170 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002171 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002172
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002173 // Get an external reference to the handler address.
2174 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002176 // If we can fall off the end of the try block, unlink from the try
2177 // chain and set the state on the frame to FALLING.
2178 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002179 // The next handler address is on top of the frame.
2180 ASSERT(StackHandlerConstants::kNextOffset == 0);
2181 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002182 __ mov(r3, Operand(handler_address));
2183 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002184 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002185
2186 // Fake a top of stack value (unneeded when FALLING) and set the
2187 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002188 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002189 frame_->EmitPush(r0);
2190 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2191 if (nof_unlinks > 0) {
2192 finally_block.Jump();
2193 }
2194 }
2195
2196 // Generate code to unlink and set the state for the (formerly)
2197 // shadowing targets that have been jumped to.
2198 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002199 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002200 // If we have come from the shadowed return, the return value is
2201 // in (a non-refcounted reference to) r0. We must preserve it
2202 // until it is pushed.
2203 //
2204 // Because we can be jumping here (to spilled code) from
2205 // unspilled code, we need to reestablish a spilled frame at
2206 // this block.
2207 shadows[i]->Bind();
2208 frame_->SpillAll();
2209
2210 // Reload sp from the top handler, because some statements that
2211 // we break from (eg, for...in) may have left stuff on the
2212 // stack.
2213 __ mov(r3, Operand(handler_address));
2214 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002215 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002216
2217 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002218 // handler address is currently on top of the frame.
2219 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002220 frame_->EmitPop(r1);
2221 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002222 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002223
2224 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002225 // If this label shadowed the function return, materialize the
2226 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002227 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002228 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002229 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002230 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002231 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002232 }
2233 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002234 if (--nof_unlinks > 0) {
2235 // If this is not the last unlink block, jump around the next.
2236 finally_block.Jump();
2237 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002238 }
2239 }
2240
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002241 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002242 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002243
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002244 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002245 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002246
2247 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002248 // and the state - while evaluating the finally block.
2249 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002250 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002251 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002252
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002253 if (has_valid_frame()) {
2254 // Restore state and return value or faked TOS.
2255 frame_->EmitPop(r2);
2256 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002257 }
2258
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002259 // Generate code to jump to the right destination for all used
2260 // formerly shadowing targets. Deallocate each shadow target.
2261 for (int i = 0; i < shadows.length(); i++) {
2262 if (has_valid_frame() && shadows[i]->is_bound()) {
2263 JumpTarget* original = shadows[i]->other_target();
2264 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2265 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002266 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002267 skip.Branch(ne);
2268 frame_->PrepareForReturn();
2269 original->Jump();
2270 skip.Bind();
2271 } else {
2272 original->Branch(eq);
2273 }
2274 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002275 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002277 if (has_valid_frame()) {
2278 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002279 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002280 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2281 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002282
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002283 // Rethrow exception.
2284 frame_->EmitPush(r0);
2285 frame_->CallRuntime(Runtime::kReThrow, 1);
2286
2287 // Done.
2288 exit.Bind();
2289 }
2290 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002291}
2292
2293
ager@chromium.org7c537e22008-10-16 08:43:32 +00002294void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002295#ifdef DEBUG
2296 int original_height = frame_->height();
2297#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002298 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002299 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002300 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002301#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +00002302 frame_->DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002303#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002304 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002305 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002306}
2307
2308
ager@chromium.org7c537e22008-10-16 08:43:32 +00002309void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002310 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002311 ASSERT(boilerplate->IsBoilerplate());
2312
ager@chromium.org3811b432009-10-28 14:53:37 +00002313 __ mov(r0, Operand(boilerplate));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002314 // Use the fast case closure allocation code that allocates in new
2315 // space for nested functions that don't need literals cloning.
2316 if (scope()->is_function_scope() && boilerplate->NumberOfLiterals() == 0) {
2317 FastNewClosureStub stub;
2318 frame_->EmitPush(r0);
2319 frame_->CallStub(&stub, 1);
2320 frame_->EmitPush(r0);
2321 } else {
2322 // Create a new closure.
2323 frame_->EmitPush(cp);
2324 frame_->EmitPush(r0);
2325 frame_->CallRuntime(Runtime::kNewClosure, 2);
2326 frame_->EmitPush(r0);
2327 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002328}
2329
2330
ager@chromium.org7c537e22008-10-16 08:43:32 +00002331void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002332#ifdef DEBUG
2333 int original_height = frame_->height();
2334#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002335 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336 Comment cmnt(masm_, "[ FunctionLiteral");
2337
2338 // Build the function boilerplate and instantiate it.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002339 Handle<JSFunction> boilerplate =
ager@chromium.org5c838252010-02-19 08:53:10 +00002340 Compiler::BuildBoilerplate(node, script(), this);
kasper.lund212ac232008-07-16 07:07:30 +00002341 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002342 if (HasStackOverflow()) {
2343 ASSERT(frame_->height() == original_height);
2344 return;
2345 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002346 InstantiateBoilerplate(boilerplate);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002347 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002348}
2349
2350
ager@chromium.org7c537e22008-10-16 08:43:32 +00002351void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002352 FunctionBoilerplateLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002353#ifdef DEBUG
2354 int original_height = frame_->height();
2355#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002356 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002357 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2358 InstantiateBoilerplate(node->boilerplate());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002359 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002360}
2361
2362
ager@chromium.org7c537e22008-10-16 08:43:32 +00002363void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002364#ifdef DEBUG
2365 int original_height = frame_->height();
2366#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002367 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002368 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002369 JumpTarget then;
2370 JumpTarget else_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002371 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002372 if (has_valid_frame()) {
2373 Branch(false, &else_);
2374 }
2375 if (has_valid_frame() || then.is_linked()) {
2376 then.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002377 LoadAndSpill(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002378 }
2379 if (else_.is_linked()) {
2380 JumpTarget exit;
2381 if (has_valid_frame()) exit.Jump();
2382 else_.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002383 LoadAndSpill(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002384 if (exit.is_linked()) exit.Bind();
2385 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002386 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002387}
2388
2389
ager@chromium.org7c537e22008-10-16 08:43:32 +00002390void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002391 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002392 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002393 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002394
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002395 JumpTarget slow;
2396 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002397
2398 // Generate fast-case code for variables that might be shadowed by
2399 // eval-introduced variables. Eval is used a lot without
2400 // introducing variables. In those cases, we do not want to
2401 // perform a runtime call for all variables in the scope
2402 // containing the eval.
2403 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2404 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002405 // If there was no control flow to slow, we can exit early.
2406 if (!slow.is_linked()) {
2407 frame_->EmitPush(r0);
2408 return;
2409 }
2410
2411 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002412
2413 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2414 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2415 // Only generate the fast case for locals that rewrite to slots.
2416 // This rules out argument loads.
2417 if (potential_slot != NULL) {
2418 __ ldr(r0,
2419 ContextSlotOperandCheckExtensions(potential_slot,
2420 r1,
2421 r2,
2422 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002423 if (potential_slot->var()->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002424 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2425 __ cmp(r0, ip);
2426 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002427 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002428 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002429 // ContextSlotOperandCheckExtensions so we have to jump around
2430 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002431 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002432 }
2433 }
2434
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002435 slow.Bind();
2436 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002437 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002438 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002439
ager@chromium.org7c537e22008-10-16 08:43:32 +00002440 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002441 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002442 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002443 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002444 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002445
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002446 done.Bind();
2447 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002448
2449 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002450 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002451 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002452 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002453 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002454 // Const slots may contain 'the hole' value (the constant hasn't been
2455 // initialized yet) which needs to be converted into the 'undefined'
2456 // value.
2457 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002458 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002459 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2460 __ cmp(r0, ip);
2461 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002462 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002463 }
2464 }
2465}
2466
2467
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002468void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
2469 ASSERT(slot != NULL);
2470 if (slot->type() == Slot::LOOKUP) {
2471 ASSERT(slot->var()->is_dynamic());
2472
2473 // For now, just do a runtime call.
2474 frame_->EmitPush(cp);
2475 __ mov(r0, Operand(slot->var()->name()));
2476 frame_->EmitPush(r0);
2477
2478 if (init_state == CONST_INIT) {
2479 // Same as the case for a normal store, but ignores attribute
2480 // (e.g. READ_ONLY) of context slot so that we can initialize
2481 // const properties (introduced via eval("const foo = (some
2482 // expr);")). Also, uses the current function context instead of
2483 // the top context.
2484 //
2485 // Note that we must declare the foo upon entry of eval(), via a
2486 // context slot declaration, but we cannot initialize it at the
2487 // same time, because the const declaration may be at the end of
2488 // the eval code (sigh...) and the const variable may have been
2489 // used before (where its value is 'undefined'). Thus, we can only
2490 // do the initialization when we actually encounter the expression
2491 // and when the expression operands are defined and valid, and
2492 // thus we need the split into 2 operations: declaration of the
2493 // context slot followed by initialization.
2494 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
2495 } else {
2496 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
2497 }
2498 // Storing a variable must keep the (new) value on the expression
2499 // stack. This is necessary for compiling assignment expressions.
2500 frame_->EmitPush(r0);
2501
2502 } else {
2503 ASSERT(!slot->var()->is_dynamic());
2504
2505 JumpTarget exit;
2506 if (init_state == CONST_INIT) {
2507 ASSERT(slot->var()->mode() == Variable::CONST);
2508 // Only the first const initialization must be executed (the slot
2509 // still contains 'the hole' value). When the assignment is
2510 // executed, the code is identical to a normal store (see below).
2511 Comment cmnt(masm_, "[ Init const");
2512 __ ldr(r2, SlotOperand(slot, r2));
2513 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2514 __ cmp(r2, ip);
2515 exit.Branch(ne);
2516 }
2517
2518 // We must execute the store. Storing a variable must keep the
2519 // (new) value on the stack. This is necessary for compiling
2520 // assignment expressions.
2521 //
2522 // Note: We will reach here even with slot->var()->mode() ==
2523 // Variable::CONST because of const declarations which will
2524 // initialize consts to 'the hole' value and by doing so, end up
2525 // calling this code. r2 may be loaded with context; used below in
2526 // RecordWrite.
2527 frame_->EmitPop(r0);
2528 __ str(r0, SlotOperand(slot, r2));
2529 frame_->EmitPush(r0);
2530 if (slot->type() == Slot::CONTEXT) {
2531 // Skip write barrier if the written value is a smi.
2532 __ tst(r0, Operand(kSmiTagMask));
2533 exit.Branch(eq);
2534 // r2 is loaded with context when calling SlotOperand above.
2535 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
2536 __ mov(r3, Operand(offset));
2537 __ RecordWrite(r2, r3, r1);
2538 }
2539 // If we definitely did not jump over the assignment, we do not need
2540 // to bind the exit label. Doing so can defeat peephole
2541 // optimization.
2542 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
2543 exit.Bind();
2544 }
2545 }
2546}
2547
2548
ager@chromium.org381abbb2009-02-25 13:23:22 +00002549void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2550 TypeofState typeof_state,
2551 Register tmp,
2552 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002553 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002554 // Check that no extension objects have been created by calls to
2555 // eval from the current scope to the global scope.
2556 Register context = cp;
2557 Scope* s = scope();
2558 while (s != NULL) {
2559 if (s->num_heap_slots() > 0) {
2560 if (s->calls_eval()) {
2561 // Check that extension is NULL.
2562 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2563 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002564 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002565 }
2566 // Load next context in chain.
2567 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2568 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2569 context = tmp;
2570 }
2571 // If no outer scope calls eval, we do not need to check more
2572 // context extensions.
2573 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2574 s = s->outer_scope();
2575 }
2576
2577 if (s->is_eval_scope()) {
2578 Label next, fast;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002579 if (!context.is(tmp)) {
2580 __ mov(tmp, Operand(context));
2581 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002582 __ bind(&next);
2583 // Terminate at global context.
2584 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002585 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
2586 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002587 __ b(eq, &fast);
2588 // Check that extension is NULL.
2589 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2590 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002591 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002592 // Load next context in chain.
2593 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2594 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2595 __ b(&next);
2596 __ bind(&fast);
2597 }
2598
2599 // All extension objects were empty and it is safe to use a global
2600 // load IC call.
2601 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2602 // Load the global object.
2603 LoadGlobal();
2604 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002605 __ mov(r2, Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002606 // Call IC stub.
2607 if (typeof_state == INSIDE_TYPEOF) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002608 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002609 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002610 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002611 }
2612
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002613 // Drop the global object. The result is in r0.
2614 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002615}
2616
2617
ager@chromium.org7c537e22008-10-16 08:43:32 +00002618void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002619#ifdef DEBUG
2620 int original_height = frame_->height();
2621#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002622 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002623 Comment cmnt(masm_, "[ Slot");
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002624 LoadFromSlot(node, NOT_INSIDE_TYPEOF);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002625 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002626}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002627
ager@chromium.org7c537e22008-10-16 08:43:32 +00002628
2629void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002630#ifdef DEBUG
2631 int original_height = frame_->height();
2632#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002633 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002634 Comment cmnt(masm_, "[ VariableProxy");
2635
2636 Variable* var = node->var();
2637 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002638 if (expr != NULL) {
2639 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002640 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002641 ASSERT(var->is_global());
2642 Reference ref(this, node);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002643 ref.GetValueAndSpill();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002644 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002645 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002646}
2647
2648
ager@chromium.org7c537e22008-10-16 08:43:32 +00002649void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002650#ifdef DEBUG
2651 int original_height = frame_->height();
2652#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002653 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002654 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002655 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002656 frame_->EmitPush(r0);
2657 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002658}
2659
2660
ager@chromium.org7c537e22008-10-16 08:43:32 +00002661void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002662#ifdef DEBUG
2663 int original_height = frame_->height();
2664#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002665 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002666 Comment cmnt(masm_, "[ RexExp Literal");
2667
2668 // Retrieve the literal array and check the allocated entry.
2669
2670 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002671 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002672
2673 // Load the literals array of the function.
2674 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2675
2676 // Load the literal at the ast saved index.
2677 int literal_offset =
2678 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2679 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2680
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002681 JumpTarget done;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002682 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2683 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002684 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002685
2686 // If the entry is undefined we call the runtime system to computed
2687 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002688 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002689 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002690 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002691 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002692 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002693 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002694 frame_->EmitPush(r0);
2695 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002696 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002697
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002698 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002700 frame_->EmitPush(r2);
2701 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002702}
2703
2704
ager@chromium.org7c537e22008-10-16 08:43:32 +00002705void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002706#ifdef DEBUG
2707 int original_height = frame_->height();
2708#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002709 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002710 Comment cmnt(masm_, "[ ObjectLiteral");
2711
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002712 // Load the function of this activation.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002713 __ ldr(r2, frame_->Function());
2714 // Literal array.
2715 __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset));
2716 // Literal index.
2717 __ mov(r1, Operand(Smi::FromInt(node->literal_index())));
2718 // Constant properties.
2719 __ mov(r0, Operand(node->constant_properties()));
2720 frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit());
2721 if (node->depth() > 1) {
2722 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 3);
2723 } else {
2724 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002725 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002726 frame_->EmitPush(r0); // save the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002727 for (int i = 0; i < node->properties()->length(); i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002728 // At the start of each iteration, the top of stack contains
2729 // the newly created object literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002730 ObjectLiteral::Property* property = node->properties()->at(i);
2731 Literal* key = property->key();
2732 Expression* value = property->value();
2733 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002734 case ObjectLiteral::Property::CONSTANT:
2735 break;
2736 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2737 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
2738 // else fall through
ager@chromium.org5c838252010-02-19 08:53:10 +00002739 case ObjectLiteral::Property::COMPUTED:
2740 if (key->handle()->IsSymbol()) {
2741 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
2742 LoadAndSpill(value);
2743 frame_->EmitPop(r0);
2744 __ mov(r2, Operand(key->handle()));
2745 __ ldr(r1, frame_->Top()); // Load the receiver.
2746 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
2747 break;
2748 }
2749 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002750 case ObjectLiteral::Property::PROTOTYPE: {
ager@chromium.org5c838252010-02-19 08:53:10 +00002751 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002752 frame_->EmitPush(r0); // dup the result
2753 LoadAndSpill(key);
2754 LoadAndSpill(value);
2755 frame_->CallRuntime(Runtime::kSetProperty, 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002756 break;
2757 }
2758 case ObjectLiteral::Property::SETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00002759 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002760 frame_->EmitPush(r0);
2761 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002762 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002763 frame_->EmitPush(r0);
2764 LoadAndSpill(value);
2765 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002766 break;
2767 }
2768 case ObjectLiteral::Property::GETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00002769 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002770 frame_->EmitPush(r0);
2771 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002772 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002773 frame_->EmitPush(r0);
2774 LoadAndSpill(value);
2775 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002776 break;
2777 }
2778 }
2779 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002780 ASSERT(frame_->height() == original_height + 1);
2781}
2782
2783
ager@chromium.org7c537e22008-10-16 08:43:32 +00002784void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002785#ifdef DEBUG
2786 int original_height = frame_->height();
2787#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002788 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002789 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002790
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002791 // Load the function of this activation.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002792 __ ldr(r2, frame_->Function());
ager@chromium.org5c838252010-02-19 08:53:10 +00002793 // Load the literals array of the function.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002794 __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002795 __ mov(r1, Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002796 __ mov(r0, Operand(node->constant_elements()));
2797 frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit());
ager@chromium.org5c838252010-02-19 08:53:10 +00002798 int length = node->values()->length();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002799 if (node->depth() > 1) {
2800 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00002801 } else if (length > FastCloneShallowArrayStub::kMaximumLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002802 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00002803 } else {
2804 FastCloneShallowArrayStub stub(length);
2805 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002806 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002807 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002808 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002809
2810 // Generate code to set the elements in the array that are not
2811 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002812 for (int i = 0; i < node->values()->length(); i++) {
2813 Expression* value = node->values()->at(i);
2814
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002815 // If value is a literal the property value is already set in the
2816 // boilerplate object.
2817 if (value->AsLiteral() != NULL) continue;
2818 // If value is a materialized literal the property value is already set
2819 // in the boilerplate object if it is simple.
2820 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002821
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002822 // The property must be set by generated code.
2823 LoadAndSpill(value);
2824 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002825
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002826 // Fetch the object literal.
2827 __ ldr(r1, frame_->Top());
2828 // Get the elements array.
2829 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002830
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002831 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002832 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002833 __ str(r0, FieldMemOperand(r1, offset));
2834
2835 // Update the write barrier for the array address.
2836 __ mov(r3, Operand(offset));
2837 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002838 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002839 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002840}
2841
2842
ager@chromium.org32912102009-01-16 10:38:43 +00002843void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002844#ifdef DEBUG
2845 int original_height = frame_->height();
2846#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002847 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org32912102009-01-16 10:38:43 +00002848 // Call runtime routine to allocate the catch extension object and
2849 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002850 Comment cmnt(masm_, "[ CatchExtensionObject");
2851 LoadAndSpill(node->key());
2852 LoadAndSpill(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002853 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2854 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002855 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002856}
2857
2858
ager@chromium.org7c537e22008-10-16 08:43:32 +00002859void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002860#ifdef DEBUG
2861 int original_height = frame_->height();
2862#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002863 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002864 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00002865
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002866 { Reference target(this, node->target(), node->is_compound());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002867 if (target.is_illegal()) {
2868 // Fool the virtual frame into thinking that we left the assignment's
2869 // value on the frame.
2870 __ mov(r0, Operand(Smi::FromInt(0)));
2871 frame_->EmitPush(r0);
2872 ASSERT(frame_->height() == original_height + 1);
2873 return;
2874 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002875
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002876 if (node->op() == Token::ASSIGN ||
2877 node->op() == Token::INIT_VAR ||
2878 node->op() == Token::INIT_CONST) {
2879 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002880
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002881 } else { // Assignment is a compound assignment.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002882 // Get the old value of the lhs.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002883 target.GetValueAndSpill();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002884 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002885 bool overwrite =
2886 (node->value()->AsBinaryOperation() != NULL &&
2887 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002888 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002889 SmiOperation(node->binary_op(),
2890 literal->handle(),
2891 false,
2892 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002893 frame_->EmitPush(r0);
2894
2895 } else {
2896 LoadAndSpill(node->value());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002897 GenericBinaryOperation(node->binary_op(),
2898 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002899 frame_->EmitPush(r0);
2900 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002901 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002902 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2903 if (var != NULL &&
2904 (var->mode() == Variable::CONST) &&
2905 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2906 // Assignment ignored - leave the value on the stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002907 UnloadReference(&target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002908 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002909 CodeForSourcePosition(node->position());
2910 if (node->op() == Token::INIT_CONST) {
2911 // Dynamic constant initializations must use the function context
2912 // and initialize the actual constant declared. Dynamic variable
2913 // initializations are simply assignments and use SetValue.
2914 target.SetValue(CONST_INIT);
2915 } else {
2916 target.SetValue(NOT_CONST_INIT);
2917 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002918 }
2919 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002920 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002921}
2922
2923
ager@chromium.org7c537e22008-10-16 08:43:32 +00002924void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002925#ifdef DEBUG
2926 int original_height = frame_->height();
2927#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002928 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002929 Comment cmnt(masm_, "[ Throw");
2930
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002931 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002932 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002933 frame_->CallRuntime(Runtime::kThrow, 1);
2934 frame_->EmitPush(r0);
2935 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002936}
2937
2938
ager@chromium.org7c537e22008-10-16 08:43:32 +00002939void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002940#ifdef DEBUG
2941 int original_height = frame_->height();
2942#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002943 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002944 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002945
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002946 { Reference property(this, node);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002947 property.GetValueAndSpill();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002948 }
2949 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002950}
2951
2952
ager@chromium.org7c537e22008-10-16 08:43:32 +00002953void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002954#ifdef DEBUG
2955 int original_height = frame_->height();
2956#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002957 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958 Comment cmnt(masm_, "[ Call");
2959
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002960 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002961 ZoneList<Expression*>* args = node->arguments();
2962
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002963 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002964 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002965 Variable* var = function->AsVariableProxy()->AsVariable();
2966 Property* property = function->AsProperty();
2967
2968 // ------------------------------------------------------------------------
2969 // Fast-case: Use inline caching.
2970 // ---
2971 // According to ECMA-262, section 11.2.3, page 44, the function to call
2972 // must be resolved after the arguments have been evaluated. The IC code
2973 // automatically handles this by loading the arguments before the function
2974 // is resolved in cache misses (this also holds for megamorphic calls).
2975 // ------------------------------------------------------------------------
2976
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002977 if (var != NULL && var->is_possibly_eval()) {
2978 // ----------------------------------
2979 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
2980 // ----------------------------------
2981
2982 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2983 // resolve the function we need to call and the receiver of the
2984 // call. Then we call the resolved function using the given
2985 // arguments.
2986 // Prepare stack for call to resolved function.
2987 LoadAndSpill(function);
2988 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
2989 frame_->EmitPush(r2); // Slot for receiver
2990 int arg_count = args->length();
2991 for (int i = 0; i < arg_count; i++) {
2992 LoadAndSpill(args->at(i));
2993 }
2994
2995 // Prepare stack for call to ResolvePossiblyDirectEval.
2996 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
2997 frame_->EmitPush(r1);
2998 if (arg_count > 0) {
2999 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3000 frame_->EmitPush(r1);
3001 } else {
3002 frame_->EmitPush(r2);
3003 }
3004
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003005 // Push the receiver.
3006 __ ldr(r1, frame_->Receiver());
3007 frame_->EmitPush(r1);
3008
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003009 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003010 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003011
3012 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003013 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003014 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3015
3016 // Call the function.
3017 CodeForSourcePosition(node->position());
3018
3019 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003020 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003021 frame_->CallStub(&call_function, arg_count + 1);
3022
3023 __ ldr(cp, frame_->Context());
3024 // Remove the function from the stack.
3025 frame_->Drop();
3026 frame_->EmitPush(r0);
3027
3028 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003029 // ----------------------------------
3030 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3031 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003032 // Pass the global object as the receiver and let the IC stub
3033 // patch the stack to use the global proxy as 'this' in the
3034 // invoked function.
3035 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003036
3037 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003038 int arg_count = args->length();
3039 for (int i = 0; i < arg_count; i++) {
3040 LoadAndSpill(args->at(i));
3041 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003042
ager@chromium.org5c838252010-02-19 08:53:10 +00003043 // Setup the name register and call the IC initialization code.
3044 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003045 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3046 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003047 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003048 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3049 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003050 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003051 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003052
3053 } else if (var != NULL && var->slot() != NULL &&
3054 var->slot()->type() == Slot::LOOKUP) {
3055 // ----------------------------------
3056 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3057 // ----------------------------------
3058
3059 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003060 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003061 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003062 frame_->EmitPush(r0);
3063 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003064 // r0: slot value; r1: receiver
3065
3066 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003067 frame_->EmitPush(r0); // function
3068 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003069
3070 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003071 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003072 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003073
3074 } else if (property != NULL) {
3075 // Check if the key is a literal string.
3076 Literal* literal = property->key()->AsLiteral();
3077
3078 if (literal != NULL && literal->handle()->IsSymbol()) {
3079 // ------------------------------------------------------------------
3080 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3081 // ------------------------------------------------------------------
3082
ager@chromium.org5c838252010-02-19 08:53:10 +00003083 LoadAndSpill(property->obj()); // Receiver.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003084 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003085 int arg_count = args->length();
3086 for (int i = 0; i < arg_count; i++) {
3087 LoadAndSpill(args->at(i));
3088 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003089
ager@chromium.org5c838252010-02-19 08:53:10 +00003090 // Set the name register and call the IC initialization code.
3091 __ mov(r2, Operand(literal->handle()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003092 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3093 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003094 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003095 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003096 __ ldr(cp, frame_->Context());
ager@chromium.org5c838252010-02-19 08:53:10 +00003097 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003098
3099 } else {
3100 // -------------------------------------------
3101 // JavaScript example: 'array[index](1, 2, 3)'
3102 // -------------------------------------------
3103
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003104 LoadAndSpill(property->obj());
3105 LoadAndSpill(property->key());
3106 EmitKeyedLoad(false);
3107 frame_->Drop(); // key
3108 // Put the function below the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003109 if (property->is_synthetic()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003110 // Use the global receiver.
3111 frame_->Drop();
3112 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003113 LoadGlobalReceiver(r0);
3114 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003115 frame_->EmitPop(r1); // receiver
3116 frame_->EmitPush(r0); // function
3117 frame_->EmitPush(r1); // receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003118 }
3119
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003120 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003121 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003122 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003123 }
3124
3125 } else {
3126 // ----------------------------------
3127 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3128 // ----------------------------------
3129
3130 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003131 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003132
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003133 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003134 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003135
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003136 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003137 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003138 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003139 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003140 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003141}
3142
3143
ager@chromium.org7c537e22008-10-16 08:43:32 +00003144void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003145#ifdef DEBUG
3146 int original_height = frame_->height();
3147#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003148 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003149 Comment cmnt(masm_, "[ CallNew");
3150
3151 // According to ECMA-262, section 11.2.2, page 44, the function
3152 // expression in new calls must be evaluated before the
3153 // arguments. This is different from ordinary calls, where the
3154 // actual function to call is resolved after the arguments have been
3155 // evaluated.
3156
3157 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003158 // receiver. There is no need to use the global proxy here because
3159 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003160 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003161 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003162
3163 // Push the arguments ("left-to-right") on the stack.
3164 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003165 int arg_count = args->length();
3166 for (int i = 0; i < arg_count; i++) {
3167 LoadAndSpill(args->at(i));
3168 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003169
mads.s.ager31e71382008-08-13 09:32:07 +00003170 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003171 __ mov(r0, Operand(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003172 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003173 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003174
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003175 // Call the construct call builtin that handles allocation and
3176 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003177 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003178 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003179 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003180
3181 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003182 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003183 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003184}
3185
3186
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003187void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
3188 VirtualFrame::SpilledScope spilled_scope;
3189 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003190 JumpTarget leave, null, function, non_function_constructor;
3191
3192 // Load the object into r0.
3193 LoadAndSpill(args->at(0));
3194 frame_->EmitPop(r0);
3195
3196 // If the object is a smi, we return null.
3197 __ tst(r0, Operand(kSmiTagMask));
3198 null.Branch(eq);
3199
3200 // Check that the object is a JS object but take special care of JS
3201 // functions to make sure they have 'Function' as their class.
3202 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
3203 null.Branch(lt);
3204
3205 // As long as JS_FUNCTION_TYPE is the last instance type and it is
3206 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
3207 // LAST_JS_OBJECT_TYPE.
3208 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3209 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
3210 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3211 function.Branch(eq);
3212
3213 // Check if the constructor in the map is a function.
3214 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
3215 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
3216 non_function_constructor.Branch(ne);
3217
3218 // The r0 register now contains the constructor function. Grab the
3219 // instance class name from there.
3220 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
3221 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003222 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003223 leave.Jump();
3224
3225 // Functions have class 'Function'.
3226 function.Bind();
3227 __ mov(r0, Operand(Factory::function_class_symbol()));
3228 frame_->EmitPush(r0);
3229 leave.Jump();
3230
3231 // Objects with a non-function constructor have class 'Object'.
3232 non_function_constructor.Bind();
3233 __ mov(r0, Operand(Factory::Object_symbol()));
3234 frame_->EmitPush(r0);
3235 leave.Jump();
3236
3237 // Non-JS objects have class null.
3238 null.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003239 __ LoadRoot(r0, Heap::kNullValueRootIndex);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003240 frame_->EmitPush(r0);
3241
3242 // All done.
3243 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003244}
3245
3246
ager@chromium.org7c537e22008-10-16 08:43:32 +00003247void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003248 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003249 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003250 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003251 LoadAndSpill(args->at(0));
3252 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003253 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003254 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003255 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003256 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3257 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003258 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003259 // Load the value.
3260 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003261 leave.Bind();
3262 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003263}
3264
3265
ager@chromium.org7c537e22008-10-16 08:43:32 +00003266void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003267 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003268 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003269 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003270 LoadAndSpill(args->at(0)); // Load the object.
3271 LoadAndSpill(args->at(1)); // Load the value.
3272 frame_->EmitPop(r0); // r0 contains value
3273 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003274 // if (object->IsSmi()) return object.
3275 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003276 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003277 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3278 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003279 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003280 // Store the value.
3281 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3282 // Update the write barrier.
3283 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3284 __ RecordWrite(r1, r2, r3);
3285 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003286 leave.Bind();
3287 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003288}
3289
3290
ager@chromium.org7c537e22008-10-16 08:43:32 +00003291void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003292 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003293 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003294 LoadAndSpill(args->at(0));
3295 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003296 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003297 cc_reg_ = eq;
3298}
3299
3300
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003301void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003302 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003303 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3304 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003305#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003306 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003307 LoadAndSpill(args->at(1));
3308 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003309 __ CallRuntime(Runtime::kLog, 2);
3310 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003311#endif
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003312 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003313 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003314}
3315
3316
ager@chromium.org7c537e22008-10-16 08:43:32 +00003317void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003318 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003319 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003320 LoadAndSpill(args->at(0));
3321 frame_->EmitPop(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003322 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003323 cc_reg_ = eq;
3324}
3325
3326
kasper.lund7276f142008-07-30 08:49:36 +00003327// This should generate code that performs a charCodeAt() call or returns
3328// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3329// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003330void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003331 VirtualFrame::SpilledScope spilled_scope;
kasper.lund7276f142008-07-30 08:49:36 +00003332 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003333 Comment(masm_, "[ GenerateFastCharCodeAt");
3334
3335 LoadAndSpill(args->at(0));
3336 LoadAndSpill(args->at(1));
3337 frame_->EmitPop(r0); // Index.
3338 frame_->EmitPop(r1); // String.
3339
3340 Label slow, end, not_a_flat_string, ascii_string, try_again_with_new_string;
3341
3342 __ tst(r1, Operand(kSmiTagMask));
3343 __ b(eq, &slow); // The 'string' was a Smi.
3344
3345 ASSERT(kSmiTag == 0);
3346 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
3347 __ b(ne, &slow); // The index was negative or not a Smi.
3348
3349 __ bind(&try_again_with_new_string);
3350 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
3351 __ b(ge, &slow);
3352
3353 // Now r2 has the string type.
3354 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003355 // Now r3 has the length of the string. Compare with the index.
3356 __ cmp(r3, Operand(r0, LSR, kSmiTagSize));
3357 __ b(le, &slow);
3358
3359 // Here we know the index is in range. Check that string is sequential.
3360 ASSERT_EQ(0, kSeqStringTag);
3361 __ tst(r2, Operand(kStringRepresentationMask));
3362 __ b(ne, &not_a_flat_string);
3363
3364 // Check whether it is an ASCII string.
3365 ASSERT_EQ(0, kTwoByteStringTag);
3366 __ tst(r2, Operand(kStringEncodingMask));
3367 __ b(ne, &ascii_string);
3368
3369 // 2-byte string. We can add without shifting since the Smi tag size is the
3370 // log2 of the number of bytes in a two-byte character.
3371 ASSERT_EQ(1, kSmiTagSize);
3372 ASSERT_EQ(0, kSmiShiftSize);
3373 __ add(r1, r1, Operand(r0));
3374 __ ldrh(r0, FieldMemOperand(r1, SeqTwoByteString::kHeaderSize));
3375 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
3376 __ jmp(&end);
3377
3378 __ bind(&ascii_string);
3379 __ add(r1, r1, Operand(r0, LSR, kSmiTagSize));
3380 __ ldrb(r0, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
3381 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
3382 __ jmp(&end);
3383
3384 __ bind(&not_a_flat_string);
3385 __ and_(r2, r2, Operand(kStringRepresentationMask));
3386 __ cmp(r2, Operand(kConsStringTag));
3387 __ b(ne, &slow);
3388
3389 // ConsString.
3390 // Check that the right hand side is the empty string (ie if this is really a
3391 // flat string in a cons string). If that is not the case we would rather go
3392 // to the runtime system now, to flatten the string.
3393 __ ldr(r2, FieldMemOperand(r1, ConsString::kSecondOffset));
3394 __ LoadRoot(r3, Heap::kEmptyStringRootIndex);
3395 __ cmp(r2, Operand(r3));
3396 __ b(ne, &slow);
3397
3398 // Get the first of the two strings.
3399 __ ldr(r1, FieldMemOperand(r1, ConsString::kFirstOffset));
3400 __ jmp(&try_again_with_new_string);
3401
3402 __ bind(&slow);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003403 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003404
3405 __ bind(&end);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003406 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003407}
3408
3409
ager@chromium.org7c537e22008-10-16 08:43:32 +00003410void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003411 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003412 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003413 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003414 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003415 // We need the CC bits to come out as not_equal in the case where the
3416 // object is a smi. This can't be done with the usual test opcode so
3417 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003418 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003419 __ and_(r1, r0, Operand(kSmiTagMask));
3420 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003421 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003422 // It is a heap object - get the map. Check if the object is a JS array.
3423 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003424 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003425 cc_reg_ = eq;
3426}
3427
3428
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00003429void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
3430 VirtualFrame::SpilledScope spilled_scope;
3431 ASSERT(args->length() == 1);
3432 LoadAndSpill(args->at(0));
3433 JumpTarget answer;
3434 // We need the CC bits to come out as not_equal in the case where the
3435 // object is a smi. This can't be done with the usual test opcode so
3436 // we use XOR to get the right CC bits.
3437 frame_->EmitPop(r0);
3438 __ and_(r1, r0, Operand(kSmiTagMask));
3439 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
3440 answer.Branch(ne);
3441 // It is a heap object - get the map. Check if the object is a regexp.
3442 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
3443 answer.Bind();
3444 cc_reg_ = eq;
3445}
3446
3447
ager@chromium.org6141cbe2009-11-20 12:14:52 +00003448void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
3449 // This generates a fast version of:
3450 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
3451 VirtualFrame::SpilledScope spilled_scope;
3452 ASSERT(args->length() == 1);
3453 LoadAndSpill(args->at(0));
3454 frame_->EmitPop(r1);
3455 __ tst(r1, Operand(kSmiTagMask));
3456 false_target()->Branch(eq);
3457
3458 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3459 __ cmp(r1, ip);
3460 true_target()->Branch(eq);
3461
3462 Register map_reg = r2;
3463 __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset));
3464 // Undetectable objects behave like undefined when tested with typeof.
3465 __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset));
3466 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3467 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3468 false_target()->Branch(eq);
3469
3470 __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
3471 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
3472 false_target()->Branch(lt);
3473 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
3474 cc_reg_ = le;
3475}
3476
3477
3478void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
3479 // This generates a fast version of:
3480 // (%_ClassOf(arg) === 'Function')
3481 VirtualFrame::SpilledScope spilled_scope;
3482 ASSERT(args->length() == 1);
3483 LoadAndSpill(args->at(0));
3484 frame_->EmitPop(r0);
3485 __ tst(r0, Operand(kSmiTagMask));
3486 false_target()->Branch(eq);
3487 Register map_reg = r2;
3488 __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE);
3489 cc_reg_ = eq;
3490}
3491
3492
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003493void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
3494 VirtualFrame::SpilledScope spilled_scope;
3495 ASSERT(args->length() == 1);
3496 LoadAndSpill(args->at(0));
3497 frame_->EmitPop(r0);
3498 __ tst(r0, Operand(kSmiTagMask));
3499 false_target()->Branch(eq);
3500 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3501 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
3502 __ tst(r1, Operand(1 << Map::kIsUndetectable));
3503 cc_reg_ = ne;
3504}
3505
3506
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003507void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
3508 VirtualFrame::SpilledScope spilled_scope;
3509 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003510
3511 // Get the frame pointer for the calling frame.
3512 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3513
3514 // Skip the arguments adaptor frame if it exists.
3515 Label check_frame_marker;
3516 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003517 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003518 __ b(ne, &check_frame_marker);
3519 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
3520
3521 // Check the marker in the calling frame.
3522 __ bind(&check_frame_marker);
3523 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
3524 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
3525 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003526}
3527
3528
ager@chromium.org7c537e22008-10-16 08:43:32 +00003529void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003530 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003531 ASSERT(args->length() == 0);
3532
mads.s.ager31e71382008-08-13 09:32:07 +00003533 // Seed the result with the formal parameters count, which will be used
3534 // in case no arguments adaptor frame is found below the current frame.
ager@chromium.org5c838252010-02-19 08:53:10 +00003535 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003536
3537 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003538 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003539 frame_->CallStub(&stub, 0);
3540 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003541}
3542
3543
ager@chromium.org7c537e22008-10-16 08:43:32 +00003544void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003545 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003546 ASSERT(args->length() == 1);
3547
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003548 // Satisfy contract with ArgumentsAccessStub:
3549 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003550 LoadAndSpill(args->at(0));
3551 frame_->EmitPop(r1);
ager@chromium.org5c838252010-02-19 08:53:10 +00003552 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003553
3554 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003555 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003556 frame_->CallStub(&stub, 0);
3557 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003558}
3559
3560
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003561void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) {
3562 VirtualFrame::SpilledScope spilled_scope;
3563 ASSERT(args->length() == 0);
3564 __ Call(ExternalReference::random_positive_smi_function().address(),
3565 RelocInfo::RUNTIME_ENTRY);
3566 frame_->EmitPush(r0);
3567}
3568
3569
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003570void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
3571 ASSERT_EQ(2, args->length());
3572
3573 Load(args->at(0));
3574 Load(args->at(1));
3575
ager@chromium.org5c838252010-02-19 08:53:10 +00003576 StringAddStub stub(NO_STRING_ADD_FLAGS);
3577 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003578 frame_->EmitPush(r0);
3579}
3580
3581
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003582void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
3583 ASSERT_EQ(3, args->length());
3584
3585 Load(args->at(0));
3586 Load(args->at(1));
3587 Load(args->at(2));
3588
ager@chromium.org5c838252010-02-19 08:53:10 +00003589 SubStringStub stub;
3590 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003591 frame_->EmitPush(r0);
3592}
3593
3594
3595void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
3596 ASSERT_EQ(2, args->length());
3597
3598 Load(args->at(0));
3599 Load(args->at(1));
3600
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003601 StringCompareStub stub;
3602 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003603 frame_->EmitPush(r0);
3604}
3605
3606
3607void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
3608 ASSERT_EQ(4, args->length());
3609
3610 Load(args->at(0));
3611 Load(args->at(1));
3612 Load(args->at(2));
3613 Load(args->at(3));
3614
3615 frame_->CallRuntime(Runtime::kRegExpExec, 4);
3616 frame_->EmitPush(r0);
3617}
3618
3619
ager@chromium.org5c838252010-02-19 08:53:10 +00003620void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
3621 ASSERT_EQ(args->length(), 1);
3622
3623 // Load the argument on the stack and jump to the runtime.
3624 Load(args->at(0));
3625
3626 frame_->CallRuntime(Runtime::kNumberToString, 1);
3627 frame_->EmitPush(r0);
3628}
3629
3630
ager@chromium.org7c537e22008-10-16 08:43:32 +00003631void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003632 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003633 ASSERT(args->length() == 2);
3634
3635 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003636 LoadAndSpill(args->at(0));
3637 LoadAndSpill(args->at(1));
3638 frame_->EmitPop(r0);
3639 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003640 __ cmp(r0, Operand(r1));
3641 cc_reg_ = eq;
3642}
3643
3644
ager@chromium.org7c537e22008-10-16 08:43:32 +00003645void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003646#ifdef DEBUG
3647 int original_height = frame_->height();
3648#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003649 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003650 if (CheckForInlineRuntimeCall(node)) {
3651 ASSERT((has_cc() && frame_->height() == original_height) ||
3652 (!has_cc() && frame_->height() == original_height + 1));
3653 return;
3654 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003655
3656 ZoneList<Expression*>* args = node->arguments();
3657 Comment cmnt(masm_, "[ CallRuntime");
3658 Runtime::Function* function = node->function();
3659
ager@chromium.org41826e72009-03-30 13:30:57 +00003660 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003661 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00003662 // Push the builtins object found in the current global object.
3663 __ ldr(r1, GlobalObject());
3664 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003665 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003666 }
mads.s.ager31e71382008-08-13 09:32:07 +00003667
ager@chromium.org41826e72009-03-30 13:30:57 +00003668 // Push the arguments ("left-to-right").
3669 int arg_count = args->length();
3670 for (int i = 0; i < arg_count; i++) {
3671 LoadAndSpill(args->at(i));
3672 }
mads.s.ager31e71382008-08-13 09:32:07 +00003673
ager@chromium.org41826e72009-03-30 13:30:57 +00003674 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003675 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00003676 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003677 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3678 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003679 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003680 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003681 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003682 } else {
3683 // Call the C runtime function.
3684 frame_->CallRuntime(function, arg_count);
3685 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003686 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003687 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003688}
3689
3690
ager@chromium.org7c537e22008-10-16 08:43:32 +00003691void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003692#ifdef DEBUG
3693 int original_height = frame_->height();
3694#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003695 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003696 Comment cmnt(masm_, "[ UnaryOperation");
3697
3698 Token::Value op = node->op();
3699
3700 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003701 LoadConditionAndSpill(node->expression(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003702 false_target(),
3703 true_target(),
3704 true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003705 // LoadCondition may (and usually does) leave a test and branch to
3706 // be emitted by the caller. In that case, negate the condition.
3707 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003708
3709 } else if (op == Token::DELETE) {
3710 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003711 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003712 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003713 LoadAndSpill(property->obj());
3714 LoadAndSpill(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003715 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003716
mads.s.ager31e71382008-08-13 09:32:07 +00003717 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003718 Slot* slot = variable->slot();
3719 if (variable->is_global()) {
3720 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003721 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003722 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003723 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003724
3725 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3726 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003727 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003728 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003729 frame_->EmitPush(r0);
3730 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003731 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003732 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003733 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003734 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003735 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003736
mads.s.ager31e71382008-08-13 09:32:07 +00003737 } else {
3738 // Default: Result of deleting non-global, not dynamically
3739 // introduced variables is false.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003740 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00003741 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003742
3743 } else {
3744 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003745 LoadAndSpill(node->expression()); // may have side-effects
3746 frame_->Drop();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003747 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003748 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003749 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003750
3751 } else if (op == Token::TYPEOF) {
3752 // Special case for loading the typeof expression; see comment on
3753 // LoadTypeofExpression().
3754 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003755 frame_->CallRuntime(Runtime::kTypeof, 1);
3756 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003757
3758 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003759 bool overwrite =
3760 (node->expression()->AsBinaryOperation() != NULL &&
3761 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003762 LoadAndSpill(node->expression());
3763 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003764 switch (op) {
3765 case Token::NOT:
3766 case Token::DELETE:
3767 case Token::TYPEOF:
3768 UNREACHABLE(); // handled above
3769 break;
3770
3771 case Token::SUB: {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003772 GenericUnaryOpStub stub(Token::SUB, overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003773 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003774 break;
3775 }
3776
3777 case Token::BIT_NOT: {
3778 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003779 JumpTarget smi_label;
3780 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003781 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003782 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003783
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003784 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
3785 frame_->CallStub(&stub, 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003786 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003787
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003788 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003789 __ mvn(r0, Operand(r0));
3790 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003791 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003792 break;
3793 }
3794
3795 case Token::VOID:
3796 // since the stack top is cached in r0, popping and then
3797 // pushing a value can be done by just writing to r0.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003798 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003799 break;
3800
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003801 case Token::ADD: {
3802 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003803 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003804 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003805 continue_label.Branch(eq);
3806 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003807 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003808 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003809 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003810 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003811 default:
3812 UNREACHABLE();
3813 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003814 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003815 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003816 ASSERT(!has_valid_frame() ||
3817 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003818 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003819}
3820
3821
ager@chromium.org7c537e22008-10-16 08:43:32 +00003822void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003823#ifdef DEBUG
3824 int original_height = frame_->height();
3825#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003826 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003827 Comment cmnt(masm_, "[ CountOperation");
3828
3829 bool is_postfix = node->is_postfix();
3830 bool is_increment = node->op() == Token::INC;
3831
3832 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3833 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3834
3835 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003836 if (is_postfix) {
3837 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003838 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003839 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003840
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003841 // A constant reference is not saved to, so a constant reference is not a
3842 // compound assignment reference.
3843 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003844 if (target.is_illegal()) {
3845 // Spoof the virtual frame to have the expected height (one higher
3846 // than on entry).
3847 if (!is_postfix) {
3848 __ mov(r0, Operand(Smi::FromInt(0)));
3849 frame_->EmitPush(r0);
3850 }
3851 ASSERT(frame_->height() == original_height + 1);
3852 return;
3853 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003854 target.GetValueAndSpill();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003855 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003856
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003857 JumpTarget slow;
3858 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003859
3860 // Load the value (1) into register r1.
3861 __ mov(r1, Operand(Smi::FromInt(1)));
3862
3863 // Check for smi operand.
3864 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003865 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003866
3867 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003868 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003869 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003870 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003871
3872 // Perform optimistic increment/decrement.
3873 if (is_increment) {
3874 __ add(r0, r0, Operand(r1), SetCC);
3875 } else {
3876 __ sub(r0, r0, Operand(r1), SetCC);
3877 }
3878
3879 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003880 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003881
3882 // Revert optimistic increment/decrement.
3883 if (is_increment) {
3884 __ sub(r0, r0, Operand(r1));
3885 } else {
3886 __ add(r0, r0, Operand(r1));
3887 }
3888
3889 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003890 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003891 {
3892 // Convert the operand to a number.
3893 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003894 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003895 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003896 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003897 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003898 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003899 }
3900
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003901 // Compute the new value.
3902 __ mov(r1, Operand(Smi::FromInt(1)));
3903 frame_->EmitPush(r0);
3904 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003905 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003906 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003907 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003908 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003909 }
3910
3911 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003912 exit.Bind();
3913 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003914 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003915 }
3916
3917 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003918 if (is_postfix) frame_->EmitPop(r0);
3919 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003920}
3921
3922
ager@chromium.org7c537e22008-10-16 08:43:32 +00003923void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003924#ifdef DEBUG
3925 int original_height = frame_->height();
3926#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003927 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003928 Comment cmnt(masm_, "[ BinaryOperation");
3929 Token::Value op = node->op();
3930
3931 // According to ECMA-262 section 11.11, page 58, the binary logical
3932 // operators must yield the result of one of the two expressions
3933 // before any ToBoolean() conversions. This means that the value
3934 // produced by a && or || operator is not necessarily a boolean.
3935
3936 // NOTE: If the left hand side produces a materialized value (not in
3937 // the CC register), we force the right hand side to do the
3938 // same. This is necessary because we may have to branch to the exit
3939 // after evaluating the left hand side (due to the shortcut
3940 // semantics), but the compiler must (statically) know if the result
3941 // of compiling the binary operation is materialized or not.
3942
3943 if (op == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003944 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003945 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003946 &is_true,
3947 false_target(),
3948 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003949 if (has_valid_frame() && !has_cc()) {
3950 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003951 JumpTarget pop_and_continue;
3952 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003953
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003954 __ ldr(r0, frame_->Top()); // Duplicate the stack top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003955 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003956 // Avoid popping the result if it converts to 'false' using the
3957 // standard ToBoolean() conversion as described in ECMA-262,
3958 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003959 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003960 Branch(false, &exit);
3961
3962 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003963 pop_and_continue.Bind();
3964 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003965
3966 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003967 is_true.Bind();
3968 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003969
3970 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003971 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003972 } else if (has_cc() || is_true.is_linked()) {
3973 // The left-hand side is either (a) partially compiled to
3974 // control flow with a final branch left to emit or (b) fully
3975 // compiled to control flow and possibly true.
3976 if (has_cc()) {
3977 Branch(false, false_target());
3978 }
3979 is_true.Bind();
3980 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003981 true_target(),
3982 false_target(),
3983 false);
3984 } else {
3985 // Nothing to do.
3986 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003987 }
3988
3989 } else if (op == Token::OR) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003990 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003991 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003992 true_target(),
3993 &is_false,
3994 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003995 if (has_valid_frame() && !has_cc()) {
3996 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003997 JumpTarget pop_and_continue;
3998 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003999
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004000 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004001 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004002 // Avoid popping the result if it converts to 'true' using the
4003 // standard ToBoolean() conversion as described in ECMA-262,
4004 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00004005 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004006 Branch(true, &exit);
4007
4008 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004009 pop_and_continue.Bind();
4010 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004011
4012 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004013 is_false.Bind();
4014 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004015
4016 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004017 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004018 } else if (has_cc() || is_false.is_linked()) {
4019 // The left-hand side is either (a) partially compiled to
4020 // control flow with a final branch left to emit or (b) fully
4021 // compiled to control flow and possibly false.
4022 if (has_cc()) {
4023 Branch(true, true_target());
4024 }
4025 is_false.Bind();
4026 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004027 true_target(),
4028 false_target(),
4029 false);
4030 } else {
4031 // Nothing to do.
4032 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004033 }
4034
4035 } else {
4036 // Optimize for the case where (at least) one of the expressions
4037 // is a literal small integer.
4038 Literal* lliteral = node->left()->AsLiteral();
4039 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004040 // NOTE: The code below assumes that the slow cases (calls to runtime)
4041 // never return a constant/immutable object.
4042 bool overwrite_left =
4043 (node->left()->AsBinaryOperation() != NULL &&
4044 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
4045 bool overwrite_right =
4046 (node->right()->AsBinaryOperation() != NULL &&
4047 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004048
4049 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004050 LoadAndSpill(node->left());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004051 SmiOperation(node->op(),
4052 rliteral->handle(),
4053 false,
4054 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004055
4056 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004057 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004058 SmiOperation(node->op(),
4059 lliteral->handle(),
4060 true,
4061 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004062
4063 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004064 OverwriteMode overwrite_mode = NO_OVERWRITE;
4065 if (overwrite_left) {
4066 overwrite_mode = OVERWRITE_LEFT;
4067 } else if (overwrite_right) {
4068 overwrite_mode = OVERWRITE_RIGHT;
4069 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004070 LoadAndSpill(node->left());
4071 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004072 GenericBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004073 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004074 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004075 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004076 ASSERT(!has_valid_frame() ||
4077 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004078 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004079}
4080
4081
ager@chromium.org7c537e22008-10-16 08:43:32 +00004082void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004083#ifdef DEBUG
4084 int original_height = frame_->height();
4085#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004086 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004087 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004088 frame_->EmitPush(r0);
4089 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004090}
4091
4092
ager@chromium.org7c537e22008-10-16 08:43:32 +00004093void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004094#ifdef DEBUG
4095 int original_height = frame_->height();
4096#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004097 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004098 Comment cmnt(masm_, "[ CompareOperation");
4099
4100 // Get the expressions from the node.
4101 Expression* left = node->left();
4102 Expression* right = node->right();
4103 Token::Value op = node->op();
4104
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004105 // To make null checks efficient, we check if either left or right is the
4106 // literal 'null'. If so, we optimize the code by inlining a null check
4107 // instead of calling the (very) general runtime routine for checking
4108 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004109 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004110 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004111 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004112 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004113 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
4114 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004115 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004116 LoadAndSpill(left_is_null ? right : left);
4117 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004118 __ LoadRoot(ip, Heap::kNullValueRootIndex);
4119 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004120
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004121 // The 'null' value is only equal to 'undefined' if using non-strict
4122 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004123 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004124 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004125
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004126 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4127 __ cmp(r0, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004128 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004129
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004130 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004131 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004132
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004133 // It can be an undetectable object.
4134 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
4135 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
4136 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
4137 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004138 }
4139
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004140 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004141 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004142 return;
4143 }
4144 }
4145
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004146 // To make typeof testing for natives implemented in JavaScript really
4147 // efficient, we generate special code for expressions of the form:
4148 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004149 UnaryOperation* operation = left->AsUnaryOperation();
4150 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
4151 (operation != NULL && operation->op() == Token::TYPEOF) &&
4152 (right->AsLiteral() != NULL &&
4153 right->AsLiteral()->handle()->IsString())) {
4154 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
4155
mads.s.ager31e71382008-08-13 09:32:07 +00004156 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004157 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004158 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004159
4160 if (check->Equals(Heap::number_symbol())) {
4161 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004162 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004163 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004164 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
4165 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004166 cc_reg_ = eq;
4167
4168 } else if (check->Equals(Heap::string_symbol())) {
4169 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004170 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004171
4172 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4173
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004174 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004175 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4176 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4177 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004178 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004179
4180 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
4181 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
4182 cc_reg_ = lt;
4183
4184 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004185 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
4186 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004187 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004188 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
4189 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004190 cc_reg_ = eq;
4191
4192 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004193 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4194 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004195 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004196
4197 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004198 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004199
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004200 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004201 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4202 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4203 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4204 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4205
4206 cc_reg_ = eq;
4207
4208 } else if (check->Equals(Heap::function_symbol())) {
4209 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004210 false_target()->Branch(eq);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004211 Register map_reg = r2;
4212 __ CompareObjectType(r1, map_reg, r1, JS_FUNCTION_TYPE);
4213 true_target()->Branch(eq);
4214 // Regular expressions are callable so typeof == 'function'.
4215 __ CompareInstanceType(map_reg, r1, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004216 cc_reg_ = eq;
4217
4218 } else if (check->Equals(Heap::object_symbol())) {
4219 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004220 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004221
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004222 __ LoadRoot(ip, Heap::kNullValueRootIndex);
4223 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004224 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004225
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004226 Register map_reg = r2;
4227 __ CompareObjectType(r1, map_reg, r1, JS_REGEXP_TYPE);
4228 false_target()->Branch(eq);
4229
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004230 // It can be an undetectable object.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004231 __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004232 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4233 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004234 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004235
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004236 __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4237 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004238 false_target()->Branch(lt);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004239 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004240 cc_reg_ = le;
4241
4242 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004243 // Uncommon case: typeof testing against a string literal that is
4244 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004245 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004246 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004247 ASSERT(!has_valid_frame() ||
4248 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004249 return;
4250 }
4251
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004252 switch (op) {
4253 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004254 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004255 break;
4256
4257 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004258 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004259 break;
4260
4261 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004262 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004263 break;
4264
4265 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004266 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004267 break;
4268
4269 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004270 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004271 break;
4272
4273 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004274 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004275 break;
4276
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004277 case Token::IN: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004278 LoadAndSpill(left);
4279 LoadAndSpill(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004280 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004281 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004282 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004283 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004284
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004285 case Token::INSTANCEOF: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004286 LoadAndSpill(left);
4287 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004288 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004289 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004290 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004291 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004292 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004293 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004294 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004295
4296 default:
4297 UNREACHABLE();
4298 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004299 ASSERT((has_cc() && frame_->height() == original_height) ||
4300 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004301}
4302
4303
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004304void CodeGenerator::EmitKeyedLoad(bool is_global) {
4305 Comment cmnt(masm_, "[ Load from keyed Property");
4306 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
4307 RelocInfo::Mode rmode = is_global
4308 ? RelocInfo::CODE_TARGET_CONTEXT
4309 : RelocInfo::CODE_TARGET;
4310 frame_->CallCodeObject(ic, rmode, 0);
4311}
4312
4313
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004314#ifdef DEBUG
4315bool CodeGenerator::HasValidEntryRegisters() { return true; }
4316#endif
4317
4318
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004319#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004320#define __ ACCESS_MASM(masm)
4321
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004322
ager@chromium.org7c537e22008-10-16 08:43:32 +00004323Handle<String> Reference::GetName() {
4324 ASSERT(type_ == NAMED);
4325 Property* property = expression_->AsProperty();
4326 if (property == NULL) {
4327 // Global variable reference treated as a named property reference.
4328 VariableProxy* proxy = expression_->AsVariableProxy();
4329 ASSERT(proxy->AsVariable() != NULL);
4330 ASSERT(proxy->AsVariable()->is_global());
4331 return proxy->name();
4332 } else {
4333 Literal* raw_name = property->key()->AsLiteral();
4334 ASSERT(raw_name != NULL);
4335 return Handle<String>(String::cast(*raw_name->handle()));
4336 }
4337}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004338
ager@chromium.org7c537e22008-10-16 08:43:32 +00004339
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004340void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004341 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004342 ASSERT(!is_illegal());
4343 ASSERT(!cgen_->has_cc());
4344 MacroAssembler* masm = cgen_->masm();
4345 Property* property = expression_->AsProperty();
4346 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004347 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004348 }
4349
4350 switch (type_) {
4351 case SLOT: {
4352 Comment cmnt(masm, "[ Load from Slot");
4353 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4354 ASSERT(slot != NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004355 cgen_->LoadFromSlot(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004356 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004357 }
4358
ager@chromium.org7c537e22008-10-16 08:43:32 +00004359 case NAMED: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004360 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004361 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004362 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004363 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004364 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4365 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004366 __ mov(r2, Operand(name));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004367 ASSERT(var == NULL || var->is_global());
4368 RelocInfo::Mode rmode = (var == NULL)
4369 ? RelocInfo::CODE_TARGET
4370 : RelocInfo::CODE_TARGET_CONTEXT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004371 frame->CallCodeObject(ic, rmode, 0);
4372 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004373 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004374 }
4375
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004376 case KEYED: {
4377 // TODO(181): Implement inlined version of array indexing once
4378 // loop nesting is properly tracked on ARM.
4379 ASSERT(property != NULL);
4380 Variable* var = expression_->AsVariableProxy()->AsVariable();
4381 ASSERT(var == NULL || var->is_global());
4382 cgen_->EmitKeyedLoad(var != NULL);
4383 cgen_->frame()->EmitPush(r0);
4384 break;
4385 }
4386
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004387 default:
4388 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004389 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004390
4391 if (!persist_after_get_) {
4392 cgen_->UnloadReference(this);
4393 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004394}
4395
4396
ager@chromium.org7c537e22008-10-16 08:43:32 +00004397void Reference::SetValue(InitState init_state) {
4398 ASSERT(!is_illegal());
4399 ASSERT(!cgen_->has_cc());
4400 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004401 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004402 Property* property = expression_->AsProperty();
4403 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004404 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004405 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004406
ager@chromium.org7c537e22008-10-16 08:43:32 +00004407 switch (type_) {
4408 case SLOT: {
4409 Comment cmnt(masm, "[ Store to Slot");
4410 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004411 cgen_->StoreToSlot(slot, init_state);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004412 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004413 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004414 }
4415
ager@chromium.org7c537e22008-10-16 08:43:32 +00004416 case NAMED: {
4417 Comment cmnt(masm, "[ Store to named Property");
4418 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004419 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004420 Handle<String> name(GetName());
4421
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004422 frame->EmitPop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004423 frame->EmitPop(r1);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004424 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004425 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004426 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004427 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004428 break;
4429 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004430
ager@chromium.org7c537e22008-10-16 08:43:32 +00004431 case KEYED: {
4432 Comment cmnt(masm, "[ Store to keyed Property");
4433 Property* property = expression_->AsProperty();
4434 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004435 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004436
4437 // Call IC code.
4438 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004439 frame->EmitPop(r0); // value
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004440 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004441 frame->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004442 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004443 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004444 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004445
4446 default:
4447 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004448 }
4449}
4450
4451
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004452void FastNewClosureStub::Generate(MacroAssembler* masm) {
4453 // Clone the boilerplate in new space. Set the context to the
4454 // current context in cp.
4455 Label gc;
4456
4457 // Pop the boilerplate function from the stack.
4458 __ pop(r3);
4459
4460 // Attempt to allocate new JSFunction in new space.
4461 __ AllocateInNewSpace(JSFunction::kSize / kPointerSize,
4462 r0,
4463 r1,
4464 r2,
4465 &gc,
4466 TAG_OBJECT);
4467
4468 // Compute the function map in the current global context and set that
4469 // as the map of the allocated object.
4470 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
4471 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
4472 __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
4473 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4474
4475 // Clone the rest of the boilerplate fields. We don't have to update
4476 // the write barrier because the allocated object is in new space.
4477 for (int offset = kPointerSize;
4478 offset < JSFunction::kSize;
4479 offset += kPointerSize) {
4480 if (offset == JSFunction::kContextOffset) {
4481 __ str(cp, FieldMemOperand(r0, offset));
4482 } else {
4483 __ ldr(r1, FieldMemOperand(r3, offset));
4484 __ str(r1, FieldMemOperand(r0, offset));
4485 }
4486 }
4487
4488 // Return result. The argument boilerplate has been popped already.
4489 __ Ret();
4490
4491 // Create a new closure through the slower runtime call.
4492 __ bind(&gc);
4493 __ push(cp);
4494 __ push(r3);
4495 __ TailCallRuntime(ExternalReference(Runtime::kNewClosure), 2, 1);
4496}
4497
4498
4499void FastNewContextStub::Generate(MacroAssembler* masm) {
4500 // Try to allocate the context in new space.
4501 Label gc;
4502 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
4503
4504 // Attempt to allocate the context in new space.
4505 __ AllocateInNewSpace(length + (FixedArray::kHeaderSize / kPointerSize),
4506 r0,
4507 r1,
4508 r2,
4509 &gc,
4510 TAG_OBJECT);
4511
4512 // Load the function from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00004513 __ ldr(r3, MemOperand(sp, 0));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004514
4515 // Setup the object header.
4516 __ LoadRoot(r2, Heap::kContextMapRootIndex);
4517 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4518 __ mov(r2, Operand(length));
4519 __ str(r2, FieldMemOperand(r0, Array::kLengthOffset));
4520
4521 // Setup the fixed slots.
4522 __ mov(r1, Operand(Smi::FromInt(0)));
4523 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
4524 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
4525 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
4526 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
4527
4528 // Copy the global object from the surrounding context.
4529 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
4530 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
4531
4532 // Initialize the rest of the slots to undefined.
4533 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
4534 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
4535 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
4536 }
4537
4538 // Remove the on-stack argument and return.
4539 __ mov(cp, r0);
4540 __ pop();
4541 __ Ret();
4542
4543 // Need to collect. Call into runtime system.
4544 __ bind(&gc);
4545 __ TailCallRuntime(ExternalReference(Runtime::kNewContext), 1, 1);
4546}
4547
4548
ager@chromium.org5c838252010-02-19 08:53:10 +00004549void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
4550 // Stack layout on entry:
4551 //
4552 // [sp]: constant elements.
4553 // [sp + kPointerSize]: literal index.
4554 // [sp + (2 * kPointerSize)]: literals array.
4555
4556 // All sizes here are multiples of kPointerSize.
4557 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
4558 int size = JSArray::kSize + elements_size;
4559
4560 // Load boilerplate object into r3 and check if we need to create a
4561 // boilerplate.
4562 Label slow_case;
4563 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
4564 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
4565 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4566 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
4567 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4568 __ cmp(r3, ip);
4569 __ b(eq, &slow_case);
4570
4571 // Allocate both the JS array and the elements array in one big
4572 // allocation. This avoids multiple limit checks.
4573 __ AllocateInNewSpace(size / kPointerSize,
4574 r0,
4575 r1,
4576 r2,
4577 &slow_case,
4578 TAG_OBJECT);
4579
4580 // Copy the JS array part.
4581 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
4582 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
4583 __ ldr(r1, FieldMemOperand(r3, i));
4584 __ str(r1, FieldMemOperand(r0, i));
4585 }
4586 }
4587
4588 if (length_ > 0) {
4589 // Get hold of the elements array of the boilerplate and setup the
4590 // elements pointer in the resulting object.
4591 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
4592 __ add(r2, r0, Operand(JSArray::kSize));
4593 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
4594
4595 // Copy the elements array.
4596 for (int i = 0; i < elements_size; i += kPointerSize) {
4597 __ ldr(r1, FieldMemOperand(r3, i));
4598 __ str(r1, FieldMemOperand(r2, i));
4599 }
4600 }
4601
4602 // Return and remove the on-stack parameters.
4603 __ add(sp, sp, Operand(3 * kPointerSize));
4604 __ Ret();
4605
4606 __ bind(&slow_case);
4607 ExternalReference runtime(Runtime::kCreateArrayLiteralShallow);
4608 __ TailCallRuntime(runtime, 3, 1);
4609}
4610
4611
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004612// Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz
4613// instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0
4614// (31 instead of 32).
4615static void CountLeadingZeros(
4616 MacroAssembler* masm,
4617 Register source,
4618 Register scratch,
4619 Register zeros) {
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00004620#ifdef CAN_USE_ARMV5_INSTRUCTIONS
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004621 __ clz(zeros, source); // This instruction is only supported after ARM5.
4622#else
4623 __ mov(zeros, Operand(0));
4624 __ mov(scratch, source);
4625 // Top 16.
4626 __ tst(scratch, Operand(0xffff0000));
4627 __ add(zeros, zeros, Operand(16), LeaveCC, eq);
4628 __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
4629 // Top 8.
4630 __ tst(scratch, Operand(0xff000000));
4631 __ add(zeros, zeros, Operand(8), LeaveCC, eq);
4632 __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
4633 // Top 4.
4634 __ tst(scratch, Operand(0xf0000000));
4635 __ add(zeros, zeros, Operand(4), LeaveCC, eq);
4636 __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
4637 // Top 2.
4638 __ tst(scratch, Operand(0xc0000000));
4639 __ add(zeros, zeros, Operand(2), LeaveCC, eq);
4640 __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
4641 // Top bit.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004642 __ tst(scratch, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004643 __ add(zeros, zeros, Operand(1), LeaveCC, eq);
4644#endif
4645}
4646
4647
4648// Takes a Smi and converts to an IEEE 64 bit floating point value in two
4649// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
4650// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
4651// scratch register. Destroys the source register. No GC occurs during this
4652// stub so you don't have to set up the frame.
4653class ConvertToDoubleStub : public CodeStub {
4654 public:
4655 ConvertToDoubleStub(Register result_reg_1,
4656 Register result_reg_2,
4657 Register source_reg,
4658 Register scratch_reg)
4659 : result1_(result_reg_1),
4660 result2_(result_reg_2),
4661 source_(source_reg),
4662 zeros_(scratch_reg) { }
4663
4664 private:
4665 Register result1_;
4666 Register result2_;
4667 Register source_;
4668 Register zeros_;
4669
4670 // Minor key encoding in 16 bits.
4671 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4672 class OpBits: public BitField<Token::Value, 2, 14> {};
4673
4674 Major MajorKey() { return ConvertToDouble; }
4675 int MinorKey() {
4676 // Encode the parameters in a unique 16 bit value.
4677 return result1_.code() +
4678 (result2_.code() << 4) +
4679 (source_.code() << 8) +
4680 (zeros_.code() << 12);
4681 }
4682
4683 void Generate(MacroAssembler* masm);
4684
4685 const char* GetName() { return "ConvertToDoubleStub"; }
4686
4687#ifdef DEBUG
4688 void Print() { PrintF("ConvertToDoubleStub\n"); }
4689#endif
4690};
4691
4692
4693void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
4694#ifndef BIG_ENDIAN_FLOATING_POINT
4695 Register exponent = result1_;
4696 Register mantissa = result2_;
4697#else
4698 Register exponent = result2_;
4699 Register mantissa = result1_;
4700#endif
4701 Label not_special;
4702 // Convert from Smi to integer.
4703 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
4704 // Move sign bit from source to destination. This works because the sign bit
4705 // in the exponent word of the double has the same position and polarity as
4706 // the 2's complement sign bit in a Smi.
4707 ASSERT(HeapNumber::kSignMask == 0x80000000u);
4708 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
4709 // Subtract from 0 if source was negative.
4710 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
4711 __ cmp(source_, Operand(1));
4712 __ b(gt, &not_special);
4713
4714 // We have -1, 0 or 1, which we treat specially.
4715 __ cmp(source_, Operand(0));
4716 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
4717 static const uint32_t exponent_word_for_1 =
4718 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
4719 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne);
4720 // 1, 0 and -1 all have 0 for the second word.
4721 __ mov(mantissa, Operand(0));
4722 __ Ret();
4723
4724 __ bind(&not_special);
4725 // Count leading zeros. Uses result2 for a scratch register on pre-ARM5.
4726 // Gets the wrong answer for 0, but we already checked for that case above.
4727 CountLeadingZeros(masm, source_, mantissa, zeros_);
4728 // Compute exponent and or it into the exponent register.
4729 // We use result2 as a scratch register here.
4730 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
4731 __ orr(exponent,
4732 exponent,
4733 Operand(mantissa, LSL, HeapNumber::kExponentShift));
4734 // Shift up the source chopping the top bit off.
4735 __ add(zeros_, zeros_, Operand(1));
4736 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
4737 __ mov(source_, Operand(source_, LSL, zeros_));
4738 // Compute lower part of fraction (last 12 bits).
4739 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
4740 // And the top (top 20 bits).
4741 __ orr(exponent,
4742 exponent,
4743 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
4744 __ Ret();
4745}
4746
4747
4748// This stub can convert a signed int32 to a heap number (double). It does
4749// not work for int32s that are in Smi range! No GC occurs during this stub
4750// so you don't have to set up the frame.
4751class WriteInt32ToHeapNumberStub : public CodeStub {
4752 public:
4753 WriteInt32ToHeapNumberStub(Register the_int,
4754 Register the_heap_number,
4755 Register scratch)
4756 : the_int_(the_int),
4757 the_heap_number_(the_heap_number),
4758 scratch_(scratch) { }
4759
4760 private:
4761 Register the_int_;
4762 Register the_heap_number_;
4763 Register scratch_;
4764
4765 // Minor key encoding in 16 bits.
4766 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4767 class OpBits: public BitField<Token::Value, 2, 14> {};
4768
4769 Major MajorKey() { return WriteInt32ToHeapNumber; }
4770 int MinorKey() {
4771 // Encode the parameters in a unique 16 bit value.
4772 return the_int_.code() +
4773 (the_heap_number_.code() << 4) +
4774 (scratch_.code() << 8);
4775 }
4776
4777 void Generate(MacroAssembler* masm);
4778
4779 const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
4780
4781#ifdef DEBUG
4782 void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
4783#endif
4784};
4785
4786
4787// See comment for class.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004788void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004789 Label max_negative_int;
4790 // the_int_ has the answer which is a signed int32 but not a Smi.
4791 // We test for the special value that has a different exponent. This test
4792 // has the neat side effect of setting the flags according to the sign.
4793 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004794 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004795 __ b(eq, &max_negative_int);
4796 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
4797 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
4798 uint32_t non_smi_exponent =
4799 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
4800 __ mov(scratch_, Operand(non_smi_exponent));
4801 // Set the sign bit in scratch_ if the value was negative.
4802 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
4803 // Subtract from 0 if the value was negative.
4804 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
4805 // We should be masking the implict first digit of the mantissa away here,
4806 // but it just ends up combining harmlessly with the last digit of the
4807 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
4808 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
4809 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
4810 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
4811 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
4812 __ str(scratch_, FieldMemOperand(the_heap_number_,
4813 HeapNumber::kExponentOffset));
4814 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
4815 __ str(scratch_, FieldMemOperand(the_heap_number_,
4816 HeapNumber::kMantissaOffset));
4817 __ Ret();
4818
4819 __ bind(&max_negative_int);
4820 // The max negative int32 is stored as a positive number in the mantissa of
4821 // a double because it uses a sign bit instead of using two's complement.
4822 // The actual mantissa bits stored are all 0 because the implicit most
4823 // significant 1 bit is not stored.
4824 non_smi_exponent += 1 << HeapNumber::kExponentShift;
4825 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
4826 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
4827 __ mov(ip, Operand(0));
4828 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
4829 __ Ret();
4830}
4831
4832
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004833// Handle the case where the lhs and rhs are the same object.
4834// Equality is almost reflexive (everything but NaN), so this is a test
4835// for "identity and not NaN".
4836static void EmitIdenticalObjectComparison(MacroAssembler* masm,
4837 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004838 Condition cc,
4839 bool never_nan_nan) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004840 Label not_identical;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004841 Label heap_number, return_equal;
4842 Register exp_mask_reg = r5;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004843 __ cmp(r0, Operand(r1));
4844 __ b(ne, &not_identical);
4845
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004846 // The two objects are identical. If we know that one of them isn't NaN then
4847 // we now know they test equal.
4848 if (cc != eq || !never_nan_nan) {
4849 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004850
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004851 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
4852 // so we do the second best thing - test it ourselves.
4853 // They are both equal and they are not both Smis so both of them are not
4854 // Smis. If it's not a heap number, then return equal.
4855 if (cc == lt || cc == gt) {
4856 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004857 __ b(ge, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004858 } else {
4859 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4860 __ b(eq, &heap_number);
4861 // Comparing JS objects with <=, >= is complicated.
4862 if (cc != eq) {
4863 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
4864 __ b(ge, slow);
4865 // Normally here we fall through to return_equal, but undefined is
4866 // special: (undefined == undefined) == true, but
4867 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
4868 if (cc == le || cc == ge) {
4869 __ cmp(r4, Operand(ODDBALL_TYPE));
4870 __ b(ne, &return_equal);
4871 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
4872 __ cmp(r0, Operand(r2));
4873 __ b(ne, &return_equal);
4874 if (cc == le) {
4875 // undefined <= undefined should fail.
4876 __ mov(r0, Operand(GREATER));
4877 } else {
4878 // undefined >= undefined should fail.
4879 __ mov(r0, Operand(LESS));
4880 }
4881 __ mov(pc, Operand(lr)); // Return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004882 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004883 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004884 }
4885 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004886
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004887 __ bind(&return_equal);
4888 if (cc == lt) {
4889 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
4890 } else if (cc == gt) {
4891 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
4892 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004893 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004894 }
4895 __ mov(pc, Operand(lr)); // Return.
4896
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004897 if (cc != eq || !never_nan_nan) {
4898 // For less and greater we don't have to check for NaN since the result of
4899 // x < x is false regardless. For the others here is some code to check
4900 // for NaN.
4901 if (cc != lt && cc != gt) {
4902 __ bind(&heap_number);
4903 // It is a heap number, so return non-equal if it's NaN and equal if it's
4904 // not NaN.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004905
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004906 // The representation of NaN values has all exponent bits (52..62) set,
4907 // and not all mantissa bits (0..51) clear.
4908 // Read top bits of double representation (second word of value).
4909 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
4910 // Test that exponent bits are all set.
4911 __ and_(r3, r2, Operand(exp_mask_reg));
4912 __ cmp(r3, Operand(exp_mask_reg));
4913 __ b(ne, &return_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004914
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004915 // Shift out flag and all exponent bits, retaining only mantissa.
4916 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
4917 // Or with all low-bits of mantissa.
4918 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
4919 __ orr(r0, r3, Operand(r2), SetCC);
4920 // For equal we already have the right value in r0: Return zero (equal)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004921 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
4922 // not (it's a NaN). For <= and >= we need to load r0 with the failing
4923 // value if it's a NaN.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004924 if (cc != eq) {
4925 // All-zero means Infinity means equal.
4926 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
4927 if (cc == le) {
4928 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
4929 } else {
4930 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
4931 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004932 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004933 __ mov(pc, Operand(lr)); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004934 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004935 // No fall through here.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004936 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004937
4938 __ bind(&not_identical);
4939}
4940
4941
4942// See comment at call site.
4943static void EmitSmiNonsmiComparison(MacroAssembler* masm,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004944 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004945 Label* slow,
4946 bool strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004947 Label rhs_is_smi;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004948 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004949 __ b(eq, &rhs_is_smi);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004950
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004951 // Lhs is a Smi. Check whether the rhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004952 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4953 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004954 // If rhs is not a number and lhs is a Smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004955 // succeed. Return non-equal (r0 is already not zero)
4956 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4957 } else {
4958 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4959 // the runtime.
4960 __ b(ne, slow);
4961 }
4962
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004963 // Lhs (r1) is a smi, rhs (r0) is a number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004964 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004965 // Convert lhs to a double in d7 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004966 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004967 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
4968 __ vmov(s15, r7);
4969 __ vcvt(d7, s15);
4970 // Load the double from rhs, tagged HeapNumber r0, to d6.
4971 __ sub(r7, r0, Operand(kHeapObjectTag));
4972 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004973 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004974 __ push(lr);
4975 // Convert lhs to a double in r2, r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004976 __ mov(r7, Operand(r1));
4977 ConvertToDoubleStub stub1(r3, r2, r7, r6);
4978 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004979 // Load rhs to a double in r0, r1.
4980 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4981 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4982 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004983 }
4984
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004985 // We now have both loaded as doubles but we can skip the lhs nan check
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004986 // since it's a smi.
4987 __ jmp(lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004988
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004989 __ bind(&rhs_is_smi);
4990 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004991 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
4992 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004993 // If lhs is not a number and rhs is a smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004994 // succeed. Return non-equal.
4995 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
4996 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4997 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004998 // Smi compared non-strictly with a non-smi non-heap-number. Call
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004999 // the runtime.
5000 __ b(ne, slow);
5001 }
5002
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005003 // Rhs (r0) is a smi, lhs (r1) is a heap number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005004 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005005 // Convert rhs to a double in d6 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005006 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005007 // Load the double from lhs, tagged HeapNumber r1, to d7.
5008 __ sub(r7, r1, Operand(kHeapObjectTag));
5009 __ vldr(d7, r7, HeapNumber::kValueOffset);
5010 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
5011 __ vmov(s13, r7);
5012 __ vcvt(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005013 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005014 __ push(lr);
5015 // Load lhs to a double in r2, r3.
5016 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
5017 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
5018 // Convert rhs to a double in r0, r1.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005019 __ mov(r7, Operand(r0));
5020 ConvertToDoubleStub stub2(r1, r0, r7, r6);
5021 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005022 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005023 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005024 // Fall through to both_loaded_as_doubles.
5025}
5026
5027
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005028void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005029 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005030 Register rhs_exponent = exp_first ? r0 : r1;
5031 Register lhs_exponent = exp_first ? r2 : r3;
5032 Register rhs_mantissa = exp_first ? r1 : r0;
5033 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005034 Label one_is_nan, neither_is_nan;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005035 Label lhs_not_nan_exp_mask_is_loaded;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005036
5037 Register exp_mask_reg = r5;
5038
5039 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005040 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
5041 __ cmp(r4, Operand(exp_mask_reg));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005042 __ b(ne, &lhs_not_nan_exp_mask_is_loaded);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005043 __ mov(r4,
5044 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
5045 SetCC);
5046 __ b(ne, &one_is_nan);
5047 __ cmp(lhs_mantissa, Operand(0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005048 __ b(ne, &one_is_nan);
5049
5050 __ bind(lhs_not_nan);
5051 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
5052 __ bind(&lhs_not_nan_exp_mask_is_loaded);
5053 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
5054 __ cmp(r4, Operand(exp_mask_reg));
5055 __ b(ne, &neither_is_nan);
5056 __ mov(r4,
5057 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
5058 SetCC);
5059 __ b(ne, &one_is_nan);
5060 __ cmp(rhs_mantissa, Operand(0));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005061 __ b(eq, &neither_is_nan);
5062
5063 __ bind(&one_is_nan);
5064 // NaN comparisons always fail.
5065 // Load whatever we need in r0 to make the comparison fail.
5066 if (cc == lt || cc == le) {
5067 __ mov(r0, Operand(GREATER));
5068 } else {
5069 __ mov(r0, Operand(LESS));
5070 }
5071 __ mov(pc, Operand(lr)); // Return.
5072
5073 __ bind(&neither_is_nan);
5074}
5075
5076
5077// See comment at call site.
5078static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
5079 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005080 Register rhs_exponent = exp_first ? r0 : r1;
5081 Register lhs_exponent = exp_first ? r2 : r3;
5082 Register rhs_mantissa = exp_first ? r1 : r0;
5083 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005084
5085 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
5086 if (cc == eq) {
5087 // Doubles are not equal unless they have the same bit pattern.
5088 // Exception: 0 and -0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005089 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
5090 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005091 // Return non-zero if the numbers are unequal.
5092 __ mov(pc, Operand(lr), LeaveCC, ne);
5093
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005094 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005095 // If exponents are equal then return 0.
5096 __ mov(pc, Operand(lr), LeaveCC, eq);
5097
5098 // Exponents are unequal. The only way we can return that the numbers
5099 // are equal is if one is -0 and the other is 0. We already dealt
5100 // with the case where both are -0 or both are 0.
5101 // We start by seeing if the mantissas (that are equal) or the bottom
5102 // 31 bits of the rhs exponent are non-zero. If so we return not
5103 // equal.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005104 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005105 __ mov(r0, Operand(r4), LeaveCC, ne);
5106 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
5107 // Now they are equal if and only if the lhs exponent is zero in its
5108 // low 31 bits.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005109 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005110 __ mov(pc, Operand(lr));
5111 } else {
5112 // Call a native function to do a comparison between two non-NaNs.
5113 // Call C routine that may not cause GC or other trouble.
5114 __ mov(r5, Operand(ExternalReference::compare_doubles()));
5115 __ Jump(r5); // Tail call.
5116 }
5117}
5118
5119
5120// See comment at call site.
5121static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
5122 // If either operand is a JSObject or an oddball value, then they are
5123 // not equal since their pointers are different.
5124 // There is no test for undetectability in strict equality.
5125 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
5126 Label first_non_object;
5127 // Get the type of the first operand into r2 and compare it with
5128 // FIRST_JS_OBJECT_TYPE.
5129 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
5130 __ b(lt, &first_non_object);
5131
5132 // Return non-zero (r0 is not zero)
5133 Label return_not_equal;
5134 __ bind(&return_not_equal);
5135 __ mov(pc, Operand(lr)); // Return.
5136
5137 __ bind(&first_non_object);
5138 // Check for oddballs: true, false, null, undefined.
5139 __ cmp(r2, Operand(ODDBALL_TYPE));
5140 __ b(eq, &return_not_equal);
5141
5142 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
5143 __ b(ge, &return_not_equal);
5144
5145 // Check for oddballs: true, false, null, undefined.
5146 __ cmp(r3, Operand(ODDBALL_TYPE));
5147 __ b(eq, &return_not_equal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005148
5149 // Now that we have the types we might as well check for symbol-symbol.
5150 // Ensure that no non-strings have the symbol bit set.
5151 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
5152 ASSERT(kSymbolTag != 0);
5153 __ and_(r2, r2, Operand(r3));
5154 __ tst(r2, Operand(kIsSymbolMask));
5155 __ b(ne, &return_not_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005156}
5157
5158
5159// See comment at call site.
5160static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
5161 Label* both_loaded_as_doubles,
5162 Label* not_heap_numbers,
5163 Label* slow) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005164 __ CompareObjectType(r0, r3, r2, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005165 __ b(ne, not_heap_numbers);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005166 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
5167 __ cmp(r2, r3);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005168 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
5169
5170 // Both are heap numbers. Load them up then jump to the code we have
5171 // for that.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005172 if (CpuFeatures::IsSupported(VFP3)) {
5173 CpuFeatures::Scope scope(VFP3);
5174 __ sub(r7, r0, Operand(kHeapObjectTag));
5175 __ vldr(d6, r7, HeapNumber::kValueOffset);
5176 __ sub(r7, r1, Operand(kHeapObjectTag));
5177 __ vldr(d7, r7, HeapNumber::kValueOffset);
5178 } else {
5179 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
5180 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
5181 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
5182 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
5183 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005184 __ jmp(both_loaded_as_doubles);
5185}
5186
5187
5188// Fast negative check for symbol-to-symbol equality.
5189static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
5190 // r2 is object type of r0.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005191 // Ensure that no non-strings have the symbol bit set.
5192 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
5193 ASSERT(kSymbolTag != 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005194 __ tst(r2, Operand(kIsSymbolMask));
5195 __ b(eq, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005196 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
5197 __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005198 __ tst(r3, Operand(kIsSymbolMask));
5199 __ b(eq, slow);
5200
5201 // Both are symbols. We already checked they weren't the same pointer
5202 // so they are not equal.
5203 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
5204 __ mov(pc, Operand(lr)); // Return.
5205}
5206
5207
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005208// On entry r0 (rhs) and r1 (lhs) are the values to be compared.
5209// On exit r0 is 0, positive or negative to indicate the result of
5210// the comparison.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005211void CompareStub::Generate(MacroAssembler* masm) {
5212 Label slow; // Call builtin.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005213 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005214
5215 // NOTICE! This code is only reached after a smi-fast-case check, so
5216 // it is certain that at least one operand isn't a smi.
5217
5218 // Handle the case where the objects are identical. Either returns the answer
5219 // or goes to slow. Only falls through if the objects were not identical.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005220 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005221
5222 // If either is a Smi (we know that not both are), then they can only
5223 // be strictly equal if the other is a HeapNumber.
5224 ASSERT_EQ(0, kSmiTag);
5225 ASSERT_EQ(0, Smi::FromInt(0));
5226 __ and_(r2, r0, Operand(r1));
5227 __ tst(r2, Operand(kSmiTagMask));
5228 __ b(ne, &not_smis);
5229 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
5230 // 1) Return the answer.
5231 // 2) Go to slow.
5232 // 3) Fall through to both_loaded_as_doubles.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005233 // 4) Jump to lhs_not_nan.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005234 // In cases 3 and 4 we have found out we were dealing with a number-number
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005235 // comparison. If VFP3 is supported the double values of the numbers have
5236 // been loaded into d7 and d6. Otherwise, the double values have been loaded
5237 // into r0, r1, r2, and r3.
5238 EmitSmiNonsmiComparison(masm, &lhs_not_nan, &slow, strict_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005239
5240 __ bind(&both_loaded_as_doubles);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005241 // The arguments have been converted to doubles and stored in d6 and d7, if
5242 // VFP3 is supported, or in r0, r1, r2, and r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005243 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005244 __ bind(&lhs_not_nan);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005245 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005246 Label no_nan;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005247 // ARMv7 VFP3 instructions to implement double precision comparison.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005248 __ vcmp(d7, d6);
5249 __ vmrs(pc); // Move vector status bits to normal status bits.
5250 Label nan;
5251 __ b(vs, &nan);
5252 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
5253 __ mov(r0, Operand(LESS), LeaveCC, lt);
5254 __ mov(r0, Operand(GREATER), LeaveCC, gt);
5255 __ mov(pc, Operand(lr));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005256
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005257 __ bind(&nan);
5258 // If one of the sides was a NaN then the v flag is set. Load r0 with
5259 // whatever it takes to make the comparison fail, since comparisons with NaN
5260 // always fail.
5261 if (cc_ == lt || cc_ == le) {
5262 __ mov(r0, Operand(GREATER));
5263 } else {
5264 __ mov(r0, Operand(LESS));
5265 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005266 __ mov(pc, Operand(lr));
5267 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005268 // Checks for NaN in the doubles we have loaded. Can return the answer or
5269 // fall through if neither is a NaN. Also binds lhs_not_nan.
5270 EmitNanCheck(masm, &lhs_not_nan, cc_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005271 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
5272 // answer. Never falls through.
5273 EmitTwoNonNanDoubleComparison(masm, cc_);
5274 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005275
5276 __ bind(&not_smis);
5277 // At this point we know we are dealing with two different objects,
5278 // and neither of them is a Smi. The objects are in r0 and r1.
5279 if (strict_) {
5280 // This returns non-equal for some object types, or falls through if it
5281 // was not lucky.
5282 EmitStrictTwoHeapObjectCompare(masm);
5283 }
5284
5285 Label check_for_symbols;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005286 Label flat_string_check;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005287 // Check for heap-number-heap-number comparison. Can jump to slow case,
5288 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
5289 // that case. If the inputs are not doubles then jumps to check_for_symbols.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005290 // In this case r2 will contain the type of r0. Never falls through.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005291 EmitCheckForTwoHeapNumbers(masm,
5292 &both_loaded_as_doubles,
5293 &check_for_symbols,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005294 &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005295
5296 __ bind(&check_for_symbols);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005297 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
5298 // symbols.
5299 if (cc_ == eq && !strict_) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005300 // Either jumps to slow or returns the answer. Assumes that r2 is the type
5301 // of r0 on entry.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005302 EmitCheckForSymbols(masm, &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005303 }
5304
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005305 // Check for both being sequential ASCII strings, and inline if that is the
5306 // case.
5307 __ bind(&flat_string_check);
5308
5309 __ JumpIfNonSmisNotBothSequentialAsciiStrings(r0, r1, r2, r3, &slow);
5310
5311 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
5312 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
5313 r1,
5314 r0,
5315 r2,
5316 r3,
5317 r4,
5318 r5);
5319 // Never falls through to here.
5320
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005321 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005322
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005323 __ push(r1);
5324 __ push(r0);
5325 // Figure out which native to call and setup the arguments.
5326 Builtins::JavaScript native;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005327 if (cc_ == eq) {
5328 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
5329 } else {
5330 native = Builtins::COMPARE;
5331 int ncr; // NaN compare result
5332 if (cc_ == lt || cc_ == le) {
5333 ncr = GREATER;
5334 } else {
5335 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
5336 ncr = LESS;
5337 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005338 __ mov(r0, Operand(Smi::FromInt(ncr)));
5339 __ push(r0);
5340 }
5341
5342 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
5343 // tagged as a small integer.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005344 __ InvokeBuiltin(native, JUMP_JS);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005345}
5346
5347
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005348// Allocates a heap number or jumps to the label if the young space is full and
5349// a scavenge is needed.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005350static void AllocateHeapNumber(
5351 MacroAssembler* masm,
5352 Label* need_gc, // Jump here if young space is full.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005353 Register result, // The tagged address of the new heap number.
5354 Register scratch1, // A scratch register.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005355 Register scratch2) { // Another scratch register.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005356 // Allocate an object in the heap for the heap number and tag it as a heap
5357 // object.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005358 __ AllocateInNewSpace(HeapNumber::kSize / kPointerSize,
5359 result,
5360 scratch1,
5361 scratch2,
5362 need_gc,
5363 TAG_OBJECT);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005364
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005365 // Get heap number map and store it in the allocated object.
5366 __ LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex);
5367 __ str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005368}
5369
5370
5371// We fall into this code if the operands were Smis, but the result was
5372// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005373// the operands were not both Smi. The operands are in r0 and r1. In order
5374// to call the C-implemented binary fp operation routines we need to end up
5375// with the double precision floating point operands in r0 and r1 (for the
5376// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005377static void HandleBinaryOpSlowCases(MacroAssembler* masm,
5378 Label* not_smi,
5379 const Builtins::JavaScript& builtin,
5380 Token::Value operation,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005381 OverwriteMode mode) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005382 Label slow, slow_pop_2_first, do_the_call;
5383 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
5384 // Smi-smi case (overflow).
5385 // Since both are Smis there is no heap number to overwrite, so allocate.
5386 // The new heap number is in r5. r6 and r7 are scratch.
5387 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005388
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005389 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
5390 // using registers d7 and d6 for the double values.
5391 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) &&
5392 Token::MOD != operation;
5393 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005394 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005395 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
5396 __ vmov(s15, r7);
5397 __ vcvt(d7, s15);
5398 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
5399 __ vmov(s13, r7);
5400 __ vcvt(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005401 } else {
5402 // Write Smi from r0 to r3 and r2 in double format. r6 is scratch.
5403 __ mov(r7, Operand(r0));
5404 ConvertToDoubleStub stub1(r3, r2, r7, r6);
5405 __ push(lr);
5406 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
5407 // Write Smi from r1 to r1 and r0 in double format. r6 is scratch.
5408 __ mov(r7, Operand(r1));
5409 ConvertToDoubleStub stub2(r1, r0, r7, r6);
5410 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
5411 __ pop(lr);
5412 }
5413
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005414 __ jmp(&do_the_call); // Tail call. No return.
5415
5416 // We jump to here if something goes wrong (one param is not a number of any
5417 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005418 __ bind(&slow);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005419
5420 // Push arguments to the stack
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005421 __ push(r1);
5422 __ push(r0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005423
5424 if (Token::ADD == operation) {
5425 // Test for string arguments before calling runtime.
5426 // r1 : first argument
5427 // r0 : second argument
5428 // sp[0] : second argument
ager@chromium.org5c838252010-02-19 08:53:10 +00005429 // sp[4] : first argument
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005430
5431 Label not_strings, not_string1, string1;
5432 __ tst(r1, Operand(kSmiTagMask));
5433 __ b(eq, &not_string1);
5434 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
5435 __ b(ge, &not_string1);
5436
5437 // First argument is a a string, test second.
5438 __ tst(r0, Operand(kSmiTagMask));
5439 __ b(eq, &string1);
5440 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
5441 __ b(ge, &string1);
5442
5443 // First and second argument are strings.
ager@chromium.org5c838252010-02-19 08:53:10 +00005444 StringAddStub stub(NO_STRING_CHECK_IN_STUB);
5445 __ TailCallStub(&stub);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005446
5447 // Only first argument is a string.
5448 __ bind(&string1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005449 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
5450
5451 // First argument was not a string, test second.
5452 __ bind(&not_string1);
5453 __ tst(r0, Operand(kSmiTagMask));
5454 __ b(eq, &not_strings);
5455 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
5456 __ b(ge, &not_strings);
5457
5458 // Only second argument is a string.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005459 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
5460
5461 __ bind(&not_strings);
5462 }
5463
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005464 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005465
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005466 // We branch here if at least one of r0 and r1 is not a Smi.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005467 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005468 if (mode == NO_OVERWRITE) {
5469 // In the case where there is no chance of an overwritable float we may as
5470 // well do the allocation immediately while r0 and r1 are untouched.
5471 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5472 }
5473
5474 // Move r0 to a double in r2-r3.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005475 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005476 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5477 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005478 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005479 if (mode == OVERWRITE_RIGHT) {
5480 __ mov(r5, Operand(r0)); // Overwrite this heap number.
5481 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005482 if (use_fp_registers) {
5483 CpuFeatures::Scope scope(VFP3);
5484 // Load the double from tagged HeapNumber r0 to d7.
5485 __ sub(r7, r0, Operand(kHeapObjectTag));
5486 __ vldr(d7, r7, HeapNumber::kValueOffset);
5487 } else {
5488 // Calling convention says that second double is in r2 and r3.
5489 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
5490 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
5491 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005492 __ jmp(&finished_loading_r0);
5493 __ bind(&r0_is_smi);
5494 if (mode == OVERWRITE_RIGHT) {
5495 // We can't overwrite a Smi so get address of new heap number into r5.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005496 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005497 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005498
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005499 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005500 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005501 // Convert smi in r0 to double in d7.
5502 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
5503 __ vmov(s15, r7);
5504 __ vcvt(d7, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005505 } else {
5506 // Write Smi from r0 to r3 and r2 in double format.
5507 __ mov(r7, Operand(r0));
5508 ConvertToDoubleStub stub3(r3, r2, r7, r6);
5509 __ push(lr);
5510 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
5511 __ pop(lr);
5512 }
5513
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005514 __ bind(&finished_loading_r0);
5515
5516 // Move r1 to a double in r0-r1.
5517 __ tst(r1, Operand(kSmiTagMask));
5518 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5519 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5520 __ b(ne, &slow);
5521 if (mode == OVERWRITE_LEFT) {
5522 __ mov(r5, Operand(r1)); // Overwrite this heap number.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005523 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005524 if (use_fp_registers) {
5525 CpuFeatures::Scope scope(VFP3);
5526 // Load the double from tagged HeapNumber r1 to d6.
5527 __ sub(r7, r1, Operand(kHeapObjectTag));
5528 __ vldr(d6, r7, HeapNumber::kValueOffset);
5529 } else {
5530 // Calling convention says that first double is in r0 and r1.
5531 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
5532 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
5533 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005534 __ jmp(&finished_loading_r1);
5535 __ bind(&r1_is_smi);
5536 if (mode == OVERWRITE_LEFT) {
5537 // We can't overwrite a Smi so get address of new heap number into r5.
5538 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5539 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005540
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005541 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005542 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005543 // Convert smi in r1 to double in d6.
5544 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
5545 __ vmov(s13, r7);
5546 __ vcvt(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005547 } else {
5548 // Write Smi from r1 to r1 and r0 in double format.
5549 __ mov(r7, Operand(r1));
5550 ConvertToDoubleStub stub4(r1, r0, r7, r6);
5551 __ push(lr);
5552 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
5553 __ pop(lr);
5554 }
5555
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005556 __ bind(&finished_loading_r1);
5557
5558 __ bind(&do_the_call);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005559 // If we are inlining the operation using VFP3 instructions for
5560 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
5561 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005562 CpuFeatures::Scope scope(VFP3);
5563 // ARMv7 VFP3 instructions to implement
5564 // double precision, add, subtract, multiply, divide.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005565
5566 if (Token::MUL == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005567 __ vmul(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005568 } else if (Token::DIV == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005569 __ vdiv(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005570 } else if (Token::ADD == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005571 __ vadd(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005572 } else if (Token::SUB == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005573 __ vsub(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005574 } else {
5575 UNREACHABLE();
5576 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005577 __ sub(r0, r5, Operand(kHeapObjectTag));
5578 __ vstr(d5, r0, HeapNumber::kValueOffset);
5579 __ add(r0, r0, Operand(kHeapObjectTag));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005580 __ mov(pc, lr);
5581 return;
5582 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005583
5584 // If we did not inline the operation, then the arguments are in:
5585 // r0: Left value (least significant part of mantissa).
5586 // r1: Left value (sign, exponent, top of mantissa).
5587 // r2: Right value (least significant part of mantissa).
5588 // r3: Right value (sign, exponent, top of mantissa).
5589 // r5: Address of heap number for result.
5590
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005591 __ push(lr); // For later.
5592 __ push(r5); // Address of heap number that is answer.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005593 __ AlignStack(0);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005594 // Call C routine that may not cause GC or other trouble.
5595 __ mov(r5, Operand(ExternalReference::double_fp_operation(operation)));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005596 __ Call(r5);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005597 __ pop(r4); // Address of heap number.
5598 __ cmp(r4, Operand(Smi::FromInt(0)));
5599 __ pop(r4, eq); // Conditional pop instruction to get rid of alignment push.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005600 // Store answer in the overwritable heap number.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005601#if !defined(USE_ARM_EABI)
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005602 // Double returned in fp coprocessor register 0 and 1, encoded as register
5603 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
5604 // substract the tag from r4.
5605 __ sub(r5, r4, Operand(kHeapObjectTag));
5606 __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset));
5607#else
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005608 // Double returned in registers 0 and 1.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005609 __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005610 __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005611#endif
5612 __ mov(r0, Operand(r4));
5613 // And we are done.
5614 __ pop(pc);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005615}
5616
5617
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005618// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005619// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005620// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
5621// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005622// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
5623// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005624static void GetInt32(MacroAssembler* masm,
5625 Register source,
5626 Register dest,
5627 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005628 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005629 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005630 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005631 // Get exponent word.
5632 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
5633 // Get exponent alone in scratch2.
5634 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005635 // Load dest with zero. We use this either for the final shift or
5636 // for the answer.
5637 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005638 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005639 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
5640 // the exponent that we are fastest at and also the highest exponent we can
5641 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005642 const uint32_t non_smi_exponent =
5643 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
5644 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005645 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
5646 __ b(eq, &right_exponent);
5647 // If the exponent is higher than that then go to slow case. This catches
5648 // numbers that don't fit in a signed int32, infinities and NaNs.
5649 __ b(gt, slow);
5650
5651 // We know the exponent is smaller than 30 (biased). If it is less than
5652 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
5653 // it rounds to zero.
5654 const uint32_t zero_exponent =
5655 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
5656 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
5657 // Dest already has a Smi zero.
5658 __ b(lt, &done);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005659 if (!CpuFeatures::IsSupported(VFP3)) {
5660 // We have a shifted exponent between 0 and 30 in scratch2.
5661 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
5662 // We now have the exponent in dest. Subtract from 30 to get
5663 // how much to shift down.
5664 __ rsb(dest, dest, Operand(30));
5665 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005666 __ bind(&right_exponent);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005667 if (CpuFeatures::IsSupported(VFP3)) {
5668 CpuFeatures::Scope scope(VFP3);
5669 // ARMv7 VFP3 instructions implementing double precision to integer
5670 // conversion using round to zero.
5671 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005672 __ vmov(d7, scratch2, scratch);
5673 __ vcvt(s15, d7);
5674 __ vmov(dest, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005675 } else {
5676 // Get the top bits of the mantissa.
5677 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
5678 // Put back the implicit 1.
5679 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
5680 // Shift up the mantissa bits to take up the space the exponent used to
5681 // take. We just orred in the implicit bit so that took care of one and
5682 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
5683 // distance.
5684 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
5685 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
5686 // Put sign in zero flag.
5687 __ tst(scratch, Operand(HeapNumber::kSignMask));
5688 // Get the second half of the double. For some exponents we don't
5689 // actually need this because the bits get shifted out again, but
5690 // it's probably slower to test than just to do it.
5691 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
5692 // Shift down 22 bits to get the last 10 bits.
5693 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
5694 // Move down according to the exponent.
5695 __ mov(dest, Operand(scratch, LSR, dest));
5696 // Fix sign if sign bit was set.
5697 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
5698 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005699 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005700}
5701
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005702// For bitwise ops where the inputs are not both Smis we here try to determine
5703// whether both inputs are either Smis or at least heap numbers that can be
5704// represented by a 32 bit signed value. We truncate towards zero as required
5705// by the ES spec. If this is the case we do the bitwise op and see if the
5706// result is a Smi. If so, great, otherwise we try to find a heap number to
5707// write the answer into (either by allocating or by overwriting).
5708// On entry the operands are in r0 and r1. On exit the answer is in r0.
5709void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) {
5710 Label slow, result_not_a_smi;
5711 Label r0_is_smi, r1_is_smi;
5712 Label done_checking_r0, done_checking_r1;
5713
5714 __ tst(r1, Operand(kSmiTagMask));
5715 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5716 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5717 __ b(ne, &slow);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005718 GetInt32(masm, r1, r3, r5, r4, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005719 __ jmp(&done_checking_r1);
5720 __ bind(&r1_is_smi);
5721 __ mov(r3, Operand(r1, ASR, 1));
5722 __ bind(&done_checking_r1);
5723
5724 __ tst(r0, Operand(kSmiTagMask));
5725 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5726 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5727 __ b(ne, &slow);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005728 GetInt32(masm, r0, r2, r5, r4, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005729 __ jmp(&done_checking_r0);
5730 __ bind(&r0_is_smi);
5731 __ mov(r2, Operand(r0, ASR, 1));
5732 __ bind(&done_checking_r0);
5733
5734 // r0 and r1: Original operands (Smi or heap numbers).
5735 // r2 and r3: Signed int32 operands.
5736 switch (op_) {
5737 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
5738 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
5739 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
5740 case Token::SAR:
5741 // Use only the 5 least significant bits of the shift count.
5742 __ and_(r2, r2, Operand(0x1f));
5743 __ mov(r2, Operand(r3, ASR, r2));
5744 break;
5745 case Token::SHR:
5746 // Use only the 5 least significant bits of the shift count.
5747 __ and_(r2, r2, Operand(0x1f));
5748 __ mov(r2, Operand(r3, LSR, r2), SetCC);
5749 // SHR is special because it is required to produce a positive answer.
5750 // The code below for writing into heap numbers isn't capable of writing
5751 // the register as an unsigned int so we go to slow case if we hit this
5752 // case.
5753 __ b(mi, &slow);
5754 break;
5755 case Token::SHL:
5756 // Use only the 5 least significant bits of the shift count.
5757 __ and_(r2, r2, Operand(0x1f));
5758 __ mov(r2, Operand(r3, LSL, r2));
5759 break;
5760 default: UNREACHABLE();
5761 }
5762 // check that the *signed* result fits in a smi
5763 __ add(r3, r2, Operand(0x40000000), SetCC);
5764 __ b(mi, &result_not_a_smi);
5765 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5766 __ Ret();
5767
5768 Label have_to_allocate, got_a_heap_number;
5769 __ bind(&result_not_a_smi);
5770 switch (mode_) {
5771 case OVERWRITE_RIGHT: {
5772 __ tst(r0, Operand(kSmiTagMask));
5773 __ b(eq, &have_to_allocate);
5774 __ mov(r5, Operand(r0));
5775 break;
5776 }
5777 case OVERWRITE_LEFT: {
5778 __ tst(r1, Operand(kSmiTagMask));
5779 __ b(eq, &have_to_allocate);
5780 __ mov(r5, Operand(r1));
5781 break;
5782 }
5783 case NO_OVERWRITE: {
5784 // Get a new heap number in r5. r6 and r7 are scratch.
5785 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5786 }
5787 default: break;
5788 }
5789 __ bind(&got_a_heap_number);
5790 // r2: Answer as signed int32.
5791 // r5: Heap number to write answer into.
5792
5793 // Nothing can go wrong now, so move the heap number to r0, which is the
5794 // result.
5795 __ mov(r0, Operand(r5));
5796
5797 // Tail call that writes the int32 in r2 to the heap number in r0, using
5798 // r3 as scratch. r0 is preserved and returned.
5799 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
5800 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
5801
5802 if (mode_ != NO_OVERWRITE) {
5803 __ bind(&have_to_allocate);
5804 // Get a new heap number in r5. r6 and r7 are scratch.
5805 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5806 __ jmp(&got_a_heap_number);
5807 }
5808
5809 // If all else failed then we go to the runtime system.
5810 __ bind(&slow);
5811 __ push(r1); // restore stack
5812 __ push(r0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005813 switch (op_) {
5814 case Token::BIT_OR:
5815 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
5816 break;
5817 case Token::BIT_AND:
5818 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
5819 break;
5820 case Token::BIT_XOR:
5821 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
5822 break;
5823 case Token::SAR:
5824 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
5825 break;
5826 case Token::SHR:
5827 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
5828 break;
5829 case Token::SHL:
5830 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
5831 break;
5832 default:
5833 UNREACHABLE();
5834 }
5835}
5836
5837
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005838// Can we multiply by x with max two shifts and an add.
5839// This answers yes to all integers from 2 to 10.
5840static bool IsEasyToMultiplyBy(int x) {
5841 if (x < 2) return false; // Avoid special cases.
5842 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
5843 if (IsPowerOf2(x)) return true; // Simple shift.
5844 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
5845 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
5846 return false;
5847}
5848
5849
5850// Can multiply by anything that IsEasyToMultiplyBy returns true for.
5851// Source and destination may be the same register. This routine does
5852// not set carry and overflow the way a mul instruction would.
5853static void MultiplyByKnownInt(MacroAssembler* masm,
5854 Register source,
5855 Register destination,
5856 int known_int) {
5857 if (IsPowerOf2(known_int)) {
5858 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
5859 } else if (PopCountLessThanEqual2(known_int)) {
5860 int first_bit = BitPosition(known_int);
5861 int second_bit = BitPosition(known_int ^ (1 << first_bit));
5862 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
5863 if (first_bit != 0) {
5864 __ mov(destination, Operand(destination, LSL, first_bit));
5865 }
5866 } else {
5867 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
5868 int the_bit = BitPosition(known_int + 1);
5869 __ rsb(destination, source, Operand(source, LSL, the_bit));
5870 }
5871}
5872
5873
5874// This function (as opposed to MultiplyByKnownInt) takes the known int in a
5875// a register for the cases where it doesn't know a good trick, and may deliver
5876// a result that needs shifting.
5877static void MultiplyByKnownInt2(
5878 MacroAssembler* masm,
5879 Register result,
5880 Register source,
5881 Register known_int_register, // Smi tagged.
5882 int known_int,
5883 int* required_shift) { // Including Smi tag shift
5884 switch (known_int) {
5885 case 3:
5886 __ add(result, source, Operand(source, LSL, 1));
5887 *required_shift = 1;
5888 break;
5889 case 5:
5890 __ add(result, source, Operand(source, LSL, 2));
5891 *required_shift = 1;
5892 break;
5893 case 6:
5894 __ add(result, source, Operand(source, LSL, 1));
5895 *required_shift = 2;
5896 break;
5897 case 7:
5898 __ rsb(result, source, Operand(source, LSL, 3));
5899 *required_shift = 1;
5900 break;
5901 case 9:
5902 __ add(result, source, Operand(source, LSL, 3));
5903 *required_shift = 1;
5904 break;
5905 case 10:
5906 __ add(result, source, Operand(source, LSL, 2));
5907 *required_shift = 2;
5908 break;
5909 default:
5910 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
5911 __ mul(result, source, known_int_register);
5912 *required_shift = 0;
5913 }
5914}
5915
5916
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005917const char* GenericBinaryOpStub::GetName() {
5918 if (name_ != NULL) return name_;
5919 const int len = 100;
5920 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
5921 if (name_ == NULL) return "OOM";
5922 const char* op_name = Token::Name(op_);
5923 const char* overwrite_name;
5924 switch (mode_) {
5925 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
5926 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
5927 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
5928 default: overwrite_name = "UnknownOverwrite"; break;
5929 }
5930
5931 OS::SNPrintF(Vector<char>(name_, len),
5932 "GenericBinaryOpStub_%s_%s%s",
5933 op_name,
5934 overwrite_name,
5935 specialized_on_rhs_ ? "_ConstantRhs" : 0);
5936 return name_;
5937}
5938
5939
ager@chromium.org5c838252010-02-19 08:53:10 +00005940
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005941void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
5942 // r1 : x
5943 // r0 : y
5944 // result : r0
5945
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005946 // All ops need to know whether we are dealing with two Smis. Set up r2 to
5947 // tell us that.
5948 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
5949
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005950 switch (op_) {
5951 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005952 Label not_smi;
5953 // Fast path.
5954 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005955 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005956 __ b(ne, &not_smi);
5957 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
5958 // Return if no overflow.
5959 __ Ret(vc);
5960 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
5961
5962 HandleBinaryOpSlowCases(masm,
5963 &not_smi,
5964 Builtins::ADD,
5965 Token::ADD,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005966 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005967 break;
5968 }
5969
5970 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005971 Label not_smi;
5972 // Fast path.
5973 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005974 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005975 __ b(ne, &not_smi);
5976 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
5977 // Return if no overflow.
5978 __ Ret(vc);
5979 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
5980
5981 HandleBinaryOpSlowCases(masm,
5982 &not_smi,
5983 Builtins::SUB,
5984 Token::SUB,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005985 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005986 break;
5987 }
5988
5989 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005990 Label not_smi, slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005991 ASSERT(kSmiTag == 0); // adjust code below
5992 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005993 __ b(ne, &not_smi);
5994 // Remove tag from one operand (but keep sign), so that result is Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005995 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005996 // Do multiplication
5997 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1.
5998 // Go slow on overflows (overflow bit is not set).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005999 __ mov(ip, Operand(r3, ASR, 31));
6000 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
6001 __ b(ne, &slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006002 // Go slow on zero result to handle -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006003 __ tst(r3, Operand(r3));
6004 __ mov(r0, Operand(r3), LeaveCC, ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006005 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006006 // We need -0 if we were multiplying a negative number with 0 to get 0.
6007 // We know one of them was zero.
6008 __ add(r2, r0, Operand(r1), SetCC);
6009 __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl);
6010 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
6011 // Slow case. We fall through here if we multiplied a negative number
6012 // with 0, because that would mean we should produce -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006013 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006014
6015 HandleBinaryOpSlowCases(masm,
6016 &not_smi,
6017 Builtins::MUL,
6018 Token::MUL,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006019 mode_);
6020 break;
6021 }
6022
6023 case Token::DIV:
6024 case Token::MOD: {
6025 Label not_smi;
6026 if (specialized_on_rhs_) {
6027 Label smi_is_unsuitable;
6028 __ BranchOnNotSmi(r1, &not_smi);
6029 if (IsPowerOf2(constant_rhs_)) {
6030 if (op_ == Token::MOD) {
6031 __ and_(r0,
6032 r1,
6033 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
6034 SetCC);
6035 // We now have the answer, but if the input was negative we also
6036 // have the sign bit. Our work is done if the result is
6037 // positive or zero:
6038 __ Ret(pl);
6039 // A mod of a negative left hand side must return a negative number.
6040 // Unfortunately if the answer is 0 then we must return -0. And we
6041 // already optimistically trashed r0 so we may need to restore it.
6042 __ eor(r0, r0, Operand(0x80000000u), SetCC);
6043 // Next two instructions are conditional on the answer being -0.
6044 __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
6045 __ b(eq, &smi_is_unsuitable);
6046 // We need to subtract the dividend. Eg. -3 % 4 == -3.
6047 __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_)));
6048 } else {
6049 ASSERT(op_ == Token::DIV);
6050 __ tst(r1,
6051 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
6052 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
6053 int shift = 0;
6054 int d = constant_rhs_;
6055 while ((d & 1) == 0) {
6056 d >>= 1;
6057 shift++;
6058 }
6059 __ mov(r0, Operand(r1, LSR, shift));
6060 __ bic(r0, r0, Operand(kSmiTagMask));
6061 }
6062 } else {
6063 // Not a power of 2.
6064 __ tst(r1, Operand(0x80000000u));
6065 __ b(ne, &smi_is_unsuitable);
6066 // Find a fixed point reciprocal of the divisor so we can divide by
6067 // multiplying.
6068 double divisor = 1.0 / constant_rhs_;
6069 int shift = 32;
6070 double scale = 4294967296.0; // 1 << 32.
6071 uint32_t mul;
6072 // Maximise the precision of the fixed point reciprocal.
6073 while (true) {
6074 mul = static_cast<uint32_t>(scale * divisor);
6075 if (mul >= 0x7fffffff) break;
6076 scale *= 2.0;
6077 shift++;
6078 }
6079 mul++;
6080 __ mov(r2, Operand(mul));
6081 __ umull(r3, r2, r2, r1);
6082 __ mov(r2, Operand(r2, LSR, shift - 31));
6083 // r2 is r1 / rhs. r2 is not Smi tagged.
6084 // r0 is still the known rhs. r0 is Smi tagged.
6085 // r1 is still the unkown lhs. r1 is Smi tagged.
6086 int required_r4_shift = 0; // Including the Smi tag shift of 1.
6087 // r4 = r2 * r0.
6088 MultiplyByKnownInt2(masm,
6089 r4,
6090 r2,
6091 r0,
6092 constant_rhs_,
6093 &required_r4_shift);
6094 // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs).
6095 if (op_ == Token::DIV) {
6096 __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC);
6097 __ b(ne, &smi_is_unsuitable); // There was a remainder.
6098 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
6099 } else {
6100 ASSERT(op_ == Token::MOD);
6101 __ sub(r0, r1, Operand(r4, LSL, required_r4_shift));
6102 }
6103 }
6104 __ Ret();
6105 __ bind(&smi_is_unsuitable);
6106 } else {
6107 __ jmp(&not_smi);
6108 }
6109 HandleBinaryOpSlowCases(masm,
6110 &not_smi,
6111 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV,
6112 op_,
6113 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006114 break;
6115 }
6116
6117 case Token::BIT_OR:
6118 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006119 case Token::BIT_XOR:
6120 case Token::SAR:
6121 case Token::SHR:
6122 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006123 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006124 ASSERT(kSmiTag == 0); // adjust code below
6125 __ tst(r2, Operand(kSmiTagMask));
6126 __ b(ne, &slow);
6127 switch (op_) {
6128 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
6129 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
6130 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006131 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006132 // Remove tags from right operand.
ager@chromium.org5c838252010-02-19 08:53:10 +00006133 __ GetLeastBitsFromSmi(r2, r0, 5);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006134 __ mov(r0, Operand(r1, ASR, r2));
6135 // Smi tag result.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006136 __ bic(r0, r0, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006137 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006138 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006139 // Remove tags from operands. We can't do this on a 31 bit number
6140 // because then the 0s get shifted into bit 30 instead of bit 31.
6141 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
ager@chromium.org5c838252010-02-19 08:53:10 +00006142 __ GetLeastBitsFromSmi(r2, r0, 5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006143 __ mov(r3, Operand(r3, LSR, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006144 // Unsigned shift is not allowed to produce a negative number, so
6145 // check the sign bit and the sign bit after Smi tagging.
6146 __ tst(r3, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006147 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006148 // Smi tag result.
6149 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006150 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006151 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006152 // Remove tags from operands.
6153 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
ager@chromium.org5c838252010-02-19 08:53:10 +00006154 __ GetLeastBitsFromSmi(r2, r0, 5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006155 __ mov(r3, Operand(r3, LSL, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006156 // Check that the signed result fits in a Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006157 __ add(r2, r3, Operand(0x40000000), SetCC);
6158 __ b(mi, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006159 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006160 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006161 default: UNREACHABLE();
6162 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006163 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006164 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006165 HandleNonSmiBitwiseOp(masm);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006166 break;
6167 }
6168
6169 default: UNREACHABLE();
6170 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006171 // This code should be unreachable.
6172 __ stop("Unreachable");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006173}
6174
6175
6176void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00006177 // Do tail-call to runtime routine. Runtime routines expect at least one
6178 // argument, so give it a Smi.
6179 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006180 __ push(r0);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006181 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006182
6183 __ StubReturn(1);
6184}
6185
6186
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006187void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006188 Label slow, done;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006189
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006190 if (op_ == Token::SUB) {
6191 // Check whether the value is a smi.
6192 Label try_float;
6193 __ tst(r0, Operand(kSmiTagMask));
6194 __ b(ne, &try_float);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006195
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006196 // Go slow case if the value of the expression is zero
6197 // to make sure that we switch between 0 and -0.
6198 __ cmp(r0, Operand(0));
6199 __ b(eq, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006200
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006201 // The value of the expression is a smi that is not zero. Try
6202 // optimistic subtraction '0 - value'.
6203 __ rsb(r1, r0, Operand(0), SetCC);
6204 __ b(vs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006205
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006206 __ mov(r0, Operand(r1)); // Set r0 to result.
6207 __ b(&done);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006208
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006209 __ bind(&try_float);
6210 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
6211 __ b(ne, &slow);
6212 // r0 is a heap number. Get a new heap number in r1.
6213 if (overwrite_) {
6214 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6215 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
6216 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6217 } else {
6218 AllocateHeapNumber(masm, &slow, r1, r2, r3);
6219 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
6220 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6221 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
6222 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
6223 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
6224 __ mov(r0, Operand(r1));
6225 }
6226 } else if (op_ == Token::BIT_NOT) {
6227 // Check if the operand is a heap number.
6228 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
6229 __ b(ne, &slow);
6230
6231 // Convert the heap number is r0 to an untagged integer in r1.
6232 GetInt32(masm, r0, r1, r2, r3, &slow);
6233
6234 // Do the bitwise operation (move negated) and check if the result
6235 // fits in a smi.
6236 Label try_float;
6237 __ mvn(r1, Operand(r1));
6238 __ add(r2, r1, Operand(0x40000000), SetCC);
6239 __ b(mi, &try_float);
6240 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
6241 __ b(&done);
6242
6243 __ bind(&try_float);
6244 if (!overwrite_) {
6245 // Allocate a fresh heap number, but don't overwrite r0 until
6246 // we're sure we can do it without going through the slow case
6247 // that needs the value in r0.
6248 AllocateHeapNumber(masm, &slow, r2, r3, r4);
6249 __ mov(r0, Operand(r2));
6250 }
6251
6252 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
6253 // have to set up a frame.
6254 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
6255 __ push(lr);
6256 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
6257 __ pop(lr);
6258 } else {
6259 UNIMPLEMENTED();
6260 }
6261
6262 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006263 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006264
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006265 // Handle the slow case by jumping to the JavaScript builtin.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006266 __ bind(&slow);
6267 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006268 switch (op_) {
6269 case Token::SUB:
6270 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
6271 break;
6272 case Token::BIT_NOT:
6273 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
6274 break;
6275 default:
6276 UNREACHABLE();
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006277 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00006278}
6279
6280
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006281void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006282 // r0 holds the exception.
6283
6284 // Adjust this code if not the case.
6285 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
6286
6287 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006288 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
6289 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006290
6291 // Restore the next handler and frame pointer, discard handler state.
6292 ASSERT(StackHandlerConstants::kNextOffset == 0);
6293 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006294 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006295 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
6296 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
6297
6298 // Before returning we restore the context from the frame pointer if
6299 // not NULL. The frame pointer is NULL in the exception handler of a
6300 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006301 __ cmp(fp, Operand(0));
6302 // Set cp to NULL if fp is NULL.
6303 __ mov(cp, Operand(0), LeaveCC, eq);
6304 // Restore cp otherwise.
6305 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006306#ifdef DEBUG
6307 if (FLAG_debug_code) {
6308 __ mov(lr, Operand(pc));
6309 }
6310#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006311 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006312 __ pop(pc);
6313}
6314
6315
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006316void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
6317 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006318 // Adjust this code if not the case.
6319 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
6320
6321 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006322 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006323 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006324
6325 // Unwind the handlers until the ENTRY handler is found.
6326 Label loop, done;
6327 __ bind(&loop);
6328 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006329 const int kStateOffset = StackHandlerConstants::kStateOffset;
6330 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006331 __ cmp(r2, Operand(StackHandler::ENTRY));
6332 __ b(eq, &done);
6333 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006334 const int kNextOffset = StackHandlerConstants::kNextOffset;
6335 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006336 __ jmp(&loop);
6337 __ bind(&done);
6338
6339 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006340 ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006341 __ pop(r2);
6342 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006343
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006344 if (type == OUT_OF_MEMORY) {
6345 // Set external caught exception to false.
6346 ExternalReference external_caught(Top::k_external_caught_exception_address);
6347 __ mov(r0, Operand(false));
6348 __ mov(r2, Operand(external_caught));
6349 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006350
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006351 // Set pending exception and r0 to out of memory exception.
6352 Failure* out_of_memory = Failure::OutOfMemoryException();
6353 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
6354 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
6355 __ str(r0, MemOperand(r2));
6356 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006357
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006358 // Stack layout at this point. See also StackHandlerConstants.
6359 // sp -> state (ENTRY)
6360 // fp
6361 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006362
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006363 // Discard handler state (r2 is not used) and restore frame pointer.
6364 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
6365 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
6366 // Before returning we restore the context from the frame pointer if
6367 // not NULL. The frame pointer is NULL in the exception handler of a
6368 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006369 __ cmp(fp, Operand(0));
6370 // Set cp to NULL if fp is NULL.
6371 __ mov(cp, Operand(0), LeaveCC, eq);
6372 // Restore cp otherwise.
6373 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006374#ifdef DEBUG
6375 if (FLAG_debug_code) {
6376 __ mov(lr, Operand(pc));
6377 }
6378#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006379 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006380 __ pop(pc);
6381}
6382
6383
6384void CEntryStub::GenerateCore(MacroAssembler* masm,
6385 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006386 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006387 Label* throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006388 bool do_gc,
6389 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006390 // r0: result parameter for PerformGC, if any
6391 // r4: number of arguments including receiver (C callee-saved)
6392 // r5: pointer to builtin function (C callee-saved)
6393 // r6: pointer to the first argument (C callee-saved)
6394
6395 if (do_gc) {
6396 // Passing r0.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006397 ExternalReference gc_reference = ExternalReference::perform_gc_function();
6398 __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006399 }
6400
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006401 ExternalReference scope_depth =
6402 ExternalReference::heap_always_allocate_scope_depth();
6403 if (always_allocate) {
6404 __ mov(r0, Operand(scope_depth));
6405 __ ldr(r1, MemOperand(r0));
6406 __ add(r1, r1, Operand(1));
6407 __ str(r1, MemOperand(r0));
6408 }
6409
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006410 // Call C built-in.
6411 // r0 = argc, r1 = argv
6412 __ mov(r0, Operand(r4));
6413 __ mov(r1, Operand(r6));
6414
6415 // TODO(1242173): To let the GC traverse the return address of the exit
6416 // frames, we need to know where the return address is. Right now,
6417 // we push it on the stack to be able to find it again, but we never
6418 // restore from it in case of changes, which makes it impossible to
6419 // support moving the C entry code stub. This should be fixed, but currently
6420 // this is OK because the CEntryStub gets generated so early in the V8 boot
6421 // sequence that it is not moving ever.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006422 masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
6423 masm->push(lr);
6424 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006425
6426 if (always_allocate) {
6427 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
6428 // though (contain the result).
6429 __ mov(r2, Operand(scope_depth));
6430 __ ldr(r3, MemOperand(r2));
6431 __ sub(r3, r3, Operand(1));
6432 __ str(r3, MemOperand(r2));
6433 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006434
6435 // check for failure result
6436 Label failure_returned;
6437 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
6438 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
6439 __ add(r2, r0, Operand(1));
6440 __ tst(r2, Operand(kFailureTagMask));
6441 __ b(eq, &failure_returned);
6442
6443 // Exit C frame and return.
6444 // r0:r1: result
6445 // sp: stack pointer
6446 // fp: frame pointer
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006447 __ LeaveExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006448
6449 // check if we should retry or throw exception
6450 Label retry;
6451 __ bind(&failure_returned);
6452 ASSERT(Failure::RETRY_AFTER_GC == 0);
6453 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
6454 __ b(eq, &retry);
6455
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006456 // Special handling of out of memory exceptions.
6457 Failure* out_of_memory = Failure::OutOfMemoryException();
6458 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
6459 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006460
6461 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00006462 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006463 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00006464 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006465 __ ldr(r0, MemOperand(ip));
6466 __ str(r3, MemOperand(ip));
6467
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006468 // Special handling of termination exceptions which are uncatchable
6469 // by javascript code.
6470 __ cmp(r0, Operand(Factory::termination_exception()));
6471 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006472
6473 // Handle normal exception.
6474 __ jmp(throw_normal_exception);
6475
6476 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
6477}
6478
6479
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006480void CEntryStub::Generate(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006481 // Called from JavaScript; parameters are on stack as if calling JS function
6482 // r0: number of arguments including receiver
6483 // r1: pointer to builtin function
6484 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006485 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006486 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006487
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006488 // Result returned in r0 or r0+r1 by default.
6489
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006490 // NOTE: Invocations of builtins may return failure objects
6491 // instead of a proper result. The builtin entry handles
6492 // this by performing a garbage collection and retrying the
6493 // builtin once.
6494
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006495 // Enter the exit frame that transitions from JavaScript to C++.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006496 __ EnterExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006497
6498 // r4: number of arguments (C callee-saved)
6499 // r5: pointer to builtin function (C callee-saved)
6500 // r6: pointer to first argument (C callee-saved)
6501
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006502 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006503 Label throw_termination_exception;
6504 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006505
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00006506 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006507 GenerateCore(masm,
6508 &throw_normal_exception,
6509 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006510 &throw_out_of_memory_exception,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00006511 false,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006512 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006513
6514 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006515 GenerateCore(masm,
6516 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006517 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006518 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006519 true,
6520 false);
6521
6522 // Do full GC and retry runtime call one final time.
6523 Failure* failure = Failure::InternalError();
6524 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
6525 GenerateCore(masm,
6526 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006527 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006528 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006529 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006530 true);
6531
6532 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006533 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
6534
6535 __ bind(&throw_termination_exception);
6536 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006537
6538 __ bind(&throw_normal_exception);
6539 GenerateThrowTOS(masm);
6540}
6541
6542
6543void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
6544 // r0: code entry
6545 // r1: function
6546 // r2: receiver
6547 // r3: argc
6548 // [sp+0]: argv
6549
6550 Label invoke, exit;
6551
6552 // Called from C, so do not pop argc and args on exit (preserve sp)
6553 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006554 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006555 __ stm(db_w, sp, kCalleeSaved | lr.bit());
6556
6557 // Get address of argv, see stm above.
6558 // r0: code entry
6559 // r1: function
6560 // r2: receiver
6561 // r3: argc
ager@chromium.org5c838252010-02-19 08:53:10 +00006562 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006563
6564 // Push a frame with special values setup to mark it as an entry frame.
6565 // r0: code entry
6566 // r1: function
6567 // r2: receiver
6568 // r3: argc
6569 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006570 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006571 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
6572 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006573 __ mov(r6, Operand(Smi::FromInt(marker)));
6574 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6575 __ ldr(r5, MemOperand(r5));
6576 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
6577
6578 // Setup frame pointer for the frame to be pushed.
6579 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6580
6581 // Call a faked try-block that does the invoke.
6582 __ bl(&invoke);
6583
6584 // Caught exception: Store result (exception) in the pending
6585 // exception field in the JSEnv and return a failure sentinel.
6586 // Coming in here the fp will be invalid because the PushTryHandler below
6587 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00006588 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006589 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006590 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006591 __ b(&exit);
6592
6593 // Invoke: Link this frame into the handler chain.
6594 __ bind(&invoke);
6595 // Must preserve r0-r4, r5-r7 are available.
6596 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006597 // If an exception not caught by another handler occurs, this handler
6598 // returns control to the code after the bl(&invoke) above, which
6599 // restores all kCalleeSaved registers (including cp and fp) to their
6600 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006601
6602 // Clear any pending exceptions.
6603 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
6604 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00006605 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006606 __ str(r5, MemOperand(ip));
6607
6608 // Invoke the function by calling through JS entry trampoline builtin.
6609 // Notice that we cannot store a reference to the trampoline code directly in
6610 // this stub, because runtime stubs are not traversed when doing GC.
6611
6612 // Expected registers by Builtins::JSEntryTrampoline
6613 // r0: code entry
6614 // r1: function
6615 // r2: receiver
6616 // r3: argc
6617 // r4: argv
6618 if (is_construct) {
6619 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
6620 __ mov(ip, Operand(construct_entry));
6621 } else {
6622 ExternalReference entry(Builtins::JSEntryTrampoline);
6623 __ mov(ip, Operand(entry));
6624 }
6625 __ ldr(ip, MemOperand(ip)); // deref address
6626
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006627 // Branch and link to JSEntryTrampoline. We don't use the double underscore
6628 // macro for the add instruction because we don't want the coverage tool
6629 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006630 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006631 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006632
6633 // Unlink this frame from the handler chain. When reading the
6634 // address of the next handler, there is no need to use the address
6635 // displacement since the current stack pointer (sp) points directly
6636 // to the stack handler.
6637 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
6638 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
6639 __ str(r3, MemOperand(ip));
6640 // No need to restore registers
6641 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
6642
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006643
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006644 __ bind(&exit); // r0 holds result
6645 // Restore the top frame descriptors from the stack.
6646 __ pop(r3);
6647 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6648 __ str(r3, MemOperand(ip));
6649
6650 // Reset the stack to the callee saved registers.
6651 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6652
6653 // Restore callee-saved registers and return.
6654#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006655 if (FLAG_debug_code) {
6656 __ mov(lr, Operand(pc));
6657 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006658#endif
6659 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
6660}
6661
6662
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006663// This stub performs an instanceof, calling the builtin function if
6664// necessary. Uses r1 for the object, r0 for the function that it may
6665// be an instance of (these are fetched from the stack).
6666void InstanceofStub::Generate(MacroAssembler* masm) {
6667 // Get the object - slow case for smis (we may need to throw an exception
6668 // depending on the rhs).
6669 Label slow, loop, is_instance, is_not_instance;
6670 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
6671 __ BranchOnSmi(r0, &slow);
6672
6673 // Check that the left hand is a JS object and put map in r3.
6674 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
6675 __ b(lt, &slow);
6676 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
6677 __ b(gt, &slow);
6678
6679 // Get the prototype of the function (r4 is result, r2 is scratch).
ager@chromium.org5c838252010-02-19 08:53:10 +00006680 __ ldr(r1, MemOperand(sp, 0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006681 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
6682
6683 // Check that the function prototype is a JS object.
6684 __ BranchOnSmi(r4, &slow);
6685 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
6686 __ b(lt, &slow);
6687 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
6688 __ b(gt, &slow);
6689
6690 // Register mapping: r3 is object map and r4 is function prototype.
6691 // Get prototype of object into r2.
6692 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
6693
6694 // Loop through the prototype chain looking for the function prototype.
6695 __ bind(&loop);
6696 __ cmp(r2, Operand(r4));
6697 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006698 __ LoadRoot(ip, Heap::kNullValueRootIndex);
6699 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006700 __ b(eq, &is_not_instance);
6701 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
6702 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
6703 __ jmp(&loop);
6704
6705 __ bind(&is_instance);
6706 __ mov(r0, Operand(Smi::FromInt(0)));
6707 __ pop();
6708 __ pop();
6709 __ mov(pc, Operand(lr)); // Return.
6710
6711 __ bind(&is_not_instance);
6712 __ mov(r0, Operand(Smi::FromInt(1)));
6713 __ pop();
6714 __ pop();
6715 __ mov(pc, Operand(lr)); // Return.
6716
6717 // Slow-case. Tail call builtin.
6718 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006719 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
6720}
6721
6722
ager@chromium.org7c537e22008-10-16 08:43:32 +00006723void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006724 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006725 Label adaptor;
6726 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6727 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006728 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006729 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006730
ager@chromium.org7c537e22008-10-16 08:43:32 +00006731 // Nothing to do: The formal number of parameters has already been
6732 // passed in register r0 by calling function. Just return it.
ager@chromium.org9085a012009-05-11 19:22:57 +00006733 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006734
ager@chromium.org7c537e22008-10-16 08:43:32 +00006735 // Arguments adaptor case: Read the arguments length from the
6736 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006737 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006738 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org9085a012009-05-11 19:22:57 +00006739 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006740}
6741
6742
ager@chromium.org7c537e22008-10-16 08:43:32 +00006743void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
6744 // The displacement is the offset of the last parameter (if any)
6745 // relative to the frame pointer.
6746 static const int kDisplacement =
6747 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006748
ager@chromium.org7c537e22008-10-16 08:43:32 +00006749 // Check that the key is a smi.
6750 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006751 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006752
ager@chromium.org7c537e22008-10-16 08:43:32 +00006753 // Check if the calling frame is an arguments adaptor frame.
6754 Label adaptor;
6755 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6756 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006757 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006758 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006759
ager@chromium.org7c537e22008-10-16 08:43:32 +00006760 // Check index against formal parameters count limit passed in
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006761 // through register r0. Use unsigned comparison to get negative
ager@chromium.org7c537e22008-10-16 08:43:32 +00006762 // check for free.
6763 __ cmp(r1, r0);
6764 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006765
ager@chromium.org7c537e22008-10-16 08:43:32 +00006766 // Read the argument from the stack and return it.
6767 __ sub(r3, r0, r1);
6768 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6769 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006770 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006771
6772 // Arguments adaptor case: Check index against actual arguments
6773 // limit found in the arguments adaptor frame. Use unsigned
6774 // comparison to get negative check for free.
6775 __ bind(&adaptor);
6776 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6777 __ cmp(r1, r0);
6778 __ b(cs, &slow);
6779
6780 // Read the argument from the adaptor frame and return it.
6781 __ sub(r3, r0, r1);
6782 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6783 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006784 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006785
6786 // Slow-case: Handle non-smi or out-of-bounds access to arguments
6787 // by calling the runtime system.
6788 __ bind(&slow);
6789 __ push(r1);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006790 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006791}
6792
6793
6794void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
ager@chromium.org5c838252010-02-19 08:53:10 +00006795 // sp[0] : number of parameters
6796 // sp[4] : receiver displacement
6797 // sp[8] : function
6798
ager@chromium.org7c537e22008-10-16 08:43:32 +00006799 // Check if the calling frame is an arguments adaptor frame.
ager@chromium.org5c838252010-02-19 08:53:10 +00006800 Label adaptor_frame, try_allocate, runtime;
ager@chromium.org7c537e22008-10-16 08:43:32 +00006801 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6802 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006803 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org5c838252010-02-19 08:53:10 +00006804 __ b(eq, &adaptor_frame);
6805
6806 // Get the length from the frame.
6807 __ ldr(r1, MemOperand(sp, 0));
6808 __ b(&try_allocate);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006809
6810 // Patch the arguments.length and the parameters pointer.
ager@chromium.org5c838252010-02-19 08:53:10 +00006811 __ bind(&adaptor_frame);
6812 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6813 __ str(r1, MemOperand(sp, 0));
6814 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006815 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
6816 __ str(r3, MemOperand(sp, 1 * kPointerSize));
6817
ager@chromium.org5c838252010-02-19 08:53:10 +00006818 // Try the new space allocation. Start out with computing the size
6819 // of the arguments object and the elements array (in words, not
6820 // bytes because AllocateInNewSpace expects words).
6821 Label add_arguments_object;
6822 __ bind(&try_allocate);
6823 __ cmp(r1, Operand(0));
6824 __ b(eq, &add_arguments_object);
6825 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
6826 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
6827 __ bind(&add_arguments_object);
6828 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
6829
6830 // Do the allocation of both objects in one go.
6831 __ AllocateInNewSpace(r1, r0, r2, r3, &runtime, TAG_OBJECT);
6832
6833 // Get the arguments boilerplate from the current (global) context.
6834 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
6835 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
6836 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
6837 __ ldr(r4, MemOperand(r4, offset));
6838
6839 // Copy the JS object part.
6840 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
6841 __ ldr(r3, FieldMemOperand(r4, i));
6842 __ str(r3, FieldMemOperand(r0, i));
6843 }
6844
6845 // Setup the callee in-object property.
6846 ASSERT(Heap::arguments_callee_index == 0);
6847 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
6848 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
6849
6850 // Get the length (smi tagged) and set that as an in-object property too.
6851 ASSERT(Heap::arguments_length_index == 1);
6852 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
6853 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
6854
6855 // If there are no actual arguments, we're done.
6856 Label done;
6857 __ cmp(r1, Operand(0));
6858 __ b(eq, &done);
6859
6860 // Get the parameters pointer from the stack and untag the length.
6861 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
6862 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
6863
6864 // Setup the elements pointer in the allocated arguments object and
6865 // initialize the header in the elements fixed array.
6866 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
6867 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
6868 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
6869 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
6870 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
6871
6872 // Copy the fixed array slots.
6873 Label loop;
6874 // Setup r4 to point to the first array slot.
6875 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
6876 __ bind(&loop);
6877 // Pre-decrement r2 with kPointerSize on each iteration.
6878 // Pre-decrement in order to skip receiver.
6879 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
6880 // Post-increment r4 with kPointerSize on each iteration.
6881 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
6882 __ sub(r1, r1, Operand(1));
6883 __ cmp(r1, Operand(0));
6884 __ b(ne, &loop);
6885
6886 // Return and remove the on-stack parameters.
6887 __ bind(&done);
6888 __ add(sp, sp, Operand(3 * kPointerSize));
6889 __ Ret();
6890
ager@chromium.org7c537e22008-10-16 08:43:32 +00006891 // Do the runtime call to allocate the arguments object.
6892 __ bind(&runtime);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006893 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006894}
6895
6896
6897void CallFunctionStub::Generate(MacroAssembler* masm) {
6898 Label slow;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006899
6900 // If the receiver might be a value (string, number or boolean) check for this
6901 // and box it if it is.
6902 if (ReceiverMightBeValue()) {
6903 // Get the receiver from the stack.
6904 // function, receiver [, arguments]
6905 Label receiver_is_value, receiver_is_js_object;
6906 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
6907
6908 // Check if receiver is a smi (which is a number value).
6909 __ BranchOnSmi(r1, &receiver_is_value);
6910
6911 // Check if the receiver is a valid JS object.
6912 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
6913 __ b(ge, &receiver_is_js_object);
6914
6915 // Call the runtime to box the value.
6916 __ bind(&receiver_is_value);
6917 __ EnterInternalFrame();
6918 __ push(r1);
6919 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
6920 __ LeaveInternalFrame();
6921 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
6922
6923 __ bind(&receiver_is_js_object);
6924 }
6925
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006926 // Get the function to call from the stack.
6927 // function, receiver [, arguments]
6928 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
6929
6930 // Check that the function is really a JavaScript function.
6931 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006932 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006933 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006934 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006935 __ b(ne, &slow);
6936
6937 // Fast-case: Invoke the function now.
6938 // r1: pushed function
6939 ParameterCount actual(argc_);
6940 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
6941
6942 // Slow-case: Non-function called.
6943 __ bind(&slow);
ager@chromium.org5c838252010-02-19 08:53:10 +00006944 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
6945 // of the original receiver from the call site).
6946 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006947 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006948 __ mov(r2, Operand(0));
6949 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
6950 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
6951 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006952}
6953
6954
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006955const char* CompareStub::GetName() {
6956 switch (cc_) {
6957 case lt: return "CompareStub_LT";
6958 case gt: return "CompareStub_GT";
6959 case le: return "CompareStub_LE";
6960 case ge: return "CompareStub_GE";
6961 case ne: {
6962 if (strict_) {
6963 if (never_nan_nan_) {
6964 return "CompareStub_NE_STRICT_NO_NAN";
6965 } else {
6966 return "CompareStub_NE_STRICT";
6967 }
6968 } else {
6969 if (never_nan_nan_) {
6970 return "CompareStub_NE_NO_NAN";
6971 } else {
6972 return "CompareStub_NE";
6973 }
6974 }
6975 }
6976 case eq: {
6977 if (strict_) {
6978 if (never_nan_nan_) {
6979 return "CompareStub_EQ_STRICT_NO_NAN";
6980 } else {
6981 return "CompareStub_EQ_STRICT";
6982 }
6983 } else {
6984 if (never_nan_nan_) {
6985 return "CompareStub_EQ_NO_NAN";
6986 } else {
6987 return "CompareStub_EQ";
6988 }
6989 }
6990 }
6991 default: return "CompareStub";
6992 }
6993}
6994
6995
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006996int CompareStub::MinorKey() {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006997 // Encode the three parameters in a unique 16 bit value.
6998 ASSERT((static_cast<unsigned>(cc_) >> 26) < (1 << 16));
6999 int nnn_value = (never_nan_nan_ ? 2 : 0);
7000 if (cc_ != eq) nnn_value = 0; // Avoid duplicate stubs.
7001 return (static_cast<unsigned>(cc_) >> 26) | nnn_value | (strict_ ? 1 : 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007002}
7003
7004
ager@chromium.org5c838252010-02-19 08:53:10 +00007005void StringStubBase::GenerateCopyCharacters(MacroAssembler* masm,
7006 Register dest,
7007 Register src,
7008 Register count,
7009 Register scratch,
7010 bool ascii) {
7011 Label loop;
7012 Label done;
7013 // This loop just copies one character at a time, as it is only used for very
7014 // short strings.
7015 if (!ascii) {
7016 __ add(count, count, Operand(count), SetCC);
7017 } else {
7018 __ cmp(count, Operand(0));
7019 }
7020 __ b(eq, &done);
7021
7022 __ bind(&loop);
7023 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
7024 // Perform sub between load and dependent store to get the load time to
7025 // complete.
7026 __ sub(count, count, Operand(1), SetCC);
7027 __ strb(scratch, MemOperand(dest, 1, PostIndex));
7028 // last iteration.
7029 __ b(gt, &loop);
7030
7031 __ bind(&done);
7032}
7033
7034
7035enum CopyCharactersFlags {
7036 COPY_ASCII = 1,
7037 DEST_ALWAYS_ALIGNED = 2
7038};
7039
7040
7041void StringStubBase::GenerateCopyCharactersLong(MacroAssembler* masm,
7042 Register dest,
7043 Register src,
7044 Register count,
7045 Register scratch1,
7046 Register scratch2,
7047 Register scratch3,
7048 Register scratch4,
7049 Register scratch5,
7050 int flags) {
7051 bool ascii = (flags & COPY_ASCII) != 0;
7052 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
7053
7054 if (dest_always_aligned && FLAG_debug_code) {
7055 // Check that destination is actually word aligned if the flag says
7056 // that it is.
7057 __ tst(dest, Operand(kPointerAlignmentMask));
7058 __ Check(eq, "Destination of copy not aligned.");
7059 }
7060
7061 const int kReadAlignment = 4;
7062 const int kReadAlignmentMask = kReadAlignment - 1;
7063 // Ensure that reading an entire aligned word containing the last character
7064 // of a string will not read outside the allocated area (because we pad up
7065 // to kObjectAlignment).
7066 ASSERT(kObjectAlignment >= kReadAlignment);
7067 // Assumes word reads and writes are little endian.
7068 // Nothing to do for zero characters.
7069 Label done;
7070 if (!ascii) {
7071 __ add(count, count, Operand(count), SetCC);
7072 } else {
7073 __ cmp(count, Operand(0));
7074 }
7075 __ b(eq, &done);
7076
7077 // Assume that you cannot read (or write) unaligned.
7078 Label byte_loop;
7079 // Must copy at least eight bytes, otherwise just do it one byte at a time.
7080 __ cmp(count, Operand(8));
7081 __ add(count, dest, Operand(count));
7082 Register limit = count; // Read until src equals this.
7083 __ b(lt, &byte_loop);
7084
7085 if (!dest_always_aligned) {
7086 // Align dest by byte copying. Copies between zero and three bytes.
7087 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
7088 Label dest_aligned;
7089 __ b(eq, &dest_aligned);
7090 __ cmp(scratch4, Operand(2));
7091 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
7092 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
7093 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
7094 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
7095 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
7096 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
7097 __ bind(&dest_aligned);
7098 }
7099
7100 Label simple_loop;
7101
7102 __ sub(scratch4, dest, Operand(src));
7103 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
7104 __ b(eq, &simple_loop);
7105 // Shift register is number of bits in a source word that
7106 // must be combined with bits in the next source word in order
7107 // to create a destination word.
7108
7109 // Complex loop for src/dst that are not aligned the same way.
7110 {
7111 Label loop;
7112 __ mov(scratch4, Operand(scratch4, LSL, 3));
7113 Register left_shift = scratch4;
7114 __ and_(src, src, Operand(~3)); // Round down to load previous word.
7115 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
7116 // Store the "shift" most significant bits of scratch in the least
7117 // signficant bits (i.e., shift down by (32-shift)).
7118 __ rsb(scratch2, left_shift, Operand(32));
7119 Register right_shift = scratch2;
7120 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
7121
7122 __ bind(&loop);
7123 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
7124 __ sub(scratch5, limit, Operand(dest));
7125 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
7126 __ str(scratch1, MemOperand(dest, 4, PostIndex));
7127 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
7128 // Loop if four or more bytes left to copy.
7129 // Compare to eight, because we did the subtract before increasing dst.
7130 __ sub(scratch5, scratch5, Operand(8), SetCC);
7131 __ b(ge, &loop);
7132 }
7133 // There is now between zero and three bytes left to copy (negative that
7134 // number is in scratch5), and between one and three bytes already read into
7135 // scratch1 (eight times that number in scratch4). We may have read past
7136 // the end of the string, but because objects are aligned, we have not read
7137 // past the end of the object.
7138 // Find the minimum of remaining characters to move and preloaded characters
7139 // and write those as bytes.
7140 __ add(scratch5, scratch5, Operand(4), SetCC);
7141 __ b(eq, &done);
7142 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
7143 // Move minimum of bytes read and bytes left to copy to scratch4.
7144 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
7145 // Between one and three (value in scratch5) characters already read into
7146 // scratch ready to write.
7147 __ cmp(scratch5, Operand(2));
7148 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
7149 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
7150 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
7151 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
7152 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
7153 // Copy any remaining bytes.
7154 __ b(&byte_loop);
7155
7156 // Simple loop.
7157 // Copy words from src to dst, until less than four bytes left.
7158 // Both src and dest are word aligned.
7159 __ bind(&simple_loop);
7160 {
7161 Label loop;
7162 __ bind(&loop);
7163 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
7164 __ sub(scratch3, limit, Operand(dest));
7165 __ str(scratch1, MemOperand(dest, 4, PostIndex));
7166 // Compare to 8, not 4, because we do the substraction before increasing
7167 // dest.
7168 __ cmp(scratch3, Operand(8));
7169 __ b(ge, &loop);
7170 }
7171
7172 // Copy bytes from src to dst until dst hits limit.
7173 __ bind(&byte_loop);
7174 __ cmp(dest, Operand(limit));
7175 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
7176 __ b(ge, &done);
7177 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
7178 __ b(&byte_loop);
7179
7180 __ bind(&done);
7181}
7182
7183
7184void SubStringStub::Generate(MacroAssembler* masm) {
7185 Label runtime;
7186
7187 // Stack frame on entry.
7188 // lr: return address
7189 // sp[0]: to
7190 // sp[4]: from
7191 // sp[8]: string
7192
7193 // This stub is called from the native-call %_SubString(...), so
7194 // nothing can be assumed about the arguments. It is tested that:
7195 // "string" is a sequential string,
7196 // both "from" and "to" are smis, and
7197 // 0 <= from <= to <= string.length.
7198 // If any of these assumptions fail, we call the runtime system.
7199
7200 static const int kToOffset = 0 * kPointerSize;
7201 static const int kFromOffset = 1 * kPointerSize;
7202 static const int kStringOffset = 2 * kPointerSize;
7203
7204
7205 // Check bounds and smi-ness.
7206 __ ldr(r7, MemOperand(sp, kToOffset));
7207 __ ldr(r6, MemOperand(sp, kFromOffset));
7208 ASSERT_EQ(0, kSmiTag);
7209 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
7210 // I.e., arithmetic shift right by one un-smi-tags.
7211 __ mov(r2, Operand(r7, ASR, 1), SetCC);
7212 __ mov(r3, Operand(r6, ASR, 1), SetCC, cc);
7213 // If either r2 or r6 had the smi tag bit set, then carry is set now.
7214 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
7215 __ b(mi, &runtime); // From is negative.
7216
7217 __ sub(r2, r2, Operand(r3), SetCC);
7218 __ b(mi, &runtime); // Fail if from > to.
7219 // Handle sub-strings of length 2 and less in the runtime system.
7220 __ cmp(r2, Operand(2));
7221 __ b(le, &runtime);
7222
7223 // r2: length
7224 // r6: from (smi)
7225 // r7: to (smi)
7226
7227 // Make sure first argument is a sequential (or flat) string.
7228 __ ldr(r5, MemOperand(sp, kStringOffset));
7229 ASSERT_EQ(0, kSmiTag);
7230 __ tst(r5, Operand(kSmiTagMask));
7231 __ b(eq, &runtime);
7232 Condition is_string = masm->IsObjectStringType(r5, r1);
7233 __ b(NegateCondition(is_string), &runtime);
7234
7235 // r1: instance type
7236 // r2: length
7237 // r5: string
7238 // r6: from (smi)
7239 // r7: to (smi)
7240 Label seq_string;
7241 __ and_(r4, r1, Operand(kStringRepresentationMask));
7242 ASSERT(kSeqStringTag < kConsStringTag);
7243 ASSERT(kExternalStringTag > kConsStringTag);
7244 __ cmp(r4, Operand(kConsStringTag));
7245 __ b(gt, &runtime); // External strings go to runtime.
7246 __ b(lt, &seq_string); // Sequential strings are handled directly.
7247
7248 // Cons string. Try to recurse (once) on the first substring.
7249 // (This adds a little more generality than necessary to handle flattened
7250 // cons strings, but not much).
7251 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
7252 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
7253 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
7254 __ tst(r1, Operand(kStringRepresentationMask));
7255 ASSERT_EQ(0, kSeqStringTag);
7256 __ b(ne, &runtime); // Cons and External strings go to runtime.
7257
7258 // Definitly a sequential string.
7259 __ bind(&seq_string);
7260
7261 // r1: instance type.
7262 // r2: length
7263 // r5: string
7264 // r6: from (smi)
7265 // r7: to (smi)
7266 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
7267 __ cmp(r4, Operand(r7, ASR, 1));
7268 __ b(lt, &runtime); // Fail if to > length.
7269
7270 // r1: instance type.
7271 // r2: result string length.
7272 // r5: string.
7273 // r6: from offset (smi)
7274 // Check for flat ascii string.
7275 Label non_ascii_flat;
7276 __ tst(r1, Operand(kStringEncodingMask));
7277 ASSERT_EQ(0, kTwoByteStringTag);
7278 __ b(eq, &non_ascii_flat);
7279
7280 // Allocate the result.
7281 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
7282
7283 // r0: result string.
7284 // r2: result string length.
7285 // r5: string.
7286 // r6: from offset (smi)
7287 // Locate first character of result.
7288 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7289 // Locate 'from' character of string.
7290 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7291 __ add(r5, r5, Operand(r6, ASR, 1));
7292
7293 // r0: result string.
7294 // r1: first character of result string.
7295 // r2: result string length.
7296 // r5: first character of sub string to copy.
7297 ASSERT_EQ(0, SeqAsciiString::kHeaderSize & kObjectAlignmentMask);
7298 GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
7299 COPY_ASCII | DEST_ALWAYS_ALIGNED);
7300 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
7301 __ add(sp, sp, Operand(3 * kPointerSize));
7302 __ Ret();
7303
7304 __ bind(&non_ascii_flat);
7305 // r2: result string length.
7306 // r5: string.
7307 // r6: from offset (smi)
7308 // Check for flat two byte string.
7309
7310 // Allocate the result.
7311 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
7312
7313 // r0: result string.
7314 // r2: result string length.
7315 // r5: string.
7316 // Locate first character of result.
7317 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7318 // Locate 'from' character of string.
7319 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7320 // As "from" is a smi it is 2 times the value which matches the size of a two
7321 // byte character.
7322 __ add(r5, r5, Operand(r6));
7323
7324 // r0: result string.
7325 // r1: first character of result.
7326 // r2: result length.
7327 // r5: first character of string to copy.
7328 ASSERT_EQ(0, SeqTwoByteString::kHeaderSize & kObjectAlignmentMask);
7329 GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
7330 DEST_ALWAYS_ALIGNED);
7331 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
7332 __ add(sp, sp, Operand(3 * kPointerSize));
7333 __ Ret();
7334
7335 // Just jump to runtime to create the sub string.
7336 __ bind(&runtime);
7337 __ TailCallRuntime(ExternalReference(Runtime::kSubString), 3, 1);
7338}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007339
7340
7341void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
7342 Register left,
7343 Register right,
7344 Register scratch1,
7345 Register scratch2,
7346 Register scratch3,
7347 Register scratch4) {
7348 Label compare_lengths;
7349 // Find minimum length and length difference.
7350 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
7351 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
7352 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
7353 Register length_delta = scratch3;
7354 __ mov(scratch1, scratch2, LeaveCC, gt);
7355 Register min_length = scratch1;
7356 __ tst(min_length, Operand(min_length));
7357 __ b(eq, &compare_lengths);
7358
7359 // Setup registers so that we only need to increment one register
7360 // in the loop.
7361 __ add(scratch2, min_length,
7362 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7363 __ add(left, left, Operand(scratch2));
7364 __ add(right, right, Operand(scratch2));
7365 // Registers left and right points to the min_length character of strings.
7366 __ rsb(min_length, min_length, Operand(-1));
7367 Register index = min_length;
7368 // Index starts at -min_length.
7369
7370 {
7371 // Compare loop.
7372 Label loop;
7373 __ bind(&loop);
7374 // Compare characters.
7375 __ add(index, index, Operand(1), SetCC);
7376 __ ldrb(scratch2, MemOperand(left, index), ne);
7377 __ ldrb(scratch4, MemOperand(right, index), ne);
7378 // Skip to compare lengths with eq condition true.
7379 __ b(eq, &compare_lengths);
7380 __ cmp(scratch2, scratch4);
7381 __ b(eq, &loop);
7382 // Fallthrough with eq condition false.
7383 }
7384 // Compare lengths - strings up to min-length are equal.
7385 __ bind(&compare_lengths);
7386 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
7387 // Use zero length_delta as result.
7388 __ mov(r0, Operand(length_delta), SetCC, eq);
7389 // Fall through to here if characters compare not-equal.
7390 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
7391 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
7392 __ Ret();
7393}
7394
7395
7396void StringCompareStub::Generate(MacroAssembler* masm) {
7397 Label runtime;
7398
7399 // Stack frame on entry.
ager@chromium.org5c838252010-02-19 08:53:10 +00007400 // sp[0]: right string
7401 // sp[4]: left string
7402 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left
7403 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007404
7405 Label not_same;
7406 __ cmp(r0, r1);
7407 __ b(ne, &not_same);
7408 ASSERT_EQ(0, EQUAL);
7409 ASSERT_EQ(0, kSmiTag);
7410 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
7411 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
7412 __ add(sp, sp, Operand(2 * kPointerSize));
7413 __ Ret();
7414
7415 __ bind(&not_same);
7416
7417 // Check that both objects are sequential ascii strings.
7418 __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime);
7419
7420 // Compare flat ascii strings natively. Remove arguments from stack first.
7421 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
7422 __ add(sp, sp, Operand(2 * kPointerSize));
7423 GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5);
7424
7425 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
7426 // tagged as a small integer.
7427 __ bind(&runtime);
7428 __ TailCallRuntime(ExternalReference(Runtime::kStringCompare), 2, 1);
7429}
7430
7431
ager@chromium.org5c838252010-02-19 08:53:10 +00007432void StringAddStub::Generate(MacroAssembler* masm) {
7433 Label string_add_runtime;
7434 // Stack on entry:
7435 // sp[0]: second argument.
7436 // sp[4]: first argument.
7437
7438 // Load the two arguments.
7439 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
7440 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
7441
7442 // Make sure that both arguments are strings if not known in advance.
7443 if (string_check_) {
7444 ASSERT_EQ(0, kSmiTag);
7445 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
7446 // Load instance types.
7447 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
7448 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
7449 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
7450 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
7451 ASSERT_EQ(0, kStringTag);
7452 // If either is not a string, go to runtime.
7453 __ tst(r4, Operand(kIsNotStringMask));
7454 __ tst(r5, Operand(kIsNotStringMask), eq);
7455 __ b(ne, &string_add_runtime);
7456 }
7457
7458 // Both arguments are strings.
7459 // r0: first string
7460 // r1: second string
7461 // r4: first string instance type (if string_check_)
7462 // r5: second string instance type (if string_check_)
7463 {
7464 Label strings_not_empty;
7465 // Check if either of the strings are empty. In that case return the other.
7466 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
7467 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
7468 __ cmp(r2, Operand(0)); // Test if first string is empty.
7469 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
7470 __ cmp(r3, Operand(0), ne); // Else test if second string is empty.
7471 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
7472
7473 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
7474 __ add(sp, sp, Operand(2 * kPointerSize));
7475 __ Ret();
7476
7477 __ bind(&strings_not_empty);
7478 }
7479
7480 // Both strings are non-empty.
7481 // r0: first string
7482 // r1: second string
7483 // r2: length of first string
7484 // r3: length of second string
7485 // r4: first string instance type (if string_check_)
7486 // r5: second string instance type (if string_check_)
7487 // Look at the length of the result of adding the two strings.
7488 Label string_add_flat_result;
7489 // Adding two lengths can't overflow.
7490 ASSERT(String::kMaxLength * 2 > String::kMaxLength);
7491 __ add(r6, r2, Operand(r3));
7492 // Use the runtime system when adding two one character strings, as it
7493 // contains optimizations for this specific case using the symbol table.
7494 __ cmp(r6, Operand(2));
7495 __ b(eq, &string_add_runtime);
7496 // Check if resulting string will be flat.
7497 __ cmp(r6, Operand(String::kMinNonFlatLength));
7498 __ b(lt, &string_add_flat_result);
7499 // Handle exceptionally long strings in the runtime system.
7500 ASSERT((String::kMaxLength & 0x80000000) == 0);
7501 ASSERT(IsPowerOf2(String::kMaxLength + 1));
7502 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
7503 __ cmp(r6, Operand(String::kMaxLength + 1));
7504 __ b(hs, &string_add_runtime);
7505
7506 // If result is not supposed to be flat, allocate a cons string object.
7507 // If both strings are ascii the result is an ascii cons string.
7508 if (!string_check_) {
7509 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
7510 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
7511 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
7512 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
7513 }
7514 Label non_ascii, allocated;
7515 ASSERT_EQ(0, kTwoByteStringTag);
7516 __ tst(r4, Operand(kStringEncodingMask));
7517 __ tst(r5, Operand(kStringEncodingMask), ne);
7518 __ b(eq, &non_ascii);
7519
7520 // Allocate an ASCII cons string.
7521 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
7522 __ bind(&allocated);
7523 // Fill the fields of the cons string.
7524 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
7525 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
7526 __ mov(r0, Operand(r7));
7527 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
7528 __ add(sp, sp, Operand(2 * kPointerSize));
7529 __ Ret();
7530
7531 __ bind(&non_ascii);
7532 // Allocate a two byte cons string.
7533 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
7534 __ jmp(&allocated);
7535
7536 // Handle creating a flat result. First check that both strings are
7537 // sequential and that they have the same encoding.
7538 // r0: first string
7539 // r1: second string
7540 // r2: length of first string
7541 // r3: length of second string
7542 // r4: first string instance type (if string_check_)
7543 // r5: second string instance type (if string_check_)
7544 // r6: sum of lengths.
7545 __ bind(&string_add_flat_result);
7546 if (!string_check_) {
7547 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
7548 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
7549 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
7550 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
7551 }
7552 // Check that both strings are sequential.
7553 ASSERT_EQ(0, kSeqStringTag);
7554 __ tst(r4, Operand(kStringRepresentationMask));
7555 __ tst(r5, Operand(kStringRepresentationMask), eq);
7556 __ b(ne, &string_add_runtime);
7557 // Now check if both strings have the same encoding (ASCII/Two-byte).
7558 // r0: first string.
7559 // r1: second string.
7560 // r2: length of first string.
7561 // r3: length of second string.
7562 // r6: sum of lengths..
7563 Label non_ascii_string_add_flat_result;
7564 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
7565 __ eor(r7, r4, Operand(r5));
7566 __ tst(r7, Operand(kStringEncodingMask));
7567 __ b(ne, &string_add_runtime);
7568 // And see if it's ASCII or two-byte.
7569 __ tst(r4, Operand(kStringEncodingMask));
7570 __ b(eq, &non_ascii_string_add_flat_result);
7571
7572 // Both strings are sequential ASCII strings. We also know that they are
7573 // short (since the sum of the lengths is less than kMinNonFlatLength).
7574 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
7575 // Locate first character of result.
7576 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7577 // Locate first character of first argument.
7578 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7579 // r0: first character of first string.
7580 // r1: second string.
7581 // r2: length of first string.
7582 // r3: length of second string.
7583 // r6: first character of result.
7584 // r7: result string.
7585 GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
7586
7587 // Load second argument and locate first character.
7588 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7589 // r1: first character of second string.
7590 // r3: length of second string.
7591 // r6: next character of result.
7592 // r7: result string.
7593 GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
7594 __ mov(r0, Operand(r7));
7595 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
7596 __ add(sp, sp, Operand(2 * kPointerSize));
7597 __ Ret();
7598
7599 __ bind(&non_ascii_string_add_flat_result);
7600 // Both strings are sequential two byte strings.
7601 // r0: first string.
7602 // r1: second string.
7603 // r2: length of first string.
7604 // r3: length of second string.
7605 // r6: sum of length of strings.
7606 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
7607 // r0: first string.
7608 // r1: second string.
7609 // r2: length of first string.
7610 // r3: length of second string.
7611 // r7: result string.
7612
7613 // Locate first character of result.
7614 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7615 // Locate first character of first argument.
7616 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7617
7618 // r0: first character of first string.
7619 // r1: second string.
7620 // r2: length of first string.
7621 // r3: length of second string.
7622 // r6: first character of result.
7623 // r7: result string.
7624 GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
7625
7626 // Locate first character of second argument.
7627 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7628
7629 // r1: first character of second string.
7630 // r3: length of second string.
7631 // r6: next character of result (after copy of first string).
7632 // r7: result string.
7633 GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
7634
7635 __ mov(r0, Operand(r7));
7636 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
7637 __ add(sp, sp, Operand(2 * kPointerSize));
7638 __ Ret();
7639
7640 // Just jump to runtime to add the two strings.
7641 __ bind(&string_add_runtime);
7642 __ TailCallRuntime(ExternalReference(Runtime::kStringAdd), 2, 1);
7643}
7644
7645
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007646#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007647
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007648} } // namespace v8::internal