blob: 046d7b95b17d09f6f4248b81803f30a60d6ad29e [file] [log] [blame]
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001// Copyright 2010 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "bootstrapper.h"
31#include "codegen-inl.h"
ager@chromium.orgc4c92722009-11-18 14:12:51 +000032#include "compiler.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000033#include "debug.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000034#include "parser.h"
35#include "register-allocator-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036#include "runtime.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000037#include "scopes.h"
38
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000039
kasperl@chromium.org71affb52009-05-26 05:44:31 +000040namespace v8 {
41namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042
ager@chromium.org65dad4b2009-04-23 08:48:43 +000043#define __ ACCESS_MASM(masm_)
44
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000045static void EmitIdenticalObjectComparison(MacroAssembler* masm,
46 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000047 Condition cc,
48 bool never_nan_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000049static void EmitSmiNonsmiComparison(MacroAssembler* masm,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000050 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000051 Label* slow,
52 bool strict);
53static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc);
54static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000055static void MultiplyByKnownInt(MacroAssembler* masm,
56 Register source,
57 Register destination,
58 int known_int);
59static bool IsEasyToMultiplyBy(int x);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000060
61
62
ager@chromium.orge2902be2009-06-08 12:21:35 +000063// -------------------------------------------------------------------------
64// Platform-specific DeferredCode functions.
65
66void DeferredCode::SaveRegisters() {
67 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) {
68 int action = registers_[i];
69 if (action == kPush) {
70 __ push(RegisterAllocator::ToRegister(i));
71 } else if (action != kIgnore && (action & kSyncedFlag) == 0) {
72 __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
73 }
74 }
75}
76
77
78void DeferredCode::RestoreRegisters() {
79 // Restore registers in reverse order due to the stack.
80 for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) {
81 int action = registers_[i];
82 if (action == kPush) {
83 __ pop(RegisterAllocator::ToRegister(i));
84 } else if (action != kIgnore) {
85 action &= ~kSyncedFlag;
86 __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
87 }
88 }
89}
90
ager@chromium.org3bf7b912008-11-17 09:09:45 +000091
92// -------------------------------------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000093// CodeGenState implementation.
94
ager@chromium.org7c537e22008-10-16 08:43:32 +000095CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000096 : owner_(owner),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000097 true_target_(NULL),
98 false_target_(NULL),
99 previous_(NULL) {
100 owner_->set_state(this);
101}
102
103
ager@chromium.org7c537e22008-10-16 08:43:32 +0000104CodeGenState::CodeGenState(CodeGenerator* owner,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000105 JumpTarget* true_target,
106 JumpTarget* false_target)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000107 : owner_(owner),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000108 true_target_(true_target),
109 false_target_(false_target),
110 previous_(owner->state()) {
111 owner_->set_state(this);
112}
113
114
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000115CodeGenState::~CodeGenState() {
116 ASSERT(owner_->state() == this);
117 owner_->set_state(previous_);
118}
119
120
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000121// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +0000122// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123
ager@chromium.org5c838252010-02-19 08:53:10 +0000124CodeGenerator::CodeGenerator(MacroAssembler* masm)
125 : deferred_(8),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000126 masm_(masm),
ager@chromium.org5c838252010-02-19 08:53:10 +0000127 info_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000128 frame_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000129 allocator_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000130 cc_reg_(al),
131 state_(NULL),
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000132 function_return_is_shadowed_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133}
134
135
ager@chromium.org5c838252010-02-19 08:53:10 +0000136Scope* CodeGenerator::scope() { return info_->function()->scope(); }
137
138
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000139// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000140// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000142// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143// cp: callee's context
144
ager@chromium.org5c838252010-02-19 08:53:10 +0000145void CodeGenerator::Generate(CompilationInfo* info, Mode mode) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000146 // Record the position for debugging purposes.
ager@chromium.org5c838252010-02-19 08:53:10 +0000147 CodeForFunctionPosition(info->function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148
149 // Initialize state.
ager@chromium.org5c838252010-02-19 08:53:10 +0000150 info_ = info;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000151 ASSERT(allocator_ == NULL);
152 RegisterAllocator register_allocator(this);
153 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000154 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000155 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000156 cc_reg_ = al;
157 {
158 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000160 // Entry:
161 // Stack: receiver, arguments
162 // lr: return address
163 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000164 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000165 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000166 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000167 allocator_->Initialize();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000168
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169#ifdef DEBUG
170 if (strlen(FLAG_stop_at) > 0 &&
ager@chromium.org5c838252010-02-19 08:53:10 +0000171 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000172 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000173 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174 }
175#endif
176
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000177 if (mode == PRIMARY) {
178 frame_->Enter();
179 // tos: code slot
180
181 // Allocate space for locals and initialize them. This also checks
182 // for stack overflow.
183 frame_->AllocateStackSlots();
184
185 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org5c838252010-02-19 08:53:10 +0000186 int heap_slots = scope()->num_heap_slots();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000187 if (heap_slots > 0) {
188 // Allocate local context.
189 // Get outer context and create a new context based on it.
190 __ ldr(r0, frame_->Function());
191 frame_->EmitPush(r0);
192 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
193 FastNewContextStub stub(heap_slots);
194 frame_->CallStub(&stub, 1);
195 } else {
196 frame_->CallRuntime(Runtime::kNewContext, 1);
197 }
198
199#ifdef DEBUG
200 JumpTarget verified_true;
201 __ cmp(r0, Operand(cp));
202 verified_true.Branch(eq);
203 __ stop("NewContext: r0 is expected to be the same as cp");
204 verified_true.Bind();
205#endif
206 // Update context local.
207 __ str(cp, frame_->Context());
208 }
209
210 // TODO(1241774): Improve this code:
211 // 1) only needed if we have a context
212 // 2) no need to recompute context ptr every single time
213 // 3) don't copy parameter operand code from SlotOperand!
214 {
215 Comment cmnt2(masm_, "[ copy context parameters into .context");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000216 // Note that iteration order is relevant here! If we have the same
217 // parameter twice (e.g., function (x, y, x)), and that parameter
218 // needs to be copied into the context, it must be the last argument
219 // passed to the parameter that needs to be copied. This is a rare
220 // case so we don't check for it, instead we rely on the copying
221 // order: such a parameter is copied repeatedly into the same
222 // context location and thus the last value is what is seen inside
223 // the function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000224 for (int i = 0; i < scope()->num_parameters(); i++) {
225 Variable* par = scope()->parameter(i);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000226 Slot* slot = par->slot();
227 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000228 ASSERT(!scope()->is_global_scope()); // No params in global scope.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000229 __ ldr(r1, frame_->ParameterAt(i));
230 // Loads r2 with context; used below in RecordWrite.
231 __ str(r1, SlotOperand(slot, r2));
232 // Load the offset into r3.
233 int slot_offset =
234 FixedArray::kHeaderSize + slot->index() * kPointerSize;
235 __ mov(r3, Operand(slot_offset));
236 __ RecordWrite(r2, r3, r1);
237 }
238 }
239 }
240
241 // Store the arguments object. This must happen after context
242 // initialization because the arguments object may be stored in the
243 // context.
ager@chromium.org5c838252010-02-19 08:53:10 +0000244 if (scope()->arguments() != NULL) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000245 Comment cmnt(masm_, "[ allocate arguments object");
ager@chromium.org5c838252010-02-19 08:53:10 +0000246 ASSERT(scope()->arguments_shadow() != NULL);
247 Variable* arguments = scope()->arguments()->var();
248 Variable* shadow = scope()->arguments_shadow()->var();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000249 ASSERT(arguments != NULL && arguments->slot() != NULL);
250 ASSERT(shadow != NULL && shadow->slot() != NULL);
251 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
252 __ ldr(r2, frame_->Function());
253 // The receiver is below the arguments, the return address, and the
254 // frame pointer on the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +0000255 const int kReceiverDisplacement = 2 + scope()->num_parameters();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000256 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
ager@chromium.org5c838252010-02-19 08:53:10 +0000257 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000258 frame_->Adjust(3);
259 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
260 frame_->CallStub(&stub, 3);
261 frame_->EmitPush(r0);
262 StoreToSlot(arguments->slot(), NOT_CONST_INIT);
263 StoreToSlot(shadow->slot(), NOT_CONST_INIT);
264 frame_->Drop(); // Value is no longer needed.
265 }
266
267 // Initialize ThisFunction reference if present.
ager@chromium.org5c838252010-02-19 08:53:10 +0000268 if (scope()->is_function_scope() && scope()->function() != NULL) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000269 __ mov(ip, Operand(Factory::the_hole_value()));
270 frame_->EmitPush(ip);
ager@chromium.org5c838252010-02-19 08:53:10 +0000271 StoreToSlot(scope()->function()->slot(), NOT_CONST_INIT);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000272 }
273 } else {
274 // When used as the secondary compiler for splitting, r1, cp,
275 // fp, and lr have been pushed on the stack. Adjust the virtual
276 // frame to match this state.
277 frame_->Adjust(4);
278 allocator_->Unuse(r1);
279 allocator_->Unuse(lr);
280 }
281
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000282 // Initialize the function return target after the locals are set
283 // up, because it needs the expected frame height from the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000284 function_return_.set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000285 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000287 // Generate code to 'execute' declarations and initialize functions
288 // (source elements). In case of an illegal redeclaration we need to
289 // handle that instead of processing the declarations.
ager@chromium.org5c838252010-02-19 08:53:10 +0000290 if (scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000291 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000292 scope()->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000293 } else {
294 Comment cmnt(masm_, "[ declarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000295 ProcessDeclarations(scope()->declarations());
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000296 // Bail out if a stack-overflow exception occurred when processing
297 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000298 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000299 }
300
mads.s.ager31e71382008-08-13 09:32:07 +0000301 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000302 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000303 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000304 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305
306 // Compile the body of the function in a vanilla state. Don't
307 // bother compiling all the code if the scope has an illegal
308 // redeclaration.
ager@chromium.org5c838252010-02-19 08:53:10 +0000309 if (!scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000310 Comment cmnt(masm_, "[ function body");
311#ifdef DEBUG
312 bool is_builtin = Bootstrapper::IsActive();
313 bool should_trace =
314 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000315 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000316 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000317 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000318 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000319#endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000320 VisitStatementsAndSpill(info->function()->body());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000322 }
323
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000324 // Generate the return sequence if necessary.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000325 if (has_valid_frame() || function_return_.is_linked()) {
326 if (!function_return_.is_linked()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000327 CodeForReturnPosition(info->function());
ager@chromium.org4af710e2009-09-15 12:20:11 +0000328 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000329 // exit
330 // r0: result
331 // sp: stack pointer
332 // fp: frame pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000333 // cp: callee's context
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000334 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +0000335
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000336 function_return_.Bind();
337 if (FLAG_trace) {
338 // Push the return value on the stack as the parameter.
339 // Runtime::TraceExit returns the parameter as it is.
340 frame_->EmitPush(r0);
341 frame_->CallRuntime(Runtime::kTraceExit, 1);
342 }
343
ager@chromium.org4af710e2009-09-15 12:20:11 +0000344 // Add a label for checking the size of the code used for returning.
345 Label check_exit_codesize;
346 masm_->bind(&check_exit_codesize);
347
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000348 // Calculate the exact length of the return sequence and make sure that
349 // the constant pool is not emitted inside of the return sequence.
ager@chromium.org5c838252010-02-19 08:53:10 +0000350 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000351 int return_sequence_length = Assembler::kJSReturnSequenceLength;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000352 if (!masm_->ImmediateFitsAddrMode1Instruction(sp_delta)) {
353 // Additional mov instruction generated.
354 return_sequence_length++;
355 }
356 masm_->BlockConstPoolFor(return_sequence_length);
357
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000358 // Tear down the frame which will restore the caller's frame pointer and
359 // the link register.
360 frame_->Exit();
361
ager@chromium.org4af710e2009-09-15 12:20:11 +0000362 // Here we use masm_-> instead of the __ macro to avoid the code coverage
363 // tool from instrumenting as we rely on the code size here.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000364 masm_->add(sp, sp, Operand(sp_delta));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000365 masm_->Jump(lr);
366
367 // Check that the size of the code used for returning matches what is
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000368 // expected by the debugger. The add instruction above is an addressing
369 // mode 1 instruction where there are restrictions on which immediate values
370 // can be encoded in the instruction and which immediate values requires
371 // use of an additional instruction for moving the immediate to a temporary
372 // register.
373 ASSERT_EQ(return_sequence_length,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000374 masm_->InstructionsGeneratedSince(&check_exit_codesize));
mads.s.ager31e71382008-08-13 09:32:07 +0000375 }
376
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000377 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000378 ASSERT(!has_cc());
379 ASSERT(state_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000380 ASSERT(!function_return_is_shadowed_);
381 function_return_.Unuse();
382 DeleteFrame();
383
384 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000385 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000386 ProcessDeferred();
387 }
388
389 allocator_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000390}
391
392
ager@chromium.org7c537e22008-10-16 08:43:32 +0000393MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
394 // Currently, this assertion will fail if we try to assign to
395 // a constant variable that is constant because it is read-only
396 // (such as the variable referring to a named function expression).
397 // We need to implement assignments to read-only variables.
398 // Ideally, we should do this during AST generation (by converting
399 // such assignments into expression statements); however, in general
400 // we may not be able to make the decision until past AST generation,
401 // that is when the entire program is known.
402 ASSERT(slot != NULL);
403 int index = slot->index();
404 switch (slot->type()) {
405 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000406 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000407
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000408 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000409 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000410
411 case Slot::CONTEXT: {
412 // Follow the context chain if necessary.
413 ASSERT(!tmp.is(cp)); // do not overwrite context register
414 Register context = cp;
415 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000416 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000417 // Load the closure.
418 // (All contexts, even 'with' contexts, have a closure,
419 // and it is the same for all contexts inside a function.
420 // There is no need to go to the function context first.)
421 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
422 // Load the function context (which is the incoming, outer context).
423 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
424 context = tmp;
425 }
426 // We may have a 'with' context now. Get the function context.
427 // (In fact this mov may never be the needed, since the scope analysis
428 // may not permit a direct context access in this case and thus we are
429 // always at a function context. However it is safe to dereference be-
430 // cause the function context of a function context is itself. Before
431 // deleting this mov we should try to create a counter-example first,
432 // though...)
433 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
434 return ContextOperand(tmp, index);
435 }
436
437 default:
438 UNREACHABLE();
439 return MemOperand(r0, 0);
440 }
441}
442
443
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000444MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
445 Slot* slot,
446 Register tmp,
447 Register tmp2,
448 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000449 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000450 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000451
ager@chromium.org381abbb2009-02-25 13:23:22 +0000452 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
453 if (s->num_heap_slots() > 0) {
454 if (s->calls_eval()) {
455 // Check that extension is NULL.
456 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
457 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000458 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000459 }
460 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
461 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
462 context = tmp;
463 }
464 }
465 // Check that last extension is NULL.
466 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
467 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000468 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000469 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000470 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000471}
472
473
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000474// Loads a value on TOS. If it is a boolean value, the result may have been
475// (partially) translated into branches, or it may have set the condition
476// code register. If force_cc is set, the value is forced to set the
477// condition code register and no value is pushed. If the condition code
478// register was set, has_cc() is true and cc_reg_ contains the condition to
479// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000480void CodeGenerator::LoadCondition(Expression* x,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000481 JumpTarget* true_target,
482 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000483 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000484 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000485 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000486
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000487 { CodeGenState new_state(this, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000488 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000489
490 // If we hit a stack overflow, we may not have actually visited
491 // the expression. In that case, we ensure that we have a
492 // valid-looking frame state because we will continue to generate
493 // code as we unwind the C++ stack.
494 //
495 // It's possible to have both a stack overflow and a valid frame
496 // state (eg, a subexpression overflowed, visiting it returned
497 // with a dummied frame state, and visiting this expression
498 // returned with a normal-looking state).
499 if (HasStackOverflow() &&
500 has_valid_frame() &&
501 !has_cc() &&
502 frame_->height() == original_height) {
503 true_target->Jump();
504 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000505 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000506 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000507 // Convert the TOS value to a boolean in the condition code register.
508 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000509 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000510 ASSERT(!force_cc || !has_valid_frame() || has_cc());
511 ASSERT(!has_valid_frame() ||
512 (has_cc() && frame_->height() == original_height) ||
513 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514}
515
516
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000517void CodeGenerator::Load(Expression* expr) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000518#ifdef DEBUG
519 int original_height = frame_->height();
520#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000521 JumpTarget true_target;
522 JumpTarget false_target;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000523 LoadCondition(expr, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000524
525 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000526 // Convert cc_reg_ into a boolean value.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000527 JumpTarget loaded;
528 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000529 materialize_true.Branch(cc_reg_);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000530 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000531 frame_->EmitPush(r0);
532 loaded.Jump();
533 materialize_true.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000534 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000535 frame_->EmitPush(r0);
536 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000537 cc_reg_ = al;
538 }
539
540 if (true_target.is_linked() || false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000541 // We have at least one condition value that has been "translated"
542 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000543 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000544 if (frame_ != NULL) {
545 loaded.Jump(); // Don't lose the current TOS.
546 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000547 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000548 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000549 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000550 true_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000551 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000552 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000553 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000554 // If both "true" and "false" need to be loaded jump across the code for
555 // "false".
556 if (both) {
557 loaded.Jump();
558 }
559 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000560 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000561 false_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000562 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000563 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000564 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000565 // A value is loaded on all paths reaching this point.
566 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000567 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000568 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000569 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000570 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000571}
572
573
ager@chromium.org7c537e22008-10-16 08:43:32 +0000574void CodeGenerator::LoadGlobal() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000575 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000576 __ ldr(r0, GlobalObject());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000577 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000578}
579
580
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000581void CodeGenerator::LoadGlobalReceiver(Register scratch) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000582 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000583 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
584 __ ldr(scratch,
585 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000586 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000587}
588
589
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000590void CodeGenerator::LoadTypeofExpression(Expression* expr) {
591 // Special handling of identifiers as subexpressions of typeof.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000592 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000593 Variable* variable = expr->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594 if (variable != NULL && !variable->is_this() && variable->is_global()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000595 // For a global variable we build the property reference
596 // <global>.<variable> and perform a (regular non-contextual) property
597 // load to make sure we do not get reference errors.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000598 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
599 Literal key(variable->name());
ager@chromium.org236ad962008-09-25 09:45:57 +0000600 Property property(&global, &key, RelocInfo::kNoPosition);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000601 Reference ref(this, &property);
602 ref.GetValueAndSpill();
603 } else if (variable != NULL && variable->slot() != NULL) {
604 // For a variable that rewrites to a slot, we signal it is the immediate
605 // subexpression of a typeof.
606 LoadFromSlot(variable->slot(), INSIDE_TYPEOF);
607 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608 } else {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000609 // Anything else can be handled normally.
610 LoadAndSpill(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611 }
612}
613
614
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000615Reference::Reference(CodeGenerator* cgen,
616 Expression* expression,
617 bool persist_after_get)
618 : cgen_(cgen),
619 expression_(expression),
620 type_(ILLEGAL),
621 persist_after_get_(persist_after_get) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622 cgen->LoadReference(this);
623}
624
625
626Reference::~Reference() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000627 ASSERT(is_unloaded() || is_illegal());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000628}
629
630
ager@chromium.org7c537e22008-10-16 08:43:32 +0000631void CodeGenerator::LoadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000632 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000633 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000634 Expression* e = ref->expression();
635 Property* property = e->AsProperty();
636 Variable* var = e->AsVariableProxy()->AsVariable();
637
638 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000639 // The expression is either a property or a variable proxy that rewrites
640 // to a property.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000641 LoadAndSpill(property->obj());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000642 if (property->key()->IsPropertyName()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000643 ref->set_type(Reference::NAMED);
644 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000645 LoadAndSpill(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000646 ref->set_type(Reference::KEYED);
647 }
648 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000649 // The expression is a variable proxy that does not rewrite to a
650 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000651 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652 LoadGlobal();
653 ref->set_type(Reference::NAMED);
654 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000655 ASSERT(var->slot() != NULL);
656 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000657 }
658 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000659 // Anything else is a runtime error.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000660 LoadAndSpill(e);
661 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662 }
663}
664
665
ager@chromium.org7c537e22008-10-16 08:43:32 +0000666void CodeGenerator::UnloadReference(Reference* ref) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000667 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000668 // Pop a reference from the stack while preserving TOS.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000669 Comment cmnt(masm_, "[ UnloadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000670 int size = ref->size();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000671 if (size > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000672 frame_->EmitPop(r0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000673 frame_->Drop(size);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000674 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000675 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000676 ref->set_unloaded();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000677}
678
679
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000680// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
681// register to a boolean in the condition code register. The code
682// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000683void CodeGenerator::ToBoolean(JumpTarget* true_target,
684 JumpTarget* false_target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000685 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000686 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000687 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000688 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689
690 // Fast case checks
691
mads.s.ager31e71382008-08-13 09:32:07 +0000692 // Check if the value is 'false'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000693 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
694 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000695 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000696
mads.s.ager31e71382008-08-13 09:32:07 +0000697 // Check if the value is 'true'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000698 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
699 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000700 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000701
mads.s.ager31e71382008-08-13 09:32:07 +0000702 // Check if the value is 'undefined'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000703 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
704 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000705 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000706
mads.s.ager31e71382008-08-13 09:32:07 +0000707 // Check if the value is a smi.
708 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000709 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000710 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000711 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000712
713 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000714 frame_->EmitPush(r0);
715 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000716 // Convert the result (r0) to a condition code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000717 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
718 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000719
720 cc_reg_ = ne;
721}
722
723
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000724void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000725 OverwriteMode overwrite_mode,
726 int constant_rhs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000727 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +0000728 // sp[0] : y
729 // sp[1] : x
730 // result : r0
731
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732 // Stub is entered with a call: 'return address' is in lr.
733 switch (op) {
734 case Token::ADD: // fall through.
735 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000736 case Token::MUL:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000737 case Token::DIV:
738 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000739 case Token::BIT_OR:
740 case Token::BIT_AND:
741 case Token::BIT_XOR:
742 case Token::SHL:
743 case Token::SHR:
744 case Token::SAR: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000745 frame_->EmitPop(r0); // r0 : y
746 frame_->EmitPop(r1); // r1 : x
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000747 GenericBinaryOpStub stub(op, overwrite_mode, constant_rhs);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000748 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000749 break;
750 }
751
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000752 case Token::COMMA:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000753 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000754 // simply discard left value
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000755 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000756 break;
757
758 default:
759 // Other cases should have been handled before this point.
760 UNREACHABLE();
761 break;
762 }
763}
764
765
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000766class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000767 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000768 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000769 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000770 bool reversed,
771 OverwriteMode overwrite_mode)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000772 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000773 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000774 reversed_(reversed),
775 overwrite_mode_(overwrite_mode) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000776 set_comment("[ DeferredInlinedSmiOperation");
777 }
778
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000779 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000780
781 private:
782 Token::Value op_;
783 int value_;
784 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000785 OverwriteMode overwrite_mode_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000786};
787
788
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000789void DeferredInlineSmiOperation::Generate() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000790 switch (op_) {
791 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000792 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000793 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000794 __ sub(r0, r0, Operand(Smi::FromInt(value_)));
795 __ mov(r1, Operand(Smi::FromInt(value_)));
796 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000797 __ sub(r1, r0, Operand(Smi::FromInt(value_)));
798 __ mov(r0, Operand(Smi::FromInt(value_)));
799 }
800 break;
801 }
802
803 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000804 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000805 if (reversed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000806 __ rsb(r0, r0, Operand(Smi::FromInt(value_)));
807 __ mov(r1, Operand(Smi::FromInt(value_)));
808 } else {
809 __ add(r1, r0, Operand(Smi::FromInt(value_)));
810 __ mov(r0, Operand(Smi::FromInt(value_)));
811 }
812 break;
813 }
814
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000815 // For these operations there is no optimistic operation that needs to be
816 // reverted.
817 case Token::MUL:
818 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000819 case Token::BIT_OR:
820 case Token::BIT_XOR:
821 case Token::BIT_AND: {
822 if (reversed_) {
823 __ mov(r1, Operand(Smi::FromInt(value_)));
824 } else {
825 __ mov(r1, Operand(r0));
826 __ mov(r0, Operand(Smi::FromInt(value_)));
827 }
828 break;
829 }
830
831 case Token::SHL:
832 case Token::SHR:
833 case Token::SAR: {
834 if (!reversed_) {
835 __ mov(r1, Operand(r0));
836 __ mov(r0, Operand(Smi::FromInt(value_)));
837 } else {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000838 UNREACHABLE(); // Should have been handled in SmiOperation.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000839 }
840 break;
841 }
842
843 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +0000844 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000845 UNREACHABLE();
846 break;
847 }
848
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000849 GenericBinaryOpStub stub(op_, overwrite_mode_, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000850 __ CallStub(&stub);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000851}
852
853
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000854static bool PopCountLessThanEqual2(unsigned int x) {
855 x &= x - 1;
856 return (x & (x - 1)) == 0;
857}
858
859
860// Returns the index of the lowest bit set.
861static int BitPosition(unsigned x) {
862 int bit_posn = 0;
863 while ((x & 0xf) == 0) {
864 bit_posn += 4;
865 x >>= 4;
866 }
867 while ((x & 1) == 0) {
868 bit_posn++;
869 x >>= 1;
870 }
871 return bit_posn;
872}
873
874
ager@chromium.org7c537e22008-10-16 08:43:32 +0000875void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000876 Handle<Object> value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000877 bool reversed,
878 OverwriteMode mode) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000879 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000880 // NOTE: This is an attempt to inline (a bit) more of the code for
881 // some possible smi operations (like + and -) when (at least) one
882 // of the operands is a literal smi. With this optimization, the
883 // performance of the system is increased by ~15%, and the generated
884 // code size is increased by ~1% (measured on a combination of
885 // different benchmarks).
886
mads.s.ager31e71382008-08-13 09:32:07 +0000887 // sp[0] : operand
888
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000889 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000890
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000891 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000892 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000894 bool something_to_inline = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895 switch (op) {
896 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000897 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000898 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000900 __ add(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000901 deferred->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000902 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000903 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000904 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905 break;
906 }
907
908 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000909 DeferredCode* deferred =
ager@chromium.orge2902be2009-06-08 12:21:35 +0000910 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000911
ager@chromium.orge2902be2009-06-08 12:21:35 +0000912 if (reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000913 __ rsb(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000914 } else {
915 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000916 }
ager@chromium.orge2902be2009-06-08 12:21:35 +0000917 deferred->Branch(vs);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000918 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000919 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000920 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000921 break;
922 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000923
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000924
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000925 case Token::BIT_OR:
926 case Token::BIT_XOR:
927 case Token::BIT_AND: {
928 DeferredCode* deferred =
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000929 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000930 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +0000931 deferred->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000932 switch (op) {
933 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
934 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
935 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
936 default: UNREACHABLE();
937 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000938 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000939 break;
940 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000941
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000942 case Token::SHL:
943 case Token::SHR:
944 case Token::SAR: {
945 if (reversed) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000946 something_to_inline = false;
947 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000948 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000949 int shift_value = int_value & 0x1f; // least significant 5 bits
950 DeferredCode* deferred =
951 new DeferredInlineSmiOperation(op, shift_value, false, mode);
952 __ tst(r0, Operand(kSmiTagMask));
953 deferred->Branch(ne);
954 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
955 switch (op) {
956 case Token::SHL: {
957 if (shift_value != 0) {
958 __ mov(r2, Operand(r2, LSL, shift_value));
959 }
960 // check that the *unsigned* result fits in a smi
961 __ add(r3, r2, Operand(0x40000000), SetCC);
962 deferred->Branch(mi);
963 break;
964 }
965 case Token::SHR: {
966 // LSR by immediate 0 means shifting 32 bits.
967 if (shift_value != 0) {
968 __ mov(r2, Operand(r2, LSR, shift_value));
969 }
970 // check that the *unsigned* result fits in a smi
971 // neither of the two high-order bits can be set:
972 // - 0x80000000: high bit would be lost when smi tagging
973 // - 0x40000000: this number would convert to negative when
974 // smi tagging these two cases can only happen with shifts
975 // by 0 or 1 when handed a valid smi
976 __ and_(r3, r2, Operand(0xc0000000), SetCC);
977 deferred->Branch(ne);
978 break;
979 }
980 case Token::SAR: {
981 if (shift_value != 0) {
982 // ASR by immediate 0 means shifting 32 bits.
983 __ mov(r2, Operand(r2, ASR, shift_value));
984 }
985 break;
986 }
987 default: UNREACHABLE();
988 }
989 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
990 deferred->BindExit();
991 break;
992 }
993
994 case Token::MOD: {
995 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
996 something_to_inline = false;
997 break;
998 }
999 DeferredCode* deferred =
1000 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
1001 unsigned mask = (0x80000000u | kSmiTagMask);
1002 __ tst(r0, Operand(mask));
1003 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1004 mask = (int_value << kSmiTagSize) - 1;
1005 __ and_(r0, r0, Operand(mask));
1006 deferred->BindExit();
1007 break;
1008 }
1009
1010 case Token::MUL: {
1011 if (!IsEasyToMultiplyBy(int_value)) {
1012 something_to_inline = false;
1013 break;
1014 }
1015 DeferredCode* deferred =
1016 new DeferredInlineSmiOperation(op, int_value, reversed, mode);
1017 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1018 max_smi_that_wont_overflow <<= kSmiTagSize;
1019 unsigned mask = 0x80000000u;
1020 while ((mask & max_smi_that_wont_overflow) == 0) {
1021 mask |= mask >> 1;
1022 }
1023 mask |= kSmiTagMask;
1024 // This does a single mask that checks for a too high value in a
1025 // conservative way and for a non-Smi. It also filters out negative
1026 // numbers, unfortunately, but since this code is inline we prefer
1027 // brevity to comprehensiveness.
1028 __ tst(r0, Operand(mask));
1029 deferred->Branch(ne);
1030 MultiplyByKnownInt(masm_, r0, r0, int_value);
1031 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001032 break;
1033 }
1034
1035 default:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001036 something_to_inline = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001037 break;
1038 }
1039
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001040 if (!something_to_inline) {
1041 if (!reversed) {
1042 frame_->EmitPush(r0);
1043 __ mov(r0, Operand(value));
1044 frame_->EmitPush(r0);
1045 GenericBinaryOperation(op, mode, int_value);
1046 } else {
1047 __ mov(ip, Operand(value));
1048 frame_->EmitPush(ip);
1049 frame_->EmitPush(r0);
1050 GenericBinaryOperation(op, mode, kUnknownIntValue);
1051 }
1052 }
1053
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001054 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001055}
1056
1057
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001058void CodeGenerator::Comparison(Condition cc,
1059 Expression* left,
1060 Expression* right,
1061 bool strict) {
1062 if (left != NULL) LoadAndSpill(left);
1063 if (right != NULL) LoadAndSpill(right);
1064
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001065 VirtualFrame::SpilledScope spilled_scope;
mads.s.ager31e71382008-08-13 09:32:07 +00001066 // sp[0] : y
1067 // sp[1] : x
1068 // result : cc register
1069
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001070 // Strict only makes sense for equality comparisons.
1071 ASSERT(!strict || cc == eq);
1072
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001073 JumpTarget exit;
1074 JumpTarget smi;
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001075 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1076 if (cc == gt || cc == le) {
1077 cc = ReverseCondition(cc);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001078 frame_->EmitPop(r1);
1079 frame_->EmitPop(r0);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001080 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001081 frame_->EmitPop(r0);
1082 frame_->EmitPop(r1);
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001083 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001084 __ orr(r2, r0, Operand(r1));
1085 __ tst(r2, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001086 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001088 // Perform non-smi comparison by stub.
1089 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1090 // We call with 0 args because there are 0 on the stack.
1091 CompareStub stub(cc, strict);
1092 frame_->CallStub(&stub, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001093 __ cmp(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001094 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001095
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001096 // Do smi comparisons by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001097 smi.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098 __ cmp(r1, Operand(r0));
1099
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001100 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101 cc_reg_ = cc;
1102}
1103
1104
mads.s.ager31e71382008-08-13 09:32:07 +00001105// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001106void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001107 CallFunctionFlags flags,
1108 int position) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001109 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001110 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001111 int arg_count = args->length();
1112 for (int i = 0; i < arg_count; i++) {
1113 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001114 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001115
kasper.lund7276f142008-07-30 08:49:36 +00001116 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001117 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001118
kasper.lund7276f142008-07-30 08:49:36 +00001119 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001120 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001121 CallFunctionStub call_function(arg_count, in_loop, flags);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001122 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123
1124 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001125 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001126 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001127}
1128
1129
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001130void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001131 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001132 ASSERT(has_cc());
1133 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001134 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001135 cc_reg_ = al;
1136}
1137
1138
ager@chromium.org7c537e22008-10-16 08:43:32 +00001139void CodeGenerator::CheckStack() {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001140 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3811b432009-10-28 14:53:37 +00001141 Comment cmnt(masm_, "[ check stack");
1142 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1143 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1144 // the implicit 8 byte offset that always applies to operations with pc and
1145 // gives a return address 12 bytes down.
1146 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1147 masm_->cmp(sp, Operand(ip));
1148 StackCheckStub stub;
1149 // Call the stub if lower.
1150 masm_->mov(pc,
1151 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1152 RelocInfo::CODE_TARGET),
1153 LeaveCC,
1154 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001155}
1156
1157
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001158void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1159#ifdef DEBUG
1160 int original_height = frame_->height();
1161#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001162 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001163 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1164 VisitAndSpill(statements->at(i));
1165 }
1166 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1167}
1168
1169
ager@chromium.org7c537e22008-10-16 08:43:32 +00001170void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001171#ifdef DEBUG
1172 int original_height = frame_->height();
1173#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001174 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001175 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001176 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001177 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001178 VisitStatementsAndSpill(node->statements());
1179 if (node->break_target()->is_linked()) {
1180 node->break_target()->Bind();
1181 }
1182 node->break_target()->Unuse();
1183 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001184}
1185
1186
ager@chromium.org7c537e22008-10-16 08:43:32 +00001187void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001188 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3811b432009-10-28 14:53:37 +00001189 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001190 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001191 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001192 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001193 frame_->EmitPush(r0);
1194 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001195 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001196}
1197
1198
ager@chromium.org7c537e22008-10-16 08:43:32 +00001199void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001200#ifdef DEBUG
1201 int original_height = frame_->height();
1202#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001203 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001204 Comment cmnt(masm_, "[ Declaration");
1205 Variable* var = node->proxy()->var();
1206 ASSERT(var != NULL); // must have been resolved
1207 Slot* slot = var->slot();
1208
1209 // If it was not possible to allocate the variable at compile time,
1210 // we need to "declare" it at runtime to make sure it actually
1211 // exists in the local context.
1212 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1213 // Variables with a "LOOKUP" slot were introduced as non-locals
1214 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001215 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001216 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001217 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001218 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001219 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001220 // Declaration nodes are always declared in only two modes.
1221 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1222 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001223 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001224 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001225 // Push initial value, if any.
1226 // Note: For variables we must not push an initial value (such as
1227 // 'undefined') because we may have a (legal) redeclaration and we
1228 // must not destroy the current value.
1229 if (node->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001230 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001231 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001232 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001233 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001234 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001235 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001236 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001237 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001238 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001239 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001240 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001241 return;
1242 }
1243
1244 ASSERT(!var->is_global());
1245
1246 // If we have a function or a constant, we need to initialize the variable.
1247 Expression* val = NULL;
1248 if (node->mode() == Variable::CONST) {
1249 val = new Literal(Factory::the_hole_value());
1250 } else {
1251 val = node->fun(); // NULL if we don't have a function
1252 }
1253
1254 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001255 {
1256 // Set initial value.
1257 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001258 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001259 target.SetValue(NOT_CONST_INIT);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001260 }
1261 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001262 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001263 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001264 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001265}
1266
1267
ager@chromium.org7c537e22008-10-16 08:43:32 +00001268void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001269#ifdef DEBUG
1270 int original_height = frame_->height();
1271#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001272 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001273 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001274 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275 Expression* expression = node->expression();
1276 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001277 LoadAndSpill(expression);
1278 frame_->Drop();
1279 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001280}
1281
1282
ager@chromium.org7c537e22008-10-16 08:43:32 +00001283void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001284#ifdef DEBUG
1285 int original_height = frame_->height();
1286#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001287 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001288 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001289 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001290 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001291 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292}
1293
1294
ager@chromium.org7c537e22008-10-16 08:43:32 +00001295void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001296#ifdef DEBUG
1297 int original_height = frame_->height();
1298#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001299 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001300 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001301 // Generate different code depending on which parts of the if statement
1302 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303 bool has_then_stm = node->HasThenStatement();
1304 bool has_else_stm = node->HasElseStatement();
1305
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001306 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001308 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001309 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001310 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001311 JumpTarget then;
1312 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001314 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001315 if (frame_ != NULL) {
1316 Branch(false, &else_);
1317 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001319 if (frame_ != NULL || then.is_linked()) {
1320 then.Bind();
1321 VisitAndSpill(node->then_statement());
1322 }
1323 if (frame_ != NULL) {
1324 exit.Jump();
1325 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001326 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001327 if (else_.is_linked()) {
1328 else_.Bind();
1329 VisitAndSpill(node->else_statement());
1330 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001331
1332 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001333 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001334 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001335 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001336 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001337 LoadConditionAndSpill(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001338 if (frame_ != NULL) {
1339 Branch(false, &exit);
1340 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001341 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001342 if (frame_ != NULL || then.is_linked()) {
1343 then.Bind();
1344 VisitAndSpill(node->then_statement());
1345 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001346
1347 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001348 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001349 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001350 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351 // if (!cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001352 LoadConditionAndSpill(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001353 if (frame_ != NULL) {
1354 Branch(true, &exit);
1355 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001356 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001357 if (frame_ != NULL || else_.is_linked()) {
1358 else_.Bind();
1359 VisitAndSpill(node->else_statement());
1360 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001361
1362 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001363 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001364 ASSERT(!has_then_stm && !has_else_stm);
1365 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001366 LoadConditionAndSpill(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001367 if (frame_ != NULL) {
1368 if (has_cc()) {
1369 cc_reg_ = al;
1370 } else {
1371 frame_->Drop();
1372 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001373 }
1374 }
1375
1376 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001377 if (exit.is_linked()) {
1378 exit.Bind();
1379 }
1380 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001381}
1382
1383
ager@chromium.org7c537e22008-10-16 08:43:32 +00001384void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001385 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001386 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001387 CodeForStatementPosition(node);
1388 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001389}
1390
1391
ager@chromium.org7c537e22008-10-16 08:43:32 +00001392void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001393 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001395 CodeForStatementPosition(node);
1396 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001397}
1398
1399
ager@chromium.org7c537e22008-10-16 08:43:32 +00001400void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001401 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001402 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001403
ager@chromium.org4af710e2009-09-15 12:20:11 +00001404 CodeForStatementPosition(node);
1405 LoadAndSpill(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001406 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001407 frame_->EmitPop(r0);
1408 function_return_.Jump();
1409 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001410 // Pop the result from the frame and prepare the frame for
1411 // returning thus making it easier to merge.
1412 frame_->EmitPop(r0);
1413 frame_->PrepareForReturn();
1414
1415 function_return_.Jump();
1416 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417}
1418
1419
ager@chromium.org7c537e22008-10-16 08:43:32 +00001420void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001421#ifdef DEBUG
1422 int original_height = frame_->height();
1423#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001424 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001425 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001426 CodeForStatementPosition(node);
1427 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001428 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001429 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001430 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001431 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001432 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001433#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001434 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001435 __ cmp(r0, Operand(cp));
1436 verified_true.Branch(eq);
1437 __ stop("PushContext: r0 is expected to be the same as cp");
1438 verified_true.Bind();
1439#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001440 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001441 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001442 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443}
1444
1445
ager@chromium.org7c537e22008-10-16 08:43:32 +00001446void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001447#ifdef DEBUG
1448 int original_height = frame_->height();
1449#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001450 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001451 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001452 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001453 // Pop context.
1454 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1455 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001456 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001457 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001458}
1459
1460
ager@chromium.org7c537e22008-10-16 08:43:32 +00001461void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001462#ifdef DEBUG
1463 int original_height = frame_->height();
1464#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001465 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001466 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001467 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001468 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001469
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001470 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001471
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001472 JumpTarget next_test;
1473 JumpTarget fall_through;
1474 JumpTarget default_entry;
1475 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001476 ZoneList<CaseClause*>* cases = node->cases();
1477 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001478 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479
1480 for (int i = 0; i < length; i++) {
1481 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001483 // Remember the default clause and compile it at the end.
1484 default_clause = clause;
1485 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001486 }
1487
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001488 Comment cmnt(masm_, "[ Case clause");
1489 // Compile the test.
1490 next_test.Bind();
1491 next_test.Unuse();
1492 // Duplicate TOS.
1493 __ ldr(r0, frame_->Top());
1494 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001495 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001496 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001497
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001498 // Before entering the body from the test, remove the switch value from
1499 // the stack.
1500 frame_->Drop();
1501
1502 // Label the body so that fall through is enabled.
1503 if (i > 0 && cases->at(i - 1)->is_default()) {
1504 default_exit.Bind();
1505 } else {
1506 fall_through.Bind();
1507 fall_through.Unuse();
1508 }
1509 VisitStatementsAndSpill(clause->statements());
1510
1511 // If control flow can fall through from the body, jump to the next body
1512 // or the end of the statement.
1513 if (frame_ != NULL) {
1514 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1515 default_entry.Jump();
1516 } else {
1517 fall_through.Jump();
1518 }
1519 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001520 }
1521
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001522 // The final "test" removes the switch value.
1523 next_test.Bind();
1524 frame_->Drop();
1525
1526 // If there is a default clause, compile it.
1527 if (default_clause != NULL) {
1528 Comment cmnt(masm_, "[ Default clause");
1529 default_entry.Bind();
1530 VisitStatementsAndSpill(default_clause->statements());
1531 // If control flow can fall out of the default and there is a case after
1532 // it, jup to that case's body.
1533 if (frame_ != NULL && default_exit.is_bound()) {
1534 default_exit.Jump();
1535 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001536 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001537
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001538 if (fall_through.is_linked()) {
1539 fall_through.Bind();
1540 }
1541
1542 if (node->break_target()->is_linked()) {
1543 node->break_target()->Bind();
1544 }
1545 node->break_target()->Unuse();
1546 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001547}
1548
1549
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001550void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001551#ifdef DEBUG
1552 int original_height = frame_->height();
1553#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001554 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001555 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001556 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001557 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001558 JumpTarget body(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001559
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001560 // Label the top of the loop for the backward CFG edge. If the test
1561 // is always true we can use the continue target, and if the test is
1562 // always false there is no need.
1563 ConditionAnalysis info = AnalyzeCondition(node->cond());
1564 switch (info) {
1565 case ALWAYS_TRUE:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001566 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001567 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001568 break;
1569 case ALWAYS_FALSE:
1570 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1571 break;
1572 case DONT_KNOW:
1573 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1574 body.Bind();
1575 break;
1576 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001577
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001578 CheckStack(); // TODO(1222600): ignore if body contains calls.
1579 VisitAndSpill(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001580
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001581 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001582 switch (info) {
1583 case ALWAYS_TRUE:
1584 // If control can fall off the end of the body, jump back to the
1585 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001586 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001587 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001588 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001590 case ALWAYS_FALSE:
1591 // If we have a continue in the body, we only have to bind its
1592 // jump target.
1593 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001594 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001595 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001596 break;
1597 case DONT_KNOW:
1598 // We have to compile the test expression if it can be reached by
1599 // control flow falling out of the body or via continue.
1600 if (node->continue_target()->is_linked()) {
1601 node->continue_target()->Bind();
1602 }
1603 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001604 Comment cmnt(masm_, "[ DoWhileCondition");
1605 CodeForDoWhileConditionPosition(node);
1606 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001607 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001608 // A invalid frame here indicates that control did not
1609 // fall out of the test expression.
1610 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001611 }
1612 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001613 break;
1614 }
1615
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001616 if (node->break_target()->is_linked()) {
1617 node->break_target()->Bind();
1618 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001619 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1620}
1621
1622
1623void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
1624#ifdef DEBUG
1625 int original_height = frame_->height();
1626#endif
1627 VirtualFrame::SpilledScope spilled_scope;
1628 Comment cmnt(masm_, "[ WhileStatement");
1629 CodeForStatementPosition(node);
1630
1631 // If the test is never true and has no side effects there is no need
1632 // to compile the test or body.
1633 ConditionAnalysis info = AnalyzeCondition(node->cond());
1634 if (info == ALWAYS_FALSE) return;
1635
1636 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1637
1638 // Label the top of the loop with the continue target for the backward
1639 // CFG edge.
1640 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
1641 node->continue_target()->Bind();
1642
1643 if (info == DONT_KNOW) {
1644 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001645 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001646 if (has_valid_frame()) {
1647 // A NULL frame indicates that control did not fall out of the
1648 // test expression.
1649 Branch(false, node->break_target());
1650 }
1651 if (has_valid_frame() || body.is_linked()) {
1652 body.Bind();
1653 }
1654 }
1655
1656 if (has_valid_frame()) {
1657 CheckStack(); // TODO(1222600): ignore if body contains calls.
1658 VisitAndSpill(node->body());
1659
1660 // If control flow can fall out of the body, jump back to the top.
1661 if (has_valid_frame()) {
1662 node->continue_target()->Jump();
1663 }
1664 }
1665 if (node->break_target()->is_linked()) {
1666 node->break_target()->Bind();
1667 }
1668 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1669}
1670
1671
1672void CodeGenerator::VisitForStatement(ForStatement* node) {
1673#ifdef DEBUG
1674 int original_height = frame_->height();
1675#endif
1676 VirtualFrame::SpilledScope spilled_scope;
1677 Comment cmnt(masm_, "[ ForStatement");
1678 CodeForStatementPosition(node);
1679 if (node->init() != NULL) {
1680 VisitAndSpill(node->init());
1681 }
1682
1683 // If the test is never true there is no need to compile the test or
1684 // body.
1685 ConditionAnalysis info = AnalyzeCondition(node->cond());
1686 if (info == ALWAYS_FALSE) return;
1687
1688 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1689
1690 // If there is no update statement, label the top of the loop with the
1691 // continue target, otherwise with the loop target.
1692 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
1693 if (node->next() == NULL) {
1694 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
1695 node->continue_target()->Bind();
1696 } else {
1697 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1698 loop.Bind();
1699 }
1700
1701 // If the test is always true, there is no need to compile it.
1702 if (info == DONT_KNOW) {
1703 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001704 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001705 if (has_valid_frame()) {
1706 Branch(false, node->break_target());
1707 }
1708 if (has_valid_frame() || body.is_linked()) {
1709 body.Bind();
1710 }
1711 }
1712
1713 if (has_valid_frame()) {
1714 CheckStack(); // TODO(1222600): ignore if body contains calls.
1715 VisitAndSpill(node->body());
1716
1717 if (node->next() == NULL) {
1718 // If there is no update statement and control flow can fall out
1719 // of the loop, jump directly to the continue label.
1720 if (has_valid_frame()) {
1721 node->continue_target()->Jump();
1722 }
1723 } else {
1724 // If there is an update statement and control flow can reach it
1725 // via falling out of the body of the loop or continuing, we
1726 // compile the update statement.
1727 if (node->continue_target()->is_linked()) {
1728 node->continue_target()->Bind();
1729 }
1730 if (has_valid_frame()) {
1731 // Record source position of the statement as this code which is
1732 // after the code for the body actually belongs to the loop
1733 // statement and not the body.
1734 CodeForStatementPosition(node);
1735 VisitAndSpill(node->next());
1736 loop.Jump();
1737 }
1738 }
1739 }
1740 if (node->break_target()->is_linked()) {
1741 node->break_target()->Bind();
1742 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001743 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001744}
1745
1746
ager@chromium.org7c537e22008-10-16 08:43:32 +00001747void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001748#ifdef DEBUG
1749 int original_height = frame_->height();
1750#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001751 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001752 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001753 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001754
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001755 JumpTarget primitive;
1756 JumpTarget jsobject;
1757 JumpTarget fixed_array;
1758 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
1759 JumpTarget end_del_check;
1760 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001761
1762 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001763 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001764
1765 // Both SpiderMonkey and kjs ignore null and undefined in contrast
1766 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001767 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001768 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1769 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001770 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001771 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1772 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001773 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001774
1775 // Stack layout in body:
1776 // [iteration counter (Smi)]
1777 // [length of array]
1778 // [FixedArray]
1779 // [Map or 0]
1780 // [Object]
1781
1782 // Check if enumerable is already a JSObject
1783 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001784 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001785 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001786 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001787
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001788 primitive.Bind();
1789 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001790 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001791
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001792 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001793 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001794 // r0: value to be iterated over
1795 frame_->EmitPush(r0); // Push the object being iterated over.
1796
1797 // Check cache validity in generated code. This is a fast case for
1798 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
1799 // guarantee cache validity, call the runtime system to check cache
1800 // validity or get the property names in a fixed array.
1801 JumpTarget call_runtime;
1802 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
1803 JumpTarget check_prototype;
1804 JumpTarget use_cache;
1805 __ mov(r1, Operand(r0));
1806 loop.Bind();
1807 // Check that there are no elements.
1808 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
1809 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
1810 __ cmp(r2, r4);
1811 call_runtime.Branch(ne);
1812 // Check that instance descriptors are not empty so that we can
1813 // check for an enum cache. Leave the map in r3 for the subsequent
1814 // prototype load.
1815 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
1816 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
1817 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
1818 __ cmp(r2, ip);
1819 call_runtime.Branch(eq);
1820 // Check that there in an enum cache in the non-empty instance
1821 // descriptors. This is the case if the next enumeration index
1822 // field does not contain a smi.
1823 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
1824 __ tst(r2, Operand(kSmiTagMask));
1825 call_runtime.Branch(eq);
1826 // For all objects but the receiver, check that the cache is empty.
1827 // r4: empty fixed array root.
1828 __ cmp(r1, r0);
1829 check_prototype.Branch(eq);
1830 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
1831 __ cmp(r2, r4);
1832 call_runtime.Branch(ne);
1833 check_prototype.Bind();
1834 // Load the prototype from the map and loop if non-null.
1835 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
1836 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1837 __ cmp(r1, ip);
1838 loop.Branch(ne);
1839 // The enum cache is valid. Load the map of the object being
1840 // iterated over and use the cache for the iteration.
1841 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
1842 use_cache.Jump();
1843
1844 call_runtime.Bind();
1845 // Call the runtime to get the property names for the object.
1846 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001847 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001848
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001849 // If we got a map from the runtime call, we can do a fast
1850 // modification check. Otherwise, we got a fixed array, and we have
1851 // to do a slow check.
1852 // r0: map or fixed array (result from call to
1853 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001854 __ mov(r2, Operand(r0));
1855 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001856 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
1857 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001858 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001859
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001860 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001861 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001862 // r0: map (either the result from a call to
1863 // Runtime::kGetPropertyNamesFast or has been fetched directly from
1864 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001865 __ mov(r1, Operand(r0));
1866 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
1867 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
1868 __ ldr(r2,
1869 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
1870
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001871 frame_->EmitPush(r0); // map
1872 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00001873 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001874 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001875 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001876 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001877 frame_->EmitPush(r0);
1878 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001879
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001880 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001881 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001882 frame_->EmitPush(r1); // insert 0 in place of Map
1883 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884
1885 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00001886 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001888 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001889 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001890 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001891
1892 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001893 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00001894 // sp[0] : index
1895 // sp[1] : array/enum cache length
1896 // sp[2] : array or enum cache
1897 // sp[3] : 0 or map
1898 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001899 // Grab the current frame's height for the break and continue
1900 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001901 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
1902 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001903
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001904 __ ldr(r0, frame_->ElementAt(0)); // load the current count
1905 __ ldr(r1, frame_->ElementAt(1)); // load the length
1906 __ cmp(r0, Operand(r1)); // compare to the array length
1907 node->break_target()->Branch(hs);
1908
1909 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00001910
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001911 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001912 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1914 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1915
1916 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001917 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001918 // Check if this (still) matches the map of the enumerable.
1919 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001920 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001921 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
1922 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001923 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924
1925 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001926 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
1927 frame_->EmitPush(r0);
1928 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001929 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001930 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931
1932 // If the property has been removed while iterating, we just skip it.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001933 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1934 __ cmp(r3, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001935 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001937 end_del_check.Bind();
1938 // Store the entry in the 'each' expression and take another spin in the
1939 // loop. r3: i'th entry of the enum cache (or string there of)
1940 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001941 { Reference each(this, node->each());
1942 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00001943 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001944 __ ldr(r0, frame_->ElementAt(each.size()));
1945 frame_->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001946 each.SetValue(NOT_CONST_INIT);
1947 frame_->Drop(2);
1948 } else {
1949 // If the reference was to a slot we rely on the convenient property
1950 // that it doesn't matter whether a value (eg, r3 pushed above) is
1951 // right on top of or right underneath a zero-sized reference.
1952 each.SetValue(NOT_CONST_INIT);
1953 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00001954 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001955 }
1956 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001957 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001958 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001959 VisitAndSpill(node->body());
1960
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001961 // Next. Reestablish a spilled frame in case we are coming here via
1962 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001963 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001964 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001965 frame_->EmitPop(r0);
1966 __ add(r0, r0, Operand(Smi::FromInt(1)));
1967 frame_->EmitPush(r0);
1968 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001969
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00001970 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
1971 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001972 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001973 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001974
1975 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001976 exit.Bind();
1977 node->continue_target()->Unuse();
1978 node->break_target()->Unuse();
1979 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001980}
1981
1982
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001983void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001984#ifdef DEBUG
1985 int original_height = frame_->height();
1986#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001987 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001988 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001989 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001990
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001991 JumpTarget try_block;
1992 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001994 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001995 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001996 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001997
1998 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001999 Variable* catch_var = node->catch_var()->var();
2000 ASSERT(catch_var != NULL && catch_var->slot() != NULL);
2001 StoreToSlot(catch_var->slot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002002
2003 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002004 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002005
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002006 VisitStatementsAndSpill(node->catch_block()->statements());
2007 if (frame_ != NULL) {
2008 exit.Jump();
2009 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010
2011
2012 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002013 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002014
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002015 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2016 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002017
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002018 // Shadow the labels for all escapes from the try block, including
2019 // returns. During shadowing, the original label is hidden as the
2020 // LabelShadow and operations on the original actually affect the
2021 // shadowing label.
2022 //
2023 // We should probably try to unify the escaping labels and the return
2024 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002025 int nof_escapes = node->escaping_targets()->length();
2026 List<ShadowTarget*> shadows(1 + nof_escapes);
2027
2028 // Add the shadow target for the function return.
2029 static const int kReturnShadowIndex = 0;
2030 shadows.Add(new ShadowTarget(&function_return_));
2031 bool function_return_was_shadowed = function_return_is_shadowed_;
2032 function_return_is_shadowed_ = true;
2033 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2034
2035 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002036 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002037 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038 }
2039
2040 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002041 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042
2043 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002044 // After shadowing stops, the original labels are unshadowed and the
2045 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002046 bool has_unlinks = false;
2047 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002049 has_unlinks = has_unlinks || shadows[i]->is_linked();
2050 }
2051 function_return_is_shadowed_ = function_return_was_shadowed;
2052
2053 // Get an external reference to the handler address.
2054 ExternalReference handler_address(Top::k_handler_address);
2055
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002056 // If we can fall off the end of the try block, unlink from try chain.
2057 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002058 // The next handler address is on top of the frame. Unlink from
2059 // the handler list and drop the rest of this handler from the
2060 // frame.
2061 ASSERT(StackHandlerConstants::kNextOffset == 0);
2062 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002063 __ mov(r3, Operand(handler_address));
2064 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002065 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002066 if (has_unlinks) {
2067 exit.Jump();
2068 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002069 }
2070
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002071 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002072 // jumped to. Deallocate each shadow target.
2073 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002074 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002075 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002076 shadows[i]->Bind();
2077 // Because we can be jumping here (to spilled code) from unspilled
2078 // code, we need to reestablish a spilled frame at this block.
2079 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002080
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081 // Reload sp from the top handler, because some statements that we
2082 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002083 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002084 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002085 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002087 ASSERT(StackHandlerConstants::kNextOffset == 0);
2088 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002089 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002090 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002092 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2093 frame_->PrepareForReturn();
2094 }
2095 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002096 }
2097 }
2098
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002099 exit.Bind();
2100 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101}
2102
2103
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002104void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002105#ifdef DEBUG
2106 int original_height = frame_->height();
2107#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002108 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002109 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002110 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002111
2112 // State: Used to keep track of reason for entering the finally
2113 // block. Should probably be extended to hold information for
2114 // break/continue from within the try block.
2115 enum { FALLING, THROWING, JUMPING };
2116
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002117 JumpTarget try_block;
2118 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002119
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002120 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002122 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123 // In case of thrown exceptions, this is where we continue.
2124 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002125 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002126
2127 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002128 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002129
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002130 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2131 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002132
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002133 // Shadow the labels for all escapes from the try block, including
2134 // returns. Shadowing hides the original label as the LabelShadow and
2135 // operations on the original actually affect the shadowing label.
2136 //
2137 // We should probably try to unify the escaping labels and the return
2138 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002139 int nof_escapes = node->escaping_targets()->length();
2140 List<ShadowTarget*> shadows(1 + nof_escapes);
2141
2142 // Add the shadow target for the function return.
2143 static const int kReturnShadowIndex = 0;
2144 shadows.Add(new ShadowTarget(&function_return_));
2145 bool function_return_was_shadowed = function_return_is_shadowed_;
2146 function_return_is_shadowed_ = true;
2147 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2148
2149 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002150 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002151 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002152 }
2153
2154 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002155 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002156
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002157 // Stop the introduced shadowing and count the number of required unlinks.
2158 // After shadowing stops, the original labels are unshadowed and the
2159 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002160 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002161 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002162 shadows[i]->StopShadowing();
2163 if (shadows[i]->is_linked()) nof_unlinks++;
2164 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002165 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002166
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002167 // Get an external reference to the handler address.
2168 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002169
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002170 // If we can fall off the end of the try block, unlink from the try
2171 // chain and set the state on the frame to FALLING.
2172 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002173 // The next handler address is on top of the frame.
2174 ASSERT(StackHandlerConstants::kNextOffset == 0);
2175 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002176 __ mov(r3, Operand(handler_address));
2177 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002178 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002179
2180 // Fake a top of stack value (unneeded when FALLING) and set the
2181 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002182 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002183 frame_->EmitPush(r0);
2184 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2185 if (nof_unlinks > 0) {
2186 finally_block.Jump();
2187 }
2188 }
2189
2190 // Generate code to unlink and set the state for the (formerly)
2191 // shadowing targets that have been jumped to.
2192 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002193 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002194 // If we have come from the shadowed return, the return value is
2195 // in (a non-refcounted reference to) r0. We must preserve it
2196 // until it is pushed.
2197 //
2198 // Because we can be jumping here (to spilled code) from
2199 // unspilled code, we need to reestablish a spilled frame at
2200 // this block.
2201 shadows[i]->Bind();
2202 frame_->SpillAll();
2203
2204 // Reload sp from the top handler, because some statements that
2205 // we break from (eg, for...in) may have left stuff on the
2206 // stack.
2207 __ mov(r3, Operand(handler_address));
2208 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002209 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002210
2211 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002212 // handler address is currently on top of the frame.
2213 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002214 frame_->EmitPop(r1);
2215 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002216 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002217
2218 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002219 // If this label shadowed the function return, materialize the
2220 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002221 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002222 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002223 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002224 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002225 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002226 }
2227 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002228 if (--nof_unlinks > 0) {
2229 // If this is not the last unlink block, jump around the next.
2230 finally_block.Jump();
2231 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002232 }
2233 }
2234
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002235 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002236 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002238 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002239 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002240
2241 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002242 // and the state - while evaluating the finally block.
2243 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002244 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002245 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002246
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002247 if (has_valid_frame()) {
2248 // Restore state and return value or faked TOS.
2249 frame_->EmitPop(r2);
2250 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002251 }
2252
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002253 // Generate code to jump to the right destination for all used
2254 // formerly shadowing targets. Deallocate each shadow target.
2255 for (int i = 0; i < shadows.length(); i++) {
2256 if (has_valid_frame() && shadows[i]->is_bound()) {
2257 JumpTarget* original = shadows[i]->other_target();
2258 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2259 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002260 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002261 skip.Branch(ne);
2262 frame_->PrepareForReturn();
2263 original->Jump();
2264 skip.Bind();
2265 } else {
2266 original->Branch(eq);
2267 }
2268 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002269 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002270
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002271 if (has_valid_frame()) {
2272 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002273 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002274 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2275 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002277 // Rethrow exception.
2278 frame_->EmitPush(r0);
2279 frame_->CallRuntime(Runtime::kReThrow, 1);
2280
2281 // Done.
2282 exit.Bind();
2283 }
2284 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002285}
2286
2287
ager@chromium.org7c537e22008-10-16 08:43:32 +00002288void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002289#ifdef DEBUG
2290 int original_height = frame_->height();
2291#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002292 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002293 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002294 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002295#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +00002296 frame_->DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002297#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002298 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002299 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002300}
2301
2302
ager@chromium.org7c537e22008-10-16 08:43:32 +00002303void CodeGenerator::InstantiateBoilerplate(Handle<JSFunction> boilerplate) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002304 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002305 ASSERT(boilerplate->IsBoilerplate());
2306
ager@chromium.org3811b432009-10-28 14:53:37 +00002307 __ mov(r0, Operand(boilerplate));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002308 // Use the fast case closure allocation code that allocates in new
2309 // space for nested functions that don't need literals cloning.
2310 if (scope()->is_function_scope() && boilerplate->NumberOfLiterals() == 0) {
2311 FastNewClosureStub stub;
2312 frame_->EmitPush(r0);
2313 frame_->CallStub(&stub, 1);
2314 frame_->EmitPush(r0);
2315 } else {
2316 // Create a new closure.
2317 frame_->EmitPush(cp);
2318 frame_->EmitPush(r0);
2319 frame_->CallRuntime(Runtime::kNewClosure, 2);
2320 frame_->EmitPush(r0);
2321 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002322}
2323
2324
ager@chromium.org7c537e22008-10-16 08:43:32 +00002325void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002326#ifdef DEBUG
2327 int original_height = frame_->height();
2328#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002329 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002330 Comment cmnt(masm_, "[ FunctionLiteral");
2331
2332 // Build the function boilerplate and instantiate it.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002333 Handle<JSFunction> boilerplate =
ager@chromium.org5c838252010-02-19 08:53:10 +00002334 Compiler::BuildBoilerplate(node, script(), this);
kasper.lund212ac232008-07-16 07:07:30 +00002335 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002336 if (HasStackOverflow()) {
2337 ASSERT(frame_->height() == original_height);
2338 return;
2339 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002340 InstantiateBoilerplate(boilerplate);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002341 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002342}
2343
2344
ager@chromium.org7c537e22008-10-16 08:43:32 +00002345void CodeGenerator::VisitFunctionBoilerplateLiteral(
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002346 FunctionBoilerplateLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002347#ifdef DEBUG
2348 int original_height = frame_->height();
2349#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002350 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002351 Comment cmnt(masm_, "[ FunctionBoilerplateLiteral");
2352 InstantiateBoilerplate(node->boilerplate());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002353 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002354}
2355
2356
ager@chromium.org7c537e22008-10-16 08:43:32 +00002357void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002358#ifdef DEBUG
2359 int original_height = frame_->height();
2360#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002361 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002362 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002363 JumpTarget then;
2364 JumpTarget else_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002365 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002366 if (has_valid_frame()) {
2367 Branch(false, &else_);
2368 }
2369 if (has_valid_frame() || then.is_linked()) {
2370 then.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002371 LoadAndSpill(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002372 }
2373 if (else_.is_linked()) {
2374 JumpTarget exit;
2375 if (has_valid_frame()) exit.Jump();
2376 else_.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002377 LoadAndSpill(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002378 if (exit.is_linked()) exit.Bind();
2379 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002380 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002381}
2382
2383
ager@chromium.org7c537e22008-10-16 08:43:32 +00002384void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002385 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002386 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002387 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002388
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002389 JumpTarget slow;
2390 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002391
2392 // Generate fast-case code for variables that might be shadowed by
2393 // eval-introduced variables. Eval is used a lot without
2394 // introducing variables. In those cases, we do not want to
2395 // perform a runtime call for all variables in the scope
2396 // containing the eval.
2397 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2398 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002399 // If there was no control flow to slow, we can exit early.
2400 if (!slow.is_linked()) {
2401 frame_->EmitPush(r0);
2402 return;
2403 }
2404
2405 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002406
2407 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2408 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2409 // Only generate the fast case for locals that rewrite to slots.
2410 // This rules out argument loads.
2411 if (potential_slot != NULL) {
2412 __ ldr(r0,
2413 ContextSlotOperandCheckExtensions(potential_slot,
2414 r1,
2415 r2,
2416 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002417 if (potential_slot->var()->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002418 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2419 __ cmp(r0, ip);
2420 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002421 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002422 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002423 // ContextSlotOperandCheckExtensions so we have to jump around
2424 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002425 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002426 }
2427 }
2428
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002429 slow.Bind();
2430 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002431 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002432 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002433
ager@chromium.org7c537e22008-10-16 08:43:32 +00002434 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002435 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002436 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002437 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002438 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002439
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002440 done.Bind();
2441 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002442
2443 } else {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002444 // Special handling for locals allocated in registers.
ager@chromium.org7c537e22008-10-16 08:43:32 +00002445 __ ldr(r0, SlotOperand(slot, r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002446 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002447 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002448 // Const slots may contain 'the hole' value (the constant hasn't been
2449 // initialized yet) which needs to be converted into the 'undefined'
2450 // value.
2451 Comment cmnt(masm_, "[ Unhole const");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002452 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002453 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2454 __ cmp(r0, ip);
2455 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002456 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002457 }
2458 }
2459}
2460
2461
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002462void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
2463 ASSERT(slot != NULL);
2464 if (slot->type() == Slot::LOOKUP) {
2465 ASSERT(slot->var()->is_dynamic());
2466
2467 // For now, just do a runtime call.
2468 frame_->EmitPush(cp);
2469 __ mov(r0, Operand(slot->var()->name()));
2470 frame_->EmitPush(r0);
2471
2472 if (init_state == CONST_INIT) {
2473 // Same as the case for a normal store, but ignores attribute
2474 // (e.g. READ_ONLY) of context slot so that we can initialize
2475 // const properties (introduced via eval("const foo = (some
2476 // expr);")). Also, uses the current function context instead of
2477 // the top context.
2478 //
2479 // Note that we must declare the foo upon entry of eval(), via a
2480 // context slot declaration, but we cannot initialize it at the
2481 // same time, because the const declaration may be at the end of
2482 // the eval code (sigh...) and the const variable may have been
2483 // used before (where its value is 'undefined'). Thus, we can only
2484 // do the initialization when we actually encounter the expression
2485 // and when the expression operands are defined and valid, and
2486 // thus we need the split into 2 operations: declaration of the
2487 // context slot followed by initialization.
2488 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
2489 } else {
2490 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
2491 }
2492 // Storing a variable must keep the (new) value on the expression
2493 // stack. This is necessary for compiling assignment expressions.
2494 frame_->EmitPush(r0);
2495
2496 } else {
2497 ASSERT(!slot->var()->is_dynamic());
2498
2499 JumpTarget exit;
2500 if (init_state == CONST_INIT) {
2501 ASSERT(slot->var()->mode() == Variable::CONST);
2502 // Only the first const initialization must be executed (the slot
2503 // still contains 'the hole' value). When the assignment is
2504 // executed, the code is identical to a normal store (see below).
2505 Comment cmnt(masm_, "[ Init const");
2506 __ ldr(r2, SlotOperand(slot, r2));
2507 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2508 __ cmp(r2, ip);
2509 exit.Branch(ne);
2510 }
2511
2512 // We must execute the store. Storing a variable must keep the
2513 // (new) value on the stack. This is necessary for compiling
2514 // assignment expressions.
2515 //
2516 // Note: We will reach here even with slot->var()->mode() ==
2517 // Variable::CONST because of const declarations which will
2518 // initialize consts to 'the hole' value and by doing so, end up
2519 // calling this code. r2 may be loaded with context; used below in
2520 // RecordWrite.
2521 frame_->EmitPop(r0);
2522 __ str(r0, SlotOperand(slot, r2));
2523 frame_->EmitPush(r0);
2524 if (slot->type() == Slot::CONTEXT) {
2525 // Skip write barrier if the written value is a smi.
2526 __ tst(r0, Operand(kSmiTagMask));
2527 exit.Branch(eq);
2528 // r2 is loaded with context when calling SlotOperand above.
2529 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
2530 __ mov(r3, Operand(offset));
2531 __ RecordWrite(r2, r3, r1);
2532 }
2533 // If we definitely did not jump over the assignment, we do not need
2534 // to bind the exit label. Doing so can defeat peephole
2535 // optimization.
2536 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
2537 exit.Bind();
2538 }
2539 }
2540}
2541
2542
ager@chromium.org381abbb2009-02-25 13:23:22 +00002543void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2544 TypeofState typeof_state,
2545 Register tmp,
2546 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002547 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002548 // Check that no extension objects have been created by calls to
2549 // eval from the current scope to the global scope.
2550 Register context = cp;
2551 Scope* s = scope();
2552 while (s != NULL) {
2553 if (s->num_heap_slots() > 0) {
2554 if (s->calls_eval()) {
2555 // Check that extension is NULL.
2556 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2557 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002558 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002559 }
2560 // Load next context in chain.
2561 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2562 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2563 context = tmp;
2564 }
2565 // If no outer scope calls eval, we do not need to check more
2566 // context extensions.
2567 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2568 s = s->outer_scope();
2569 }
2570
2571 if (s->is_eval_scope()) {
2572 Label next, fast;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002573 if (!context.is(tmp)) {
2574 __ mov(tmp, Operand(context));
2575 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002576 __ bind(&next);
2577 // Terminate at global context.
2578 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002579 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
2580 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002581 __ b(eq, &fast);
2582 // Check that extension is NULL.
2583 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2584 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002585 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002586 // Load next context in chain.
2587 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2588 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2589 __ b(&next);
2590 __ bind(&fast);
2591 }
2592
2593 // All extension objects were empty and it is safe to use a global
2594 // load IC call.
2595 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2596 // Load the global object.
2597 LoadGlobal();
2598 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002599 __ mov(r2, Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002600 // Call IC stub.
2601 if (typeof_state == INSIDE_TYPEOF) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002602 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002603 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002604 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002605 }
2606
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002607 // Drop the global object. The result is in r0.
2608 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002609}
2610
2611
ager@chromium.org7c537e22008-10-16 08:43:32 +00002612void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002613#ifdef DEBUG
2614 int original_height = frame_->height();
2615#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002616 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002617 Comment cmnt(masm_, "[ Slot");
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002618 LoadFromSlot(node, NOT_INSIDE_TYPEOF);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002619 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002620}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002621
ager@chromium.org7c537e22008-10-16 08:43:32 +00002622
2623void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002624#ifdef DEBUG
2625 int original_height = frame_->height();
2626#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002627 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org7c537e22008-10-16 08:43:32 +00002628 Comment cmnt(masm_, "[ VariableProxy");
2629
2630 Variable* var = node->var();
2631 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002632 if (expr != NULL) {
2633 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002634 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002635 ASSERT(var->is_global());
2636 Reference ref(this, node);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002637 ref.GetValueAndSpill();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002638 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002639 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002640}
2641
2642
ager@chromium.org7c537e22008-10-16 08:43:32 +00002643void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002644#ifdef DEBUG
2645 int original_height = frame_->height();
2646#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002647 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002648 Comment cmnt(masm_, "[ Literal");
mads.s.ager31e71382008-08-13 09:32:07 +00002649 __ mov(r0, Operand(node->handle()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002650 frame_->EmitPush(r0);
2651 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002652}
2653
2654
ager@chromium.org7c537e22008-10-16 08:43:32 +00002655void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002656#ifdef DEBUG
2657 int original_height = frame_->height();
2658#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002659 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002660 Comment cmnt(masm_, "[ RexExp Literal");
2661
2662 // Retrieve the literal array and check the allocated entry.
2663
2664 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002665 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002666
2667 // Load the literals array of the function.
2668 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
2669
2670 // Load the literal at the ast saved index.
2671 int literal_offset =
2672 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
2673 __ ldr(r2, FieldMemOperand(r1, literal_offset));
2674
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002675 JumpTarget done;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002676 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2677 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002678 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002679
2680 // If the entry is undefined we call the runtime system to computed
2681 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002682 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00002683 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002684 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00002685 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002686 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002687 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002688 frame_->EmitPush(r0);
2689 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00002690 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002691
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002692 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002693 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002694 frame_->EmitPush(r2);
2695 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002696}
2697
2698
ager@chromium.org7c537e22008-10-16 08:43:32 +00002699void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002700#ifdef DEBUG
2701 int original_height = frame_->height();
2702#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002703 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002704 Comment cmnt(masm_, "[ ObjectLiteral");
2705
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002706 // Load the function of this activation.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002707 __ ldr(r2, frame_->Function());
2708 // Literal array.
2709 __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset));
2710 // Literal index.
2711 __ mov(r1, Operand(Smi::FromInt(node->literal_index())));
2712 // Constant properties.
2713 __ mov(r0, Operand(node->constant_properties()));
2714 frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit());
2715 if (node->depth() > 1) {
2716 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 3);
2717 } else {
2718 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002719 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002720 frame_->EmitPush(r0); // save the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002721 for (int i = 0; i < node->properties()->length(); i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002722 // At the start of each iteration, the top of stack contains
2723 // the newly created object literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002724 ObjectLiteral::Property* property = node->properties()->at(i);
2725 Literal* key = property->key();
2726 Expression* value = property->value();
2727 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002728 case ObjectLiteral::Property::CONSTANT:
2729 break;
2730 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
2731 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
2732 // else fall through
ager@chromium.org5c838252010-02-19 08:53:10 +00002733 case ObjectLiteral::Property::COMPUTED:
2734 if (key->handle()->IsSymbol()) {
2735 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
2736 LoadAndSpill(value);
2737 frame_->EmitPop(r0);
2738 __ mov(r2, Operand(key->handle()));
2739 __ ldr(r1, frame_->Top()); // Load the receiver.
2740 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
2741 break;
2742 }
2743 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002744 case ObjectLiteral::Property::PROTOTYPE: {
ager@chromium.org5c838252010-02-19 08:53:10 +00002745 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002746 frame_->EmitPush(r0); // dup the result
2747 LoadAndSpill(key);
2748 LoadAndSpill(value);
2749 frame_->CallRuntime(Runtime::kSetProperty, 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002750 break;
2751 }
2752 case ObjectLiteral::Property::SETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00002753 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002754 frame_->EmitPush(r0);
2755 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002756 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002757 frame_->EmitPush(r0);
2758 LoadAndSpill(value);
2759 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002760 break;
2761 }
2762 case ObjectLiteral::Property::GETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00002763 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002764 frame_->EmitPush(r0);
2765 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00002766 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002767 frame_->EmitPush(r0);
2768 LoadAndSpill(value);
2769 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002770 break;
2771 }
2772 }
2773 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002774 ASSERT(frame_->height() == original_height + 1);
2775}
2776
2777
ager@chromium.org7c537e22008-10-16 08:43:32 +00002778void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002779#ifdef DEBUG
2780 int original_height = frame_->height();
2781#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002782 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002783 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002784
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002785 // Load the function of this activation.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002786 __ ldr(r2, frame_->Function());
ager@chromium.org5c838252010-02-19 08:53:10 +00002787 // Load the literals array of the function.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002788 __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002789 __ mov(r1, Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002790 __ mov(r0, Operand(node->constant_elements()));
2791 frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit());
ager@chromium.org5c838252010-02-19 08:53:10 +00002792 int length = node->values()->length();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002793 if (node->depth() > 1) {
2794 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00002795 } else if (length > FastCloneShallowArrayStub::kMaximumLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002796 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00002797 } else {
2798 FastCloneShallowArrayStub stub(length);
2799 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002800 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002801 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002802 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002803
2804 // Generate code to set the elements in the array that are not
2805 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002806 for (int i = 0; i < node->values()->length(); i++) {
2807 Expression* value = node->values()->at(i);
2808
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002809 // If value is a literal the property value is already set in the
2810 // boilerplate object.
2811 if (value->AsLiteral() != NULL) continue;
2812 // If value is a materialized literal the property value is already set
2813 // in the boilerplate object if it is simple.
2814 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002815
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002816 // The property must be set by generated code.
2817 LoadAndSpill(value);
2818 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002819
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002820 // Fetch the object literal.
2821 __ ldr(r1, frame_->Top());
2822 // Get the elements array.
2823 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002824
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002825 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00002826 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002827 __ str(r0, FieldMemOperand(r1, offset));
2828
2829 // Update the write barrier for the array address.
2830 __ mov(r3, Operand(offset));
2831 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002832 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002833 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002834}
2835
2836
ager@chromium.org32912102009-01-16 10:38:43 +00002837void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002838#ifdef DEBUG
2839 int original_height = frame_->height();
2840#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002841 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org32912102009-01-16 10:38:43 +00002842 // Call runtime routine to allocate the catch extension object and
2843 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002844 Comment cmnt(masm_, "[ CatchExtensionObject");
2845 LoadAndSpill(node->key());
2846 LoadAndSpill(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002847 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
2848 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002849 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00002850}
2851
2852
ager@chromium.org7c537e22008-10-16 08:43:32 +00002853void CodeGenerator::VisitAssignment(Assignment* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002854#ifdef DEBUG
2855 int original_height = frame_->height();
2856#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002857 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002858 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00002859
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002860 { Reference target(this, node->target(), node->is_compound());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002861 if (target.is_illegal()) {
2862 // Fool the virtual frame into thinking that we left the assignment's
2863 // value on the frame.
2864 __ mov(r0, Operand(Smi::FromInt(0)));
2865 frame_->EmitPush(r0);
2866 ASSERT(frame_->height() == original_height + 1);
2867 return;
2868 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002869
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002870 if (node->op() == Token::ASSIGN ||
2871 node->op() == Token::INIT_VAR ||
2872 node->op() == Token::INIT_CONST) {
2873 LoadAndSpill(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00002874
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002875 } else { // Assignment is a compound assignment.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002876 // Get the old value of the lhs.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002877 target.GetValueAndSpill();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002878 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002879 bool overwrite =
2880 (node->value()->AsBinaryOperation() != NULL &&
2881 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002882 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002883 SmiOperation(node->binary_op(),
2884 literal->handle(),
2885 false,
2886 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002887 frame_->EmitPush(r0);
2888
2889 } else {
2890 LoadAndSpill(node->value());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002891 GenericBinaryOperation(node->binary_op(),
2892 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002893 frame_->EmitPush(r0);
2894 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002895 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002896 Variable* var = node->target()->AsVariableProxy()->AsVariable();
2897 if (var != NULL &&
2898 (var->mode() == Variable::CONST) &&
2899 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
2900 // Assignment ignored - leave the value on the stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002901 UnloadReference(&target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002902 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002903 CodeForSourcePosition(node->position());
2904 if (node->op() == Token::INIT_CONST) {
2905 // Dynamic constant initializations must use the function context
2906 // and initialize the actual constant declared. Dynamic variable
2907 // initializations are simply assignments and use SetValue.
2908 target.SetValue(CONST_INIT);
2909 } else {
2910 target.SetValue(NOT_CONST_INIT);
2911 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002912 }
2913 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002914 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002915}
2916
2917
ager@chromium.org7c537e22008-10-16 08:43:32 +00002918void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002919#ifdef DEBUG
2920 int original_height = frame_->height();
2921#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002922 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002923 Comment cmnt(masm_, "[ Throw");
2924
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002925 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00002926 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002927 frame_->CallRuntime(Runtime::kThrow, 1);
2928 frame_->EmitPush(r0);
2929 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002930}
2931
2932
ager@chromium.org7c537e22008-10-16 08:43:32 +00002933void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002934#ifdef DEBUG
2935 int original_height = frame_->height();
2936#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002937 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002938 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002939
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002940 { Reference property(this, node);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002941 property.GetValueAndSpill();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002942 }
2943 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002944}
2945
2946
ager@chromium.org7c537e22008-10-16 08:43:32 +00002947void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002948#ifdef DEBUG
2949 int original_height = frame_->height();
2950#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002951 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002952 Comment cmnt(masm_, "[ Call");
2953
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002954 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002955 ZoneList<Expression*>* args = node->arguments();
2956
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002957 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002958 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002959 Variable* var = function->AsVariableProxy()->AsVariable();
2960 Property* property = function->AsProperty();
2961
2962 // ------------------------------------------------------------------------
2963 // Fast-case: Use inline caching.
2964 // ---
2965 // According to ECMA-262, section 11.2.3, page 44, the function to call
2966 // must be resolved after the arguments have been evaluated. The IC code
2967 // automatically handles this by loading the arguments before the function
2968 // is resolved in cache misses (this also holds for megamorphic calls).
2969 // ------------------------------------------------------------------------
2970
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002971 if (var != NULL && var->is_possibly_eval()) {
2972 // ----------------------------------
2973 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
2974 // ----------------------------------
2975
2976 // In a call to eval, we first call %ResolvePossiblyDirectEval to
2977 // resolve the function we need to call and the receiver of the
2978 // call. Then we call the resolved function using the given
2979 // arguments.
2980 // Prepare stack for call to resolved function.
2981 LoadAndSpill(function);
2982 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
2983 frame_->EmitPush(r2); // Slot for receiver
2984 int arg_count = args->length();
2985 for (int i = 0; i < arg_count; i++) {
2986 LoadAndSpill(args->at(i));
2987 }
2988
2989 // Prepare stack for call to ResolvePossiblyDirectEval.
2990 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
2991 frame_->EmitPush(r1);
2992 if (arg_count > 0) {
2993 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
2994 frame_->EmitPush(r1);
2995 } else {
2996 frame_->EmitPush(r2);
2997 }
2998
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002999 // Push the receiver.
3000 __ ldr(r1, frame_->Receiver());
3001 frame_->EmitPush(r1);
3002
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003003 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003004 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003005
3006 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003007 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003008 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3009
3010 // Call the function.
3011 CodeForSourcePosition(node->position());
3012
3013 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003014 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003015 frame_->CallStub(&call_function, arg_count + 1);
3016
3017 __ ldr(cp, frame_->Context());
3018 // Remove the function from the stack.
3019 frame_->Drop();
3020 frame_->EmitPush(r0);
3021
3022 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003023 // ----------------------------------
3024 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3025 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003026 // Pass the global object as the receiver and let the IC stub
3027 // patch the stack to use the global proxy as 'this' in the
3028 // invoked function.
3029 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003030
3031 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003032 int arg_count = args->length();
3033 for (int i = 0; i < arg_count; i++) {
3034 LoadAndSpill(args->at(i));
3035 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003036
ager@chromium.org5c838252010-02-19 08:53:10 +00003037 // Setup the name register and call the IC initialization code.
3038 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003039 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3040 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003041 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003042 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3043 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003044 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003045 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003046
3047 } else if (var != NULL && var->slot() != NULL &&
3048 var->slot()->type() == Slot::LOOKUP) {
3049 // ----------------------------------
3050 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3051 // ----------------------------------
3052
3053 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003054 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003055 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003056 frame_->EmitPush(r0);
3057 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003058 // r0: slot value; r1: receiver
3059
3060 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003061 frame_->EmitPush(r0); // function
3062 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003063
3064 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003065 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003066 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003067
3068 } else if (property != NULL) {
3069 // Check if the key is a literal string.
3070 Literal* literal = property->key()->AsLiteral();
3071
3072 if (literal != NULL && literal->handle()->IsSymbol()) {
3073 // ------------------------------------------------------------------
3074 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3075 // ------------------------------------------------------------------
3076
ager@chromium.org5c838252010-02-19 08:53:10 +00003077 LoadAndSpill(property->obj()); // Receiver.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003078 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003079 int arg_count = args->length();
3080 for (int i = 0; i < arg_count; i++) {
3081 LoadAndSpill(args->at(i));
3082 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003083
ager@chromium.org5c838252010-02-19 08:53:10 +00003084 // Set the name register and call the IC initialization code.
3085 __ mov(r2, Operand(literal->handle()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003086 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3087 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003088 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003089 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003090 __ ldr(cp, frame_->Context());
ager@chromium.org5c838252010-02-19 08:53:10 +00003091 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003092
3093 } else {
3094 // -------------------------------------------
3095 // JavaScript example: 'array[index](1, 2, 3)'
3096 // -------------------------------------------
3097
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003098 LoadAndSpill(property->obj());
3099 LoadAndSpill(property->key());
3100 EmitKeyedLoad(false);
3101 frame_->Drop(); // key
3102 // Put the function below the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003103 if (property->is_synthetic()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003104 // Use the global receiver.
3105 frame_->Drop();
3106 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003107 LoadGlobalReceiver(r0);
3108 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003109 frame_->EmitPop(r1); // receiver
3110 frame_->EmitPush(r0); // function
3111 frame_->EmitPush(r1); // receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003112 }
3113
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003114 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003115 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003116 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003117 }
3118
3119 } else {
3120 // ----------------------------------
3121 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3122 // ----------------------------------
3123
3124 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003125 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003126
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003127 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003128 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003129
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003130 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003131 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003132 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003133 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003134 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003135}
3136
3137
ager@chromium.org7c537e22008-10-16 08:43:32 +00003138void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003139#ifdef DEBUG
3140 int original_height = frame_->height();
3141#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003142 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003143 Comment cmnt(masm_, "[ CallNew");
3144
3145 // According to ECMA-262, section 11.2.2, page 44, the function
3146 // expression in new calls must be evaluated before the
3147 // arguments. This is different from ordinary calls, where the
3148 // actual function to call is resolved after the arguments have been
3149 // evaluated.
3150
3151 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003152 // receiver. There is no need to use the global proxy here because
3153 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003154 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003155 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003156
3157 // Push the arguments ("left-to-right") on the stack.
3158 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003159 int arg_count = args->length();
3160 for (int i = 0; i < arg_count; i++) {
3161 LoadAndSpill(args->at(i));
3162 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003163
mads.s.ager31e71382008-08-13 09:32:07 +00003164 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003165 __ mov(r0, Operand(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003166 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003167 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003168
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003169 // Call the construct call builtin that handles allocation and
3170 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003171 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003172 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003173 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003174
3175 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003176 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003177 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003178}
3179
3180
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003181void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
3182 VirtualFrame::SpilledScope spilled_scope;
3183 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003184 JumpTarget leave, null, function, non_function_constructor;
3185
3186 // Load the object into r0.
3187 LoadAndSpill(args->at(0));
3188 frame_->EmitPop(r0);
3189
3190 // If the object is a smi, we return null.
3191 __ tst(r0, Operand(kSmiTagMask));
3192 null.Branch(eq);
3193
3194 // Check that the object is a JS object but take special care of JS
3195 // functions to make sure they have 'Function' as their class.
3196 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
3197 null.Branch(lt);
3198
3199 // As long as JS_FUNCTION_TYPE is the last instance type and it is
3200 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
3201 // LAST_JS_OBJECT_TYPE.
3202 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3203 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
3204 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3205 function.Branch(eq);
3206
3207 // Check if the constructor in the map is a function.
3208 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
3209 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
3210 non_function_constructor.Branch(ne);
3211
3212 // The r0 register now contains the constructor function. Grab the
3213 // instance class name from there.
3214 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
3215 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003216 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003217 leave.Jump();
3218
3219 // Functions have class 'Function'.
3220 function.Bind();
3221 __ mov(r0, Operand(Factory::function_class_symbol()));
3222 frame_->EmitPush(r0);
3223 leave.Jump();
3224
3225 // Objects with a non-function constructor have class 'Object'.
3226 non_function_constructor.Bind();
3227 __ mov(r0, Operand(Factory::Object_symbol()));
3228 frame_->EmitPush(r0);
3229 leave.Jump();
3230
3231 // Non-JS objects have class null.
3232 null.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003233 __ LoadRoot(r0, Heap::kNullValueRootIndex);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003234 frame_->EmitPush(r0);
3235
3236 // All done.
3237 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003238}
3239
3240
ager@chromium.org7c537e22008-10-16 08:43:32 +00003241void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003242 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003243 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003244 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003245 LoadAndSpill(args->at(0));
3246 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003247 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003248 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003249 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003250 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3251 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003252 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003253 // Load the value.
3254 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003255 leave.Bind();
3256 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003257}
3258
3259
ager@chromium.org7c537e22008-10-16 08:43:32 +00003260void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003261 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003262 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003263 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003264 LoadAndSpill(args->at(0)); // Load the object.
3265 LoadAndSpill(args->at(1)); // Load the value.
3266 frame_->EmitPop(r0); // r0 contains value
3267 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003268 // if (object->IsSmi()) return object.
3269 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003270 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003271 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3272 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003273 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003274 // Store the value.
3275 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3276 // Update the write barrier.
3277 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3278 __ RecordWrite(r1, r2, r3);
3279 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003280 leave.Bind();
3281 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003282}
3283
3284
ager@chromium.org7c537e22008-10-16 08:43:32 +00003285void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003286 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003287 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003288 LoadAndSpill(args->at(0));
3289 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003290 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003291 cc_reg_ = eq;
3292}
3293
3294
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003295void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003296 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003297 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3298 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003299#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003300 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003301 LoadAndSpill(args->at(1));
3302 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003303 __ CallRuntime(Runtime::kLog, 2);
3304 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003305#endif
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003306 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003307 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003308}
3309
3310
ager@chromium.org7c537e22008-10-16 08:43:32 +00003311void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003312 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003313 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003314 LoadAndSpill(args->at(0));
3315 frame_->EmitPop(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003316 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003317 cc_reg_ = eq;
3318}
3319
3320
kasper.lund7276f142008-07-30 08:49:36 +00003321// This should generate code that performs a charCodeAt() call or returns
3322// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3323// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003324void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003325 VirtualFrame::SpilledScope spilled_scope;
kasper.lund7276f142008-07-30 08:49:36 +00003326 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003327 Comment(masm_, "[ GenerateFastCharCodeAt");
3328
3329 LoadAndSpill(args->at(0));
3330 LoadAndSpill(args->at(1));
3331 frame_->EmitPop(r0); // Index.
3332 frame_->EmitPop(r1); // String.
3333
3334 Label slow, end, not_a_flat_string, ascii_string, try_again_with_new_string;
3335
3336 __ tst(r1, Operand(kSmiTagMask));
3337 __ b(eq, &slow); // The 'string' was a Smi.
3338
3339 ASSERT(kSmiTag == 0);
3340 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
3341 __ b(ne, &slow); // The index was negative or not a Smi.
3342
3343 __ bind(&try_again_with_new_string);
3344 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
3345 __ b(ge, &slow);
3346
3347 // Now r2 has the string type.
3348 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003349 // Now r3 has the length of the string. Compare with the index.
3350 __ cmp(r3, Operand(r0, LSR, kSmiTagSize));
3351 __ b(le, &slow);
3352
3353 // Here we know the index is in range. Check that string is sequential.
3354 ASSERT_EQ(0, kSeqStringTag);
3355 __ tst(r2, Operand(kStringRepresentationMask));
3356 __ b(ne, &not_a_flat_string);
3357
3358 // Check whether it is an ASCII string.
3359 ASSERT_EQ(0, kTwoByteStringTag);
3360 __ tst(r2, Operand(kStringEncodingMask));
3361 __ b(ne, &ascii_string);
3362
3363 // 2-byte string. We can add without shifting since the Smi tag size is the
3364 // log2 of the number of bytes in a two-byte character.
3365 ASSERT_EQ(1, kSmiTagSize);
3366 ASSERT_EQ(0, kSmiShiftSize);
3367 __ add(r1, r1, Operand(r0));
3368 __ ldrh(r0, FieldMemOperand(r1, SeqTwoByteString::kHeaderSize));
3369 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
3370 __ jmp(&end);
3371
3372 __ bind(&ascii_string);
3373 __ add(r1, r1, Operand(r0, LSR, kSmiTagSize));
3374 __ ldrb(r0, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
3375 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
3376 __ jmp(&end);
3377
3378 __ bind(&not_a_flat_string);
3379 __ and_(r2, r2, Operand(kStringRepresentationMask));
3380 __ cmp(r2, Operand(kConsStringTag));
3381 __ b(ne, &slow);
3382
3383 // ConsString.
3384 // Check that the right hand side is the empty string (ie if this is really a
3385 // flat string in a cons string). If that is not the case we would rather go
3386 // to the runtime system now, to flatten the string.
3387 __ ldr(r2, FieldMemOperand(r1, ConsString::kSecondOffset));
3388 __ LoadRoot(r3, Heap::kEmptyStringRootIndex);
3389 __ cmp(r2, Operand(r3));
3390 __ b(ne, &slow);
3391
3392 // Get the first of the two strings.
3393 __ ldr(r1, FieldMemOperand(r1, ConsString::kFirstOffset));
3394 __ jmp(&try_again_with_new_string);
3395
3396 __ bind(&slow);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003397 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003398
3399 __ bind(&end);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003400 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003401}
3402
3403
ager@chromium.org7c537e22008-10-16 08:43:32 +00003404void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003405 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003406 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003407 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003408 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003409 // We need the CC bits to come out as not_equal in the case where the
3410 // object is a smi. This can't be done with the usual test opcode so
3411 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003412 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003413 __ and_(r1, r0, Operand(kSmiTagMask));
3414 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003415 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003416 // It is a heap object - get the map. Check if the object is a JS array.
3417 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003418 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003419 cc_reg_ = eq;
3420}
3421
3422
ager@chromium.org6141cbe2009-11-20 12:14:52 +00003423void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
3424 // This generates a fast version of:
3425 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
3426 VirtualFrame::SpilledScope spilled_scope;
3427 ASSERT(args->length() == 1);
3428 LoadAndSpill(args->at(0));
3429 frame_->EmitPop(r1);
3430 __ tst(r1, Operand(kSmiTagMask));
3431 false_target()->Branch(eq);
3432
3433 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3434 __ cmp(r1, ip);
3435 true_target()->Branch(eq);
3436
3437 Register map_reg = r2;
3438 __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset));
3439 // Undetectable objects behave like undefined when tested with typeof.
3440 __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset));
3441 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3442 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3443 false_target()->Branch(eq);
3444
3445 __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
3446 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
3447 false_target()->Branch(lt);
3448 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
3449 cc_reg_ = le;
3450}
3451
3452
3453void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
3454 // This generates a fast version of:
3455 // (%_ClassOf(arg) === 'Function')
3456 VirtualFrame::SpilledScope spilled_scope;
3457 ASSERT(args->length() == 1);
3458 LoadAndSpill(args->at(0));
3459 frame_->EmitPop(r0);
3460 __ tst(r0, Operand(kSmiTagMask));
3461 false_target()->Branch(eq);
3462 Register map_reg = r2;
3463 __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE);
3464 cc_reg_ = eq;
3465}
3466
3467
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003468void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
3469 VirtualFrame::SpilledScope spilled_scope;
3470 ASSERT(args->length() == 1);
3471 LoadAndSpill(args->at(0));
3472 frame_->EmitPop(r0);
3473 __ tst(r0, Operand(kSmiTagMask));
3474 false_target()->Branch(eq);
3475 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3476 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
3477 __ tst(r1, Operand(1 << Map::kIsUndetectable));
3478 cc_reg_ = ne;
3479}
3480
3481
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003482void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
3483 VirtualFrame::SpilledScope spilled_scope;
3484 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003485
3486 // Get the frame pointer for the calling frame.
3487 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3488
3489 // Skip the arguments adaptor frame if it exists.
3490 Label check_frame_marker;
3491 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003492 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003493 __ b(ne, &check_frame_marker);
3494 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
3495
3496 // Check the marker in the calling frame.
3497 __ bind(&check_frame_marker);
3498 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
3499 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
3500 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003501}
3502
3503
ager@chromium.org7c537e22008-10-16 08:43:32 +00003504void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003505 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003506 ASSERT(args->length() == 0);
3507
mads.s.ager31e71382008-08-13 09:32:07 +00003508 // Seed the result with the formal parameters count, which will be used
3509 // in case no arguments adaptor frame is found below the current frame.
ager@chromium.org5c838252010-02-19 08:53:10 +00003510 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003511
3512 // Call the shared stub to get to the arguments.length.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003513 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_LENGTH);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003514 frame_->CallStub(&stub, 0);
3515 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003516}
3517
3518
ager@chromium.org7c537e22008-10-16 08:43:32 +00003519void CodeGenerator::GenerateArgumentsAccess(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003520 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003521 ASSERT(args->length() == 1);
3522
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003523 // Satisfy contract with ArgumentsAccessStub:
3524 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003525 LoadAndSpill(args->at(0));
3526 frame_->EmitPop(r1);
ager@chromium.org5c838252010-02-19 08:53:10 +00003527 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003528
3529 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003530 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003531 frame_->CallStub(&stub, 0);
3532 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003533}
3534
3535
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003536void CodeGenerator::GenerateRandomPositiveSmi(ZoneList<Expression*>* args) {
3537 VirtualFrame::SpilledScope spilled_scope;
3538 ASSERT(args->length() == 0);
3539 __ Call(ExternalReference::random_positive_smi_function().address(),
3540 RelocInfo::RUNTIME_ENTRY);
3541 frame_->EmitPush(r0);
3542}
3543
3544
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003545void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
3546 ASSERT_EQ(2, args->length());
3547
3548 Load(args->at(0));
3549 Load(args->at(1));
3550
ager@chromium.org5c838252010-02-19 08:53:10 +00003551 StringAddStub stub(NO_STRING_ADD_FLAGS);
3552 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003553 frame_->EmitPush(r0);
3554}
3555
3556
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003557void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
3558 ASSERT_EQ(3, args->length());
3559
3560 Load(args->at(0));
3561 Load(args->at(1));
3562 Load(args->at(2));
3563
ager@chromium.org5c838252010-02-19 08:53:10 +00003564 SubStringStub stub;
3565 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003566 frame_->EmitPush(r0);
3567}
3568
3569
3570void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
3571 ASSERT_EQ(2, args->length());
3572
3573 Load(args->at(0));
3574 Load(args->at(1));
3575
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003576 StringCompareStub stub;
3577 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003578 frame_->EmitPush(r0);
3579}
3580
3581
3582void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
3583 ASSERT_EQ(4, args->length());
3584
3585 Load(args->at(0));
3586 Load(args->at(1));
3587 Load(args->at(2));
3588 Load(args->at(3));
3589
3590 frame_->CallRuntime(Runtime::kRegExpExec, 4);
3591 frame_->EmitPush(r0);
3592}
3593
3594
ager@chromium.org5c838252010-02-19 08:53:10 +00003595void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
3596 ASSERT_EQ(args->length(), 1);
3597
3598 // Load the argument on the stack and jump to the runtime.
3599 Load(args->at(0));
3600
3601 frame_->CallRuntime(Runtime::kNumberToString, 1);
3602 frame_->EmitPush(r0);
3603}
3604
3605
ager@chromium.org7c537e22008-10-16 08:43:32 +00003606void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003607 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003608 ASSERT(args->length() == 2);
3609
3610 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003611 LoadAndSpill(args->at(0));
3612 LoadAndSpill(args->at(1));
3613 frame_->EmitPop(r0);
3614 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00003615 __ cmp(r0, Operand(r1));
3616 cc_reg_ = eq;
3617}
3618
3619
ager@chromium.org7c537e22008-10-16 08:43:32 +00003620void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003621#ifdef DEBUG
3622 int original_height = frame_->height();
3623#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003624 VirtualFrame::SpilledScope spilled_scope;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003625 if (CheckForInlineRuntimeCall(node)) {
3626 ASSERT((has_cc() && frame_->height() == original_height) ||
3627 (!has_cc() && frame_->height() == original_height + 1));
3628 return;
3629 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003630
3631 ZoneList<Expression*>* args = node->arguments();
3632 Comment cmnt(masm_, "[ CallRuntime");
3633 Runtime::Function* function = node->function();
3634
ager@chromium.org41826e72009-03-30 13:30:57 +00003635 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00003636 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00003637 // Push the builtins object found in the current global object.
3638 __ ldr(r1, GlobalObject());
3639 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003640 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003641 }
mads.s.ager31e71382008-08-13 09:32:07 +00003642
ager@chromium.org41826e72009-03-30 13:30:57 +00003643 // Push the arguments ("left-to-right").
3644 int arg_count = args->length();
3645 for (int i = 0; i < arg_count; i++) {
3646 LoadAndSpill(args->at(i));
3647 }
mads.s.ager31e71382008-08-13 09:32:07 +00003648
ager@chromium.org41826e72009-03-30 13:30:57 +00003649 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003650 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00003651 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003652 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3653 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003654 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003655 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003656 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00003657 } else {
3658 // Call the C runtime function.
3659 frame_->CallRuntime(function, arg_count);
3660 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003661 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003662 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003663}
3664
3665
ager@chromium.org7c537e22008-10-16 08:43:32 +00003666void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003667#ifdef DEBUG
3668 int original_height = frame_->height();
3669#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003670 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003671 Comment cmnt(masm_, "[ UnaryOperation");
3672
3673 Token::Value op = node->op();
3674
3675 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003676 LoadConditionAndSpill(node->expression(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003677 false_target(),
3678 true_target(),
3679 true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003680 // LoadCondition may (and usually does) leave a test and branch to
3681 // be emitted by the caller. In that case, negate the condition.
3682 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003683
3684 } else if (op == Token::DELETE) {
3685 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00003686 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003687 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003688 LoadAndSpill(property->obj());
3689 LoadAndSpill(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003690 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003691
mads.s.ager31e71382008-08-13 09:32:07 +00003692 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003693 Slot* slot = variable->slot();
3694 if (variable->is_global()) {
3695 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00003696 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003697 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003698 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003699
3700 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
3701 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003702 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003703 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003704 frame_->EmitPush(r0);
3705 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003706 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003707 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003708 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003709 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003710 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003711
mads.s.ager31e71382008-08-13 09:32:07 +00003712 } else {
3713 // Default: Result of deleting non-global, not dynamically
3714 // introduced variables is false.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003715 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00003716 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003717
3718 } else {
3719 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003720 LoadAndSpill(node->expression()); // may have side-effects
3721 frame_->Drop();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003722 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003723 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003724 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003725
3726 } else if (op == Token::TYPEOF) {
3727 // Special case for loading the typeof expression; see comment on
3728 // LoadTypeofExpression().
3729 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003730 frame_->CallRuntime(Runtime::kTypeof, 1);
3731 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003732
3733 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003734 bool overwrite =
3735 (node->expression()->AsBinaryOperation() != NULL &&
3736 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003737 LoadAndSpill(node->expression());
3738 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003739 switch (op) {
3740 case Token::NOT:
3741 case Token::DELETE:
3742 case Token::TYPEOF:
3743 UNREACHABLE(); // handled above
3744 break;
3745
3746 case Token::SUB: {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003747 GenericUnaryOpStub stub(Token::SUB, overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003748 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003749 break;
3750 }
3751
3752 case Token::BIT_NOT: {
3753 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003754 JumpTarget smi_label;
3755 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003756 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003757 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003758
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003759 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
3760 frame_->CallStub(&stub, 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003761 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003762
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003763 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003764 __ mvn(r0, Operand(r0));
3765 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003766 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003767 break;
3768 }
3769
3770 case Token::VOID:
3771 // since the stack top is cached in r0, popping and then
3772 // pushing a value can be done by just writing to r0.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003773 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003774 break;
3775
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003776 case Token::ADD: {
3777 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003778 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003779 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003780 continue_label.Branch(eq);
3781 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003782 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003783 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003784 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003785 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003786 default:
3787 UNREACHABLE();
3788 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003789 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003790 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003791 ASSERT(!has_valid_frame() ||
3792 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003793 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003794}
3795
3796
ager@chromium.org7c537e22008-10-16 08:43:32 +00003797void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003798#ifdef DEBUG
3799 int original_height = frame_->height();
3800#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003801 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003802 Comment cmnt(masm_, "[ CountOperation");
3803
3804 bool is_postfix = node->is_postfix();
3805 bool is_increment = node->op() == Token::INC;
3806
3807 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
3808 bool is_const = (var != NULL && var->mode() == Variable::CONST);
3809
3810 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00003811 if (is_postfix) {
3812 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003813 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003814 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003815
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003816 // A constant reference is not saved to, so a constant reference is not a
3817 // compound assignment reference.
3818 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003819 if (target.is_illegal()) {
3820 // Spoof the virtual frame to have the expected height (one higher
3821 // than on entry).
3822 if (!is_postfix) {
3823 __ mov(r0, Operand(Smi::FromInt(0)));
3824 frame_->EmitPush(r0);
3825 }
3826 ASSERT(frame_->height() == original_height + 1);
3827 return;
3828 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003829 target.GetValueAndSpill();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003830 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003831
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003832 JumpTarget slow;
3833 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003834
3835 // Load the value (1) into register r1.
3836 __ mov(r1, Operand(Smi::FromInt(1)));
3837
3838 // Check for smi operand.
3839 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003840 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003841
3842 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003843 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003844 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003845 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003846
3847 // Perform optimistic increment/decrement.
3848 if (is_increment) {
3849 __ add(r0, r0, Operand(r1), SetCC);
3850 } else {
3851 __ sub(r0, r0, Operand(r1), SetCC);
3852 }
3853
3854 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003855 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003856
3857 // Revert optimistic increment/decrement.
3858 if (is_increment) {
3859 __ sub(r0, r0, Operand(r1));
3860 } else {
3861 __ add(r0, r0, Operand(r1));
3862 }
3863
3864 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003865 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003866 {
3867 // Convert the operand to a number.
3868 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00003869 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003870 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003871 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003872 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003873 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003874 }
3875
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003876 // Compute the new value.
3877 __ mov(r1, Operand(Smi::FromInt(1)));
3878 frame_->EmitPush(r0);
3879 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003880 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003881 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003882 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00003883 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003884 }
3885
3886 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003887 exit.Bind();
3888 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00003889 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003890 }
3891
3892 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003893 if (is_postfix) frame_->EmitPop(r0);
3894 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003895}
3896
3897
ager@chromium.org7c537e22008-10-16 08:43:32 +00003898void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003899#ifdef DEBUG
3900 int original_height = frame_->height();
3901#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003902 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003903 Comment cmnt(masm_, "[ BinaryOperation");
3904 Token::Value op = node->op();
3905
3906 // According to ECMA-262 section 11.11, page 58, the binary logical
3907 // operators must yield the result of one of the two expressions
3908 // before any ToBoolean() conversions. This means that the value
3909 // produced by a && or || operator is not necessarily a boolean.
3910
3911 // NOTE: If the left hand side produces a materialized value (not in
3912 // the CC register), we force the right hand side to do the
3913 // same. This is necessary because we may have to branch to the exit
3914 // after evaluating the left hand side (due to the shortcut
3915 // semantics), but the compiler must (statically) know if the result
3916 // of compiling the binary operation is materialized or not.
3917
3918 if (op == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003919 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003920 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003921 &is_true,
3922 false_target(),
3923 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003924 if (has_valid_frame() && !has_cc()) {
3925 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003926 JumpTarget pop_and_continue;
3927 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003928
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003929 __ ldr(r0, frame_->Top()); // Duplicate the stack top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003930 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003931 // Avoid popping the result if it converts to 'false' using the
3932 // standard ToBoolean() conversion as described in ECMA-262,
3933 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003934 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003935 Branch(false, &exit);
3936
3937 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003938 pop_and_continue.Bind();
3939 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003940
3941 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003942 is_true.Bind();
3943 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003944
3945 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003946 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003947 } else if (has_cc() || is_true.is_linked()) {
3948 // The left-hand side is either (a) partially compiled to
3949 // control flow with a final branch left to emit or (b) fully
3950 // compiled to control flow and possibly true.
3951 if (has_cc()) {
3952 Branch(false, false_target());
3953 }
3954 is_true.Bind();
3955 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003956 true_target(),
3957 false_target(),
3958 false);
3959 } else {
3960 // Nothing to do.
3961 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003962 }
3963
3964 } else if (op == Token::OR) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003965 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003966 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003967 true_target(),
3968 &is_false,
3969 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003970 if (has_valid_frame() && !has_cc()) {
3971 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003972 JumpTarget pop_and_continue;
3973 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003974
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003975 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003976 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003977 // Avoid popping the result if it converts to 'true' using the
3978 // standard ToBoolean() conversion as described in ECMA-262,
3979 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00003980 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003981 Branch(true, &exit);
3982
3983 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003984 pop_and_continue.Bind();
3985 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003986
3987 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003988 is_false.Bind();
3989 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003990
3991 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003992 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003993 } else if (has_cc() || is_false.is_linked()) {
3994 // The left-hand side is either (a) partially compiled to
3995 // control flow with a final branch left to emit or (b) fully
3996 // compiled to control flow and possibly false.
3997 if (has_cc()) {
3998 Branch(true, true_target());
3999 }
4000 is_false.Bind();
4001 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004002 true_target(),
4003 false_target(),
4004 false);
4005 } else {
4006 // Nothing to do.
4007 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004008 }
4009
4010 } else {
4011 // Optimize for the case where (at least) one of the expressions
4012 // is a literal small integer.
4013 Literal* lliteral = node->left()->AsLiteral();
4014 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004015 // NOTE: The code below assumes that the slow cases (calls to runtime)
4016 // never return a constant/immutable object.
4017 bool overwrite_left =
4018 (node->left()->AsBinaryOperation() != NULL &&
4019 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
4020 bool overwrite_right =
4021 (node->right()->AsBinaryOperation() != NULL &&
4022 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004023
4024 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004025 LoadAndSpill(node->left());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004026 SmiOperation(node->op(),
4027 rliteral->handle(),
4028 false,
4029 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004030
4031 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004032 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004033 SmiOperation(node->op(),
4034 lliteral->handle(),
4035 true,
4036 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004037
4038 } else {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004039 OverwriteMode overwrite_mode = NO_OVERWRITE;
4040 if (overwrite_left) {
4041 overwrite_mode = OVERWRITE_LEFT;
4042 } else if (overwrite_right) {
4043 overwrite_mode = OVERWRITE_RIGHT;
4044 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004045 LoadAndSpill(node->left());
4046 LoadAndSpill(node->right());
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004047 GenericBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004048 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004049 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004050 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004051 ASSERT(!has_valid_frame() ||
4052 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004053 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004054}
4055
4056
ager@chromium.org7c537e22008-10-16 08:43:32 +00004057void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004058#ifdef DEBUG
4059 int original_height = frame_->height();
4060#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004061 VirtualFrame::SpilledScope spilled_scope;
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004062 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004063 frame_->EmitPush(r0);
4064 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004065}
4066
4067
ager@chromium.org7c537e22008-10-16 08:43:32 +00004068void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004069#ifdef DEBUG
4070 int original_height = frame_->height();
4071#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004072 VirtualFrame::SpilledScope spilled_scope;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004073 Comment cmnt(masm_, "[ CompareOperation");
4074
4075 // Get the expressions from the node.
4076 Expression* left = node->left();
4077 Expression* right = node->right();
4078 Token::Value op = node->op();
4079
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004080 // To make null checks efficient, we check if either left or right is the
4081 // literal 'null'. If so, we optimize the code by inlining a null check
4082 // instead of calling the (very) general runtime routine for checking
4083 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004084 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004085 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004086 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004087 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004088 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
4089 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004090 if (left_is_null || right_is_null) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004091 LoadAndSpill(left_is_null ? right : left);
4092 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004093 __ LoadRoot(ip, Heap::kNullValueRootIndex);
4094 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004095
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004096 // The 'null' value is only equal to 'undefined' if using non-strict
4097 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004098 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004099 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004100
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004101 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4102 __ cmp(r0, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004103 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004104
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004105 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004106 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004107
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004108 // It can be an undetectable object.
4109 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
4110 __ ldrb(r0, FieldMemOperand(r0, Map::kBitFieldOffset));
4111 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
4112 __ cmp(r0, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004113 }
4114
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004115 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004116 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004117 return;
4118 }
4119 }
4120
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004121 // To make typeof testing for natives implemented in JavaScript really
4122 // efficient, we generate special code for expressions of the form:
4123 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004124 UnaryOperation* operation = left->AsUnaryOperation();
4125 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
4126 (operation != NULL && operation->op() == Token::TYPEOF) &&
4127 (right->AsLiteral() != NULL &&
4128 right->AsLiteral()->handle()->IsString())) {
4129 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
4130
mads.s.ager31e71382008-08-13 09:32:07 +00004131 // Load the operand, move it to register r1.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004132 LoadTypeofExpression(operation->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004133 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004134
4135 if (check->Equals(Heap::number_symbol())) {
4136 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004137 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004138 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004139 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
4140 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004141 cc_reg_ = eq;
4142
4143 } else if (check->Equals(Heap::string_symbol())) {
4144 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004145 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004146
4147 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4148
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004149 // It can be an undetectable string object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004150 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4151 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4152 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004153 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004154
4155 __ ldrb(r2, FieldMemOperand(r1, Map::kInstanceTypeOffset));
4156 __ cmp(r2, Operand(FIRST_NONSTRING_TYPE));
4157 cc_reg_ = lt;
4158
4159 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004160 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
4161 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004162 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004163 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
4164 __ cmp(r1, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004165 cc_reg_ = eq;
4166
4167 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004168 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4169 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004170 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004171
4172 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004173 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004174
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004175 // It can be an undetectable object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004176 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
4177 __ ldrb(r2, FieldMemOperand(r1, Map::kBitFieldOffset));
4178 __ and_(r2, r2, Operand(1 << Map::kIsUndetectable));
4179 __ cmp(r2, Operand(1 << Map::kIsUndetectable));
4180
4181 cc_reg_ = eq;
4182
4183 } else if (check->Equals(Heap::function_symbol())) {
4184 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004185 false_target()->Branch(eq);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004186 Register map_reg = r2;
4187 __ CompareObjectType(r1, map_reg, r1, JS_FUNCTION_TYPE);
4188 true_target()->Branch(eq);
4189 // Regular expressions are callable so typeof == 'function'.
4190 __ CompareInstanceType(map_reg, r1, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004191 cc_reg_ = eq;
4192
4193 } else if (check->Equals(Heap::object_symbol())) {
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.orgab99eea2009-08-25 07:05:41 +00004197 __ LoadRoot(ip, Heap::kNullValueRootIndex);
4198 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004199 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004200
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004201 Register map_reg = r2;
4202 __ CompareObjectType(r1, map_reg, r1, JS_REGEXP_TYPE);
4203 false_target()->Branch(eq);
4204
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004205 // It can be an undetectable object.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004206 __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004207 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
4208 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004209 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004210
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004211 __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4212 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004213 false_target()->Branch(lt);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004214 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004215 cc_reg_ = le;
4216
4217 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004218 // Uncommon case: typeof testing against a string literal that is
4219 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004220 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004221 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004222 ASSERT(!has_valid_frame() ||
4223 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004224 return;
4225 }
4226
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004227 switch (op) {
4228 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004229 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004230 break;
4231
4232 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004233 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004234 break;
4235
4236 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004237 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004238 break;
4239
4240 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004241 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004242 break;
4243
4244 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004245 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004246 break;
4247
4248 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004249 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004250 break;
4251
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004252 case Token::IN: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004253 LoadAndSpill(left);
4254 LoadAndSpill(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004255 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004256 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004257 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004258 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004259
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004260 case Token::INSTANCEOF: {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004261 LoadAndSpill(left);
4262 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004263 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004264 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004265 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004266 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004267 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004268 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004269 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004270
4271 default:
4272 UNREACHABLE();
4273 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004274 ASSERT((has_cc() && frame_->height() == original_height) ||
4275 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004276}
4277
4278
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004279void CodeGenerator::EmitKeyedLoad(bool is_global) {
4280 Comment cmnt(masm_, "[ Load from keyed Property");
4281 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
4282 RelocInfo::Mode rmode = is_global
4283 ? RelocInfo::CODE_TARGET_CONTEXT
4284 : RelocInfo::CODE_TARGET;
4285 frame_->CallCodeObject(ic, rmode, 0);
4286}
4287
4288
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004289#ifdef DEBUG
4290bool CodeGenerator::HasValidEntryRegisters() { return true; }
4291#endif
4292
4293
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004294#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004295#define __ ACCESS_MASM(masm)
4296
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004297
ager@chromium.org7c537e22008-10-16 08:43:32 +00004298Handle<String> Reference::GetName() {
4299 ASSERT(type_ == NAMED);
4300 Property* property = expression_->AsProperty();
4301 if (property == NULL) {
4302 // Global variable reference treated as a named property reference.
4303 VariableProxy* proxy = expression_->AsVariableProxy();
4304 ASSERT(proxy->AsVariable() != NULL);
4305 ASSERT(proxy->AsVariable()->is_global());
4306 return proxy->name();
4307 } else {
4308 Literal* raw_name = property->key()->AsLiteral();
4309 ASSERT(raw_name != NULL);
4310 return Handle<String>(String::cast(*raw_name->handle()));
4311 }
4312}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004313
ager@chromium.org7c537e22008-10-16 08:43:32 +00004314
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004315void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004316 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004317 ASSERT(!is_illegal());
4318 ASSERT(!cgen_->has_cc());
4319 MacroAssembler* masm = cgen_->masm();
4320 Property* property = expression_->AsProperty();
4321 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004322 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004323 }
4324
4325 switch (type_) {
4326 case SLOT: {
4327 Comment cmnt(masm, "[ Load from Slot");
4328 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4329 ASSERT(slot != NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004330 cgen_->LoadFromSlot(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004331 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004332 }
4333
ager@chromium.org7c537e22008-10-16 08:43:32 +00004334 case NAMED: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004335 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004336 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004337 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004338 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004339 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4340 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004341 __ mov(r2, Operand(name));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004342 ASSERT(var == NULL || var->is_global());
4343 RelocInfo::Mode rmode = (var == NULL)
4344 ? RelocInfo::CODE_TARGET
4345 : RelocInfo::CODE_TARGET_CONTEXT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004346 frame->CallCodeObject(ic, rmode, 0);
4347 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004348 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004349 }
4350
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004351 case KEYED: {
4352 // TODO(181): Implement inlined version of array indexing once
4353 // loop nesting is properly tracked on ARM.
4354 ASSERT(property != NULL);
4355 Variable* var = expression_->AsVariableProxy()->AsVariable();
4356 ASSERT(var == NULL || var->is_global());
4357 cgen_->EmitKeyedLoad(var != NULL);
4358 cgen_->frame()->EmitPush(r0);
4359 break;
4360 }
4361
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004362 default:
4363 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004364 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004365
4366 if (!persist_after_get_) {
4367 cgen_->UnloadReference(this);
4368 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004369}
4370
4371
ager@chromium.org7c537e22008-10-16 08:43:32 +00004372void Reference::SetValue(InitState init_state) {
4373 ASSERT(!is_illegal());
4374 ASSERT(!cgen_->has_cc());
4375 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004376 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004377 Property* property = expression_->AsProperty();
4378 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004379 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004380 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004381
ager@chromium.org7c537e22008-10-16 08:43:32 +00004382 switch (type_) {
4383 case SLOT: {
4384 Comment cmnt(masm, "[ Store to Slot");
4385 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004386 cgen_->StoreToSlot(slot, init_state);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004387 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004388 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004389 }
4390
ager@chromium.org7c537e22008-10-16 08:43:32 +00004391 case NAMED: {
4392 Comment cmnt(masm, "[ Store to named Property");
4393 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00004394 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004395 Handle<String> name(GetName());
4396
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004397 frame->EmitPop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004398 frame->EmitPop(r1);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004399 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004400 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004401 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00004402 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004403 break;
4404 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004405
ager@chromium.org7c537e22008-10-16 08:43:32 +00004406 case KEYED: {
4407 Comment cmnt(masm, "[ Store to keyed Property");
4408 Property* property = expression_->AsProperty();
4409 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004410 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004411
4412 // Call IC code.
4413 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004414 frame->EmitPop(r0); // value
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004415 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004416 frame->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004417 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004418 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004419 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00004420
4421 default:
4422 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004423 }
4424}
4425
4426
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004427void FastNewClosureStub::Generate(MacroAssembler* masm) {
4428 // Clone the boilerplate in new space. Set the context to the
4429 // current context in cp.
4430 Label gc;
4431
4432 // Pop the boilerplate function from the stack.
4433 __ pop(r3);
4434
4435 // Attempt to allocate new JSFunction in new space.
4436 __ AllocateInNewSpace(JSFunction::kSize / kPointerSize,
4437 r0,
4438 r1,
4439 r2,
4440 &gc,
4441 TAG_OBJECT);
4442
4443 // Compute the function map in the current global context and set that
4444 // as the map of the allocated object.
4445 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
4446 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
4447 __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
4448 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4449
4450 // Clone the rest of the boilerplate fields. We don't have to update
4451 // the write barrier because the allocated object is in new space.
4452 for (int offset = kPointerSize;
4453 offset < JSFunction::kSize;
4454 offset += kPointerSize) {
4455 if (offset == JSFunction::kContextOffset) {
4456 __ str(cp, FieldMemOperand(r0, offset));
4457 } else {
4458 __ ldr(r1, FieldMemOperand(r3, offset));
4459 __ str(r1, FieldMemOperand(r0, offset));
4460 }
4461 }
4462
4463 // Return result. The argument boilerplate has been popped already.
4464 __ Ret();
4465
4466 // Create a new closure through the slower runtime call.
4467 __ bind(&gc);
4468 __ push(cp);
4469 __ push(r3);
4470 __ TailCallRuntime(ExternalReference(Runtime::kNewClosure), 2, 1);
4471}
4472
4473
4474void FastNewContextStub::Generate(MacroAssembler* masm) {
4475 // Try to allocate the context in new space.
4476 Label gc;
4477 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
4478
4479 // Attempt to allocate the context in new space.
4480 __ AllocateInNewSpace(length + (FixedArray::kHeaderSize / kPointerSize),
4481 r0,
4482 r1,
4483 r2,
4484 &gc,
4485 TAG_OBJECT);
4486
4487 // Load the function from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00004488 __ ldr(r3, MemOperand(sp, 0));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004489
4490 // Setup the object header.
4491 __ LoadRoot(r2, Heap::kContextMapRootIndex);
4492 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4493 __ mov(r2, Operand(length));
4494 __ str(r2, FieldMemOperand(r0, Array::kLengthOffset));
4495
4496 // Setup the fixed slots.
4497 __ mov(r1, Operand(Smi::FromInt(0)));
4498 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
4499 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
4500 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
4501 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
4502
4503 // Copy the global object from the surrounding context.
4504 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
4505 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
4506
4507 // Initialize the rest of the slots to undefined.
4508 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
4509 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
4510 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
4511 }
4512
4513 // Remove the on-stack argument and return.
4514 __ mov(cp, r0);
4515 __ pop();
4516 __ Ret();
4517
4518 // Need to collect. Call into runtime system.
4519 __ bind(&gc);
4520 __ TailCallRuntime(ExternalReference(Runtime::kNewContext), 1, 1);
4521}
4522
4523
ager@chromium.org5c838252010-02-19 08:53:10 +00004524void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
4525 // Stack layout on entry:
4526 //
4527 // [sp]: constant elements.
4528 // [sp + kPointerSize]: literal index.
4529 // [sp + (2 * kPointerSize)]: literals array.
4530
4531 // All sizes here are multiples of kPointerSize.
4532 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
4533 int size = JSArray::kSize + elements_size;
4534
4535 // Load boilerplate object into r3 and check if we need to create a
4536 // boilerplate.
4537 Label slow_case;
4538 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
4539 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
4540 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4541 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
4542 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4543 __ cmp(r3, ip);
4544 __ b(eq, &slow_case);
4545
4546 // Allocate both the JS array and the elements array in one big
4547 // allocation. This avoids multiple limit checks.
4548 __ AllocateInNewSpace(size / kPointerSize,
4549 r0,
4550 r1,
4551 r2,
4552 &slow_case,
4553 TAG_OBJECT);
4554
4555 // Copy the JS array part.
4556 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
4557 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
4558 __ ldr(r1, FieldMemOperand(r3, i));
4559 __ str(r1, FieldMemOperand(r0, i));
4560 }
4561 }
4562
4563 if (length_ > 0) {
4564 // Get hold of the elements array of the boilerplate and setup the
4565 // elements pointer in the resulting object.
4566 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
4567 __ add(r2, r0, Operand(JSArray::kSize));
4568 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
4569
4570 // Copy the elements array.
4571 for (int i = 0; i < elements_size; i += kPointerSize) {
4572 __ ldr(r1, FieldMemOperand(r3, i));
4573 __ str(r1, FieldMemOperand(r2, i));
4574 }
4575 }
4576
4577 // Return and remove the on-stack parameters.
4578 __ add(sp, sp, Operand(3 * kPointerSize));
4579 __ Ret();
4580
4581 __ bind(&slow_case);
4582 ExternalReference runtime(Runtime::kCreateArrayLiteralShallow);
4583 __ TailCallRuntime(runtime, 3, 1);
4584}
4585
4586
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004587// Count leading zeros in a 32 bit word. On ARM5 and later it uses the clz
4588// instruction. On pre-ARM5 hardware this routine gives the wrong answer for 0
4589// (31 instead of 32).
4590static void CountLeadingZeros(
4591 MacroAssembler* masm,
4592 Register source,
4593 Register scratch,
4594 Register zeros) {
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00004595#ifdef CAN_USE_ARMV5_INSTRUCTIONS
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004596 __ clz(zeros, source); // This instruction is only supported after ARM5.
4597#else
4598 __ mov(zeros, Operand(0));
4599 __ mov(scratch, source);
4600 // Top 16.
4601 __ tst(scratch, Operand(0xffff0000));
4602 __ add(zeros, zeros, Operand(16), LeaveCC, eq);
4603 __ mov(scratch, Operand(scratch, LSL, 16), LeaveCC, eq);
4604 // Top 8.
4605 __ tst(scratch, Operand(0xff000000));
4606 __ add(zeros, zeros, Operand(8), LeaveCC, eq);
4607 __ mov(scratch, Operand(scratch, LSL, 8), LeaveCC, eq);
4608 // Top 4.
4609 __ tst(scratch, Operand(0xf0000000));
4610 __ add(zeros, zeros, Operand(4), LeaveCC, eq);
4611 __ mov(scratch, Operand(scratch, LSL, 4), LeaveCC, eq);
4612 // Top 2.
4613 __ tst(scratch, Operand(0xc0000000));
4614 __ add(zeros, zeros, Operand(2), LeaveCC, eq);
4615 __ mov(scratch, Operand(scratch, LSL, 2), LeaveCC, eq);
4616 // Top bit.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004617 __ tst(scratch, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004618 __ add(zeros, zeros, Operand(1), LeaveCC, eq);
4619#endif
4620}
4621
4622
4623// Takes a Smi and converts to an IEEE 64 bit floating point value in two
4624// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
4625// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
4626// scratch register. Destroys the source register. No GC occurs during this
4627// stub so you don't have to set up the frame.
4628class ConvertToDoubleStub : public CodeStub {
4629 public:
4630 ConvertToDoubleStub(Register result_reg_1,
4631 Register result_reg_2,
4632 Register source_reg,
4633 Register scratch_reg)
4634 : result1_(result_reg_1),
4635 result2_(result_reg_2),
4636 source_(source_reg),
4637 zeros_(scratch_reg) { }
4638
4639 private:
4640 Register result1_;
4641 Register result2_;
4642 Register source_;
4643 Register zeros_;
4644
4645 // Minor key encoding in 16 bits.
4646 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4647 class OpBits: public BitField<Token::Value, 2, 14> {};
4648
4649 Major MajorKey() { return ConvertToDouble; }
4650 int MinorKey() {
4651 // Encode the parameters in a unique 16 bit value.
4652 return result1_.code() +
4653 (result2_.code() << 4) +
4654 (source_.code() << 8) +
4655 (zeros_.code() << 12);
4656 }
4657
4658 void Generate(MacroAssembler* masm);
4659
4660 const char* GetName() { return "ConvertToDoubleStub"; }
4661
4662#ifdef DEBUG
4663 void Print() { PrintF("ConvertToDoubleStub\n"); }
4664#endif
4665};
4666
4667
4668void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
4669#ifndef BIG_ENDIAN_FLOATING_POINT
4670 Register exponent = result1_;
4671 Register mantissa = result2_;
4672#else
4673 Register exponent = result2_;
4674 Register mantissa = result1_;
4675#endif
4676 Label not_special;
4677 // Convert from Smi to integer.
4678 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
4679 // Move sign bit from source to destination. This works because the sign bit
4680 // in the exponent word of the double has the same position and polarity as
4681 // the 2's complement sign bit in a Smi.
4682 ASSERT(HeapNumber::kSignMask == 0x80000000u);
4683 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
4684 // Subtract from 0 if source was negative.
4685 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
4686 __ cmp(source_, Operand(1));
4687 __ b(gt, &not_special);
4688
4689 // We have -1, 0 or 1, which we treat specially.
4690 __ cmp(source_, Operand(0));
4691 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
4692 static const uint32_t exponent_word_for_1 =
4693 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
4694 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, ne);
4695 // 1, 0 and -1 all have 0 for the second word.
4696 __ mov(mantissa, Operand(0));
4697 __ Ret();
4698
4699 __ bind(&not_special);
4700 // Count leading zeros. Uses result2 for a scratch register on pre-ARM5.
4701 // Gets the wrong answer for 0, but we already checked for that case above.
4702 CountLeadingZeros(masm, source_, mantissa, zeros_);
4703 // Compute exponent and or it into the exponent register.
4704 // We use result2 as a scratch register here.
4705 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
4706 __ orr(exponent,
4707 exponent,
4708 Operand(mantissa, LSL, HeapNumber::kExponentShift));
4709 // Shift up the source chopping the top bit off.
4710 __ add(zeros_, zeros_, Operand(1));
4711 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
4712 __ mov(source_, Operand(source_, LSL, zeros_));
4713 // Compute lower part of fraction (last 12 bits).
4714 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
4715 // And the top (top 20 bits).
4716 __ orr(exponent,
4717 exponent,
4718 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
4719 __ Ret();
4720}
4721
4722
4723// This stub can convert a signed int32 to a heap number (double). It does
4724// not work for int32s that are in Smi range! No GC occurs during this stub
4725// so you don't have to set up the frame.
4726class WriteInt32ToHeapNumberStub : public CodeStub {
4727 public:
4728 WriteInt32ToHeapNumberStub(Register the_int,
4729 Register the_heap_number,
4730 Register scratch)
4731 : the_int_(the_int),
4732 the_heap_number_(the_heap_number),
4733 scratch_(scratch) { }
4734
4735 private:
4736 Register the_int_;
4737 Register the_heap_number_;
4738 Register scratch_;
4739
4740 // Minor key encoding in 16 bits.
4741 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
4742 class OpBits: public BitField<Token::Value, 2, 14> {};
4743
4744 Major MajorKey() { return WriteInt32ToHeapNumber; }
4745 int MinorKey() {
4746 // Encode the parameters in a unique 16 bit value.
4747 return the_int_.code() +
4748 (the_heap_number_.code() << 4) +
4749 (scratch_.code() << 8);
4750 }
4751
4752 void Generate(MacroAssembler* masm);
4753
4754 const char* GetName() { return "WriteInt32ToHeapNumberStub"; }
4755
4756#ifdef DEBUG
4757 void Print() { PrintF("WriteInt32ToHeapNumberStub\n"); }
4758#endif
4759};
4760
4761
4762// See comment for class.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004763void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004764 Label max_negative_int;
4765 // the_int_ has the answer which is a signed int32 but not a Smi.
4766 // We test for the special value that has a different exponent. This test
4767 // has the neat side effect of setting the flags according to the sign.
4768 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00004769 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004770 __ b(eq, &max_negative_int);
4771 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
4772 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
4773 uint32_t non_smi_exponent =
4774 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
4775 __ mov(scratch_, Operand(non_smi_exponent));
4776 // Set the sign bit in scratch_ if the value was negative.
4777 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
4778 // Subtract from 0 if the value was negative.
4779 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
4780 // We should be masking the implict first digit of the mantissa away here,
4781 // but it just ends up combining harmlessly with the last digit of the
4782 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
4783 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
4784 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
4785 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
4786 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
4787 __ str(scratch_, FieldMemOperand(the_heap_number_,
4788 HeapNumber::kExponentOffset));
4789 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
4790 __ str(scratch_, FieldMemOperand(the_heap_number_,
4791 HeapNumber::kMantissaOffset));
4792 __ Ret();
4793
4794 __ bind(&max_negative_int);
4795 // The max negative int32 is stored as a positive number in the mantissa of
4796 // a double because it uses a sign bit instead of using two's complement.
4797 // The actual mantissa bits stored are all 0 because the implicit most
4798 // significant 1 bit is not stored.
4799 non_smi_exponent += 1 << HeapNumber::kExponentShift;
4800 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
4801 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
4802 __ mov(ip, Operand(0));
4803 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
4804 __ Ret();
4805}
4806
4807
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004808// Handle the case where the lhs and rhs are the same object.
4809// Equality is almost reflexive (everything but NaN), so this is a test
4810// for "identity and not NaN".
4811static void EmitIdenticalObjectComparison(MacroAssembler* masm,
4812 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004813 Condition cc,
4814 bool never_nan_nan) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004815 Label not_identical;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004816 Label heap_number, return_equal;
4817 Register exp_mask_reg = r5;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004818 __ cmp(r0, Operand(r1));
4819 __ b(ne, &not_identical);
4820
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004821 // The two objects are identical. If we know that one of them isn't NaN then
4822 // we now know they test equal.
4823 if (cc != eq || !never_nan_nan) {
4824 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004825
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004826 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
4827 // so we do the second best thing - test it ourselves.
4828 // They are both equal and they are not both Smis so both of them are not
4829 // Smis. If it's not a heap number, then return equal.
4830 if (cc == lt || cc == gt) {
4831 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004832 __ b(ge, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004833 } else {
4834 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4835 __ b(eq, &heap_number);
4836 // Comparing JS objects with <=, >= is complicated.
4837 if (cc != eq) {
4838 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
4839 __ b(ge, slow);
4840 // Normally here we fall through to return_equal, but undefined is
4841 // special: (undefined == undefined) == true, but
4842 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
4843 if (cc == le || cc == ge) {
4844 __ cmp(r4, Operand(ODDBALL_TYPE));
4845 __ b(ne, &return_equal);
4846 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
4847 __ cmp(r0, Operand(r2));
4848 __ b(ne, &return_equal);
4849 if (cc == le) {
4850 // undefined <= undefined should fail.
4851 __ mov(r0, Operand(GREATER));
4852 } else {
4853 // undefined >= undefined should fail.
4854 __ mov(r0, Operand(LESS));
4855 }
4856 __ mov(pc, Operand(lr)); // Return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004857 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004858 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004859 }
4860 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004861
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004862 __ bind(&return_equal);
4863 if (cc == lt) {
4864 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
4865 } else if (cc == gt) {
4866 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
4867 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004868 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004869 }
4870 __ mov(pc, Operand(lr)); // Return.
4871
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004872 if (cc != eq || !never_nan_nan) {
4873 // For less and greater we don't have to check for NaN since the result of
4874 // x < x is false regardless. For the others here is some code to check
4875 // for NaN.
4876 if (cc != lt && cc != gt) {
4877 __ bind(&heap_number);
4878 // It is a heap number, so return non-equal if it's NaN and equal if it's
4879 // not NaN.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004880
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004881 // The representation of NaN values has all exponent bits (52..62) set,
4882 // and not all mantissa bits (0..51) clear.
4883 // Read top bits of double representation (second word of value).
4884 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
4885 // Test that exponent bits are all set.
4886 __ and_(r3, r2, Operand(exp_mask_reg));
4887 __ cmp(r3, Operand(exp_mask_reg));
4888 __ b(ne, &return_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004889
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004890 // Shift out flag and all exponent bits, retaining only mantissa.
4891 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
4892 // Or with all low-bits of mantissa.
4893 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
4894 __ orr(r0, r3, Operand(r2), SetCC);
4895 // For equal we already have the right value in r0: Return zero (equal)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004896 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
4897 // not (it's a NaN). For <= and >= we need to load r0 with the failing
4898 // value if it's a NaN.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004899 if (cc != eq) {
4900 // All-zero means Infinity means equal.
4901 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
4902 if (cc == le) {
4903 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
4904 } else {
4905 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
4906 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004907 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004908 __ mov(pc, Operand(lr)); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004909 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004910 // No fall through here.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004911 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004912
4913 __ bind(&not_identical);
4914}
4915
4916
4917// See comment at call site.
4918static void EmitSmiNonsmiComparison(MacroAssembler* masm,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004919 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004920 Label* slow,
4921 bool strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004922 Label rhs_is_smi;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004923 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004924 __ b(eq, &rhs_is_smi);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004925
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004926 // Lhs is a Smi. Check whether the rhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004927 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
4928 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004929 // If rhs is not a number and lhs is a Smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004930 // succeed. Return non-equal (r0 is already not zero)
4931 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4932 } else {
4933 // Smi compared non-strictly with a non-Smi non-heap-number. Call
4934 // the runtime.
4935 __ b(ne, slow);
4936 }
4937
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004938 // Lhs (r1) is a smi, rhs (r0) is a number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004939 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004940 // Convert lhs to a double in d7 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004941 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004942 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
4943 __ vmov(s15, r7);
4944 __ vcvt(d7, s15);
4945 // Load the double from rhs, tagged HeapNumber r0, to d6.
4946 __ sub(r7, r0, Operand(kHeapObjectTag));
4947 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004948 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004949 __ push(lr);
4950 // Convert lhs to a double in r2, r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004951 __ mov(r7, Operand(r1));
4952 ConvertToDoubleStub stub1(r3, r2, r7, r6);
4953 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004954 // Load rhs to a double in r0, r1.
4955 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
4956 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
4957 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004958 }
4959
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004960 // We now have both loaded as doubles but we can skip the lhs nan check
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004961 // since it's a smi.
4962 __ jmp(lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004963
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004964 __ bind(&rhs_is_smi);
4965 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004966 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
4967 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004968 // If lhs is not a number and rhs is a smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004969 // succeed. Return non-equal.
4970 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
4971 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
4972 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004973 // Smi compared non-strictly with a non-smi non-heap-number. Call
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004974 // the runtime.
4975 __ b(ne, slow);
4976 }
4977
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004978 // Rhs (r0) is a smi, lhs (r1) is a heap number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004979 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004980 // Convert rhs to a double in d6 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004981 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004982 // Load the double from lhs, tagged HeapNumber r1, to d7.
4983 __ sub(r7, r1, Operand(kHeapObjectTag));
4984 __ vldr(d7, r7, HeapNumber::kValueOffset);
4985 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
4986 __ vmov(s13, r7);
4987 __ vcvt(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004988 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004989 __ push(lr);
4990 // Load lhs to a double in r2, r3.
4991 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
4992 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
4993 // Convert rhs to a double in r0, r1.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004994 __ mov(r7, Operand(r0));
4995 ConvertToDoubleStub stub2(r1, r0, r7, r6);
4996 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004997 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004998 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004999 // Fall through to both_loaded_as_doubles.
5000}
5001
5002
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005003void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005004 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005005 Register rhs_exponent = exp_first ? r0 : r1;
5006 Register lhs_exponent = exp_first ? r2 : r3;
5007 Register rhs_mantissa = exp_first ? r1 : r0;
5008 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005009 Label one_is_nan, neither_is_nan;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005010 Label lhs_not_nan_exp_mask_is_loaded;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005011
5012 Register exp_mask_reg = r5;
5013
5014 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005015 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
5016 __ cmp(r4, Operand(exp_mask_reg));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005017 __ b(ne, &lhs_not_nan_exp_mask_is_loaded);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005018 __ mov(r4,
5019 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
5020 SetCC);
5021 __ b(ne, &one_is_nan);
5022 __ cmp(lhs_mantissa, Operand(0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005023 __ b(ne, &one_is_nan);
5024
5025 __ bind(lhs_not_nan);
5026 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
5027 __ bind(&lhs_not_nan_exp_mask_is_loaded);
5028 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
5029 __ cmp(r4, Operand(exp_mask_reg));
5030 __ b(ne, &neither_is_nan);
5031 __ mov(r4,
5032 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
5033 SetCC);
5034 __ b(ne, &one_is_nan);
5035 __ cmp(rhs_mantissa, Operand(0));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005036 __ b(eq, &neither_is_nan);
5037
5038 __ bind(&one_is_nan);
5039 // NaN comparisons always fail.
5040 // Load whatever we need in r0 to make the comparison fail.
5041 if (cc == lt || cc == le) {
5042 __ mov(r0, Operand(GREATER));
5043 } else {
5044 __ mov(r0, Operand(LESS));
5045 }
5046 __ mov(pc, Operand(lr)); // Return.
5047
5048 __ bind(&neither_is_nan);
5049}
5050
5051
5052// See comment at call site.
5053static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
5054 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005055 Register rhs_exponent = exp_first ? r0 : r1;
5056 Register lhs_exponent = exp_first ? r2 : r3;
5057 Register rhs_mantissa = exp_first ? r1 : r0;
5058 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005059
5060 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
5061 if (cc == eq) {
5062 // Doubles are not equal unless they have the same bit pattern.
5063 // Exception: 0 and -0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005064 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
5065 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005066 // Return non-zero if the numbers are unequal.
5067 __ mov(pc, Operand(lr), LeaveCC, ne);
5068
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005069 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005070 // If exponents are equal then return 0.
5071 __ mov(pc, Operand(lr), LeaveCC, eq);
5072
5073 // Exponents are unequal. The only way we can return that the numbers
5074 // are equal is if one is -0 and the other is 0. We already dealt
5075 // with the case where both are -0 or both are 0.
5076 // We start by seeing if the mantissas (that are equal) or the bottom
5077 // 31 bits of the rhs exponent are non-zero. If so we return not
5078 // equal.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005079 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005080 __ mov(r0, Operand(r4), LeaveCC, ne);
5081 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
5082 // Now they are equal if and only if the lhs exponent is zero in its
5083 // low 31 bits.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005084 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005085 __ mov(pc, Operand(lr));
5086 } else {
5087 // Call a native function to do a comparison between two non-NaNs.
5088 // Call C routine that may not cause GC or other trouble.
5089 __ mov(r5, Operand(ExternalReference::compare_doubles()));
5090 __ Jump(r5); // Tail call.
5091 }
5092}
5093
5094
5095// See comment at call site.
5096static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
5097 // If either operand is a JSObject or an oddball value, then they are
5098 // not equal since their pointers are different.
5099 // There is no test for undetectability in strict equality.
5100 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
5101 Label first_non_object;
5102 // Get the type of the first operand into r2 and compare it with
5103 // FIRST_JS_OBJECT_TYPE.
5104 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
5105 __ b(lt, &first_non_object);
5106
5107 // Return non-zero (r0 is not zero)
5108 Label return_not_equal;
5109 __ bind(&return_not_equal);
5110 __ mov(pc, Operand(lr)); // Return.
5111
5112 __ bind(&first_non_object);
5113 // Check for oddballs: true, false, null, undefined.
5114 __ cmp(r2, Operand(ODDBALL_TYPE));
5115 __ b(eq, &return_not_equal);
5116
5117 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
5118 __ b(ge, &return_not_equal);
5119
5120 // Check for oddballs: true, false, null, undefined.
5121 __ cmp(r3, Operand(ODDBALL_TYPE));
5122 __ b(eq, &return_not_equal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005123
5124 // Now that we have the types we might as well check for symbol-symbol.
5125 // Ensure that no non-strings have the symbol bit set.
5126 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
5127 ASSERT(kSymbolTag != 0);
5128 __ and_(r2, r2, Operand(r3));
5129 __ tst(r2, Operand(kIsSymbolMask));
5130 __ b(ne, &return_not_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005131}
5132
5133
5134// See comment at call site.
5135static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
5136 Label* both_loaded_as_doubles,
5137 Label* not_heap_numbers,
5138 Label* slow) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005139 __ CompareObjectType(r0, r3, r2, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005140 __ b(ne, not_heap_numbers);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005141 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
5142 __ cmp(r2, r3);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005143 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
5144
5145 // Both are heap numbers. Load them up then jump to the code we have
5146 // for that.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005147 if (CpuFeatures::IsSupported(VFP3)) {
5148 CpuFeatures::Scope scope(VFP3);
5149 __ sub(r7, r0, Operand(kHeapObjectTag));
5150 __ vldr(d6, r7, HeapNumber::kValueOffset);
5151 __ sub(r7, r1, Operand(kHeapObjectTag));
5152 __ vldr(d7, r7, HeapNumber::kValueOffset);
5153 } else {
5154 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
5155 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
5156 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
5157 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
5158 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005159 __ jmp(both_loaded_as_doubles);
5160}
5161
5162
5163// Fast negative check for symbol-to-symbol equality.
5164static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
5165 // r2 is object type of r0.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005166 // Ensure that no non-strings have the symbol bit set.
5167 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
5168 ASSERT(kSymbolTag != 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005169 __ tst(r2, Operand(kIsSymbolMask));
5170 __ b(eq, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005171 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
5172 __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005173 __ tst(r3, Operand(kIsSymbolMask));
5174 __ b(eq, slow);
5175
5176 // Both are symbols. We already checked they weren't the same pointer
5177 // so they are not equal.
5178 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
5179 __ mov(pc, Operand(lr)); // Return.
5180}
5181
5182
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005183// On entry r0 (rhs) and r1 (lhs) are the values to be compared.
5184// On exit r0 is 0, positive or negative to indicate the result of
5185// the comparison.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005186void CompareStub::Generate(MacroAssembler* masm) {
5187 Label slow; // Call builtin.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005188 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005189
5190 // NOTICE! This code is only reached after a smi-fast-case check, so
5191 // it is certain that at least one operand isn't a smi.
5192
5193 // Handle the case where the objects are identical. Either returns the answer
5194 // or goes to slow. Only falls through if the objects were not identical.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005195 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005196
5197 // If either is a Smi (we know that not both are), then they can only
5198 // be strictly equal if the other is a HeapNumber.
5199 ASSERT_EQ(0, kSmiTag);
5200 ASSERT_EQ(0, Smi::FromInt(0));
5201 __ and_(r2, r0, Operand(r1));
5202 __ tst(r2, Operand(kSmiTagMask));
5203 __ b(ne, &not_smis);
5204 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
5205 // 1) Return the answer.
5206 // 2) Go to slow.
5207 // 3) Fall through to both_loaded_as_doubles.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005208 // 4) Jump to lhs_not_nan.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005209 // In cases 3 and 4 we have found out we were dealing with a number-number
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005210 // comparison. If VFP3 is supported the double values of the numbers have
5211 // been loaded into d7 and d6. Otherwise, the double values have been loaded
5212 // into r0, r1, r2, and r3.
5213 EmitSmiNonsmiComparison(masm, &lhs_not_nan, &slow, strict_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005214
5215 __ bind(&both_loaded_as_doubles);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005216 // The arguments have been converted to doubles and stored in d6 and d7, if
5217 // VFP3 is supported, or in r0, r1, r2, and r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005218 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005219 __ bind(&lhs_not_nan);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005220 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005221 Label no_nan;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005222 // ARMv7 VFP3 instructions to implement double precision comparison.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005223 __ vcmp(d7, d6);
5224 __ vmrs(pc); // Move vector status bits to normal status bits.
5225 Label nan;
5226 __ b(vs, &nan);
5227 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
5228 __ mov(r0, Operand(LESS), LeaveCC, lt);
5229 __ mov(r0, Operand(GREATER), LeaveCC, gt);
5230 __ mov(pc, Operand(lr));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005231
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005232 __ bind(&nan);
5233 // If one of the sides was a NaN then the v flag is set. Load r0 with
5234 // whatever it takes to make the comparison fail, since comparisons with NaN
5235 // always fail.
5236 if (cc_ == lt || cc_ == le) {
5237 __ mov(r0, Operand(GREATER));
5238 } else {
5239 __ mov(r0, Operand(LESS));
5240 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005241 __ mov(pc, Operand(lr));
5242 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005243 // Checks for NaN in the doubles we have loaded. Can return the answer or
5244 // fall through if neither is a NaN. Also binds lhs_not_nan.
5245 EmitNanCheck(masm, &lhs_not_nan, cc_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005246 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
5247 // answer. Never falls through.
5248 EmitTwoNonNanDoubleComparison(masm, cc_);
5249 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005250
5251 __ bind(&not_smis);
5252 // At this point we know we are dealing with two different objects,
5253 // and neither of them is a Smi. The objects are in r0 and r1.
5254 if (strict_) {
5255 // This returns non-equal for some object types, or falls through if it
5256 // was not lucky.
5257 EmitStrictTwoHeapObjectCompare(masm);
5258 }
5259
5260 Label check_for_symbols;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005261 Label flat_string_check;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005262 // Check for heap-number-heap-number comparison. Can jump to slow case,
5263 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
5264 // that case. If the inputs are not doubles then jumps to check_for_symbols.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005265 // In this case r2 will contain the type of r0. Never falls through.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005266 EmitCheckForTwoHeapNumbers(masm,
5267 &both_loaded_as_doubles,
5268 &check_for_symbols,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005269 &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005270
5271 __ bind(&check_for_symbols);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005272 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
5273 // symbols.
5274 if (cc_ == eq && !strict_) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005275 // Either jumps to slow or returns the answer. Assumes that r2 is the type
5276 // of r0 on entry.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005277 EmitCheckForSymbols(masm, &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005278 }
5279
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005280 // Check for both being sequential ASCII strings, and inline if that is the
5281 // case.
5282 __ bind(&flat_string_check);
5283
5284 __ JumpIfNonSmisNotBothSequentialAsciiStrings(r0, r1, r2, r3, &slow);
5285
5286 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
5287 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
5288 r1,
5289 r0,
5290 r2,
5291 r3,
5292 r4,
5293 r5);
5294 // Never falls through to here.
5295
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005296 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005297
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005298 __ push(r1);
5299 __ push(r0);
5300 // Figure out which native to call and setup the arguments.
5301 Builtins::JavaScript native;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005302 if (cc_ == eq) {
5303 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
5304 } else {
5305 native = Builtins::COMPARE;
5306 int ncr; // NaN compare result
5307 if (cc_ == lt || cc_ == le) {
5308 ncr = GREATER;
5309 } else {
5310 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
5311 ncr = LESS;
5312 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005313 __ mov(r0, Operand(Smi::FromInt(ncr)));
5314 __ push(r0);
5315 }
5316
5317 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
5318 // tagged as a small integer.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005319 __ InvokeBuiltin(native, JUMP_JS);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005320}
5321
5322
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005323// Allocates a heap number or jumps to the label if the young space is full and
5324// a scavenge is needed.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005325static void AllocateHeapNumber(
5326 MacroAssembler* masm,
5327 Label* need_gc, // Jump here if young space is full.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005328 Register result, // The tagged address of the new heap number.
5329 Register scratch1, // A scratch register.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005330 Register scratch2) { // Another scratch register.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005331 // Allocate an object in the heap for the heap number and tag it as a heap
5332 // object.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005333 __ AllocateInNewSpace(HeapNumber::kSize / kPointerSize,
5334 result,
5335 scratch1,
5336 scratch2,
5337 need_gc,
5338 TAG_OBJECT);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005339
ager@chromium.org18ad94b2009-09-02 08:22:29 +00005340 // Get heap number map and store it in the allocated object.
5341 __ LoadRoot(scratch1, Heap::kHeapNumberMapRootIndex);
5342 __ str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005343}
5344
5345
5346// We fall into this code if the operands were Smis, but the result was
5347// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005348// the operands were not both Smi. The operands are in r0 and r1. In order
5349// to call the C-implemented binary fp operation routines we need to end up
5350// with the double precision floating point operands in r0 and r1 (for the
5351// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005352static void HandleBinaryOpSlowCases(MacroAssembler* masm,
5353 Label* not_smi,
5354 const Builtins::JavaScript& builtin,
5355 Token::Value operation,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005356 OverwriteMode mode) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005357 Label slow, slow_pop_2_first, do_the_call;
5358 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
5359 // Smi-smi case (overflow).
5360 // Since both are Smis there is no heap number to overwrite, so allocate.
5361 // The new heap number is in r5. r6 and r7 are scratch.
5362 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005363
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005364 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
5365 // using registers d7 and d6 for the double values.
5366 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) &&
5367 Token::MOD != operation;
5368 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005369 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005370 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
5371 __ vmov(s15, r7);
5372 __ vcvt(d7, s15);
5373 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
5374 __ vmov(s13, r7);
5375 __ vcvt(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005376 } else {
5377 // Write Smi from r0 to r3 and r2 in double format. r6 is scratch.
5378 __ mov(r7, Operand(r0));
5379 ConvertToDoubleStub stub1(r3, r2, r7, r6);
5380 __ push(lr);
5381 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
5382 // Write Smi from r1 to r1 and r0 in double format. r6 is scratch.
5383 __ mov(r7, Operand(r1));
5384 ConvertToDoubleStub stub2(r1, r0, r7, r6);
5385 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
5386 __ pop(lr);
5387 }
5388
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005389 __ jmp(&do_the_call); // Tail call. No return.
5390
5391 // We jump to here if something goes wrong (one param is not a number of any
5392 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005393 __ bind(&slow);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005394
5395 // Push arguments to the stack
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005396 __ push(r1);
5397 __ push(r0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005398
5399 if (Token::ADD == operation) {
5400 // Test for string arguments before calling runtime.
5401 // r1 : first argument
5402 // r0 : second argument
5403 // sp[0] : second argument
ager@chromium.org5c838252010-02-19 08:53:10 +00005404 // sp[4] : first argument
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005405
5406 Label not_strings, not_string1, string1;
5407 __ tst(r1, Operand(kSmiTagMask));
5408 __ b(eq, &not_string1);
5409 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
5410 __ b(ge, &not_string1);
5411
5412 // First argument is a a string, test second.
5413 __ tst(r0, Operand(kSmiTagMask));
5414 __ b(eq, &string1);
5415 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
5416 __ b(ge, &string1);
5417
5418 // First and second argument are strings.
ager@chromium.org5c838252010-02-19 08:53:10 +00005419 StringAddStub stub(NO_STRING_CHECK_IN_STUB);
5420 __ TailCallStub(&stub);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005421
5422 // Only first argument is a string.
5423 __ bind(&string1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005424 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
5425
5426 // First argument was not a string, test second.
5427 __ bind(&not_string1);
5428 __ tst(r0, Operand(kSmiTagMask));
5429 __ b(eq, &not_strings);
5430 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
5431 __ b(ge, &not_strings);
5432
5433 // Only second argument is a string.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00005434 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
5435
5436 __ bind(&not_strings);
5437 }
5438
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005439 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005440
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005441 // We branch here if at least one of r0 and r1 is not a Smi.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005442 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005443 if (mode == NO_OVERWRITE) {
5444 // In the case where there is no chance of an overwritable float we may as
5445 // well do the allocation immediately while r0 and r1 are untouched.
5446 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5447 }
5448
5449 // Move r0 to a double in r2-r3.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005450 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005451 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5452 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005453 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005454 if (mode == OVERWRITE_RIGHT) {
5455 __ mov(r5, Operand(r0)); // Overwrite this heap number.
5456 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005457 if (use_fp_registers) {
5458 CpuFeatures::Scope scope(VFP3);
5459 // Load the double from tagged HeapNumber r0 to d7.
5460 __ sub(r7, r0, Operand(kHeapObjectTag));
5461 __ vldr(d7, r7, HeapNumber::kValueOffset);
5462 } else {
5463 // Calling convention says that second double is in r2 and r3.
5464 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
5465 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
5466 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005467 __ jmp(&finished_loading_r0);
5468 __ bind(&r0_is_smi);
5469 if (mode == OVERWRITE_RIGHT) {
5470 // We can't overwrite a Smi so get address of new heap number into r5.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005471 AllocateHeapNumber(masm, &slow, r5, r6, r7);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005472 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005473
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005474 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005475 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005476 // Convert smi in r0 to double in d7.
5477 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
5478 __ vmov(s15, r7);
5479 __ vcvt(d7, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005480 } else {
5481 // Write Smi from r0 to r3 and r2 in double format.
5482 __ mov(r7, Operand(r0));
5483 ConvertToDoubleStub stub3(r3, r2, r7, r6);
5484 __ push(lr);
5485 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
5486 __ pop(lr);
5487 }
5488
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005489 __ bind(&finished_loading_r0);
5490
5491 // Move r1 to a double in r0-r1.
5492 __ tst(r1, Operand(kSmiTagMask));
5493 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5494 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5495 __ b(ne, &slow);
5496 if (mode == OVERWRITE_LEFT) {
5497 __ mov(r5, Operand(r1)); // Overwrite this heap number.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005498 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005499 if (use_fp_registers) {
5500 CpuFeatures::Scope scope(VFP3);
5501 // Load the double from tagged HeapNumber r1 to d6.
5502 __ sub(r7, r1, Operand(kHeapObjectTag));
5503 __ vldr(d6, r7, HeapNumber::kValueOffset);
5504 } else {
5505 // Calling convention says that first double is in r0 and r1.
5506 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
5507 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
5508 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005509 __ jmp(&finished_loading_r1);
5510 __ bind(&r1_is_smi);
5511 if (mode == OVERWRITE_LEFT) {
5512 // We can't overwrite a Smi so get address of new heap number into r5.
5513 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5514 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005515
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005516 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005517 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005518 // Convert smi in r1 to double in d6.
5519 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
5520 __ vmov(s13, r7);
5521 __ vcvt(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005522 } else {
5523 // Write Smi from r1 to r1 and r0 in double format.
5524 __ mov(r7, Operand(r1));
5525 ConvertToDoubleStub stub4(r1, r0, r7, r6);
5526 __ push(lr);
5527 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
5528 __ pop(lr);
5529 }
5530
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005531 __ bind(&finished_loading_r1);
5532
5533 __ bind(&do_the_call);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005534 // If we are inlining the operation using VFP3 instructions for
5535 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
5536 if (use_fp_registers) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005537 CpuFeatures::Scope scope(VFP3);
5538 // ARMv7 VFP3 instructions to implement
5539 // double precision, add, subtract, multiply, divide.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005540
5541 if (Token::MUL == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005542 __ vmul(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005543 } else if (Token::DIV == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005544 __ vdiv(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005545 } else if (Token::ADD == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005546 __ vadd(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005547 } else if (Token::SUB == operation) {
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005548 __ vsub(d5, d6, d7);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005549 } else {
5550 UNREACHABLE();
5551 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005552 __ sub(r0, r5, Operand(kHeapObjectTag));
5553 __ vstr(d5, r0, HeapNumber::kValueOffset);
5554 __ add(r0, r0, Operand(kHeapObjectTag));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005555 __ mov(pc, lr);
5556 return;
5557 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005558
5559 // If we did not inline the operation, then the arguments are in:
5560 // r0: Left value (least significant part of mantissa).
5561 // r1: Left value (sign, exponent, top of mantissa).
5562 // r2: Right value (least significant part of mantissa).
5563 // r3: Right value (sign, exponent, top of mantissa).
5564 // r5: Address of heap number for result.
5565
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005566 __ push(lr); // For later.
5567 __ push(r5); // Address of heap number that is answer.
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005568 __ AlignStack(0);
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005569 // Call C routine that may not cause GC or other trouble.
5570 __ mov(r5, Operand(ExternalReference::double_fp_operation(operation)));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005571 __ Call(r5);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00005572 __ pop(r4); // Address of heap number.
5573 __ cmp(r4, Operand(Smi::FromInt(0)));
5574 __ pop(r4, eq); // Conditional pop instruction to get rid of alignment push.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005575 // Store answer in the overwritable heap number.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005576#if !defined(USE_ARM_EABI)
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005577 // Double returned in fp coprocessor register 0 and 1, encoded as register
5578 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
5579 // substract the tag from r4.
5580 __ sub(r5, r4, Operand(kHeapObjectTag));
5581 __ stc(p1, cr8, MemOperand(r5, HeapNumber::kValueOffset));
5582#else
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005583 // Double returned in registers 0 and 1.
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005584 __ str(r0, FieldMemOperand(r4, HeapNumber::kValueOffset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005585 __ str(r1, FieldMemOperand(r4, HeapNumber::kValueOffset + 4));
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005586#endif
5587 __ mov(r0, Operand(r4));
5588 // And we are done.
5589 __ pop(pc);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005590}
5591
5592
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005593// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005594// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005595// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
5596// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005597// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
5598// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005599static void GetInt32(MacroAssembler* masm,
5600 Register source,
5601 Register dest,
5602 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005603 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005604 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005605 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005606 // Get exponent word.
5607 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
5608 // Get exponent alone in scratch2.
5609 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005610 // Load dest with zero. We use this either for the final shift or
5611 // for the answer.
5612 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005613 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005614 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
5615 // the exponent that we are fastest at and also the highest exponent we can
5616 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005617 const uint32_t non_smi_exponent =
5618 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
5619 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005620 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
5621 __ b(eq, &right_exponent);
5622 // If the exponent is higher than that then go to slow case. This catches
5623 // numbers that don't fit in a signed int32, infinities and NaNs.
5624 __ b(gt, slow);
5625
5626 // We know the exponent is smaller than 30 (biased). If it is less than
5627 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
5628 // it rounds to zero.
5629 const uint32_t zero_exponent =
5630 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
5631 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
5632 // Dest already has a Smi zero.
5633 __ b(lt, &done);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005634 if (!CpuFeatures::IsSupported(VFP3)) {
5635 // We have a shifted exponent between 0 and 30 in scratch2.
5636 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
5637 // We now have the exponent in dest. Subtract from 30 to get
5638 // how much to shift down.
5639 __ rsb(dest, dest, Operand(30));
5640 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005641 __ bind(&right_exponent);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005642 if (CpuFeatures::IsSupported(VFP3)) {
5643 CpuFeatures::Scope scope(VFP3);
5644 // ARMv7 VFP3 instructions implementing double precision to integer
5645 // conversion using round to zero.
5646 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005647 __ vmov(d7, scratch2, scratch);
5648 __ vcvt(s15, d7);
5649 __ vmov(dest, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005650 } else {
5651 // Get the top bits of the mantissa.
5652 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
5653 // Put back the implicit 1.
5654 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
5655 // Shift up the mantissa bits to take up the space the exponent used to
5656 // take. We just orred in the implicit bit so that took care of one and
5657 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
5658 // distance.
5659 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
5660 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
5661 // Put sign in zero flag.
5662 __ tst(scratch, Operand(HeapNumber::kSignMask));
5663 // Get the second half of the double. For some exponents we don't
5664 // actually need this because the bits get shifted out again, but
5665 // it's probably slower to test than just to do it.
5666 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
5667 // Shift down 22 bits to get the last 10 bits.
5668 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
5669 // Move down according to the exponent.
5670 __ mov(dest, Operand(scratch, LSR, dest));
5671 // Fix sign if sign bit was set.
5672 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
5673 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00005674 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005675}
5676
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005677// For bitwise ops where the inputs are not both Smis we here try to determine
5678// whether both inputs are either Smis or at least heap numbers that can be
5679// represented by a 32 bit signed value. We truncate towards zero as required
5680// by the ES spec. If this is the case we do the bitwise op and see if the
5681// result is a Smi. If so, great, otherwise we try to find a heap number to
5682// write the answer into (either by allocating or by overwriting).
5683// On entry the operands are in r0 and r1. On exit the answer is in r0.
5684void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm) {
5685 Label slow, result_not_a_smi;
5686 Label r0_is_smi, r1_is_smi;
5687 Label done_checking_r0, done_checking_r1;
5688
5689 __ tst(r1, Operand(kSmiTagMask));
5690 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
5691 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5692 __ b(ne, &slow);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005693 GetInt32(masm, r1, r3, r5, r4, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005694 __ jmp(&done_checking_r1);
5695 __ bind(&r1_is_smi);
5696 __ mov(r3, Operand(r1, ASR, 1));
5697 __ bind(&done_checking_r1);
5698
5699 __ tst(r0, Operand(kSmiTagMask));
5700 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
5701 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5702 __ b(ne, &slow);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005703 GetInt32(masm, r0, r2, r5, r4, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005704 __ jmp(&done_checking_r0);
5705 __ bind(&r0_is_smi);
5706 __ mov(r2, Operand(r0, ASR, 1));
5707 __ bind(&done_checking_r0);
5708
5709 // r0 and r1: Original operands (Smi or heap numbers).
5710 // r2 and r3: Signed int32 operands.
5711 switch (op_) {
5712 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
5713 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
5714 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
5715 case Token::SAR:
5716 // Use only the 5 least significant bits of the shift count.
5717 __ and_(r2, r2, Operand(0x1f));
5718 __ mov(r2, Operand(r3, ASR, r2));
5719 break;
5720 case Token::SHR:
5721 // Use only the 5 least significant bits of the shift count.
5722 __ and_(r2, r2, Operand(0x1f));
5723 __ mov(r2, Operand(r3, LSR, r2), SetCC);
5724 // SHR is special because it is required to produce a positive answer.
5725 // The code below for writing into heap numbers isn't capable of writing
5726 // the register as an unsigned int so we go to slow case if we hit this
5727 // case.
5728 __ b(mi, &slow);
5729 break;
5730 case Token::SHL:
5731 // Use only the 5 least significant bits of the shift count.
5732 __ and_(r2, r2, Operand(0x1f));
5733 __ mov(r2, Operand(r3, LSL, r2));
5734 break;
5735 default: UNREACHABLE();
5736 }
5737 // check that the *signed* result fits in a smi
5738 __ add(r3, r2, Operand(0x40000000), SetCC);
5739 __ b(mi, &result_not_a_smi);
5740 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
5741 __ Ret();
5742
5743 Label have_to_allocate, got_a_heap_number;
5744 __ bind(&result_not_a_smi);
5745 switch (mode_) {
5746 case OVERWRITE_RIGHT: {
5747 __ tst(r0, Operand(kSmiTagMask));
5748 __ b(eq, &have_to_allocate);
5749 __ mov(r5, Operand(r0));
5750 break;
5751 }
5752 case OVERWRITE_LEFT: {
5753 __ tst(r1, Operand(kSmiTagMask));
5754 __ b(eq, &have_to_allocate);
5755 __ mov(r5, Operand(r1));
5756 break;
5757 }
5758 case NO_OVERWRITE: {
5759 // Get a new heap number in r5. r6 and r7 are scratch.
5760 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5761 }
5762 default: break;
5763 }
5764 __ bind(&got_a_heap_number);
5765 // r2: Answer as signed int32.
5766 // r5: Heap number to write answer into.
5767
5768 // Nothing can go wrong now, so move the heap number to r0, which is the
5769 // result.
5770 __ mov(r0, Operand(r5));
5771
5772 // Tail call that writes the int32 in r2 to the heap number in r0, using
5773 // r3 as scratch. r0 is preserved and returned.
5774 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
5775 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
5776
5777 if (mode_ != NO_OVERWRITE) {
5778 __ bind(&have_to_allocate);
5779 // Get a new heap number in r5. r6 and r7 are scratch.
5780 AllocateHeapNumber(masm, &slow, r5, r6, r7);
5781 __ jmp(&got_a_heap_number);
5782 }
5783
5784 // If all else failed then we go to the runtime system.
5785 __ bind(&slow);
5786 __ push(r1); // restore stack
5787 __ push(r0);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005788 switch (op_) {
5789 case Token::BIT_OR:
5790 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
5791 break;
5792 case Token::BIT_AND:
5793 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
5794 break;
5795 case Token::BIT_XOR:
5796 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
5797 break;
5798 case Token::SAR:
5799 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
5800 break;
5801 case Token::SHR:
5802 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
5803 break;
5804 case Token::SHL:
5805 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
5806 break;
5807 default:
5808 UNREACHABLE();
5809 }
5810}
5811
5812
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005813// Can we multiply by x with max two shifts and an add.
5814// This answers yes to all integers from 2 to 10.
5815static bool IsEasyToMultiplyBy(int x) {
5816 if (x < 2) return false; // Avoid special cases.
5817 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
5818 if (IsPowerOf2(x)) return true; // Simple shift.
5819 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
5820 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
5821 return false;
5822}
5823
5824
5825// Can multiply by anything that IsEasyToMultiplyBy returns true for.
5826// Source and destination may be the same register. This routine does
5827// not set carry and overflow the way a mul instruction would.
5828static void MultiplyByKnownInt(MacroAssembler* masm,
5829 Register source,
5830 Register destination,
5831 int known_int) {
5832 if (IsPowerOf2(known_int)) {
5833 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
5834 } else if (PopCountLessThanEqual2(known_int)) {
5835 int first_bit = BitPosition(known_int);
5836 int second_bit = BitPosition(known_int ^ (1 << first_bit));
5837 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
5838 if (first_bit != 0) {
5839 __ mov(destination, Operand(destination, LSL, first_bit));
5840 }
5841 } else {
5842 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
5843 int the_bit = BitPosition(known_int + 1);
5844 __ rsb(destination, source, Operand(source, LSL, the_bit));
5845 }
5846}
5847
5848
5849// This function (as opposed to MultiplyByKnownInt) takes the known int in a
5850// a register for the cases where it doesn't know a good trick, and may deliver
5851// a result that needs shifting.
5852static void MultiplyByKnownInt2(
5853 MacroAssembler* masm,
5854 Register result,
5855 Register source,
5856 Register known_int_register, // Smi tagged.
5857 int known_int,
5858 int* required_shift) { // Including Smi tag shift
5859 switch (known_int) {
5860 case 3:
5861 __ add(result, source, Operand(source, LSL, 1));
5862 *required_shift = 1;
5863 break;
5864 case 5:
5865 __ add(result, source, Operand(source, LSL, 2));
5866 *required_shift = 1;
5867 break;
5868 case 6:
5869 __ add(result, source, Operand(source, LSL, 1));
5870 *required_shift = 2;
5871 break;
5872 case 7:
5873 __ rsb(result, source, Operand(source, LSL, 3));
5874 *required_shift = 1;
5875 break;
5876 case 9:
5877 __ add(result, source, Operand(source, LSL, 3));
5878 *required_shift = 1;
5879 break;
5880 case 10:
5881 __ add(result, source, Operand(source, LSL, 2));
5882 *required_shift = 2;
5883 break;
5884 default:
5885 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
5886 __ mul(result, source, known_int_register);
5887 *required_shift = 0;
5888 }
5889}
5890
5891
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00005892const char* GenericBinaryOpStub::GetName() {
5893 if (name_ != NULL) return name_;
5894 const int len = 100;
5895 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
5896 if (name_ == NULL) return "OOM";
5897 const char* op_name = Token::Name(op_);
5898 const char* overwrite_name;
5899 switch (mode_) {
5900 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
5901 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
5902 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
5903 default: overwrite_name = "UnknownOverwrite"; break;
5904 }
5905
5906 OS::SNPrintF(Vector<char>(name_, len),
5907 "GenericBinaryOpStub_%s_%s%s",
5908 op_name,
5909 overwrite_name,
5910 specialized_on_rhs_ ? "_ConstantRhs" : 0);
5911 return name_;
5912}
5913
5914
ager@chromium.org5c838252010-02-19 08:53:10 +00005915
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005916void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
5917 // r1 : x
5918 // r0 : y
5919 // result : r0
5920
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005921 // All ops need to know whether we are dealing with two Smis. Set up r2 to
5922 // tell us that.
5923 __ orr(r2, r1, Operand(r0)); // r2 = x | y;
5924
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005925 switch (op_) {
5926 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005927 Label not_smi;
5928 // Fast path.
5929 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005930 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005931 __ b(ne, &not_smi);
5932 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
5933 // Return if no overflow.
5934 __ Ret(vc);
5935 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
5936
5937 HandleBinaryOpSlowCases(masm,
5938 &not_smi,
5939 Builtins::ADD,
5940 Token::ADD,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005941 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005942 break;
5943 }
5944
5945 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005946 Label not_smi;
5947 // Fast path.
5948 ASSERT(kSmiTag == 0); // Adjust code below.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005949 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005950 __ b(ne, &not_smi);
5951 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
5952 // Return if no overflow.
5953 __ Ret(vc);
5954 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
5955
5956 HandleBinaryOpSlowCases(masm,
5957 &not_smi,
5958 Builtins::SUB,
5959 Token::SUB,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005960 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005961 break;
5962 }
5963
5964 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005965 Label not_smi, slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005966 ASSERT(kSmiTag == 0); // adjust code below
5967 __ tst(r2, Operand(kSmiTagMask));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005968 __ b(ne, &not_smi);
5969 // Remove tag from one operand (but keep sign), so that result is Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005970 __ mov(ip, Operand(r0, ASR, kSmiTagSize));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005971 // Do multiplication
5972 __ smull(r3, r2, r1, ip); // r3 = lower 32 bits of ip*r1.
5973 // Go slow on overflows (overflow bit is not set).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005974 __ mov(ip, Operand(r3, ASR, 31));
5975 __ cmp(ip, Operand(r2)); // no overflow if higher 33 bits are identical
5976 __ b(ne, &slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005977 // Go slow on zero result to handle -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005978 __ tst(r3, Operand(r3));
5979 __ mov(r0, Operand(r3), LeaveCC, ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005980 __ Ret(ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005981 // We need -0 if we were multiplying a negative number with 0 to get 0.
5982 // We know one of them was zero.
5983 __ add(r2, r0, Operand(r1), SetCC);
5984 __ mov(r0, Operand(Smi::FromInt(0)), LeaveCC, pl);
5985 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
5986 // Slow case. We fall through here if we multiplied a negative number
5987 // with 0, because that would mean we should produce -0.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005988 __ bind(&slow);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00005989
5990 HandleBinaryOpSlowCases(masm,
5991 &not_smi,
5992 Builtins::MUL,
5993 Token::MUL,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005994 mode_);
5995 break;
5996 }
5997
5998 case Token::DIV:
5999 case Token::MOD: {
6000 Label not_smi;
6001 if (specialized_on_rhs_) {
6002 Label smi_is_unsuitable;
6003 __ BranchOnNotSmi(r1, &not_smi);
6004 if (IsPowerOf2(constant_rhs_)) {
6005 if (op_ == Token::MOD) {
6006 __ and_(r0,
6007 r1,
6008 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
6009 SetCC);
6010 // We now have the answer, but if the input was negative we also
6011 // have the sign bit. Our work is done if the result is
6012 // positive or zero:
6013 __ Ret(pl);
6014 // A mod of a negative left hand side must return a negative number.
6015 // Unfortunately if the answer is 0 then we must return -0. And we
6016 // already optimistically trashed r0 so we may need to restore it.
6017 __ eor(r0, r0, Operand(0x80000000u), SetCC);
6018 // Next two instructions are conditional on the answer being -0.
6019 __ mov(r0, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
6020 __ b(eq, &smi_is_unsuitable);
6021 // We need to subtract the dividend. Eg. -3 % 4 == -3.
6022 __ sub(r0, r0, Operand(Smi::FromInt(constant_rhs_)));
6023 } else {
6024 ASSERT(op_ == Token::DIV);
6025 __ tst(r1,
6026 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
6027 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
6028 int shift = 0;
6029 int d = constant_rhs_;
6030 while ((d & 1) == 0) {
6031 d >>= 1;
6032 shift++;
6033 }
6034 __ mov(r0, Operand(r1, LSR, shift));
6035 __ bic(r0, r0, Operand(kSmiTagMask));
6036 }
6037 } else {
6038 // Not a power of 2.
6039 __ tst(r1, Operand(0x80000000u));
6040 __ b(ne, &smi_is_unsuitable);
6041 // Find a fixed point reciprocal of the divisor so we can divide by
6042 // multiplying.
6043 double divisor = 1.0 / constant_rhs_;
6044 int shift = 32;
6045 double scale = 4294967296.0; // 1 << 32.
6046 uint32_t mul;
6047 // Maximise the precision of the fixed point reciprocal.
6048 while (true) {
6049 mul = static_cast<uint32_t>(scale * divisor);
6050 if (mul >= 0x7fffffff) break;
6051 scale *= 2.0;
6052 shift++;
6053 }
6054 mul++;
6055 __ mov(r2, Operand(mul));
6056 __ umull(r3, r2, r2, r1);
6057 __ mov(r2, Operand(r2, LSR, shift - 31));
6058 // r2 is r1 / rhs. r2 is not Smi tagged.
6059 // r0 is still the known rhs. r0 is Smi tagged.
6060 // r1 is still the unkown lhs. r1 is Smi tagged.
6061 int required_r4_shift = 0; // Including the Smi tag shift of 1.
6062 // r4 = r2 * r0.
6063 MultiplyByKnownInt2(masm,
6064 r4,
6065 r2,
6066 r0,
6067 constant_rhs_,
6068 &required_r4_shift);
6069 // r4 << required_r4_shift is now the Smi tagged rhs * (r1 / rhs).
6070 if (op_ == Token::DIV) {
6071 __ sub(r3, r1, Operand(r4, LSL, required_r4_shift), SetCC);
6072 __ b(ne, &smi_is_unsuitable); // There was a remainder.
6073 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
6074 } else {
6075 ASSERT(op_ == Token::MOD);
6076 __ sub(r0, r1, Operand(r4, LSL, required_r4_shift));
6077 }
6078 }
6079 __ Ret();
6080 __ bind(&smi_is_unsuitable);
6081 } else {
6082 __ jmp(&not_smi);
6083 }
6084 HandleBinaryOpSlowCases(masm,
6085 &not_smi,
6086 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV,
6087 op_,
6088 mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006089 break;
6090 }
6091
6092 case Token::BIT_OR:
6093 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006094 case Token::BIT_XOR:
6095 case Token::SAR:
6096 case Token::SHR:
6097 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006098 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006099 ASSERT(kSmiTag == 0); // adjust code below
6100 __ tst(r2, Operand(kSmiTagMask));
6101 __ b(ne, &slow);
6102 switch (op_) {
6103 case Token::BIT_OR: __ orr(r0, r0, Operand(r1)); break;
6104 case Token::BIT_AND: __ and_(r0, r0, Operand(r1)); break;
6105 case Token::BIT_XOR: __ eor(r0, r0, Operand(r1)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006106 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006107 // Remove tags from right operand.
ager@chromium.org5c838252010-02-19 08:53:10 +00006108 __ GetLeastBitsFromSmi(r2, r0, 5);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006109 __ mov(r0, Operand(r1, ASR, r2));
6110 // Smi tag result.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006111 __ bic(r0, r0, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006112 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006113 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006114 // Remove tags from operands. We can't do this on a 31 bit number
6115 // because then the 0s get shifted into bit 30 instead of bit 31.
6116 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
ager@chromium.org5c838252010-02-19 08:53:10 +00006117 __ GetLeastBitsFromSmi(r2, r0, 5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006118 __ mov(r3, Operand(r3, LSR, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006119 // Unsigned shift is not allowed to produce a negative number, so
6120 // check the sign bit and the sign bit after Smi tagging.
6121 __ tst(r3, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006122 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006123 // Smi tag result.
6124 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006125 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006126 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006127 // Remove tags from operands.
6128 __ mov(r3, Operand(r1, ASR, kSmiTagSize)); // x
ager@chromium.org5c838252010-02-19 08:53:10 +00006129 __ GetLeastBitsFromSmi(r2, r0, 5);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006130 __ mov(r3, Operand(r3, LSL, r2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006131 // Check that the signed result fits in a Smi.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006132 __ add(r2, r3, Operand(0x40000000), SetCC);
6133 __ b(mi, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006134 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006135 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006136 default: UNREACHABLE();
6137 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006138 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006139 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006140 HandleNonSmiBitwiseOp(masm);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006141 break;
6142 }
6143
6144 default: UNREACHABLE();
6145 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006146 // This code should be unreachable.
6147 __ stop("Unreachable");
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006148}
6149
6150
6151void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00006152 // Do tail-call to runtime routine. Runtime routines expect at least one
6153 // argument, so give it a Smi.
6154 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006155 __ push(r0);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006156 __ TailCallRuntime(ExternalReference(Runtime::kStackGuard), 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006157
6158 __ StubReturn(1);
6159}
6160
6161
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006162void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006163 Label slow, done;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006164
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006165 if (op_ == Token::SUB) {
6166 // Check whether the value is a smi.
6167 Label try_float;
6168 __ tst(r0, Operand(kSmiTagMask));
6169 __ b(ne, &try_float);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006170
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006171 // Go slow case if the value of the expression is zero
6172 // to make sure that we switch between 0 and -0.
6173 __ cmp(r0, Operand(0));
6174 __ b(eq, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006175
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006176 // The value of the expression is a smi that is not zero. Try
6177 // optimistic subtraction '0 - value'.
6178 __ rsb(r1, r0, Operand(0), SetCC);
6179 __ b(vs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006180
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006181 __ mov(r0, Operand(r1)); // Set r0 to result.
6182 __ b(&done);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006183
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006184 __ bind(&try_float);
6185 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
6186 __ b(ne, &slow);
6187 // r0 is a heap number. Get a new heap number in r1.
6188 if (overwrite_) {
6189 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6190 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
6191 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6192 } else {
6193 AllocateHeapNumber(masm, &slow, r1, r2, r3);
6194 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
6195 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6196 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
6197 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
6198 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
6199 __ mov(r0, Operand(r1));
6200 }
6201 } else if (op_ == Token::BIT_NOT) {
6202 // Check if the operand is a heap number.
6203 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
6204 __ b(ne, &slow);
6205
6206 // Convert the heap number is r0 to an untagged integer in r1.
6207 GetInt32(masm, r0, r1, r2, r3, &slow);
6208
6209 // Do the bitwise operation (move negated) and check if the result
6210 // fits in a smi.
6211 Label try_float;
6212 __ mvn(r1, Operand(r1));
6213 __ add(r2, r1, Operand(0x40000000), SetCC);
6214 __ b(mi, &try_float);
6215 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
6216 __ b(&done);
6217
6218 __ bind(&try_float);
6219 if (!overwrite_) {
6220 // Allocate a fresh heap number, but don't overwrite r0 until
6221 // we're sure we can do it without going through the slow case
6222 // that needs the value in r0.
6223 AllocateHeapNumber(masm, &slow, r2, r3, r4);
6224 __ mov(r0, Operand(r2));
6225 }
6226
6227 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
6228 // have to set up a frame.
6229 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
6230 __ push(lr);
6231 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
6232 __ pop(lr);
6233 } else {
6234 UNIMPLEMENTED();
6235 }
6236
6237 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006238 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006239
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006240 // Handle the slow case by jumping to the JavaScript builtin.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006241 __ bind(&slow);
6242 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006243 switch (op_) {
6244 case Token::SUB:
6245 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
6246 break;
6247 case Token::BIT_NOT:
6248 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
6249 break;
6250 default:
6251 UNREACHABLE();
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006252 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00006253}
6254
6255
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006256void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006257 // r0 holds the exception.
6258
6259 // Adjust this code if not the case.
6260 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
6261
6262 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006263 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
6264 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006265
6266 // Restore the next handler and frame pointer, discard handler state.
6267 ASSERT(StackHandlerConstants::kNextOffset == 0);
6268 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006269 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006270 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
6271 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
6272
6273 // Before returning we restore the context from the frame pointer if
6274 // not NULL. The frame pointer is NULL in the exception handler of a
6275 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006276 __ cmp(fp, Operand(0));
6277 // Set cp to NULL if fp is NULL.
6278 __ mov(cp, Operand(0), LeaveCC, eq);
6279 // Restore cp otherwise.
6280 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006281#ifdef DEBUG
6282 if (FLAG_debug_code) {
6283 __ mov(lr, Operand(pc));
6284 }
6285#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006286 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006287 __ pop(pc);
6288}
6289
6290
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006291void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
6292 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006293 // Adjust this code if not the case.
6294 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
6295
6296 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006297 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006298 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006299
6300 // Unwind the handlers until the ENTRY handler is found.
6301 Label loop, done;
6302 __ bind(&loop);
6303 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006304 const int kStateOffset = StackHandlerConstants::kStateOffset;
6305 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006306 __ cmp(r2, Operand(StackHandler::ENTRY));
6307 __ b(eq, &done);
6308 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006309 const int kNextOffset = StackHandlerConstants::kNextOffset;
6310 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006311 __ jmp(&loop);
6312 __ bind(&done);
6313
6314 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006315 ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006316 __ pop(r2);
6317 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006318
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006319 if (type == OUT_OF_MEMORY) {
6320 // Set external caught exception to false.
6321 ExternalReference external_caught(Top::k_external_caught_exception_address);
6322 __ mov(r0, Operand(false));
6323 __ mov(r2, Operand(external_caught));
6324 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006325
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006326 // Set pending exception and r0 to out of memory exception.
6327 Failure* out_of_memory = Failure::OutOfMemoryException();
6328 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
6329 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
6330 __ str(r0, MemOperand(r2));
6331 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006332
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006333 // Stack layout at this point. See also StackHandlerConstants.
6334 // sp -> state (ENTRY)
6335 // fp
6336 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006337
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006338 // Discard handler state (r2 is not used) and restore frame pointer.
6339 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
6340 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
6341 // Before returning we restore the context from the frame pointer if
6342 // not NULL. The frame pointer is NULL in the exception handler of a
6343 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006344 __ cmp(fp, Operand(0));
6345 // Set cp to NULL if fp is NULL.
6346 __ mov(cp, Operand(0), LeaveCC, eq);
6347 // Restore cp otherwise.
6348 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006349#ifdef DEBUG
6350 if (FLAG_debug_code) {
6351 __ mov(lr, Operand(pc));
6352 }
6353#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006354 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006355 __ pop(pc);
6356}
6357
6358
6359void CEntryStub::GenerateCore(MacroAssembler* masm,
6360 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006361 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006362 Label* throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006363 bool do_gc,
6364 bool always_allocate) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006365 // r0: result parameter for PerformGC, if any
6366 // r4: number of arguments including receiver (C callee-saved)
6367 // r5: pointer to builtin function (C callee-saved)
6368 // r6: pointer to the first argument (C callee-saved)
6369
6370 if (do_gc) {
6371 // Passing r0.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006372 ExternalReference gc_reference = ExternalReference::perform_gc_function();
6373 __ Call(gc_reference.address(), RelocInfo::RUNTIME_ENTRY);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006374 }
6375
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006376 ExternalReference scope_depth =
6377 ExternalReference::heap_always_allocate_scope_depth();
6378 if (always_allocate) {
6379 __ mov(r0, Operand(scope_depth));
6380 __ ldr(r1, MemOperand(r0));
6381 __ add(r1, r1, Operand(1));
6382 __ str(r1, MemOperand(r0));
6383 }
6384
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006385 // Call C built-in.
6386 // r0 = argc, r1 = argv
6387 __ mov(r0, Operand(r4));
6388 __ mov(r1, Operand(r6));
6389
6390 // TODO(1242173): To let the GC traverse the return address of the exit
6391 // frames, we need to know where the return address is. Right now,
6392 // we push it on the stack to be able to find it again, but we never
6393 // restore from it in case of changes, which makes it impossible to
6394 // support moving the C entry code stub. This should be fixed, but currently
6395 // this is OK because the CEntryStub gets generated so early in the V8 boot
6396 // sequence that it is not moving ever.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006397 masm->add(lr, pc, Operand(4)); // compute return address: (pc + 8) + 4
6398 masm->push(lr);
6399 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006400
6401 if (always_allocate) {
6402 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
6403 // though (contain the result).
6404 __ mov(r2, Operand(scope_depth));
6405 __ ldr(r3, MemOperand(r2));
6406 __ sub(r3, r3, Operand(1));
6407 __ str(r3, MemOperand(r2));
6408 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006409
6410 // check for failure result
6411 Label failure_returned;
6412 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
6413 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
6414 __ add(r2, r0, Operand(1));
6415 __ tst(r2, Operand(kFailureTagMask));
6416 __ b(eq, &failure_returned);
6417
6418 // Exit C frame and return.
6419 // r0:r1: result
6420 // sp: stack pointer
6421 // fp: frame pointer
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006422 __ LeaveExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006423
6424 // check if we should retry or throw exception
6425 Label retry;
6426 __ bind(&failure_returned);
6427 ASSERT(Failure::RETRY_AFTER_GC == 0);
6428 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
6429 __ b(eq, &retry);
6430
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006431 // Special handling of out of memory exceptions.
6432 Failure* out_of_memory = Failure::OutOfMemoryException();
6433 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
6434 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006435
6436 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00006437 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006438 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00006439 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006440 __ ldr(r0, MemOperand(ip));
6441 __ str(r3, MemOperand(ip));
6442
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006443 // Special handling of termination exceptions which are uncatchable
6444 // by javascript code.
6445 __ cmp(r0, Operand(Factory::termination_exception()));
6446 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006447
6448 // Handle normal exception.
6449 __ jmp(throw_normal_exception);
6450
6451 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
6452}
6453
6454
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006455void CEntryStub::Generate(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006456 // Called from JavaScript; parameters are on stack as if calling JS function
6457 // r0: number of arguments including receiver
6458 // r1: pointer to builtin function
6459 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006460 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006461 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006462
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006463 // Result returned in r0 or r0+r1 by default.
6464
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006465 // NOTE: Invocations of builtins may return failure objects
6466 // instead of a proper result. The builtin entry handles
6467 // this by performing a garbage collection and retrying the
6468 // builtin once.
6469
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006470 // Enter the exit frame that transitions from JavaScript to C++.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006471 __ EnterExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006472
6473 // r4: number of arguments (C callee-saved)
6474 // r5: pointer to builtin function (C callee-saved)
6475 // r6: pointer to first argument (C callee-saved)
6476
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006477 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006478 Label throw_termination_exception;
6479 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006480
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00006481 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006482 GenerateCore(masm,
6483 &throw_normal_exception,
6484 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006485 &throw_out_of_memory_exception,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00006486 false,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006487 false);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006488
6489 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006490 GenerateCore(masm,
6491 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006492 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006493 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006494 true,
6495 false);
6496
6497 // Do full GC and retry runtime call one final time.
6498 Failure* failure = Failure::InternalError();
6499 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
6500 GenerateCore(masm,
6501 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006502 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006503 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00006504 true,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006505 true);
6506
6507 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00006508 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
6509
6510 __ bind(&throw_termination_exception);
6511 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006512
6513 __ bind(&throw_normal_exception);
6514 GenerateThrowTOS(masm);
6515}
6516
6517
6518void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
6519 // r0: code entry
6520 // r1: function
6521 // r2: receiver
6522 // r3: argc
6523 // [sp+0]: argv
6524
6525 Label invoke, exit;
6526
6527 // Called from C, so do not pop argc and args on exit (preserve sp)
6528 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006529 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006530 __ stm(db_w, sp, kCalleeSaved | lr.bit());
6531
6532 // Get address of argv, see stm above.
6533 // r0: code entry
6534 // r1: function
6535 // r2: receiver
6536 // r3: argc
ager@chromium.org5c838252010-02-19 08:53:10 +00006537 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006538
6539 // Push a frame with special values setup to mark it as an entry frame.
6540 // r0: code entry
6541 // r1: function
6542 // r2: receiver
6543 // r3: argc
6544 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006545 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006546 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
6547 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006548 __ mov(r6, Operand(Smi::FromInt(marker)));
6549 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6550 __ ldr(r5, MemOperand(r5));
6551 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
6552
6553 // Setup frame pointer for the frame to be pushed.
6554 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6555
6556 // Call a faked try-block that does the invoke.
6557 __ bl(&invoke);
6558
6559 // Caught exception: Store result (exception) in the pending
6560 // exception field in the JSEnv and return a failure sentinel.
6561 // Coming in here the fp will be invalid because the PushTryHandler below
6562 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00006563 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006564 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006565 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006566 __ b(&exit);
6567
6568 // Invoke: Link this frame into the handler chain.
6569 __ bind(&invoke);
6570 // Must preserve r0-r4, r5-r7 are available.
6571 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006572 // If an exception not caught by another handler occurs, this handler
6573 // returns control to the code after the bl(&invoke) above, which
6574 // restores all kCalleeSaved registers (including cp and fp) to their
6575 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006576
6577 // Clear any pending exceptions.
6578 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
6579 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00006580 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006581 __ str(r5, MemOperand(ip));
6582
6583 // Invoke the function by calling through JS entry trampoline builtin.
6584 // Notice that we cannot store a reference to the trampoline code directly in
6585 // this stub, because runtime stubs are not traversed when doing GC.
6586
6587 // Expected registers by Builtins::JSEntryTrampoline
6588 // r0: code entry
6589 // r1: function
6590 // r2: receiver
6591 // r3: argc
6592 // r4: argv
6593 if (is_construct) {
6594 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
6595 __ mov(ip, Operand(construct_entry));
6596 } else {
6597 ExternalReference entry(Builtins::JSEntryTrampoline);
6598 __ mov(ip, Operand(entry));
6599 }
6600 __ ldr(ip, MemOperand(ip)); // deref address
6601
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006602 // Branch and link to JSEntryTrampoline. We don't use the double underscore
6603 // macro for the add instruction because we don't want the coverage tool
6604 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006605 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006606 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006607
6608 // Unlink this frame from the handler chain. When reading the
6609 // address of the next handler, there is no need to use the address
6610 // displacement since the current stack pointer (sp) points directly
6611 // to the stack handler.
6612 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
6613 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
6614 __ str(r3, MemOperand(ip));
6615 // No need to restore registers
6616 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
6617
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006618
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006619 __ bind(&exit); // r0 holds result
6620 // Restore the top frame descriptors from the stack.
6621 __ pop(r3);
6622 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
6623 __ str(r3, MemOperand(ip));
6624
6625 // Reset the stack to the callee saved registers.
6626 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
6627
6628 // Restore callee-saved registers and return.
6629#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006630 if (FLAG_debug_code) {
6631 __ mov(lr, Operand(pc));
6632 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006633#endif
6634 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
6635}
6636
6637
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006638// This stub performs an instanceof, calling the builtin function if
6639// necessary. Uses r1 for the object, r0 for the function that it may
6640// be an instance of (these are fetched from the stack).
6641void InstanceofStub::Generate(MacroAssembler* masm) {
6642 // Get the object - slow case for smis (we may need to throw an exception
6643 // depending on the rhs).
6644 Label slow, loop, is_instance, is_not_instance;
6645 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
6646 __ BranchOnSmi(r0, &slow);
6647
6648 // Check that the left hand is a JS object and put map in r3.
6649 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
6650 __ b(lt, &slow);
6651 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
6652 __ b(gt, &slow);
6653
6654 // Get the prototype of the function (r4 is result, r2 is scratch).
ager@chromium.org5c838252010-02-19 08:53:10 +00006655 __ ldr(r1, MemOperand(sp, 0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006656 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
6657
6658 // Check that the function prototype is a JS object.
6659 __ BranchOnSmi(r4, &slow);
6660 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
6661 __ b(lt, &slow);
6662 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
6663 __ b(gt, &slow);
6664
6665 // Register mapping: r3 is object map and r4 is function prototype.
6666 // Get prototype of object into r2.
6667 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
6668
6669 // Loop through the prototype chain looking for the function prototype.
6670 __ bind(&loop);
6671 __ cmp(r2, Operand(r4));
6672 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00006673 __ LoadRoot(ip, Heap::kNullValueRootIndex);
6674 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006675 __ b(eq, &is_not_instance);
6676 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
6677 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
6678 __ jmp(&loop);
6679
6680 __ bind(&is_instance);
6681 __ mov(r0, Operand(Smi::FromInt(0)));
6682 __ pop();
6683 __ pop();
6684 __ mov(pc, Operand(lr)); // Return.
6685
6686 __ bind(&is_not_instance);
6687 __ mov(r0, Operand(Smi::FromInt(1)));
6688 __ pop();
6689 __ pop();
6690 __ mov(pc, Operand(lr)); // Return.
6691
6692 // Slow-case. Tail call builtin.
6693 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006694 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
6695}
6696
6697
ager@chromium.org7c537e22008-10-16 08:43:32 +00006698void ArgumentsAccessStub::GenerateReadLength(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006699 // Check if the calling frame is an arguments adaptor frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006700 Label adaptor;
6701 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6702 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006703 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006704 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006705
ager@chromium.org7c537e22008-10-16 08:43:32 +00006706 // Nothing to do: The formal number of parameters has already been
6707 // passed in register r0 by calling function. Just return it.
ager@chromium.org9085a012009-05-11 19:22:57 +00006708 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006709
ager@chromium.org7c537e22008-10-16 08:43:32 +00006710 // Arguments adaptor case: Read the arguments length from the
6711 // adaptor frame and return it.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006712 __ bind(&adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006713 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
ager@chromium.org9085a012009-05-11 19:22:57 +00006714 __ Jump(lr);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006715}
6716
6717
ager@chromium.org7c537e22008-10-16 08:43:32 +00006718void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
6719 // The displacement is the offset of the last parameter (if any)
6720 // relative to the frame pointer.
6721 static const int kDisplacement =
6722 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006723
ager@chromium.org7c537e22008-10-16 08:43:32 +00006724 // Check that the key is a smi.
6725 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006726 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006727
ager@chromium.org7c537e22008-10-16 08:43:32 +00006728 // Check if the calling frame is an arguments adaptor frame.
6729 Label adaptor;
6730 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6731 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006732 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006733 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006734
ager@chromium.org7c537e22008-10-16 08:43:32 +00006735 // Check index against formal parameters count limit passed in
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006736 // through register r0. Use unsigned comparison to get negative
ager@chromium.org7c537e22008-10-16 08:43:32 +00006737 // check for free.
6738 __ cmp(r1, r0);
6739 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006740
ager@chromium.org7c537e22008-10-16 08:43:32 +00006741 // Read the argument from the stack and return it.
6742 __ sub(r3, r0, r1);
6743 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6744 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006745 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006746
6747 // Arguments adaptor case: Check index against actual arguments
6748 // limit found in the arguments adaptor frame. Use unsigned
6749 // comparison to get negative check for free.
6750 __ bind(&adaptor);
6751 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6752 __ cmp(r1, r0);
6753 __ b(cs, &slow);
6754
6755 // Read the argument from the adaptor frame and return it.
6756 __ sub(r3, r0, r1);
6757 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
6758 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00006759 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006760
6761 // Slow-case: Handle non-smi or out-of-bounds access to arguments
6762 // by calling the runtime system.
6763 __ bind(&slow);
6764 __ push(r1);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006765 __ TailCallRuntime(ExternalReference(Runtime::kGetArgumentsProperty), 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006766}
6767
6768
6769void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
ager@chromium.org5c838252010-02-19 08:53:10 +00006770 // sp[0] : number of parameters
6771 // sp[4] : receiver displacement
6772 // sp[8] : function
6773
ager@chromium.org7c537e22008-10-16 08:43:32 +00006774 // Check if the calling frame is an arguments adaptor frame.
ager@chromium.org5c838252010-02-19 08:53:10 +00006775 Label adaptor_frame, try_allocate, runtime;
ager@chromium.org7c537e22008-10-16 08:43:32 +00006776 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
6777 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00006778 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org5c838252010-02-19 08:53:10 +00006779 __ b(eq, &adaptor_frame);
6780
6781 // Get the length from the frame.
6782 __ ldr(r1, MemOperand(sp, 0));
6783 __ b(&try_allocate);
ager@chromium.org7c537e22008-10-16 08:43:32 +00006784
6785 // Patch the arguments.length and the parameters pointer.
ager@chromium.org5c838252010-02-19 08:53:10 +00006786 __ bind(&adaptor_frame);
6787 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
6788 __ str(r1, MemOperand(sp, 0));
6789 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
ager@chromium.org7c537e22008-10-16 08:43:32 +00006790 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
6791 __ str(r3, MemOperand(sp, 1 * kPointerSize));
6792
ager@chromium.org5c838252010-02-19 08:53:10 +00006793 // Try the new space allocation. Start out with computing the size
6794 // of the arguments object and the elements array (in words, not
6795 // bytes because AllocateInNewSpace expects words).
6796 Label add_arguments_object;
6797 __ bind(&try_allocate);
6798 __ cmp(r1, Operand(0));
6799 __ b(eq, &add_arguments_object);
6800 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
6801 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
6802 __ bind(&add_arguments_object);
6803 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
6804
6805 // Do the allocation of both objects in one go.
6806 __ AllocateInNewSpace(r1, r0, r2, r3, &runtime, TAG_OBJECT);
6807
6808 // Get the arguments boilerplate from the current (global) context.
6809 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
6810 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
6811 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
6812 __ ldr(r4, MemOperand(r4, offset));
6813
6814 // Copy the JS object part.
6815 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
6816 __ ldr(r3, FieldMemOperand(r4, i));
6817 __ str(r3, FieldMemOperand(r0, i));
6818 }
6819
6820 // Setup the callee in-object property.
6821 ASSERT(Heap::arguments_callee_index == 0);
6822 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
6823 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
6824
6825 // Get the length (smi tagged) and set that as an in-object property too.
6826 ASSERT(Heap::arguments_length_index == 1);
6827 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
6828 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
6829
6830 // If there are no actual arguments, we're done.
6831 Label done;
6832 __ cmp(r1, Operand(0));
6833 __ b(eq, &done);
6834
6835 // Get the parameters pointer from the stack and untag the length.
6836 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
6837 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
6838
6839 // Setup the elements pointer in the allocated arguments object and
6840 // initialize the header in the elements fixed array.
6841 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
6842 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
6843 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
6844 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
6845 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
6846
6847 // Copy the fixed array slots.
6848 Label loop;
6849 // Setup r4 to point to the first array slot.
6850 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
6851 __ bind(&loop);
6852 // Pre-decrement r2 with kPointerSize on each iteration.
6853 // Pre-decrement in order to skip receiver.
6854 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
6855 // Post-increment r4 with kPointerSize on each iteration.
6856 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
6857 __ sub(r1, r1, Operand(1));
6858 __ cmp(r1, Operand(0));
6859 __ b(ne, &loop);
6860
6861 // Return and remove the on-stack parameters.
6862 __ bind(&done);
6863 __ add(sp, sp, Operand(3 * kPointerSize));
6864 __ Ret();
6865
ager@chromium.org7c537e22008-10-16 08:43:32 +00006866 // Do the runtime call to allocate the arguments object.
6867 __ bind(&runtime);
ager@chromium.orga1645e22009-09-09 19:27:10 +00006868 __ TailCallRuntime(ExternalReference(Runtime::kNewArgumentsFast), 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006869}
6870
6871
6872void CallFunctionStub::Generate(MacroAssembler* masm) {
6873 Label slow;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006874
6875 // If the receiver might be a value (string, number or boolean) check for this
6876 // and box it if it is.
6877 if (ReceiverMightBeValue()) {
6878 // Get the receiver from the stack.
6879 // function, receiver [, arguments]
6880 Label receiver_is_value, receiver_is_js_object;
6881 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
6882
6883 // Check if receiver is a smi (which is a number value).
6884 __ BranchOnSmi(r1, &receiver_is_value);
6885
6886 // Check if the receiver is a valid JS object.
6887 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
6888 __ b(ge, &receiver_is_js_object);
6889
6890 // Call the runtime to box the value.
6891 __ bind(&receiver_is_value);
6892 __ EnterInternalFrame();
6893 __ push(r1);
6894 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
6895 __ LeaveInternalFrame();
6896 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
6897
6898 __ bind(&receiver_is_js_object);
6899 }
6900
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006901 // Get the function to call from the stack.
6902 // function, receiver [, arguments]
6903 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
6904
6905 // Check that the function is really a JavaScript function.
6906 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006907 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006908 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006909 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006910 __ b(ne, &slow);
6911
6912 // Fast-case: Invoke the function now.
6913 // r1: pushed function
6914 ParameterCount actual(argc_);
6915 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
6916
6917 // Slow-case: Non-function called.
6918 __ bind(&slow);
ager@chromium.org5c838252010-02-19 08:53:10 +00006919 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
6920 // of the original receiver from the call site).
6921 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006922 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00006923 __ mov(r2, Operand(0));
6924 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
6925 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
6926 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006927}
6928
6929
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006930const char* CompareStub::GetName() {
6931 switch (cc_) {
6932 case lt: return "CompareStub_LT";
6933 case gt: return "CompareStub_GT";
6934 case le: return "CompareStub_LE";
6935 case ge: return "CompareStub_GE";
6936 case ne: {
6937 if (strict_) {
6938 if (never_nan_nan_) {
6939 return "CompareStub_NE_STRICT_NO_NAN";
6940 } else {
6941 return "CompareStub_NE_STRICT";
6942 }
6943 } else {
6944 if (never_nan_nan_) {
6945 return "CompareStub_NE_NO_NAN";
6946 } else {
6947 return "CompareStub_NE";
6948 }
6949 }
6950 }
6951 case eq: {
6952 if (strict_) {
6953 if (never_nan_nan_) {
6954 return "CompareStub_EQ_STRICT_NO_NAN";
6955 } else {
6956 return "CompareStub_EQ_STRICT";
6957 }
6958 } else {
6959 if (never_nan_nan_) {
6960 return "CompareStub_EQ_NO_NAN";
6961 } else {
6962 return "CompareStub_EQ";
6963 }
6964 }
6965 }
6966 default: return "CompareStub";
6967 }
6968}
6969
6970
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006971int CompareStub::MinorKey() {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006972 // Encode the three parameters in a unique 16 bit value.
6973 ASSERT((static_cast<unsigned>(cc_) >> 26) < (1 << 16));
6974 int nnn_value = (never_nan_nan_ ? 2 : 0);
6975 if (cc_ != eq) nnn_value = 0; // Avoid duplicate stubs.
6976 return (static_cast<unsigned>(cc_) >> 26) | nnn_value | (strict_ ? 1 : 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00006977}
6978
6979
ager@chromium.org5c838252010-02-19 08:53:10 +00006980void StringStubBase::GenerateCopyCharacters(MacroAssembler* masm,
6981 Register dest,
6982 Register src,
6983 Register count,
6984 Register scratch,
6985 bool ascii) {
6986 Label loop;
6987 Label done;
6988 // This loop just copies one character at a time, as it is only used for very
6989 // short strings.
6990 if (!ascii) {
6991 __ add(count, count, Operand(count), SetCC);
6992 } else {
6993 __ cmp(count, Operand(0));
6994 }
6995 __ b(eq, &done);
6996
6997 __ bind(&loop);
6998 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
6999 // Perform sub between load and dependent store to get the load time to
7000 // complete.
7001 __ sub(count, count, Operand(1), SetCC);
7002 __ strb(scratch, MemOperand(dest, 1, PostIndex));
7003 // last iteration.
7004 __ b(gt, &loop);
7005
7006 __ bind(&done);
7007}
7008
7009
7010enum CopyCharactersFlags {
7011 COPY_ASCII = 1,
7012 DEST_ALWAYS_ALIGNED = 2
7013};
7014
7015
7016void StringStubBase::GenerateCopyCharactersLong(MacroAssembler* masm,
7017 Register dest,
7018 Register src,
7019 Register count,
7020 Register scratch1,
7021 Register scratch2,
7022 Register scratch3,
7023 Register scratch4,
7024 Register scratch5,
7025 int flags) {
7026 bool ascii = (flags & COPY_ASCII) != 0;
7027 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
7028
7029 if (dest_always_aligned && FLAG_debug_code) {
7030 // Check that destination is actually word aligned if the flag says
7031 // that it is.
7032 __ tst(dest, Operand(kPointerAlignmentMask));
7033 __ Check(eq, "Destination of copy not aligned.");
7034 }
7035
7036 const int kReadAlignment = 4;
7037 const int kReadAlignmentMask = kReadAlignment - 1;
7038 // Ensure that reading an entire aligned word containing the last character
7039 // of a string will not read outside the allocated area (because we pad up
7040 // to kObjectAlignment).
7041 ASSERT(kObjectAlignment >= kReadAlignment);
7042 // Assumes word reads and writes are little endian.
7043 // Nothing to do for zero characters.
7044 Label done;
7045 if (!ascii) {
7046 __ add(count, count, Operand(count), SetCC);
7047 } else {
7048 __ cmp(count, Operand(0));
7049 }
7050 __ b(eq, &done);
7051
7052 // Assume that you cannot read (or write) unaligned.
7053 Label byte_loop;
7054 // Must copy at least eight bytes, otherwise just do it one byte at a time.
7055 __ cmp(count, Operand(8));
7056 __ add(count, dest, Operand(count));
7057 Register limit = count; // Read until src equals this.
7058 __ b(lt, &byte_loop);
7059
7060 if (!dest_always_aligned) {
7061 // Align dest by byte copying. Copies between zero and three bytes.
7062 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
7063 Label dest_aligned;
7064 __ b(eq, &dest_aligned);
7065 __ cmp(scratch4, Operand(2));
7066 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
7067 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
7068 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
7069 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
7070 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
7071 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
7072 __ bind(&dest_aligned);
7073 }
7074
7075 Label simple_loop;
7076
7077 __ sub(scratch4, dest, Operand(src));
7078 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
7079 __ b(eq, &simple_loop);
7080 // Shift register is number of bits in a source word that
7081 // must be combined with bits in the next source word in order
7082 // to create a destination word.
7083
7084 // Complex loop for src/dst that are not aligned the same way.
7085 {
7086 Label loop;
7087 __ mov(scratch4, Operand(scratch4, LSL, 3));
7088 Register left_shift = scratch4;
7089 __ and_(src, src, Operand(~3)); // Round down to load previous word.
7090 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
7091 // Store the "shift" most significant bits of scratch in the least
7092 // signficant bits (i.e., shift down by (32-shift)).
7093 __ rsb(scratch2, left_shift, Operand(32));
7094 Register right_shift = scratch2;
7095 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
7096
7097 __ bind(&loop);
7098 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
7099 __ sub(scratch5, limit, Operand(dest));
7100 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
7101 __ str(scratch1, MemOperand(dest, 4, PostIndex));
7102 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
7103 // Loop if four or more bytes left to copy.
7104 // Compare to eight, because we did the subtract before increasing dst.
7105 __ sub(scratch5, scratch5, Operand(8), SetCC);
7106 __ b(ge, &loop);
7107 }
7108 // There is now between zero and three bytes left to copy (negative that
7109 // number is in scratch5), and between one and three bytes already read into
7110 // scratch1 (eight times that number in scratch4). We may have read past
7111 // the end of the string, but because objects are aligned, we have not read
7112 // past the end of the object.
7113 // Find the minimum of remaining characters to move and preloaded characters
7114 // and write those as bytes.
7115 __ add(scratch5, scratch5, Operand(4), SetCC);
7116 __ b(eq, &done);
7117 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
7118 // Move minimum of bytes read and bytes left to copy to scratch4.
7119 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
7120 // Between one and three (value in scratch5) characters already read into
7121 // scratch ready to write.
7122 __ cmp(scratch5, Operand(2));
7123 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
7124 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
7125 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
7126 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
7127 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
7128 // Copy any remaining bytes.
7129 __ b(&byte_loop);
7130
7131 // Simple loop.
7132 // Copy words from src to dst, until less than four bytes left.
7133 // Both src and dest are word aligned.
7134 __ bind(&simple_loop);
7135 {
7136 Label loop;
7137 __ bind(&loop);
7138 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
7139 __ sub(scratch3, limit, Operand(dest));
7140 __ str(scratch1, MemOperand(dest, 4, PostIndex));
7141 // Compare to 8, not 4, because we do the substraction before increasing
7142 // dest.
7143 __ cmp(scratch3, Operand(8));
7144 __ b(ge, &loop);
7145 }
7146
7147 // Copy bytes from src to dst until dst hits limit.
7148 __ bind(&byte_loop);
7149 __ cmp(dest, Operand(limit));
7150 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
7151 __ b(ge, &done);
7152 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
7153 __ b(&byte_loop);
7154
7155 __ bind(&done);
7156}
7157
7158
7159void SubStringStub::Generate(MacroAssembler* masm) {
7160 Label runtime;
7161
7162 // Stack frame on entry.
7163 // lr: return address
7164 // sp[0]: to
7165 // sp[4]: from
7166 // sp[8]: string
7167
7168 // This stub is called from the native-call %_SubString(...), so
7169 // nothing can be assumed about the arguments. It is tested that:
7170 // "string" is a sequential string,
7171 // both "from" and "to" are smis, and
7172 // 0 <= from <= to <= string.length.
7173 // If any of these assumptions fail, we call the runtime system.
7174
7175 static const int kToOffset = 0 * kPointerSize;
7176 static const int kFromOffset = 1 * kPointerSize;
7177 static const int kStringOffset = 2 * kPointerSize;
7178
7179
7180 // Check bounds and smi-ness.
7181 __ ldr(r7, MemOperand(sp, kToOffset));
7182 __ ldr(r6, MemOperand(sp, kFromOffset));
7183 ASSERT_EQ(0, kSmiTag);
7184 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
7185 // I.e., arithmetic shift right by one un-smi-tags.
7186 __ mov(r2, Operand(r7, ASR, 1), SetCC);
7187 __ mov(r3, Operand(r6, ASR, 1), SetCC, cc);
7188 // If either r2 or r6 had the smi tag bit set, then carry is set now.
7189 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
7190 __ b(mi, &runtime); // From is negative.
7191
7192 __ sub(r2, r2, Operand(r3), SetCC);
7193 __ b(mi, &runtime); // Fail if from > to.
7194 // Handle sub-strings of length 2 and less in the runtime system.
7195 __ cmp(r2, Operand(2));
7196 __ b(le, &runtime);
7197
7198 // r2: length
7199 // r6: from (smi)
7200 // r7: to (smi)
7201
7202 // Make sure first argument is a sequential (or flat) string.
7203 __ ldr(r5, MemOperand(sp, kStringOffset));
7204 ASSERT_EQ(0, kSmiTag);
7205 __ tst(r5, Operand(kSmiTagMask));
7206 __ b(eq, &runtime);
7207 Condition is_string = masm->IsObjectStringType(r5, r1);
7208 __ b(NegateCondition(is_string), &runtime);
7209
7210 // r1: instance type
7211 // r2: length
7212 // r5: string
7213 // r6: from (smi)
7214 // r7: to (smi)
7215 Label seq_string;
7216 __ and_(r4, r1, Operand(kStringRepresentationMask));
7217 ASSERT(kSeqStringTag < kConsStringTag);
7218 ASSERT(kExternalStringTag > kConsStringTag);
7219 __ cmp(r4, Operand(kConsStringTag));
7220 __ b(gt, &runtime); // External strings go to runtime.
7221 __ b(lt, &seq_string); // Sequential strings are handled directly.
7222
7223 // Cons string. Try to recurse (once) on the first substring.
7224 // (This adds a little more generality than necessary to handle flattened
7225 // cons strings, but not much).
7226 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
7227 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
7228 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
7229 __ tst(r1, Operand(kStringRepresentationMask));
7230 ASSERT_EQ(0, kSeqStringTag);
7231 __ b(ne, &runtime); // Cons and External strings go to runtime.
7232
7233 // Definitly a sequential string.
7234 __ bind(&seq_string);
7235
7236 // r1: instance type.
7237 // r2: length
7238 // r5: string
7239 // r6: from (smi)
7240 // r7: to (smi)
7241 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
7242 __ cmp(r4, Operand(r7, ASR, 1));
7243 __ b(lt, &runtime); // Fail if to > length.
7244
7245 // r1: instance type.
7246 // r2: result string length.
7247 // r5: string.
7248 // r6: from offset (smi)
7249 // Check for flat ascii string.
7250 Label non_ascii_flat;
7251 __ tst(r1, Operand(kStringEncodingMask));
7252 ASSERT_EQ(0, kTwoByteStringTag);
7253 __ b(eq, &non_ascii_flat);
7254
7255 // Allocate the result.
7256 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
7257
7258 // r0: result string.
7259 // r2: result string length.
7260 // r5: string.
7261 // r6: from offset (smi)
7262 // Locate first character of result.
7263 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7264 // Locate 'from' character of string.
7265 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7266 __ add(r5, r5, Operand(r6, ASR, 1));
7267
7268 // r0: result string.
7269 // r1: first character of result string.
7270 // r2: result string length.
7271 // r5: first character of sub string to copy.
7272 ASSERT_EQ(0, SeqAsciiString::kHeaderSize & kObjectAlignmentMask);
7273 GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
7274 COPY_ASCII | DEST_ALWAYS_ALIGNED);
7275 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
7276 __ add(sp, sp, Operand(3 * kPointerSize));
7277 __ Ret();
7278
7279 __ bind(&non_ascii_flat);
7280 // r2: result string length.
7281 // r5: string.
7282 // r6: from offset (smi)
7283 // Check for flat two byte string.
7284
7285 // Allocate the result.
7286 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
7287
7288 // r0: result string.
7289 // r2: result string length.
7290 // r5: string.
7291 // Locate first character of result.
7292 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7293 // Locate 'from' character of string.
7294 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7295 // As "from" is a smi it is 2 times the value which matches the size of a two
7296 // byte character.
7297 __ add(r5, r5, Operand(r6));
7298
7299 // r0: result string.
7300 // r1: first character of result.
7301 // r2: result length.
7302 // r5: first character of string to copy.
7303 ASSERT_EQ(0, SeqTwoByteString::kHeaderSize & kObjectAlignmentMask);
7304 GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
7305 DEST_ALWAYS_ALIGNED);
7306 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
7307 __ add(sp, sp, Operand(3 * kPointerSize));
7308 __ Ret();
7309
7310 // Just jump to runtime to create the sub string.
7311 __ bind(&runtime);
7312 __ TailCallRuntime(ExternalReference(Runtime::kSubString), 3, 1);
7313}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007314
7315
7316void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
7317 Register left,
7318 Register right,
7319 Register scratch1,
7320 Register scratch2,
7321 Register scratch3,
7322 Register scratch4) {
7323 Label compare_lengths;
7324 // Find minimum length and length difference.
7325 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
7326 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
7327 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
7328 Register length_delta = scratch3;
7329 __ mov(scratch1, scratch2, LeaveCC, gt);
7330 Register min_length = scratch1;
7331 __ tst(min_length, Operand(min_length));
7332 __ b(eq, &compare_lengths);
7333
7334 // Setup registers so that we only need to increment one register
7335 // in the loop.
7336 __ add(scratch2, min_length,
7337 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7338 __ add(left, left, Operand(scratch2));
7339 __ add(right, right, Operand(scratch2));
7340 // Registers left and right points to the min_length character of strings.
7341 __ rsb(min_length, min_length, Operand(-1));
7342 Register index = min_length;
7343 // Index starts at -min_length.
7344
7345 {
7346 // Compare loop.
7347 Label loop;
7348 __ bind(&loop);
7349 // Compare characters.
7350 __ add(index, index, Operand(1), SetCC);
7351 __ ldrb(scratch2, MemOperand(left, index), ne);
7352 __ ldrb(scratch4, MemOperand(right, index), ne);
7353 // Skip to compare lengths with eq condition true.
7354 __ b(eq, &compare_lengths);
7355 __ cmp(scratch2, scratch4);
7356 __ b(eq, &loop);
7357 // Fallthrough with eq condition false.
7358 }
7359 // Compare lengths - strings up to min-length are equal.
7360 __ bind(&compare_lengths);
7361 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
7362 // Use zero length_delta as result.
7363 __ mov(r0, Operand(length_delta), SetCC, eq);
7364 // Fall through to here if characters compare not-equal.
7365 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
7366 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
7367 __ Ret();
7368}
7369
7370
7371void StringCompareStub::Generate(MacroAssembler* masm) {
7372 Label runtime;
7373
7374 // Stack frame on entry.
ager@chromium.org5c838252010-02-19 08:53:10 +00007375 // sp[0]: right string
7376 // sp[4]: left string
7377 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left
7378 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007379
7380 Label not_same;
7381 __ cmp(r0, r1);
7382 __ b(ne, &not_same);
7383 ASSERT_EQ(0, EQUAL);
7384 ASSERT_EQ(0, kSmiTag);
7385 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
7386 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
7387 __ add(sp, sp, Operand(2 * kPointerSize));
7388 __ Ret();
7389
7390 __ bind(&not_same);
7391
7392 // Check that both objects are sequential ascii strings.
7393 __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime);
7394
7395 // Compare flat ascii strings natively. Remove arguments from stack first.
7396 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
7397 __ add(sp, sp, Operand(2 * kPointerSize));
7398 GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5);
7399
7400 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
7401 // tagged as a small integer.
7402 __ bind(&runtime);
7403 __ TailCallRuntime(ExternalReference(Runtime::kStringCompare), 2, 1);
7404}
7405
7406
ager@chromium.org5c838252010-02-19 08:53:10 +00007407void StringAddStub::Generate(MacroAssembler* masm) {
7408 Label string_add_runtime;
7409 // Stack on entry:
7410 // sp[0]: second argument.
7411 // sp[4]: first argument.
7412
7413 // Load the two arguments.
7414 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
7415 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
7416
7417 // Make sure that both arguments are strings if not known in advance.
7418 if (string_check_) {
7419 ASSERT_EQ(0, kSmiTag);
7420 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
7421 // Load instance types.
7422 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
7423 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
7424 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
7425 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
7426 ASSERT_EQ(0, kStringTag);
7427 // If either is not a string, go to runtime.
7428 __ tst(r4, Operand(kIsNotStringMask));
7429 __ tst(r5, Operand(kIsNotStringMask), eq);
7430 __ b(ne, &string_add_runtime);
7431 }
7432
7433 // Both arguments are strings.
7434 // r0: first string
7435 // r1: second string
7436 // r4: first string instance type (if string_check_)
7437 // r5: second string instance type (if string_check_)
7438 {
7439 Label strings_not_empty;
7440 // Check if either of the strings are empty. In that case return the other.
7441 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
7442 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
7443 __ cmp(r2, Operand(0)); // Test if first string is empty.
7444 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
7445 __ cmp(r3, Operand(0), ne); // Else test if second string is empty.
7446 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
7447
7448 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
7449 __ add(sp, sp, Operand(2 * kPointerSize));
7450 __ Ret();
7451
7452 __ bind(&strings_not_empty);
7453 }
7454
7455 // Both strings are non-empty.
7456 // r0: first string
7457 // r1: second string
7458 // r2: length of first string
7459 // r3: length of second string
7460 // r4: first string instance type (if string_check_)
7461 // r5: second string instance type (if string_check_)
7462 // Look at the length of the result of adding the two strings.
7463 Label string_add_flat_result;
7464 // Adding two lengths can't overflow.
7465 ASSERT(String::kMaxLength * 2 > String::kMaxLength);
7466 __ add(r6, r2, Operand(r3));
7467 // Use the runtime system when adding two one character strings, as it
7468 // contains optimizations for this specific case using the symbol table.
7469 __ cmp(r6, Operand(2));
7470 __ b(eq, &string_add_runtime);
7471 // Check if resulting string will be flat.
7472 __ cmp(r6, Operand(String::kMinNonFlatLength));
7473 __ b(lt, &string_add_flat_result);
7474 // Handle exceptionally long strings in the runtime system.
7475 ASSERT((String::kMaxLength & 0x80000000) == 0);
7476 ASSERT(IsPowerOf2(String::kMaxLength + 1));
7477 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
7478 __ cmp(r6, Operand(String::kMaxLength + 1));
7479 __ b(hs, &string_add_runtime);
7480
7481 // If result is not supposed to be flat, allocate a cons string object.
7482 // If both strings are ascii the result is an ascii cons string.
7483 if (!string_check_) {
7484 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
7485 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
7486 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
7487 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
7488 }
7489 Label non_ascii, allocated;
7490 ASSERT_EQ(0, kTwoByteStringTag);
7491 __ tst(r4, Operand(kStringEncodingMask));
7492 __ tst(r5, Operand(kStringEncodingMask), ne);
7493 __ b(eq, &non_ascii);
7494
7495 // Allocate an ASCII cons string.
7496 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
7497 __ bind(&allocated);
7498 // Fill the fields of the cons string.
7499 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
7500 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
7501 __ mov(r0, Operand(r7));
7502 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
7503 __ add(sp, sp, Operand(2 * kPointerSize));
7504 __ Ret();
7505
7506 __ bind(&non_ascii);
7507 // Allocate a two byte cons string.
7508 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
7509 __ jmp(&allocated);
7510
7511 // Handle creating a flat result. First check that both strings are
7512 // sequential and that they have the same encoding.
7513 // r0: first string
7514 // r1: second string
7515 // r2: length of first string
7516 // r3: length of second string
7517 // r4: first string instance type (if string_check_)
7518 // r5: second string instance type (if string_check_)
7519 // r6: sum of lengths.
7520 __ bind(&string_add_flat_result);
7521 if (!string_check_) {
7522 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
7523 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
7524 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
7525 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
7526 }
7527 // Check that both strings are sequential.
7528 ASSERT_EQ(0, kSeqStringTag);
7529 __ tst(r4, Operand(kStringRepresentationMask));
7530 __ tst(r5, Operand(kStringRepresentationMask), eq);
7531 __ b(ne, &string_add_runtime);
7532 // Now check if both strings have the same encoding (ASCII/Two-byte).
7533 // r0: first string.
7534 // r1: second string.
7535 // r2: length of first string.
7536 // r3: length of second string.
7537 // r6: sum of lengths..
7538 Label non_ascii_string_add_flat_result;
7539 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
7540 __ eor(r7, r4, Operand(r5));
7541 __ tst(r7, Operand(kStringEncodingMask));
7542 __ b(ne, &string_add_runtime);
7543 // And see if it's ASCII or two-byte.
7544 __ tst(r4, Operand(kStringEncodingMask));
7545 __ b(eq, &non_ascii_string_add_flat_result);
7546
7547 // Both strings are sequential ASCII strings. We also know that they are
7548 // short (since the sum of the lengths is less than kMinNonFlatLength).
7549 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
7550 // Locate first character of result.
7551 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7552 // Locate first character of first argument.
7553 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7554 // r0: first character of first string.
7555 // r1: second string.
7556 // r2: length of first string.
7557 // r3: length of second string.
7558 // r6: first character of result.
7559 // r7: result string.
7560 GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
7561
7562 // Load second argument and locate first character.
7563 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7564 // r1: first character of second string.
7565 // r3: length of second string.
7566 // r6: next character of result.
7567 // r7: result string.
7568 GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
7569 __ mov(r0, Operand(r7));
7570 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
7571 __ add(sp, sp, Operand(2 * kPointerSize));
7572 __ Ret();
7573
7574 __ bind(&non_ascii_string_add_flat_result);
7575 // Both strings are sequential two byte strings.
7576 // r0: first string.
7577 // r1: second string.
7578 // r2: length of first string.
7579 // r3: length of second string.
7580 // r6: sum of length of strings.
7581 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
7582 // r0: first string.
7583 // r1: second string.
7584 // r2: length of first string.
7585 // r3: length of second string.
7586 // r7: result string.
7587
7588 // Locate first character of result.
7589 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7590 // Locate first character of first argument.
7591 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7592
7593 // r0: first character of first string.
7594 // r1: second string.
7595 // r2: length of first string.
7596 // r3: length of second string.
7597 // r6: first character of result.
7598 // r7: result string.
7599 GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
7600
7601 // Locate first character of second argument.
7602 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
7603
7604 // r1: first character of second string.
7605 // r3: length of second string.
7606 // r6: next character of result (after copy of first string).
7607 // r7: result string.
7608 GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
7609
7610 __ mov(r0, Operand(r7));
7611 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
7612 __ add(sp, sp, Operand(2 * kPointerSize));
7613 __ Ret();
7614
7615 // Just jump to runtime to add the two strings.
7616 __ bind(&string_add_runtime);
7617 __ TailCallRuntime(ExternalReference(Runtime::kStringAdd), 2, 1);
7618}
7619
7620
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007621#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007622
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00007623} } // namespace v8::internal