blob: 6644d02487475dc69a10eaea89a5cdf83911b9ab [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"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000038#include "virtual-frame-inl.h"
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
136// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000137// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000139// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000140// cp: callee's context
141
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000142void CodeGenerator::Generate(CompilationInfo* info) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000143 // Record the position for debugging purposes.
ager@chromium.org5c838252010-02-19 08:53:10 +0000144 CodeForFunctionPosition(info->function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145
146 // Initialize state.
ager@chromium.org5c838252010-02-19 08:53:10 +0000147 info_ = info;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000148 ASSERT(allocator_ == NULL);
149 RegisterAllocator register_allocator(this);
150 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000151 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000152 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000153 cc_reg_ = al;
154 {
155 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000156
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000157 // Entry:
158 // Stack: receiver, arguments
159 // lr: return address
160 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000161 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000162 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000163 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000164 allocator_->Initialize();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000165
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166#ifdef DEBUG
167 if (strlen(FLAG_stop_at) > 0 &&
ager@chromium.org5c838252010-02-19 08:53:10 +0000168 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000169 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000170 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000171 }
172#endif
173
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000174 if (info->mode() == CompilationInfo::PRIMARY) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000175 frame_->Enter();
176 // tos: code slot
177
178 // Allocate space for locals and initialize them. This also checks
179 // for stack overflow.
180 frame_->AllocateStackSlots();
181
182 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org5c838252010-02-19 08:53:10 +0000183 int heap_slots = scope()->num_heap_slots();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000184 if (heap_slots > 0) {
185 // Allocate local context.
186 // Get outer context and create a new context based on it.
187 __ ldr(r0, frame_->Function());
188 frame_->EmitPush(r0);
189 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
190 FastNewContextStub stub(heap_slots);
191 frame_->CallStub(&stub, 1);
192 } else {
193 frame_->CallRuntime(Runtime::kNewContext, 1);
194 }
195
196#ifdef DEBUG
197 JumpTarget verified_true;
198 __ cmp(r0, Operand(cp));
199 verified_true.Branch(eq);
200 __ stop("NewContext: r0 is expected to be the same as cp");
201 verified_true.Bind();
202#endif
203 // Update context local.
204 __ str(cp, frame_->Context());
205 }
206
207 // TODO(1241774): Improve this code:
208 // 1) only needed if we have a context
209 // 2) no need to recompute context ptr every single time
210 // 3) don't copy parameter operand code from SlotOperand!
211 {
212 Comment cmnt2(masm_, "[ copy context parameters into .context");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000213 // Note that iteration order is relevant here! If we have the same
214 // parameter twice (e.g., function (x, y, x)), and that parameter
215 // needs to be copied into the context, it must be the last argument
216 // passed to the parameter that needs to be copied. This is a rare
217 // case so we don't check for it, instead we rely on the copying
218 // order: such a parameter is copied repeatedly into the same
219 // context location and thus the last value is what is seen inside
220 // the function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000221 for (int i = 0; i < scope()->num_parameters(); i++) {
222 Variable* par = scope()->parameter(i);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000223 Slot* slot = par->slot();
224 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000225 ASSERT(!scope()->is_global_scope()); // No params in global scope.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000226 __ ldr(r1, frame_->ParameterAt(i));
227 // Loads r2 with context; used below in RecordWrite.
228 __ str(r1, SlotOperand(slot, r2));
229 // Load the offset into r3.
230 int slot_offset =
231 FixedArray::kHeaderSize + slot->index() * kPointerSize;
232 __ mov(r3, Operand(slot_offset));
233 __ RecordWrite(r2, r3, r1);
234 }
235 }
236 }
237
238 // Store the arguments object. This must happen after context
239 // initialization because the arguments object may be stored in the
240 // context.
ager@chromium.org5c838252010-02-19 08:53:10 +0000241 if (scope()->arguments() != NULL) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000242 Comment cmnt(masm_, "[ allocate arguments object");
ager@chromium.org5c838252010-02-19 08:53:10 +0000243 ASSERT(scope()->arguments_shadow() != NULL);
244 Variable* arguments = scope()->arguments()->var();
245 Variable* shadow = scope()->arguments_shadow()->var();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000246 ASSERT(arguments != NULL && arguments->slot() != NULL);
247 ASSERT(shadow != NULL && shadow->slot() != NULL);
248 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
249 __ ldr(r2, frame_->Function());
250 // The receiver is below the arguments, the return address, and the
251 // frame pointer on the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +0000252 const int kReceiverDisplacement = 2 + scope()->num_parameters();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000253 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
ager@chromium.org5c838252010-02-19 08:53:10 +0000254 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000255 frame_->Adjust(3);
256 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
257 frame_->CallStub(&stub, 3);
258 frame_->EmitPush(r0);
259 StoreToSlot(arguments->slot(), NOT_CONST_INIT);
260 StoreToSlot(shadow->slot(), NOT_CONST_INIT);
261 frame_->Drop(); // Value is no longer needed.
262 }
263
264 // Initialize ThisFunction reference if present.
ager@chromium.org5c838252010-02-19 08:53:10 +0000265 if (scope()->is_function_scope() && scope()->function() != NULL) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000266 __ mov(ip, Operand(Factory::the_hole_value()));
267 frame_->EmitPush(ip);
ager@chromium.org5c838252010-02-19 08:53:10 +0000268 StoreToSlot(scope()->function()->slot(), NOT_CONST_INIT);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000269 }
270 } else {
271 // When used as the secondary compiler for splitting, r1, cp,
272 // fp, and lr have been pushed on the stack. Adjust the virtual
273 // frame to match this state.
274 frame_->Adjust(4);
275 allocator_->Unuse(r1);
276 allocator_->Unuse(lr);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000277
278 // Bind all the bailout labels to the beginning of the function.
279 List<CompilationInfo::Bailout*>* bailouts = info->bailouts();
280 for (int i = 0; i < bailouts->length(); i++) {
281 __ bind(bailouts->at(i)->label());
282 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000283 }
284
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000285 // Initialize the function return target after the locals are set
286 // up, because it needs the expected frame height from the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000287 function_return_.set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000288 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000290 // Generate code to 'execute' declarations and initialize functions
291 // (source elements). In case of an illegal redeclaration we need to
292 // handle that instead of processing the declarations.
ager@chromium.org5c838252010-02-19 08:53:10 +0000293 if (scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000294 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000295 scope()->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296 } else {
297 Comment cmnt(masm_, "[ declarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000298 ProcessDeclarations(scope()->declarations());
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000299 // Bail out if a stack-overflow exception occurred when processing
300 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000301 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000302 }
303
mads.s.ager31e71382008-08-13 09:32:07 +0000304 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000305 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000306 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000307 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308
309 // Compile the body of the function in a vanilla state. Don't
310 // bother compiling all the code if the scope has an illegal
311 // redeclaration.
ager@chromium.org5c838252010-02-19 08:53:10 +0000312 if (!scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313 Comment cmnt(masm_, "[ function body");
314#ifdef DEBUG
315 bool is_builtin = Bootstrapper::IsActive();
316 bool should_trace =
317 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000318 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000319 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000320 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000321 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000322#endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000323 VisitStatementsAndSpill(info->function()->body());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000324 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000325 }
326
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000327 // Generate the return sequence if necessary.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000328 if (has_valid_frame() || function_return_.is_linked()) {
329 if (!function_return_.is_linked()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000330 CodeForReturnPosition(info->function());
ager@chromium.org4af710e2009-09-15 12:20:11 +0000331 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000332 // exit
333 // r0: result
334 // sp: stack pointer
335 // fp: frame pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000336 // cp: callee's context
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000337 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +0000338
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000339 function_return_.Bind();
340 if (FLAG_trace) {
341 // Push the return value on the stack as the parameter.
342 // Runtime::TraceExit returns the parameter as it is.
343 frame_->EmitPush(r0);
344 frame_->CallRuntime(Runtime::kTraceExit, 1);
345 }
346
ager@chromium.org4af710e2009-09-15 12:20:11 +0000347 // Add a label for checking the size of the code used for returning.
348 Label check_exit_codesize;
349 masm_->bind(&check_exit_codesize);
350
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000351 // Calculate the exact length of the return sequence and make sure that
352 // the constant pool is not emitted inside of the return sequence.
ager@chromium.org5c838252010-02-19 08:53:10 +0000353 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000354 int return_sequence_length = Assembler::kJSReturnSequenceLength;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000355 if (!masm_->ImmediateFitsAddrMode1Instruction(sp_delta)) {
356 // Additional mov instruction generated.
357 return_sequence_length++;
358 }
359 masm_->BlockConstPoolFor(return_sequence_length);
360
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000361 // Tear down the frame which will restore the caller's frame pointer and
362 // the link register.
363 frame_->Exit();
364
ager@chromium.org4af710e2009-09-15 12:20:11 +0000365 // Here we use masm_-> instead of the __ macro to avoid the code coverage
366 // tool from instrumenting as we rely on the code size here.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000367 masm_->add(sp, sp, Operand(sp_delta));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000368 masm_->Jump(lr);
369
370 // Check that the size of the code used for returning matches what is
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000371 // expected by the debugger. The add instruction above is an addressing
372 // mode 1 instruction where there are restrictions on which immediate values
373 // can be encoded in the instruction and which immediate values requires
374 // use of an additional instruction for moving the immediate to a temporary
375 // register.
376 ASSERT_EQ(return_sequence_length,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000377 masm_->InstructionsGeneratedSince(&check_exit_codesize));
mads.s.ager31e71382008-08-13 09:32:07 +0000378 }
379
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000380 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000381 ASSERT(!has_cc());
382 ASSERT(state_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000383 ASSERT(!function_return_is_shadowed_);
384 function_return_.Unuse();
385 DeleteFrame();
386
387 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000388 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000389 ProcessDeferred();
390 }
391
392 allocator_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393}
394
395
ager@chromium.org7c537e22008-10-16 08:43:32 +0000396MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
397 // Currently, this assertion will fail if we try to assign to
398 // a constant variable that is constant because it is read-only
399 // (such as the variable referring to a named function expression).
400 // We need to implement assignments to read-only variables.
401 // Ideally, we should do this during AST generation (by converting
402 // such assignments into expression statements); however, in general
403 // we may not be able to make the decision until past AST generation,
404 // that is when the entire program is known.
405 ASSERT(slot != NULL);
406 int index = slot->index();
407 switch (slot->type()) {
408 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000409 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000410
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000411 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000412 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000413
414 case Slot::CONTEXT: {
415 // Follow the context chain if necessary.
416 ASSERT(!tmp.is(cp)); // do not overwrite context register
417 Register context = cp;
418 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000419 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000420 // Load the closure.
421 // (All contexts, even 'with' contexts, have a closure,
422 // and it is the same for all contexts inside a function.
423 // There is no need to go to the function context first.)
424 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
425 // Load the function context (which is the incoming, outer context).
426 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
427 context = tmp;
428 }
429 // We may have a 'with' context now. Get the function context.
430 // (In fact this mov may never be the needed, since the scope analysis
431 // may not permit a direct context access in this case and thus we are
432 // always at a function context. However it is safe to dereference be-
433 // cause the function context of a function context is itself. Before
434 // deleting this mov we should try to create a counter-example first,
435 // though...)
436 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
437 return ContextOperand(tmp, index);
438 }
439
440 default:
441 UNREACHABLE();
442 return MemOperand(r0, 0);
443 }
444}
445
446
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000447MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
448 Slot* slot,
449 Register tmp,
450 Register tmp2,
451 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000452 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000453 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000454
ager@chromium.org381abbb2009-02-25 13:23:22 +0000455 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
456 if (s->num_heap_slots() > 0) {
457 if (s->calls_eval()) {
458 // Check that extension is NULL.
459 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
460 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000461 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000462 }
463 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
464 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
465 context = tmp;
466 }
467 }
468 // Check that last extension is NULL.
469 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
470 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000471 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000472 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000473 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000474}
475
476
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000477// Loads a value on TOS. If it is a boolean value, the result may have been
478// (partially) translated into branches, or it may have set the condition
479// code register. If force_cc is set, the value is forced to set the
480// condition code register and no value is pushed. If the condition code
481// register was set, has_cc() is true and cc_reg_ contains the condition to
482// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000483void CodeGenerator::LoadCondition(Expression* x,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000484 JumpTarget* true_target,
485 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000486 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000487 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000488 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000489
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000490 { CodeGenState new_state(this, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000491 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000492
493 // If we hit a stack overflow, we may not have actually visited
494 // the expression. In that case, we ensure that we have a
495 // valid-looking frame state because we will continue to generate
496 // code as we unwind the C++ stack.
497 //
498 // It's possible to have both a stack overflow and a valid frame
499 // state (eg, a subexpression overflowed, visiting it returned
500 // with a dummied frame state, and visiting this expression
501 // returned with a normal-looking state).
502 if (HasStackOverflow() &&
503 has_valid_frame() &&
504 !has_cc() &&
505 frame_->height() == original_height) {
506 true_target->Jump();
507 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000508 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000509 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000510 // Convert the TOS value to a boolean in the condition code register.
511 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000512 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000513 ASSERT(!force_cc || !has_valid_frame() || has_cc());
514 ASSERT(!has_valid_frame() ||
515 (has_cc() && frame_->height() == original_height) ||
516 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000517}
518
519
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000520void CodeGenerator::Load(Expression* expr) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000521#ifdef DEBUG
522 int original_height = frame_->height();
523#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000524 JumpTarget true_target;
525 JumpTarget false_target;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000526 LoadCondition(expr, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000527
528 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000529 // Convert cc_reg_ into a boolean value.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000530 JumpTarget loaded;
531 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000532 materialize_true.Branch(cc_reg_);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000533 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000534 frame_->EmitPush(r0);
535 loaded.Jump();
536 materialize_true.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000537 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000538 frame_->EmitPush(r0);
539 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000540 cc_reg_ = al;
541 }
542
543 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000544 // We have at least one condition value that has been "translated"
545 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000546 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000547 if (frame_ != NULL) {
548 loaded.Jump(); // Don't lose the current TOS.
549 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000550 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000551 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000552 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000553 true_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000554 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000555 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000556 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000557 // If both "true" and "false" need to be loaded jump across the code for
558 // "false".
559 if (both) {
560 loaded.Jump();
561 }
562 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000564 false_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000565 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000566 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000568 // A value is loaded on all paths reaching this point.
569 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000570 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000571 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000573 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000574}
575
576
ager@chromium.org7c537e22008-10-16 08:43:32 +0000577void CodeGenerator::LoadGlobal() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000578 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000579 __ ldr(r0, GlobalObject());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000580 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000581}
582
583
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000584void CodeGenerator::LoadGlobalReceiver(Register scratch) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000585 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000586 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
587 __ ldr(scratch,
588 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000589 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000590}
591
592
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000593void CodeGenerator::LoadTypeofExpression(Expression* expr) {
594 // Special handling of identifiers as subexpressions of typeof.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000595 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000596 Variable* variable = expr->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000597 if (variable != NULL && !variable->is_this() && variable->is_global()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000598 // For a global variable we build the property reference
599 // <global>.<variable> and perform a (regular non-contextual) property
600 // load to make sure we do not get reference errors.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000601 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
602 Literal key(variable->name());
ager@chromium.org236ad962008-09-25 09:45:57 +0000603 Property property(&global, &key, RelocInfo::kNoPosition);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000604 Reference ref(this, &property);
605 ref.GetValueAndSpill();
606 } else if (variable != NULL && variable->slot() != NULL) {
607 // For a variable that rewrites to a slot, we signal it is the immediate
608 // subexpression of a typeof.
609 LoadFromSlot(variable->slot(), INSIDE_TYPEOF);
610 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611 } else {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000612 // Anything else can be handled normally.
613 LoadAndSpill(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000614 }
615}
616
617
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000618Reference::Reference(CodeGenerator* cgen,
619 Expression* expression,
620 bool persist_after_get)
621 : cgen_(cgen),
622 expression_(expression),
623 type_(ILLEGAL),
624 persist_after_get_(persist_after_get) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000625 cgen->LoadReference(this);
626}
627
628
629Reference::~Reference() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000630 ASSERT(is_unloaded() || is_illegal());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000631}
632
633
ager@chromium.org7c537e22008-10-16 08:43:32 +0000634void CodeGenerator::LoadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000635 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000636 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000637 Expression* e = ref->expression();
638 Property* property = e->AsProperty();
639 Variable* var = e->AsVariableProxy()->AsVariable();
640
641 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000642 // The expression is either a property or a variable proxy that rewrites
643 // to a property.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000644 LoadAndSpill(property->obj());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000645 if (property->key()->IsPropertyName()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000646 ref->set_type(Reference::NAMED);
647 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000648 LoadAndSpill(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000649 ref->set_type(Reference::KEYED);
650 }
651 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000652 // The expression is a variable proxy that does not rewrite to a
653 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000654 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000655 LoadGlobal();
656 ref->set_type(Reference::NAMED);
657 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000658 ASSERT(var->slot() != NULL);
659 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000660 }
661 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000662 // Anything else is a runtime error.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000663 LoadAndSpill(e);
664 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000665 }
666}
667
668
ager@chromium.org7c537e22008-10-16 08:43:32 +0000669void CodeGenerator::UnloadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000670 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000671 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000672 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000673 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000674 if (size > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000675 frame_->EmitPop(r0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000676 frame_->Drop(size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000677 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000679 ref->set_unloaded();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000680}
681
682
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000683// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
684// register to a boolean in the condition code register. The code
685// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000686void CodeGenerator::ToBoolean(JumpTarget* true_target,
687 JumpTarget* false_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000688 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000689 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000690 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000691 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692
693 // Fast case checks
694
mads.s.ager31e71382008-08-13 09:32:07 +0000695 // Check if the value is 'false'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000696 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
697 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000698 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000699
mads.s.ager31e71382008-08-13 09:32:07 +0000700 // Check if the value is 'true'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000701 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
702 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000703 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000704
mads.s.ager31e71382008-08-13 09:32:07 +0000705 // Check if the value is 'undefined'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000706 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
707 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000708 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000709
mads.s.ager31e71382008-08-13 09:32:07 +0000710 // Check if the value is a smi.
711 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000712 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000713 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000714 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000715
716 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000717 frame_->EmitPush(r0);
718 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000719 // Convert the result (r0) to a condition code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000720 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
721 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722
723 cc_reg_ = ne;
724}
725
726
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000727void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000728 OverwriteMode overwrite_mode,
729 int constant_rhs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000730 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000731 // sp[0] : y
732 // sp[1] : x
733 // result : r0
734
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000735 // Stub is entered with a call: 'return address' is in lr.
736 switch (op) {
737 case Token::ADD: // fall through.
738 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000739 case Token::MUL:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000740 case Token::DIV:
741 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000742 case Token::BIT_OR:
743 case Token::BIT_AND:
744 case Token::BIT_XOR:
745 case Token::SHL:
746 case Token::SHR:
747 case Token::SAR: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000748 frame_->EmitPop(r0); // r0 : y
749 frame_->EmitPop(r1); // r1 : x
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000750 GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000751 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000752 break;
753 }
754
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755 case Token::COMMA:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000756 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000757 // simply discard left value
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000758 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000759 break;
760
761 default:
762 // Other cases should have been handled before this point.
763 UNREACHABLE();
764 break;
765 }
766}
767
768
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000769class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000770 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000771 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000772 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000773 bool reversed,
774 OverwriteMode overwrite_mode)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000775 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000776 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000777 reversed_(reversed),
778 overwrite_mode_(overwrite_mode) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000779 set_comment("[ DeferredInlinedSmiOperation");
780 }
781
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000782 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000783
784 private:
785 Token::Value op_;
786 int value_;
787 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000788 OverwriteMode overwrite_mode_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000789};
790
791
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000792void DeferredInlineSmiOperation::Generate() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000793 switch (op_) {
794 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000795 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000796 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000797 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
798 __ mov(r1, Operand(Smi::FromInt(value_)));
799 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000800 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
801 __ mov(r0, Operand(Smi::FromInt(value_)));
802 }
803 break;
804 }
805
806 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000807 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000808 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000809 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
810 __ mov(r1, Operand(Smi::FromInt(value_)));
811 } else {
812 __ add(r1, r0, Operand(Smi::FromInt(value_)));
813 __ mov(r0, Operand(Smi::FromInt(value_)));
814 }
815 break;
816 }
817
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000818 // For these operations there is no optimistic operation that needs to be
819 // reverted.
820 case Token::MUL:
821 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000822 case Token::BIT_OR:
823 case Token::BIT_XOR:
824 case Token::BIT_AND: {
825 if (reversed_) {
826 __ mov(r1, Operand(Smi::FromInt(value_)));
827 } else {
828 __ mov(r1, Operand(r0));
829 __ mov(r0, Operand(Smi::FromInt(value_)));
830 }
831 break;
832 }
833
834 case Token::SHL:
835 case Token::SHR:
836 case Token::SAR: {
837 if (!reversed_) {
838 __ mov(r1, Operand(r0));
839 __ mov(r0, Operand(Smi::FromInt(value_)));
840 } else {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000841 UNREACHABLE(); // Should have been handled in SmiOperation.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000842 }
843 break;
844 }
845
846 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +0000847 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000848 UNREACHABLE();
849 break;
850 }
851
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000852 GenericBinaryOpStub stub(op_, overwrite_mode_, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000853 __ CallStub(&stub);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000854}
855
856
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000857static bool PopCountLessThanEqual2(unsigned int x) {
858 x &= x - 1;
859 return (x & (x - 1)) == 0;
860}
861
862
863// Returns the index of the lowest bit set.
864static int BitPosition(unsigned x) {
865 int bit_posn = 0;
866 while ((x & 0xf) == 0) {
867 bit_posn += 4;
868 x >>= 4;
869 }
870 while ((x & 1) == 0) {
871 bit_posn++;
872 x >>= 1;
873 }
874 return bit_posn;
875}
876
877
ager@chromium.org7c537e22008-10-16 08:43:32 +0000878void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000879 Handle<Object> value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000880 bool reversed,
881 OverwriteMode mode) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000882 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000883 // NOTE: This is an attempt to inline (a bit) more of the code for
884 // some possible smi operations (like + and -) when (at least) one
885 // of the operands is a literal smi. With this optimization, the
886 // performance of the system is increased by ~15%, and the generated
887 // code size is increased by ~1% (measured on a combination of
888 // different benchmarks).
889
mads.s.ager31e71382008-08-13 09:32:07 +0000890 // sp[0] : operand
891
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000892 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000894 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000895 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000896
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000897 bool something_to_inline = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898 switch (op) {
899 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000900 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000901 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000903 __ add(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000904 deferred->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000906 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000907 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000908 break;
909 }
910
911 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000912 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000913 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000914
ager@chromium.orge2902be2009-06-08 12:21:35 +0000915 if (reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000916 __ rsb(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000917 } else {
918 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919 }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000920 deferred->Branch(vs);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000921 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000922 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000923 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000924 break;
925 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000926
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000927
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000928 case Token::BIT_OR:
929 case Token::BIT_XOR:
930 case Token::BIT_AND: {
931 DeferredCode* deferred =
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000932 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000933 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000934 deferred->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000935 switch (op) {
936 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
937 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
938 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
939 default: UNREACHABLE();
940 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000941 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000942 break;
943 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000944
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000945 case Token::SHL:
946 case Token::SHR:
947 case Token::SAR: {
948 if (reversed) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000949 something_to_inline = false;
950 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000951 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000952 int shift_value = int_value & 0x1f; // least significant 5 bits
953 DeferredCode* deferred =
954 new DeferredInlineSmiOperation(op, shift_value, false, mode);
955 __ tst(r0, Operand(kSmiTagMask));
956 deferred->Branch(ne);
957 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
958 switch (op) {
959 case Token::SHL: {
960 if (shift_value != 0) {
961 __ mov(r2, Operand(r2, LSL, shift_value));
962 }
963 // check that the *unsigned* result fits in a smi
964 __ add(r3, r2, Operand(0x40000000), SetCC);
965 deferred->Branch(mi);
966 break;
967 }
968 case Token::SHR: {
969 // LSR by immediate 0 means shifting 32 bits.
970 if (shift_value != 0) {
971 __ mov(r2, Operand(r2, LSR, shift_value));
972 }
973 // check that the *unsigned* result fits in a smi
974 // neither of the two high-order bits can be set:
975 // - 0x80000000: high bit would be lost when smi tagging
976 // - 0x40000000: this number would convert to negative when
977 // smi tagging these two cases can only happen with shifts
978 // by 0 or 1 when handed a valid smi
979 __ and_(r3, r2, Operand(0xc0000000), SetCC);
980 deferred->Branch(ne);
981 break;
982 }
983 case Token::SAR: {
984 if (shift_value != 0) {
985 // ASR by immediate 0 means shifting 32 bits.
986 __ mov(r2, Operand(r2, ASR, shift_value));
987 }
988 break;
989 }
990 default: UNREACHABLE();
991 }
992 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
993 deferred->BindExit();
994 break;
995 }
996
997 case Token::MOD: {
998 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
999 something_to_inline = false;
1000 break;
1001 }
1002 DeferredCode* deferred =
1003 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
1004 unsigned mask = (0x80000000u | kSmiTagMask);
1005 __ tst(r0, Operand(mask));
1006 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1007 mask = (int_value << kSmiTagSize) - 1;
1008 __ and_(r0, r0, Operand(mask));
1009 deferred->BindExit();
1010 break;
1011 }
1012
1013 case Token::MUL: {
1014 if (!IsEasyToMultiplyBy(int_value)) {
1015 something_to_inline = false;
1016 break;
1017 }
1018 DeferredCode* deferred =
1019 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
1020 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1021 max_smi_that_wont_overflow <<= kSmiTagSize;
1022 unsigned mask = 0x80000000u;
1023 while ((mask & max_smi_that_wont_overflow) == 0) {
1024 mask |= mask >> 1;
1025 }
1026 mask |= kSmiTagMask;
1027 // This does a single mask that checks for a too high value in a
1028 // conservative way and for a non-Smi. It also filters out negative
1029 // numbers, unfortunately, but since this code is inline we prefer
1030 // brevity to comprehensiveness.
1031 __ tst(r0, Operand(mask));
1032 deferred->Branch(ne);
1033 MultiplyByKnownInt(masm_, r0, r0, int_value);
1034 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001035 break;
1036 }
1037
1038 default:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001039 something_to_inline = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001040 break;
1041 }
1042
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001043 if (!something_to_inline) {
1044 if (!reversed) {
1045 frame_->EmitPush(r0);
1046 __ mov(r0, Operand(value));
1047 frame_->EmitPush(r0);
1048 GenericBinaryOperation(op, mode, int_value);
1049 } else {
1050 __ mov(ip, Operand(value));
1051 frame_->EmitPush(ip);
1052 frame_->EmitPush(r0);
1053 GenericBinaryOperation(op, mode, kUnknownIntValue);
1054 }
1055 }
1056
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001057 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058}
1059
1060
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001061void CodeGenerator::Comparison(Condition cc,
1062 Expression* left,
1063 Expression* right,
1064 bool strict) {
1065 if (left != NULL) LoadAndSpill(left);
1066 if (right != NULL) LoadAndSpill(right);
1067
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001068 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001069 // sp[0] : y
1070 // sp[1] : x
1071 // result : cc register
1072
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001073 // Strict only makes sense for equality comparisons.
1074 ASSERT(!strict || cc == eq);
1075
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001076 JumpTarget exit;
1077 JumpTarget smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001078 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1079 if (cc == gt || cc == le) {
1080 cc = ReverseCondition(cc);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001081 frame_->EmitPop(r1);
1082 frame_->EmitPop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001083 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001084 frame_->EmitPop(r0);
1085 frame_->EmitPop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001086 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087 __ orr(r2, r0, Operand(r1));
1088 __ tst(r2, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001089 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001090
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001091 // Perform non-smi comparison by stub.
1092 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1093 // We call with 0 args because there are 0 on the stack.
1094 CompareStub stub(cc, strict);
1095 frame_->CallStub(&stub, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001096 __ cmp(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001097 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001099 // Do smi comparisons by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001100 smi.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101 __ cmp(r1, Operand(r0));
1102
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001103 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104 cc_reg_ = cc;
1105}
1106
1107
mads.s.ager31e71382008-08-13 09:32:07 +00001108// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001109void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001110 CallFunctionFlags flags,
1111 int position) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001112 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001113 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001114 int arg_count = args->length();
1115 for (int i = 0; i < arg_count; i++) {
1116 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001117 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001118
kasper.lund7276f142008-07-30 08:49:36 +00001119 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001120 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001121
kasper.lund7276f142008-07-30 08:49:36 +00001122 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001123 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001124 CallFunctionStub call_function(arg_count, in_loop, flags);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001125 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001126
1127 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001128 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001129 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001130}
1131
1132
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001133void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001134 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001135 ASSERT(has_cc());
1136 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001137 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001138 cc_reg_ = al;
1139}
1140
1141
ager@chromium.org7c537e22008-10-16 08:43:32 +00001142void CodeGenerator::CheckStack() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001143 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3811b432009-10-28 14:53:37 +00001144 Comment cmnt(masm_, "[ check stack");
1145 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1146 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1147 // the implicit 8 byte offset that always applies to operations with pc and
1148 // gives a return address 12 bytes down.
1149 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1150 masm_->cmp(sp, Operand(ip));
1151 StackCheckStub stub;
1152 // Call the stub if lower.
1153 masm_->mov(pc,
1154 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1155 RelocInfo::CODE_TARGET),
1156 LeaveCC,
1157 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001158}
1159
1160
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001161void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1162#ifdef DEBUG
1163 int original_height = frame_->height();
1164#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001165 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001166 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1167 VisitAndSpill(statements->at(i));
1168 }
1169 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1170}
1171
1172
ager@chromium.org7c537e22008-10-16 08:43:32 +00001173void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001174#ifdef DEBUG
1175 int original_height = frame_->height();
1176#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001177 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001178 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001179 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001180 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001181 VisitStatementsAndSpill(node->statements());
1182 if (node->break_target()->is_linked()) {
1183 node->break_target()->Bind();
1184 }
1185 node->break_target()->Unuse();
1186 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001187}
1188
1189
ager@chromium.org7c537e22008-10-16 08:43:32 +00001190void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001191 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3811b432009-10-28 14:53:37 +00001192 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001193 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001194 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001195 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001196 frame_->EmitPush(r0);
1197 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001198 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199}
1200
1201
ager@chromium.org7c537e22008-10-16 08:43:32 +00001202void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001203#ifdef DEBUG
1204 int original_height = frame_->height();
1205#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001206 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001207 Comment cmnt(masm_, "[ Declaration");
1208 Variable* var = node->proxy()->var();
1209 ASSERT(var != NULL); // must have been resolved
1210 Slot* slot = var->slot();
1211
1212 // If it was not possible to allocate the variable at compile time,
1213 // we need to "declare" it at runtime to make sure it actually
1214 // exists in the local context.
1215 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1216 // Variables with a "LOOKUP" slot were introduced as non-locals
1217 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001218 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001219 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001220 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001221 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001222 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001223 // Declaration nodes are always declared in only two modes.
1224 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1225 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001226 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001227 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001228 // Push initial value, if any.
1229 // Note: For variables we must not push an initial value (such as
1230 // 'undefined') because we may have a (legal) redeclaration and we
1231 // must not destroy the current value.
1232 if (node->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001233 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001234 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001235 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001236 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001237 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001238 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001239 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001240 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001241 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001242 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001243 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001244 return;
1245 }
1246
1247 ASSERT(!var->is_global());
1248
1249 // If we have a function or a constant, we need to initialize the variable.
1250 Expression* val = NULL;
1251 if (node->mode() == Variable::CONST) {
1252 val = new Literal(Factory::the_hole_value());
1253 } else {
1254 val = node->fun(); // NULL if we don't have a function
1255 }
1256
1257 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001258 {
1259 // Set initial value.
1260 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001261 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001262 target.SetValue(NOT_CONST_INIT);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001263 }
1264 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001265 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001266 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001267 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001268}
1269
1270
ager@chromium.org7c537e22008-10-16 08:43:32 +00001271void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001272#ifdef DEBUG
1273 int original_height = frame_->height();
1274#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001275 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001277 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 Expression* expression = node->expression();
1279 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001280 LoadAndSpill(expression);
1281 frame_->Drop();
1282 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001283}
1284
1285
ager@chromium.org7c537e22008-10-16 08:43:32 +00001286void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001287#ifdef DEBUG
1288 int original_height = frame_->height();
1289#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001290 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001291 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001292 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001293 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001294 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001295}
1296
1297
ager@chromium.org7c537e22008-10-16 08:43:32 +00001298void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001299#ifdef DEBUG
1300 int original_height = frame_->height();
1301#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001302 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001304 // Generate different code depending on which parts of the if statement
1305 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001306 bool has_then_stm = node->HasThenStatement();
1307 bool has_else_stm = node->HasElseStatement();
1308
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001309 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001310
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001311 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001312 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001313 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001314 JumpTarget then;
1315 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001317 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001318 if (frame_ != NULL) {
1319 Branch(false, &else_);
1320 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001321 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001322 if (frame_ != NULL || then.is_linked()) {
1323 then.Bind();
1324 VisitAndSpill(node->then_statement());
1325 }
1326 if (frame_ != NULL) {
1327 exit.Jump();
1328 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001329 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001330 if (else_.is_linked()) {
1331 else_.Bind();
1332 VisitAndSpill(node->else_statement());
1333 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334
1335 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001336 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001337 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001338 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001339 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001340 LoadConditionAndSpill(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001341 if (frame_ != NULL) {
1342 Branch(false, &exit);
1343 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001344 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001345 if (frame_ != NULL || then.is_linked()) {
1346 then.Bind();
1347 VisitAndSpill(node->then_statement());
1348 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349
1350 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001351 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001353 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001354 // if (!cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001355 LoadConditionAndSpill(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001356 if (frame_ != NULL) {
1357 Branch(true, &exit);
1358 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001359 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001360 if (frame_ != NULL || else_.is_linked()) {
1361 else_.Bind();
1362 VisitAndSpill(node->else_statement());
1363 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001364
1365 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001366 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001367 ASSERT(!has_then_stm && !has_else_stm);
1368 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001369 LoadConditionAndSpill(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001370 if (frame_ != NULL) {
1371 if (has_cc()) {
1372 cc_reg_ = al;
1373 } else {
1374 frame_->Drop();
1375 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376 }
1377 }
1378
1379 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001380 if (exit.is_linked()) {
1381 exit.Bind();
1382 }
1383 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001384}
1385
1386
ager@chromium.org7c537e22008-10-16 08:43:32 +00001387void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001388 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001389 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001390 CodeForStatementPosition(node);
1391 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392}
1393
1394
ager@chromium.org7c537e22008-10-16 08:43:32 +00001395void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001396 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001397 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001398 CodeForStatementPosition(node);
1399 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001400}
1401
1402
ager@chromium.org7c537e22008-10-16 08:43:32 +00001403void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001404 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001405 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001406
ager@chromium.org4af710e2009-09-15 12:20:11 +00001407 CodeForStatementPosition(node);
1408 LoadAndSpill(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001409 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001410 frame_->EmitPop(r0);
1411 function_return_.Jump();
1412 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001413 // Pop the result from the frame and prepare the frame for
1414 // returning thus making it easier to merge.
1415 frame_->EmitPop(r0);
1416 frame_->PrepareForReturn();
1417
1418 function_return_.Jump();
1419 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001420}
1421
1422
ager@chromium.org7c537e22008-10-16 08:43:32 +00001423void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001424#ifdef DEBUG
1425 int original_height = frame_->height();
1426#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001427 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001428 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001429 CodeForStatementPosition(node);
1430 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001431 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001432 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001433 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001434 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001435 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001436#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001437 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001438 __ cmp(r0, Operand(cp));
1439 verified_true.Branch(eq);
1440 __ stop("PushContext: r0 is expected to be the same as cp");
1441 verified_true.Bind();
1442#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001444 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001445 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001446}
1447
1448
ager@chromium.org7c537e22008-10-16 08:43:32 +00001449void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001450#ifdef DEBUG
1451 int original_height = frame_->height();
1452#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001453 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001454 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001455 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001456 // Pop context.
1457 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1458 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001459 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001460 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001461}
1462
1463
ager@chromium.org7c537e22008-10-16 08:43:32 +00001464void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001465#ifdef DEBUG
1466 int original_height = frame_->height();
1467#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001468 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001470 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001471 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001472
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001473 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001474
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001475 JumpTarget next_test;
1476 JumpTarget fall_through;
1477 JumpTarget default_entry;
1478 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479 ZoneList<CaseClause*>* cases = node->cases();
1480 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001481 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482
1483 for (int i = 0; i < length; i++) {
1484 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001485 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001486 // Remember the default clause and compile it at the end.
1487 default_clause = clause;
1488 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001489 }
1490
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001491 Comment cmnt(masm_, "[ Case clause");
1492 // Compile the test.
1493 next_test.Bind();
1494 next_test.Unuse();
1495 // Duplicate TOS.
1496 __ ldr(r0, frame_->Top());
1497 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001498 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001499 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001500
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001501 // Before entering the body from the test, remove the switch value from
1502 // the stack.
1503 frame_->Drop();
1504
1505 // Label the body so that fall through is enabled.
1506 if (i > 0 && cases->at(i - 1)->is_default()) {
1507 default_exit.Bind();
1508 } else {
1509 fall_through.Bind();
1510 fall_through.Unuse();
1511 }
1512 VisitStatementsAndSpill(clause->statements());
1513
1514 // If control flow can fall through from the body, jump to the next body
1515 // or the end of the statement.
1516 if (frame_ != NULL) {
1517 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1518 default_entry.Jump();
1519 } else {
1520 fall_through.Jump();
1521 }
1522 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001523 }
1524
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001525 // The final "test" removes the switch value.
1526 next_test.Bind();
1527 frame_->Drop();
1528
1529 // If there is a default clause, compile it.
1530 if (default_clause != NULL) {
1531 Comment cmnt(masm_, "[ Default clause");
1532 default_entry.Bind();
1533 VisitStatementsAndSpill(default_clause->statements());
1534 // If control flow can fall out of the default and there is a case after
1535 // it, jup to that case's body.
1536 if (frame_ != NULL && default_exit.is_bound()) {
1537 default_exit.Jump();
1538 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001539 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001540
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001541 if (fall_through.is_linked()) {
1542 fall_through.Bind();
1543 }
1544
1545 if (node->break_target()->is_linked()) {
1546 node->break_target()->Bind();
1547 }
1548 node->break_target()->Unuse();
1549 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001550}
1551
1552
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001553void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001554#ifdef DEBUG
1555 int original_height = frame_->height();
1556#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001557 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001558 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001559 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001560 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001561 JumpTarget body(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001562
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001563 // Label the top of the loop for the backward CFG edge. If the test
1564 // is always true we can use the continue target, and if the test is
1565 // always false there is no need.
1566 ConditionAnalysis info = AnalyzeCondition(node->cond());
1567 switch (info) {
1568 case ALWAYS_TRUE:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001569 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001570 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001571 break;
1572 case ALWAYS_FALSE:
1573 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1574 break;
1575 case DONT_KNOW:
1576 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1577 body.Bind();
1578 break;
1579 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001580
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001581 CheckStack(); // TODO(1222600): ignore if body contains calls.
1582 VisitAndSpill(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001583
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001584 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001585 switch (info) {
1586 case ALWAYS_TRUE:
1587 // If control can fall off the end of the body, jump back to the
1588 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001589 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001590 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001591 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001592 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001593 case ALWAYS_FALSE:
1594 // If we have a continue in the body, we only have to bind its
1595 // jump target.
1596 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001597 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001598 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001599 break;
1600 case DONT_KNOW:
1601 // We have to compile the test expression if it can be reached by
1602 // control flow falling out of the body or via continue.
1603 if (node->continue_target()->is_linked()) {
1604 node->continue_target()->Bind();
1605 }
1606 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001607 Comment cmnt(masm_, "[ DoWhileCondition");
1608 CodeForDoWhileConditionPosition(node);
1609 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001610 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001611 // A invalid frame here indicates that control did not
1612 // fall out of the test expression.
1613 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001614 }
1615 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001616 break;
1617 }
1618
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001619 if (node->break_target()->is_linked()) {
1620 node->break_target()->Bind();
1621 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001622 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1623}
1624
1625
1626void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
1627#ifdef DEBUG
1628 int original_height = frame_->height();
1629#endif
1630 VirtualFrame::SpilledScope spilled_scope;
1631 Comment cmnt(masm_, "[ WhileStatement");
1632 CodeForStatementPosition(node);
1633
1634 // If the test is never true and has no side effects there is no need
1635 // to compile the test or body.
1636 ConditionAnalysis info = AnalyzeCondition(node->cond());
1637 if (info == ALWAYS_FALSE) return;
1638
1639 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1640
1641 // Label the top of the loop with the continue target for the backward
1642 // CFG edge.
1643 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
1644 node->continue_target()->Bind();
1645
1646 if (info == DONT_KNOW) {
1647 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001648 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001649 if (has_valid_frame()) {
1650 // A NULL frame indicates that control did not fall out of the
1651 // test expression.
1652 Branch(false, node->break_target());
1653 }
1654 if (has_valid_frame() || body.is_linked()) {
1655 body.Bind();
1656 }
1657 }
1658
1659 if (has_valid_frame()) {
1660 CheckStack(); // TODO(1222600): ignore if body contains calls.
1661 VisitAndSpill(node->body());
1662
1663 // If control flow can fall out of the body, jump back to the top.
1664 if (has_valid_frame()) {
1665 node->continue_target()->Jump();
1666 }
1667 }
1668 if (node->break_target()->is_linked()) {
1669 node->break_target()->Bind();
1670 }
1671 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1672}
1673
1674
1675void CodeGenerator::VisitForStatement(ForStatement* node) {
1676#ifdef DEBUG
1677 int original_height = frame_->height();
1678#endif
1679 VirtualFrame::SpilledScope spilled_scope;
1680 Comment cmnt(masm_, "[ ForStatement");
1681 CodeForStatementPosition(node);
1682 if (node->init() != NULL) {
1683 VisitAndSpill(node->init());
1684 }
1685
1686 // If the test is never true there is no need to compile the test or
1687 // body.
1688 ConditionAnalysis info = AnalyzeCondition(node->cond());
1689 if (info == ALWAYS_FALSE) return;
1690
1691 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1692
1693 // If there is no update statement, label the top of the loop with the
1694 // continue target, otherwise with the loop target.
1695 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
1696 if (node->next() == NULL) {
1697 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
1698 node->continue_target()->Bind();
1699 } else {
1700 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1701 loop.Bind();
1702 }
1703
1704 // If the test is always true, there is no need to compile it.
1705 if (info == DONT_KNOW) {
1706 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001707 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001708 if (has_valid_frame()) {
1709 Branch(false, node->break_target());
1710 }
1711 if (has_valid_frame() || body.is_linked()) {
1712 body.Bind();
1713 }
1714 }
1715
1716 if (has_valid_frame()) {
1717 CheckStack(); // TODO(1222600): ignore if body contains calls.
1718 VisitAndSpill(node->body());
1719
1720 if (node->next() == NULL) {
1721 // If there is no update statement and control flow can fall out
1722 // of the loop, jump directly to the continue label.
1723 if (has_valid_frame()) {
1724 node->continue_target()->Jump();
1725 }
1726 } else {
1727 // If there is an update statement and control flow can reach it
1728 // via falling out of the body of the loop or continuing, we
1729 // compile the update statement.
1730 if (node->continue_target()->is_linked()) {
1731 node->continue_target()->Bind();
1732 }
1733 if (has_valid_frame()) {
1734 // Record source position of the statement as this code which is
1735 // after the code for the body actually belongs to the loop
1736 // statement and not the body.
1737 CodeForStatementPosition(node);
1738 VisitAndSpill(node->next());
1739 loop.Jump();
1740 }
1741 }
1742 }
1743 if (node->break_target()->is_linked()) {
1744 node->break_target()->Bind();
1745 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001746 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001747}
1748
1749
ager@chromium.org7c537e22008-10-16 08:43:32 +00001750void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001751#ifdef DEBUG
1752 int original_height = frame_->height();
1753#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001754 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001756 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001757
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001758 JumpTarget primitive;
1759 JumpTarget jsobject;
1760 JumpTarget fixed_array;
1761 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
1762 JumpTarget end_del_check;
1763 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001764
1765 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001766 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767
1768 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1769 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001770 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001771 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1772 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001773 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001774 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1775 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001776 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001777
1778 // Stack layout in body:
1779 // [iteration counter (Smi)]
1780 // [length of array]
1781 // [FixedArray]
1782 // [Map or 0]
1783 // [Object]
1784
1785 // Check if enumerable is already a JSObject
1786 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001787 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001788 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001789 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001790
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001791 primitive.Bind();
1792 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001793 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001794
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001795 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001796 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001797 // r0: value to be iterated over
1798 frame_->EmitPush(r0); // Push the object being iterated over.
1799
1800 // Check cache validity in generated code. This is a fast case for
1801 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1802 // guarantee cache validity, call the runtime system to check cache
1803 // validity or get the property names in a fixed array.
1804 JumpTarget call_runtime;
1805 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
1806 JumpTarget check_prototype;
1807 JumpTarget use_cache;
1808 __ mov(r1, Operand(r0));
1809 loop.Bind();
1810 // Check that there are no elements.
1811 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
1812 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
1813 __ cmp(r2, r4);
1814 call_runtime.Branch(ne);
1815 // Check that instance descriptors are not empty so that we can
1816 // check for an enum cache. Leave the map in r3 for the subsequent
1817 // prototype load.
1818 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
1819 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
1820 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
1821 __ cmp(r2, ip);
1822 call_runtime.Branch(eq);
1823 // Check that there in an enum cache in the non-empty instance
1824 // descriptors. This is the case if the next enumeration index
1825 // field does not contain a smi.
1826 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
1827 __ tst(r2, Operand(kSmiTagMask));
1828 call_runtime.Branch(eq);
1829 // For all objects but the receiver, check that the cache is empty.
1830 // r4: empty fixed array root.
1831 __ cmp(r1, r0);
1832 check_prototype.Branch(eq);
1833 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
1834 __ cmp(r2, r4);
1835 call_runtime.Branch(ne);
1836 check_prototype.Bind();
1837 // Load the prototype from the map and loop if non-null.
1838 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
1839 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1840 __ cmp(r1, ip);
1841 loop.Branch(ne);
1842 // The enum cache is valid. Load the map of the object being
1843 // iterated over and use the cache for the iteration.
1844 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
1845 use_cache.Jump();
1846
1847 call_runtime.Bind();
1848 // Call the runtime to get the property names for the object.
1849 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001850 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001852 // If we got a map from the runtime call, we can do a fast
1853 // modification check. Otherwise, we got a fixed array, and we have
1854 // to do a slow check.
1855 // r0: map or fixed array (result from call to
1856 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857 __ mov(r2, Operand(r0));
1858 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001859 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
1860 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001861 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001862
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001863 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001864 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001865 // r0: map (either the result from a call to
1866 // Runtime::kGetPropertyNamesFast or has been fetched directly from
1867 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 __ mov(r1, Operand(r0));
1869 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1870 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1871 __ ldr(r2,
1872 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1873
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001874 frame_->EmitPush(r0); // map
1875 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001876 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001877 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001878 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001879 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001880 frame_->EmitPush(r0);
1881 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001882
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001883 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001885 frame_->EmitPush(r1); // insert 0 in place of Map
1886 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887
1888 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001889 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001890 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001891 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001892 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001893 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001894
1895 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001896 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00001897 // sp[0] : index
1898 // sp[1] : array/enum cache length
1899 // sp[2] : array or enum cache
1900 // sp[3] : 0 or map
1901 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001902 // Grab the current frame's height for the break and continue
1903 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001904 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1905 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001906
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001907 __ ldr(r0, frame_->ElementAt(0)); // load the current count
1908 __ ldr(r1, frame_->ElementAt(1)); // load the length
1909 __ cmp(r0, Operand(r1)); // compare to the array length
1910 node->break_target()->Branch(hs);
1911
1912 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001913
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001914 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001915 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001916 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1917 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1918
1919 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001920 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001921 // Check if this (still) matches the map of the enumerable.
1922 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001923 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1925 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001926 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927
1928 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001929 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
1930 frame_->EmitPush(r0);
1931 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001932 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001933 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001934
1935 // If the property has been removed while iterating, we just skip it.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001936 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1937 __ cmp(r3, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001938 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001939
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001940 end_del_check.Bind();
1941 // Store the entry in the 'each' expression and take another spin in the
1942 // loop. r3: i'th entry of the enum cache (or string there of)
1943 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001944 { Reference each(this, node->each());
1945 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001946 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001947 __ ldr(r0, frame_->ElementAt(each.size()));
1948 frame_->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001949 each.SetValue(NOT_CONST_INIT);
1950 frame_->Drop(2);
1951 } else {
1952 // If the reference was to a slot we rely on the convenient property
1953 // that it doesn't matter whether a value (eg, r3 pushed above) is
1954 // right on top of or right underneath a zero-sized reference.
1955 each.SetValue(NOT_CONST_INIT);
1956 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00001957 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001958 }
1959 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001960 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001961 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001962 VisitAndSpill(node->body());
1963
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001964 // Next. Reestablish a spilled frame in case we are coming here via
1965 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001966 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001967 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001968 frame_->EmitPop(r0);
1969 __ add(r0, r0, Operand(Smi::FromInt(1)));
1970 frame_->EmitPush(r0);
1971 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001972
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001973 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
1974 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001975 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001976 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001977
1978 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001979 exit.Bind();
1980 node->continue_target()->Unuse();
1981 node->break_target()->Unuse();
1982 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001983}
1984
1985
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001986void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001987#ifdef DEBUG
1988 int original_height = frame_->height();
1989#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001990 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001991 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001992 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001994 JumpTarget try_block;
1995 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001996
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001997 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001998 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001999 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002000
2001 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002002 Variable* catch_var = node->catch_var()->var();
2003 ASSERT(catch_var != NULL && catch_var->slot() != NULL);
2004 StoreToSlot(catch_var->slot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002005
2006 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002007 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002008
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002009 VisitStatementsAndSpill(node->catch_block()->statements());
2010 if (frame_ != NULL) {
2011 exit.Jump();
2012 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002013
2014
2015 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002016 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002017
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002018 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2019 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002021 // Shadow the labels for all escapes from the try block, including
2022 // returns. During shadowing, the original label is hidden as the
2023 // LabelShadow and operations on the original actually affect the
2024 // shadowing label.
2025 //
2026 // We should probably try to unify the escaping labels and the return
2027 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002028 int nof_escapes = node->escaping_targets()->length();
2029 List<ShadowTarget*> shadows(1 + nof_escapes);
2030
2031 // Add the shadow target for the function return.
2032 static const int kReturnShadowIndex = 0;
2033 shadows.Add(new ShadowTarget(&function_return_));
2034 bool function_return_was_shadowed = function_return_is_shadowed_;
2035 function_return_is_shadowed_ = true;
2036 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2037
2038 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002039 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002040 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041 }
2042
2043 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002044 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045
2046 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002047 // After shadowing stops, the original labels are unshadowed and the
2048 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002049 bool has_unlinks = false;
2050 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002052 has_unlinks = has_unlinks || shadows[i]->is_linked();
2053 }
2054 function_return_is_shadowed_ = function_return_was_shadowed;
2055
2056 // Get an external reference to the handler address.
2057 ExternalReference handler_address(Top::k_handler_address);
2058
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002059 // If we can fall off the end of the try block, unlink from try chain.
2060 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002061 // The next handler address is on top of the frame. Unlink from
2062 // the handler list and drop the rest of this handler from the
2063 // frame.
2064 ASSERT(StackHandlerConstants::kNextOffset == 0);
2065 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002066 __ mov(r3, Operand(handler_address));
2067 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002068 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002069 if (has_unlinks) {
2070 exit.Jump();
2071 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072 }
2073
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002074 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002075 // jumped to. Deallocate each shadow target.
2076 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002077 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002078 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002079 shadows[i]->Bind();
2080 // Because we can be jumping here (to spilled code) from unspilled
2081 // code, we need to reestablish a spilled frame at this block.
2082 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002083
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002084 // Reload sp from the top handler, because some statements that we
2085 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002086 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002087 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002088 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002089
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002090 ASSERT(StackHandlerConstants::kNextOffset == 0);
2091 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002093 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002094
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002095 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2096 frame_->PrepareForReturn();
2097 }
2098 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002099 }
2100 }
2101
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002102 exit.Bind();
2103 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002104}
2105
2106
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002107void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002108#ifdef DEBUG
2109 int original_height = frame_->height();
2110#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002111 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002112 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002113 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002114
2115 // State: Used to keep track of reason for entering the finally
2116 // block. Should probably be extended to hold information for
2117 // break/continue from within the try block.
2118 enum { FALLING, THROWING, JUMPING };
2119
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002120 JumpTarget try_block;
2121 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002122
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002123 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002124
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002125 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002126 // In case of thrown exceptions, this is where we continue.
2127 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002128 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002129
2130 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002131 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002132
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002133 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2134 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002135
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002136 // Shadow the labels for all escapes from the try block, including
2137 // returns. Shadowing hides the original label as the LabelShadow and
2138 // operations on the original actually affect the shadowing label.
2139 //
2140 // We should probably try to unify the escaping labels and the return
2141 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002142 int nof_escapes = node->escaping_targets()->length();
2143 List<ShadowTarget*> shadows(1 + nof_escapes);
2144
2145 // Add the shadow target for the function return.
2146 static const int kReturnShadowIndex = 0;
2147 shadows.Add(new ShadowTarget(&function_return_));
2148 bool function_return_was_shadowed = function_return_is_shadowed_;
2149 function_return_is_shadowed_ = true;
2150 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2151
2152 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002153 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002154 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002155 }
2156
2157 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002158 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002159
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002160 // Stop the introduced shadowing and count the number of required unlinks.
2161 // After shadowing stops, the original labels are unshadowed and the
2162 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002163 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002164 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002165 shadows[i]->StopShadowing();
2166 if (shadows[i]->is_linked()) nof_unlinks++;
2167 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002168 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002169
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002170 // Get an external reference to the handler address.
2171 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002172
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002173 // If we can fall off the end of the try block, unlink from the try
2174 // chain and set the state on the frame to FALLING.
2175 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002176 // The next handler address is on top of the frame.
2177 ASSERT(StackHandlerConstants::kNextOffset == 0);
2178 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002179 __ mov(r3, Operand(handler_address));
2180 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002181 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002182
2183 // Fake a top of stack value (unneeded when FALLING) and set the
2184 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002185 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002186 frame_->EmitPush(r0);
2187 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2188 if (nof_unlinks > 0) {
2189 finally_block.Jump();
2190 }
2191 }
2192
2193 // Generate code to unlink and set the state for the (formerly)
2194 // shadowing targets that have been jumped to.
2195 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002196 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002197 // If we have come from the shadowed return, the return value is
2198 // in (a non-refcounted reference to) r0. We must preserve it
2199 // until it is pushed.
2200 //
2201 // Because we can be jumping here (to spilled code) from
2202 // unspilled code, we need to reestablish a spilled frame at
2203 // this block.
2204 shadows[i]->Bind();
2205 frame_->SpillAll();
2206
2207 // Reload sp from the top handler, because some statements that
2208 // we break from (eg, for...in) may have left stuff on the
2209 // stack.
2210 __ mov(r3, Operand(handler_address));
2211 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002212 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002213
2214 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002215 // handler address is currently on top of the frame.
2216 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002217 frame_->EmitPop(r1);
2218 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002219 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002220
2221 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002222 // If this label shadowed the function return, materialize the
2223 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002224 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002225 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002226 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002227 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002228 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002229 }
2230 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002231 if (--nof_unlinks > 0) {
2232 // If this is not the last unlink block, jump around the next.
2233 finally_block.Jump();
2234 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002235 }
2236 }
2237
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002238 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002239 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002240
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002241 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002242 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002243
2244 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002245 // and the state - while evaluating the finally block.
2246 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002248 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002249
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002250 if (has_valid_frame()) {
2251 // Restore state and return value or faked TOS.
2252 frame_->EmitPop(r2);
2253 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002254 }
2255
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002256 // Generate code to jump to the right destination for all used
2257 // formerly shadowing targets. Deallocate each shadow target.
2258 for (int i = 0; i < shadows.length(); i++) {
2259 if (has_valid_frame() && shadows[i]->is_bound()) {
2260 JumpTarget* original = shadows[i]->other_target();
2261 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2262 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002263 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002264 skip.Branch(ne);
2265 frame_->PrepareForReturn();
2266 original->Jump();
2267 skip.Bind();
2268 } else {
2269 original->Branch(eq);
2270 }
2271 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002272 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002273
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002274 if (has_valid_frame()) {
2275 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002276 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002277 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2278 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002279
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002280 // Rethrow exception.
2281 frame_->EmitPush(r0);
2282 frame_->CallRuntime(Runtime::kReThrow, 1);
2283
2284 // Done.
2285 exit.Bind();
2286 }
2287 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002288}
2289
2290
ager@chromium.org7c537e22008-10-16 08:43:32 +00002291void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002292#ifdef DEBUG
2293 int original_height = frame_->height();
2294#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002295 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002296 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002297 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002298#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +00002299 frame_->DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002300#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002301 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002302 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303}
2304
2305
ager@chromium.org7c537e22008-10-16 08:43:32 +00002306void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002307 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308 ASSERT(boilerplate->IsBoilerplate());
2309
ager@chromium.org3811b432009-10-28 14:53:37 +00002310 __ mov(r0, Operand(boilerplate));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002311 // Use the fast case closure allocation code that allocates in new
2312 // space for nested functions that don't need literals cloning.
2313 if (scope()->is_function_scope() && boilerplate->NumberOfLiterals() == 0) {
2314 FastNewClosureStub stub;
2315 frame_->EmitPush(r0);
2316 frame_->CallStub(&stub, 1);
2317 frame_->EmitPush(r0);
2318 } else {
2319 // Create a new closure.
2320 frame_->EmitPush(cp);
2321 frame_->EmitPush(r0);
2322 frame_->CallRuntime(Runtime::kNewClosure, 2);
2323 frame_->EmitPush(r0);
2324 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002325}
2326
2327
ager@chromium.org7c537e22008-10-16 08:43:32 +00002328void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002329#ifdef DEBUG
2330 int original_height = frame_->height();
2331#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002332 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002333 Comment cmnt(masm_, "[ FunctionLiteral");
2334
2335 // Build the function boilerplate and instantiate it.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002336 Handle<JSFunction> boilerplate =
ager@chromium.org5c838252010-02-19 08:53:10 +00002337 Compiler::BuildBoilerplate(node, script(), this);
kasper.lund212ac232008-07-16 07:07:30 +00002338 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002339 if (HasStackOverflow()) {
2340 ASSERT(frame_->height() == original_height);
2341 return;
2342 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002343 InstantiateBoilerplate(boilerplate);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002344 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002345}
2346
2347
ager@chromium.org7c537e22008-10-16 08:43:32 +00002348void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002349 FunctionBoilerplateLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002350#ifdef DEBUG
2351 int original_height = frame_->height();
2352#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002353 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002354 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2355 InstantiateBoilerplate(node->boilerplate());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002356 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002357}
2358
2359
ager@chromium.org7c537e22008-10-16 08:43:32 +00002360void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002361#ifdef DEBUG
2362 int original_height = frame_->height();
2363#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002364 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002365 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002366 JumpTarget then;
2367 JumpTarget else_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002368 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002369 if (has_valid_frame()) {
2370 Branch(false, &else_);
2371 }
2372 if (has_valid_frame() || then.is_linked()) {
2373 then.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002374 LoadAndSpill(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002375 }
2376 if (else_.is_linked()) {
2377 JumpTarget exit;
2378 if (has_valid_frame()) exit.Jump();
2379 else_.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002380 LoadAndSpill(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002381 if (exit.is_linked()) exit.Bind();
2382 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002383 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002384}
2385
2386
ager@chromium.org7c537e22008-10-16 08:43:32 +00002387void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002388 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002389 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002390 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002391
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002392 JumpTarget slow;
2393 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002394
2395 // Generate fast-case code for variables that might be shadowed by
2396 // eval-introduced variables. Eval is used a lot without
2397 // introducing variables. In those cases, we do not want to
2398 // perform a runtime call for all variables in the scope
2399 // containing the eval.
2400 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2401 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002402 // If there was no control flow to slow, we can exit early.
2403 if (!slow.is_linked()) {
2404 frame_->EmitPush(r0);
2405 return;
2406 }
2407
2408 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002409
2410 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2411 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2412 // Only generate the fast case for locals that rewrite to slots.
2413 // This rules out argument loads.
2414 if (potential_slot != NULL) {
2415 __ ldr(r0,
2416 ContextSlotOperandCheckExtensions(potential_slot,
2417 r1,
2418 r2,
2419 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002420 if (potential_slot->var()->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002421 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2422 __ cmp(r0, ip);
2423 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002424 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002425 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002426 // ContextSlotOperandCheckExtensions so we have to jump around
2427 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002428 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002429 }
2430 }
2431
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002432 slow.Bind();
2433 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002434 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002435 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002436
ager@chromium.org7c537e22008-10-16 08:43:32 +00002437 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002438 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002439 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002440 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002441 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002442
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002443 done.Bind();
2444 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002445
2446 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002447 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002448 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002449 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002450 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002451 // Const slots may contain 'the hole' value (the constant hasn't been
2452 // initialized yet) which needs to be converted into the 'undefined'
2453 // value.
2454 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002455 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002456 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2457 __ cmp(r0, ip);
2458 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002459 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002460 }
2461 }
2462}
2463
2464
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002465void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
2466 ASSERT(slot != NULL);
2467 if (slot->type() == Slot::LOOKUP) {
2468 ASSERT(slot->var()->is_dynamic());
2469
2470 // For now, just do a runtime call.
2471 frame_->EmitPush(cp);
2472 __ mov(r0, Operand(slot->var()->name()));
2473 frame_->EmitPush(r0);
2474
2475 if (init_state == CONST_INIT) {
2476 // Same as the case for a normal store, but ignores attribute
2477 // (e.g. READ_ONLY) of context slot so that we can initialize
2478 // const properties (introduced via eval("const foo = (some
2479 // expr);")). Also, uses the current function context instead of
2480 // the top context.
2481 //
2482 // Note that we must declare the foo upon entry of eval(), via a
2483 // context slot declaration, but we cannot initialize it at the
2484 // same time, because the const declaration may be at the end of
2485 // the eval code (sigh...) and the const variable may have been
2486 // used before (where its value is 'undefined'). Thus, we can only
2487 // do the initialization when we actually encounter the expression
2488 // and when the expression operands are defined and valid, and
2489 // thus we need the split into 2 operations: declaration of the
2490 // context slot followed by initialization.
2491 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
2492 } else {
2493 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
2494 }
2495 // Storing a variable must keep the (new) value on the expression
2496 // stack. This is necessary for compiling assignment expressions.
2497 frame_->EmitPush(r0);
2498
2499 } else {
2500 ASSERT(!slot->var()->is_dynamic());
2501
2502 JumpTarget exit;
2503 if (init_state == CONST_INIT) {
2504 ASSERT(slot->var()->mode() == Variable::CONST);
2505 // Only the first const initialization must be executed (the slot
2506 // still contains 'the hole' value). When the assignment is
2507 // executed, the code is identical to a normal store (see below).
2508 Comment cmnt(masm_, "[ Init const");
2509 __ ldr(r2, SlotOperand(slot, r2));
2510 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2511 __ cmp(r2, ip);
2512 exit.Branch(ne);
2513 }
2514
2515 // We must execute the store. Storing a variable must keep the
2516 // (new) value on the stack. This is necessary for compiling
2517 // assignment expressions.
2518 //
2519 // Note: We will reach here even with slot->var()->mode() ==
2520 // Variable::CONST because of const declarations which will
2521 // initialize consts to 'the hole' value and by doing so, end up
2522 // calling this code. r2 may be loaded with context; used below in
2523 // RecordWrite.
2524 frame_->EmitPop(r0);
2525 __ str(r0, SlotOperand(slot, r2));
2526 frame_->EmitPush(r0);
2527 if (slot->type() == Slot::CONTEXT) {
2528 // Skip write barrier if the written value is a smi.
2529 __ tst(r0, Operand(kSmiTagMask));
2530 exit.Branch(eq);
2531 // r2 is loaded with context when calling SlotOperand above.
2532 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
2533 __ mov(r3, Operand(offset));
2534 __ RecordWrite(r2, r3, r1);
2535 }
2536 // If we definitely did not jump over the assignment, we do not need
2537 // to bind the exit label. Doing so can defeat peephole
2538 // optimization.
2539 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
2540 exit.Bind();
2541 }
2542 }
2543}
2544
2545
ager@chromium.org381abbb2009-02-25 13:23:22 +00002546void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2547 TypeofState typeof_state,
2548 Register tmp,
2549 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002550 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002551 // Check that no extension objects have been created by calls to
2552 // eval from the current scope to the global scope.
2553 Register context = cp;
2554 Scope* s = scope();
2555 while (s != NULL) {
2556 if (s->num_heap_slots() > 0) {
2557 if (s->calls_eval()) {
2558 // Check that extension is NULL.
2559 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2560 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002561 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002562 }
2563 // Load next context in chain.
2564 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2565 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2566 context = tmp;
2567 }
2568 // If no outer scope calls eval, we do not need to check more
2569 // context extensions.
2570 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2571 s = s->outer_scope();
2572 }
2573
2574 if (s->is_eval_scope()) {
2575 Label next, fast;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002576 if (!context.is(tmp)) {
2577 __ mov(tmp, Operand(context));
2578 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002579 __ bind(&next);
2580 // Terminate at global context.
2581 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002582 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
2583 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002584 __ b(eq, &fast);
2585 // Check that extension is NULL.
2586 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2587 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002588 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002589 // Load next context in chain.
2590 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2591 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2592 __ b(&next);
2593 __ bind(&fast);
2594 }
2595
2596 // All extension objects were empty and it is safe to use a global
2597 // load IC call.
2598 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2599 // Load the global object.
2600 LoadGlobal();
2601 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002602 __ mov(r2, Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002603 // Call IC stub.
2604 if (typeof_state == INSIDE_TYPEOF) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002605 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002606 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002607 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002608 }
2609
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002610 // Drop the global object. The result is in r0.
2611 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002612}
2613
2614
ager@chromium.org7c537e22008-10-16 08:43:32 +00002615void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002616#ifdef DEBUG
2617 int original_height = frame_->height();
2618#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002619 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002620 Comment cmnt(masm_, "[ Slot");
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002621 LoadFromSlot(node, NOT_INSIDE_TYPEOF);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002622 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002623}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002624
ager@chromium.org7c537e22008-10-16 08:43:32 +00002625
2626void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002627#ifdef DEBUG
2628 int original_height = frame_->height();
2629#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002630 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002631 Comment cmnt(masm_, "[ VariableProxy");
2632
2633 Variable* var = node->var();
2634 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002635 if (expr != NULL) {
2636 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002637 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002638 ASSERT(var->is_global());
2639 Reference ref(this, node);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002640 ref.GetValueAndSpill();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002641 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002642 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002643}
2644
2645
ager@chromium.org7c537e22008-10-16 08:43:32 +00002646void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002647#ifdef DEBUG
2648 int original_height = frame_->height();
2649#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002650 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002651 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002652 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002653 frame_->EmitPush(r0);
2654 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002655}
2656
2657
ager@chromium.org7c537e22008-10-16 08:43:32 +00002658void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002659#ifdef DEBUG
2660 int original_height = frame_->height();
2661#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002662 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002663 Comment cmnt(masm_, "[ RexExp Literal");
2664
2665 // Retrieve the literal array and check the allocated entry.
2666
2667 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002668 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002669
2670 // Load the literals array of the function.
2671 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2672
2673 // Load the literal at the ast saved index.
2674 int literal_offset =
2675 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2676 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2677
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002678 JumpTarget done;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002679 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2680 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002681 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002682
2683 // If the entry is undefined we call the runtime system to computed
2684 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002685 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002686 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002687 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002688 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002689 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002690 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002691 frame_->EmitPush(r0);
2692 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002693 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002694
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002695 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002696 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002697 frame_->EmitPush(r2);
2698 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002699}
2700
2701
ager@chromium.org7c537e22008-10-16 08:43:32 +00002702void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002703#ifdef DEBUG
2704 int original_height = frame_->height();
2705#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002706 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002707 Comment cmnt(masm_, "[ ObjectLiteral");
2708
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002709 // Load the function of this activation.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002710 __ ldr(r2, frame_->Function());
2711 // Literal array.
2712 __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset));
2713 // Literal index.
2714 __ mov(r1, Operand(Smi::FromInt(node->literal_index())));
2715 // Constant properties.
2716 __ mov(r0, Operand(node->constant_properties()));
2717 frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit());
2718 if (node->depth() > 1) {
2719 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 3);
2720 } else {
2721 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002722 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002723 frame_->EmitPush(r0); // save the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002724 for (int i = 0; i < node->properties()->length(); i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002725 // At the start of each iteration, the top of stack contains
2726 // the newly created object literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002727 ObjectLiteral::Property* property = node->properties()->at(i);
2728 Literal* key = property->key();
2729 Expression* value = property->value();
2730 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002731 case ObjectLiteral::Property::CONSTANT:
2732 break;
2733 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2734 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
2735 // else fall through
ager@chromium.org5c838252010-02-19 08:53:10 +00002736 case ObjectLiteral::Property::COMPUTED:
2737 if (key->handle()->IsSymbol()) {
2738 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
2739 LoadAndSpill(value);
2740 frame_->EmitPop(r0);
2741 __ mov(r2, Operand(key->handle()));
2742 __ ldr(r1, frame_->Top()); // Load the receiver.
2743 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
2744 break;
2745 }
2746 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002747 case ObjectLiteral::Property::PROTOTYPE: {
ager@chromium.org5c838252010-02-19 08:53:10 +00002748 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002749 frame_->EmitPush(r0); // dup the result
2750 LoadAndSpill(key);
2751 LoadAndSpill(value);
2752 frame_->CallRuntime(Runtime::kSetProperty, 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002753 break;
2754 }
2755 case ObjectLiteral::Property::SETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00002756 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002757 frame_->EmitPush(r0);
2758 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002759 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002760 frame_->EmitPush(r0);
2761 LoadAndSpill(value);
2762 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002763 break;
2764 }
2765 case ObjectLiteral::Property::GETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00002766 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002767 frame_->EmitPush(r0);
2768 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002769 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002770 frame_->EmitPush(r0);
2771 LoadAndSpill(value);
2772 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002773 break;
2774 }
2775 }
2776 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002777 ASSERT(frame_->height() == original_height + 1);
2778}
2779
2780
ager@chromium.org7c537e22008-10-16 08:43:32 +00002781void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002782#ifdef DEBUG
2783 int original_height = frame_->height();
2784#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002785 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002786 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002787
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002788 // Load the function of this activation.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002789 __ ldr(r2, frame_->Function());
ager@chromium.org5c838252010-02-19 08:53:10 +00002790 // Load the literals array of the function.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002791 __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002792 __ mov(r1, Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002793 __ mov(r0, Operand(node->constant_elements()));
2794 frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit());
ager@chromium.org5c838252010-02-19 08:53:10 +00002795 int length = node->values()->length();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002796 if (node->depth() > 1) {
2797 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00002798 } else if (length > FastCloneShallowArrayStub::kMaximumLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002799 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00002800 } else {
2801 FastCloneShallowArrayStub stub(length);
2802 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002803 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002804 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002805 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002806
2807 // Generate code to set the elements in the array that are not
2808 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002809 for (int i = 0; i < node->values()->length(); i++) {
2810 Expression* value = node->values()->at(i);
2811
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002812 // If value is a literal the property value is already set in the
2813 // boilerplate object.
2814 if (value->AsLiteral() != NULL) continue;
2815 // If value is a materialized literal the property value is already set
2816 // in the boilerplate object if it is simple.
2817 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002818
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002819 // The property must be set by generated code.
2820 LoadAndSpill(value);
2821 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002822
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002823 // Fetch the object literal.
2824 __ ldr(r1, frame_->Top());
2825 // Get the elements array.
2826 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002827
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002828 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002829 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002830 __ str(r0, FieldMemOperand(r1, offset));
2831
2832 // Update the write barrier for the array address.
2833 __ mov(r3, Operand(offset));
2834 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002835 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002836 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002837}
2838
2839
ager@chromium.org32912102009-01-16 10:38:43 +00002840void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002841#ifdef DEBUG
2842 int original_height = frame_->height();
2843#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002844 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org32912102009-01-16 10:38:43 +00002845 // Call runtime routine to allocate the catch extension object and
2846 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002847 Comment cmnt(masm_, "[ CatchExtensionObject");
2848 LoadAndSpill(node->key());
2849 LoadAndSpill(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002850 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2851 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002852 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002853}
2854
2855
ager@chromium.org7c537e22008-10-16 08:43:32 +00002856void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002857#ifdef DEBUG
2858 int original_height = frame_->height();
2859#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002860 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002861 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00002862
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002863 { Reference target(this, node->target(), node->is_compound());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002864 if (target.is_illegal()) {
2865 // Fool the virtual frame into thinking that we left the assignment's
2866 // value on the frame.
2867 __ mov(r0, Operand(Smi::FromInt(0)));
2868 frame_->EmitPush(r0);
2869 ASSERT(frame_->height() == original_height + 1);
2870 return;
2871 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002872
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002873 if (node->op() == Token::ASSIGN ||
2874 node->op() == Token::INIT_VAR ||
2875 node->op() == Token::INIT_CONST) {
2876 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002877
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002878 } else { // Assignment is a compound assignment.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002879 // Get the old value of the lhs.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002880 target.GetValueAndSpill();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002881 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002882 bool overwrite =
2883 (node->value()->AsBinaryOperation() != NULL &&
2884 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002885 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002886 SmiOperation(node->binary_op(),
2887 literal->handle(),
2888 false,
2889 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002890 frame_->EmitPush(r0);
2891
2892 } else {
2893 LoadAndSpill(node->value());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002894 GenericBinaryOperation(node->binary_op(),
2895 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002896 frame_->EmitPush(r0);
2897 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002898 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002899 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2900 if (var != NULL &&
2901 (var->mode() == Variable::CONST) &&
2902 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2903 // Assignment ignored - leave the value on the stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002904 UnloadReference(&target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002905 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002906 CodeForSourcePosition(node->position());
2907 if (node->op() == Token::INIT_CONST) {
2908 // Dynamic constant initializations must use the function context
2909 // and initialize the actual constant declared. Dynamic variable
2910 // initializations are simply assignments and use SetValue.
2911 target.SetValue(CONST_INIT);
2912 } else {
2913 target.SetValue(NOT_CONST_INIT);
2914 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002915 }
2916 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002917 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002918}
2919
2920
ager@chromium.org7c537e22008-10-16 08:43:32 +00002921void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002922#ifdef DEBUG
2923 int original_height = frame_->height();
2924#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002925 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002926 Comment cmnt(masm_, "[ Throw");
2927
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002928 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002929 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002930 frame_->CallRuntime(Runtime::kThrow, 1);
2931 frame_->EmitPush(r0);
2932 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002933}
2934
2935
ager@chromium.org7c537e22008-10-16 08:43:32 +00002936void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002937#ifdef DEBUG
2938 int original_height = frame_->height();
2939#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002940 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002941 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002942
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002943 { Reference property(this, node);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002944 property.GetValueAndSpill();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002945 }
2946 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002947}
2948
2949
ager@chromium.org7c537e22008-10-16 08:43:32 +00002950void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002951#ifdef DEBUG
2952 int original_height = frame_->height();
2953#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002954 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002955 Comment cmnt(masm_, "[ Call");
2956
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002957 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958 ZoneList<Expression*>* args = node->arguments();
2959
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002960 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002961 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002962 Variable* var = function->AsVariableProxy()->AsVariable();
2963 Property* property = function->AsProperty();
2964
2965 // ------------------------------------------------------------------------
2966 // Fast-case: Use inline caching.
2967 // ---
2968 // According to ECMA-262, section 11.2.3, page 44, the function to call
2969 // must be resolved after the arguments have been evaluated. The IC code
2970 // automatically handles this by loading the arguments before the function
2971 // is resolved in cache misses (this also holds for megamorphic calls).
2972 // ------------------------------------------------------------------------
2973
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002974 if (var != NULL && var->is_possibly_eval()) {
2975 // ----------------------------------
2976 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
2977 // ----------------------------------
2978
2979 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2980 // resolve the function we need to call and the receiver of the
2981 // call. Then we call the resolved function using the given
2982 // arguments.
2983 // Prepare stack for call to resolved function.
2984 LoadAndSpill(function);
2985 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
2986 frame_->EmitPush(r2); // Slot for receiver
2987 int arg_count = args->length();
2988 for (int i = 0; i < arg_count; i++) {
2989 LoadAndSpill(args->at(i));
2990 }
2991
2992 // Prepare stack for call to ResolvePossiblyDirectEval.
2993 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
2994 frame_->EmitPush(r1);
2995 if (arg_count > 0) {
2996 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
2997 frame_->EmitPush(r1);
2998 } else {
2999 frame_->EmitPush(r2);
3000 }
3001
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003002 // Push the receiver.
3003 __ ldr(r1, frame_->Receiver());
3004 frame_->EmitPush(r1);
3005
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003006 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003007 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003008
3009 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003010 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003011 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3012
3013 // Call the function.
3014 CodeForSourcePosition(node->position());
3015
3016 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003017 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003018 frame_->CallStub(&call_function, arg_count + 1);
3019
3020 __ ldr(cp, frame_->Context());
3021 // Remove the function from the stack.
3022 frame_->Drop();
3023 frame_->EmitPush(r0);
3024
3025 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003026 // ----------------------------------
3027 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3028 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003029 // Pass the global object as the receiver and let the IC stub
3030 // patch the stack to use the global proxy as 'this' in the
3031 // invoked function.
3032 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003033
3034 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003035 int arg_count = args->length();
3036 for (int i = 0; i < arg_count; i++) {
3037 LoadAndSpill(args->at(i));
3038 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003039
ager@chromium.org5c838252010-02-19 08:53:10 +00003040 // Setup the name register and call the IC initialization code.
3041 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003042 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3043 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003044 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003045 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3046 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003047 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003048 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003049
3050 } else if (var != NULL && var->slot() != NULL &&
3051 var->slot()->type() == Slot::LOOKUP) {
3052 // ----------------------------------
3053 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3054 // ----------------------------------
3055
3056 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003057 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003058 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003059 frame_->EmitPush(r0);
3060 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003061 // r0: slot value; r1: receiver
3062
3063 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003064 frame_->EmitPush(r0); // function
3065 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003066
3067 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003068 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003069 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003070
3071 } else if (property != NULL) {
3072 // Check if the key is a literal string.
3073 Literal* literal = property->key()->AsLiteral();
3074
3075 if (literal != NULL && literal->handle()->IsSymbol()) {
3076 // ------------------------------------------------------------------
3077 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3078 // ------------------------------------------------------------------
3079
ager@chromium.org5c838252010-02-19 08:53:10 +00003080 LoadAndSpill(property->obj()); // Receiver.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003081 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003082 int arg_count = args->length();
3083 for (int i = 0; i < arg_count; i++) {
3084 LoadAndSpill(args->at(i));
3085 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003086
ager@chromium.org5c838252010-02-19 08:53:10 +00003087 // Set the name register and call the IC initialization code.
3088 __ mov(r2, Operand(literal->handle()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003089 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3090 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003091 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003092 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003093 __ ldr(cp, frame_->Context());
ager@chromium.org5c838252010-02-19 08:53:10 +00003094 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003095
3096 } else {
3097 // -------------------------------------------
3098 // JavaScript example: 'array[index](1, 2, 3)'
3099 // -------------------------------------------
3100
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003101 LoadAndSpill(property->obj());
3102 LoadAndSpill(property->key());
3103 EmitKeyedLoad(false);
3104 frame_->Drop(); // key
3105 // Put the function below the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003106 if (property->is_synthetic()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003107 // Use the global receiver.
3108 frame_->Drop();
3109 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003110 LoadGlobalReceiver(r0);
3111 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003112 frame_->EmitPop(r1); // receiver
3113 frame_->EmitPush(r0); // function
3114 frame_->EmitPush(r1); // receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003115 }
3116
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003117 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003118 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003119 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003120 }
3121
3122 } else {
3123 // ----------------------------------
3124 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3125 // ----------------------------------
3126
3127 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003128 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003129
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003130 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003131 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003132
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003133 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003134 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003135 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003136 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003137 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003138}
3139
3140
ager@chromium.org7c537e22008-10-16 08:43:32 +00003141void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003142#ifdef DEBUG
3143 int original_height = frame_->height();
3144#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003145 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003146 Comment cmnt(masm_, "[ CallNew");
3147
3148 // According to ECMA-262, section 11.2.2, page 44, the function
3149 // expression in new calls must be evaluated before the
3150 // arguments. This is different from ordinary calls, where the
3151 // actual function to call is resolved after the arguments have been
3152 // evaluated.
3153
3154 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003155 // receiver. There is no need to use the global proxy here because
3156 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003157 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003158 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003159
3160 // Push the arguments ("left-to-right") on the stack.
3161 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003162 int arg_count = args->length();
3163 for (int i = 0; i < arg_count; i++) {
3164 LoadAndSpill(args->at(i));
3165 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003166
mads.s.ager31e71382008-08-13 09:32:07 +00003167 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003168 __ mov(r0, Operand(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003169 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003170 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003171
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003172 // Call the construct call builtin that handles allocation and
3173 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003174 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003175 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003176 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003177
3178 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003179 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003180 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003181}
3182
3183
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003184void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
3185 VirtualFrame::SpilledScope spilled_scope;
3186 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003187 JumpTarget leave, null, function, non_function_constructor;
3188
3189 // Load the object into r0.
3190 LoadAndSpill(args->at(0));
3191 frame_->EmitPop(r0);
3192
3193 // If the object is a smi, we return null.
3194 __ tst(r0, Operand(kSmiTagMask));
3195 null.Branch(eq);
3196
3197 // Check that the object is a JS object but take special care of JS
3198 // functions to make sure they have 'Function' as their class.
3199 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
3200 null.Branch(lt);
3201
3202 // As long as JS_FUNCTION_TYPE is the last instance type and it is
3203 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
3204 // LAST_JS_OBJECT_TYPE.
3205 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3206 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
3207 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3208 function.Branch(eq);
3209
3210 // Check if the constructor in the map is a function.
3211 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
3212 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
3213 non_function_constructor.Branch(ne);
3214
3215 // The r0 register now contains the constructor function. Grab the
3216 // instance class name from there.
3217 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
3218 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003219 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003220 leave.Jump();
3221
3222 // Functions have class 'Function'.
3223 function.Bind();
3224 __ mov(r0, Operand(Factory::function_class_symbol()));
3225 frame_->EmitPush(r0);
3226 leave.Jump();
3227
3228 // Objects with a non-function constructor have class 'Object'.
3229 non_function_constructor.Bind();
3230 __ mov(r0, Operand(Factory::Object_symbol()));
3231 frame_->EmitPush(r0);
3232 leave.Jump();
3233
3234 // Non-JS objects have class null.
3235 null.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003236 __ LoadRoot(r0, Heap::kNullValueRootIndex);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003237 frame_->EmitPush(r0);
3238
3239 // All done.
3240 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003241}
3242
3243
ager@chromium.org7c537e22008-10-16 08:43:32 +00003244void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003245 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003246 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003247 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003248 LoadAndSpill(args->at(0));
3249 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003250 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003251 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003252 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003253 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3254 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003255 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003256 // Load the value.
3257 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003258 leave.Bind();
3259 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003260}
3261
3262
ager@chromium.org7c537e22008-10-16 08:43:32 +00003263void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003264 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003265 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003266 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003267 LoadAndSpill(args->at(0)); // Load the object.
3268 LoadAndSpill(args->at(1)); // Load the value.
3269 frame_->EmitPop(r0); // r0 contains value
3270 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003271 // if (object->IsSmi()) return object.
3272 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003273 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003274 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3275 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003276 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003277 // Store the value.
3278 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3279 // Update the write barrier.
3280 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3281 __ RecordWrite(r1, r2, r3);
3282 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003283 leave.Bind();
3284 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003285}
3286
3287
ager@chromium.org7c537e22008-10-16 08:43:32 +00003288void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003289 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003290 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003291 LoadAndSpill(args->at(0));
3292 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003293 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003294 cc_reg_ = eq;
3295}
3296
3297
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003298void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003299 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003300 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3301 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003302#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003303 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003304 LoadAndSpill(args->at(1));
3305 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003306 __ CallRuntime(Runtime::kLog, 2);
3307 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003308#endif
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003309 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003310 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003311}
3312
3313
ager@chromium.org7c537e22008-10-16 08:43:32 +00003314void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003315 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003316 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003317 LoadAndSpill(args->at(0));
3318 frame_->EmitPop(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003319 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003320 cc_reg_ = eq;
3321}
3322
3323
kasper.lund7276f142008-07-30 08:49:36 +00003324// This should generate code that performs a charCodeAt() call or returns
3325// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3326// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003327void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003328 VirtualFrame::SpilledScope spilled_scope;
kasper.lund7276f142008-07-30 08:49:36 +00003329 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003330 Comment(masm_, "[ GenerateFastCharCodeAt");
3331
3332 LoadAndSpill(args->at(0));
3333 LoadAndSpill(args->at(1));
3334 frame_->EmitPop(r0); // Index.
3335 frame_->EmitPop(r1); // String.
3336
3337 Label slow, end, not_a_flat_string, ascii_string, try_again_with_new_string;
3338
3339 __ tst(r1, Operand(kSmiTagMask));
3340 __ b(eq, &slow); // The 'string' was a Smi.
3341
3342 ASSERT(kSmiTag == 0);
3343 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
3344 __ b(ne, &slow); // The index was negative or not a Smi.
3345
3346 __ bind(&try_again_with_new_string);
3347 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
3348 __ b(ge, &slow);
3349
3350 // Now r2 has the string type.
3351 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003352 // Now r3 has the length of the string. Compare with the index.
3353 __ cmp(r3, Operand(r0, LSR, kSmiTagSize));
3354 __ b(le, &slow);
3355
3356 // Here we know the index is in range. Check that string is sequential.
3357 ASSERT_EQ(0, kSeqStringTag);
3358 __ tst(r2, Operand(kStringRepresentationMask));
3359 __ b(ne, &not_a_flat_string);
3360
3361 // Check whether it is an ASCII string.
3362 ASSERT_EQ(0, kTwoByteStringTag);
3363 __ tst(r2, Operand(kStringEncodingMask));
3364 __ b(ne, &ascii_string);
3365
3366 // 2-byte string. We can add without shifting since the Smi tag size is the
3367 // log2 of the number of bytes in a two-byte character.
3368 ASSERT_EQ(1, kSmiTagSize);
3369 ASSERT_EQ(0, kSmiShiftSize);
3370 __ add(r1, r1, Operand(r0));
3371 __ ldrh(r0, FieldMemOperand(r1, SeqTwoByteString::kHeaderSize));
3372 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
3373 __ jmp(&end);
3374
3375 __ bind(&ascii_string);
3376 __ add(r1, r1, Operand(r0, LSR, kSmiTagSize));
3377 __ ldrb(r0, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
3378 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
3379 __ jmp(&end);
3380
3381 __ bind(&not_a_flat_string);
3382 __ and_(r2, r2, Operand(kStringRepresentationMask));
3383 __ cmp(r2, Operand(kConsStringTag));
3384 __ b(ne, &slow);
3385
3386 // ConsString.
3387 // Check that the right hand side is the empty string (ie if this is really a
3388 // flat string in a cons string). If that is not the case we would rather go
3389 // to the runtime system now, to flatten the string.
3390 __ ldr(r2, FieldMemOperand(r1, ConsString::kSecondOffset));
3391 __ LoadRoot(r3, Heap::kEmptyStringRootIndex);
3392 __ cmp(r2, Operand(r3));
3393 __ b(ne, &slow);
3394
3395 // Get the first of the two strings.
3396 __ ldr(r1, FieldMemOperand(r1, ConsString::kFirstOffset));
3397 __ jmp(&try_again_with_new_string);
3398
3399 __ bind(&slow);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003400 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003401
3402 __ bind(&end);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003403 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003404}
3405
3406
ager@chromium.org7c537e22008-10-16 08:43:32 +00003407void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003408 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003409 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003410 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003411 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003412 // We need the CC bits to come out as not_equal in the case where the
3413 // object is a smi. This can't be done with the usual test opcode so
3414 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003415 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003416 __ and_(r1, r0, Operand(kSmiTagMask));
3417 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003418 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003419 // It is a heap object - get the map. Check if the object is a JS array.
3420 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003421 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003422 cc_reg_ = eq;
3423}
3424
3425
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00003426void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
3427 VirtualFrame::SpilledScope spilled_scope;
3428 ASSERT(args->length() == 1);
3429 LoadAndSpill(args->at(0));
3430 JumpTarget answer;
3431 // We need the CC bits to come out as not_equal in the case where the
3432 // object is a smi. This can't be done with the usual test opcode so
3433 // we use XOR to get the right CC bits.
3434 frame_->EmitPop(r0);
3435 __ and_(r1, r0, Operand(kSmiTagMask));
3436 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
3437 answer.Branch(ne);
3438 // It is a heap object - get the map. Check if the object is a regexp.
3439 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
3440 answer.Bind();
3441 cc_reg_ = eq;
3442}
3443
3444
ager@chromium.org6141cbe2009-11-20 12:14:52 +00003445void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
3446 // This generates a fast version of:
3447 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
3448 VirtualFrame::SpilledScope spilled_scope;
3449 ASSERT(args->length() == 1);
3450 LoadAndSpill(args->at(0));
3451 frame_->EmitPop(r1);
3452 __ tst(r1, Operand(kSmiTagMask));
3453 false_target()->Branch(eq);
3454
3455 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3456 __ cmp(r1, ip);
3457 true_target()->Branch(eq);
3458
3459 Register map_reg = r2;
3460 __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset));
3461 // Undetectable objects behave like undefined when tested with typeof.
3462 __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset));
3463 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3464 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3465 false_target()->Branch(eq);
3466
3467 __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
3468 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
3469 false_target()->Branch(lt);
3470 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
3471 cc_reg_ = le;
3472}
3473
3474
3475void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
3476 // This generates a fast version of:
3477 // (%_ClassOf(arg) === 'Function')
3478 VirtualFrame::SpilledScope spilled_scope;
3479 ASSERT(args->length() == 1);
3480 LoadAndSpill(args->at(0));
3481 frame_->EmitPop(r0);
3482 __ tst(r0, Operand(kSmiTagMask));
3483 false_target()->Branch(eq);
3484 Register map_reg = r2;
3485 __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE);
3486 cc_reg_ = eq;
3487}
3488
3489
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003490void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
3491 VirtualFrame::SpilledScope spilled_scope;
3492 ASSERT(args->length() == 1);
3493 LoadAndSpill(args->at(0));
3494 frame_->EmitPop(r0);
3495 __ tst(r0, Operand(kSmiTagMask));
3496 false_target()->Branch(eq);
3497 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3498 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
3499 __ tst(r1, Operand(1 << Map::kIsUndetectable));
3500 cc_reg_ = ne;
3501}
3502
3503
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003504void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
3505 VirtualFrame::SpilledScope spilled_scope;
3506 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003507
3508 // Get the frame pointer for the calling frame.
3509 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3510
3511 // Skip the arguments adaptor frame if it exists.
3512 Label check_frame_marker;
3513 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003514 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003515 __ b(ne, &check_frame_marker);
3516 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
3517
3518 // Check the marker in the calling frame.
3519 __ bind(&check_frame_marker);
3520 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
3521 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
3522 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003523}
3524
3525
ager@chromium.org7c537e22008-10-16 08:43:32 +00003526void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003527 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003528 ASSERT(args->length() == 0);
3529
mads.s.ager31e71382008-08-13 09:32:07 +00003530 // Seed the result with the formal parameters count, which will be used
3531 // in case no arguments adaptor frame is found below the current frame.
ager@chromium.org5c838252010-02-19 08:53:10 +00003532 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003533
3534 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003535 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003536 frame_->CallStub(&stub, 0);
3537 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003538}
3539
3540
ager@chromium.org7c537e22008-10-16 08:43:32 +00003541void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003542 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003543 ASSERT(args->length() == 1);
3544
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003545 // Satisfy contract with ArgumentsAccessStub:
3546 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003547 LoadAndSpill(args->at(0));
3548 frame_->EmitPop(r1);
ager@chromium.org5c838252010-02-19 08:53:10 +00003549 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003550
3551 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003552 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003553 frame_->CallStub(&stub, 0);
3554 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003555}
3556
3557
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003558void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) {
3559 VirtualFrame::SpilledScope spilled_scope;
3560 ASSERT(args->length() == 0);
3561 __ Call(ExternalReference::random_positive_smi_function().address(),
3562 RelocInfo::RUNTIME_ENTRY);
3563 frame_->EmitPush(r0);
3564}
3565
3566
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003567void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
3568 ASSERT_EQ(2, args->length());
3569
3570 Load(args->at(0));
3571 Load(args->at(1));
3572
ager@chromium.org5c838252010-02-19 08:53:10 +00003573 StringAddStub stub(NO_STRING_ADD_FLAGS);
3574 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003575 frame_->EmitPush(r0);
3576}
3577
3578
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003579void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
3580 ASSERT_EQ(3, args->length());
3581
3582 Load(args->at(0));
3583 Load(args->at(1));
3584 Load(args->at(2));
3585
ager@chromium.org5c838252010-02-19 08:53:10 +00003586 SubStringStub stub;
3587 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003588 frame_->EmitPush(r0);
3589}
3590
3591
3592void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
3593 ASSERT_EQ(2, args->length());
3594
3595 Load(args->at(0));
3596 Load(args->at(1));
3597
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003598 StringCompareStub stub;
3599 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003600 frame_->EmitPush(r0);
3601}
3602
3603
3604void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
3605 ASSERT_EQ(4, args->length());
3606
3607 Load(args->at(0));
3608 Load(args->at(1));
3609 Load(args->at(2));
3610 Load(args->at(3));
3611
3612 frame_->CallRuntime(Runtime::kRegExpExec, 4);
3613 frame_->EmitPush(r0);
3614}
3615
3616
ager@chromium.org5c838252010-02-19 08:53:10 +00003617void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
3618 ASSERT_EQ(args->length(), 1);
3619
3620 // Load the argument on the stack and jump to the runtime.
3621 Load(args->at(0));
3622
3623 frame_->CallRuntime(Runtime::kNumberToString, 1);
3624 frame_->EmitPush(r0);
3625}
3626
3627
ager@chromium.org7c537e22008-10-16 08:43:32 +00003628void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003629 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003630 ASSERT(args->length() == 2);
3631
3632 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003633 LoadAndSpill(args->at(0));
3634 LoadAndSpill(args->at(1));
3635 frame_->EmitPop(r0);
3636 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003637 __ cmp(r0, Operand(r1));
3638 cc_reg_ = eq;
3639}
3640
3641
ager@chromium.org7c537e22008-10-16 08:43:32 +00003642void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003643#ifdef DEBUG
3644 int original_height = frame_->height();
3645#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003646 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003647 if (CheckForInlineRuntimeCall(node)) {
3648 ASSERT((has_cc() && frame_->height() == original_height) ||
3649 (!has_cc() && frame_->height() == original_height + 1));
3650 return;
3651 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003652
3653 ZoneList<Expression*>* args = node->arguments();
3654 Comment cmnt(masm_, "[ CallRuntime");
3655 Runtime::Function* function = node->function();
3656
ager@chromium.org41826e72009-03-30 13:30:57 +00003657 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003658 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00003659 // Push the builtins object found in the current global object.
3660 __ ldr(r1, GlobalObject());
3661 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003662 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003663 }
mads.s.ager31e71382008-08-13 09:32:07 +00003664
ager@chromium.org41826e72009-03-30 13:30:57 +00003665 // Push the arguments ("left-to-right").
3666 int arg_count = args->length();
3667 for (int i = 0; i < arg_count; i++) {
3668 LoadAndSpill(args->at(i));
3669 }
mads.s.ager31e71382008-08-13 09:32:07 +00003670
ager@chromium.org41826e72009-03-30 13:30:57 +00003671 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003672 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00003673 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003674 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3675 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003676 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003677 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003678 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003679 } else {
3680 // Call the C runtime function.
3681 frame_->CallRuntime(function, arg_count);
3682 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003683 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003684 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003685}
3686
3687
ager@chromium.org7c537e22008-10-16 08:43:32 +00003688void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003689#ifdef DEBUG
3690 int original_height = frame_->height();
3691#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003692 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003693 Comment cmnt(masm_, "[ UnaryOperation");
3694
3695 Token::Value op = node->op();
3696
3697 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003698 LoadConditionAndSpill(node->expression(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003699 false_target(),
3700 true_target(),
3701 true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003702 // LoadCondition may (and usually does) leave a test and branch to
3703 // be emitted by the caller. In that case, negate the condition.
3704 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003705
3706 } else if (op == Token::DELETE) {
3707 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003708 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003709 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003710 LoadAndSpill(property->obj());
3711 LoadAndSpill(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003712 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003713
mads.s.ager31e71382008-08-13 09:32:07 +00003714 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003715 Slot* slot = variable->slot();
3716 if (variable->is_global()) {
3717 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003718 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003719 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003720 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003721
3722 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3723 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003724 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003725 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003726 frame_->EmitPush(r0);
3727 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003728 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003729 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003730 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003731 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003732 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003733
mads.s.ager31e71382008-08-13 09:32:07 +00003734 } else {
3735 // Default: Result of deleting non-global, not dynamically
3736 // introduced variables is false.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003737 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00003738 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003739
3740 } else {
3741 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003742 LoadAndSpill(node->expression()); // may have side-effects
3743 frame_->Drop();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003744 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003745 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003746 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003747
3748 } else if (op == Token::TYPEOF) {
3749 // Special case for loading the typeof expression; see comment on
3750 // LoadTypeofExpression().
3751 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003752 frame_->CallRuntime(Runtime::kTypeof, 1);
3753 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003754
3755 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003756 bool overwrite =
3757 (node->expression()->AsBinaryOperation() != NULL &&
3758 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003759 LoadAndSpill(node->expression());
3760 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003761 switch (op) {
3762 case Token::NOT:
3763 case Token::DELETE:
3764 case Token::TYPEOF:
3765 UNREACHABLE(); // handled above
3766 break;
3767
3768 case Token::SUB: {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003769 GenericUnaryOpStub stub(Token::SUB, overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003770 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003771 break;
3772 }
3773
3774 case Token::BIT_NOT: {
3775 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003776 JumpTarget smi_label;
3777 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003778 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003779 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003780
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003781 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
3782 frame_->CallStub(&stub, 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003783 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003784
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003785 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003786 __ mvn(r0, Operand(r0));
3787 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003788 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003789 break;
3790 }
3791
3792 case Token::VOID:
3793 // since the stack top is cached in r0, popping and then
3794 // pushing a value can be done by just writing to r0.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003795 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003796 break;
3797
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003798 case Token::ADD: {
3799 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003800 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003801 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003802 continue_label.Branch(eq);
3803 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003804 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003805 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003806 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003807 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003808 default:
3809 UNREACHABLE();
3810 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003811 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003812 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003813 ASSERT(!has_valid_frame() ||
3814 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003815 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003816}
3817
3818
ager@chromium.org7c537e22008-10-16 08:43:32 +00003819void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003820#ifdef DEBUG
3821 int original_height = frame_->height();
3822#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003823 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003824 Comment cmnt(masm_, "[ CountOperation");
3825
3826 bool is_postfix = node->is_postfix();
3827 bool is_increment = node->op() == Token::INC;
3828
3829 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3830 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3831
3832 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003833 if (is_postfix) {
3834 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003835 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003836 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003837
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003838 // A constant reference is not saved to, so a constant reference is not a
3839 // compound assignment reference.
3840 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003841 if (target.is_illegal()) {
3842 // Spoof the virtual frame to have the expected height (one higher
3843 // than on entry).
3844 if (!is_postfix) {
3845 __ mov(r0, Operand(Smi::FromInt(0)));
3846 frame_->EmitPush(r0);
3847 }
3848 ASSERT(frame_->height() == original_height + 1);
3849 return;
3850 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003851 target.GetValueAndSpill();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003852 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003853
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003854 JumpTarget slow;
3855 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003856
3857 // Load the value (1) into register r1.
3858 __ mov(r1, Operand(Smi::FromInt(1)));
3859
3860 // Check for smi operand.
3861 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003862 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003863
3864 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003865 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003866 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003867 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003868
3869 // Perform optimistic increment/decrement.
3870 if (is_increment) {
3871 __ add(r0, r0, Operand(r1), SetCC);
3872 } else {
3873 __ sub(r0, r0, Operand(r1), SetCC);
3874 }
3875
3876 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003877 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003878
3879 // Revert optimistic increment/decrement.
3880 if (is_increment) {
3881 __ sub(r0, r0, Operand(r1));
3882 } else {
3883 __ add(r0, r0, Operand(r1));
3884 }
3885
3886 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003887 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003888 {
3889 // Convert the operand to a number.
3890 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003891 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003892 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003893 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003894 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003895 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003896 }
3897
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003898 // Compute the new value.
3899 __ mov(r1, Operand(Smi::FromInt(1)));
3900 frame_->EmitPush(r0);
3901 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003902 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003903 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003904 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003905 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003906 }
3907
3908 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003909 exit.Bind();
3910 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003911 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003912 }
3913
3914 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003915 if (is_postfix) frame_->EmitPop(r0);
3916 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003917}
3918
3919
ager@chromium.org7c537e22008-10-16 08:43:32 +00003920void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003921#ifdef DEBUG
3922 int original_height = frame_->height();
3923#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003924 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003925 Comment cmnt(masm_, "[ BinaryOperation");
3926 Token::Value op = node->op();
3927
3928 // According to ECMA-262 section 11.11, page 58, the binary logical
3929 // operators must yield the result of one of the two expressions
3930 // before any ToBoolean() conversions. This means that the value
3931 // produced by a && or || operator is not necessarily a boolean.
3932
3933 // NOTE: If the left hand side produces a materialized value (not in
3934 // the CC register), we force the right hand side to do the
3935 // same. This is necessary because we may have to branch to the exit
3936 // after evaluating the left hand side (due to the shortcut
3937 // semantics), but the compiler must (statically) know if the result
3938 // of compiling the binary operation is materialized or not.
3939
3940 if (op == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003941 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003942 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003943 &is_true,
3944 false_target(),
3945 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003946 if (has_valid_frame() && !has_cc()) {
3947 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003948 JumpTarget pop_and_continue;
3949 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003950
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003951 __ ldr(r0, frame_->Top()); // Duplicate the stack top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003952 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003953 // Avoid popping the result if it converts to 'false' using the
3954 // standard ToBoolean() conversion as described in ECMA-262,
3955 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003956 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003957 Branch(false, &exit);
3958
3959 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003960 pop_and_continue.Bind();
3961 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003962
3963 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003964 is_true.Bind();
3965 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003966
3967 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003968 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003969 } else if (has_cc() || is_true.is_linked()) {
3970 // The left-hand side is either (a) partially compiled to
3971 // control flow with a final branch left to emit or (b) fully
3972 // compiled to control flow and possibly true.
3973 if (has_cc()) {
3974 Branch(false, false_target());
3975 }
3976 is_true.Bind();
3977 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003978 true_target(),
3979 false_target(),
3980 false);
3981 } else {
3982 // Nothing to do.
3983 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003984 }
3985
3986 } else if (op == Token::OR) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003987 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003988 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003989 true_target(),
3990 &is_false,
3991 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003992 if (has_valid_frame() && !has_cc()) {
3993 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003994 JumpTarget pop_and_continue;
3995 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003996
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003997 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003998 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003999 // Avoid popping the result if it converts to 'true' using the
4000 // standard ToBoolean() conversion as described in ECMA-262,
4001 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00004002 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004003 Branch(true, &exit);
4004
4005 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004006 pop_and_continue.Bind();
4007 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004008
4009 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004010 is_false.Bind();
4011 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004012
4013 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004014 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004015 } else if (has_cc() || is_false.is_linked()) {
4016 // The left-hand side is either (a) partially compiled to
4017 // control flow with a final branch left to emit or (b) fully
4018 // compiled to control flow and possibly false.
4019 if (has_cc()) {
4020 Branch(true, true_target());
4021 }
4022 is_false.Bind();
4023 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004024 true_target(),
4025 false_target(),
4026 false);
4027 } else {
4028 // Nothing to do.
4029 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004030 }
4031
4032 } else {
4033 // Optimize for the case where (at least) one of the expressions
4034 // is a literal small integer.
4035 Literal* lliteral = node->left()->AsLiteral();
4036 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004037 // NOTE: The code below assumes that the slow cases (calls to runtime)
4038 // never return a constant/immutable object.
4039 bool overwrite_left =
4040 (node->left()->AsBinaryOperation() != NULL &&
4041 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
4042 bool overwrite_right =
4043 (node->right()->AsBinaryOperation() != NULL &&
4044 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004045
4046 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004047 LoadAndSpill(node->left());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004048 SmiOperation(node->op(),
4049 rliteral->handle(),
4050 false,
4051 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004052
4053 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004054 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004055 SmiOperation(node->op(),
4056 lliteral->handle(),
4057 true,
4058 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004059
4060 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004061 OverwriteMode overwrite_mode = NO_OVERWRITE;
4062 if (overwrite_left) {
4063 overwrite_mode = OVERWRITE_LEFT;
4064 } else if (overwrite_right) {
4065 overwrite_mode = OVERWRITE_RIGHT;
4066 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004067 LoadAndSpill(node->left());
4068 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004069 GenericBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004070 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004071 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004072 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004073 ASSERT(!has_valid_frame() ||
4074 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004075 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004076}
4077
4078
ager@chromium.org7c537e22008-10-16 08:43:32 +00004079void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004080#ifdef DEBUG
4081 int original_height = frame_->height();
4082#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004083 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004084 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004085 frame_->EmitPush(r0);
4086 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004087}
4088
4089
ager@chromium.org7c537e22008-10-16 08:43:32 +00004090void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004091#ifdef DEBUG
4092 int original_height = frame_->height();
4093#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004094 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004095 Comment cmnt(masm_, "[ CompareOperation");
4096
4097 // Get the expressions from the node.
4098 Expression* left = node->left();
4099 Expression* right = node->right();
4100 Token::Value op = node->op();
4101
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004102 // To make null checks efficient, we check if either left or right is the
4103 // literal 'null'. If so, we optimize the code by inlining a null check
4104 // instead of calling the (very) general runtime routine for checking
4105 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004106 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004107 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004108 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004109 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004110 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
4111 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004112 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004113 LoadAndSpill(left_is_null ? right : left);
4114 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004115 __ LoadRoot(ip, Heap::kNullValueRootIndex);
4116 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004117
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004118 // The 'null' value is only equal to 'undefined' if using non-strict
4119 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004120 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004121 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004122
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004123 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4124 __ cmp(r0, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004125 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004126
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004127 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004128 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004129
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004130 // It can be an undetectable object.
4131 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
4132 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
4133 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
4134 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004135 }
4136
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004137 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004138 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004139 return;
4140 }
4141 }
4142
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004143 // To make typeof testing for natives implemented in JavaScript really
4144 // efficient, we generate special code for expressions of the form:
4145 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004146 UnaryOperation* operation = left->AsUnaryOperation();
4147 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
4148 (operation != NULL && operation->op() == Token::TYPEOF) &&
4149 (right->AsLiteral() != NULL &&
4150 right->AsLiteral()->handle()->IsString())) {
4151 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
4152
mads.s.ager31e71382008-08-13 09:32:07 +00004153 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004154 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004155 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004156
4157 if (check->Equals(Heap::number_symbol())) {
4158 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004159 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004160 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004161 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
4162 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004163 cc_reg_ = eq;
4164
4165 } else if (check->Equals(Heap::string_symbol())) {
4166 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004167 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004168
4169 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4170
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004171 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004172 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4173 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4174 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004175 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004176
4177 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
4178 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
4179 cc_reg_ = lt;
4180
4181 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004182 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
4183 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004184 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004185 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
4186 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004187 cc_reg_ = eq;
4188
4189 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004190 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4191 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004192 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004193
4194 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004195 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004196
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004197 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004198 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4199 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4200 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4201 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4202
4203 cc_reg_ = eq;
4204
4205 } else if (check->Equals(Heap::function_symbol())) {
4206 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004207 false_target()->Branch(eq);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004208 Register map_reg = r2;
4209 __ CompareObjectType(r1, map_reg, r1, JS_FUNCTION_TYPE);
4210 true_target()->Branch(eq);
4211 // Regular expressions are callable so typeof == 'function'.
4212 __ CompareInstanceType(map_reg, r1, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004213 cc_reg_ = eq;
4214
4215 } else if (check->Equals(Heap::object_symbol())) {
4216 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004217 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004218
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004219 __ LoadRoot(ip, Heap::kNullValueRootIndex);
4220 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004221 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004222
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004223 Register map_reg = r2;
4224 __ CompareObjectType(r1, map_reg, r1, JS_REGEXP_TYPE);
4225 false_target()->Branch(eq);
4226
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004227 // It can be an undetectable object.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004228 __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004229 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4230 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004231 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004232
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004233 __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4234 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004235 false_target()->Branch(lt);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004236 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004237 cc_reg_ = le;
4238
4239 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004240 // Uncommon case: typeof testing against a string literal that is
4241 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004242 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004243 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004244 ASSERT(!has_valid_frame() ||
4245 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004246 return;
4247 }
4248
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004249 switch (op) {
4250 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004251 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004252 break;
4253
4254 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004255 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004256 break;
4257
4258 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004259 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004260 break;
4261
4262 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004263 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004264 break;
4265
4266 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004267 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004268 break;
4269
4270 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004271 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004272 break;
4273
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004274 case Token::IN: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004275 LoadAndSpill(left);
4276 LoadAndSpill(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004277 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004278 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004279 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004280 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004281
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004282 case Token::INSTANCEOF: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004283 LoadAndSpill(left);
4284 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004285 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004286 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004287 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004288 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004289 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004290 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004291 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004292
4293 default:
4294 UNREACHABLE();
4295 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004296 ASSERT((has_cc() && frame_->height() == original_height) ||
4297 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004298}
4299
4300
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004301void CodeGenerator::EmitKeyedLoad(bool is_global) {
4302 Comment cmnt(masm_, "[ Load from keyed Property");
4303 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
4304 RelocInfo::Mode rmode = is_global
4305 ? RelocInfo::CODE_TARGET_CONTEXT
4306 : RelocInfo::CODE_TARGET;
4307 frame_->CallCodeObject(ic, rmode, 0);
4308}
4309
4310
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004311#ifdef DEBUG
4312bool CodeGenerator::HasValidEntryRegisters() { return true; }
4313#endif
4314
4315
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004316#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004317#define __ ACCESS_MASM(masm)
4318
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004319
ager@chromium.org7c537e22008-10-16 08:43:32 +00004320Handle<String> Reference::GetName() {
4321 ASSERT(type_ == NAMED);
4322 Property* property = expression_->AsProperty();
4323 if (property == NULL) {
4324 // Global variable reference treated as a named property reference.
4325 VariableProxy* proxy = expression_->AsVariableProxy();
4326 ASSERT(proxy->AsVariable() != NULL);
4327 ASSERT(proxy->AsVariable()->is_global());
4328 return proxy->name();
4329 } else {
4330 Literal* raw_name = property->key()->AsLiteral();
4331 ASSERT(raw_name != NULL);
4332 return Handle<String>(String::cast(*raw_name->handle()));
4333 }
4334}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004335
ager@chromium.org7c537e22008-10-16 08:43:32 +00004336
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004337void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004338 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004339 ASSERT(!is_illegal());
4340 ASSERT(!cgen_->has_cc());
4341 MacroAssembler* masm = cgen_->masm();
4342 Property* property = expression_->AsProperty();
4343 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004344 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004345 }
4346
4347 switch (type_) {
4348 case SLOT: {
4349 Comment cmnt(masm, "[ Load from Slot");
4350 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4351 ASSERT(slot != NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004352 cgen_->LoadFromSlot(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004353 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004354 }
4355
ager@chromium.org7c537e22008-10-16 08:43:32 +00004356 case NAMED: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004357 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004358 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004359 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004360 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004361 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4362 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004363 __ mov(r2, Operand(name));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004364 ASSERT(var == NULL || var->is_global());
4365 RelocInfo::Mode rmode = (var == NULL)
4366 ? RelocInfo::CODE_TARGET
4367 : RelocInfo::CODE_TARGET_CONTEXT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004368 frame->CallCodeObject(ic, rmode, 0);
4369 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004370 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004371 }
4372
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004373 case KEYED: {
4374 // TODO(181): Implement inlined version of array indexing once
4375 // loop nesting is properly tracked on ARM.
4376 ASSERT(property != NULL);
4377 Variable* var = expression_->AsVariableProxy()->AsVariable();
4378 ASSERT(var == NULL || var->is_global());
4379 cgen_->EmitKeyedLoad(var != NULL);
4380 cgen_->frame()->EmitPush(r0);
4381 break;
4382 }
4383
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004384 default:
4385 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004386 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004387
4388 if (!persist_after_get_) {
4389 cgen_->UnloadReference(this);
4390 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004391}
4392
4393
ager@chromium.org7c537e22008-10-16 08:43:32 +00004394void Reference::SetValue(InitState init_state) {
4395 ASSERT(!is_illegal());
4396 ASSERT(!cgen_->has_cc());
4397 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004398 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004399 Property* property = expression_->AsProperty();
4400 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004401 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004402 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004403
ager@chromium.org7c537e22008-10-16 08:43:32 +00004404 switch (type_) {
4405 case SLOT: {
4406 Comment cmnt(masm, "[ Store to Slot");
4407 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004408 cgen_->StoreToSlot(slot, init_state);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004409 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004410 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004411 }
4412
ager@chromium.org7c537e22008-10-16 08:43:32 +00004413 case NAMED: {
4414 Comment cmnt(masm, "[ Store to named Property");
4415 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004416 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004417 Handle<String> name(GetName());
4418
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004419 frame->EmitPop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004420 frame->EmitPop(r1);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004421 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004422 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004423 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004424 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004425 break;
4426 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004427
ager@chromium.org7c537e22008-10-16 08:43:32 +00004428 case KEYED: {
4429 Comment cmnt(masm, "[ Store to keyed Property");
4430 Property* property = expression_->AsProperty();
4431 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004432 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004433
4434 // Call IC code.
4435 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004436 frame->EmitPop(r0); // value
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004437 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004438 frame->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004439 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004440 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004441 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004442
4443 default:
4444 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004445 }
4446}
4447
4448
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004449void FastNewClosureStub::Generate(MacroAssembler* masm) {
4450 // Clone the boilerplate in new space. Set the context to the
4451 // current context in cp.
4452 Label gc;
4453
4454 // Pop the boilerplate function from the stack.
4455 __ pop(r3);
4456
4457 // Attempt to allocate new JSFunction in new space.
4458 __ AllocateInNewSpace(JSFunction::kSize / kPointerSize,
4459 r0,
4460 r1,
4461 r2,
4462 &gc,
4463 TAG_OBJECT);
4464
4465 // Compute the function map in the current global context and set that
4466 // as the map of the allocated object.
4467 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
4468 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
4469 __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
4470 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4471
4472 // Clone the rest of the boilerplate fields. We don't have to update
4473 // the write barrier because the allocated object is in new space.
4474 for (int offset = kPointerSize;
4475 offset < JSFunction::kSize;
4476 offset += kPointerSize) {
4477 if (offset == JSFunction::kContextOffset) {
4478 __ str(cp, FieldMemOperand(r0, offset));
4479 } else {
4480 __ ldr(r1, FieldMemOperand(r3, offset));
4481 __ str(r1, FieldMemOperand(r0, offset));
4482 }
4483 }
4484
4485 // Return result. The argument boilerplate has been popped already.
4486 __ Ret();
4487
4488 // Create a new closure through the slower runtime call.
4489 __ bind(&gc);
4490 __ push(cp);
4491 __ push(r3);
4492 __ TailCallRuntime(ExternalReference(Runtime::kNewClosure), 2, 1);
4493}
4494
4495
4496void FastNewContextStub::Generate(MacroAssembler* masm) {
4497 // Try to allocate the context in new space.
4498 Label gc;
4499 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
4500
4501 // Attempt to allocate the context in new space.
4502 __ AllocateInNewSpace(length + (FixedArray::kHeaderSize / kPointerSize),
4503 r0,
4504 r1,
4505 r2,
4506 &gc,
4507 TAG_OBJECT);
4508
4509 // Load the function from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00004510 __ ldr(r3, MemOperand(sp, 0));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004511
4512 // Setup the object header.
4513 __ LoadRoot(r2, Heap::kContextMapRootIndex);
4514 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4515 __ mov(r2, Operand(length));
4516 __ str(r2, FieldMemOperand(r0, Array::kLengthOffset));
4517
4518 // Setup the fixed slots.
4519 __ mov(r1, Operand(Smi::FromInt(0)));
4520 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
4521 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
4522 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
4523 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
4524
4525 // Copy the global object from the surrounding context.
4526 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
4527 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
4528
4529 // Initialize the rest of the slots to undefined.
4530 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
4531 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
4532 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
4533 }
4534
4535 // Remove the on-stack argument and return.
4536 __ mov(cp, r0);
4537 __ pop();
4538 __ Ret();
4539
4540 // Need to collect. Call into runtime system.
4541 __ bind(&gc);
4542 __ TailCallRuntime(ExternalReference(Runtime::kNewContext), 1, 1);
4543}
4544
4545
ager@chromium.org5c838252010-02-19 08:53:10 +00004546void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
4547 // Stack layout on entry:
4548 //
4549 // [sp]: constant elements.
4550 // [sp + kPointerSize]: literal index.
4551 // [sp + (2 * kPointerSize)]: literals array.
4552
4553 // All sizes here are multiples of kPointerSize.
4554 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
4555 int size = JSArray::kSize + elements_size;
4556
4557 // Load boilerplate object into r3 and check if we need to create a
4558 // boilerplate.
4559 Label slow_case;
4560 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
4561 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
4562 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4563 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
4564 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4565 __ cmp(r3, ip);
4566 __ b(eq, &slow_case);
4567
4568 // Allocate both the JS array and the elements array in one big
4569 // allocation. This avoids multiple limit checks.
4570 __ AllocateInNewSpace(size / kPointerSize,
4571 r0,
4572 r1,
4573 r2,
4574 &slow_case,
4575 TAG_OBJECT);
4576
4577 // Copy the JS array part.
4578 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
4579 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
4580 __ ldr(r1, FieldMemOperand(r3, i));
4581 __ str(r1, FieldMemOperand(r0, i));
4582 }
4583 }
4584
4585 if (length_ > 0) {
4586 // Get hold of the elements array of the boilerplate and setup the
4587 // elements pointer in the resulting object.
4588 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
4589 __ add(r2, r0, Operand(JSArray::kSize));
4590 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
4591
4592 // Copy the elements array.
4593 for (int i = 0; i < elements_size; i += kPointerSize) {
4594 __ ldr(r1, FieldMemOperand(r3, i));
4595 __ str(r1, FieldMemOperand(r2, i));
4596 }
4597 }
4598
4599 // Return and remove the on-stack parameters.
4600 __ add(sp, sp, Operand(3 * kPointerSize));
4601 __ Ret();
4602
4603 __ bind(&slow_case);
4604 ExternalReference runtime(Runtime::kCreateArrayLiteralShallow);
4605 __ TailCallRuntime(runtime, 3, 1);
4606}
4607
4608
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004609// Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz
4610// instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0
4611// (31 instead of 32).
4612static void CountLeadingZeros(
4613 MacroAssembler* masm,
4614 Register source,
4615 Register scratch,
4616 Register zeros) {
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00004617#ifdef CAN_USE_ARMV5_INSTRUCTIONS
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004618 __ clz(zeros, source); // This instruction is only supported after ARM5.
4619#else
4620 __ mov(zeros, Operand(0));
4621 __ mov(scratch, source);
4622 // Top 16.
4623 __ tst(scratch, Operand(0xffff0000));
4624 __ add(zeros, zeros, Operand(16), LeaveCC, eq);
4625 __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
4626 // Top 8.
4627 __ tst(scratch, Operand(0xff000000));
4628 __ add(zeros, zeros, Operand(8), LeaveCC, eq);
4629 __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
4630 // Top 4.
4631 __ tst(scratch, Operand(0xf0000000));
4632 __ add(zeros, zeros, Operand(4), LeaveCC, eq);
4633 __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
4634 // Top 2.
4635 __ tst(scratch, Operand(0xc0000000));
4636 __ add(zeros, zeros, Operand(2), LeaveCC, eq);
4637 __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
4638 // Top bit.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004639 __ tst(scratch, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004640 __ add(zeros, zeros, Operand(1), LeaveCC, eq);
4641#endif
4642}
4643
4644
4645// Takes a Smi and converts to an IEEE 64 bit floating point value in two
4646// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
4647// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
4648// scratch register. Destroys the source register. No GC occurs during this
4649// stub so you don't have to set up the frame.
4650class ConvertToDoubleStub : public CodeStub {
4651 public:
4652 ConvertToDoubleStub(Register result_reg_1,
4653 Register result_reg_2,
4654 Register source_reg,
4655 Register scratch_reg)
4656 : result1_(result_reg_1),
4657 result2_(result_reg_2),
4658 source_(source_reg),
4659 zeros_(scratch_reg) { }
4660
4661 private:
4662 Register result1_;
4663 Register result2_;
4664 Register source_;
4665 Register zeros_;
4666
4667 // Minor key encoding in 16 bits.
4668 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4669 class OpBits: public BitField<Token::Value, 2, 14> {};
4670
4671 Major MajorKey() { return ConvertToDouble; }
4672 int MinorKey() {
4673 // Encode the parameters in a unique 16 bit value.
4674 return result1_.code() +
4675 (result2_.code() << 4) +
4676 (source_.code() << 8) +
4677 (zeros_.code() << 12);
4678 }
4679
4680 void Generate(MacroAssembler* masm);
4681
4682 const char* GetName() { return "ConvertToDoubleStub"; }
4683
4684#ifdef DEBUG
4685 void Print() { PrintF("ConvertToDoubleStub\n"); }
4686#endif
4687};
4688
4689
4690void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
4691#ifndef BIG_ENDIAN_FLOATING_POINT
4692 Register exponent = result1_;
4693 Register mantissa = result2_;
4694#else
4695 Register exponent = result2_;
4696 Register mantissa = result1_;
4697#endif
4698 Label not_special;
4699 // Convert from Smi to integer.
4700 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
4701 // Move sign bit from source to destination. This works because the sign bit
4702 // in the exponent word of the double has the same position and polarity as
4703 // the 2's complement sign bit in a Smi.
4704 ASSERT(HeapNumber::kSignMask == 0x80000000u);
4705 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
4706 // Subtract from 0 if source was negative.
4707 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
4708 __ cmp(source_, Operand(1));
4709 __ b(gt, &not_special);
4710
4711 // We have -1, 0 or 1, which we treat specially.
4712 __ cmp(source_, Operand(0));
4713 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
4714 static const uint32_t exponent_word_for_1 =
4715 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
4716 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne);
4717 // 1, 0 and -1 all have 0 for the second word.
4718 __ mov(mantissa, Operand(0));
4719 __ Ret();
4720
4721 __ bind(&not_special);
4722 // Count leading zeros. Uses result2 for a scratch register on pre-ARM5.
4723 // Gets the wrong answer for 0, but we already checked for that case above.
4724 CountLeadingZeros(masm, source_, mantissa, zeros_);
4725 // Compute exponent and or it into the exponent register.
4726 // We use result2 as a scratch register here.
4727 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
4728 __ orr(exponent,
4729 exponent,
4730 Operand(mantissa, LSL, HeapNumber::kExponentShift));
4731 // Shift up the source chopping the top bit off.
4732 __ add(zeros_, zeros_, Operand(1));
4733 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
4734 __ mov(source_, Operand(source_, LSL, zeros_));
4735 // Compute lower part of fraction (last 12 bits).
4736 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
4737 // And the top (top 20 bits).
4738 __ orr(exponent,
4739 exponent,
4740 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
4741 __ Ret();
4742}
4743
4744
4745// This stub can convert a signed int32 to a heap number (double). It does
4746// not work for int32s that are in Smi range! No GC occurs during this stub
4747// so you don't have to set up the frame.
4748class WriteInt32ToHeapNumberStub : public CodeStub {
4749 public:
4750 WriteInt32ToHeapNumberStub(Register the_int,
4751 Register the_heap_number,
4752 Register scratch)
4753 : the_int_(the_int),
4754 the_heap_number_(the_heap_number),
4755 scratch_(scratch) { }
4756
4757 private:
4758 Register the_int_;
4759 Register the_heap_number_;
4760 Register scratch_;
4761
4762 // Minor key encoding in 16 bits.
4763 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4764 class OpBits: public BitField<Token::Value, 2, 14> {};
4765
4766 Major MajorKey() { return WriteInt32ToHeapNumber; }
4767 int MinorKey() {
4768 // Encode the parameters in a unique 16 bit value.
4769 return the_int_.code() +
4770 (the_heap_number_.code() << 4) +
4771 (scratch_.code() << 8);
4772 }
4773
4774 void Generate(MacroAssembler* masm);
4775
4776 const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
4777
4778#ifdef DEBUG
4779 void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
4780#endif
4781};
4782
4783
4784// See comment for class.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004785void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004786 Label max_negative_int;
4787 // the_int_ has the answer which is a signed int32 but not a Smi.
4788 // We test for the special value that has a different exponent. This test
4789 // has the neat side effect of setting the flags according to the sign.
4790 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004791 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004792 __ b(eq, &max_negative_int);
4793 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
4794 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
4795 uint32_t non_smi_exponent =
4796 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
4797 __ mov(scratch_, Operand(non_smi_exponent));
4798 // Set the sign bit in scratch_ if the value was negative.
4799 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
4800 // Subtract from 0 if the value was negative.
4801 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
4802 // We should be masking the implict first digit of the mantissa away here,
4803 // but it just ends up combining harmlessly with the last digit of the
4804 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
4805 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
4806 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
4807 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
4808 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
4809 __ str(scratch_, FieldMemOperand(the_heap_number_,
4810 HeapNumber::kExponentOffset));
4811 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
4812 __ str(scratch_, FieldMemOperand(the_heap_number_,
4813 HeapNumber::kMantissaOffset));
4814 __ Ret();
4815
4816 __ bind(&max_negative_int);
4817 // The max negative int32 is stored as a positive number in the mantissa of
4818 // a double because it uses a sign bit instead of using two's complement.
4819 // The actual mantissa bits stored are all 0 because the implicit most
4820 // significant 1 bit is not stored.
4821 non_smi_exponent += 1 << HeapNumber::kExponentShift;
4822 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
4823 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
4824 __ mov(ip, Operand(0));
4825 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
4826 __ Ret();
4827}
4828
4829
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004830// Handle the case where the lhs and rhs are the same object.
4831// Equality is almost reflexive (everything but NaN), so this is a test
4832// for "identity and not NaN".
4833static void EmitIdenticalObjectComparison(MacroAssembler* masm,
4834 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004835 Condition cc,
4836 bool never_nan_nan) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004837 Label not_identical;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004838 Label heap_number, return_equal;
4839 Register exp_mask_reg = r5;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004840 __ cmp(r0, Operand(r1));
4841 __ b(ne, &not_identical);
4842
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004843 // The two objects are identical. If we know that one of them isn't NaN then
4844 // we now know they test equal.
4845 if (cc != eq || !never_nan_nan) {
4846 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004847
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004848 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
4849 // so we do the second best thing - test it ourselves.
4850 // They are both equal and they are not both Smis so both of them are not
4851 // Smis. If it's not a heap number, then return equal.
4852 if (cc == lt || cc == gt) {
4853 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004854 __ b(ge, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004855 } else {
4856 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4857 __ b(eq, &heap_number);
4858 // Comparing JS objects with <=, >= is complicated.
4859 if (cc != eq) {
4860 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
4861 __ b(ge, slow);
4862 // Normally here we fall through to return_equal, but undefined is
4863 // special: (undefined == undefined) == true, but
4864 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
4865 if (cc == le || cc == ge) {
4866 __ cmp(r4, Operand(ODDBALL_TYPE));
4867 __ b(ne, &return_equal);
4868 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
4869 __ cmp(r0, Operand(r2));
4870 __ b(ne, &return_equal);
4871 if (cc == le) {
4872 // undefined <= undefined should fail.
4873 __ mov(r0, Operand(GREATER));
4874 } else {
4875 // undefined >= undefined should fail.
4876 __ mov(r0, Operand(LESS));
4877 }
4878 __ mov(pc, Operand(lr)); // Return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004879 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004880 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004881 }
4882 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004883
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004884 __ bind(&return_equal);
4885 if (cc == lt) {
4886 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
4887 } else if (cc == gt) {
4888 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
4889 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004890 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004891 }
4892 __ mov(pc, Operand(lr)); // Return.
4893
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004894 if (cc != eq || !never_nan_nan) {
4895 // For less and greater we don't have to check for NaN since the result of
4896 // x < x is false regardless. For the others here is some code to check
4897 // for NaN.
4898 if (cc != lt && cc != gt) {
4899 __ bind(&heap_number);
4900 // It is a heap number, so return non-equal if it's NaN and equal if it's
4901 // not NaN.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004902
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004903 // The representation of NaN values has all exponent bits (52..62) set,
4904 // and not all mantissa bits (0..51) clear.
4905 // Read top bits of double representation (second word of value).
4906 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
4907 // Test that exponent bits are all set.
4908 __ and_(r3, r2, Operand(exp_mask_reg));
4909 __ cmp(r3, Operand(exp_mask_reg));
4910 __ b(ne, &return_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004911
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004912 // Shift out flag and all exponent bits, retaining only mantissa.
4913 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
4914 // Or with all low-bits of mantissa.
4915 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
4916 __ orr(r0, r3, Operand(r2), SetCC);
4917 // For equal we already have the right value in r0: Return zero (equal)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004918 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
4919 // not (it's a NaN). For <= and >= we need to load r0 with the failing
4920 // value if it's a NaN.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004921 if (cc != eq) {
4922 // All-zero means Infinity means equal.
4923 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
4924 if (cc == le) {
4925 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
4926 } else {
4927 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
4928 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004929 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004930 __ mov(pc, Operand(lr)); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004931 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004932 // No fall through here.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004933 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004934
4935 __ bind(&not_identical);
4936}
4937
4938
4939// See comment at call site.
4940static void EmitSmiNonsmiComparison(MacroAssembler* masm,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004941 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004942 Label* slow,
4943 bool strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004944 Label rhs_is_smi;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004945 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004946 __ b(eq, &rhs_is_smi);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004947
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004948 // Lhs is a Smi. Check whether the rhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004949 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4950 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004951 // If rhs is not a number and lhs is a Smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004952 // succeed. Return non-equal (r0 is already not zero)
4953 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4954 } else {
4955 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4956 // the runtime.
4957 __ b(ne, slow);
4958 }
4959
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004960 // Lhs (r1) is a smi, rhs (r0) is a number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004961 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004962 // Convert lhs to a double in d7 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004963 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004964 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
4965 __ vmov(s15, r7);
4966 __ vcvt(d7, s15);
4967 // Load the double from rhs, tagged HeapNumber r0, to d6.
4968 __ sub(r7, r0, Operand(kHeapObjectTag));
4969 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004970 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004971 __ push(lr);
4972 // Convert lhs to a double in r2, r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004973 __ mov(r7, Operand(r1));
4974 ConvertToDoubleStub stub1(r3, r2, r7, r6);
4975 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004976 // Load rhs to a double in r0, r1.
4977 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4978 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4979 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004980 }
4981
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004982 // We now have both loaded as doubles but we can skip the lhs nan check
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004983 // since it's a smi.
4984 __ jmp(lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004985
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004986 __ bind(&rhs_is_smi);
4987 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004988 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
4989 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004990 // If lhs is not a number and rhs is a smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004991 // succeed. Return non-equal.
4992 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
4993 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4994 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004995 // Smi compared non-strictly with a non-smi non-heap-number. Call
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004996 // the runtime.
4997 __ b(ne, slow);
4998 }
4999
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005000 // Rhs (r0) is a smi, lhs (r1) is a heap number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005001 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005002 // Convert rhs to a double in d6 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005003 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005004 // Load the double from lhs, tagged HeapNumber r1, to d7.
5005 __ sub(r7, r1, Operand(kHeapObjectTag));
5006 __ vldr(d7, r7, HeapNumber::kValueOffset);
5007 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
5008 __ vmov(s13, r7);
5009 __ vcvt(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005010 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005011 __ push(lr);
5012 // Load lhs to a double in r2, r3.
5013 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
5014 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
5015 // Convert rhs to a double in r0, r1.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005016 __ mov(r7, Operand(r0));
5017 ConvertToDoubleStub stub2(r1, r0, r7, r6);
5018 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005019 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005020 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005021 // Fall through to both_loaded_as_doubles.
5022}
5023
5024
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005025void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005026 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005027 Register rhs_exponent = exp_first ? r0 : r1;
5028 Register lhs_exponent = exp_first ? r2 : r3;
5029 Register rhs_mantissa = exp_first ? r1 : r0;
5030 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005031 Label one_is_nan, neither_is_nan;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005032 Label lhs_not_nan_exp_mask_is_loaded;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005033
5034 Register exp_mask_reg = r5;
5035
5036 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005037 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
5038 __ cmp(r4, Operand(exp_mask_reg));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005039 __ b(ne, &lhs_not_nan_exp_mask_is_loaded);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005040 __ mov(r4,
5041 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
5042 SetCC);
5043 __ b(ne, &one_is_nan);
5044 __ cmp(lhs_mantissa, Operand(0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005045 __ b(ne, &one_is_nan);
5046
5047 __ bind(lhs_not_nan);
5048 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
5049 __ bind(&lhs_not_nan_exp_mask_is_loaded);
5050 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
5051 __ cmp(r4, Operand(exp_mask_reg));
5052 __ b(ne, &neither_is_nan);
5053 __ mov(r4,
5054 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
5055 SetCC);
5056 __ b(ne, &one_is_nan);
5057 __ cmp(rhs_mantissa, Operand(0));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005058 __ b(eq, &neither_is_nan);
5059
5060 __ bind(&one_is_nan);
5061 // NaN comparisons always fail.
5062 // Load whatever we need in r0 to make the comparison fail.
5063 if (cc == lt || cc == le) {
5064 __ mov(r0, Operand(GREATER));
5065 } else {
5066 __ mov(r0, Operand(LESS));
5067 }
5068 __ mov(pc, Operand(lr)); // Return.
5069
5070 __ bind(&neither_is_nan);
5071}
5072
5073
5074// See comment at call site.
5075static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
5076 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005077 Register rhs_exponent = exp_first ? r0 : r1;
5078 Register lhs_exponent = exp_first ? r2 : r3;
5079 Register rhs_mantissa = exp_first ? r1 : r0;
5080 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005081
5082 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
5083 if (cc == eq) {
5084 // Doubles are not equal unless they have the same bit pattern.
5085 // Exception: 0 and -0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005086 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
5087 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005088 // Return non-zero if the numbers are unequal.
5089 __ mov(pc, Operand(lr), LeaveCC, ne);
5090
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005091 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005092 // If exponents are equal then return 0.
5093 __ mov(pc, Operand(lr), LeaveCC, eq);
5094
5095 // Exponents are unequal. The only way we can return that the numbers
5096 // are equal is if one is -0 and the other is 0. We already dealt
5097 // with the case where both are -0 or both are 0.
5098 // We start by seeing if the mantissas (that are equal) or the bottom
5099 // 31 bits of the rhs exponent are non-zero. If so we return not
5100 // equal.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005101 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005102 __ mov(r0, Operand(r4), LeaveCC, ne);
5103 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
5104 // Now they are equal if and only if the lhs exponent is zero in its
5105 // low 31 bits.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005106 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005107 __ mov(pc, Operand(lr));
5108 } else {
5109 // Call a native function to do a comparison between two non-NaNs.
5110 // Call C routine that may not cause GC or other trouble.
5111 __ mov(r5, Operand(ExternalReference::compare_doubles()));
5112 __ Jump(r5); // Tail call.
5113 }
5114}
5115
5116
5117// See comment at call site.
5118static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
5119 // If either operand is a JSObject or an oddball value, then they are
5120 // not equal since their pointers are different.
5121 // There is no test for undetectability in strict equality.
5122 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
5123 Label first_non_object;
5124 // Get the type of the first operand into r2 and compare it with
5125 // FIRST_JS_OBJECT_TYPE.
5126 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
5127 __ b(lt, &first_non_object);
5128
5129 // Return non-zero (r0 is not zero)
5130 Label return_not_equal;
5131 __ bind(&return_not_equal);
5132 __ mov(pc, Operand(lr)); // Return.
5133
5134 __ bind(&first_non_object);
5135 // Check for oddballs: true, false, null, undefined.
5136 __ cmp(r2, Operand(ODDBALL_TYPE));
5137 __ b(eq, &return_not_equal);
5138
5139 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
5140 __ b(ge, &return_not_equal);
5141
5142 // Check for oddballs: true, false, null, undefined.
5143 __ cmp(r3, Operand(ODDBALL_TYPE));
5144 __ b(eq, &return_not_equal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005145
5146 // Now that we have the types we might as well check for symbol-symbol.
5147 // Ensure that no non-strings have the symbol bit set.
5148 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
5149 ASSERT(kSymbolTag != 0);
5150 __ and_(r2, r2, Operand(r3));
5151 __ tst(r2, Operand(kIsSymbolMask));
5152 __ b(ne, &return_not_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005153}
5154
5155
5156// See comment at call site.
5157static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
5158 Label* both_loaded_as_doubles,
5159 Label* not_heap_numbers,
5160 Label* slow) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005161 __ CompareObjectType(r0, r3, r2, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005162 __ b(ne, not_heap_numbers);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005163 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
5164 __ cmp(r2, r3);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005165 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
5166
5167 // Both are heap numbers. Load them up then jump to the code we have
5168 // for that.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005169 if (CpuFeatures::IsSupported(VFP3)) {
5170 CpuFeatures::Scope scope(VFP3);
5171 __ sub(r7, r0, Operand(kHeapObjectTag));
5172 __ vldr(d6, r7, HeapNumber::kValueOffset);
5173 __ sub(r7, r1, Operand(kHeapObjectTag));
5174 __ vldr(d7, r7, HeapNumber::kValueOffset);
5175 } else {
5176 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
5177 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
5178 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
5179 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
5180 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005181 __ jmp(both_loaded_as_doubles);
5182}
5183
5184
5185// Fast negative check for symbol-to-symbol equality.
5186static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
5187 // r2 is object type of r0.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005188 // Ensure that no non-strings have the symbol bit set.
5189 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
5190 ASSERT(kSymbolTag != 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005191 __ tst(r2, Operand(kIsSymbolMask));
5192 __ b(eq, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005193 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
5194 __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005195 __ tst(r3, Operand(kIsSymbolMask));
5196 __ b(eq, slow);
5197
5198 // Both are symbols. We already checked they weren't the same pointer
5199 // so they are not equal.
5200 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
5201 __ mov(pc, Operand(lr)); // Return.
5202}
5203
5204
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005205// On entry r0 (rhs) and r1 (lhs) are the values to be compared.
5206// On exit r0 is 0, positive or negative to indicate the result of
5207// the comparison.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005208void CompareStub::Generate(MacroAssembler* masm) {
5209 Label slow; // Call builtin.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005210 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005211
5212 // NOTICE! This code is only reached after a smi-fast-case check, so
5213 // it is certain that at least one operand isn't a smi.
5214
5215 // Handle the case where the objects are identical. Either returns the answer
5216 // or goes to slow. Only falls through if the objects were not identical.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005217 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005218
5219 // If either is a Smi (we know that not both are), then they can only
5220 // be strictly equal if the other is a HeapNumber.
5221 ASSERT_EQ(0, kSmiTag);
5222 ASSERT_EQ(0, Smi::FromInt(0));
5223 __ and_(r2, r0, Operand(r1));
5224 __ tst(r2, Operand(kSmiTagMask));
5225 __ b(ne, &not_smis);
5226 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
5227 // 1) Return the answer.
5228 // 2) Go to slow.
5229 // 3) Fall through to both_loaded_as_doubles.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005230 // 4) Jump to lhs_not_nan.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005231 // In cases 3 and 4 we have found out we were dealing with a number-number
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005232 // comparison. If VFP3 is supported the double values of the numbers have
5233 // been loaded into d7 and d6. Otherwise, the double values have been loaded
5234 // into r0, r1, r2, and r3.
5235 EmitSmiNonsmiComparison(masm, &lhs_not_nan, &slow, strict_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005236
5237 __ bind(&both_loaded_as_doubles);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005238 // The arguments have been converted to doubles and stored in d6 and d7, if
5239 // VFP3 is supported, or in r0, r1, r2, and r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005240 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005241 __ bind(&lhs_not_nan);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005242 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005243 Label no_nan;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005244 // ARMv7 VFP3 instructions to implement double precision comparison.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005245 __ vcmp(d7, d6);
5246 __ vmrs(pc); // Move vector status bits to normal status bits.
5247 Label nan;
5248 __ b(vs, &nan);
5249 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
5250 __ mov(r0, Operand(LESS), LeaveCC, lt);
5251 __ mov(r0, Operand(GREATER), LeaveCC, gt);
5252 __ mov(pc, Operand(lr));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005253
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005254 __ bind(&nan);
5255 // If one of the sides was a NaN then the v flag is set. Load r0 with
5256 // whatever it takes to make the comparison fail, since comparisons with NaN
5257 // always fail.
5258 if (cc_ == lt || cc_ == le) {
5259 __ mov(r0, Operand(GREATER));
5260 } else {
5261 __ mov(r0, Operand(LESS));
5262 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005263 __ mov(pc, Operand(lr));
5264 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005265 // Checks for NaN in the doubles we have loaded. Can return the answer or
5266 // fall through if neither is a NaN. Also binds lhs_not_nan.
5267 EmitNanCheck(masm, &lhs_not_nan, cc_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005268 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
5269 // answer. Never falls through.
5270 EmitTwoNonNanDoubleComparison(masm, cc_);
5271 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005272
5273 __ bind(&not_smis);
5274 // At this point we know we are dealing with two different objects,
5275 // and neither of them is a Smi. The objects are in r0 and r1.
5276 if (strict_) {
5277 // This returns non-equal for some object types, or falls through if it
5278 // was not lucky.
5279 EmitStrictTwoHeapObjectCompare(masm);
5280 }
5281
5282 Label check_for_symbols;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005283 Label flat_string_check;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005284 // Check for heap-number-heap-number comparison. Can jump to slow case,
5285 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
5286 // that case. If the inputs are not doubles then jumps to check_for_symbols.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005287 // In this case r2 will contain the type of r0. Never falls through.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005288 EmitCheckForTwoHeapNumbers(masm,
5289 &both_loaded_as_doubles,
5290 &check_for_symbols,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005291 &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005292
5293 __ bind(&check_for_symbols);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005294 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
5295 // symbols.
5296 if (cc_ == eq && !strict_) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005297 // Either jumps to slow or returns the answer. Assumes that r2 is the type
5298 // of r0 on entry.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005299 EmitCheckForSymbols(masm, &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005300 }
5301
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005302 // Check for both being sequential ASCII strings, and inline if that is the
5303 // case.
5304 __ bind(&flat_string_check);
5305
5306 __ JumpIfNonSmisNotBothSequentialAsciiStrings(r0, r1, r2, r3, &slow);
5307
5308 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
5309 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
5310 r1,
5311 r0,
5312 r2,
5313 r3,
5314 r4,
5315 r5);
5316 // Never falls through to here.
5317
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005318 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005319
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005320 __ push(r1);
5321 __ push(r0);
5322 // Figure out which native to call and setup the arguments.
5323 Builtins::JavaScript native;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005324 if (cc_ == eq) {
5325 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
5326 } else {
5327 native = Builtins::COMPARE;
5328 int ncr; // NaN compare result
5329 if (cc_ == lt || cc_ == le) {
5330 ncr = GREATER;
5331 } else {
5332 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
5333 ncr = LESS;
5334 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005335 __ mov(r0, Operand(Smi::FromInt(ncr)));
5336 __ push(r0);
5337 }
5338
5339 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
5340 // tagged as a small integer.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005341 __ InvokeBuiltin(native, JUMP_JS);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005342}
5343
5344
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005345// Allocates a heap number or jumps to the label if the young space is full and
5346// a scavenge is needed.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005347static void AllocateHeapNumber(
5348 MacroAssembler* masm,
5349 Label* need_gc, // Jump here if young space is full.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005350 Register result, // The tagged address of the new heap number.
5351 Register scratch1, // A scratch register.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005352 Register scratch2) { // Another scratch register.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005353 // Allocate an object in the heap for the heap number and tag it as a heap
5354 // object.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005355 __ AllocateInNewSpace(HeapNumber::kSize / kPointerSize,
5356 result,
5357 scratch1,
5358 scratch2,
5359 need_gc,
5360 TAG_OBJECT);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005361
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005362 // Get heap number map and store it in the allocated object.
5363 __ LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex);
5364 __ str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005365}
5366
5367
5368// We fall into this code if the operands were Smis, but the result was
5369// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005370// the operands were not both Smi. The operands are in r0 and r1. In order
5371// to call the C-implemented binary fp operation routines we need to end up
5372// with the double precision floating point operands in r0 and r1 (for the
5373// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005374static void HandleBinaryOpSlowCases(MacroAssembler* masm,
5375 Label* not_smi,
5376 const Builtins::JavaScript& builtin,
5377 Token::Value operation,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005378 OverwriteMode mode) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005379 Label slow, slow_pop_2_first, do_the_call;
5380 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
5381 // Smi-smi case (overflow).
5382 // Since both are Smis there is no heap number to overwrite, so allocate.
5383 // The new heap number is in r5. r6 and r7 are scratch.
5384 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005385
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005386 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
5387 // using registers d7 and d6 for the double values.
5388 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) &&
5389 Token::MOD != operation;
5390 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005391 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005392 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
5393 __ vmov(s15, r7);
5394 __ vcvt(d7, s15);
5395 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
5396 __ vmov(s13, r7);
5397 __ vcvt(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005398 } else {
5399 // Write Smi from r0 to r3 and r2 in double format. r6 is scratch.
5400 __ mov(r7, Operand(r0));
5401 ConvertToDoubleStub stub1(r3, r2, r7, r6);
5402 __ push(lr);
5403 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
5404 // Write Smi from r1 to r1 and r0 in double format. r6 is scratch.
5405 __ mov(r7, Operand(r1));
5406 ConvertToDoubleStub stub2(r1, r0, r7, r6);
5407 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
5408 __ pop(lr);
5409 }
5410
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005411 __ jmp(&do_the_call); // Tail call. No return.
5412
5413 // We jump to here if something goes wrong (one param is not a number of any
5414 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005415 __ bind(&slow);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005416
5417 // Push arguments to the stack
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005418 __ push(r1);
5419 __ push(r0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005420
5421 if (Token::ADD == operation) {
5422 // Test for string arguments before calling runtime.
5423 // r1 : first argument
5424 // r0 : second argument
5425 // sp[0] : second argument
ager@chromium.org5c838252010-02-19 08:53:10 +00005426 // sp[4] : first argument
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005427
5428 Label not_strings, not_string1, string1;
5429 __ tst(r1, Operand(kSmiTagMask));
5430 __ b(eq, &not_string1);
5431 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
5432 __ b(ge, &not_string1);
5433
5434 // First argument is a a string, test second.
5435 __ tst(r0, Operand(kSmiTagMask));
5436 __ b(eq, &string1);
5437 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
5438 __ b(ge, &string1);
5439
5440 // First and second argument are strings.
ager@chromium.org5c838252010-02-19 08:53:10 +00005441 StringAddStub stub(NO_STRING_CHECK_IN_STUB);
5442 __ TailCallStub(&stub);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005443
5444 // Only first argument is a string.
5445 __ bind(&string1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005446 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
5447
5448 // First argument was not a string, test second.
5449 __ bind(&not_string1);
5450 __ tst(r0, Operand(kSmiTagMask));
5451 __ b(eq, &not_strings);
5452 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
5453 __ b(ge, &not_strings);
5454
5455 // Only second argument is a string.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005456 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
5457
5458 __ bind(&not_strings);
5459 }
5460
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005461 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005462
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005463 // We branch here if at least one of r0 and r1 is not a Smi.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005464 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005465 if (mode == NO_OVERWRITE) {
5466 // In the case where there is no chance of an overwritable float we may as
5467 // well do the allocation immediately while r0 and r1 are untouched.
5468 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5469 }
5470
5471 // Move r0 to a double in r2-r3.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005472 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005473 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5474 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005475 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005476 if (mode == OVERWRITE_RIGHT) {
5477 __ mov(r5, Operand(r0)); // Overwrite this heap number.
5478 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005479 if (use_fp_registers) {
5480 CpuFeatures::Scope scope(VFP3);
5481 // Load the double from tagged HeapNumber r0 to d7.
5482 __ sub(r7, r0, Operand(kHeapObjectTag));
5483 __ vldr(d7, r7, HeapNumber::kValueOffset);
5484 } else {
5485 // Calling convention says that second double is in r2 and r3.
5486 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
5487 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
5488 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005489 __ jmp(&finished_loading_r0);
5490 __ bind(&r0_is_smi);
5491 if (mode == OVERWRITE_RIGHT) {
5492 // We can't overwrite a Smi so get address of new heap number into r5.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005493 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005494 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005495
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005496 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005497 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005498 // Convert smi in r0 to double in d7.
5499 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
5500 __ vmov(s15, r7);
5501 __ vcvt(d7, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005502 } else {
5503 // Write Smi from r0 to r3 and r2 in double format.
5504 __ mov(r7, Operand(r0));
5505 ConvertToDoubleStub stub3(r3, r2, r7, r6);
5506 __ push(lr);
5507 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
5508 __ pop(lr);
5509 }
5510
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005511 __ bind(&finished_loading_r0);
5512
5513 // Move r1 to a double in r0-r1.
5514 __ tst(r1, Operand(kSmiTagMask));
5515 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5516 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5517 __ b(ne, &slow);
5518 if (mode == OVERWRITE_LEFT) {
5519 __ mov(r5, Operand(r1)); // Overwrite this heap number.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005520 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005521 if (use_fp_registers) {
5522 CpuFeatures::Scope scope(VFP3);
5523 // Load the double from tagged HeapNumber r1 to d6.
5524 __ sub(r7, r1, Operand(kHeapObjectTag));
5525 __ vldr(d6, r7, HeapNumber::kValueOffset);
5526 } else {
5527 // Calling convention says that first double is in r0 and r1.
5528 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
5529 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
5530 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005531 __ jmp(&finished_loading_r1);
5532 __ bind(&r1_is_smi);
5533 if (mode == OVERWRITE_LEFT) {
5534 // We can't overwrite a Smi so get address of new heap number into r5.
5535 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5536 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005537
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005538 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005539 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005540 // Convert smi in r1 to double in d6.
5541 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
5542 __ vmov(s13, r7);
5543 __ vcvt(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005544 } else {
5545 // Write Smi from r1 to r1 and r0 in double format.
5546 __ mov(r7, Operand(r1));
5547 ConvertToDoubleStub stub4(r1, r0, r7, r6);
5548 __ push(lr);
5549 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
5550 __ pop(lr);
5551 }
5552
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005553 __ bind(&finished_loading_r1);
5554
5555 __ bind(&do_the_call);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005556 // If we are inlining the operation using VFP3 instructions for
5557 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
5558 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005559 CpuFeatures::Scope scope(VFP3);
5560 // ARMv7 VFP3 instructions to implement
5561 // double precision, add, subtract, multiply, divide.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005562
5563 if (Token::MUL == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005564 __ vmul(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005565 } else if (Token::DIV == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005566 __ vdiv(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005567 } else if (Token::ADD == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005568 __ vadd(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005569 } else if (Token::SUB == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005570 __ vsub(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005571 } else {
5572 UNREACHABLE();
5573 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005574 __ sub(r0, r5, Operand(kHeapObjectTag));
5575 __ vstr(d5, r0, HeapNumber::kValueOffset);
5576 __ add(r0, r0, Operand(kHeapObjectTag));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005577 __ mov(pc, lr);
5578 return;
5579 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005580
5581 // If we did not inline the operation, then the arguments are in:
5582 // r0: Left value (least significant part of mantissa).
5583 // r1: Left value (sign, exponent, top of mantissa).
5584 // r2: Right value (least significant part of mantissa).
5585 // r3: Right value (sign, exponent, top of mantissa).
5586 // r5: Address of heap number for result.
5587
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005588 __ push(lr); // For later.
5589 __ push(r5); // Address of heap number that is answer.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005590 __ AlignStack(0);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005591 // Call C routine that may not cause GC or other trouble.
5592 __ mov(r5, Operand(ExternalReference::double_fp_operation(operation)));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005593 __ Call(r5);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005594 __ pop(r4); // Address of heap number.
5595 __ cmp(r4, Operand(Smi::FromInt(0)));
5596 __ pop(r4, eq); // Conditional pop instruction to get rid of alignment push.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005597 // Store answer in the overwritable heap number.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005598#if !defined(USE_ARM_EABI)
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005599 // Double returned in fp coprocessor register 0 and 1, encoded as register
5600 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
5601 // substract the tag from r4.
5602 __ sub(r5, r4, Operand(kHeapObjectTag));
5603 __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset));
5604#else
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005605 // Double returned in registers 0 and 1.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005606 __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005607 __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005608#endif
5609 __ mov(r0, Operand(r4));
5610 // And we are done.
5611 __ pop(pc);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005612}
5613
5614
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005615// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005616// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005617// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
5618// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005619// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
5620// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005621static void GetInt32(MacroAssembler* masm,
5622 Register source,
5623 Register dest,
5624 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005625 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005626 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005627 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005628 // Get exponent word.
5629 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
5630 // Get exponent alone in scratch2.
5631 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005632 // Load dest with zero. We use this either for the final shift or
5633 // for the answer.
5634 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005635 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005636 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
5637 // the exponent that we are fastest at and also the highest exponent we can
5638 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005639 const uint32_t non_smi_exponent =
5640 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
5641 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005642 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
5643 __ b(eq, &right_exponent);
5644 // If the exponent is higher than that then go to slow case. This catches
5645 // numbers that don't fit in a signed int32, infinities and NaNs.
5646 __ b(gt, slow);
5647
5648 // We know the exponent is smaller than 30 (biased). If it is less than
5649 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
5650 // it rounds to zero.
5651 const uint32_t zero_exponent =
5652 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
5653 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
5654 // Dest already has a Smi zero.
5655 __ b(lt, &done);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005656 if (!CpuFeatures::IsSupported(VFP3)) {
5657 // We have a shifted exponent between 0 and 30 in scratch2.
5658 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
5659 // We now have the exponent in dest. Subtract from 30 to get
5660 // how much to shift down.
5661 __ rsb(dest, dest, Operand(30));
5662 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005663 __ bind(&right_exponent);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005664 if (CpuFeatures::IsSupported(VFP3)) {
5665 CpuFeatures::Scope scope(VFP3);
5666 // ARMv7 VFP3 instructions implementing double precision to integer
5667 // conversion using round to zero.
5668 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005669 __ vmov(d7, scratch2, scratch);
5670 __ vcvt(s15, d7);
5671 __ vmov(dest, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005672 } else {
5673 // Get the top bits of the mantissa.
5674 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
5675 // Put back the implicit 1.
5676 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
5677 // Shift up the mantissa bits to take up the space the exponent used to
5678 // take. We just orred in the implicit bit so that took care of one and
5679 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
5680 // distance.
5681 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
5682 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
5683 // Put sign in zero flag.
5684 __ tst(scratch, Operand(HeapNumber::kSignMask));
5685 // Get the second half of the double. For some exponents we don't
5686 // actually need this because the bits get shifted out again, but
5687 // it's probably slower to test than just to do it.
5688 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
5689 // Shift down 22 bits to get the last 10 bits.
5690 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
5691 // Move down according to the exponent.
5692 __ mov(dest, Operand(scratch, LSR, dest));
5693 // Fix sign if sign bit was set.
5694 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
5695 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005696 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005697}
5698
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005699// For bitwise ops where the inputs are not both Smis we here try to determine
5700// whether both inputs are either Smis or at least heap numbers that can be
5701// represented by a 32 bit signed value. We truncate towards zero as required
5702// by the ES spec. If this is the case we do the bitwise op and see if the
5703// result is a Smi. If so, great, otherwise we try to find a heap number to
5704// write the answer into (either by allocating or by overwriting).
5705// On entry the operands are in r0 and r1. On exit the answer is in r0.
5706void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) {
5707 Label slow, result_not_a_smi;
5708 Label r0_is_smi, r1_is_smi;
5709 Label done_checking_r0, done_checking_r1;
5710
5711 __ tst(r1, Operand(kSmiTagMask));
5712 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5713 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5714 __ b(ne, &slow);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005715 GetInt32(masm, r1, r3, r5, r4, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005716 __ jmp(&done_checking_r1);
5717 __ bind(&r1_is_smi);
5718 __ mov(r3, Operand(r1, ASR, 1));
5719 __ bind(&done_checking_r1);
5720
5721 __ tst(r0, Operand(kSmiTagMask));
5722 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5723 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5724 __ b(ne, &slow);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005725 GetInt32(masm, r0, r2, r5, r4, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005726 __ jmp(&done_checking_r0);
5727 __ bind(&r0_is_smi);
5728 __ mov(r2, Operand(r0, ASR, 1));
5729 __ bind(&done_checking_r0);
5730
5731 // r0 and r1: Original operands (Smi or heap numbers).
5732 // r2 and r3: Signed int32 operands.
5733 switch (op_) {
5734 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
5735 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
5736 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
5737 case Token::SAR:
5738 // Use only the 5 least significant bits of the shift count.
5739 __ and_(r2, r2, Operand(0x1f));
5740 __ mov(r2, Operand(r3, ASR, r2));
5741 break;
5742 case Token::SHR:
5743 // Use only the 5 least significant bits of the shift count.
5744 __ and_(r2, r2, Operand(0x1f));
5745 __ mov(r2, Operand(r3, LSR, r2), SetCC);
5746 // SHR is special because it is required to produce a positive answer.
5747 // The code below for writing into heap numbers isn't capable of writing
5748 // the register as an unsigned int so we go to slow case if we hit this
5749 // case.
5750 __ b(mi, &slow);
5751 break;
5752 case Token::SHL:
5753 // Use only the 5 least significant bits of the shift count.
5754 __ and_(r2, r2, Operand(0x1f));
5755 __ mov(r2, Operand(r3, LSL, r2));
5756 break;
5757 default: UNREACHABLE();
5758 }
5759 // check that the *signed* result fits in a smi
5760 __ add(r3, r2, Operand(0x40000000), SetCC);
5761 __ b(mi, &result_not_a_smi);
5762 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5763 __ Ret();
5764
5765 Label have_to_allocate, got_a_heap_number;
5766 __ bind(&result_not_a_smi);
5767 switch (mode_) {
5768 case OVERWRITE_RIGHT: {
5769 __ tst(r0, Operand(kSmiTagMask));
5770 __ b(eq, &have_to_allocate);
5771 __ mov(r5, Operand(r0));
5772 break;
5773 }
5774 case OVERWRITE_LEFT: {
5775 __ tst(r1, Operand(kSmiTagMask));
5776 __ b(eq, &have_to_allocate);
5777 __ mov(r5, Operand(r1));
5778 break;
5779 }
5780 case NO_OVERWRITE: {
5781 // Get a new heap number in r5. r6 and r7 are scratch.
5782 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5783 }
5784 default: break;
5785 }
5786 __ bind(&got_a_heap_number);
5787 // r2: Answer as signed int32.
5788 // r5: Heap number to write answer into.
5789
5790 // Nothing can go wrong now, so move the heap number to r0, which is the
5791 // result.
5792 __ mov(r0, Operand(r5));
5793
5794 // Tail call that writes the int32 in r2 to the heap number in r0, using
5795 // r3 as scratch. r0 is preserved and returned.
5796 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
5797 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
5798
5799 if (mode_ != NO_OVERWRITE) {
5800 __ bind(&have_to_allocate);
5801 // Get a new heap number in r5. r6 and r7 are scratch.
5802 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5803 __ jmp(&got_a_heap_number);
5804 }
5805
5806 // If all else failed then we go to the runtime system.
5807 __ bind(&slow);
5808 __ push(r1); // restore stack
5809 __ push(r0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005810 switch (op_) {
5811 case Token::BIT_OR:
5812 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
5813 break;
5814 case Token::BIT_AND:
5815 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
5816 break;
5817 case Token::BIT_XOR:
5818 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
5819 break;
5820 case Token::SAR:
5821 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
5822 break;
5823 case Token::SHR:
5824 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
5825 break;
5826 case Token::SHL:
5827 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
5828 break;
5829 default:
5830 UNREACHABLE();
5831 }
5832}
5833
5834
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005835// Can we multiply by x with max two shifts and an add.
5836// This answers yes to all integers from 2 to 10.
5837static bool IsEasyToMultiplyBy(int x) {
5838 if (x < 2) return false; // Avoid special cases.
5839 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
5840 if (IsPowerOf2(x)) return true; // Simple shift.
5841 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
5842 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
5843 return false;
5844}
5845
5846
5847// Can multiply by anything that IsEasyToMultiplyBy returns true for.
5848// Source and destination may be the same register. This routine does
5849// not set carry and overflow the way a mul instruction would.
5850static void MultiplyByKnownInt(MacroAssembler* masm,
5851 Register source,
5852 Register destination,
5853 int known_int) {
5854 if (IsPowerOf2(known_int)) {
5855 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
5856 } else if (PopCountLessThanEqual2(known_int)) {
5857 int first_bit = BitPosition(known_int);
5858 int second_bit = BitPosition(known_int ^ (1 << first_bit));
5859 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
5860 if (first_bit != 0) {
5861 __ mov(destination, Operand(destination, LSL, first_bit));
5862 }
5863 } else {
5864 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
5865 int the_bit = BitPosition(known_int + 1);
5866 __ rsb(destination, source, Operand(source, LSL, the_bit));
5867 }
5868}
5869
5870
5871// This function (as opposed to MultiplyByKnownInt) takes the known int in a
5872// a register for the cases where it doesn't know a good trick, and may deliver
5873// a result that needs shifting.
5874static void MultiplyByKnownInt2(
5875 MacroAssembler* masm,
5876 Register result,
5877 Register source,
5878 Register known_int_register, // Smi tagged.
5879 int known_int,
5880 int* required_shift) { // Including Smi tag shift
5881 switch (known_int) {
5882 case 3:
5883 __ add(result, source, Operand(source, LSL, 1));
5884 *required_shift = 1;
5885 break;
5886 case 5:
5887 __ add(result, source, Operand(source, LSL, 2));
5888 *required_shift = 1;
5889 break;
5890 case 6:
5891 __ add(result, source, Operand(source, LSL, 1));
5892 *required_shift = 2;
5893 break;
5894 case 7:
5895 __ rsb(result, source, Operand(source, LSL, 3));
5896 *required_shift = 1;
5897 break;
5898 case 9:
5899 __ add(result, source, Operand(source, LSL, 3));
5900 *required_shift = 1;
5901 break;
5902 case 10:
5903 __ add(result, source, Operand(source, LSL, 2));
5904 *required_shift = 2;
5905 break;
5906 default:
5907 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
5908 __ mul(result, source, known_int_register);
5909 *required_shift = 0;
5910 }
5911}
5912
5913
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005914const char* GenericBinaryOpStub::GetName() {
5915 if (name_ != NULL) return name_;
5916 const int len = 100;
5917 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
5918 if (name_ == NULL) return "OOM";
5919 const char* op_name = Token::Name(op_);
5920 const char* overwrite_name;
5921 switch (mode_) {
5922 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
5923 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
5924 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
5925 default: overwrite_name = "UnknownOverwrite"; break;
5926 }
5927
5928 OS::SNPrintF(Vector<char>(name_, len),
5929 "GenericBinaryOpStub_%s_%s%s",
5930 op_name,
5931 overwrite_name,
5932 specialized_on_rhs_ ? "_ConstantRhs" : 0);
5933 return name_;
5934}
5935
5936
ager@chromium.org5c838252010-02-19 08:53:10 +00005937
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005938void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
5939 // r1 : x
5940 // r0 : y
5941 // result : r0
5942
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005943 // All ops need to know whether we are dealing with two Smis. Set up r2 to
5944 // tell us that.
5945 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
5946
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005947 switch (op_) {
5948 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005949 Label not_smi;
5950 // Fast path.
5951 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005952 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005953 __ b(ne, &not_smi);
5954 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
5955 // Return if no overflow.
5956 __ Ret(vc);
5957 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
5958
5959 HandleBinaryOpSlowCases(masm,
5960 &not_smi,
5961 Builtins::ADD,
5962 Token::ADD,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005963 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005964 break;
5965 }
5966
5967 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005968 Label not_smi;
5969 // Fast path.
5970 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005971 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005972 __ b(ne, &not_smi);
5973 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
5974 // Return if no overflow.
5975 __ Ret(vc);
5976 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
5977
5978 HandleBinaryOpSlowCases(masm,
5979 &not_smi,
5980 Builtins::SUB,
5981 Token::SUB,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005982 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005983 break;
5984 }
5985
5986 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005987 Label not_smi, slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005988 ASSERT(kSmiTag == 0); // adjust code below
5989 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005990 __ b(ne, &not_smi);
5991 // Remove tag from one operand (but keep sign), so that result is Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005992 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005993 // Do multiplication
5994 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1.
5995 // Go slow on overflows (overflow bit is not set).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005996 __ mov(ip, Operand(r3, ASR, 31));
5997 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
5998 __ b(ne, &slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005999 // Go slow on zero result to handle -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006000 __ tst(r3, Operand(r3));
6001 __ mov(r0, Operand(r3), LeaveCC, ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006002 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006003 // We need -0 if we were multiplying a negative number with 0 to get 0.
6004 // We know one of them was zero.
6005 __ add(r2, r0, Operand(r1), SetCC);
6006 __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl);
6007 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
6008 // Slow case. We fall through here if we multiplied a negative number
6009 // with 0, because that would mean we should produce -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006010 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006011
6012 HandleBinaryOpSlowCases(masm,
6013 &not_smi,
6014 Builtins::MUL,
6015 Token::MUL,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006016 mode_);
6017 break;
6018 }
6019
6020 case Token::DIV:
6021 case Token::MOD: {
6022 Label not_smi;
6023 if (specialized_on_rhs_) {
6024 Label smi_is_unsuitable;
6025 __ BranchOnNotSmi(r1, &not_smi);
6026 if (IsPowerOf2(constant_rhs_)) {
6027 if (op_ == Token::MOD) {
6028 __ and_(r0,
6029 r1,
6030 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
6031 SetCC);
6032 // We now have the answer, but if the input was negative we also
6033 // have the sign bit. Our work is done if the result is
6034 // positive or zero:
6035 __ Ret(pl);
6036 // A mod of a negative left hand side must return a negative number.
6037 // Unfortunately if the answer is 0 then we must return -0. And we
6038 // already optimistically trashed r0 so we may need to restore it.
6039 __ eor(r0, r0, Operand(0x80000000u), SetCC);
6040 // Next two instructions are conditional on the answer being -0.
6041 __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
6042 __ b(eq, &smi_is_unsuitable);
6043 // We need to subtract the dividend. Eg. -3 % 4 == -3.
6044 __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_)));
6045 } else {
6046 ASSERT(op_ == Token::DIV);
6047 __ tst(r1,
6048 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
6049 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
6050 int shift = 0;
6051 int d = constant_rhs_;
6052 while ((d & 1) == 0) {
6053 d >>= 1;
6054 shift++;
6055 }
6056 __ mov(r0, Operand(r1, LSR, shift));
6057 __ bic(r0, r0, Operand(kSmiTagMask));
6058 }
6059 } else {
6060 // Not a power of 2.
6061 __ tst(r1, Operand(0x80000000u));
6062 __ b(ne, &smi_is_unsuitable);
6063 // Find a fixed point reciprocal of the divisor so we can divide by
6064 // multiplying.
6065 double divisor = 1.0 / constant_rhs_;
6066 int shift = 32;
6067 double scale = 4294967296.0; // 1 << 32.
6068 uint32_t mul;
6069 // Maximise the precision of the fixed point reciprocal.
6070 while (true) {
6071 mul = static_cast<uint32_t>(scale * divisor);
6072 if (mul >= 0x7fffffff) break;
6073 scale *= 2.0;
6074 shift++;
6075 }
6076 mul++;
6077 __ mov(r2, Operand(mul));
6078 __ umull(r3, r2, r2, r1);
6079 __ mov(r2, Operand(r2, LSR, shift - 31));
6080 // r2 is r1 / rhs. r2 is not Smi tagged.
6081 // r0 is still the known rhs. r0 is Smi tagged.
6082 // r1 is still the unkown lhs. r1 is Smi tagged.
6083 int required_r4_shift = 0; // Including the Smi tag shift of 1.
6084 // r4 = r2 * r0.
6085 MultiplyByKnownInt2(masm,
6086 r4,
6087 r2,
6088 r0,
6089 constant_rhs_,
6090 &required_r4_shift);
6091 // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs).
6092 if (op_ == Token::DIV) {
6093 __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC);
6094 __ b(ne, &smi_is_unsuitable); // There was a remainder.
6095 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
6096 } else {
6097 ASSERT(op_ == Token::MOD);
6098 __ sub(r0, r1, Operand(r4, LSL, required_r4_shift));
6099 }
6100 }
6101 __ Ret();
6102 __ bind(&smi_is_unsuitable);
6103 } else {
6104 __ jmp(&not_smi);
6105 }
6106 HandleBinaryOpSlowCases(masm,
6107 &not_smi,
6108 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV,
6109 op_,
6110 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006111 break;
6112 }
6113
6114 case Token::BIT_OR:
6115 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006116 case Token::BIT_XOR:
6117 case Token::SAR:
6118 case Token::SHR:
6119 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006120 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006121 ASSERT(kSmiTag == 0); // adjust code below
6122 __ tst(r2, Operand(kSmiTagMask));
6123 __ b(ne, &slow);
6124 switch (op_) {
6125 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
6126 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
6127 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006128 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006129 // Remove tags from right operand.
ager@chromium.org5c838252010-02-19 08:53:10 +00006130 __ GetLeastBitsFromSmi(r2, r0, 5);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006131 __ mov(r0, Operand(r1, ASR, r2));
6132 // Smi tag result.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006133 __ bic(r0, r0, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006134 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006135 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006136 // Remove tags from operands. We can't do this on a 31 bit number
6137 // because then the 0s get shifted into bit 30 instead of bit 31.
6138 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
ager@chromium.org5c838252010-02-19 08:53:10 +00006139 __ GetLeastBitsFromSmi(r2, r0, 5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006140 __ mov(r3, Operand(r3, LSR, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006141 // Unsigned shift is not allowed to produce a negative number, so
6142 // check the sign bit and the sign bit after Smi tagging.
6143 __ tst(r3, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006144 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006145 // Smi tag result.
6146 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006147 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006148 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006149 // Remove tags from operands.
6150 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
ager@chromium.org5c838252010-02-19 08:53:10 +00006151 __ GetLeastBitsFromSmi(r2, r0, 5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006152 __ mov(r3, Operand(r3, LSL, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006153 // Check that the signed result fits in a Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006154 __ add(r2, r3, Operand(0x40000000), SetCC);
6155 __ b(mi, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006156 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006157 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006158 default: UNREACHABLE();
6159 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006160 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006161 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006162 HandleNonSmiBitwiseOp(masm);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006163 break;
6164 }
6165
6166 default: UNREACHABLE();
6167 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006168 // This code should be unreachable.
6169 __ stop("Unreachable");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006170}
6171
6172
6173void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00006174 // Do tail-call to runtime routine. Runtime routines expect at least one
6175 // argument, so give it a Smi.
6176 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006177 __ push(r0);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006178 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006179
6180 __ StubReturn(1);
6181}
6182
6183
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006184void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006185 Label slow, done;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006186
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006187 if (op_ == Token::SUB) {
6188 // Check whether the value is a smi.
6189 Label try_float;
6190 __ tst(r0, Operand(kSmiTagMask));
6191 __ b(ne, &try_float);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006192
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006193 // Go slow case if the value of the expression is zero
6194 // to make sure that we switch between 0 and -0.
6195 __ cmp(r0, Operand(0));
6196 __ b(eq, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006197
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006198 // The value of the expression is a smi that is not zero. Try
6199 // optimistic subtraction '0 - value'.
6200 __ rsb(r1, r0, Operand(0), SetCC);
6201 __ b(vs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006202
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006203 __ mov(r0, Operand(r1)); // Set r0 to result.
6204 __ b(&done);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006205
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006206 __ bind(&try_float);
6207 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
6208 __ b(ne, &slow);
6209 // r0 is a heap number. Get a new heap number in r1.
6210 if (overwrite_) {
6211 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6212 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
6213 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6214 } else {
6215 AllocateHeapNumber(masm, &slow, r1, r2, r3);
6216 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
6217 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6218 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
6219 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
6220 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
6221 __ mov(r0, Operand(r1));
6222 }
6223 } else if (op_ == Token::BIT_NOT) {
6224 // Check if the operand is a heap number.
6225 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
6226 __ b(ne, &slow);
6227
6228 // Convert the heap number is r0 to an untagged integer in r1.
6229 GetInt32(masm, r0, r1, r2, r3, &slow);
6230
6231 // Do the bitwise operation (move negated) and check if the result
6232 // fits in a smi.
6233 Label try_float;
6234 __ mvn(r1, Operand(r1));
6235 __ add(r2, r1, Operand(0x40000000), SetCC);
6236 __ b(mi, &try_float);
6237 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
6238 __ b(&done);
6239
6240 __ bind(&try_float);
6241 if (!overwrite_) {
6242 // Allocate a fresh heap number, but don't overwrite r0 until
6243 // we're sure we can do it without going through the slow case
6244 // that needs the value in r0.
6245 AllocateHeapNumber(masm, &slow, r2, r3, r4);
6246 __ mov(r0, Operand(r2));
6247 }
6248
6249 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
6250 // have to set up a frame.
6251 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
6252 __ push(lr);
6253 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
6254 __ pop(lr);
6255 } else {
6256 UNIMPLEMENTED();
6257 }
6258
6259 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006260 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006261
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006262 // Handle the slow case by jumping to the JavaScript builtin.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006263 __ bind(&slow);
6264 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006265 switch (op_) {
6266 case Token::SUB:
6267 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
6268 break;
6269 case Token::BIT_NOT:
6270 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
6271 break;
6272 default:
6273 UNREACHABLE();
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006274 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00006275}
6276
6277
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006278void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006279 // r0 holds the exception.
6280
6281 // Adjust this code if not the case.
6282 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
6283
6284 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006285 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
6286 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006287
6288 // Restore the next handler and frame pointer, discard handler state.
6289 ASSERT(StackHandlerConstants::kNextOffset == 0);
6290 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006291 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006292 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
6293 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
6294
6295 // Before returning we restore the context from the frame pointer if
6296 // not NULL. The frame pointer is NULL in the exception handler of a
6297 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006298 __ cmp(fp, Operand(0));
6299 // Set cp to NULL if fp is NULL.
6300 __ mov(cp, Operand(0), LeaveCC, eq);
6301 // Restore cp otherwise.
6302 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006303#ifdef DEBUG
6304 if (FLAG_debug_code) {
6305 __ mov(lr, Operand(pc));
6306 }
6307#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006308 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006309 __ pop(pc);
6310}
6311
6312
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006313void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
6314 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006315 // Adjust this code if not the case.
6316 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
6317
6318 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006319 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006320 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006321
6322 // Unwind the handlers until the ENTRY handler is found.
6323 Label loop, done;
6324 __ bind(&loop);
6325 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006326 const int kStateOffset = StackHandlerConstants::kStateOffset;
6327 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006328 __ cmp(r2, Operand(StackHandler::ENTRY));
6329 __ b(eq, &done);
6330 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006331 const int kNextOffset = StackHandlerConstants::kNextOffset;
6332 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006333 __ jmp(&loop);
6334 __ bind(&done);
6335
6336 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006337 ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006338 __ pop(r2);
6339 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006340
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006341 if (type == OUT_OF_MEMORY) {
6342 // Set external caught exception to false.
6343 ExternalReference external_caught(Top::k_external_caught_exception_address);
6344 __ mov(r0, Operand(false));
6345 __ mov(r2, Operand(external_caught));
6346 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006347
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006348 // Set pending exception and r0 to out of memory exception.
6349 Failure* out_of_memory = Failure::OutOfMemoryException();
6350 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
6351 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
6352 __ str(r0, MemOperand(r2));
6353 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006354
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006355 // Stack layout at this point. See also StackHandlerConstants.
6356 // sp -> state (ENTRY)
6357 // fp
6358 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006359
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006360 // Discard handler state (r2 is not used) and restore frame pointer.
6361 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
6362 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
6363 // Before returning we restore the context from the frame pointer if
6364 // not NULL. The frame pointer is NULL in the exception handler of a
6365 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006366 __ cmp(fp, Operand(0));
6367 // Set cp to NULL if fp is NULL.
6368 __ mov(cp, Operand(0), LeaveCC, eq);
6369 // Restore cp otherwise.
6370 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006371#ifdef DEBUG
6372 if (FLAG_debug_code) {
6373 __ mov(lr, Operand(pc));
6374 }
6375#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006376 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006377 __ pop(pc);
6378}
6379
6380
6381void CEntryStub::GenerateCore(MacroAssembler* masm,
6382 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006383 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006384 Label* throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006385 bool do_gc,
6386 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006387 // r0: result parameter for PerformGC, if any
6388 // r4: number of arguments including receiver (C callee-saved)
6389 // r5: pointer to builtin function (C callee-saved)
6390 // r6: pointer to the first argument (C callee-saved)
6391
6392 if (do_gc) {
6393 // Passing r0.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006394 ExternalReference gc_reference = ExternalReference::perform_gc_function();
6395 __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006396 }
6397
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006398 ExternalReference scope_depth =
6399 ExternalReference::heap_always_allocate_scope_depth();
6400 if (always_allocate) {
6401 __ mov(r0, Operand(scope_depth));
6402 __ ldr(r1, MemOperand(r0));
6403 __ add(r1, r1, Operand(1));
6404 __ str(r1, MemOperand(r0));
6405 }
6406
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006407 // Call C built-in.
6408 // r0 = argc, r1 = argv
6409 __ mov(r0, Operand(r4));
6410 __ mov(r1, Operand(r6));
6411
6412 // TODO(1242173): To let the GC traverse the return address of the exit
6413 // frames, we need to know where the return address is. Right now,
6414 // we push it on the stack to be able to find it again, but we never
6415 // restore from it in case of changes, which makes it impossible to
6416 // support moving the C entry code stub. This should be fixed, but currently
6417 // this is OK because the CEntryStub gets generated so early in the V8 boot
6418 // sequence that it is not moving ever.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006419 masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
6420 masm->push(lr);
6421 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006422
6423 if (always_allocate) {
6424 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
6425 // though (contain the result).
6426 __ mov(r2, Operand(scope_depth));
6427 __ ldr(r3, MemOperand(r2));
6428 __ sub(r3, r3, Operand(1));
6429 __ str(r3, MemOperand(r2));
6430 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006431
6432 // check for failure result
6433 Label failure_returned;
6434 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
6435 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
6436 __ add(r2, r0, Operand(1));
6437 __ tst(r2, Operand(kFailureTagMask));
6438 __ b(eq, &failure_returned);
6439
6440 // Exit C frame and return.
6441 // r0:r1: result
6442 // sp: stack pointer
6443 // fp: frame pointer
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006444 __ LeaveExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006445
6446 // check if we should retry or throw exception
6447 Label retry;
6448 __ bind(&failure_returned);
6449 ASSERT(Failure::RETRY_AFTER_GC == 0);
6450 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
6451 __ b(eq, &retry);
6452
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006453 // Special handling of out of memory exceptions.
6454 Failure* out_of_memory = Failure::OutOfMemoryException();
6455 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
6456 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006457
6458 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00006459 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006460 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00006461 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006462 __ ldr(r0, MemOperand(ip));
6463 __ str(r3, MemOperand(ip));
6464
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006465 // Special handling of termination exceptions which are uncatchable
6466 // by javascript code.
6467 __ cmp(r0, Operand(Factory::termination_exception()));
6468 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006469
6470 // Handle normal exception.
6471 __ jmp(throw_normal_exception);
6472
6473 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
6474}
6475
6476
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006477void CEntryStub::Generate(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006478 // Called from JavaScript; parameters are on stack as if calling JS function
6479 // r0: number of arguments including receiver
6480 // r1: pointer to builtin function
6481 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006482 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006483 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006484
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006485 // Result returned in r0 or r0+r1 by default.
6486
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006487 // NOTE: Invocations of builtins may return failure objects
6488 // instead of a proper result. The builtin entry handles
6489 // this by performing a garbage collection and retrying the
6490 // builtin once.
6491
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006492 // Enter the exit frame that transitions from JavaScript to C++.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006493 __ EnterExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006494
6495 // r4: number of arguments (C callee-saved)
6496 // r5: pointer to builtin function (C callee-saved)
6497 // r6: pointer to first argument (C callee-saved)
6498
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006499 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006500 Label throw_termination_exception;
6501 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006502
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00006503 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006504 GenerateCore(masm,
6505 &throw_normal_exception,
6506 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006507 &throw_out_of_memory_exception,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00006508 false,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006509 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006510
6511 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006512 GenerateCore(masm,
6513 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006514 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006515 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006516 true,
6517 false);
6518
6519 // Do full GC and retry runtime call one final time.
6520 Failure* failure = Failure::InternalError();
6521 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
6522 GenerateCore(masm,
6523 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006524 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006525 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006526 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006527 true);
6528
6529 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006530 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
6531
6532 __ bind(&throw_termination_exception);
6533 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006534
6535 __ bind(&throw_normal_exception);
6536 GenerateThrowTOS(masm);
6537}
6538
6539
6540void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
6541 // r0: code entry
6542 // r1: function
6543 // r2: receiver
6544 // r3: argc
6545 // [sp+0]: argv
6546
6547 Label invoke, exit;
6548
6549 // Called from C, so do not pop argc and args on exit (preserve sp)
6550 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006551 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006552 __ stm(db_w, sp, kCalleeSaved | lr.bit());
6553
6554 // Get address of argv, see stm above.
6555 // r0: code entry
6556 // r1: function
6557 // r2: receiver
6558 // r3: argc
ager@chromium.org5c838252010-02-19 08:53:10 +00006559 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006560
6561 // Push a frame with special values setup to mark it as an entry frame.
6562 // r0: code entry
6563 // r1: function
6564 // r2: receiver
6565 // r3: argc
6566 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006567 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006568 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
6569 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006570 __ mov(r6, Operand(Smi::FromInt(marker)));
6571 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6572 __ ldr(r5, MemOperand(r5));
6573 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
6574
6575 // Setup frame pointer for the frame to be pushed.
6576 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6577
6578 // Call a faked try-block that does the invoke.
6579 __ bl(&invoke);
6580
6581 // Caught exception: Store result (exception) in the pending
6582 // exception field in the JSEnv and return a failure sentinel.
6583 // Coming in here the fp will be invalid because the PushTryHandler below
6584 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00006585 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006586 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006587 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006588 __ b(&exit);
6589
6590 // Invoke: Link this frame into the handler chain.
6591 __ bind(&invoke);
6592 // Must preserve r0-r4, r5-r7 are available.
6593 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006594 // If an exception not caught by another handler occurs, this handler
6595 // returns control to the code after the bl(&invoke) above, which
6596 // restores all kCalleeSaved registers (including cp and fp) to their
6597 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006598
6599 // Clear any pending exceptions.
6600 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
6601 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00006602 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006603 __ str(r5, MemOperand(ip));
6604
6605 // Invoke the function by calling through JS entry trampoline builtin.
6606 // Notice that we cannot store a reference to the trampoline code directly in
6607 // this stub, because runtime stubs are not traversed when doing GC.
6608
6609 // Expected registers by Builtins::JSEntryTrampoline
6610 // r0: code entry
6611 // r1: function
6612 // r2: receiver
6613 // r3: argc
6614 // r4: argv
6615 if (is_construct) {
6616 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
6617 __ mov(ip, Operand(construct_entry));
6618 } else {
6619 ExternalReference entry(Builtins::JSEntryTrampoline);
6620 __ mov(ip, Operand(entry));
6621 }
6622 __ ldr(ip, MemOperand(ip)); // deref address
6623
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006624 // Branch and link to JSEntryTrampoline. We don't use the double underscore
6625 // macro for the add instruction because we don't want the coverage tool
6626 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006627 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006628 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006629
6630 // Unlink this frame from the handler chain. When reading the
6631 // address of the next handler, there is no need to use the address
6632 // displacement since the current stack pointer (sp) points directly
6633 // to the stack handler.
6634 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
6635 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
6636 __ str(r3, MemOperand(ip));
6637 // No need to restore registers
6638 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
6639
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006640
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006641 __ bind(&exit); // r0 holds result
6642 // Restore the top frame descriptors from the stack.
6643 __ pop(r3);
6644 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6645 __ str(r3, MemOperand(ip));
6646
6647 // Reset the stack to the callee saved registers.
6648 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6649
6650 // Restore callee-saved registers and return.
6651#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006652 if (FLAG_debug_code) {
6653 __ mov(lr, Operand(pc));
6654 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006655#endif
6656 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
6657}
6658
6659
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006660// This stub performs an instanceof, calling the builtin function if
6661// necessary. Uses r1 for the object, r0 for the function that it may
6662// be an instance of (these are fetched from the stack).
6663void InstanceofStub::Generate(MacroAssembler* masm) {
6664 // Get the object - slow case for smis (we may need to throw an exception
6665 // depending on the rhs).
6666 Label slow, loop, is_instance, is_not_instance;
6667 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
6668 __ BranchOnSmi(r0, &slow);
6669
6670 // Check that the left hand is a JS object and put map in r3.
6671 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
6672 __ b(lt, &slow);
6673 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
6674 __ b(gt, &slow);
6675
6676 // Get the prototype of the function (r4 is result, r2 is scratch).
ager@chromium.org5c838252010-02-19 08:53:10 +00006677 __ ldr(r1, MemOperand(sp, 0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006678 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
6679
6680 // Check that the function prototype is a JS object.
6681 __ BranchOnSmi(r4, &slow);
6682 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
6683 __ b(lt, &slow);
6684 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
6685 __ b(gt, &slow);
6686
6687 // Register mapping: r3 is object map and r4 is function prototype.
6688 // Get prototype of object into r2.
6689 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
6690
6691 // Loop through the prototype chain looking for the function prototype.
6692 __ bind(&loop);
6693 __ cmp(r2, Operand(r4));
6694 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006695 __ LoadRoot(ip, Heap::kNullValueRootIndex);
6696 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006697 __ b(eq, &is_not_instance);
6698 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
6699 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
6700 __ jmp(&loop);
6701
6702 __ bind(&is_instance);
6703 __ mov(r0, Operand(Smi::FromInt(0)));
6704 __ pop();
6705 __ pop();
6706 __ mov(pc, Operand(lr)); // Return.
6707
6708 __ bind(&is_not_instance);
6709 __ mov(r0, Operand(Smi::FromInt(1)));
6710 __ pop();
6711 __ pop();
6712 __ mov(pc, Operand(lr)); // Return.
6713
6714 // Slow-case. Tail call builtin.
6715 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006716 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
6717}
6718
6719
ager@chromium.org7c537e22008-10-16 08:43:32 +00006720void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006721 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006722 Label adaptor;
6723 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6724 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006725 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006726 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006727
ager@chromium.org7c537e22008-10-16 08:43:32 +00006728 // Nothing to do: The formal number of parameters has already been
6729 // passed in register r0 by calling function. Just return it.
ager@chromium.org9085a012009-05-11 19:22:57 +00006730 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006731
ager@chromium.org7c537e22008-10-16 08:43:32 +00006732 // Arguments adaptor case: Read the arguments length from the
6733 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006734 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006735 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org9085a012009-05-11 19:22:57 +00006736 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006737}
6738
6739
ager@chromium.org7c537e22008-10-16 08:43:32 +00006740void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
6741 // The displacement is the offset of the last parameter (if any)
6742 // relative to the frame pointer.
6743 static const int kDisplacement =
6744 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006745
ager@chromium.org7c537e22008-10-16 08:43:32 +00006746 // Check that the key is a smi.
6747 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006748 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006749
ager@chromium.org7c537e22008-10-16 08:43:32 +00006750 // Check if the calling frame is an arguments adaptor frame.
6751 Label adaptor;
6752 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6753 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006754 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006755 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006756
ager@chromium.org7c537e22008-10-16 08:43:32 +00006757 // Check index against formal parameters count limit passed in
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006758 // through register r0. Use unsigned comparison to get negative
ager@chromium.org7c537e22008-10-16 08:43:32 +00006759 // check for free.
6760 __ cmp(r1, r0);
6761 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006762
ager@chromium.org7c537e22008-10-16 08:43:32 +00006763 // Read the argument from the stack and return it.
6764 __ sub(r3, r0, r1);
6765 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6766 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006767 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006768
6769 // Arguments adaptor case: Check index against actual arguments
6770 // limit found in the arguments adaptor frame. Use unsigned
6771 // comparison to get negative check for free.
6772 __ bind(&adaptor);
6773 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6774 __ cmp(r1, r0);
6775 __ b(cs, &slow);
6776
6777 // Read the argument from the adaptor frame and return it.
6778 __ sub(r3, r0, r1);
6779 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6780 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006781 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006782
6783 // Slow-case: Handle non-smi or out-of-bounds access to arguments
6784 // by calling the runtime system.
6785 __ bind(&slow);
6786 __ push(r1);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006787 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006788}
6789
6790
6791void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
ager@chromium.org5c838252010-02-19 08:53:10 +00006792 // sp[0] : number of parameters
6793 // sp[4] : receiver displacement
6794 // sp[8] : function
6795
ager@chromium.org7c537e22008-10-16 08:43:32 +00006796 // Check if the calling frame is an arguments adaptor frame.
ager@chromium.org5c838252010-02-19 08:53:10 +00006797 Label adaptor_frame, try_allocate, runtime;
ager@chromium.org7c537e22008-10-16 08:43:32 +00006798 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6799 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006800 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org5c838252010-02-19 08:53:10 +00006801 __ b(eq, &adaptor_frame);
6802
6803 // Get the length from the frame.
6804 __ ldr(r1, MemOperand(sp, 0));
6805 __ b(&try_allocate);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006806
6807 // Patch the arguments.length and the parameters pointer.
ager@chromium.org5c838252010-02-19 08:53:10 +00006808 __ bind(&adaptor_frame);
6809 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6810 __ str(r1, MemOperand(sp, 0));
6811 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006812 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
6813 __ str(r3, MemOperand(sp, 1 * kPointerSize));
6814
ager@chromium.org5c838252010-02-19 08:53:10 +00006815 // Try the new space allocation. Start out with computing the size
6816 // of the arguments object and the elements array (in words, not
6817 // bytes because AllocateInNewSpace expects words).
6818 Label add_arguments_object;
6819 __ bind(&try_allocate);
6820 __ cmp(r1, Operand(0));
6821 __ b(eq, &add_arguments_object);
6822 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
6823 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
6824 __ bind(&add_arguments_object);
6825 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
6826
6827 // Do the allocation of both objects in one go.
6828 __ AllocateInNewSpace(r1, r0, r2, r3, &runtime, TAG_OBJECT);
6829
6830 // Get the arguments boilerplate from the current (global) context.
6831 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
6832 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
6833 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
6834 __ ldr(r4, MemOperand(r4, offset));
6835
6836 // Copy the JS object part.
6837 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
6838 __ ldr(r3, FieldMemOperand(r4, i));
6839 __ str(r3, FieldMemOperand(r0, i));
6840 }
6841
6842 // Setup the callee in-object property.
6843 ASSERT(Heap::arguments_callee_index == 0);
6844 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
6845 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
6846
6847 // Get the length (smi tagged) and set that as an in-object property too.
6848 ASSERT(Heap::arguments_length_index == 1);
6849 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
6850 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
6851
6852 // If there are no actual arguments, we're done.
6853 Label done;
6854 __ cmp(r1, Operand(0));
6855 __ b(eq, &done);
6856
6857 // Get the parameters pointer from the stack and untag the length.
6858 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
6859 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
6860
6861 // Setup the elements pointer in the allocated arguments object and
6862 // initialize the header in the elements fixed array.
6863 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
6864 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
6865 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
6866 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
6867 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
6868
6869 // Copy the fixed array slots.
6870 Label loop;
6871 // Setup r4 to point to the first array slot.
6872 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
6873 __ bind(&loop);
6874 // Pre-decrement r2 with kPointerSize on each iteration.
6875 // Pre-decrement in order to skip receiver.
6876 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
6877 // Post-increment r4 with kPointerSize on each iteration.
6878 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
6879 __ sub(r1, r1, Operand(1));
6880 __ cmp(r1, Operand(0));
6881 __ b(ne, &loop);
6882
6883 // Return and remove the on-stack parameters.
6884 __ bind(&done);
6885 __ add(sp, sp, Operand(3 * kPointerSize));
6886 __ Ret();
6887
ager@chromium.org7c537e22008-10-16 08:43:32 +00006888 // Do the runtime call to allocate the arguments object.
6889 __ bind(&runtime);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006890 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006891}
6892
6893
6894void CallFunctionStub::Generate(MacroAssembler* masm) {
6895 Label slow;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006896
6897 // If the receiver might be a value (string, number or boolean) check for this
6898 // and box it if it is.
6899 if (ReceiverMightBeValue()) {
6900 // Get the receiver from the stack.
6901 // function, receiver [, arguments]
6902 Label receiver_is_value, receiver_is_js_object;
6903 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
6904
6905 // Check if receiver is a smi (which is a number value).
6906 __ BranchOnSmi(r1, &receiver_is_value);
6907
6908 // Check if the receiver is a valid JS object.
6909 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
6910 __ b(ge, &receiver_is_js_object);
6911
6912 // Call the runtime to box the value.
6913 __ bind(&receiver_is_value);
6914 __ EnterInternalFrame();
6915 __ push(r1);
6916 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
6917 __ LeaveInternalFrame();
6918 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
6919
6920 __ bind(&receiver_is_js_object);
6921 }
6922
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006923 // Get the function to call from the stack.
6924 // function, receiver [, arguments]
6925 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
6926
6927 // Check that the function is really a JavaScript function.
6928 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006929 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006930 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006931 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006932 __ b(ne, &slow);
6933
6934 // Fast-case: Invoke the function now.
6935 // r1: pushed function
6936 ParameterCount actual(argc_);
6937 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
6938
6939 // Slow-case: Non-function called.
6940 __ bind(&slow);
ager@chromium.org5c838252010-02-19 08:53:10 +00006941 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
6942 // of the original receiver from the call site).
6943 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006944 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006945 __ mov(r2, Operand(0));
6946 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
6947 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
6948 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006949}
6950
6951
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006952const char* CompareStub::GetName() {
6953 switch (cc_) {
6954 case lt: return "CompareStub_LT";
6955 case gt: return "CompareStub_GT";
6956 case le: return "CompareStub_LE";
6957 case ge: return "CompareStub_GE";
6958 case ne: {
6959 if (strict_) {
6960 if (never_nan_nan_) {
6961 return "CompareStub_NE_STRICT_NO_NAN";
6962 } else {
6963 return "CompareStub_NE_STRICT";
6964 }
6965 } else {
6966 if (never_nan_nan_) {
6967 return "CompareStub_NE_NO_NAN";
6968 } else {
6969 return "CompareStub_NE";
6970 }
6971 }
6972 }
6973 case eq: {
6974 if (strict_) {
6975 if (never_nan_nan_) {
6976 return "CompareStub_EQ_STRICT_NO_NAN";
6977 } else {
6978 return "CompareStub_EQ_STRICT";
6979 }
6980 } else {
6981 if (never_nan_nan_) {
6982 return "CompareStub_EQ_NO_NAN";
6983 } else {
6984 return "CompareStub_EQ";
6985 }
6986 }
6987 }
6988 default: return "CompareStub";
6989 }
6990}
6991
6992
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006993int CompareStub::MinorKey() {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006994 // Encode the three parameters in a unique 16 bit value.
6995 ASSERT((static_cast<unsigned>(cc_) >> 26) < (1 << 16));
6996 int nnn_value = (never_nan_nan_ ? 2 : 0);
6997 if (cc_ != eq) nnn_value = 0; // Avoid duplicate stubs.
6998 return (static_cast<unsigned>(cc_) >> 26) | nnn_value | (strict_ ? 1 : 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006999}
7000
7001
ager@chromium.org5c838252010-02-19 08:53:10 +00007002void StringStubBase::GenerateCopyCharacters(MacroAssembler* masm,
7003 Register dest,
7004 Register src,
7005 Register count,
7006 Register scratch,
7007 bool ascii) {
7008 Label loop;
7009 Label done;
7010 // This loop just copies one character at a time, as it is only used for very
7011 // short strings.
7012 if (!ascii) {
7013 __ add(count, count, Operand(count), SetCC);
7014 } else {
7015 __ cmp(count, Operand(0));
7016 }
7017 __ b(eq, &done);
7018
7019 __ bind(&loop);
7020 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
7021 // Perform sub between load and dependent store to get the load time to
7022 // complete.
7023 __ sub(count, count, Operand(1), SetCC);
7024 __ strb(scratch, MemOperand(dest, 1, PostIndex));
7025 // last iteration.
7026 __ b(gt, &loop);
7027
7028 __ bind(&done);
7029}
7030
7031
7032enum CopyCharactersFlags {
7033 COPY_ASCII = 1,
7034 DEST_ALWAYS_ALIGNED = 2
7035};
7036
7037
7038void StringStubBase::GenerateCopyCharactersLong(MacroAssembler* masm,
7039 Register dest,
7040 Register src,
7041 Register count,
7042 Register scratch1,
7043 Register scratch2,
7044 Register scratch3,
7045 Register scratch4,
7046 Register scratch5,
7047 int flags) {
7048 bool ascii = (flags & COPY_ASCII) != 0;
7049 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
7050
7051 if (dest_always_aligned && FLAG_debug_code) {
7052 // Check that destination is actually word aligned if the flag says
7053 // that it is.
7054 __ tst(dest, Operand(kPointerAlignmentMask));
7055 __ Check(eq, "Destination of copy not aligned.");
7056 }
7057
7058 const int kReadAlignment = 4;
7059 const int kReadAlignmentMask = kReadAlignment - 1;
7060 // Ensure that reading an entire aligned word containing the last character
7061 // of a string will not read outside the allocated area (because we pad up
7062 // to kObjectAlignment).
7063 ASSERT(kObjectAlignment >= kReadAlignment);
7064 // Assumes word reads and writes are little endian.
7065 // Nothing to do for zero characters.
7066 Label done;
7067 if (!ascii) {
7068 __ add(count, count, Operand(count), SetCC);
7069 } else {
7070 __ cmp(count, Operand(0));
7071 }
7072 __ b(eq, &done);
7073
7074 // Assume that you cannot read (or write) unaligned.
7075 Label byte_loop;
7076 // Must copy at least eight bytes, otherwise just do it one byte at a time.
7077 __ cmp(count, Operand(8));
7078 __ add(count, dest, Operand(count));
7079 Register limit = count; // Read until src equals this.
7080 __ b(lt, &byte_loop);
7081
7082 if (!dest_always_aligned) {
7083 // Align dest by byte copying. Copies between zero and three bytes.
7084 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
7085 Label dest_aligned;
7086 __ b(eq, &dest_aligned);
7087 __ cmp(scratch4, Operand(2));
7088 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
7089 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
7090 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
7091 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
7092 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
7093 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
7094 __ bind(&dest_aligned);
7095 }
7096
7097 Label simple_loop;
7098
7099 __ sub(scratch4, dest, Operand(src));
7100 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
7101 __ b(eq, &simple_loop);
7102 // Shift register is number of bits in a source word that
7103 // must be combined with bits in the next source word in order
7104 // to create a destination word.
7105
7106 // Complex loop for src/dst that are not aligned the same way.
7107 {
7108 Label loop;
7109 __ mov(scratch4, Operand(scratch4, LSL, 3));
7110 Register left_shift = scratch4;
7111 __ and_(src, src, Operand(~3)); // Round down to load previous word.
7112 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
7113 // Store the "shift" most significant bits of scratch in the least
7114 // signficant bits (i.e., shift down by (32-shift)).
7115 __ rsb(scratch2, left_shift, Operand(32));
7116 Register right_shift = scratch2;
7117 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
7118
7119 __ bind(&loop);
7120 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
7121 __ sub(scratch5, limit, Operand(dest));
7122 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
7123 __ str(scratch1, MemOperand(dest, 4, PostIndex));
7124 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
7125 // Loop if four or more bytes left to copy.
7126 // Compare to eight, because we did the subtract before increasing dst.
7127 __ sub(scratch5, scratch5, Operand(8), SetCC);
7128 __ b(ge, &loop);
7129 }
7130 // There is now between zero and three bytes left to copy (negative that
7131 // number is in scratch5), and between one and three bytes already read into
7132 // scratch1 (eight times that number in scratch4). We may have read past
7133 // the end of the string, but because objects are aligned, we have not read
7134 // past the end of the object.
7135 // Find the minimum of remaining characters to move and preloaded characters
7136 // and write those as bytes.
7137 __ add(scratch5, scratch5, Operand(4), SetCC);
7138 __ b(eq, &done);
7139 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
7140 // Move minimum of bytes read and bytes left to copy to scratch4.
7141 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
7142 // Between one and three (value in scratch5) characters already read into
7143 // scratch ready to write.
7144 __ cmp(scratch5, Operand(2));
7145 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
7146 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
7147 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
7148 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
7149 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
7150 // Copy any remaining bytes.
7151 __ b(&byte_loop);
7152
7153 // Simple loop.
7154 // Copy words from src to dst, until less than four bytes left.
7155 // Both src and dest are word aligned.
7156 __ bind(&simple_loop);
7157 {
7158 Label loop;
7159 __ bind(&loop);
7160 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
7161 __ sub(scratch3, limit, Operand(dest));
7162 __ str(scratch1, MemOperand(dest, 4, PostIndex));
7163 // Compare to 8, not 4, because we do the substraction before increasing
7164 // dest.
7165 __ cmp(scratch3, Operand(8));
7166 __ b(ge, &loop);
7167 }
7168
7169 // Copy bytes from src to dst until dst hits limit.
7170 __ bind(&byte_loop);
7171 __ cmp(dest, Operand(limit));
7172 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
7173 __ b(ge, &done);
7174 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
7175 __ b(&byte_loop);
7176
7177 __ bind(&done);
7178}
7179
7180
7181void SubStringStub::Generate(MacroAssembler* masm) {
7182 Label runtime;
7183
7184 // Stack frame on entry.
7185 // lr: return address
7186 // sp[0]: to
7187 // sp[4]: from
7188 // sp[8]: string
7189
7190 // This stub is called from the native-call %_SubString(...), so
7191 // nothing can be assumed about the arguments. It is tested that:
7192 // "string" is a sequential string,
7193 // both "from" and "to" are smis, and
7194 // 0 <= from <= to <= string.length.
7195 // If any of these assumptions fail, we call the runtime system.
7196
7197 static const int kToOffset = 0 * kPointerSize;
7198 static const int kFromOffset = 1 * kPointerSize;
7199 static const int kStringOffset = 2 * kPointerSize;
7200
7201
7202 // Check bounds and smi-ness.
7203 __ ldr(r7, MemOperand(sp, kToOffset));
7204 __ ldr(r6, MemOperand(sp, kFromOffset));
7205 ASSERT_EQ(0, kSmiTag);
7206 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
7207 // I.e., arithmetic shift right by one un-smi-tags.
7208 __ mov(r2, Operand(r7, ASR, 1), SetCC);
7209 __ mov(r3, Operand(r6, ASR, 1), SetCC, cc);
7210 // If either r2 or r6 had the smi tag bit set, then carry is set now.
7211 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
7212 __ b(mi, &runtime); // From is negative.
7213
7214 __ sub(r2, r2, Operand(r3), SetCC);
7215 __ b(mi, &runtime); // Fail if from > to.
7216 // Handle sub-strings of length 2 and less in the runtime system.
7217 __ cmp(r2, Operand(2));
7218 __ b(le, &runtime);
7219
7220 // r2: length
7221 // r6: from (smi)
7222 // r7: to (smi)
7223
7224 // Make sure first argument is a sequential (or flat) string.
7225 __ ldr(r5, MemOperand(sp, kStringOffset));
7226 ASSERT_EQ(0, kSmiTag);
7227 __ tst(r5, Operand(kSmiTagMask));
7228 __ b(eq, &runtime);
7229 Condition is_string = masm->IsObjectStringType(r5, r1);
7230 __ b(NegateCondition(is_string), &runtime);
7231
7232 // r1: instance type
7233 // r2: length
7234 // r5: string
7235 // r6: from (smi)
7236 // r7: to (smi)
7237 Label seq_string;
7238 __ and_(r4, r1, Operand(kStringRepresentationMask));
7239 ASSERT(kSeqStringTag < kConsStringTag);
7240 ASSERT(kExternalStringTag > kConsStringTag);
7241 __ cmp(r4, Operand(kConsStringTag));
7242 __ b(gt, &runtime); // External strings go to runtime.
7243 __ b(lt, &seq_string); // Sequential strings are handled directly.
7244
7245 // Cons string. Try to recurse (once) on the first substring.
7246 // (This adds a little more generality than necessary to handle flattened
7247 // cons strings, but not much).
7248 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
7249 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
7250 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
7251 __ tst(r1, Operand(kStringRepresentationMask));
7252 ASSERT_EQ(0, kSeqStringTag);
7253 __ b(ne, &runtime); // Cons and External strings go to runtime.
7254
7255 // Definitly a sequential string.
7256 __ bind(&seq_string);
7257
7258 // r1: instance type.
7259 // r2: length
7260 // r5: string
7261 // r6: from (smi)
7262 // r7: to (smi)
7263 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
7264 __ cmp(r4, Operand(r7, ASR, 1));
7265 __ b(lt, &runtime); // Fail if to > length.
7266
7267 // r1: instance type.
7268 // r2: result string length.
7269 // r5: string.
7270 // r6: from offset (smi)
7271 // Check for flat ascii string.
7272 Label non_ascii_flat;
7273 __ tst(r1, Operand(kStringEncodingMask));
7274 ASSERT_EQ(0, kTwoByteStringTag);
7275 __ b(eq, &non_ascii_flat);
7276
7277 // Allocate the result.
7278 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
7279
7280 // r0: result string.
7281 // r2: result string length.
7282 // r5: string.
7283 // r6: from offset (smi)
7284 // Locate first character of result.
7285 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7286 // Locate 'from' character of string.
7287 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7288 __ add(r5, r5, Operand(r6, ASR, 1));
7289
7290 // r0: result string.
7291 // r1: first character of result string.
7292 // r2: result string length.
7293 // r5: first character of sub string to copy.
7294 ASSERT_EQ(0, SeqAsciiString::kHeaderSize & kObjectAlignmentMask);
7295 GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
7296 COPY_ASCII | DEST_ALWAYS_ALIGNED);
7297 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
7298 __ add(sp, sp, Operand(3 * kPointerSize));
7299 __ Ret();
7300
7301 __ bind(&non_ascii_flat);
7302 // r2: result string length.
7303 // r5: string.
7304 // r6: from offset (smi)
7305 // Check for flat two byte string.
7306
7307 // Allocate the result.
7308 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
7309
7310 // r0: result string.
7311 // r2: result string length.
7312 // r5: string.
7313 // Locate first character of result.
7314 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7315 // Locate 'from' character of string.
7316 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7317 // As "from" is a smi it is 2 times the value which matches the size of a two
7318 // byte character.
7319 __ add(r5, r5, Operand(r6));
7320
7321 // r0: result string.
7322 // r1: first character of result.
7323 // r2: result length.
7324 // r5: first character of string to copy.
7325 ASSERT_EQ(0, SeqTwoByteString::kHeaderSize & kObjectAlignmentMask);
7326 GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
7327 DEST_ALWAYS_ALIGNED);
7328 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
7329 __ add(sp, sp, Operand(3 * kPointerSize));
7330 __ Ret();
7331
7332 // Just jump to runtime to create the sub string.
7333 __ bind(&runtime);
7334 __ TailCallRuntime(ExternalReference(Runtime::kSubString), 3, 1);
7335}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007336
7337
7338void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
7339 Register left,
7340 Register right,
7341 Register scratch1,
7342 Register scratch2,
7343 Register scratch3,
7344 Register scratch4) {
7345 Label compare_lengths;
7346 // Find minimum length and length difference.
7347 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
7348 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
7349 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
7350 Register length_delta = scratch3;
7351 __ mov(scratch1, scratch2, LeaveCC, gt);
7352 Register min_length = scratch1;
7353 __ tst(min_length, Operand(min_length));
7354 __ b(eq, &compare_lengths);
7355
7356 // Setup registers so that we only need to increment one register
7357 // in the loop.
7358 __ add(scratch2, min_length,
7359 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7360 __ add(left, left, Operand(scratch2));
7361 __ add(right, right, Operand(scratch2));
7362 // Registers left and right points to the min_length character of strings.
7363 __ rsb(min_length, min_length, Operand(-1));
7364 Register index = min_length;
7365 // Index starts at -min_length.
7366
7367 {
7368 // Compare loop.
7369 Label loop;
7370 __ bind(&loop);
7371 // Compare characters.
7372 __ add(index, index, Operand(1), SetCC);
7373 __ ldrb(scratch2, MemOperand(left, index), ne);
7374 __ ldrb(scratch4, MemOperand(right, index), ne);
7375 // Skip to compare lengths with eq condition true.
7376 __ b(eq, &compare_lengths);
7377 __ cmp(scratch2, scratch4);
7378 __ b(eq, &loop);
7379 // Fallthrough with eq condition false.
7380 }
7381 // Compare lengths - strings up to min-length are equal.
7382 __ bind(&compare_lengths);
7383 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
7384 // Use zero length_delta as result.
7385 __ mov(r0, Operand(length_delta), SetCC, eq);
7386 // Fall through to here if characters compare not-equal.
7387 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
7388 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
7389 __ Ret();
7390}
7391
7392
7393void StringCompareStub::Generate(MacroAssembler* masm) {
7394 Label runtime;
7395
7396 // Stack frame on entry.
ager@chromium.org5c838252010-02-19 08:53:10 +00007397 // sp[0]: right string
7398 // sp[4]: left string
7399 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left
7400 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007401
7402 Label not_same;
7403 __ cmp(r0, r1);
7404 __ b(ne, &not_same);
7405 ASSERT_EQ(0, EQUAL);
7406 ASSERT_EQ(0, kSmiTag);
7407 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
7408 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
7409 __ add(sp, sp, Operand(2 * kPointerSize));
7410 __ Ret();
7411
7412 __ bind(&not_same);
7413
7414 // Check that both objects are sequential ascii strings.
7415 __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime);
7416
7417 // Compare flat ascii strings natively. Remove arguments from stack first.
7418 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
7419 __ add(sp, sp, Operand(2 * kPointerSize));
7420 GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5);
7421
7422 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
7423 // tagged as a small integer.
7424 __ bind(&runtime);
7425 __ TailCallRuntime(ExternalReference(Runtime::kStringCompare), 2, 1);
7426}
7427
7428
ager@chromium.org5c838252010-02-19 08:53:10 +00007429void StringAddStub::Generate(MacroAssembler* masm) {
7430 Label string_add_runtime;
7431 // Stack on entry:
7432 // sp[0]: second argument.
7433 // sp[4]: first argument.
7434
7435 // Load the two arguments.
7436 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
7437 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
7438
7439 // Make sure that both arguments are strings if not known in advance.
7440 if (string_check_) {
7441 ASSERT_EQ(0, kSmiTag);
7442 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
7443 // Load instance types.
7444 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
7445 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
7446 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
7447 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
7448 ASSERT_EQ(0, kStringTag);
7449 // If either is not a string, go to runtime.
7450 __ tst(r4, Operand(kIsNotStringMask));
7451 __ tst(r5, Operand(kIsNotStringMask), eq);
7452 __ b(ne, &string_add_runtime);
7453 }
7454
7455 // Both arguments are strings.
7456 // r0: first string
7457 // r1: second string
7458 // r4: first string instance type (if string_check_)
7459 // r5: second string instance type (if string_check_)
7460 {
7461 Label strings_not_empty;
7462 // Check if either of the strings are empty. In that case return the other.
7463 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
7464 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
7465 __ cmp(r2, Operand(0)); // Test if first string is empty.
7466 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
7467 __ cmp(r3, Operand(0), ne); // Else test if second string is empty.
7468 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
7469
7470 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
7471 __ add(sp, sp, Operand(2 * kPointerSize));
7472 __ Ret();
7473
7474 __ bind(&strings_not_empty);
7475 }
7476
7477 // Both strings are non-empty.
7478 // r0: first string
7479 // r1: second string
7480 // r2: length of first string
7481 // r3: length of second string
7482 // r4: first string instance type (if string_check_)
7483 // r5: second string instance type (if string_check_)
7484 // Look at the length of the result of adding the two strings.
7485 Label string_add_flat_result;
7486 // Adding two lengths can't overflow.
7487 ASSERT(String::kMaxLength * 2 > String::kMaxLength);
7488 __ add(r6, r2, Operand(r3));
7489 // Use the runtime system when adding two one character strings, as it
7490 // contains optimizations for this specific case using the symbol table.
7491 __ cmp(r6, Operand(2));
7492 __ b(eq, &string_add_runtime);
7493 // Check if resulting string will be flat.
7494 __ cmp(r6, Operand(String::kMinNonFlatLength));
7495 __ b(lt, &string_add_flat_result);
7496 // Handle exceptionally long strings in the runtime system.
7497 ASSERT((String::kMaxLength & 0x80000000) == 0);
7498 ASSERT(IsPowerOf2(String::kMaxLength + 1));
7499 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
7500 __ cmp(r6, Operand(String::kMaxLength + 1));
7501 __ b(hs, &string_add_runtime);
7502
7503 // If result is not supposed to be flat, allocate a cons string object.
7504 // If both strings are ascii the result is an ascii cons string.
7505 if (!string_check_) {
7506 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
7507 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
7508 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
7509 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
7510 }
7511 Label non_ascii, allocated;
7512 ASSERT_EQ(0, kTwoByteStringTag);
7513 __ tst(r4, Operand(kStringEncodingMask));
7514 __ tst(r5, Operand(kStringEncodingMask), ne);
7515 __ b(eq, &non_ascii);
7516
7517 // Allocate an ASCII cons string.
7518 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
7519 __ bind(&allocated);
7520 // Fill the fields of the cons string.
7521 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
7522 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
7523 __ mov(r0, Operand(r7));
7524 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
7525 __ add(sp, sp, Operand(2 * kPointerSize));
7526 __ Ret();
7527
7528 __ bind(&non_ascii);
7529 // Allocate a two byte cons string.
7530 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
7531 __ jmp(&allocated);
7532
7533 // Handle creating a flat result. First check that both strings are
7534 // sequential and that they have the same encoding.
7535 // r0: first string
7536 // r1: second string
7537 // r2: length of first string
7538 // r3: length of second string
7539 // r4: first string instance type (if string_check_)
7540 // r5: second string instance type (if string_check_)
7541 // r6: sum of lengths.
7542 __ bind(&string_add_flat_result);
7543 if (!string_check_) {
7544 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
7545 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
7546 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
7547 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
7548 }
7549 // Check that both strings are sequential.
7550 ASSERT_EQ(0, kSeqStringTag);
7551 __ tst(r4, Operand(kStringRepresentationMask));
7552 __ tst(r5, Operand(kStringRepresentationMask), eq);
7553 __ b(ne, &string_add_runtime);
7554 // Now check if both strings have the same encoding (ASCII/Two-byte).
7555 // r0: first string.
7556 // r1: second string.
7557 // r2: length of first string.
7558 // r3: length of second string.
7559 // r6: sum of lengths..
7560 Label non_ascii_string_add_flat_result;
7561 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
7562 __ eor(r7, r4, Operand(r5));
7563 __ tst(r7, Operand(kStringEncodingMask));
7564 __ b(ne, &string_add_runtime);
7565 // And see if it's ASCII or two-byte.
7566 __ tst(r4, Operand(kStringEncodingMask));
7567 __ b(eq, &non_ascii_string_add_flat_result);
7568
7569 // Both strings are sequential ASCII strings. We also know that they are
7570 // short (since the sum of the lengths is less than kMinNonFlatLength).
7571 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
7572 // Locate first character of result.
7573 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7574 // Locate first character of first argument.
7575 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7576 // r0: first character of first string.
7577 // r1: second string.
7578 // r2: length of first string.
7579 // r3: length of second string.
7580 // r6: first character of result.
7581 // r7: result string.
7582 GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
7583
7584 // Load second argument and locate first character.
7585 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7586 // r1: first character of second string.
7587 // r3: length of second string.
7588 // r6: next character of result.
7589 // r7: result string.
7590 GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
7591 __ mov(r0, Operand(r7));
7592 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
7593 __ add(sp, sp, Operand(2 * kPointerSize));
7594 __ Ret();
7595
7596 __ bind(&non_ascii_string_add_flat_result);
7597 // Both strings are sequential two byte strings.
7598 // r0: first string.
7599 // r1: second string.
7600 // r2: length of first string.
7601 // r3: length of second string.
7602 // r6: sum of length of strings.
7603 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
7604 // r0: first string.
7605 // r1: second string.
7606 // r2: length of first string.
7607 // r3: length of second string.
7608 // r7: result string.
7609
7610 // Locate first character of result.
7611 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7612 // Locate first character of first argument.
7613 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7614
7615 // r0: first character of first string.
7616 // r1: second string.
7617 // r2: length of first string.
7618 // r3: length of second string.
7619 // r6: first character of result.
7620 // r7: result string.
7621 GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
7622
7623 // Locate first character of second argument.
7624 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7625
7626 // r1: first character of second string.
7627 // r3: length of second string.
7628 // r6: next character of result (after copy of first string).
7629 // r7: result string.
7630 GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
7631
7632 __ mov(r0, Operand(r7));
7633 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
7634 __ add(sp, sp, Operand(2 * kPointerSize));
7635 __ Ret();
7636
7637 // Just jump to runtime to add the two strings.
7638 __ bind(&string_add_runtime);
7639 __ TailCallRuntime(ExternalReference(Runtime::kStringAdd), 2, 1);
7640}
7641
7642
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007643#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007644
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007645} } // namespace v8::internal