blob: b2fe05f5a3acaffc3742669cd025865cdc42c897 [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.orgce5e87b2010-03-10 10:24:18 +000034#include "ic-inl.h"
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000035#include "jsregexp.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000036#include "parser.h"
ricow@chromium.orgc9c80822010-04-21 08:22:37 +000037#include "regexp-macro-assembler.h"
38#include "regexp-stack.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000039#include "register-allocator-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040#include "runtime.h"
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000041#include "scopes.h"
sgjesse@chromium.org833cdd72010-02-26 10:06:16 +000042#include "virtual-frame-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000043
kasperl@chromium.org71affb52009-05-26 05:44:31 +000044namespace v8 {
45namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046
ager@chromium.org65dad4b2009-04-23 08:48:43 +000047#define __ ACCESS_MASM(masm_)
48
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000049static void EmitIdenticalObjectComparison(MacroAssembler* masm,
50 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000051 Condition cc,
52 bool never_nan_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000053static void EmitSmiNonsmiComparison(MacroAssembler* masm,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000054 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000055 Label* slow,
56 bool strict);
57static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc);
58static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000059static void MultiplyByKnownInt(MacroAssembler* masm,
60 Register source,
61 Register destination,
62 int known_int);
63static bool IsEasyToMultiplyBy(int x);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +000064
65
66
ager@chromium.orge2902be2009-06-08 12:21:35 +000067// -------------------------------------------------------------------------
68// Platform-specific DeferredCode functions.
69
70void DeferredCode::SaveRegisters() {
71 for (int i = 0; i < RegisterAllocator::kNumRegisters; i++) {
72 int action = registers_[i];
73 if (action == kPush) {
74 __ push(RegisterAllocator::ToRegister(i));
75 } else if (action != kIgnore && (action & kSyncedFlag) == 0) {
76 __ str(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
77 }
78 }
79}
80
81
82void DeferredCode::RestoreRegisters() {
83 // Restore registers in reverse order due to the stack.
84 for (int i = RegisterAllocator::kNumRegisters - 1; i >= 0; i--) {
85 int action = registers_[i];
86 if (action == kPush) {
87 __ pop(RegisterAllocator::ToRegister(i));
88 } else if (action != kIgnore) {
89 action &= ~kSyncedFlag;
90 __ ldr(RegisterAllocator::ToRegister(i), MemOperand(fp, action));
91 }
92 }
93}
94
ager@chromium.org3bf7b912008-11-17 09:09:45 +000095
96// -------------------------------------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000097// CodeGenState implementation.
98
ager@chromium.org7c537e22008-10-16 08:43:32 +000099CodeGenState::CodeGenState(CodeGenerator* owner)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000100 : owner_(owner),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000101 true_target_(NULL),
102 false_target_(NULL),
103 previous_(NULL) {
104 owner_->set_state(this);
105}
106
107
ager@chromium.org7c537e22008-10-16 08:43:32 +0000108CodeGenState::CodeGenState(CodeGenerator* owner,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000109 JumpTarget* true_target,
110 JumpTarget* false_target)
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000111 : owner_(owner),
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000112 true_target_(true_target),
113 false_target_(false_target),
114 previous_(owner->state()) {
115 owner_->set_state(this);
116}
117
118
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000119CodeGenState::~CodeGenState() {
120 ASSERT(owner_->state() == this);
121 owner_->set_state(previous_);
122}
123
124
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000125// -------------------------------------------------------------------------
ager@chromium.org7c537e22008-10-16 08:43:32 +0000126// CodeGenerator implementation
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000127
ager@chromium.org5c838252010-02-19 08:53:10 +0000128CodeGenerator::CodeGenerator(MacroAssembler* masm)
129 : deferred_(8),
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000130 masm_(masm),
ager@chromium.org5c838252010-02-19 08:53:10 +0000131 info_(NULL),
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000132 frame_(NULL),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000133 allocator_(NULL),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134 cc_reg_(al),
135 state_(NULL),
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000136 loop_nesting_(0),
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000137 function_return_is_shadowed_(false) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138}
139
140
141// Calling conventions:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000142// fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000143// sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000144// r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000145// cp: callee's context
146
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000147void CodeGenerator::Generate(CompilationInfo* info) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000148 // Record the position for debugging purposes.
ager@chromium.org5c838252010-02-19 08:53:10 +0000149 CodeForFunctionPosition(info->function());
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000150 Comment cmnt(masm_, "[ function compiled by virtual frame code generator");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000151
152 // Initialize state.
ager@chromium.org5c838252010-02-19 08:53:10 +0000153 info_ = info;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000154 ASSERT(allocator_ == NULL);
155 RegisterAllocator register_allocator(this);
156 allocator_ = &register_allocator;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000157 ASSERT(frame_ == NULL);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000158 frame_ = new VirtualFrame();
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000159 cc_reg_ = al;
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000160
161 // Adjust for function-level loop nesting.
162 ASSERT_EQ(0, loop_nesting_);
163 loop_nesting_ = info->loop_nesting();
164
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000165 {
166 CodeGenState state(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000168 // Entry:
169 // Stack: receiver, arguments
170 // lr: return address
171 // fp: caller's frame pointer
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172 // sp: stack pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000173 // r1: called JS function
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000174 // cp: callee's context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000175 allocator_->Initialize();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000176
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177#ifdef DEBUG
178 if (strlen(FLAG_stop_at) > 0 &&
ager@chromium.org5c838252010-02-19 08:53:10 +0000179 info->function()->name()->IsEqualTo(CStrVector(FLAG_stop_at))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000180 frame_->SpillAll();
kasper.lund7276f142008-07-30 08:49:36 +0000181 __ stop("stop-at");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000182 }
183#endif
184
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000185 if (info->mode() == CompilationInfo::PRIMARY) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000186 frame_->Enter();
187 // tos: code slot
188
189 // Allocate space for locals and initialize them. This also checks
190 // for stack overflow.
191 frame_->AllocateStackSlots();
192
ager@chromium.org357bf652010-04-12 11:30:10 +0000193 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org5c838252010-02-19 08:53:10 +0000194 int heap_slots = scope()->num_heap_slots();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000195 if (heap_slots > 0) {
196 // Allocate local context.
197 // Get outer context and create a new context based on it.
198 __ ldr(r0, frame_->Function());
199 frame_->EmitPush(r0);
200 if (heap_slots <= FastNewContextStub::kMaximumSlots) {
201 FastNewContextStub stub(heap_slots);
202 frame_->CallStub(&stub, 1);
203 } else {
204 frame_->CallRuntime(Runtime::kNewContext, 1);
205 }
206
207#ifdef DEBUG
208 JumpTarget verified_true;
209 __ cmp(r0, Operand(cp));
210 verified_true.Branch(eq);
211 __ stop("NewContext: r0 is expected to be the same as cp");
212 verified_true.Bind();
213#endif
214 // Update context local.
215 __ str(cp, frame_->Context());
216 }
217
218 // TODO(1241774): Improve this code:
219 // 1) only needed if we have a context
220 // 2) no need to recompute context ptr every single time
221 // 3) don't copy parameter operand code from SlotOperand!
222 {
223 Comment cmnt2(masm_, "[ copy context parameters into .context");
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000224 // Note that iteration order is relevant here! If we have the same
225 // parameter twice (e.g., function (x, y, x)), and that parameter
226 // needs to be copied into the context, it must be the last argument
227 // passed to the parameter that needs to be copied. This is a rare
228 // case so we don't check for it, instead we rely on the copying
229 // order: such a parameter is copied repeatedly into the same
230 // context location and thus the last value is what is seen inside
231 // the function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000232 for (int i = 0; i < scope()->num_parameters(); i++) {
233 Variable* par = scope()->parameter(i);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000234 Slot* slot = par->slot();
235 if (slot != NULL && slot->type() == Slot::CONTEXT) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000236 ASSERT(!scope()->is_global_scope()); // No params in global scope.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000237 __ ldr(r1, frame_->ParameterAt(i));
238 // Loads r2 with context; used below in RecordWrite.
239 __ str(r1, SlotOperand(slot, r2));
240 // Load the offset into r3.
241 int slot_offset =
242 FixedArray::kHeaderSize + slot->index() * kPointerSize;
243 __ mov(r3, Operand(slot_offset));
244 __ RecordWrite(r2, r3, r1);
245 }
246 }
247 }
248
249 // Store the arguments object. This must happen after context
250 // initialization because the arguments object may be stored in the
251 // context.
ager@chromium.org5c838252010-02-19 08:53:10 +0000252 if (scope()->arguments() != NULL) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000253 Comment cmnt(masm_, "[ allocate arguments object");
ager@chromium.org5c838252010-02-19 08:53:10 +0000254 ASSERT(scope()->arguments_shadow() != NULL);
255 Variable* arguments = scope()->arguments()->var();
256 Variable* shadow = scope()->arguments_shadow()->var();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000257 ASSERT(arguments != NULL && arguments->slot() != NULL);
258 ASSERT(shadow != NULL && shadow->slot() != NULL);
259 ArgumentsAccessStub stub(ArgumentsAccessStub::NEW_OBJECT);
260 __ ldr(r2, frame_->Function());
261 // The receiver is below the arguments, the return address, and the
262 // frame pointer on the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +0000263 const int kReceiverDisplacement = 2 + scope()->num_parameters();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000264 __ add(r1, fp, Operand(kReceiverDisplacement * kPointerSize));
ager@chromium.org5c838252010-02-19 08:53:10 +0000265 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000266 frame_->Adjust(3);
267 __ stm(db_w, sp, r0.bit() | r1.bit() | r2.bit());
268 frame_->CallStub(&stub, 3);
269 frame_->EmitPush(r0);
270 StoreToSlot(arguments->slot(), NOT_CONST_INIT);
271 StoreToSlot(shadow->slot(), NOT_CONST_INIT);
272 frame_->Drop(); // Value is no longer needed.
273 }
274
275 // Initialize ThisFunction reference if present.
ager@chromium.org5c838252010-02-19 08:53:10 +0000276 if (scope()->is_function_scope() && scope()->function() != NULL) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000277 __ mov(ip, Operand(Factory::the_hole_value()));
278 frame_->EmitPush(ip);
ager@chromium.org5c838252010-02-19 08:53:10 +0000279 StoreToSlot(scope()->function()->slot(), NOT_CONST_INIT);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000280 }
281 } else {
282 // When used as the secondary compiler for splitting, r1, cp,
283 // fp, and lr have been pushed on the stack. Adjust the virtual
284 // frame to match this state.
285 frame_->Adjust(4);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +0000286
287 // Bind all the bailout labels to the beginning of the function.
288 List<CompilationInfo::Bailout*>* bailouts = info->bailouts();
289 for (int i = 0; i < bailouts->length(); i++) {
290 __ bind(bailouts->at(i)->label());
291 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000292 }
293
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000294 // Initialize the function return target after the locals are set
295 // up, because it needs the expected frame height from the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000296 function_return_.set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000297 function_return_is_shadowed_ = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000298
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000299 // Generate code to 'execute' declarations and initialize functions
300 // (source elements). In case of an illegal redeclaration we need to
301 // handle that instead of processing the declarations.
ager@chromium.org5c838252010-02-19 08:53:10 +0000302 if (scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000303 Comment cmnt(masm_, "[ illegal redeclarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000304 scope()->VisitIllegalRedeclaration(this);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000305 } else {
306 Comment cmnt(masm_, "[ declarations");
ager@chromium.org5c838252010-02-19 08:53:10 +0000307 ProcessDeclarations(scope()->declarations());
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000308 // Bail out if a stack-overflow exception occurred when processing
309 // declarations.
kasper.lund212ac232008-07-16 07:07:30 +0000310 if (HasStackOverflow()) return;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000311 }
312
mads.s.ager31e71382008-08-13 09:32:07 +0000313 if (FLAG_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000314 frame_->CallRuntime(Runtime::kTraceEnter, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000315 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000316 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000317
318 // Compile the body of the function in a vanilla state. Don't
319 // bother compiling all the code if the scope has an illegal
320 // redeclaration.
ager@chromium.org5c838252010-02-19 08:53:10 +0000321 if (!scope()->HasIllegalRedeclaration()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000322 Comment cmnt(masm_, "[ function body");
323#ifdef DEBUG
324 bool is_builtin = Bootstrapper::IsActive();
325 bool should_trace =
326 is_builtin ? FLAG_trace_builtin_calls : FLAG_trace_calls;
mads.s.ager31e71382008-08-13 09:32:07 +0000327 if (should_trace) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000328 frame_->CallRuntime(Runtime::kDebugTrace, 0);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000329 // Ignore the return value.
mads.s.ager31e71382008-08-13 09:32:07 +0000330 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000331#endif
ager@chromium.org5c838252010-02-19 08:53:10 +0000332 VisitStatementsAndSpill(info->function()->body());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000333 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000334 }
335
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000336 // Generate the return sequence if necessary.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000337 if (has_valid_frame() || function_return_.is_linked()) {
338 if (!function_return_.is_linked()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000339 CodeForReturnPosition(info->function());
ager@chromium.org4af710e2009-09-15 12:20:11 +0000340 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000341 // exit
342 // r0: result
343 // sp: stack pointer
344 // fp: frame pointer
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000345 // cp: callee's context
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000346 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +0000347
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000348 function_return_.Bind();
349 if (FLAG_trace) {
350 // Push the return value on the stack as the parameter.
351 // Runtime::TraceExit returns the parameter as it is.
352 frame_->EmitPush(r0);
353 frame_->CallRuntime(Runtime::kTraceExit, 1);
354 }
355
ager@chromium.org4af710e2009-09-15 12:20:11 +0000356 // Add a label for checking the size of the code used for returning.
357 Label check_exit_codesize;
358 masm_->bind(&check_exit_codesize);
359
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000360 // Calculate the exact length of the return sequence and make sure that
361 // the constant pool is not emitted inside of the return sequence.
ager@chromium.org5c838252010-02-19 08:53:10 +0000362 int32_t sp_delta = (scope()->num_parameters() + 1) * kPointerSize;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000363 int return_sequence_length = Assembler::kJSReturnSequenceLength;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000364 if (!masm_->ImmediateFitsAddrMode1Instruction(sp_delta)) {
365 // Additional mov instruction generated.
366 return_sequence_length++;
367 }
368 masm_->BlockConstPoolFor(return_sequence_length);
369
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000370 // Tear down the frame which will restore the caller's frame pointer and
371 // the link register.
372 frame_->Exit();
373
ager@chromium.org4af710e2009-09-15 12:20:11 +0000374 // Here we use masm_-> instead of the __ macro to avoid the code coverage
375 // tool from instrumenting as we rely on the code size here.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000376 masm_->add(sp, sp, Operand(sp_delta));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000377 masm_->Jump(lr);
378
379 // Check that the size of the code used for returning matches what is
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000380 // expected by the debugger. The add instruction above is an addressing
381 // mode 1 instruction where there are restrictions on which immediate values
382 // can be encoded in the instruction and which immediate values requires
383 // use of an additional instruction for moving the immediate to a temporary
384 // register.
385 ASSERT_EQ(return_sequence_length,
ager@chromium.org4af710e2009-09-15 12:20:11 +0000386 masm_->InstructionsGeneratedSince(&check_exit_codesize));
mads.s.ager31e71382008-08-13 09:32:07 +0000387 }
388
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000389 // Adjust for function-level loop nesting.
390 ASSERT(loop_nesting_ == info->loop_nesting());
391 loop_nesting_ = 0;
392
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000393 // Code generation state must be reset.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000394 ASSERT(!has_cc());
395 ASSERT(state_ == NULL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000396 ASSERT(!function_return_is_shadowed_);
397 function_return_.Unuse();
398 DeleteFrame();
399
400 // Process any deferred code using the register allocator.
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000401 if (!HasStackOverflow()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000402 ProcessDeferred();
403 }
404
405 allocator_ = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000406}
407
408
ager@chromium.org7c537e22008-10-16 08:43:32 +0000409MemOperand CodeGenerator::SlotOperand(Slot* slot, Register tmp) {
410 // Currently, this assertion will fail if we try to assign to
411 // a constant variable that is constant because it is read-only
412 // (such as the variable referring to a named function expression).
413 // We need to implement assignments to read-only variables.
414 // Ideally, we should do this during AST generation (by converting
415 // such assignments into expression statements); however, in general
416 // we may not be able to make the decision until past AST generation,
417 // that is when the entire program is known.
418 ASSERT(slot != NULL);
419 int index = slot->index();
420 switch (slot->type()) {
421 case Slot::PARAMETER:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000422 return frame_->ParameterAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000423
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000424 case Slot::LOCAL:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000425 return frame_->LocalAt(index);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000426
427 case Slot::CONTEXT: {
428 // Follow the context chain if necessary.
429 ASSERT(!tmp.is(cp)); // do not overwrite context register
430 Register context = cp;
431 int chain_length = scope()->ContextChainLength(slot->var()->scope());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000432 for (int i = 0; i < chain_length; i++) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000433 // Load the closure.
434 // (All contexts, even 'with' contexts, have a closure,
435 // and it is the same for all contexts inside a function.
436 // There is no need to go to the function context first.)
437 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
438 // Load the function context (which is the incoming, outer context).
439 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
440 context = tmp;
441 }
442 // We may have a 'with' context now. Get the function context.
443 // (In fact this mov may never be the needed, since the scope analysis
444 // may not permit a direct context access in this case and thus we are
445 // always at a function context. However it is safe to dereference be-
446 // cause the function context of a function context is itself. Before
447 // deleting this mov we should try to create a counter-example first,
448 // though...)
449 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
450 return ContextOperand(tmp, index);
451 }
452
453 default:
454 UNREACHABLE();
455 return MemOperand(r0, 0);
456 }
457}
458
459
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000460MemOperand CodeGenerator::ContextSlotOperandCheckExtensions(
461 Slot* slot,
462 Register tmp,
463 Register tmp2,
464 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +0000465 ASSERT(slot->type() == Slot::CONTEXT);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000466 Register context = cp;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000467
ager@chromium.org381abbb2009-02-25 13:23:22 +0000468 for (Scope* s = scope(); s != slot->var()->scope(); s = s->outer_scope()) {
469 if (s->num_heap_slots() > 0) {
470 if (s->calls_eval()) {
471 // Check that extension is NULL.
472 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
473 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000474 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000475 }
476 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
477 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
478 context = tmp;
479 }
480 }
481 // Check that last extension is NULL.
482 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
483 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000484 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +0000485 __ ldr(tmp, ContextOperand(context, Context::FCONTEXT_INDEX));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000486 return ContextOperand(tmp, slot->index());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000487}
488
489
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000490// Loads a value on TOS. If it is a boolean value, the result may have been
491// (partially) translated into branches, or it may have set the condition
492// code register. If force_cc is set, the value is forced to set the
493// condition code register and no value is pushed. If the condition code
494// register was set, has_cc() is true and cc_reg_ contains the condition to
495// test for 'true'.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000496void CodeGenerator::LoadCondition(Expression* x,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000497 JumpTarget* true_target,
498 JumpTarget* false_target,
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000499 bool force_cc) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000500 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000501 int original_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000502
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000503 { CodeGenState new_state(this, true_target, false_target);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000504 Visit(x);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000505
506 // If we hit a stack overflow, we may not have actually visited
507 // the expression. In that case, we ensure that we have a
508 // valid-looking frame state because we will continue to generate
509 // code as we unwind the C++ stack.
510 //
511 // It's possible to have both a stack overflow and a valid frame
512 // state (eg, a subexpression overflowed, visiting it returned
513 // with a dummied frame state, and visiting this expression
514 // returned with a normal-looking state).
515 if (HasStackOverflow() &&
516 has_valid_frame() &&
517 !has_cc() &&
518 frame_->height() == original_height) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000519 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000520 true_target->Jump();
521 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000522 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000523 if (force_cc && frame_ != NULL && !has_cc()) {
mads.s.ager31e71382008-08-13 09:32:07 +0000524 // Convert the TOS value to a boolean in the condition code register.
525 ToBoolean(true_target, false_target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000526 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000527 ASSERT(!force_cc || !has_valid_frame() || has_cc());
528 ASSERT(!has_valid_frame() ||
529 (has_cc() && frame_->height() == original_height) ||
530 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000531}
532
533
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000534void CodeGenerator::Load(Expression* expr) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000535#ifdef DEBUG
536 int original_height = frame_->height();
537#endif
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000538 JumpTarget true_target;
539 JumpTarget false_target;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000540 LoadCondition(expr, &true_target, &false_target, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000541
542 if (has_cc()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000543 // Convert cc_reg_ into a boolean value.
ager@chromium.org357bf652010-04-12 11:30:10 +0000544 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000545 JumpTarget loaded;
546 JumpTarget materialize_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000547 materialize_true.Branch(cc_reg_);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000548 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000549 frame_->EmitPush(r0);
550 loaded.Jump();
551 materialize_true.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000552 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000553 frame_->EmitPush(r0);
554 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000555 cc_reg_ = al;
556 }
557
558 if (true_target.is_linked() || false_target.is_linked()) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000559 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000560 // We have at least one condition value that has been "translated"
561 // into a branch, thus it needs to be loaded explicitly.
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000562 JumpTarget loaded;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000563 if (frame_ != NULL) {
564 loaded.Jump(); // Don't lose the current TOS.
565 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000566 bool both = true_target.is_linked() && false_target.is_linked();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000567 // Load "true" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000568 if (true_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000569 true_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000570 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000571 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000572 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000573 // If both "true" and "false" need to be loaded jump across the code for
574 // "false".
575 if (both) {
576 loaded.Jump();
577 }
578 // Load "false" if necessary.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000579 if (false_target.is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000580 false_target.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000581 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000582 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000583 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000584 // A value is loaded on all paths reaching this point.
585 loaded.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000586 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000587 ASSERT(has_valid_frame());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000588 ASSERT(!has_cc());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000589 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000590}
591
592
ager@chromium.org7c537e22008-10-16 08:43:32 +0000593void CodeGenerator::LoadGlobal() {
ager@chromium.org357bf652010-04-12 11:30:10 +0000594 VirtualFrame::SpilledScope spilled_scope(frame_);
mads.s.ager31e71382008-08-13 09:32:07 +0000595 __ ldr(r0, GlobalObject());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000596 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000597}
598
599
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000600void CodeGenerator::LoadGlobalReceiver(Register scratch) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000601 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000602 __ ldr(scratch, ContextOperand(cp, Context::GLOBAL_INDEX));
603 __ ldr(scratch,
604 FieldMemOperand(scratch, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000605 frame_->EmitPush(scratch);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000606}
607
608
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000609void CodeGenerator::LoadTypeofExpression(Expression* expr) {
610 // Special handling of identifiers as subexpressions of typeof.
ager@chromium.org357bf652010-04-12 11:30:10 +0000611 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000612 Variable* variable = expr->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000613 if (variable != NULL && !variable->is_this() && variable->is_global()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000614 // For a global variable we build the property reference
615 // <global>.<variable> and perform a (regular non-contextual) property
616 // load to make sure we do not get reference errors.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000617 Slot global(variable, Slot::CONTEXT, Context::GLOBAL_INDEX);
618 Literal key(variable->name());
ager@chromium.org236ad962008-09-25 09:45:57 +0000619 Property property(&global, &key, RelocInfo::kNoPosition);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000620 Reference ref(this, &property);
ager@chromium.org357bf652010-04-12 11:30:10 +0000621 ref.GetValue();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000622 } else if (variable != NULL && variable->slot() != NULL) {
623 // For a variable that rewrites to a slot, we signal it is the immediate
624 // subexpression of a typeof.
625 LoadFromSlot(variable->slot(), INSIDE_TYPEOF);
626 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627 } else {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000628 // Anything else can be handled normally.
629 LoadAndSpill(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000630 }
631}
632
633
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000634Reference::Reference(CodeGenerator* cgen,
635 Expression* expression,
636 bool persist_after_get)
637 : cgen_(cgen),
638 expression_(expression),
639 type_(ILLEGAL),
640 persist_after_get_(persist_after_get) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641 cgen->LoadReference(this);
642}
643
644
645Reference::~Reference() {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000646 ASSERT(is_unloaded() || is_illegal());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000647}
648
649
ager@chromium.org7c537e22008-10-16 08:43:32 +0000650void CodeGenerator::LoadReference(Reference* ref) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000651 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000652 Comment cmnt(masm_, "[ LoadReference");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000653 Expression* e = ref->expression();
654 Property* property = e->AsProperty();
655 Variable* var = e->AsVariableProxy()->AsVariable();
656
657 if (property != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000658 // The expression is either a property or a variable proxy that rewrites
659 // to a property.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000660 LoadAndSpill(property->obj());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000661 if (property->key()->IsPropertyName()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000662 ref->set_type(Reference::NAMED);
663 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000664 LoadAndSpill(property->key());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000665 ref->set_type(Reference::KEYED);
666 }
667 } else if (var != NULL) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000668 // The expression is a variable proxy that does not rewrite to a
669 // property. Global variables are treated as named property references.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000670 if (var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000671 LoadGlobal();
672 ref->set_type(Reference::NAMED);
673 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000674 ASSERT(var->slot() != NULL);
675 ref->set_type(Reference::SLOT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000676 }
677 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000678 // Anything else is a runtime error.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000679 LoadAndSpill(e);
680 frame_->CallRuntime(Runtime::kThrowReferenceError, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000681 }
682}
683
684
ager@chromium.org7c537e22008-10-16 08:43:32 +0000685void CodeGenerator::UnloadReference(Reference* ref) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000686 int size = ref->size();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000687 ref->set_unloaded();
ager@chromium.org357bf652010-04-12 11:30:10 +0000688 if (size == 0) return;
689
690 // Pop a reference from the stack while preserving TOS.
691 VirtualFrame::RegisterAllocationScope scope(this);
692 Comment cmnt(masm_, "[ UnloadReference");
693 if (size > 0) {
694 Register tos = frame_->PopToRegister();
695 frame_->Drop(size);
696 frame_->EmitPush(tos);
697 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000698}
699
700
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000701// ECMA-262, section 9.2, page 30: ToBoolean(). Convert the given
702// register to a boolean in the condition code register. The code
703// may jump to 'false_target' in case the register converts to 'false'.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000704void CodeGenerator::ToBoolean(JumpTarget* true_target,
705 JumpTarget* false_target) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000706 VirtualFrame::SpilledScope spilled_scope(frame_);
mads.s.ager31e71382008-08-13 09:32:07 +0000707 // Note: The generated code snippet does not change stack variables.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708 // Only the condition code should be set.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000709 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000710
711 // Fast case checks
712
mads.s.ager31e71382008-08-13 09:32:07 +0000713 // Check if the value is 'false'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000714 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
715 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000716 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000717
mads.s.ager31e71382008-08-13 09:32:07 +0000718 // Check if the value is 'true'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000719 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
720 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000721 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000722
mads.s.ager31e71382008-08-13 09:32:07 +0000723 // Check if the value is 'undefined'.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000724 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
725 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000726 false_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000727
mads.s.ager31e71382008-08-13 09:32:07 +0000728 // Check if the value is a smi.
729 __ cmp(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000730 false_target->Branch(eq);
mads.s.ager31e71382008-08-13 09:32:07 +0000731 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000732 true_target->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000733
734 // Slow case: call the runtime.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000735 frame_->EmitPush(r0);
736 frame_->CallRuntime(Runtime::kToBool, 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000737 // Convert the result (r0) to a condition code.
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000738 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
739 __ cmp(r0, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000740
741 cc_reg_ = ne;
742}
743
744
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000745void CodeGenerator::GenericBinaryOperation(Token::Value op,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000746 OverwriteMode overwrite_mode,
747 int constant_rhs) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000748 VirtualFrame::SpilledScope spilled_scope(frame_);
mads.s.ager31e71382008-08-13 09:32:07 +0000749 // sp[0] : y
750 // sp[1] : x
751 // result : r0
752
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000753 // Stub is entered with a call: 'return address' is in lr.
754 switch (op) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000755 case Token::ADD:
756 case Token::SUB:
757 case Token::MUL:
758 case Token::DIV:
759 case Token::MOD:
760 case Token::BIT_OR:
761 case Token::BIT_AND:
762 case Token::BIT_XOR:
763 case Token::SHL:
764 case Token::SHR:
765 case Token::SAR: {
766 frame_->EmitPop(r0); // r0 : y
767 frame_->EmitPop(r1); // r1 : x
768 GenericBinaryOpStub stub(op, overwrite_mode, r1, r0, constant_rhs);
769 frame_->CallStub(&stub, 0);
770 break;
771 }
772
773 case Token::COMMA:
774 frame_->EmitPop(r0);
775 // Simply discard left value.
776 frame_->Drop();
777 break;
778
779 default:
780 // Other cases should have been handled before this point.
781 UNREACHABLE();
782 break;
783 }
784}
785
786
787void CodeGenerator::VirtualFrameBinaryOperation(Token::Value op,
788 OverwriteMode overwrite_mode,
789 int constant_rhs) {
790 // top of virtual frame: y
791 // 2nd elt. on virtual frame : x
792 // result : top of virtual frame
793
794 // Stub is entered with a call: 'return address' is in lr.
795 switch (op) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000796 case Token::ADD: // fall through.
797 case Token::SUB: // fall through.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000798 case Token::MUL:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000799 case Token::DIV:
800 case Token::MOD:
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000801 case Token::BIT_OR:
802 case Token::BIT_AND:
803 case Token::BIT_XOR:
804 case Token::SHL:
805 case Token::SHR:
806 case Token::SAR: {
ager@chromium.org357bf652010-04-12 11:30:10 +0000807 Register rhs = frame_->PopToRegister();
808 Register lhs = frame_->PopToRegister(rhs); // Don't pop to rhs register.
809 {
810 VirtualFrame::SpilledScope spilled_scope(frame_);
811 GenericBinaryOpStub stub(op, overwrite_mode, lhs, rhs, constant_rhs);
812 frame_->CallStub(&stub, 0);
813 }
814 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000815 break;
816 }
817
ager@chromium.org357bf652010-04-12 11:30:10 +0000818 case Token::COMMA: {
819 Register scratch = frame_->PopToRegister();
820 // Simply discard left value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000821 frame_->Drop();
ager@chromium.org357bf652010-04-12 11:30:10 +0000822 frame_->EmitPush(scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000823 break;
ager@chromium.org357bf652010-04-12 11:30:10 +0000824 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000825
826 default:
827 // Other cases should have been handled before this point.
828 UNREACHABLE();
829 break;
830 }
831}
832
833
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000834class DeferredInlineSmiOperation: public DeferredCode {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000835 public:
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000836 DeferredInlineSmiOperation(Token::Value op,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000837 int value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000838 bool reversed,
ager@chromium.org357bf652010-04-12 11:30:10 +0000839 OverwriteMode overwrite_mode,
840 Register tos)
sgjesse@chromium.org755c5b12009-05-29 11:04:38 +0000841 : op_(op),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000842 value_(value),
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000843 reversed_(reversed),
ager@chromium.org357bf652010-04-12 11:30:10 +0000844 overwrite_mode_(overwrite_mode),
845 tos_register_(tos) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000846 set_comment("[ DeferredInlinedSmiOperation");
847 }
848
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000849 virtual void Generate();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000850
851 private:
852 Token::Value op_;
853 int value_;
854 bool reversed_;
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000855 OverwriteMode overwrite_mode_;
ager@chromium.org357bf652010-04-12 11:30:10 +0000856 Register tos_register_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000857};
858
859
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000860void DeferredInlineSmiOperation::Generate() {
ager@chromium.org357bf652010-04-12 11:30:10 +0000861 Register lhs = r1;
862 Register rhs = r0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000863 switch (op_) {
864 case Token::ADD: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000865 // Revert optimistic add.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000866 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000867 __ sub(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000868 __ mov(r1, Operand(Smi::FromInt(value_)));
869 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000870 __ sub(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000871 __ mov(r0, Operand(Smi::FromInt(value_)));
872 }
873 break;
874 }
875
876 case Token::SUB: {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000877 // Revert optimistic sub.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000878 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000879 __ rsb(r0, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000880 __ mov(r1, Operand(Smi::FromInt(value_)));
881 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000882 __ add(r1, tos_register_, Operand(Smi::FromInt(value_)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000883 __ mov(r0, Operand(Smi::FromInt(value_)));
884 }
885 break;
886 }
887
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000888 // For these operations there is no optimistic operation that needs to be
889 // reverted.
890 case Token::MUL:
891 case Token::MOD:
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000892 case Token::BIT_OR:
893 case Token::BIT_XOR:
894 case Token::BIT_AND: {
895 if (reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000896 if (tos_register_.is(r0)) {
897 __ mov(r1, Operand(Smi::FromInt(value_)));
898 } else {
899 ASSERT(tos_register_.is(r1));
900 __ mov(r0, Operand(Smi::FromInt(value_)));
901 lhs = r0;
902 rhs = r1;
903 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000904 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +0000905 if (tos_register_.is(r1)) {
906 __ mov(r0, Operand(Smi::FromInt(value_)));
907 } else {
908 ASSERT(tos_register_.is(r0));
909 __ mov(r1, Operand(Smi::FromInt(value_)));
910 lhs = r0;
911 rhs = r1;
912 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000913 }
914 break;
915 }
916
917 case Token::SHL:
918 case Token::SHR:
919 case Token::SAR: {
920 if (!reversed_) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000921 if (tos_register_.is(r1)) {
922 __ mov(r0, Operand(Smi::FromInt(value_)));
923 } else {
924 ASSERT(tos_register_.is(r0));
925 __ mov(r1, Operand(Smi::FromInt(value_)));
926 lhs = r0;
927 rhs = r1;
928 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000929 } else {
ager@chromium.orge2902be2009-06-08 12:21:35 +0000930 UNREACHABLE(); // Should have been handled in SmiOperation.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000931 }
932 break;
933 }
934
935 default:
ager@chromium.orge2902be2009-06-08 12:21:35 +0000936 // Other cases should have been handled before this point.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000937 UNREACHABLE();
938 break;
939 }
940
ager@chromium.org357bf652010-04-12 11:30:10 +0000941 GenericBinaryOpStub stub(op_, overwrite_mode_, lhs, rhs, value_);
ager@chromium.orge2902be2009-06-08 12:21:35 +0000942 __ CallStub(&stub);
ager@chromium.org357bf652010-04-12 11:30:10 +0000943 // The generic stub returns its value in r0, but that's not
944 // necessarily what we want. We want whatever the inlined code
945 // expected, which is that the answer is in the same register as
946 // the operand was.
947 __ Move(tos_register_, r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000948}
949
950
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000951static bool PopCountLessThanEqual2(unsigned int x) {
952 x &= x - 1;
953 return (x & (x - 1)) == 0;
954}
955
956
957// Returns the index of the lowest bit set.
958static int BitPosition(unsigned x) {
959 int bit_posn = 0;
960 while ((x & 0xf) == 0) {
961 bit_posn += 4;
962 x >>= 4;
963 }
964 while ((x & 1) == 0) {
965 bit_posn++;
966 x >>= 1;
967 }
968 return bit_posn;
969}
970
971
ager@chromium.org357bf652010-04-12 11:30:10 +0000972void CodeGenerator::VirtualFrameSmiOperation(Token::Value op,
973 Handle<Object> value,
974 bool reversed,
975 OverwriteMode mode) {
976 int int_value = Smi::cast(*value)->value();
977
978 bool something_to_inline;
979 switch (op) {
980 case Token::ADD:
981 case Token::SUB:
982 case Token::BIT_AND:
983 case Token::BIT_OR:
984 case Token::BIT_XOR: {
985 something_to_inline = true;
986 break;
987 }
988 case Token::SHL:
989 case Token::SHR:
990 case Token::SAR: {
991 if (reversed) {
992 something_to_inline = false;
993 } else {
994 something_to_inline = true;
995 }
996 break;
997 }
998 case Token::MOD: {
999 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
1000 something_to_inline = false;
1001 } else {
1002 something_to_inline = true;
1003 }
1004 break;
1005 }
1006 case Token::MUL: {
1007 if (!IsEasyToMultiplyBy(int_value)) {
1008 something_to_inline = false;
1009 } else {
1010 something_to_inline = true;
1011 }
1012 break;
1013 }
1014 default: {
1015 something_to_inline = false;
1016 break;
1017 }
1018 }
1019
1020 if (!something_to_inline) {
1021 if (!reversed) {
1022 // Push the rhs onto the virtual frame by putting it in a TOS register.
1023 Register rhs = frame_->GetTOSRegister();
1024 __ mov(rhs, Operand(value));
1025 frame_->EmitPush(rhs);
1026 VirtualFrameBinaryOperation(op, mode, int_value);
1027 } else {
1028 // Pop the rhs, then push lhs and rhs in the right order. Only performs
1029 // at most one pop, the rest takes place in TOS registers.
1030 Register lhs = frame_->GetTOSRegister(); // Get reg for pushing.
1031 Register rhs = frame_->PopToRegister(lhs); // Don't use lhs for this.
1032 __ mov(lhs, Operand(value));
1033 frame_->EmitPush(lhs);
1034 frame_->EmitPush(rhs);
1035 VirtualFrameBinaryOperation(op, mode, kUnknownIntValue);
1036 }
1037 return;
1038 }
1039
1040 // We move the top of stack to a register (normally no move is invoved).
1041 Register tos = frame_->PopToRegister();
1042 // All other registers are spilled. The deferred code expects one argument
1043 // in a register and all other values are flushed to the stack. The
1044 // answer is returned in the same register that the top of stack argument was
1045 // in.
1046 frame_->SpillAll();
1047
1048 switch (op) {
1049 case Token::ADD: {
1050 DeferredCode* deferred =
1051 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1052
1053 __ add(tos, tos, Operand(value), SetCC);
1054 deferred->Branch(vs);
1055 __ tst(tos, Operand(kSmiTagMask));
1056 deferred->Branch(ne);
1057 deferred->BindExit();
1058 frame_->EmitPush(tos);
1059 break;
1060 }
1061
1062 case Token::SUB: {
1063 DeferredCode* deferred =
1064 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1065
1066 if (reversed) {
1067 __ rsb(tos, tos, Operand(value), SetCC);
1068 } else {
1069 __ sub(tos, tos, Operand(value), SetCC);
1070 }
1071 deferred->Branch(vs);
1072 __ tst(tos, Operand(kSmiTagMask));
1073 deferred->Branch(ne);
1074 deferred->BindExit();
1075 frame_->EmitPush(tos);
1076 break;
1077 }
1078
1079
1080 case Token::BIT_OR:
1081 case Token::BIT_XOR:
1082 case Token::BIT_AND: {
1083 DeferredCode* deferred =
1084 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1085 __ tst(tos, Operand(kSmiTagMask));
1086 deferred->Branch(ne);
1087 switch (op) {
1088 case Token::BIT_OR: __ orr(tos, tos, Operand(value)); break;
1089 case Token::BIT_XOR: __ eor(tos, tos, Operand(value)); break;
1090 case Token::BIT_AND: __ and_(tos, tos, Operand(value)); break;
1091 default: UNREACHABLE();
1092 }
1093 deferred->BindExit();
1094 frame_->EmitPush(tos);
1095 break;
1096 }
1097
1098 case Token::SHL:
1099 case Token::SHR:
1100 case Token::SAR: {
1101 ASSERT(!reversed);
1102 Register scratch = VirtualFrame::scratch0();
1103 Register scratch2 = VirtualFrame::scratch1();
1104 int shift_value = int_value & 0x1f; // least significant 5 bits
1105 DeferredCode* deferred =
1106 new DeferredInlineSmiOperation(op, shift_value, false, mode, tos);
1107 __ tst(tos, Operand(kSmiTagMask));
1108 deferred->Branch(ne);
1109 __ mov(scratch, Operand(tos, ASR, kSmiTagSize)); // remove tags
1110 switch (op) {
1111 case Token::SHL: {
1112 if (shift_value != 0) {
1113 __ mov(scratch, Operand(scratch, LSL, shift_value));
1114 }
1115 // check that the *signed* result fits in a smi
1116 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
1117 deferred->Branch(mi);
1118 break;
1119 }
1120 case Token::SHR: {
1121 // LSR by immediate 0 means shifting 32 bits.
1122 if (shift_value != 0) {
1123 __ mov(scratch, Operand(scratch, LSR, shift_value));
1124 }
1125 // check that the *unsigned* result fits in a smi
1126 // neither of the two high-order bits can be set:
1127 // - 0x80000000: high bit would be lost when smi tagging
1128 // - 0x40000000: this number would convert to negative when
1129 // smi tagging these two cases can only happen with shifts
1130 // by 0 or 1 when handed a valid smi
1131 __ tst(scratch, Operand(0xc0000000));
1132 deferred->Branch(ne);
1133 break;
1134 }
1135 case Token::SAR: {
1136 if (shift_value != 0) {
1137 // ASR by immediate 0 means shifting 32 bits.
1138 __ mov(scratch, Operand(scratch, ASR, shift_value));
1139 }
1140 break;
1141 }
1142 default: UNREACHABLE();
1143 }
1144 __ mov(tos, Operand(scratch, LSL, kSmiTagSize));
1145 deferred->BindExit();
1146 frame_->EmitPush(tos);
1147 break;
1148 }
1149
1150 case Token::MOD: {
1151 ASSERT(!reversed);
1152 ASSERT(int_value >= 2);
1153 ASSERT(IsPowerOf2(int_value));
1154 DeferredCode* deferred =
1155 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1156 unsigned mask = (0x80000000u | kSmiTagMask);
1157 __ tst(tos, Operand(mask));
1158 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1159 mask = (int_value << kSmiTagSize) - 1;
1160 __ and_(tos, tos, Operand(mask));
1161 deferred->BindExit();
1162 frame_->EmitPush(tos);
1163 break;
1164 }
1165
1166 case Token::MUL: {
1167 ASSERT(IsEasyToMultiplyBy(int_value));
1168 DeferredCode* deferred =
1169 new DeferredInlineSmiOperation(op, int_value, reversed, mode, tos);
1170 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1171 max_smi_that_wont_overflow <<= kSmiTagSize;
1172 unsigned mask = 0x80000000u;
1173 while ((mask & max_smi_that_wont_overflow) == 0) {
1174 mask |= mask >> 1;
1175 }
1176 mask |= kSmiTagMask;
1177 // This does a single mask that checks for a too high value in a
1178 // conservative way and for a non-Smi. It also filters out negative
1179 // numbers, unfortunately, but since this code is inline we prefer
1180 // brevity to comprehensiveness.
1181 __ tst(tos, Operand(mask));
1182 deferred->Branch(ne);
1183 MultiplyByKnownInt(masm_, tos, tos, int_value);
1184 deferred->BindExit();
1185 frame_->EmitPush(tos);
1186 break;
1187 }
1188
1189 default:
1190 UNREACHABLE();
1191 break;
1192 }
1193}
1194
1195
ager@chromium.org7c537e22008-10-16 08:43:32 +00001196void CodeGenerator::SmiOperation(Token::Value op,
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001197 Handle<Object> value,
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001198 bool reversed,
1199 OverwriteMode mode) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001200 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201 // NOTE: This is an attempt to inline (a bit) more of the code for
1202 // some possible smi operations (like + and -) when (at least) one
1203 // of the operands is a literal smi. With this optimization, the
1204 // performance of the system is increased by ~15%, and the generated
1205 // code size is increased by ~1% (measured on a combination of
1206 // different benchmarks).
1207
mads.s.ager31e71382008-08-13 09:32:07 +00001208 // sp[0] : operand
1209
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001210 int int_value = Smi::cast(*value)->value();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001211
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001212 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001213 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001214
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001215 bool something_to_inline = true;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001216 switch (op) {
1217 case Token::ADD: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001218 DeferredCode* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001219 new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001220
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001221 __ add(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001222 deferred->Branch(vs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001223 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +00001224 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001225 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001226 break;
1227 }
1228
1229 case Token::SUB: {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001230 DeferredCode* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001231 new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001232
ager@chromium.orge2902be2009-06-08 12:21:35 +00001233 if (reversed) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001234 __ rsb(r0, r0, Operand(value), SetCC);
ager@chromium.orge2902be2009-06-08 12:21:35 +00001235 } else {
1236 __ sub(r0, r0, Operand(value), SetCC);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001237 }
ager@chromium.orge2902be2009-06-08 12:21:35 +00001238 deferred->Branch(vs);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001239 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +00001240 deferred->Branch(ne);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001241 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001242 break;
1243 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001244
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001245
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001246 case Token::BIT_OR:
1247 case Token::BIT_XOR:
1248 case Token::BIT_AND: {
1249 DeferredCode* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001250 new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001251 __ tst(r0, Operand(kSmiTagMask));
ager@chromium.orge2902be2009-06-08 12:21:35 +00001252 deferred->Branch(ne);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001253 switch (op) {
1254 case Token::BIT_OR: __ orr(r0, r0, Operand(value)); break;
1255 case Token::BIT_XOR: __ eor(r0, r0, Operand(value)); break;
1256 case Token::BIT_AND: __ and_(r0, r0, Operand(value)); break;
1257 default: UNREACHABLE();
1258 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001259 deferred->BindExit();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001260 break;
1261 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001262
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001263 case Token::SHL:
1264 case Token::SHR:
1265 case Token::SAR: {
1266 if (reversed) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001267 something_to_inline = false;
1268 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00001269 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001270 int shift_value = int_value & 0x1f; // least significant 5 bits
1271 DeferredCode* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001272 new DeferredInlineSmiOperation(op, shift_value, false, mode, r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001273 __ tst(r0, Operand(kSmiTagMask));
1274 deferred->Branch(ne);
1275 __ mov(r2, Operand(r0, ASR, kSmiTagSize)); // remove tags
1276 switch (op) {
1277 case Token::SHL: {
1278 if (shift_value != 0) {
1279 __ mov(r2, Operand(r2, LSL, shift_value));
1280 }
1281 // check that the *unsigned* result fits in a smi
1282 __ add(r3, r2, Operand(0x40000000), SetCC);
1283 deferred->Branch(mi);
1284 break;
1285 }
1286 case Token::SHR: {
1287 // LSR by immediate 0 means shifting 32 bits.
1288 if (shift_value != 0) {
1289 __ mov(r2, Operand(r2, LSR, shift_value));
1290 }
1291 // check that the *unsigned* result fits in a smi
1292 // neither of the two high-order bits can be set:
1293 // - 0x80000000: high bit would be lost when smi tagging
1294 // - 0x40000000: this number would convert to negative when
1295 // smi tagging these two cases can only happen with shifts
1296 // by 0 or 1 when handed a valid smi
1297 __ and_(r3, r2, Operand(0xc0000000), SetCC);
1298 deferred->Branch(ne);
1299 break;
1300 }
1301 case Token::SAR: {
1302 if (shift_value != 0) {
1303 // ASR by immediate 0 means shifting 32 bits.
1304 __ mov(r2, Operand(r2, ASR, shift_value));
1305 }
1306 break;
1307 }
1308 default: UNREACHABLE();
1309 }
1310 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
1311 deferred->BindExit();
1312 break;
1313 }
1314
1315 case Token::MOD: {
1316 if (reversed || int_value < 2 || !IsPowerOf2(int_value)) {
1317 something_to_inline = false;
1318 break;
1319 }
1320 DeferredCode* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001321 new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001322 unsigned mask = (0x80000000u | kSmiTagMask);
1323 __ tst(r0, Operand(mask));
1324 deferred->Branch(ne); // Go to deferred code on non-Smis and negative.
1325 mask = (int_value << kSmiTagSize) - 1;
1326 __ and_(r0, r0, Operand(mask));
1327 deferred->BindExit();
1328 break;
1329 }
1330
1331 case Token::MUL: {
1332 if (!IsEasyToMultiplyBy(int_value)) {
1333 something_to_inline = false;
1334 break;
1335 }
1336 DeferredCode* deferred =
ager@chromium.org357bf652010-04-12 11:30:10 +00001337 new DeferredInlineSmiOperation(op, int_value, reversed, mode, r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001338 unsigned max_smi_that_wont_overflow = Smi::kMaxValue / int_value;
1339 max_smi_that_wont_overflow <<= kSmiTagSize;
1340 unsigned mask = 0x80000000u;
1341 while ((mask & max_smi_that_wont_overflow) == 0) {
1342 mask |= mask >> 1;
1343 }
1344 mask |= kSmiTagMask;
1345 // This does a single mask that checks for a too high value in a
1346 // conservative way and for a non-Smi. It also filters out negative
1347 // numbers, unfortunately, but since this code is inline we prefer
1348 // brevity to comprehensiveness.
1349 __ tst(r0, Operand(mask));
1350 deferred->Branch(ne);
1351 MultiplyByKnownInt(masm_, r0, r0, int_value);
1352 deferred->BindExit();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001353 break;
1354 }
1355
1356 default:
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001357 something_to_inline = false;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001358 break;
1359 }
1360
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00001361 if (!something_to_inline) {
1362 if (!reversed) {
1363 frame_->EmitPush(r0);
1364 __ mov(r0, Operand(value));
1365 frame_->EmitPush(r0);
1366 GenericBinaryOperation(op, mode, int_value);
1367 } else {
1368 __ mov(ip, Operand(value));
1369 frame_->EmitPush(ip);
1370 frame_->EmitPush(r0);
1371 GenericBinaryOperation(op, mode, kUnknownIntValue);
1372 }
1373 }
1374
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001375 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001376}
1377
1378
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001379void CodeGenerator::Comparison(Condition cc,
1380 Expression* left,
1381 Expression* right,
1382 bool strict) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001383 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001384
ager@chromium.org357bf652010-04-12 11:30:10 +00001385 if (left != NULL) Load(left);
1386 if (right != NULL) Load(right);
1387
mads.s.ager31e71382008-08-13 09:32:07 +00001388 // sp[0] : y
1389 // sp[1] : x
1390 // result : cc register
1391
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392 // Strict only makes sense for equality comparisons.
1393 ASSERT(!strict || cc == eq);
1394
ager@chromium.org357bf652010-04-12 11:30:10 +00001395 Register lhs;
1396 Register rhs;
1397
1398 // We load the top two stack positions into registers chosen by the virtual
1399 // frame. This should keep the register shuffling to a minimum.
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001400 // Implement '>' and '<=' by reversal to obtain ECMA-262 conversion order.
1401 if (cc == gt || cc == le) {
1402 cc = ReverseCondition(cc);
ager@chromium.org357bf652010-04-12 11:30:10 +00001403 lhs = frame_->PopToRegister();
1404 rhs = frame_->PopToRegister(lhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001405 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00001406 rhs = frame_->PopToRegister();
1407 lhs = frame_->PopToRegister(rhs); // Don't pop to the same register again!
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +00001408 }
ager@chromium.org357bf652010-04-12 11:30:10 +00001409
1410 ASSERT(rhs.is(r0) || rhs.is(r1));
1411 ASSERT(lhs.is(r0) || lhs.is(r1));
1412
1413 // Now we have the two sides in r0 and r1. We flush any other registers
1414 // because the stub doesn't know about register allocation.
1415 frame_->SpillAll();
1416 Register scratch = VirtualFrame::scratch0();
1417 __ orr(scratch, lhs, Operand(rhs));
1418 __ tst(scratch, Operand(kSmiTagMask));
1419 JumpTarget smi;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001420 smi.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001421
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001422 // Perform non-smi comparison by stub.
1423 // CompareStub takes arguments in r0 and r1, returns <0, >0 or 0 in r0.
1424 // We call with 0 args because there are 0 on the stack.
ager@chromium.org357bf652010-04-12 11:30:10 +00001425 if (!rhs.is(r0)) {
1426 __ Swap(rhs, lhs, ip);
1427 }
1428
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001429 CompareStub stub(cc, strict);
1430 frame_->CallStub(&stub, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001431 __ cmp(r0, Operand(0));
ager@chromium.org357bf652010-04-12 11:30:10 +00001432 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001433 exit.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001434
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001435 // Do smi comparisons by pointer comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001436 smi.Bind();
ager@chromium.org357bf652010-04-12 11:30:10 +00001437 __ cmp(lhs, Operand(rhs));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001439 exit.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001440 cc_reg_ = cc;
1441}
1442
1443
mads.s.ager31e71382008-08-13 09:32:07 +00001444// Call the function on the stack with the given arguments.
ager@chromium.org7c537e22008-10-16 08:43:32 +00001445void CodeGenerator::CallWithArguments(ZoneList<Expression*>* args,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001446 CallFunctionFlags flags,
1447 int position) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001448 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449 // Push the arguments ("left-to-right") on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001450 int arg_count = args->length();
1451 for (int i = 0; i < arg_count; i++) {
1452 LoadAndSpill(args->at(i));
mads.s.ager31e71382008-08-13 09:32:07 +00001453 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001454
kasper.lund7276f142008-07-30 08:49:36 +00001455 // Record the position for debugging purposes.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001456 CodeForSourcePosition(position);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001457
kasper.lund7276f142008-07-30 08:49:36 +00001458 // Use the shared code stub to call the function.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001459 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001460 CallFunctionStub call_function(arg_count, in_loop, flags);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001461 frame_->CallStub(&call_function, arg_count + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001462
1463 // Restore context and pop function from the stack.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001464 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001465 frame_->Drop(); // discard the TOS
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001466}
1467
1468
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001469void CodeGenerator::Branch(bool if_true, JumpTarget* target) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001470 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001471 ASSERT(has_cc());
1472 Condition cc = if_true ? cc_reg_ : NegateCondition(cc_reg_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001473 target->Branch(cc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001474 cc_reg_ = al;
1475}
1476
1477
ager@chromium.org7c537e22008-10-16 08:43:32 +00001478void CodeGenerator::CheckStack() {
ager@chromium.org357bf652010-04-12 11:30:10 +00001479 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3811b432009-10-28 14:53:37 +00001480 Comment cmnt(masm_, "[ check stack");
1481 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1482 // Put the lr setup instruction in the delay slot. kInstrSize is added to
1483 // the implicit 8 byte offset that always applies to operations with pc and
1484 // gives a return address 12 bytes down.
1485 masm_->add(lr, pc, Operand(Assembler::kInstrSize));
1486 masm_->cmp(sp, Operand(ip));
1487 StackCheckStub stub;
1488 // Call the stub if lower.
1489 masm_->mov(pc,
1490 Operand(reinterpret_cast<intptr_t>(stub.GetCode().location()),
1491 RelocInfo::CODE_TARGET),
1492 LeaveCC,
1493 lo);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001494}
1495
1496
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001497void CodeGenerator::VisitStatements(ZoneList<Statement*>* statements) {
1498#ifdef DEBUG
1499 int original_height = frame_->height();
1500#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001501 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001502 for (int i = 0; frame_ != NULL && i < statements->length(); i++) {
1503 VisitAndSpill(statements->at(i));
1504 }
1505 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1506}
1507
1508
ager@chromium.org7c537e22008-10-16 08:43:32 +00001509void CodeGenerator::VisitBlock(Block* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001510#ifdef DEBUG
1511 int original_height = frame_->height();
1512#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001513 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001514 Comment cmnt(masm_, "[ Block");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001515 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001516 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001517 VisitStatementsAndSpill(node->statements());
1518 if (node->break_target()->is_linked()) {
1519 node->break_target()->Bind();
1520 }
1521 node->break_target()->Unuse();
1522 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001523}
1524
1525
ager@chromium.org7c537e22008-10-16 08:43:32 +00001526void CodeGenerator::DeclareGlobals(Handle<FixedArray> pairs) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001527 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3811b432009-10-28 14:53:37 +00001528 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001529 __ mov(r0, Operand(pairs));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001530 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00001531 __ mov(r0, Operand(Smi::FromInt(is_eval() ? 1 : 0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001532 frame_->EmitPush(r0);
1533 frame_->CallRuntime(Runtime::kDeclareGlobals, 3);
mads.s.ager31e71382008-08-13 09:32:07 +00001534 // The result is discarded.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001535}
1536
1537
ager@chromium.org7c537e22008-10-16 08:43:32 +00001538void CodeGenerator::VisitDeclaration(Declaration* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001539#ifdef DEBUG
1540 int original_height = frame_->height();
1541#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001542 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001543 Comment cmnt(masm_, "[ Declaration");
1544 Variable* var = node->proxy()->var();
1545 ASSERT(var != NULL); // must have been resolved
1546 Slot* slot = var->slot();
1547
1548 // If it was not possible to allocate the variable at compile time,
1549 // we need to "declare" it at runtime to make sure it actually
1550 // exists in the local context.
1551 if (slot != NULL && slot->type() == Slot::LOOKUP) {
1552 // Variables with a "LOOKUP" slot were introduced as non-locals
1553 // during variable resolution and must have mode DYNAMIC.
ager@chromium.org381abbb2009-02-25 13:23:22 +00001554 ASSERT(var->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001555 // For now, just do a runtime call.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001556 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00001557 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001558 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001559 // Declaration nodes are always declared in only two modes.
1560 ASSERT(node->mode() == Variable::VAR || node->mode() == Variable::CONST);
1561 PropertyAttributes attr = node->mode() == Variable::VAR ? NONE : READ_ONLY;
mads.s.ager31e71382008-08-13 09:32:07 +00001562 __ mov(r0, Operand(Smi::FromInt(attr)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001563 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001564 // Push initial value, if any.
1565 // Note: For variables we must not push an initial value (such as
1566 // 'undefined') because we may have a (legal) redeclaration and we
1567 // must not destroy the current value.
1568 if (node->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00001569 __ LoadRoot(r0, Heap::kTheHoleValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001570 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001571 } else if (node->fun() != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001572 LoadAndSpill(node->fun());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001573 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001574 __ mov(r0, Operand(0)); // no initial value!
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001575 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001576 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001577 frame_->CallRuntime(Runtime::kDeclareContextSlot, 4);
ager@chromium.org7c537e22008-10-16 08:43:32 +00001578 // Ignore the return value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001579 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580 return;
1581 }
1582
1583 ASSERT(!var->is_global());
1584
1585 // If we have a function or a constant, we need to initialize the variable.
1586 Expression* val = NULL;
1587 if (node->mode() == Variable::CONST) {
1588 val = new Literal(Factory::the_hole_value());
1589 } else {
1590 val = node->fun(); // NULL if we don't have a function
1591 }
1592
1593 if (val != NULL) {
iposva@chromium.org245aa852009-02-10 00:49:54 +00001594 {
1595 // Set initial value.
1596 Reference target(this, node->proxy());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001597 LoadAndSpill(val);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001598 target.SetValue(NOT_CONST_INIT);
iposva@chromium.org245aa852009-02-10 00:49:54 +00001599 }
1600 // Get rid of the assigned value (declarations are statements).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001601 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001602 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001603 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001604}
1605
1606
ager@chromium.org7c537e22008-10-16 08:43:32 +00001607void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001608#ifdef DEBUG
1609 int original_height = frame_->height();
1610#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001611 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001612 Comment cmnt(masm_, "[ ExpressionStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001613 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001614 Expression* expression = node->expression();
1615 expression->MarkAsStatement();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001616 LoadAndSpill(expression);
1617 frame_->Drop();
1618 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001619}
1620
1621
ager@chromium.org7c537e22008-10-16 08:43:32 +00001622void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001623#ifdef DEBUG
1624 int original_height = frame_->height();
1625#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001626 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001627 Comment cmnt(masm_, "// EmptyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001628 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001629 // nothing to do
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001630 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001631}
1632
1633
ager@chromium.org7c537e22008-10-16 08:43:32 +00001634void CodeGenerator::VisitIfStatement(IfStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001635#ifdef DEBUG
1636 int original_height = frame_->height();
1637#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001638 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001639 Comment cmnt(masm_, "[ IfStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001640 // Generate different code depending on which parts of the if statement
1641 // are present or not.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001642 bool has_then_stm = node->HasThenStatement();
1643 bool has_else_stm = node->HasElseStatement();
1644
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001645 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001646
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001647 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001648 if (has_then_stm && has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001649 Comment cmnt(masm_, "[ IfThenElse");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001650 JumpTarget then;
1651 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001652 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001653 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001654 if (frame_ != NULL) {
1655 Branch(false, &else_);
1656 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001657 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001658 if (frame_ != NULL || then.is_linked()) {
1659 then.Bind();
1660 VisitAndSpill(node->then_statement());
1661 }
1662 if (frame_ != NULL) {
1663 exit.Jump();
1664 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001665 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001666 if (else_.is_linked()) {
1667 else_.Bind();
1668 VisitAndSpill(node->else_statement());
1669 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001670
1671 } else if (has_then_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001672 Comment cmnt(masm_, "[ IfThen");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001673 ASSERT(!has_else_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001674 JumpTarget then;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001675 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001676 LoadConditionAndSpill(node->condition(), &then, &exit, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001677 if (frame_ != NULL) {
1678 Branch(false, &exit);
1679 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680 // then
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001681 if (frame_ != NULL || then.is_linked()) {
1682 then.Bind();
1683 VisitAndSpill(node->then_statement());
1684 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001685
1686 } else if (has_else_stm) {
mads.s.ager31e71382008-08-13 09:32:07 +00001687 Comment cmnt(masm_, "[ IfElse");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001688 ASSERT(!has_then_stm);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001689 JumpTarget else_;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001690 // if (!cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001691 LoadConditionAndSpill(node->condition(), &exit, &else_, true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001692 if (frame_ != NULL) {
1693 Branch(true, &exit);
1694 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001695 // else
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001696 if (frame_ != NULL || else_.is_linked()) {
1697 else_.Bind();
1698 VisitAndSpill(node->else_statement());
1699 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700
1701 } else {
mads.s.ager31e71382008-08-13 09:32:07 +00001702 Comment cmnt(masm_, "[ If");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001703 ASSERT(!has_then_stm && !has_else_stm);
1704 // if (cond)
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001705 LoadConditionAndSpill(node->condition(), &exit, &exit, false);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001706 if (frame_ != NULL) {
1707 if (has_cc()) {
1708 cc_reg_ = al;
1709 } else {
1710 frame_->Drop();
1711 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001712 }
1713 }
1714
1715 // end
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001716 if (exit.is_linked()) {
1717 exit.Bind();
1718 }
1719 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001720}
1721
1722
ager@chromium.org7c537e22008-10-16 08:43:32 +00001723void CodeGenerator::VisitContinueStatement(ContinueStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001724 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001725 Comment cmnt(masm_, "[ ContinueStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001726 CodeForStatementPosition(node);
1727 node->target()->continue_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001728}
1729
1730
ager@chromium.org7c537e22008-10-16 08:43:32 +00001731void CodeGenerator::VisitBreakStatement(BreakStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001732 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001733 Comment cmnt(masm_, "[ BreakStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001734 CodeForStatementPosition(node);
1735 node->target()->break_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001736}
1737
1738
ager@chromium.org7c537e22008-10-16 08:43:32 +00001739void CodeGenerator::VisitReturnStatement(ReturnStatement* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00001740 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001741 Comment cmnt(masm_, "[ ReturnStatement");
mads.s.ager31e71382008-08-13 09:32:07 +00001742
ager@chromium.org4af710e2009-09-15 12:20:11 +00001743 CodeForStatementPosition(node);
1744 LoadAndSpill(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001745 if (function_return_is_shadowed_) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001746 frame_->EmitPop(r0);
1747 function_return_.Jump();
1748 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001749 // Pop the result from the frame and prepare the frame for
1750 // returning thus making it easier to merge.
1751 frame_->EmitPop(r0);
1752 frame_->PrepareForReturn();
1753
1754 function_return_.Jump();
1755 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001756}
1757
1758
ager@chromium.org7c537e22008-10-16 08:43:32 +00001759void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001760#ifdef DEBUG
1761 int original_height = frame_->height();
1762#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001763 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001764 Comment cmnt(masm_, "[ WithEnterStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001765 CodeForStatementPosition(node);
1766 LoadAndSpill(node->expression());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001767 if (node->is_catch_block()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001768 frame_->CallRuntime(Runtime::kPushCatchContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001769 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001770 frame_->CallRuntime(Runtime::kPushContext, 1);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00001771 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001772#ifdef DEBUG
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001773 JumpTarget verified_true;
ager@chromium.org65dad4b2009-04-23 08:48:43 +00001774 __ cmp(r0, Operand(cp));
1775 verified_true.Branch(eq);
1776 __ stop("PushContext: r0 is expected to be the same as cp");
1777 verified_true.Bind();
1778#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001779 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001780 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001781 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001782}
1783
1784
ager@chromium.org7c537e22008-10-16 08:43:32 +00001785void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001786#ifdef DEBUG
1787 int original_height = frame_->height();
1788#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001789 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001790 Comment cmnt(masm_, "[ WithExitStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001791 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792 // Pop context.
1793 __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX));
1794 // Update context local.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001795 __ str(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001796 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001797}
1798
1799
ager@chromium.org7c537e22008-10-16 08:43:32 +00001800void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001801#ifdef DEBUG
1802 int original_height = frame_->height();
1803#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001804 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001805 Comment cmnt(masm_, "[ SwitchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001806 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001807 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001808
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001809 LoadAndSpill(node->tag());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00001810
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001811 JumpTarget next_test;
1812 JumpTarget fall_through;
1813 JumpTarget default_entry;
1814 JumpTarget default_exit(JumpTarget::BIDIRECTIONAL);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001815 ZoneList<CaseClause*>* cases = node->cases();
1816 int length = cases->length();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001817 CaseClause* default_clause = NULL;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818
1819 for (int i = 0; i < length; i++) {
1820 CaseClause* clause = cases->at(i);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001821 if (clause->is_default()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001822 // Remember the default clause and compile it at the end.
1823 default_clause = clause;
1824 continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001825 }
1826
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001827 Comment cmnt(masm_, "[ Case clause");
1828 // Compile the test.
1829 next_test.Bind();
1830 next_test.Unuse();
1831 // Duplicate TOS.
1832 __ ldr(r0, frame_->Top());
1833 frame_->EmitPush(r0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001834 Comparison(eq, NULL, clause->label(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001835 Branch(false, &next_test);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001836
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001837 // Before entering the body from the test, remove the switch value from
1838 // the stack.
1839 frame_->Drop();
1840
1841 // Label the body so that fall through is enabled.
1842 if (i > 0 && cases->at(i - 1)->is_default()) {
1843 default_exit.Bind();
1844 } else {
1845 fall_through.Bind();
1846 fall_through.Unuse();
1847 }
1848 VisitStatementsAndSpill(clause->statements());
1849
1850 // If control flow can fall through from the body, jump to the next body
1851 // or the end of the statement.
1852 if (frame_ != NULL) {
1853 if (i < length - 1 && cases->at(i + 1)->is_default()) {
1854 default_entry.Jump();
1855 } else {
1856 fall_through.Jump();
1857 }
1858 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001859 }
1860
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001861 // The final "test" removes the switch value.
1862 next_test.Bind();
1863 frame_->Drop();
1864
1865 // If there is a default clause, compile it.
1866 if (default_clause != NULL) {
1867 Comment cmnt(masm_, "[ Default clause");
1868 default_entry.Bind();
1869 VisitStatementsAndSpill(default_clause->statements());
1870 // If control flow can fall out of the default and there is a case after
1871 // it, jup to that case's body.
1872 if (frame_ != NULL && default_exit.is_bound()) {
1873 default_exit.Jump();
1874 }
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001875 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001876
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001877 if (fall_through.is_linked()) {
1878 fall_through.Bind();
1879 }
1880
1881 if (node->break_target()->is_linked()) {
1882 node->break_target()->Bind();
1883 }
1884 node->break_target()->Unuse();
1885 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001886}
1887
1888
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001889void CodeGenerator::VisitDoWhileStatement(DoWhileStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001890#ifdef DEBUG
1891 int original_height = frame_->height();
1892#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001893 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001894 Comment cmnt(masm_, "[ DoWhileStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001895 CodeForStatementPosition(node);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001896 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001897 JumpTarget body(JumpTarget::BIDIRECTIONAL);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001898 IncrementLoopNesting();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001899
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001900 // Label the top of the loop for the backward CFG edge. If the test
1901 // is always true we can use the continue target, and if the test is
1902 // always false there is no need.
1903 ConditionAnalysis info = AnalyzeCondition(node->cond());
1904 switch (info) {
1905 case ALWAYS_TRUE:
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001906 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001907 node->continue_target()->Bind();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001908 break;
1909 case ALWAYS_FALSE:
1910 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1911 break;
1912 case DONT_KNOW:
1913 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
1914 body.Bind();
1915 break;
1916 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001917
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001918 CheckStack(); // TODO(1222600): ignore if body contains calls.
1919 VisitAndSpill(node->body());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001920
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00001921 // Compile the test.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001922 switch (info) {
1923 case ALWAYS_TRUE:
1924 // If control can fall off the end of the body, jump back to the
1925 // top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001926 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001927 node->continue_target()->Jump();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001928 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001929 break;
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001930 case ALWAYS_FALSE:
1931 // If we have a continue in the body, we only have to bind its
1932 // jump target.
1933 if (node->continue_target()->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001934 node->continue_target()->Bind();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001935 }
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001936 break;
1937 case DONT_KNOW:
1938 // We have to compile the test expression if it can be reached by
1939 // control flow falling out of the body or via continue.
1940 if (node->continue_target()->is_linked()) {
1941 node->continue_target()->Bind();
1942 }
1943 if (has_valid_frame()) {
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001944 Comment cmnt(masm_, "[ DoWhileCondition");
1945 CodeForDoWhileConditionPosition(node);
1946 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001947 if (has_valid_frame()) {
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001948 // A invalid frame here indicates that control did not
1949 // fall out of the test expression.
1950 Branch(true, &body);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001951 }
1952 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001953 break;
1954 }
1955
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001956 if (node->break_target()->is_linked()) {
1957 node->break_target()->Bind();
1958 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001959 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001960 ASSERT(!has_valid_frame() || frame_->height() == original_height);
1961}
1962
1963
1964void CodeGenerator::VisitWhileStatement(WhileStatement* node) {
1965#ifdef DEBUG
1966 int original_height = frame_->height();
1967#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00001968 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001969 Comment cmnt(masm_, "[ WhileStatement");
1970 CodeForStatementPosition(node);
1971
1972 // If the test is never true and has no side effects there is no need
1973 // to compile the test or body.
1974 ConditionAnalysis info = AnalyzeCondition(node->cond());
1975 if (info == ALWAYS_FALSE) return;
1976
1977 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001978 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001979
1980 // Label the top of the loop with the continue target for the backward
1981 // CFG edge.
1982 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
1983 node->continue_target()->Bind();
1984
1985 if (info == DONT_KNOW) {
1986 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001987 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001988 if (has_valid_frame()) {
1989 // A NULL frame indicates that control did not fall out of the
1990 // test expression.
1991 Branch(false, node->break_target());
1992 }
1993 if (has_valid_frame() || body.is_linked()) {
1994 body.Bind();
1995 }
1996 }
1997
1998 if (has_valid_frame()) {
1999 CheckStack(); // TODO(1222600): ignore if body contains calls.
2000 VisitAndSpill(node->body());
2001
2002 // If control flow can fall out of the body, jump back to the top.
2003 if (has_valid_frame()) {
2004 node->continue_target()->Jump();
2005 }
2006 }
2007 if (node->break_target()->is_linked()) {
2008 node->break_target()->Bind();
2009 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002010 DecrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002011 ASSERT(!has_valid_frame() || frame_->height() == original_height);
2012}
2013
2014
2015void CodeGenerator::VisitForStatement(ForStatement* node) {
2016#ifdef DEBUG
2017 int original_height = frame_->height();
2018#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002019 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002020 Comment cmnt(masm_, "[ ForStatement");
2021 CodeForStatementPosition(node);
2022 if (node->init() != NULL) {
2023 VisitAndSpill(node->init());
2024 }
2025
2026 // If the test is never true there is no need to compile the test or
2027 // body.
2028 ConditionAnalysis info = AnalyzeCondition(node->cond());
2029 if (info == ALWAYS_FALSE) return;
2030
2031 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002032 IncrementLoopNesting();
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002033
2034 // If there is no update statement, label the top of the loop with the
2035 // continue target, otherwise with the loop target.
2036 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2037 if (node->next() == NULL) {
2038 node->continue_target()->set_direction(JumpTarget::BIDIRECTIONAL);
2039 node->continue_target()->Bind();
2040 } else {
2041 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
2042 loop.Bind();
2043 }
2044
2045 // If the test is always true, there is no need to compile it.
2046 if (info == DONT_KNOW) {
2047 JumpTarget body;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002048 LoadConditionAndSpill(node->cond(), &body, node->break_target(), true);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002049 if (has_valid_frame()) {
2050 Branch(false, node->break_target());
2051 }
2052 if (has_valid_frame() || body.is_linked()) {
2053 body.Bind();
2054 }
2055 }
2056
2057 if (has_valid_frame()) {
2058 CheckStack(); // TODO(1222600): ignore if body contains calls.
2059 VisitAndSpill(node->body());
2060
2061 if (node->next() == NULL) {
2062 // If there is no update statement and control flow can fall out
2063 // of the loop, jump directly to the continue label.
2064 if (has_valid_frame()) {
2065 node->continue_target()->Jump();
2066 }
2067 } else {
2068 // If there is an update statement and control flow can reach it
2069 // via falling out of the body of the loop or continuing, we
2070 // compile the update statement.
2071 if (node->continue_target()->is_linked()) {
2072 node->continue_target()->Bind();
2073 }
2074 if (has_valid_frame()) {
2075 // Record source position of the statement as this code which is
2076 // after the code for the body actually belongs to the loop
2077 // statement and not the body.
2078 CodeForStatementPosition(node);
2079 VisitAndSpill(node->next());
2080 loop.Jump();
2081 }
2082 }
2083 }
2084 if (node->break_target()->is_linked()) {
2085 node->break_target()->Bind();
2086 }
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002087 DecrementLoopNesting();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002088 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002089}
2090
2091
ager@chromium.org7c537e22008-10-16 08:43:32 +00002092void CodeGenerator::VisitForInStatement(ForInStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002093#ifdef DEBUG
2094 int original_height = frame_->height();
2095#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002096 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097 Comment cmnt(masm_, "[ ForInStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002098 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002099
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002100 JumpTarget primitive;
2101 JumpTarget jsobject;
2102 JumpTarget fixed_array;
2103 JumpTarget entry(JumpTarget::BIDIRECTIONAL);
2104 JumpTarget end_del_check;
2105 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106
2107 // Get the object to enumerate over (converted to JSObject).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002108 LoadAndSpill(node->enumerable());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109
2110 // Both SpiderMonkey and kjs ignore null and undefined in contrast
2111 // to the specification. 12.6.4 mandates a call to ToObject.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002112 frame_->EmitPop(r0);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002113 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2114 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002115 exit.Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002116 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2117 __ cmp(r0, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002118 exit.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002119
2120 // Stack layout in body:
2121 // [iteration counter (Smi)]
2122 // [length of array]
2123 // [FixedArray]
2124 // [Map or 0]
2125 // [Object]
2126
2127 // Check if enumerable is already a JSObject
2128 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002129 primitive.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002130 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002131 jsobject.Branch(hs);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002132
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002133 primitive.Bind();
2134 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002135 frame_->InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002136
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002137 jsobject.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002138 // Get the set of properties (as a FixedArray or Map).
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002139 // r0: value to be iterated over
2140 frame_->EmitPush(r0); // Push the object being iterated over.
2141
2142 // Check cache validity in generated code. This is a fast case for
2143 // the JSObject::IsSimpleEnum cache validity checks. If we cannot
2144 // guarantee cache validity, call the runtime system to check cache
2145 // validity or get the property names in a fixed array.
2146 JumpTarget call_runtime;
2147 JumpTarget loop(JumpTarget::BIDIRECTIONAL);
2148 JumpTarget check_prototype;
2149 JumpTarget use_cache;
2150 __ mov(r1, Operand(r0));
2151 loop.Bind();
2152 // Check that there are no elements.
2153 __ ldr(r2, FieldMemOperand(r1, JSObject::kElementsOffset));
2154 __ LoadRoot(r4, Heap::kEmptyFixedArrayRootIndex);
2155 __ cmp(r2, r4);
2156 call_runtime.Branch(ne);
2157 // Check that instance descriptors are not empty so that we can
2158 // check for an enum cache. Leave the map in r3 for the subsequent
2159 // prototype load.
2160 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
2161 __ ldr(r2, FieldMemOperand(r3, Map::kInstanceDescriptorsOffset));
2162 __ LoadRoot(ip, Heap::kEmptyDescriptorArrayRootIndex);
2163 __ cmp(r2, ip);
2164 call_runtime.Branch(eq);
2165 // Check that there in an enum cache in the non-empty instance
2166 // descriptors. This is the case if the next enumeration index
2167 // field does not contain a smi.
2168 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumerationIndexOffset));
2169 __ tst(r2, Operand(kSmiTagMask));
2170 call_runtime.Branch(eq);
2171 // For all objects but the receiver, check that the cache is empty.
2172 // r4: empty fixed array root.
2173 __ cmp(r1, r0);
2174 check_prototype.Branch(eq);
2175 __ ldr(r2, FieldMemOperand(r2, DescriptorArray::kEnumCacheBridgeCacheOffset));
2176 __ cmp(r2, r4);
2177 call_runtime.Branch(ne);
2178 check_prototype.Bind();
2179 // Load the prototype from the map and loop if non-null.
2180 __ ldr(r1, FieldMemOperand(r3, Map::kPrototypeOffset));
2181 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2182 __ cmp(r1, ip);
2183 loop.Branch(ne);
2184 // The enum cache is valid. Load the map of the object being
2185 // iterated over and use the cache for the iteration.
2186 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
2187 use_cache.Jump();
2188
2189 call_runtime.Bind();
2190 // Call the runtime to get the property names for the object.
2191 frame_->EmitPush(r0); // push the object (slot 4) for the runtime call
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002192 frame_->CallRuntime(Runtime::kGetPropertyNamesFast, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002193
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002194 // If we got a map from the runtime call, we can do a fast
2195 // modification check. Otherwise, we got a fixed array, and we have
2196 // to do a slow check.
2197 // r0: map or fixed array (result from call to
2198 // Runtime::kGetPropertyNamesFast)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002199 __ mov(r2, Operand(r0));
2200 __ ldr(r1, FieldMemOperand(r2, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002201 __ LoadRoot(ip, Heap::kMetaMapRootIndex);
2202 __ cmp(r1, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002203 fixed_array.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002204
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002205 use_cache.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002206 // Get enum cache
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00002207 // r0: map (either the result from a call to
2208 // Runtime::kGetPropertyNamesFast or has been fetched directly from
2209 // the object)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002210 __ mov(r1, Operand(r0));
2211 __ ldr(r1, FieldMemOperand(r1, Map::kInstanceDescriptorsOffset));
2212 __ ldr(r1, FieldMemOperand(r1, DescriptorArray::kEnumerationIndexOffset));
2213 __ ldr(r2,
2214 FieldMemOperand(r1, DescriptorArray::kEnumCacheBridgeCacheOffset));
2215
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002216 frame_->EmitPush(r0); // map
2217 frame_->EmitPush(r2); // enum cache bridge cache
mads.s.ager31e71382008-08-13 09:32:07 +00002218 __ ldr(r0, FieldMemOperand(r2, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002219 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002220 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002221 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002222 frame_->EmitPush(r0);
2223 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002224
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002225 fixed_array.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002226 __ mov(r1, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002227 frame_->EmitPush(r1); // insert 0 in place of Map
2228 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002229
2230 // Push the length of the array and the initial index onto the stack.
mads.s.ager31e71382008-08-13 09:32:07 +00002231 __ ldr(r0, FieldMemOperand(r0, FixedArray::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002232 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002233 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002234 __ mov(r0, Operand(Smi::FromInt(0))); // init index
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002235 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236
2237 // Condition.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002238 entry.Bind();
mads.s.ager31e71382008-08-13 09:32:07 +00002239 // sp[0] : index
2240 // sp[1] : array/enum cache length
2241 // sp[2] : array or enum cache
2242 // sp[3] : 0 or map
2243 // sp[4] : enumerable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002244 // Grab the current frame's height for the break and continue
2245 // targets only after all the state is pushed on the frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002246 node->break_target()->set_direction(JumpTarget::FORWARD_ONLY);
2247 node->continue_target()->set_direction(JumpTarget::FORWARD_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002248
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002249 __ ldr(r0, frame_->ElementAt(0)); // load the current count
2250 __ ldr(r1, frame_->ElementAt(1)); // load the length
2251 __ cmp(r0, Operand(r1)); // compare to the array length
2252 node->break_target()->Branch(hs);
2253
2254 __ ldr(r0, frame_->ElementAt(0));
mads.s.ager31e71382008-08-13 09:32:07 +00002255
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256 // Get the i'th entry of the array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002257 __ ldr(r2, frame_->ElementAt(2));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002258 __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2259 __ ldr(r3, MemOperand(r2, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
2260
2261 // Get Map or 0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002262 __ ldr(r2, frame_->ElementAt(3));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002263 // Check if this (still) matches the map of the enumerable.
2264 // If not, we have to filter the key.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002265 __ ldr(r1, frame_->ElementAt(4));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002266 __ ldr(r1, FieldMemOperand(r1, HeapObject::kMapOffset));
2267 __ cmp(r1, Operand(r2));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002268 end_del_check.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002269
2270 // Convert the entry to a string (or null if it isn't a property anymore).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002271 __ ldr(r0, frame_->ElementAt(4)); // push enumerable
2272 frame_->EmitPush(r0);
2273 frame_->EmitPush(r3); // push entry
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002274 frame_->InvokeBuiltin(Builtins::FILTER_KEY, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002275 __ mov(r3, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276
2277 // If the property has been removed while iterating, we just skip it.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002278 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2279 __ cmp(r3, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002280 node->continue_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002281
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002282 end_del_check.Bind();
2283 // Store the entry in the 'each' expression and take another spin in the
2284 // loop. r3: i'th entry of the enum cache (or string there of)
2285 frame_->EmitPush(r3); // push entry
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286 { Reference each(this, node->each());
2287 if (!each.is_illegal()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002288 if (each.size() > 0) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002289 __ ldr(r0, frame_->ElementAt(each.size()));
2290 frame_->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002291 each.SetValue(NOT_CONST_INIT);
2292 frame_->Drop(2);
2293 } else {
2294 // If the reference was to a slot we rely on the convenient property
2295 // that it doesn't matter whether a value (eg, r3 pushed above) is
2296 // right on top of or right underneath a zero-sized reference.
2297 each.SetValue(NOT_CONST_INIT);
2298 frame_->Drop();
mads.s.ager31e71382008-08-13 09:32:07 +00002299 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002300 }
2301 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002302 // Body.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303 CheckStack(); // TODO(1222600): ignore if body contains calls.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002304 VisitAndSpill(node->body());
2305
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002306 // Next. Reestablish a spilled frame in case we are coming here via
2307 // a continue in the body.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002308 node->continue_target()->Bind();
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002309 frame_->SpillAll();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002310 frame_->EmitPop(r0);
2311 __ add(r0, r0, Operand(Smi::FromInt(1)));
2312 frame_->EmitPush(r0);
2313 entry.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002314
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00002315 // Cleanup. No need to spill because VirtualFrame::Drop is safe for
2316 // any frame.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002317 node->break_target()->Bind();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002318 frame_->Drop(5);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002319
2320 // Exit.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002321 exit.Bind();
2322 node->continue_target()->Unuse();
2323 node->break_target()->Unuse();
2324 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002325}
2326
2327
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002328void CodeGenerator::VisitTryCatchStatement(TryCatchStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002329#ifdef DEBUG
2330 int original_height = frame_->height();
2331#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002332 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002333 Comment cmnt(masm_, "[ TryCatchStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002334 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002335
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002336 JumpTarget try_block;
2337 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002338
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002339 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002340 // --- Catch block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002341 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002342
2343 // Store the caught exception in the catch variable.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002344 Variable* catch_var = node->catch_var()->var();
2345 ASSERT(catch_var != NULL && catch_var->slot() != NULL);
2346 StoreToSlot(catch_var->slot(), NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002347
2348 // Remove the exception from the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002349 frame_->Drop();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002351 VisitStatementsAndSpill(node->catch_block()->statements());
2352 if (frame_ != NULL) {
2353 exit.Jump();
2354 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002355
2356
2357 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002358 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002359
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002360 frame_->PushTryHandler(TRY_CATCH_HANDLER);
2361 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002362
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002363 // Shadow the labels for all escapes from the try block, including
2364 // returns. During shadowing, the original label is hidden as the
2365 // LabelShadow and operations on the original actually affect the
2366 // shadowing label.
2367 //
2368 // We should probably try to unify the escaping labels and the return
2369 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002370 int nof_escapes = node->escaping_targets()->length();
2371 List<ShadowTarget*> shadows(1 + nof_escapes);
2372
2373 // Add the shadow target for the function return.
2374 static const int kReturnShadowIndex = 0;
2375 shadows.Add(new ShadowTarget(&function_return_));
2376 bool function_return_was_shadowed = function_return_is_shadowed_;
2377 function_return_is_shadowed_ = true;
2378 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2379
2380 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002381 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002382 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002383 }
2384
2385 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002386 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002387
2388 // Stop the introduced shadowing and count the number of required unlinks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002389 // After shadowing stops, the original labels are unshadowed and the
2390 // LabelShadows represent the formerly shadowing labels.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002391 bool has_unlinks = false;
2392 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002393 shadows[i]->StopShadowing();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002394 has_unlinks = has_unlinks || shadows[i]->is_linked();
2395 }
2396 function_return_is_shadowed_ = function_return_was_shadowed;
2397
2398 // Get an external reference to the handler address.
2399 ExternalReference handler_address(Top::k_handler_address);
2400
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002401 // If we can fall off the end of the try block, unlink from try chain.
2402 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002403 // The next handler address is on top of the frame. Unlink from
2404 // the handler list and drop the rest of this handler from the
2405 // frame.
2406 ASSERT(StackHandlerConstants::kNextOffset == 0);
2407 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002408 __ mov(r3, Operand(handler_address));
2409 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002410 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002411 if (has_unlinks) {
2412 exit.Jump();
2413 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002414 }
2415
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002416 // Generate unlink code for the (formerly) shadowing labels that have been
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002417 // jumped to. Deallocate each shadow target.
2418 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002419 if (shadows[i]->is_linked()) {
mads.s.ager31e71382008-08-13 09:32:07 +00002420 // Unlink from try chain;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002421 shadows[i]->Bind();
2422 // Because we can be jumping here (to spilled code) from unspilled
2423 // code, we need to reestablish a spilled frame at this block.
2424 frame_->SpillAll();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002425
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002426 // Reload sp from the top handler, because some statements that we
2427 // break from (eg, for...in) may have left stuff on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002428 __ mov(r3, Operand(handler_address));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002429 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002430 frame_->Forget(frame_->height() - handler_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002431
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002432 ASSERT(StackHandlerConstants::kNextOffset == 0);
2433 frame_->EmitPop(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002434 __ str(r1, MemOperand(r3));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002435 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002436
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002437 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
2438 frame_->PrepareForReturn();
2439 }
2440 shadows[i]->other_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002441 }
2442 }
2443
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002444 exit.Bind();
2445 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002446}
2447
2448
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002449void CodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002450#ifdef DEBUG
2451 int original_height = frame_->height();
2452#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002453 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00002454 Comment cmnt(masm_, "[ TryFinallyStatement");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002455 CodeForStatementPosition(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002456
2457 // State: Used to keep track of reason for entering the finally
2458 // block. Should probably be extended to hold information for
2459 // break/continue from within the try block.
2460 enum { FALLING, THROWING, JUMPING };
2461
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002462 JumpTarget try_block;
2463 JumpTarget finally_block;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002464
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002465 try_block.Call();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002466
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002467 frame_->EmitPush(r0); // save exception object on the stack
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468 // In case of thrown exceptions, this is where we continue.
2469 __ mov(r2, Operand(Smi::FromInt(THROWING)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002470 finally_block.Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002471
2472 // --- Try block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002473 try_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002474
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002475 frame_->PushTryHandler(TRY_FINALLY_HANDLER);
2476 int handler_height = frame_->height();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002477
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002478 // Shadow the labels for all escapes from the try block, including
2479 // returns. Shadowing hides the original label as the LabelShadow and
2480 // operations on the original actually affect the shadowing label.
2481 //
2482 // We should probably try to unify the escaping labels and the return
2483 // label.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002484 int nof_escapes = node->escaping_targets()->length();
2485 List<ShadowTarget*> shadows(1 + nof_escapes);
2486
2487 // Add the shadow target for the function return.
2488 static const int kReturnShadowIndex = 0;
2489 shadows.Add(new ShadowTarget(&function_return_));
2490 bool function_return_was_shadowed = function_return_is_shadowed_;
2491 function_return_is_shadowed_ = true;
2492 ASSERT(shadows[kReturnShadowIndex]->other_target() == &function_return_);
2493
2494 // Add the remaining shadow targets.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002495 for (int i = 0; i < nof_escapes; i++) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002496 shadows.Add(new ShadowTarget(node->escaping_targets()->at(i)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002497 }
2498
2499 // Generate code for the statements in the try block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002500 VisitStatementsAndSpill(node->try_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002501
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002502 // Stop the introduced shadowing and count the number of required unlinks.
2503 // After shadowing stops, the original labels are unshadowed and the
2504 // LabelShadows represent the formerly shadowing labels.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002505 int nof_unlinks = 0;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002506 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002507 shadows[i]->StopShadowing();
2508 if (shadows[i]->is_linked()) nof_unlinks++;
2509 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002510 function_return_is_shadowed_ = function_return_was_shadowed;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002511
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002512 // Get an external reference to the handler address.
2513 ExternalReference handler_address(Top::k_handler_address);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002514
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002515 // If we can fall off the end of the try block, unlink from the try
2516 // chain and set the state on the frame to FALLING.
2517 if (has_valid_frame()) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002518 // The next handler address is on top of the frame.
2519 ASSERT(StackHandlerConstants::kNextOffset == 0);
2520 frame_->EmitPop(r1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002521 __ mov(r3, Operand(handler_address));
2522 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002523 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002524
2525 // Fake a top of stack value (unneeded when FALLING) and set the
2526 // state in r2, then jump around the unlink blocks if any.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002527 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002528 frame_->EmitPush(r0);
2529 __ mov(r2, Operand(Smi::FromInt(FALLING)));
2530 if (nof_unlinks > 0) {
2531 finally_block.Jump();
2532 }
2533 }
2534
2535 // Generate code to unlink and set the state for the (formerly)
2536 // shadowing targets that have been jumped to.
2537 for (int i = 0; i < shadows.length(); i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002538 if (shadows[i]->is_linked()) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002539 // If we have come from the shadowed return, the return value is
2540 // in (a non-refcounted reference to) r0. We must preserve it
2541 // until it is pushed.
2542 //
2543 // Because we can be jumping here (to spilled code) from
2544 // unspilled code, we need to reestablish a spilled frame at
2545 // this block.
2546 shadows[i]->Bind();
2547 frame_->SpillAll();
2548
2549 // Reload sp from the top handler, because some statements that
2550 // we break from (eg, for...in) may have left stuff on the
2551 // stack.
2552 __ mov(r3, Operand(handler_address));
2553 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002554 frame_->Forget(frame_->height() - handler_height);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002555
2556 // Unlink this handler and drop it from the frame. The next
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002557 // handler address is currently on top of the frame.
2558 ASSERT(StackHandlerConstants::kNextOffset == 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002559 frame_->EmitPop(r1);
2560 __ str(r1, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00002561 frame_->Drop(StackHandlerConstants::kSize / kPointerSize - 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002562
2563 if (i == kReturnShadowIndex) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002564 // If this label shadowed the function return, materialize the
2565 // return value on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002566 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00002567 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002568 // Fake TOS for targets that shadowed breaks and continues.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002569 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002570 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002571 }
2572 __ mov(r2, Operand(Smi::FromInt(JUMPING + i)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002573 if (--nof_unlinks > 0) {
2574 // If this is not the last unlink block, jump around the next.
2575 finally_block.Jump();
2576 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002577 }
2578 }
2579
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002580 // --- Finally block ---
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002581 finally_block.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002582
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002583 // Push the state on the stack.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002584 frame_->EmitPush(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00002585
2586 // We keep two elements on the stack - the (possibly faked) result
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002587 // and the state - while evaluating the finally block.
2588 //
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002589 // Generate code for the statements in the finally block.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002590 VisitStatementsAndSpill(node->finally_block()->statements());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002591
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002592 if (has_valid_frame()) {
2593 // Restore state and return value or faked TOS.
2594 frame_->EmitPop(r2);
2595 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002596 }
2597
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002598 // Generate code to jump to the right destination for all used
2599 // formerly shadowing targets. Deallocate each shadow target.
2600 for (int i = 0; i < shadows.length(); i++) {
2601 if (has_valid_frame() && shadows[i]->is_bound()) {
2602 JumpTarget* original = shadows[i]->other_target();
2603 __ cmp(r2, Operand(Smi::FromInt(JUMPING + i)));
2604 if (!function_return_is_shadowed_ && i == kReturnShadowIndex) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002605 JumpTarget skip;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002606 skip.Branch(ne);
2607 frame_->PrepareForReturn();
2608 original->Jump();
2609 skip.Bind();
2610 } else {
2611 original->Branch(eq);
2612 }
2613 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002614 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002615
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002616 if (has_valid_frame()) {
2617 // Check if we need to rethrow the exception.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002618 JumpTarget exit;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002619 __ cmp(r2, Operand(Smi::FromInt(THROWING)));
2620 exit.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002621
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002622 // Rethrow exception.
2623 frame_->EmitPush(r0);
2624 frame_->CallRuntime(Runtime::kReThrow, 1);
2625
2626 // Done.
2627 exit.Bind();
2628 }
2629 ASSERT(!has_valid_frame() || frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002630}
2631
2632
ager@chromium.org7c537e22008-10-16 08:43:32 +00002633void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002634#ifdef DEBUG
2635 int original_height = frame_->height();
2636#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002637 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002638 Comment cmnt(masm_, "[ DebuggerStatament");
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002639 CodeForStatementPosition(node);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002640#ifdef ENABLE_DEBUGGER_SUPPORT
ager@chromium.org5c838252010-02-19 08:53:10 +00002641 frame_->DebugBreak();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00002642#endif
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002643 // Ignore the return value.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002644 ASSERT(frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002645}
2646
2647
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002648void CodeGenerator::InstantiateFunction(
2649 Handle<SharedFunctionInfo> function_info) {
ager@chromium.org357bf652010-04-12 11:30:10 +00002650 VirtualFrame::SpilledScope spilled_scope(frame_);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002651 __ mov(r0, Operand(function_info));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002652 // Use the fast case closure allocation code that allocates in new
2653 // space for nested functions that don't need literals cloning.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002654 if (scope()->is_function_scope() && function_info->num_literals() == 0) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002655 FastNewClosureStub stub;
2656 frame_->EmitPush(r0);
2657 frame_->CallStub(&stub, 1);
2658 frame_->EmitPush(r0);
2659 } else {
2660 // Create a new closure.
2661 frame_->EmitPush(cp);
2662 frame_->EmitPush(r0);
2663 frame_->CallRuntime(Runtime::kNewClosure, 2);
2664 frame_->EmitPush(r0);
2665 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002666}
2667
2668
ager@chromium.org7c537e22008-10-16 08:43:32 +00002669void CodeGenerator::VisitFunctionLiteral(FunctionLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002670#ifdef DEBUG
2671 int original_height = frame_->height();
2672#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002673 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002674 Comment cmnt(masm_, "[ FunctionLiteral");
2675
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002676 // Build the function info and instantiate it.
2677 Handle<SharedFunctionInfo> function_info =
2678 Compiler::BuildFunctionInfo(node, script(), this);
kasper.lund212ac232008-07-16 07:07:30 +00002679 // Check for stack-overflow exception.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002680 if (HasStackOverflow()) {
2681 ASSERT(frame_->height() == original_height);
2682 return;
2683 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002684 InstantiateFunction(function_info);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002685 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002686}
2687
2688
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002689void CodeGenerator::VisitSharedFunctionInfoLiteral(
2690 SharedFunctionInfoLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002691#ifdef DEBUG
2692 int original_height = frame_->height();
2693#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002694 VirtualFrame::SpilledScope spilled_scope(frame_);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00002695 Comment cmnt(masm_, "[ SharedFunctionInfoLiteral");
2696 InstantiateFunction(node->shared_function_info());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002697 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002698}
2699
2700
ager@chromium.org7c537e22008-10-16 08:43:32 +00002701void CodeGenerator::VisitConditional(Conditional* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002702#ifdef DEBUG
2703 int original_height = frame_->height();
2704#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00002705 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002706 Comment cmnt(masm_, "[ Conditional");
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002707 JumpTarget then;
2708 JumpTarget else_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002709 LoadConditionAndSpill(node->condition(), &then, &else_, true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002710 if (has_valid_frame()) {
2711 Branch(false, &else_);
2712 }
2713 if (has_valid_frame() || then.is_linked()) {
2714 then.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002715 LoadAndSpill(node->then_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002716 }
2717 if (else_.is_linked()) {
2718 JumpTarget exit;
2719 if (has_valid_frame()) exit.Jump();
2720 else_.Bind();
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002721 LoadAndSpill(node->else_expression());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002722 if (exit.is_linked()) exit.Bind();
2723 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002724 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002725}
2726
2727
ager@chromium.org7c537e22008-10-16 08:43:32 +00002728void CodeGenerator::LoadFromSlot(Slot* slot, TypeofState typeof_state) {
2729 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org357bf652010-04-12 11:30:10 +00002730 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002731 ASSERT(slot->var()->is_dynamic());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002732
kasperl@chromium.org71affb52009-05-26 05:44:31 +00002733 JumpTarget slow;
2734 JumpTarget done;
ager@chromium.org381abbb2009-02-25 13:23:22 +00002735
2736 // Generate fast-case code for variables that might be shadowed by
2737 // eval-introduced variables. Eval is used a lot without
2738 // introducing variables. In those cases, we do not want to
2739 // perform a runtime call for all variables in the scope
2740 // containing the eval.
2741 if (slot->var()->mode() == Variable::DYNAMIC_GLOBAL) {
2742 LoadFromGlobalSlotCheckExtensions(slot, typeof_state, r1, r2, &slow);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002743 // If there was no control flow to slow, we can exit early.
2744 if (!slow.is_linked()) {
2745 frame_->EmitPush(r0);
2746 return;
2747 }
2748
2749 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002750
2751 } else if (slot->var()->mode() == Variable::DYNAMIC_LOCAL) {
2752 Slot* potential_slot = slot->var()->local_if_not_shadowed()->slot();
2753 // Only generate the fast case for locals that rewrite to slots.
2754 // This rules out argument loads.
2755 if (potential_slot != NULL) {
2756 __ ldr(r0,
2757 ContextSlotOperandCheckExtensions(potential_slot,
2758 r1,
2759 r2,
2760 &slow));
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002761 if (potential_slot->var()->mode() == Variable::CONST) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002762 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
2763 __ cmp(r0, ip);
2764 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex, eq);
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002765 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002766 // There is always control flow to slow from
kasperl@chromium.org2d18d102009-04-15 13:27:32 +00002767 // ContextSlotOperandCheckExtensions so we have to jump around
2768 // it.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002769 done.Jump();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002770 }
2771 }
2772
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002773 slow.Bind();
2774 frame_->EmitPush(cp);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002775 __ mov(r0, Operand(slot->var()->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002776 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002777
ager@chromium.org7c537e22008-10-16 08:43:32 +00002778 if (typeof_state == INSIDE_TYPEOF) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002779 frame_->CallRuntime(Runtime::kLoadContextSlotNoReferenceError, 2);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002780 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002781 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002782 }
ager@chromium.org381abbb2009-02-25 13:23:22 +00002783
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002784 done.Bind();
2785 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002786
2787 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00002788 Register scratch = VirtualFrame::scratch0();
2789 frame_->EmitPush(SlotOperand(slot, scratch));
ager@chromium.org7c537e22008-10-16 08:43:32 +00002790 if (slot->var()->mode() == Variable::CONST) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002791 // Const slots may contain 'the hole' value (the constant hasn't been
2792 // initialized yet) which needs to be converted into the 'undefined'
2793 // value.
2794 Comment cmnt(masm_, "[ Unhole const");
ager@chromium.org357bf652010-04-12 11:30:10 +00002795 frame_->EmitPop(scratch);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002796 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00002797 __ cmp(scratch, ip);
2798 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex, eq);
2799 frame_->EmitPush(scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002800 }
2801 }
2802}
2803
2804
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002805void CodeGenerator::StoreToSlot(Slot* slot, InitState init_state) {
2806 ASSERT(slot != NULL);
2807 if (slot->type() == Slot::LOOKUP) {
ager@chromium.org357bf652010-04-12 11:30:10 +00002808 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002809 ASSERT(slot->var()->is_dynamic());
2810
2811 // For now, just do a runtime call.
2812 frame_->EmitPush(cp);
2813 __ mov(r0, Operand(slot->var()->name()));
2814 frame_->EmitPush(r0);
2815
2816 if (init_state == CONST_INIT) {
2817 // Same as the case for a normal store, but ignores attribute
2818 // (e.g. READ_ONLY) of context slot so that we can initialize
2819 // const properties (introduced via eval("const foo = (some
2820 // expr);")). Also, uses the current function context instead of
2821 // the top context.
2822 //
2823 // Note that we must declare the foo upon entry of eval(), via a
2824 // context slot declaration, but we cannot initialize it at the
2825 // same time, because the const declaration may be at the end of
2826 // the eval code (sigh...) and the const variable may have been
2827 // used before (where its value is 'undefined'). Thus, we can only
2828 // do the initialization when we actually encounter the expression
2829 // and when the expression operands are defined and valid, and
2830 // thus we need the split into 2 operations: declaration of the
2831 // context slot followed by initialization.
2832 frame_->CallRuntime(Runtime::kInitializeConstContextSlot, 3);
2833 } else {
2834 frame_->CallRuntime(Runtime::kStoreContextSlot, 3);
2835 }
2836 // Storing a variable must keep the (new) value on the expression
2837 // stack. This is necessary for compiling assignment expressions.
2838 frame_->EmitPush(r0);
2839
2840 } else {
2841 ASSERT(!slot->var()->is_dynamic());
ager@chromium.org357bf652010-04-12 11:30:10 +00002842 Register scratch = VirtualFrame::scratch0();
2843 VirtualFrame::RegisterAllocationScope scope(this);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002844
ager@chromium.org357bf652010-04-12 11:30:10 +00002845 // The frame must be spilled when branching to this target.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002846 JumpTarget exit;
ager@chromium.org357bf652010-04-12 11:30:10 +00002847
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002848 if (init_state == CONST_INIT) {
2849 ASSERT(slot->var()->mode() == Variable::CONST);
2850 // Only the first const initialization must be executed (the slot
2851 // still contains 'the hole' value). When the assignment is
2852 // executed, the code is identical to a normal store (see below).
2853 Comment cmnt(masm_, "[ Init const");
ager@chromium.org357bf652010-04-12 11:30:10 +00002854 __ ldr(scratch, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002855 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00002856 __ cmp(scratch, ip);
2857 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002858 exit.Branch(ne);
2859 }
2860
2861 // We must execute the store. Storing a variable must keep the
2862 // (new) value on the stack. This is necessary for compiling
2863 // assignment expressions.
2864 //
2865 // Note: We will reach here even with slot->var()->mode() ==
2866 // Variable::CONST because of const declarations which will
2867 // initialize consts to 'the hole' value and by doing so, end up
2868 // calling this code. r2 may be loaded with context; used below in
2869 // RecordWrite.
ager@chromium.org357bf652010-04-12 11:30:10 +00002870 Register tos = frame_->Peek();
2871 __ str(tos, SlotOperand(slot, scratch));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002872 if (slot->type() == Slot::CONTEXT) {
2873 // Skip write barrier if the written value is a smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00002874 __ tst(tos, Operand(kSmiTagMask));
2875 // We don't use tos any more after here.
2876 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002877 exit.Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00002878 // scratch is loaded with context when calling SlotOperand above.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002879 int offset = FixedArray::kHeaderSize + slot->index() * kPointerSize;
2880 __ mov(r3, Operand(offset));
ager@chromium.org357bf652010-04-12 11:30:10 +00002881 // r1 could be identical with tos, but that doesn't matter.
2882 __ RecordWrite(scratch, r3, r1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002883 }
2884 // If we definitely did not jump over the assignment, we do not need
2885 // to bind the exit label. Doing so can defeat peephole
2886 // optimization.
2887 if (init_state == CONST_INIT || slot->type() == Slot::CONTEXT) {
ager@chromium.org357bf652010-04-12 11:30:10 +00002888 frame_->SpillAll();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002889 exit.Bind();
2890 }
2891 }
2892}
2893
2894
ager@chromium.org381abbb2009-02-25 13:23:22 +00002895void CodeGenerator::LoadFromGlobalSlotCheckExtensions(Slot* slot,
2896 TypeofState typeof_state,
2897 Register tmp,
2898 Register tmp2,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002899 JumpTarget* slow) {
ager@chromium.org381abbb2009-02-25 13:23:22 +00002900 // Check that no extension objects have been created by calls to
2901 // eval from the current scope to the global scope.
2902 Register context = cp;
2903 Scope* s = scope();
2904 while (s != NULL) {
2905 if (s->num_heap_slots() > 0) {
2906 if (s->calls_eval()) {
2907 // Check that extension is NULL.
2908 __ ldr(tmp2, ContextOperand(context, Context::EXTENSION_INDEX));
2909 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002910 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002911 }
2912 // Load next context in chain.
2913 __ ldr(tmp, ContextOperand(context, Context::CLOSURE_INDEX));
2914 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2915 context = tmp;
2916 }
2917 // If no outer scope calls eval, we do not need to check more
2918 // context extensions.
2919 if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break;
2920 s = s->outer_scope();
2921 }
2922
2923 if (s->is_eval_scope()) {
2924 Label next, fast;
ager@chromium.org357bf652010-04-12 11:30:10 +00002925 __ Move(tmp, context);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002926 __ bind(&next);
2927 // Terminate at global context.
2928 __ ldr(tmp2, FieldMemOperand(tmp, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00002929 __ LoadRoot(ip, Heap::kGlobalContextMapRootIndex);
2930 __ cmp(tmp2, ip);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002931 __ b(eq, &fast);
2932 // Check that extension is NULL.
2933 __ ldr(tmp2, ContextOperand(tmp, Context::EXTENSION_INDEX));
2934 __ tst(tmp2, tmp2);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002935 slow->Branch(ne);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002936 // Load next context in chain.
2937 __ ldr(tmp, ContextOperand(tmp, Context::CLOSURE_INDEX));
2938 __ ldr(tmp, FieldMemOperand(tmp, JSFunction::kContextOffset));
2939 __ b(&next);
2940 __ bind(&fast);
2941 }
2942
2943 // All extension objects were empty and it is safe to use a global
2944 // load IC call.
2945 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
2946 // Load the global object.
2947 LoadGlobal();
2948 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00002949 __ mov(r2, Operand(slot->var()->name()));
ager@chromium.org381abbb2009-02-25 13:23:22 +00002950 // Call IC stub.
2951 if (typeof_state == INSIDE_TYPEOF) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002952 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002953 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002954 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET_CONTEXT, 0);
ager@chromium.org381abbb2009-02-25 13:23:22 +00002955 }
2956
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002957 // Drop the global object. The result is in r0.
2958 frame_->Drop();
ager@chromium.org381abbb2009-02-25 13:23:22 +00002959}
2960
2961
ager@chromium.org7c537e22008-10-16 08:43:32 +00002962void CodeGenerator::VisitSlot(Slot* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002963#ifdef DEBUG
2964 int original_height = frame_->height();
2965#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00002966 Comment cmnt(masm_, "[ Slot");
ager@chromium.orgc4c92722009-11-18 14:12:51 +00002967 LoadFromSlot(node, NOT_INSIDE_TYPEOF);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002968 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00002969}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002970
ager@chromium.org7c537e22008-10-16 08:43:32 +00002971
2972void CodeGenerator::VisitVariableProxy(VariableProxy* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002973#ifdef DEBUG
2974 int original_height = frame_->height();
2975#endif
ager@chromium.org7c537e22008-10-16 08:43:32 +00002976 Comment cmnt(masm_, "[ VariableProxy");
2977
2978 Variable* var = node->var();
2979 Expression* expr = var->rewrite();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00002980 if (expr != NULL) {
2981 Visit(expr);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002982 } else {
ager@chromium.org7c537e22008-10-16 08:43:32 +00002983 ASSERT(var->is_global());
2984 Reference ref(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00002985 ref.GetValue();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002986 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002987 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002988}
2989
2990
ager@chromium.org7c537e22008-10-16 08:43:32 +00002991void CodeGenerator::VisitLiteral(Literal* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002992#ifdef DEBUG
2993 int original_height = frame_->height();
2994#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002995 Comment cmnt(masm_, "[ Literal");
ager@chromium.org357bf652010-04-12 11:30:10 +00002996 Register reg = frame_->GetTOSRegister();
2997 __ mov(reg, Operand(node->handle()));
2998 frame_->EmitPush(reg);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002999 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003000}
3001
3002
ager@chromium.org7c537e22008-10-16 08:43:32 +00003003void CodeGenerator::VisitRegExpLiteral(RegExpLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003004#ifdef DEBUG
3005 int original_height = frame_->height();
3006#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003007 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003008 Comment cmnt(masm_, "[ RexExp Literal");
3009
3010 // Retrieve the literal array and check the allocated entry.
3011
3012 // Load the function of this activation.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003013 __ ldr(r1, frame_->Function());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003014
3015 // Load the literals array of the function.
3016 __ ldr(r1, FieldMemOperand(r1, JSFunction::kLiteralsOffset));
3017
3018 // Load the literal at the ast saved index.
3019 int literal_offset =
3020 FixedArray::kHeaderSize + node->literal_index() * kPointerSize;
3021 __ ldr(r2, FieldMemOperand(r1, literal_offset));
3022
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003023 JumpTarget done;
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003024 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3025 __ cmp(r2, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003026 done.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003027
3028 // If the entry is undefined we call the runtime system to computed
3029 // the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003030 frame_->EmitPush(r1); // literal array (0)
mads.s.ager31e71382008-08-13 09:32:07 +00003031 __ mov(r0, Operand(Smi::FromInt(node->literal_index())));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003032 frame_->EmitPush(r0); // literal index (1)
mads.s.ager31e71382008-08-13 09:32:07 +00003033 __ mov(r0, Operand(node->pattern())); // RegExp pattern (2)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003034 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003035 __ mov(r0, Operand(node->flags())); // RegExp flags (3)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003036 frame_->EmitPush(r0);
3037 frame_->CallRuntime(Runtime::kMaterializeRegExpLiteral, 4);
mads.s.ager31e71382008-08-13 09:32:07 +00003038 __ mov(r2, Operand(r0));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003039
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003040 done.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003041 // Push the literal.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003042 frame_->EmitPush(r2);
3043 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003044}
3045
3046
ager@chromium.org7c537e22008-10-16 08:43:32 +00003047void CodeGenerator::VisitObjectLiteral(ObjectLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003048#ifdef DEBUG
3049 int original_height = frame_->height();
3050#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003051 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003052 Comment cmnt(masm_, "[ ObjectLiteral");
3053
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003054 // Load the function of this activation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003055 __ ldr(r3, frame_->Function());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003056 // Literal array.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003057 __ ldr(r3, FieldMemOperand(r3, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003058 // Literal index.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003059 __ mov(r2, Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003060 // Constant properties.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003061 __ mov(r1, Operand(node->constant_properties()));
3062 // Should the object literal have fast elements?
3063 __ mov(r0, Operand(Smi::FromInt(node->fast_elements() ? 1 : 0)));
3064 frame_->EmitPushMultiple(4, r3.bit() | r2.bit() | r1.bit() | r0.bit());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003065 if (node->depth() > 1) {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003066 frame_->CallRuntime(Runtime::kCreateObjectLiteral, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003067 } else {
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003068 frame_->CallRuntime(Runtime::kCreateObjectLiteralShallow, 4);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003069 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003070 frame_->EmitPush(r0); // save the result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003071 for (int i = 0; i < node->properties()->length(); i++) {
ager@chromium.org5c838252010-02-19 08:53:10 +00003072 // At the start of each iteration, the top of stack contains
3073 // the newly created object literal.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003074 ObjectLiteral::Property* property = node->properties()->at(i);
3075 Literal* key = property->key();
3076 Expression* value = property->value();
3077 switch (property->kind()) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003078 case ObjectLiteral::Property::CONSTANT:
3079 break;
3080 case ObjectLiteral::Property::MATERIALIZED_LITERAL:
3081 if (CompileTimeValue::IsCompileTimeValue(property->value())) break;
3082 // else fall through
ager@chromium.org5c838252010-02-19 08:53:10 +00003083 case ObjectLiteral::Property::COMPUTED:
3084 if (key->handle()->IsSymbol()) {
3085 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
3086 LoadAndSpill(value);
3087 frame_->EmitPop(r0);
3088 __ mov(r2, Operand(key->handle()));
3089 __ ldr(r1, frame_->Top()); // Load the receiver.
3090 frame_->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
3091 break;
3092 }
3093 // else fall through
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003094 case ObjectLiteral::Property::PROTOTYPE: {
ager@chromium.org5c838252010-02-19 08:53:10 +00003095 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003096 frame_->EmitPush(r0); // dup the result
3097 LoadAndSpill(key);
3098 LoadAndSpill(value);
3099 frame_->CallRuntime(Runtime::kSetProperty, 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003100 break;
3101 }
3102 case ObjectLiteral::Property::SETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00003103 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003104 frame_->EmitPush(r0);
3105 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00003106 __ mov(r0, Operand(Smi::FromInt(1)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003107 frame_->EmitPush(r0);
3108 LoadAndSpill(value);
3109 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003110 break;
3111 }
3112 case ObjectLiteral::Property::GETTER: {
ager@chromium.org5c838252010-02-19 08:53:10 +00003113 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003114 frame_->EmitPush(r0);
3115 LoadAndSpill(key);
mads.s.ager31e71382008-08-13 09:32:07 +00003116 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003117 frame_->EmitPush(r0);
3118 LoadAndSpill(value);
3119 frame_->CallRuntime(Runtime::kDefineAccessor, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003120 break;
3121 }
3122 }
3123 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003124 ASSERT(frame_->height() == original_height + 1);
3125}
3126
3127
ager@chromium.org7c537e22008-10-16 08:43:32 +00003128void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003129#ifdef DEBUG
3130 int original_height = frame_->height();
3131#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003132 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003133 Comment cmnt(masm_, "[ ArrayLiteral");
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003134
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003135 // Load the function of this activation.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003136 __ ldr(r2, frame_->Function());
ager@chromium.org5c838252010-02-19 08:53:10 +00003137 // Load the literals array of the function.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003138 __ ldr(r2, FieldMemOperand(r2, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003139 __ mov(r1, Operand(Smi::FromInt(node->literal_index())));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003140 __ mov(r0, Operand(node->constant_elements()));
3141 frame_->EmitPushMultiple(3, r2.bit() | r1.bit() | r0.bit());
ager@chromium.org5c838252010-02-19 08:53:10 +00003142 int length = node->values()->length();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003143 if (node->depth() > 1) {
3144 frame_->CallRuntime(Runtime::kCreateArrayLiteral, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003145 } else if (length > FastCloneShallowArrayStub::kMaximumLength) {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003146 frame_->CallRuntime(Runtime::kCreateArrayLiteralShallow, 3);
ager@chromium.org5c838252010-02-19 08:53:10 +00003147 } else {
3148 FastCloneShallowArrayStub stub(length);
3149 frame_->CallStub(&stub, 3);
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003150 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003151 frame_->EmitPush(r0); // save the result
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003152 // r0: created object literal
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00003153
3154 // Generate code to set the elements in the array that are not
3155 // literals.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003156 for (int i = 0; i < node->values()->length(); i++) {
3157 Expression* value = node->values()->at(i);
3158
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003159 // If value is a literal the property value is already set in the
3160 // boilerplate object.
3161 if (value->AsLiteral() != NULL) continue;
3162 // If value is a materialized literal the property value is already set
3163 // in the boilerplate object if it is simple.
3164 if (CompileTimeValue::IsCompileTimeValue(value)) continue;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003165
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003166 // The property must be set by generated code.
3167 LoadAndSpill(value);
3168 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003169
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003170 // Fetch the object literal.
3171 __ ldr(r1, frame_->Top());
3172 // Get the elements array.
3173 __ ldr(r1, FieldMemOperand(r1, JSObject::kElementsOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003174
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003175 // Write to the indexed properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00003176 int offset = i * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orgbb29dc92009-03-24 13:25:23 +00003177 __ str(r0, FieldMemOperand(r1, offset));
3178
3179 // Update the write barrier for the array address.
3180 __ mov(r3, Operand(offset));
3181 __ RecordWrite(r1, r3, r2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003182 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003183 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003184}
3185
3186
ager@chromium.org32912102009-01-16 10:38:43 +00003187void CodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003188#ifdef DEBUG
3189 int original_height = frame_->height();
3190#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003191 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org32912102009-01-16 10:38:43 +00003192 // Call runtime routine to allocate the catch extension object and
3193 // assign the exception value to the catch variable.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003194 Comment cmnt(masm_, "[ CatchExtensionObject");
3195 LoadAndSpill(node->key());
3196 LoadAndSpill(node->value());
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003197 frame_->CallRuntime(Runtime::kCreateCatchExtensionObject, 2);
3198 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003199 ASSERT(frame_->height() == original_height + 1);
ager@chromium.org32912102009-01-16 10:38:43 +00003200}
3201
3202
ager@chromium.org7c537e22008-10-16 08:43:32 +00003203void CodeGenerator::VisitAssignment(Assignment* node) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003204 VirtualFrame::RegisterAllocationScope scope(this);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003205#ifdef DEBUG
3206 int original_height = frame_->height();
3207#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003208 Comment cmnt(masm_, "[ Assignment");
mads.s.ager31e71382008-08-13 09:32:07 +00003209
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003210 { Reference target(this, node->target(), node->is_compound());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003211 if (target.is_illegal()) {
3212 // Fool the virtual frame into thinking that we left the assignment's
3213 // value on the frame.
ager@chromium.org357bf652010-04-12 11:30:10 +00003214 Register tos = frame_->GetTOSRegister();
3215 __ mov(tos, Operand(Smi::FromInt(0)));
3216 frame_->EmitPush(tos);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003217 ASSERT(frame_->height() == original_height + 1);
3218 return;
3219 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003220
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003221 if (node->op() == Token::ASSIGN ||
3222 node->op() == Token::INIT_VAR ||
3223 node->op() == Token::INIT_CONST) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003224 Load(node->value());
mads.s.ager31e71382008-08-13 09:32:07 +00003225
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003226 } else { // Assignment is a compound assignment.
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003227 // Get the old value of the lhs.
ager@chromium.org357bf652010-04-12 11:30:10 +00003228 target.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003229 Literal* literal = node->value()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00003230 bool overwrite =
3231 (node->value()->AsBinaryOperation() != NULL &&
3232 node->value()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003233 if (literal != NULL && literal->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003234 VirtualFrameSmiOperation(node->binary_op(),
3235 literal->handle(),
3236 false,
3237 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003238 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00003239 Load(node->value());
3240 VirtualFrameBinaryOperation(node->binary_op(),
3241 overwrite ? OVERWRITE_RIGHT : NO_OVERWRITE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003242 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003243 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003244 Variable* var = node->target()->AsVariableProxy()->AsVariable();
3245 if (var != NULL &&
3246 (var->mode() == Variable::CONST) &&
3247 node->op() != Token::INIT_VAR && node->op() != Token::INIT_CONST) {
3248 // Assignment ignored - leave the value on the stack.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003249 UnloadReference(&target);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003250 } else {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003251 CodeForSourcePosition(node->position());
3252 if (node->op() == Token::INIT_CONST) {
3253 // Dynamic constant initializations must use the function context
3254 // and initialize the actual constant declared. Dynamic variable
3255 // initializations are simply assignments and use SetValue.
3256 target.SetValue(CONST_INIT);
3257 } else {
3258 target.SetValue(NOT_CONST_INIT);
3259 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003260 }
3261 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003262 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003263}
3264
3265
ager@chromium.org7c537e22008-10-16 08:43:32 +00003266void CodeGenerator::VisitThrow(Throw* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003267#ifdef DEBUG
3268 int original_height = frame_->height();
3269#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003270 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003271 Comment cmnt(masm_, "[ Throw");
3272
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003273 LoadAndSpill(node->exception());
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003274 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003275 frame_->CallRuntime(Runtime::kThrow, 1);
3276 frame_->EmitPush(r0);
3277 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003278}
3279
3280
ager@chromium.org7c537e22008-10-16 08:43:32 +00003281void CodeGenerator::VisitProperty(Property* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003282#ifdef DEBUG
3283 int original_height = frame_->height();
3284#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003285 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003286 Comment cmnt(masm_, "[ Property");
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003287
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003288 { Reference property(this, node);
ager@chromium.org357bf652010-04-12 11:30:10 +00003289 property.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003290 }
3291 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003292}
3293
3294
ager@chromium.org7c537e22008-10-16 08:43:32 +00003295void CodeGenerator::VisitCall(Call* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003296#ifdef DEBUG
3297 int original_height = frame_->height();
3298#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003299 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003300 Comment cmnt(masm_, "[ Call");
3301
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003302 Expression* function = node->expression();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003303 ZoneList<Expression*>* args = node->arguments();
3304
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003305 // Standard function call.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003306 // Check if the function is a variable or a property.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003307 Variable* var = function->AsVariableProxy()->AsVariable();
3308 Property* property = function->AsProperty();
3309
3310 // ------------------------------------------------------------------------
3311 // Fast-case: Use inline caching.
3312 // ---
3313 // According to ECMA-262, section 11.2.3, page 44, the function to call
3314 // must be resolved after the arguments have been evaluated. The IC code
3315 // automatically handles this by loading the arguments before the function
3316 // is resolved in cache misses (this also holds for megamorphic calls).
3317 // ------------------------------------------------------------------------
3318
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003319 if (var != NULL && var->is_possibly_eval()) {
3320 // ----------------------------------
3321 // JavaScript example: 'eval(arg)' // eval is not known to be shadowed
3322 // ----------------------------------
3323
3324 // In a call to eval, we first call %ResolvePossiblyDirectEval to
3325 // resolve the function we need to call and the receiver of the
3326 // call. Then we call the resolved function using the given
3327 // arguments.
3328 // Prepare stack for call to resolved function.
3329 LoadAndSpill(function);
3330 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
3331 frame_->EmitPush(r2); // Slot for receiver
3332 int arg_count = args->length();
3333 for (int i = 0; i < arg_count; i++) {
3334 LoadAndSpill(args->at(i));
3335 }
3336
3337 // Prepare stack for call to ResolvePossiblyDirectEval.
3338 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize + kPointerSize));
3339 frame_->EmitPush(r1);
3340 if (arg_count > 0) {
3341 __ ldr(r1, MemOperand(sp, arg_count * kPointerSize));
3342 frame_->EmitPush(r1);
3343 } else {
3344 frame_->EmitPush(r2);
3345 }
3346
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003347 // Push the receiver.
3348 __ ldr(r1, frame_->Receiver());
3349 frame_->EmitPush(r1);
3350
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003351 // Resolve the call.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003352 frame_->CallRuntime(Runtime::kResolvePossiblyDirectEval, 3);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003353
3354 // Touch up stack with the right values for the function and the receiver.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00003355 __ str(r0, MemOperand(sp, (arg_count + 1) * kPointerSize));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003356 __ str(r1, MemOperand(sp, arg_count * kPointerSize));
3357
3358 // Call the function.
3359 CodeForSourcePosition(node->position());
3360
3361 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003362 CallFunctionStub call_function(arg_count, in_loop, RECEIVER_MIGHT_BE_VALUE);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00003363 frame_->CallStub(&call_function, arg_count + 1);
3364
3365 __ ldr(cp, frame_->Context());
3366 // Remove the function from the stack.
3367 frame_->Drop();
3368 frame_->EmitPush(r0);
3369
3370 } else if (var != NULL && !var->is_this() && var->is_global()) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003371 // ----------------------------------
3372 // JavaScript example: 'foo(1, 2, 3)' // foo is global
3373 // ----------------------------------
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003374 // Pass the global object as the receiver and let the IC stub
3375 // patch the stack to use the global proxy as 'this' in the
3376 // invoked function.
3377 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003378
3379 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003380 int arg_count = args->length();
3381 for (int i = 0; i < arg_count; i++) {
3382 LoadAndSpill(args->at(i));
3383 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003384
ager@chromium.org5c838252010-02-19 08:53:10 +00003385 // Setup the name register and call the IC initialization code.
3386 __ mov(r2, Operand(var->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003387 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3388 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003389 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003390 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET_CONTEXT,
3391 arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003392 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003393 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003394
3395 } else if (var != NULL && var->slot() != NULL &&
3396 var->slot()->type() == Slot::LOOKUP) {
3397 // ----------------------------------
3398 // JavaScript example: 'with (obj) foo(1, 2, 3)' // foo is in obj
3399 // ----------------------------------
3400
3401 // Load the function
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003402 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00003403 __ mov(r0, Operand(var->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003404 frame_->EmitPush(r0);
3405 frame_->CallRuntime(Runtime::kLoadContextSlot, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003406 // r0: slot value; r1: receiver
3407
3408 // Load the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003409 frame_->EmitPush(r0); // function
3410 frame_->EmitPush(r1); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003411
3412 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003413 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003414 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003415
3416 } else if (property != NULL) {
3417 // Check if the key is a literal string.
3418 Literal* literal = property->key()->AsLiteral();
3419
3420 if (literal != NULL && literal->handle()->IsSymbol()) {
3421 // ------------------------------------------------------------------
3422 // JavaScript example: 'object.foo(1, 2, 3)' or 'map["key"](1, 2, 3)'
3423 // ------------------------------------------------------------------
3424
ager@chromium.org5c838252010-02-19 08:53:10 +00003425 LoadAndSpill(property->obj()); // Receiver.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003426 // Load the arguments.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003427 int arg_count = args->length();
3428 for (int i = 0; i < arg_count; i++) {
3429 LoadAndSpill(args->at(i));
3430 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003431
ager@chromium.org5c838252010-02-19 08:53:10 +00003432 // Set the name register and call the IC initialization code.
3433 __ mov(r2, Operand(literal->handle()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003434 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
3435 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003436 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003437 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003438 __ ldr(cp, frame_->Context());
ager@chromium.org5c838252010-02-19 08:53:10 +00003439 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003440
3441 } else {
3442 // -------------------------------------------
3443 // JavaScript example: 'array[index](1, 2, 3)'
3444 // -------------------------------------------
3445
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003446 LoadAndSpill(property->obj());
3447 LoadAndSpill(property->key());
3448 EmitKeyedLoad(false);
3449 frame_->Drop(); // key
3450 // Put the function below the receiver.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003451 if (property->is_synthetic()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003452 // Use the global receiver.
3453 frame_->Drop();
3454 frame_->EmitPush(r0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003455 LoadGlobalReceiver(r0);
3456 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003457 frame_->EmitPop(r1); // receiver
3458 frame_->EmitPush(r0); // function
3459 frame_->EmitPush(r1); // receiver
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003460 }
3461
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003462 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003463 CallWithArguments(args, RECEIVER_MIGHT_BE_VALUE, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003464 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003465 }
3466
3467 } else {
3468 // ----------------------------------
3469 // JavaScript example: 'foo(1, 2, 3)' // foo is not global
3470 // ----------------------------------
3471
3472 // Load the function.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003473 LoadAndSpill(function);
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003474
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003475 // Pass the global proxy as the receiver.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00003476 LoadGlobalReceiver(r0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003477
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003478 // Call the function.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003479 CallWithArguments(args, NO_CALL_FUNCTION_FLAGS, node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003480 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003481 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003482 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003483}
3484
3485
ager@chromium.org7c537e22008-10-16 08:43:32 +00003486void CodeGenerator::VisitCallNew(CallNew* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003487#ifdef DEBUG
3488 int original_height = frame_->height();
3489#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00003490 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003491 Comment cmnt(masm_, "[ CallNew");
3492
3493 // According to ECMA-262, section 11.2.2, page 44, the function
3494 // expression in new calls must be evaluated before the
3495 // arguments. This is different from ordinary calls, where the
3496 // actual function to call is resolved after the arguments have been
3497 // evaluated.
3498
3499 // Compute function to call and use the global object as the
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003500 // receiver. There is no need to use the global proxy here because
3501 // it will always be replaced with a newly allocated object.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003502 LoadAndSpill(node->expression());
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00003503 LoadGlobal();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003504
3505 // Push the arguments ("left-to-right") on the stack.
3506 ZoneList<Expression*>* args = node->arguments();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003507 int arg_count = args->length();
3508 for (int i = 0; i < arg_count; i++) {
3509 LoadAndSpill(args->at(i));
3510 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003511
mads.s.ager31e71382008-08-13 09:32:07 +00003512 // r0: the number of arguments.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003513 __ mov(r0, Operand(arg_count));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003514 // Load the function into r1 as per calling convention.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00003515 __ ldr(r1, frame_->ElementAt(arg_count + 1));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003516
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003517 // Call the construct call builtin that handles allocation and
3518 // constructor invocation.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003519 CodeForSourcePosition(node->position());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003520 Handle<Code> ic(Builtins::builtin(Builtins::JSConstructCall));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003521 frame_->CallCodeObject(ic, RelocInfo::CONSTRUCT_CALL, arg_count + 1);
mads.s.ager31e71382008-08-13 09:32:07 +00003522
3523 // Discard old TOS value and push r0 on the stack (same as Pop(), push(r0)).
ager@chromium.org3bf7b912008-11-17 09:09:45 +00003524 __ str(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003525 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003526}
3527
3528
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003529void CodeGenerator::GenerateClassOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003530 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003531 ASSERT(args->length() == 1);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003532 JumpTarget leave, null, function, non_function_constructor;
3533
3534 // Load the object into r0.
3535 LoadAndSpill(args->at(0));
3536 frame_->EmitPop(r0);
3537
3538 // If the object is a smi, we return null.
3539 __ tst(r0, Operand(kSmiTagMask));
3540 null.Branch(eq);
3541
3542 // Check that the object is a JS object but take special care of JS
3543 // functions to make sure they have 'Function' as their class.
3544 __ CompareObjectType(r0, r0, r1, FIRST_JS_OBJECT_TYPE);
3545 null.Branch(lt);
3546
3547 // As long as JS_FUNCTION_TYPE is the last instance type and it is
3548 // right after LAST_JS_OBJECT_TYPE, we can avoid checking for
3549 // LAST_JS_OBJECT_TYPE.
3550 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3551 ASSERT(JS_FUNCTION_TYPE == LAST_JS_OBJECT_TYPE + 1);
3552 __ cmp(r1, Operand(JS_FUNCTION_TYPE));
3553 function.Branch(eq);
3554
3555 // Check if the constructor in the map is a function.
3556 __ ldr(r0, FieldMemOperand(r0, Map::kConstructorOffset));
3557 __ CompareObjectType(r0, r1, r1, JS_FUNCTION_TYPE);
3558 non_function_constructor.Branch(ne);
3559
3560 // The r0 register now contains the constructor function. Grab the
3561 // instance class name from there.
3562 __ ldr(r0, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
3563 __ ldr(r0, FieldMemOperand(r0, SharedFunctionInfo::kInstanceClassNameOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003564 frame_->EmitPush(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003565 leave.Jump();
3566
3567 // Functions have class 'Function'.
3568 function.Bind();
3569 __ mov(r0, Operand(Factory::function_class_symbol()));
3570 frame_->EmitPush(r0);
3571 leave.Jump();
3572
3573 // Objects with a non-function constructor have class 'Object'.
3574 non_function_constructor.Bind();
3575 __ mov(r0, Operand(Factory::Object_symbol()));
3576 frame_->EmitPush(r0);
3577 leave.Jump();
3578
3579 // Non-JS objects have class null.
3580 null.Bind();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003581 __ LoadRoot(r0, Heap::kNullValueRootIndex);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003582 frame_->EmitPush(r0);
3583
3584 // All done.
3585 leave.Bind();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003586}
3587
3588
ager@chromium.org7c537e22008-10-16 08:43:32 +00003589void CodeGenerator::GenerateValueOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003590 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003591 ASSERT(args->length() == 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003592 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003593 LoadAndSpill(args->at(0));
3594 frame_->EmitPop(r0); // r0 contains object.
mads.s.ager31e71382008-08-13 09:32:07 +00003595 // if (object->IsSmi()) return the object.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003596 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003597 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003598 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3599 __ CompareObjectType(r0, r1, r1, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003600 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003601 // Load the value.
3602 __ ldr(r0, FieldMemOperand(r0, JSValue::kValueOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003603 leave.Bind();
3604 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003605}
3606
3607
ager@chromium.org7c537e22008-10-16 08:43:32 +00003608void CodeGenerator::GenerateSetValueOf(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003609 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003610 ASSERT(args->length() == 2);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003611 JumpTarget leave;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003612 LoadAndSpill(args->at(0)); // Load the object.
3613 LoadAndSpill(args->at(1)); // Load the value.
3614 frame_->EmitPop(r0); // r0 contains value
3615 frame_->EmitPop(r1); // r1 contains object
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003616 // if (object->IsSmi()) return object.
3617 __ tst(r1, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003618 leave.Branch(eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003619 // It is a heap object - get map. If (!object->IsJSValue()) return the object.
3620 __ CompareObjectType(r1, r2, r2, JS_VALUE_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003621 leave.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003622 // Store the value.
3623 __ str(r0, FieldMemOperand(r1, JSValue::kValueOffset));
3624 // Update the write barrier.
3625 __ mov(r2, Operand(JSValue::kValueOffset - kHeapObjectTag));
3626 __ RecordWrite(r1, r2, r3);
3627 // Leave.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003628 leave.Bind();
3629 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003630}
3631
3632
ager@chromium.org7c537e22008-10-16 08:43:32 +00003633void CodeGenerator::GenerateIsSmi(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003634 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003635 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003636 LoadAndSpill(args->at(0));
3637 frame_->EmitPop(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00003638 __ tst(r0, Operand(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003639 cc_reg_ = eq;
3640}
3641
3642
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003643void CodeGenerator::GenerateLog(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003644 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003645 // See comment in CodeGenerator::GenerateLog in codegen-ia32.cc.
3646 ASSERT_EQ(args->length(), 3);
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003647#ifdef ENABLE_LOGGING_AND_PROFILING
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003648 if (ShouldGenerateLog(args->at(0))) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003649 LoadAndSpill(args->at(1));
3650 LoadAndSpill(args->at(2));
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003651 __ CallRuntime(Runtime::kLog, 2);
3652 }
christian.plesner.hansen@gmail.comaca49682009-01-07 14:29:04 +00003653#endif
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003654 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003655 frame_->EmitPush(r0);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00003656}
3657
3658
ager@chromium.org7c537e22008-10-16 08:43:32 +00003659void CodeGenerator::GenerateIsNonNegativeSmi(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003660 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003661 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003662 LoadAndSpill(args->at(0));
3663 frame_->EmitPop(r0);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00003664 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +00003665 cc_reg_ = eq;
3666}
3667
3668
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003669// Generates the Math.pow method - currently just calls runtime.
3670void CodeGenerator::GenerateMathPow(ZoneList<Expression*>* args) {
3671 ASSERT(args->length() == 2);
3672 Load(args->at(0));
3673 Load(args->at(1));
3674 frame_->CallRuntime(Runtime::kMath_pow, 2);
3675 frame_->EmitPush(r0);
3676}
3677
3678
3679// Generates the Math.sqrt method - currently just calls runtime.
3680void CodeGenerator::GenerateMathSqrt(ZoneList<Expression*>* args) {
3681 ASSERT(args->length() == 1);
3682 Load(args->at(0));
3683 frame_->CallRuntime(Runtime::kMath_sqrt, 1);
3684 frame_->EmitPush(r0);
3685}
3686
3687
kasper.lund7276f142008-07-30 08:49:36 +00003688// This should generate code that performs a charCodeAt() call or returns
3689// undefined in order to trigger the slow case, Runtime_StringCharCodeAt.
3690// It is not yet implemented on ARM, so it always goes to the slow case.
ager@chromium.org7c537e22008-10-16 08:43:32 +00003691void CodeGenerator::GenerateFastCharCodeAt(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003692 VirtualFrame::SpilledScope spilled_scope(frame_);
kasper.lund7276f142008-07-30 08:49:36 +00003693 ASSERT(args->length() == 2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003694 Comment(masm_, "[ GenerateFastCharCodeAt");
3695
3696 LoadAndSpill(args->at(0));
3697 LoadAndSpill(args->at(1));
3698 frame_->EmitPop(r0); // Index.
3699 frame_->EmitPop(r1); // String.
3700
3701 Label slow, end, not_a_flat_string, ascii_string, try_again_with_new_string;
3702
3703 __ tst(r1, Operand(kSmiTagMask));
3704 __ b(eq, &slow); // The 'string' was a Smi.
3705
3706 ASSERT(kSmiTag == 0);
3707 __ tst(r0, Operand(kSmiTagMask | 0x80000000u));
3708 __ b(ne, &slow); // The index was negative or not a Smi.
3709
3710 __ bind(&try_again_with_new_string);
3711 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
3712 __ b(ge, &slow);
3713
3714 // Now r2 has the string type.
3715 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003716 // Now r3 has the length of the string. Compare with the index.
3717 __ cmp(r3, Operand(r0, LSR, kSmiTagSize));
3718 __ b(le, &slow);
3719
3720 // Here we know the index is in range. Check that string is sequential.
3721 ASSERT_EQ(0, kSeqStringTag);
3722 __ tst(r2, Operand(kStringRepresentationMask));
3723 __ b(ne, &not_a_flat_string);
3724
3725 // Check whether it is an ASCII string.
3726 ASSERT_EQ(0, kTwoByteStringTag);
3727 __ tst(r2, Operand(kStringEncodingMask));
3728 __ b(ne, &ascii_string);
3729
3730 // 2-byte string. We can add without shifting since the Smi tag size is the
3731 // log2 of the number of bytes in a two-byte character.
3732 ASSERT_EQ(1, kSmiTagSize);
3733 ASSERT_EQ(0, kSmiShiftSize);
3734 __ add(r1, r1, Operand(r0));
3735 __ ldrh(r0, FieldMemOperand(r1, SeqTwoByteString::kHeaderSize));
3736 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
3737 __ jmp(&end);
3738
3739 __ bind(&ascii_string);
3740 __ add(r1, r1, Operand(r0, LSR, kSmiTagSize));
3741 __ ldrb(r0, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
3742 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
3743 __ jmp(&end);
3744
3745 __ bind(&not_a_flat_string);
3746 __ and_(r2, r2, Operand(kStringRepresentationMask));
3747 __ cmp(r2, Operand(kConsStringTag));
3748 __ b(ne, &slow);
3749
3750 // ConsString.
3751 // Check that the right hand side is the empty string (ie if this is really a
3752 // flat string in a cons string). If that is not the case we would rather go
3753 // to the runtime system now, to flatten the string.
3754 __ ldr(r2, FieldMemOperand(r1, ConsString::kSecondOffset));
3755 __ LoadRoot(r3, Heap::kEmptyStringRootIndex);
3756 __ cmp(r2, Operand(r3));
3757 __ b(ne, &slow);
3758
3759 // Get the first of the two strings.
3760 __ ldr(r1, FieldMemOperand(r1, ConsString::kFirstOffset));
3761 __ jmp(&try_again_with_new_string);
3762
3763 __ bind(&slow);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00003764 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00003765
3766 __ bind(&end);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003767 frame_->EmitPush(r0);
kasper.lund7276f142008-07-30 08:49:36 +00003768}
3769
3770
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00003771void CodeGenerator::GenerateCharFromCode(ZoneList<Expression*>* args) {
3772 Comment(masm_, "[ GenerateCharFromCode");
3773 ASSERT(args->length() == 1);
3774
3775 LoadAndSpill(args->at(0));
3776 frame_->EmitPop(r0);
3777
3778 JumpTarget slow_case;
3779 JumpTarget exit;
3780
3781 // Fast case of Heap::LookupSingleCharacterStringFromCode.
3782 ASSERT(kSmiTag == 0);
3783 ASSERT(kSmiShiftSize == 0);
3784 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
3785 __ tst(r0, Operand(kSmiTagMask |
3786 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
3787 slow_case.Branch(nz);
3788
3789 ASSERT(kSmiTag == 0);
3790 __ mov(r1, Operand(Factory::single_character_string_cache()));
3791 __ add(r1, r1, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
3792 __ ldr(r1, MemOperand(r1, FixedArray::kHeaderSize - kHeapObjectTag));
3793 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3794 __ cmp(r1, ip);
3795 slow_case.Branch(eq);
3796
3797 frame_->EmitPush(r1);
3798 exit.Jump();
3799
3800 slow_case.Bind();
3801 frame_->EmitPush(r0);
3802 frame_->CallRuntime(Runtime::kCharFromCode, 1);
3803 frame_->EmitPush(r0);
3804
3805 exit.Bind();
3806}
3807
3808
ager@chromium.org7c537e22008-10-16 08:43:32 +00003809void CodeGenerator::GenerateIsArray(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003810 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003811 ASSERT(args->length() == 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003812 LoadAndSpill(args->at(0));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00003813 JumpTarget answer;
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003814 // We need the CC bits to come out as not_equal in the case where the
3815 // object is a smi. This can't be done with the usual test opcode so
3816 // we use XOR to get the right CC bits.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003817 frame_->EmitPop(r0);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003818 __ and_(r1, r0, Operand(kSmiTagMask));
3819 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003820 answer.Branch(ne);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003821 // It is a heap object - get the map. Check if the object is a JS array.
3822 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003823 answer.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003824 cc_reg_ = eq;
3825}
3826
3827
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00003828void CodeGenerator::GenerateIsRegExp(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003829 VirtualFrame::SpilledScope spilled_scope(frame_);
fschneider@chromium.orgb95b98b2010-02-23 10:34:29 +00003830 ASSERT(args->length() == 1);
3831 LoadAndSpill(args->at(0));
3832 JumpTarget answer;
3833 // We need the CC bits to come out as not_equal in the case where the
3834 // object is a smi. This can't be done with the usual test opcode so
3835 // we use XOR to get the right CC bits.
3836 frame_->EmitPop(r0);
3837 __ and_(r1, r0, Operand(kSmiTagMask));
3838 __ eor(r1, r1, Operand(kSmiTagMask), SetCC);
3839 answer.Branch(ne);
3840 // It is a heap object - get the map. Check if the object is a regexp.
3841 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
3842 answer.Bind();
3843 cc_reg_ = eq;
3844}
3845
3846
ager@chromium.org6141cbe2009-11-20 12:14:52 +00003847void CodeGenerator::GenerateIsObject(ZoneList<Expression*>* args) {
3848 // This generates a fast version of:
3849 // (typeof(arg) === 'object' || %_ClassOf(arg) == 'RegExp')
ager@chromium.org357bf652010-04-12 11:30:10 +00003850 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00003851 ASSERT(args->length() == 1);
3852 LoadAndSpill(args->at(0));
3853 frame_->EmitPop(r1);
3854 __ tst(r1, Operand(kSmiTagMask));
3855 false_target()->Branch(eq);
3856
3857 __ LoadRoot(ip, Heap::kNullValueRootIndex);
3858 __ cmp(r1, ip);
3859 true_target()->Branch(eq);
3860
3861 Register map_reg = r2;
3862 __ ldr(map_reg, FieldMemOperand(r1, HeapObject::kMapOffset));
3863 // Undetectable objects behave like undefined when tested with typeof.
3864 __ ldrb(r1, FieldMemOperand(map_reg, Map::kBitFieldOffset));
3865 __ and_(r1, r1, Operand(1 << Map::kIsUndetectable));
3866 __ cmp(r1, Operand(1 << Map::kIsUndetectable));
3867 false_target()->Branch(eq);
3868
3869 __ ldrb(r1, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
3870 __ cmp(r1, Operand(FIRST_JS_OBJECT_TYPE));
3871 false_target()->Branch(lt);
3872 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
3873 cc_reg_ = le;
3874}
3875
3876
3877void CodeGenerator::GenerateIsFunction(ZoneList<Expression*>* args) {
3878 // This generates a fast version of:
3879 // (%_ClassOf(arg) === 'Function')
ager@chromium.org357bf652010-04-12 11:30:10 +00003880 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org6141cbe2009-11-20 12:14:52 +00003881 ASSERT(args->length() == 1);
3882 LoadAndSpill(args->at(0));
3883 frame_->EmitPop(r0);
3884 __ tst(r0, Operand(kSmiTagMask));
3885 false_target()->Branch(eq);
3886 Register map_reg = r2;
3887 __ CompareObjectType(r0, map_reg, r1, JS_FUNCTION_TYPE);
3888 cc_reg_ = eq;
3889}
3890
3891
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003892void CodeGenerator::GenerateIsUndetectableObject(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003893 VirtualFrame::SpilledScope spilled_scope(frame_);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00003894 ASSERT(args->length() == 1);
3895 LoadAndSpill(args->at(0));
3896 frame_->EmitPop(r0);
3897 __ tst(r0, Operand(kSmiTagMask));
3898 false_target()->Branch(eq);
3899 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3900 __ ldrb(r1, FieldMemOperand(r1, Map::kBitFieldOffset));
3901 __ tst(r1, Operand(1 << Map::kIsUndetectable));
3902 cc_reg_ = ne;
3903}
3904
3905
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003906void CodeGenerator::GenerateIsConstructCall(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003907 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003908 ASSERT(args->length() == 0);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003909
3910 // Get the frame pointer for the calling frame.
3911 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3912
3913 // Skip the arguments adaptor frame if it exists.
3914 Label check_frame_marker;
3915 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00003916 __ cmp(r1, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00003917 __ b(ne, &check_frame_marker);
3918 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
3919
3920 // Check the marker in the calling frame.
3921 __ bind(&check_frame_marker);
3922 __ ldr(r1, MemOperand(r2, StandardFrameConstants::kMarkerOffset));
3923 __ cmp(r1, Operand(Smi::FromInt(StackFrame::CONSTRUCT)));
3924 cc_reg_ = eq;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00003925}
3926
3927
ager@chromium.org7c537e22008-10-16 08:43:32 +00003928void CodeGenerator::GenerateArgumentsLength(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003929 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003930 ASSERT(args->length() == 0);
3931
lrn@chromium.org25156de2010-04-06 13:10:27 +00003932 Label exit;
3933
3934 // Get the number of formal parameters.
ager@chromium.org5c838252010-02-19 08:53:10 +00003935 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003936
lrn@chromium.org25156de2010-04-06 13:10:27 +00003937 // Check if the calling frame is an arguments adaptor frame.
3938 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3939 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
3940 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3941 __ b(ne, &exit);
3942
3943 // Arguments adaptor case: Read the arguments length from the
3944 // adaptor frame.
3945 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3946
3947 __ bind(&exit);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003948 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003949}
3950
3951
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00003952void CodeGenerator::GenerateArguments(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00003953 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003954 ASSERT(args->length() == 1);
3955
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00003956 // Satisfy contract with ArgumentsAccessStub:
3957 // Load the key into r1 and the formal parameters count into r0.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003958 LoadAndSpill(args->at(0));
3959 frame_->EmitPop(r1);
ager@chromium.org5c838252010-02-19 08:53:10 +00003960 __ mov(r0, Operand(Smi::FromInt(scope()->num_parameters())));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003961
3962 // Call the shared stub to get to arguments[key].
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00003963 ArgumentsAccessStub stub(ArgumentsAccessStub::READ_ELEMENT);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00003964 frame_->CallStub(&stub, 0);
3965 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00003966}
3967
3968
ager@chromium.org357bf652010-04-12 11:30:10 +00003969void CodeGenerator::GenerateRandomHeapNumber(
3970 ZoneList<Expression*>* args) {
3971 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003972 ASSERT(args->length() == 0);
ager@chromium.org357bf652010-04-12 11:30:10 +00003973
3974 Label slow_allocate_heapnumber;
3975 Label heapnumber_allocated;
3976
3977 __ AllocateHeapNumber(r0, r1, r2, &slow_allocate_heapnumber);
3978 __ jmp(&heapnumber_allocated);
3979
3980 __ bind(&slow_allocate_heapnumber);
3981 __ mov(r0, Operand(Smi::FromInt(0)));
3982 __ push(r0);
3983 __ CallRuntime(Runtime::kNumberUnaryMinus, 1);
3984
3985 __ bind(&heapnumber_allocated);
3986 __ PrepareCallCFunction(1, r1);
3987 __ CallCFunction(
3988 ExternalReference::fill_heap_number_with_random_function(), 1);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00003989 frame_->EmitPush(r0);
3990}
3991
3992
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00003993void CodeGenerator::GenerateStringAdd(ZoneList<Expression*>* args) {
3994 ASSERT_EQ(2, args->length());
3995
3996 Load(args->at(0));
3997 Load(args->at(1));
3998
ager@chromium.org5c838252010-02-19 08:53:10 +00003999 StringAddStub stub(NO_STRING_ADD_FLAGS);
4000 frame_->CallStub(&stub, 2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00004001 frame_->EmitPush(r0);
4002}
4003
4004
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004005void CodeGenerator::GenerateSubString(ZoneList<Expression*>* args) {
4006 ASSERT_EQ(3, args->length());
4007
4008 Load(args->at(0));
4009 Load(args->at(1));
4010 Load(args->at(2));
4011
ager@chromium.org5c838252010-02-19 08:53:10 +00004012 SubStringStub stub;
4013 frame_->CallStub(&stub, 3);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004014 frame_->EmitPush(r0);
4015}
4016
4017
4018void CodeGenerator::GenerateStringCompare(ZoneList<Expression*>* args) {
4019 ASSERT_EQ(2, args->length());
4020
4021 Load(args->at(0));
4022 Load(args->at(1));
4023
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004024 StringCompareStub stub;
4025 frame_->CallStub(&stub, 2);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004026 frame_->EmitPush(r0);
4027}
4028
4029
4030void CodeGenerator::GenerateRegExpExec(ZoneList<Expression*>* args) {
4031 ASSERT_EQ(4, args->length());
4032
4033 Load(args->at(0));
4034 Load(args->at(1));
4035 Load(args->at(2));
4036 Load(args->at(3));
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004037 RegExpExecStub stub;
4038 frame_->CallStub(&stub, 4);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004039 frame_->EmitPush(r0);
4040}
4041
4042
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +00004043void CodeGenerator::GenerateRegExpConstructResult(ZoneList<Expression*>* args) {
4044 // No stub. This code only occurs a few times in regexp.js.
4045 const int kMaxInlineLength = 100;
4046 ASSERT_EQ(3, args->length());
4047 Load(args->at(0)); // Size of array, smi.
4048 Load(args->at(1)); // "index" property value.
4049 Load(args->at(2)); // "input" property value.
4050 {
4051 VirtualFrame::SpilledScope spilled_scope(frame_);
4052 Label slowcase;
4053 Label done;
4054 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
4055 STATIC_ASSERT(kSmiTag == 0);
4056 STATIC_ASSERT(kSmiTagSize == 1);
4057 __ tst(r1, Operand(kSmiTagMask));
4058 __ b(ne, &slowcase);
4059 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
4060 __ b(hi, &slowcase);
4061 // Smi-tagging is equivalent to multiplying by 2.
4062 // Allocate RegExpResult followed by FixedArray with size in ebx.
4063 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
4064 // Elements: [Map][Length][..elements..]
4065 // Size of JSArray with two in-object properties and the header of a
4066 // FixedArray.
4067 int objects_size =
4068 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
4069 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
4070 __ add(r2, r5, Operand(objects_size));
4071 __ AllocateInNewSpace(r2, // In: Size, in words.
4072 r0, // Out: Start of allocation (tagged).
4073 r3, // Scratch register.
4074 r4, // Scratch register.
4075 &slowcase,
4076 TAG_OBJECT);
4077 // r0: Start of allocated area, object-tagged.
4078 // r1: Number of elements in array, as smi.
4079 // r5: Number of elements, untagged.
4080
4081 // Set JSArray map to global.regexp_result_map().
4082 // Set empty properties FixedArray.
4083 // Set elements to point to FixedArray allocated right after the JSArray.
4084 // Interleave operations for better latency.
4085 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
4086 __ add(r3, r0, Operand(JSRegExpResult::kSize));
4087 __ mov(r4, Operand(Factory::empty_fixed_array()));
4088 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
4089 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
4090 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
4091 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
4092 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4093
4094 // Set input, index and length fields from arguments.
4095 __ ldm(ia_w, sp, static_cast<RegList>(r2.bit() | r4.bit()));
4096 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
4097 __ add(sp, sp, Operand(kPointerSize));
4098 __ str(r4, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
4099 __ str(r2, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
4100
4101 // Fill out the elements FixedArray.
4102 // r0: JSArray, tagged.
4103 // r3: FixedArray, tagged.
4104 // r5: Number of elements in array, untagged.
4105
4106 // Set map.
4107 __ mov(r2, Operand(Factory::fixed_array_map()));
4108 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
4109 // Set FixedArray length.
4110 __ str(r5, FieldMemOperand(r3, FixedArray::kLengthOffset));
4111 // Fill contents of fixed-array with the-hole.
4112 __ mov(r2, Operand(Factory::the_hole_value()));
4113 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4114 // Fill fixed array elements with hole.
4115 // r0: JSArray, tagged.
4116 // r2: the hole.
4117 // r3: Start of elements in FixedArray.
4118 // r5: Number of elements to fill.
4119 Label loop;
4120 __ tst(r5, Operand(r5));
4121 __ bind(&loop);
4122 __ b(le, &done); // Jump if r1 is negative or zero.
4123 __ sub(r5, r5, Operand(1), SetCC);
4124 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
4125 __ jmp(&loop);
4126
4127 __ bind(&slowcase);
4128 __ CallRuntime(Runtime::kRegExpConstructResult, 3);
4129
4130 __ bind(&done);
4131 }
4132 frame_->Forget(3);
4133 frame_->EmitPush(r0);
4134}
4135
4136
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00004137class DeferredSearchCache: public DeferredCode {
4138 public:
4139 DeferredSearchCache(Register dst, Register cache, Register key)
4140 : dst_(dst), cache_(cache), key_(key) {
4141 set_comment("[ DeferredSearchCache");
4142 }
4143
4144 virtual void Generate();
4145
4146 private:
4147 Register dst_, cache_, key_;
4148};
4149
4150
4151void DeferredSearchCache::Generate() {
4152 __ push(cache_);
4153 __ push(key_);
4154 __ CallRuntime(Runtime::kGetFromCache, 2);
4155 if (!dst_.is(r0)) {
4156 __ mov(dst_, r0);
4157 }
4158}
4159
4160
4161void CodeGenerator::GenerateGetFromCache(ZoneList<Expression*>* args) {
4162 ASSERT_EQ(2, args->length());
4163
4164 ASSERT_NE(NULL, args->at(0)->AsLiteral());
4165 int cache_id = Smi::cast(*(args->at(0)->AsLiteral()->handle()))->value();
4166
4167 Handle<FixedArray> jsfunction_result_caches(
4168 Top::global_context()->jsfunction_result_caches());
4169 if (jsfunction_result_caches->length() <= cache_id) {
4170 __ Abort("Attempt to use undefined cache.");
4171 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
4172 frame_->EmitPush(r0);
4173 return;
4174 }
4175 Handle<FixedArray> cache_obj(
4176 FixedArray::cast(jsfunction_result_caches->get(cache_id)));
4177
4178 Load(args->at(1));
4179 frame_->EmitPop(r2);
4180
4181 DeferredSearchCache* deferred = new DeferredSearchCache(r0, r1, r2);
4182
4183 const int kFingerOffset =
4184 FixedArray::OffsetOfElementAt(JSFunctionResultCache::kFingerIndex);
4185 ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
4186 __ mov(r1, Operand(cache_obj));
4187 __ ldr(r0, FieldMemOperand(r1, kFingerOffset));
4188 // r0 now holds finger offset as a smi.
4189 __ add(r3, r1, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4190 // r3 now points to the start of fixed array elements.
4191 __ ldr(r0, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize, PreIndex));
4192 // Note side effect of PreIndex: r3 now points to the key of the pair.
4193 __ cmp(r2, r0);
4194 deferred->Branch(ne);
4195
4196 __ ldr(r0, MemOperand(r3, kPointerSize));
4197
4198 deferred->BindExit();
4199 frame_->EmitPush(r0);
4200}
4201
4202
ager@chromium.org5c838252010-02-19 08:53:10 +00004203void CodeGenerator::GenerateNumberToString(ZoneList<Expression*>* args) {
4204 ASSERT_EQ(args->length(), 1);
4205
4206 // Load the argument on the stack and jump to the runtime.
4207 Load(args->at(0));
4208
fschneider@chromium.org086aac62010-03-17 13:18:24 +00004209 NumberToStringStub stub;
4210 frame_->CallStub(&stub, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00004211 frame_->EmitPush(r0);
4212}
4213
4214
ager@chromium.org357bf652010-04-12 11:30:10 +00004215void CodeGenerator::GenerateCallFunction(ZoneList<Expression*>* args) {
4216 Comment cmnt(masm_, "[ GenerateCallFunction");
4217
4218 ASSERT(args->length() >= 2);
4219
4220 int n_args = args->length() - 2; // for receiver and function.
4221 Load(args->at(0)); // receiver
4222 for (int i = 0; i < n_args; i++) {
4223 Load(args->at(i + 1));
4224 }
4225 Load(args->at(n_args + 1)); // function
4226 frame_->CallJSFunction(n_args);
4227 frame_->EmitPush(r0);
4228}
4229
4230
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00004231void CodeGenerator::GenerateMathSin(ZoneList<Expression*>* args) {
4232 ASSERT_EQ(args->length(), 1);
4233 // Load the argument on the stack and jump to the runtime.
4234 Load(args->at(0));
4235 frame_->CallRuntime(Runtime::kMath_sin, 1);
4236 frame_->EmitPush(r0);
4237}
4238
4239
4240void CodeGenerator::GenerateMathCos(ZoneList<Expression*>* args) {
4241 ASSERT_EQ(args->length(), 1);
4242 // Load the argument on the stack and jump to the runtime.
4243 Load(args->at(0));
4244 frame_->CallRuntime(Runtime::kMath_cos, 1);
4245 frame_->EmitPush(r0);
4246}
4247
4248
ager@chromium.org7c537e22008-10-16 08:43:32 +00004249void CodeGenerator::GenerateObjectEquals(ZoneList<Expression*>* args) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004250 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004251 ASSERT(args->length() == 2);
4252
4253 // Load the two objects into registers and perform the comparison.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004254 LoadAndSpill(args->at(0));
4255 LoadAndSpill(args->at(1));
4256 frame_->EmitPop(r0);
4257 frame_->EmitPop(r1);
ager@chromium.org9258b6b2008-09-11 09:11:10 +00004258 __ cmp(r0, Operand(r1));
4259 cc_reg_ = eq;
4260}
4261
4262
ager@chromium.org7c537e22008-10-16 08:43:32 +00004263void CodeGenerator::VisitCallRuntime(CallRuntime* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004264#ifdef DEBUG
4265 int original_height = frame_->height();
4266#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004267 VirtualFrame::SpilledScope spilled_scope(frame_);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004268 if (CheckForInlineRuntimeCall(node)) {
4269 ASSERT((has_cc() && frame_->height() == original_height) ||
4270 (!has_cc() && frame_->height() == original_height + 1));
4271 return;
4272 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004273
4274 ZoneList<Expression*>* args = node->arguments();
4275 Comment cmnt(masm_, "[ CallRuntime");
4276 Runtime::Function* function = node->function();
4277
ager@chromium.org41826e72009-03-30 13:30:57 +00004278 if (function == NULL) {
mads.s.ager31e71382008-08-13 09:32:07 +00004279 // Prepare stack for calling JS runtime function.
mads.s.ager31e71382008-08-13 09:32:07 +00004280 // Push the builtins object found in the current global object.
4281 __ ldr(r1, GlobalObject());
4282 __ ldr(r0, FieldMemOperand(r1, GlobalObject::kBuiltinsOffset));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004283 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00004284 }
mads.s.ager31e71382008-08-13 09:32:07 +00004285
ager@chromium.org41826e72009-03-30 13:30:57 +00004286 // Push the arguments ("left-to-right").
4287 int arg_count = args->length();
4288 for (int i = 0; i < arg_count; i++) {
4289 LoadAndSpill(args->at(i));
4290 }
mads.s.ager31e71382008-08-13 09:32:07 +00004291
ager@chromium.org41826e72009-03-30 13:30:57 +00004292 if (function == NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004293 // Call the JS runtime function.
ager@chromium.org5c838252010-02-19 08:53:10 +00004294 __ mov(r2, Operand(node->name()));
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004295 InLoopFlag in_loop = loop_nesting() > 0 ? IN_LOOP : NOT_IN_LOOP;
4296 Handle<Code> stub = ComputeCallInitialize(arg_count, in_loop);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004297 frame_->CallCodeObject(stub, RelocInfo::CODE_TARGET, arg_count + 1);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004298 __ ldr(cp, frame_->Context());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004299 frame_->EmitPush(r0);
ager@chromium.org41826e72009-03-30 13:30:57 +00004300 } else {
4301 // Call the C runtime function.
4302 frame_->CallRuntime(function, arg_count);
4303 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004304 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004305 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004306}
4307
4308
ager@chromium.org7c537e22008-10-16 08:43:32 +00004309void CodeGenerator::VisitUnaryOperation(UnaryOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004310#ifdef DEBUG
4311 int original_height = frame_->height();
4312#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004313 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004314 Comment cmnt(masm_, "[ UnaryOperation");
4315
4316 Token::Value op = node->op();
4317
4318 if (op == Token::NOT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004319 LoadConditionAndSpill(node->expression(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004320 false_target(),
4321 true_target(),
4322 true);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00004323 // LoadCondition may (and usually does) leave a test and branch to
4324 // be emitted by the caller. In that case, negate the condition.
4325 if (has_cc()) cc_reg_ = NegateCondition(cc_reg_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004326
4327 } else if (op == Token::DELETE) {
4328 Property* property = node->expression()->AsProperty();
mads.s.ager31e71382008-08-13 09:32:07 +00004329 Variable* variable = node->expression()->AsVariableProxy()->AsVariable();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004330 if (property != NULL) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004331 LoadAndSpill(property->obj());
4332 LoadAndSpill(property->key());
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004333 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004334
mads.s.ager31e71382008-08-13 09:32:07 +00004335 } else if (variable != NULL) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004336 Slot* slot = variable->slot();
4337 if (variable->is_global()) {
4338 LoadGlobal();
mads.s.ager31e71382008-08-13 09:32:07 +00004339 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004340 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004341 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004342
4343 } else if (slot != NULL && slot->type() == Slot::LOOKUP) {
4344 // lookup the context holding the named variable
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004345 frame_->EmitPush(cp);
mads.s.ager31e71382008-08-13 09:32:07 +00004346 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004347 frame_->EmitPush(r0);
4348 frame_->CallRuntime(Runtime::kLookupContext, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004349 // r0: context
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004350 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00004351 __ mov(r0, Operand(variable->name()));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004352 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004353 frame_->InvokeBuiltin(Builtins::DELETE, CALL_JS, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004354
mads.s.ager31e71382008-08-13 09:32:07 +00004355 } else {
4356 // Default: Result of deleting non-global, not dynamically
4357 // introduced variables is false.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004358 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
mads.s.ager31e71382008-08-13 09:32:07 +00004359 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004360
4361 } else {
4362 // Default: Result of deleting expressions is true.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004363 LoadAndSpill(node->expression()); // may have side-effects
4364 frame_->Drop();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004365 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004366 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004367 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004368
4369 } else if (op == Token::TYPEOF) {
4370 // Special case for loading the typeof expression; see comment on
4371 // LoadTypeofExpression().
4372 LoadTypeofExpression(node->expression());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004373 frame_->CallRuntime(Runtime::kTypeof, 1);
4374 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004375
4376 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004377 bool overwrite =
4378 (node->expression()->AsBinaryOperation() != NULL &&
4379 node->expression()->AsBinaryOperation()->ResultOverwriteAllowed());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004380 LoadAndSpill(node->expression());
4381 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004382 switch (op) {
4383 case Token::NOT:
4384 case Token::DELETE:
4385 case Token::TYPEOF:
4386 UNREACHABLE(); // handled above
4387 break;
4388
4389 case Token::SUB: {
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00004390 GenericUnaryOpStub stub(Token::SUB, overwrite);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004391 frame_->CallStub(&stub, 0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004392 break;
4393 }
4394
4395 case Token::BIT_NOT: {
4396 // smi check
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004397 JumpTarget smi_label;
4398 JumpTarget continue_label;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004399 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004400 smi_label.Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004401
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004402 GenericUnaryOpStub stub(Token::BIT_NOT, overwrite);
4403 frame_->CallStub(&stub, 0);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004404 continue_label.Jump();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004405
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004406 smi_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004407 __ mvn(r0, Operand(r0));
4408 __ bic(r0, r0, Operand(kSmiTagMask)); // bit-clear inverted smi-tag
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004409 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004410 break;
4411 }
4412
4413 case Token::VOID:
4414 // since the stack top is cached in r0, popping and then
4415 // pushing a value can be done by just writing to r0.
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004416 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004417 break;
4418
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004419 case Token::ADD: {
4420 // Smi check.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004421 JumpTarget continue_label;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004422 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004423 continue_label.Branch(eq);
4424 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004425 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004426 continue_label.Bind();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004427 break;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00004428 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004429 default:
4430 UNREACHABLE();
4431 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004432 frame_->EmitPush(r0); // r0 has result
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004433 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004434 ASSERT(!has_valid_frame() ||
4435 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004436 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004437}
4438
4439
ager@chromium.org7c537e22008-10-16 08:43:32 +00004440void CodeGenerator::VisitCountOperation(CountOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004441#ifdef DEBUG
4442 int original_height = frame_->height();
4443#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004444 VirtualFrame::SpilledScope spilled_scope(frame_);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004445 Comment cmnt(masm_, "[ CountOperation");
4446
4447 bool is_postfix = node->is_postfix();
4448 bool is_increment = node->op() == Token::INC;
4449
4450 Variable* var = node->expression()->AsVariableProxy()->AsVariable();
4451 bool is_const = (var != NULL && var->mode() == Variable::CONST);
4452
4453 // Postfix: Make room for the result.
mads.s.ager31e71382008-08-13 09:32:07 +00004454 if (is_postfix) {
4455 __ mov(r0, Operand(0));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004456 frame_->EmitPush(r0);
mads.s.ager31e71382008-08-13 09:32:07 +00004457 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004458
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004459 // A constant reference is not saved to, so a constant reference is not a
4460 // compound assignment reference.
4461 { Reference target(this, node->expression(), !is_const);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004462 if (target.is_illegal()) {
4463 // Spoof the virtual frame to have the expected height (one higher
4464 // than on entry).
4465 if (!is_postfix) {
4466 __ mov(r0, Operand(Smi::FromInt(0)));
4467 frame_->EmitPush(r0);
4468 }
4469 ASSERT(frame_->height() == original_height + 1);
4470 return;
4471 }
ager@chromium.org357bf652010-04-12 11:30:10 +00004472 target.GetValue();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004473 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004474
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004475 JumpTarget slow;
4476 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004477
4478 // Load the value (1) into register r1.
4479 __ mov(r1, Operand(Smi::FromInt(1)));
4480
4481 // Check for smi operand.
4482 __ tst(r0, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004483 slow.Branch(ne);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004484
4485 // Postfix: Store the old value as the result.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004486 if (is_postfix) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004487 __ str(r0, frame_->ElementAt(target.size()));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004488 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004489
4490 // Perform optimistic increment/decrement.
4491 if (is_increment) {
4492 __ add(r0, r0, Operand(r1), SetCC);
4493 } else {
4494 __ sub(r0, r0, Operand(r1), SetCC);
4495 }
4496
4497 // If the increment/decrement didn't overflow, we're done.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004498 exit.Branch(vc);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004499
4500 // Revert optimistic increment/decrement.
4501 if (is_increment) {
4502 __ sub(r0, r0, Operand(r1));
4503 } else {
4504 __ add(r0, r0, Operand(r1));
4505 }
4506
4507 // Slow case: Convert to number.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004508 slow.Bind();
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004509 {
4510 // Convert the operand to a number.
4511 frame_->EmitPush(r0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004512 frame_->InvokeBuiltin(Builtins::TO_NUMBER, CALL_JS, 1);
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004513 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004514 if (is_postfix) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004515 // Postfix: store to result (on the stack).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004516 __ str(r0, frame_->ElementAt(target.size()));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004517 }
4518
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004519 // Compute the new value.
4520 __ mov(r1, Operand(Smi::FromInt(1)));
4521 frame_->EmitPush(r0);
4522 frame_->EmitPush(r1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004523 if (is_increment) {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004524 frame_->CallRuntime(Runtime::kNumberAdd, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004525 } else {
kasperl@chromium.org8ccb0be2009-04-07 07:21:39 +00004526 frame_->CallRuntime(Runtime::kNumberSub, 2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004527 }
4528
4529 // Store the new value in the target if not const.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004530 exit.Bind();
4531 frame_->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004532 if (!is_const) target.SetValue(NOT_CONST_INIT);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004533 }
4534
4535 // Postfix: Discard the new value and use the old.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004536 if (is_postfix) frame_->EmitPop(r0);
4537 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004538}
4539
4540
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004541void CodeGenerator::GenerateLogicalBooleanOperation(BinaryOperation* node) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004542 // According to ECMA-262 section 11.11, page 58, the binary logical
4543 // operators must yield the result of one of the two expressions
4544 // before any ToBoolean() conversions. This means that the value
4545 // produced by a && or || operator is not necessarily a boolean.
4546
4547 // NOTE: If the left hand side produces a materialized value (not in
4548 // the CC register), we force the right hand side to do the
4549 // same. This is necessary because we may have to branch to the exit
4550 // after evaluating the left hand side (due to the shortcut
4551 // semantics), but the compiler must (statically) know if the result
4552 // of compiling the binary operation is materialized or not.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004553 if (node->op() == Token::AND) {
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004554 JumpTarget is_true;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004555 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004556 &is_true,
4557 false_target(),
4558 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004559 if (has_valid_frame() && !has_cc()) {
4560 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004561 JumpTarget pop_and_continue;
4562 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004563
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004564 __ ldr(r0, frame_->Top()); // Duplicate the stack top.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004565 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004566 // Avoid popping the result if it converts to 'false' using the
4567 // standard ToBoolean() conversion as described in ECMA-262,
4568 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00004569 ToBoolean(&pop_and_continue, &exit);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004570 Branch(false, &exit);
4571
4572 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004573 pop_and_continue.Bind();
4574 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004575
4576 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004577 is_true.Bind();
4578 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004579
4580 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004581 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004582 } else if (has_cc() || is_true.is_linked()) {
4583 // The left-hand side is either (a) partially compiled to
4584 // control flow with a final branch left to emit or (b) fully
4585 // compiled to control flow and possibly true.
4586 if (has_cc()) {
4587 Branch(false, false_target());
4588 }
4589 is_true.Bind();
4590 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004591 true_target(),
4592 false_target(),
4593 false);
4594 } else {
4595 // Nothing to do.
4596 ASSERT(!has_valid_frame() && !has_cc() && !is_true.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004597 }
4598
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004599 } else {
4600 ASSERT(node->op() == Token::OR);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004601 JumpTarget is_false;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004602 LoadConditionAndSpill(node->left(),
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004603 true_target(),
4604 &is_false,
4605 false);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004606 if (has_valid_frame() && !has_cc()) {
4607 // The left-hand side result is on top of the virtual frame.
kasperl@chromium.org71affb52009-05-26 05:44:31 +00004608 JumpTarget pop_and_continue;
4609 JumpTarget exit;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004610
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004611 __ ldr(r0, frame_->Top());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004612 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004613 // Avoid popping the result if it converts to 'true' using the
4614 // standard ToBoolean() conversion as described in ECMA-262,
4615 // section 9.2, page 30.
mads.s.ager31e71382008-08-13 09:32:07 +00004616 ToBoolean(&exit, &pop_and_continue);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004617 Branch(true, &exit);
4618
4619 // Pop the result of evaluating the first part.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004620 pop_and_continue.Bind();
4621 frame_->EmitPop(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004622
4623 // Evaluate right side expression.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004624 is_false.Bind();
4625 LoadAndSpill(node->right());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004626
4627 // Exit (always with a materialized value).
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004628 exit.Bind();
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004629 } else if (has_cc() || is_false.is_linked()) {
4630 // The left-hand side is either (a) partially compiled to
4631 // control flow with a final branch left to emit or (b) fully
4632 // compiled to control flow and possibly false.
4633 if (has_cc()) {
4634 Branch(true, true_target());
4635 }
4636 is_false.Bind();
4637 LoadConditionAndSpill(node->right(),
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004638 true_target(),
4639 false_target(),
4640 false);
4641 } else {
4642 // Nothing to do.
4643 ASSERT(!has_valid_frame() && !has_cc() && !is_false.is_linked());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004644 }
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004645 }
4646}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004647
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004648
4649void CodeGenerator::VisitBinaryOperation(BinaryOperation* node) {
4650#ifdef DEBUG
4651 int original_height = frame_->height();
4652#endif
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004653 Comment cmnt(masm_, "[ BinaryOperation");
4654
4655 if (node->op() == Token::AND || node->op() == Token::OR) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004656 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00004657 GenerateLogicalBooleanOperation(node);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004658 } else {
4659 // Optimize for the case where (at least) one of the expressions
4660 // is a literal small integer.
4661 Literal* lliteral = node->left()->AsLiteral();
4662 Literal* rliteral = node->right()->AsLiteral();
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004663 // NOTE: The code below assumes that the slow cases (calls to runtime)
4664 // never return a constant/immutable object.
4665 bool overwrite_left =
4666 (node->left()->AsBinaryOperation() != NULL &&
4667 node->left()->AsBinaryOperation()->ResultOverwriteAllowed());
4668 bool overwrite_right =
4669 (node->right()->AsBinaryOperation() != NULL &&
4670 node->right()->AsBinaryOperation()->ResultOverwriteAllowed());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004671
4672 if (rliteral != NULL && rliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004673 VirtualFrame::RegisterAllocationScope scope(this);
4674 Load(node->left());
4675 VirtualFrameSmiOperation(
4676 node->op(),
4677 rliteral->handle(),
4678 false,
4679 overwrite_right ? OVERWRITE_RIGHT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004680 } else if (lliteral != NULL && lliteral->handle()->IsSmi()) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004681 VirtualFrame::RegisterAllocationScope scope(this);
4682 Load(node->right());
4683 VirtualFrameSmiOperation(node->op(),
4684 lliteral->handle(),
4685 true,
4686 overwrite_left ? OVERWRITE_LEFT : NO_OVERWRITE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004687 } else {
ager@chromium.org357bf652010-04-12 11:30:10 +00004688 VirtualFrame::RegisterAllocationScope scope(this);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004689 OverwriteMode overwrite_mode = NO_OVERWRITE;
4690 if (overwrite_left) {
4691 overwrite_mode = OVERWRITE_LEFT;
4692 } else if (overwrite_right) {
4693 overwrite_mode = OVERWRITE_RIGHT;
4694 }
ager@chromium.org357bf652010-04-12 11:30:10 +00004695 Load(node->left());
4696 Load(node->right());
4697 VirtualFrameBinaryOperation(node->op(), overwrite_mode);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004698 }
4699 }
kasperl@chromium.orge959c182009-07-27 08:59:04 +00004700 ASSERT(!has_valid_frame() ||
4701 (has_cc() && frame_->height() == original_height) ||
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004702 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004703}
4704
4705
ager@chromium.org7c537e22008-10-16 08:43:32 +00004706void CodeGenerator::VisitThisFunction(ThisFunction* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004707#ifdef DEBUG
4708 int original_height = frame_->height();
4709#endif
ager@chromium.org357bf652010-04-12 11:30:10 +00004710 VirtualFrame::SpilledScope spilled_scope(frame_);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004711 __ ldr(r0, frame_->Function());
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004712 frame_->EmitPush(r0);
4713 ASSERT(frame_->height() == original_height + 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004714}
4715
4716
ager@chromium.org7c537e22008-10-16 08:43:32 +00004717void CodeGenerator::VisitCompareOperation(CompareOperation* node) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004718#ifdef DEBUG
4719 int original_height = frame_->height();
4720#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004721 Comment cmnt(masm_, "[ CompareOperation");
4722
ager@chromium.org357bf652010-04-12 11:30:10 +00004723 VirtualFrame::RegisterAllocationScope nonspilled_scope(this);
4724
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004725 // Get the expressions from the node.
4726 Expression* left = node->left();
4727 Expression* right = node->right();
4728 Token::Value op = node->op();
4729
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004730 // To make null checks efficient, we check if either left or right is the
4731 // literal 'null'. If so, we optimize the code by inlining a null check
4732 // instead of calling the (very) general runtime routine for checking
4733 // equality.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004734 if (op == Token::EQ || op == Token::EQ_STRICT) {
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004735 bool left_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004736 left->AsLiteral() != NULL && left->AsLiteral()->IsNull();
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00004737 bool right_is_null =
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004738 right->AsLiteral() != NULL && right->AsLiteral()->IsNull();
4739 // The 'null' value can only be equal to 'null' or 'undefined'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004740 if (left_is_null || right_is_null) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004741 Load(left_is_null ? right : left);
4742 Register tos = frame_->PopToRegister();
4743 // JumpTargets can't cope with register allocation yet.
4744 frame_->SpillAll();
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004745 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004746 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004747
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004748 // The 'null' value is only equal to 'undefined' if using non-strict
4749 // comparisons.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004750 if (op != Token::EQ_STRICT) {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004751 true_target()->Branch(eq);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004752
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004753 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004754 __ cmp(tos, Operand(ip));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004755 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004756
ager@chromium.org357bf652010-04-12 11:30:10 +00004757 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004758 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004759
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004760 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00004761 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
4762 __ ldrb(tos, FieldMemOperand(tos, Map::kBitFieldOffset));
4763 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
4764 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004765 }
4766
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004767 cc_reg_ = eq;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004768 ASSERT(has_cc() && frame_->height() == original_height);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004769 return;
4770 }
4771 }
4772
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004773 // To make typeof testing for natives implemented in JavaScript really
4774 // efficient, we generate special code for expressions of the form:
4775 // 'typeof <expression> == <string>'.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004776 UnaryOperation* operation = left->AsUnaryOperation();
4777 if ((op == Token::EQ || op == Token::EQ_STRICT) &&
4778 (operation != NULL && operation->op() == Token::TYPEOF) &&
4779 (right->AsLiteral() != NULL &&
4780 right->AsLiteral()->handle()->IsString())) {
4781 Handle<String> check(String::cast(*right->AsLiteral()->handle()));
4782
ager@chromium.org357bf652010-04-12 11:30:10 +00004783 // Load the operand, move it to a register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004784 LoadTypeofExpression(operation->expression());
ager@chromium.org357bf652010-04-12 11:30:10 +00004785 Register tos = frame_->PopToRegister();
4786
4787 // JumpTargets can't cope with register allocation yet.
4788 frame_->SpillAll();
4789
4790 Register scratch = VirtualFrame::scratch0();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004791
4792 if (check->Equals(Heap::number_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004793 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004794 true_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00004795 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004796 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004797 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004798 cc_reg_ = eq;
4799
4800 } else if (check->Equals(Heap::string_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004801 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004802 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004803
ager@chromium.org357bf652010-04-12 11:30:10 +00004804 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004805
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004806 // It can be an undetectable string object.
ager@chromium.org357bf652010-04-12 11:30:10 +00004807 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
4808 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
4809 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004810 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004811
ager@chromium.org357bf652010-04-12 11:30:10 +00004812 __ ldrb(scratch, FieldMemOperand(tos, Map::kInstanceTypeOffset));
4813 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004814 cc_reg_ = lt;
4815
4816 } else if (check->Equals(Heap::boolean_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004817 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004818 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004819 true_target()->Branch(eq);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004820 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004821 __ cmp(tos, ip);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004822 cc_reg_ = eq;
4823
4824 } else if (check->Equals(Heap::undefined_symbol())) {
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004825 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004826 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004827 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004828
ager@chromium.org357bf652010-04-12 11:30:10 +00004829 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004830 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004831
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004832 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00004833 __ ldr(tos, FieldMemOperand(tos, HeapObject::kMapOffset));
4834 __ ldrb(scratch, FieldMemOperand(tos, Map::kBitFieldOffset));
4835 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
4836 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004837
4838 cc_reg_ = eq;
4839
4840 } else if (check->Equals(Heap::function_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004841 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004842 false_target()->Branch(eq);
ager@chromium.org357bf652010-04-12 11:30:10 +00004843 Register map_reg = scratch;
4844 __ CompareObjectType(tos, map_reg, tos, JS_FUNCTION_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004845 true_target()->Branch(eq);
4846 // Regular expressions are callable so typeof == 'function'.
ager@chromium.org357bf652010-04-12 11:30:10 +00004847 __ CompareInstanceType(map_reg, tos, JS_REGEXP_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004848 cc_reg_ = eq;
4849
4850 } else if (check->Equals(Heap::object_symbol())) {
ager@chromium.org357bf652010-04-12 11:30:10 +00004851 __ tst(tos, Operand(kSmiTagMask));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004852 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004853
ager@chromium.orgab99eea2009-08-25 07:05:41 +00004854 __ LoadRoot(ip, Heap::kNullValueRootIndex);
ager@chromium.org357bf652010-04-12 11:30:10 +00004855 __ cmp(tos, ip);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004856 true_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004857
ager@chromium.org357bf652010-04-12 11:30:10 +00004858 Register map_reg = scratch;
4859 __ CompareObjectType(tos, map_reg, tos, JS_REGEXP_TYPE);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004860 false_target()->Branch(eq);
4861
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004862 // It can be an undetectable object.
ager@chromium.org357bf652010-04-12 11:30:10 +00004863 __ ldrb(tos, FieldMemOperand(map_reg, Map::kBitFieldOffset));
4864 __ and_(tos, tos, Operand(1 << Map::kIsUndetectable));
4865 __ cmp(tos, Operand(1 << Map::kIsUndetectable));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004866 false_target()->Branch(eq);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004867
ager@chromium.org357bf652010-04-12 11:30:10 +00004868 __ ldrb(tos, FieldMemOperand(map_reg, Map::kInstanceTypeOffset));
4869 __ cmp(tos, Operand(FIRST_JS_OBJECT_TYPE));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004870 false_target()->Branch(lt);
ager@chromium.org357bf652010-04-12 11:30:10 +00004871 __ cmp(tos, Operand(LAST_JS_OBJECT_TYPE));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004872 cc_reg_ = le;
4873
4874 } else {
ager@chromium.org3bf7b912008-11-17 09:09:45 +00004875 // Uncommon case: typeof testing against a string literal that is
4876 // never returned from the typeof operator.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004877 false_target()->Jump();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004878 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004879 ASSERT(!has_valid_frame() ||
4880 (has_cc() && frame_->height() == original_height));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004881 return;
4882 }
4883
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004884 switch (op) {
4885 case Token::EQ:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004886 Comparison(eq, left, right, false);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004887 break;
4888
4889 case Token::LT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004890 Comparison(lt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004891 break;
4892
4893 case Token::GT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004894 Comparison(gt, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004895 break;
4896
4897 case Token::LTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004898 Comparison(le, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004899 break;
4900
4901 case Token::GTE:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004902 Comparison(ge, left, right);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004903 break;
4904
4905 case Token::EQ_STRICT:
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004906 Comparison(eq, left, right, true);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004907 break;
4908
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004909 case Token::IN: {
ager@chromium.org357bf652010-04-12 11:30:10 +00004910 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004911 LoadAndSpill(left);
4912 LoadAndSpill(right);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00004913 frame_->InvokeBuiltin(Builtins::IN, CALL_JS, 2);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004914 frame_->EmitPush(r0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004915 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004916 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004917
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004918 case Token::INSTANCEOF: {
ager@chromium.org357bf652010-04-12 11:30:10 +00004919 VirtualFrame::SpilledScope scope(frame_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00004920 LoadAndSpill(left);
4921 LoadAndSpill(right);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004922 InstanceofStub stub;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004923 frame_->CallStub(&stub, 2);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00004924 // At this point if instanceof succeeded then r0 == 0.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00004925 __ tst(r0, Operand(r0));
ager@chromium.org7c537e22008-10-16 08:43:32 +00004926 cc_reg_ = eq;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004927 break;
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004928 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004929
4930 default:
4931 UNREACHABLE();
4932 }
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004933 ASSERT((has_cc() && frame_->height() == original_height) ||
4934 (!has_cc() && frame_->height() == original_height + 1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004935}
4936
4937
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00004938void CodeGenerator::EmitKeyedLoad(bool is_global) {
4939 Comment cmnt(masm_, "[ Load from keyed Property");
4940 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
4941 RelocInfo::Mode rmode = is_global
4942 ? RelocInfo::CODE_TARGET_CONTEXT
4943 : RelocInfo::CODE_TARGET;
4944 frame_->CallCodeObject(ic, rmode, 0);
4945}
4946
4947
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004948#ifdef DEBUG
4949bool CodeGenerator::HasValidEntryRegisters() { return true; }
4950#endif
4951
4952
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004953#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +00004954#define __ ACCESS_MASM(masm)
4955
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00004956
ager@chromium.org7c537e22008-10-16 08:43:32 +00004957Handle<String> Reference::GetName() {
4958 ASSERT(type_ == NAMED);
4959 Property* property = expression_->AsProperty();
4960 if (property == NULL) {
4961 // Global variable reference treated as a named property reference.
4962 VariableProxy* proxy = expression_->AsVariableProxy();
4963 ASSERT(proxy->AsVariable() != NULL);
4964 ASSERT(proxy->AsVariable()->is_global());
4965 return proxy->name();
4966 } else {
4967 Literal* raw_name = property->key()->AsLiteral();
4968 ASSERT(raw_name != NULL);
4969 return Handle<String>(String::cast(*raw_name->handle()));
4970 }
4971}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004972
ager@chromium.org7c537e22008-10-16 08:43:32 +00004973
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004974void Reference::GetValue() {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004975 ASSERT(cgen_->HasValidEntryRegisters());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004976 ASSERT(!is_illegal());
4977 ASSERT(!cgen_->has_cc());
4978 MacroAssembler* masm = cgen_->masm();
4979 Property* property = expression_->AsProperty();
4980 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00004981 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004982 }
4983
4984 switch (type_) {
4985 case SLOT: {
4986 Comment cmnt(masm, "[ Load from Slot");
4987 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
4988 ASSERT(slot != NULL);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00004989 cgen_->LoadFromSlot(slot, NOT_INSIDE_TYPEOF);
ager@chromium.org7c537e22008-10-16 08:43:32 +00004990 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00004991 }
4992
ager@chromium.org7c537e22008-10-16 08:43:32 +00004993 case NAMED: {
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004994 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00004995 Comment cmnt(masm, "[ Load from named Property");
ager@chromium.org7c537e22008-10-16 08:43:32 +00004996 Handle<String> name(GetName());
ager@chromium.org7c537e22008-10-16 08:43:32 +00004997 Variable* var = expression_->AsVariableProxy()->AsVariable();
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00004998 Handle<Code> ic(Builtins::builtin(Builtins::LoadIC_Initialize));
4999 // Setup the name register.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005000 __ mov(r2, Operand(name));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005001 ASSERT(var == NULL || var->is_global());
5002 RelocInfo::Mode rmode = (var == NULL)
5003 ? RelocInfo::CODE_TARGET
5004 : RelocInfo::CODE_TARGET_CONTEXT;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005005 frame->CallCodeObject(ic, rmode, 0);
5006 frame->EmitPush(r0);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005007 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005008 }
5009
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005010 case KEYED: {
5011 // TODO(181): Implement inlined version of array indexing once
5012 // loop nesting is properly tracked on ARM.
5013 ASSERT(property != NULL);
5014 Variable* var = expression_->AsVariableProxy()->AsVariable();
5015 ASSERT(var == NULL || var->is_global());
5016 cgen_->EmitKeyedLoad(var != NULL);
5017 cgen_->frame()->EmitPush(r0);
5018 break;
5019 }
5020
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005021 default:
5022 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005023 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005024
5025 if (!persist_after_get_) {
5026 cgen_->UnloadReference(this);
5027 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005028}
5029
5030
ager@chromium.org7c537e22008-10-16 08:43:32 +00005031void Reference::SetValue(InitState init_state) {
5032 ASSERT(!is_illegal());
5033 ASSERT(!cgen_->has_cc());
5034 MacroAssembler* masm = cgen_->masm();
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005035 VirtualFrame* frame = cgen_->frame();
ager@chromium.org7c537e22008-10-16 08:43:32 +00005036 Property* property = expression_->AsProperty();
5037 if (property != NULL) {
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00005038 cgen_->CodeForSourcePosition(property->position());
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005039 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005040
ager@chromium.org7c537e22008-10-16 08:43:32 +00005041 switch (type_) {
5042 case SLOT: {
5043 Comment cmnt(masm, "[ Store to Slot");
5044 Slot* slot = expression_->AsVariableProxy()->AsVariable()->slot();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005045 cgen_->StoreToSlot(slot, init_state);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005046 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005047 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005048 }
5049
ager@chromium.org7c537e22008-10-16 08:43:32 +00005050 case NAMED: {
ager@chromium.org357bf652010-04-12 11:30:10 +00005051 VirtualFrame::SpilledScope scope(frame);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005052 Comment cmnt(masm, "[ Store to named Property");
5053 // Call the appropriate IC code.
ager@chromium.org7c537e22008-10-16 08:43:32 +00005054 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Initialize));
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00005055 Handle<String> name(GetName());
5056
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005057 frame->EmitPop(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00005058 frame->EmitPop(r1);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005059 __ mov(r2, Operand(name));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005060 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005061 frame->EmitPush(r0);
ager@chromium.org5c838252010-02-19 08:53:10 +00005062 set_unloaded();
ager@chromium.org7c537e22008-10-16 08:43:32 +00005063 break;
5064 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005065
ager@chromium.org7c537e22008-10-16 08:43:32 +00005066 case KEYED: {
ager@chromium.org357bf652010-04-12 11:30:10 +00005067 VirtualFrame::SpilledScope scope(frame);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005068 Comment cmnt(masm, "[ Store to keyed Property");
5069 Property* property = expression_->AsProperty();
5070 ASSERT(property != NULL);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +00005071 cgen_->CodeForSourcePosition(property->position());
ager@chromium.org3bf7b912008-11-17 09:09:45 +00005072
5073 // Call IC code.
5074 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize));
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005075 frame->EmitPop(r0); // value
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005076 frame->CallCodeObject(ic, RelocInfo::CODE_TARGET, 0);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00005077 frame->EmitPush(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005078 cgen_->UnloadReference(this);
ager@chromium.org7c537e22008-10-16 08:43:32 +00005079 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005080 }
ager@chromium.org7c537e22008-10-16 08:43:32 +00005081
5082 default:
5083 UNREACHABLE();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00005084 }
5085}
5086
5087
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005088void FastNewClosureStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005089 // Create a new closure from the given function info in new
5090 // space. Set the context to the current context in cp.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005091 Label gc;
5092
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005093 // Pop the function info from the stack.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005094 __ pop(r3);
5095
5096 // Attempt to allocate new JSFunction in new space.
5097 __ AllocateInNewSpace(JSFunction::kSize / kPointerSize,
5098 r0,
5099 r1,
5100 r2,
5101 &gc,
5102 TAG_OBJECT);
5103
5104 // Compute the function map in the current global context and set that
5105 // as the map of the allocated object.
5106 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
5107 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
5108 __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
5109 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5110
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005111 // Initialize the rest of the function. We don't have to update the
5112 // write barrier because the allocated object is in new space.
5113 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
5114 __ LoadRoot(r2, Heap::kTheHoleValueRootIndex);
5115 __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset));
5116 __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset));
5117 __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset));
5118 __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
5119 __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset));
5120 __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005121
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005122 // Return result. The argument function info has been popped already.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005123 __ Ret();
5124
5125 // Create a new closure through the slower runtime call.
5126 __ bind(&gc);
5127 __ push(cp);
5128 __ push(r3);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005129 __ TailCallRuntime(Runtime::kNewClosure, 2, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005130}
5131
5132
5133void FastNewContextStub::Generate(MacroAssembler* masm) {
5134 // Try to allocate the context in new space.
5135 Label gc;
5136 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
5137
5138 // Attempt to allocate the context in new space.
5139 __ AllocateInNewSpace(length + (FixedArray::kHeaderSize / kPointerSize),
5140 r0,
5141 r1,
5142 r2,
5143 &gc,
5144 TAG_OBJECT);
5145
5146 // Load the function from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00005147 __ ldr(r3, MemOperand(sp, 0));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005148
5149 // Setup the object header.
5150 __ LoadRoot(r2, Heap::kContextMapRootIndex);
5151 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5152 __ mov(r2, Operand(length));
5153 __ str(r2, FieldMemOperand(r0, Array::kLengthOffset));
5154
5155 // Setup the fixed slots.
5156 __ mov(r1, Operand(Smi::FromInt(0)));
5157 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
5158 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
5159 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
5160 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
5161
5162 // Copy the global object from the surrounding context.
5163 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
5164 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
5165
5166 // Initialize the rest of the slots to undefined.
5167 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
5168 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
5169 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
5170 }
5171
5172 // Remove the on-stack argument and return.
5173 __ mov(cp, r0);
5174 __ pop();
5175 __ Ret();
5176
5177 // Need to collect. Call into runtime system.
5178 __ bind(&gc);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005179 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005180}
5181
5182
ager@chromium.org5c838252010-02-19 08:53:10 +00005183void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
5184 // Stack layout on entry:
5185 //
5186 // [sp]: constant elements.
5187 // [sp + kPointerSize]: literal index.
5188 // [sp + (2 * kPointerSize)]: literals array.
5189
5190 // All sizes here are multiples of kPointerSize.
5191 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
5192 int size = JSArray::kSize + elements_size;
5193
5194 // Load boilerplate object into r3 and check if we need to create a
5195 // boilerplate.
5196 Label slow_case;
5197 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
5198 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
5199 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5200 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
5201 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
5202 __ cmp(r3, ip);
5203 __ b(eq, &slow_case);
5204
5205 // Allocate both the JS array and the elements array in one big
5206 // allocation. This avoids multiple limit checks.
5207 __ AllocateInNewSpace(size / kPointerSize,
5208 r0,
5209 r1,
5210 r2,
5211 &slow_case,
5212 TAG_OBJECT);
5213
5214 // Copy the JS array part.
5215 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
5216 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
5217 __ ldr(r1, FieldMemOperand(r3, i));
5218 __ str(r1, FieldMemOperand(r0, i));
5219 }
5220 }
5221
5222 if (length_ > 0) {
5223 // Get hold of the elements array of the boilerplate and setup the
5224 // elements pointer in the resulting object.
5225 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
5226 __ add(r2, r0, Operand(JSArray::kSize));
5227 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
5228
5229 // Copy the elements array.
5230 for (int i = 0; i < elements_size; i += kPointerSize) {
5231 __ ldr(r1, FieldMemOperand(r3, i));
5232 __ str(r1, FieldMemOperand(r2, i));
5233 }
5234 }
5235
5236 // Return and remove the on-stack parameters.
5237 __ add(sp, sp, Operand(3 * kPointerSize));
5238 __ Ret();
5239
5240 __ bind(&slow_case);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00005241 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00005242}
5243
5244
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005245// Takes a Smi and converts to an IEEE 64 bit floating point value in two
5246// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
5247// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
5248// scratch register. Destroys the source register. No GC occurs during this
5249// stub so you don't have to set up the frame.
5250class ConvertToDoubleStub : public CodeStub {
5251 public:
5252 ConvertToDoubleStub(Register result_reg_1,
5253 Register result_reg_2,
5254 Register source_reg,
5255 Register scratch_reg)
5256 : result1_(result_reg_1),
5257 result2_(result_reg_2),
5258 source_(source_reg),
5259 zeros_(scratch_reg) { }
5260
5261 private:
5262 Register result1_;
5263 Register result2_;
5264 Register source_;
5265 Register zeros_;
5266
5267 // Minor key encoding in 16 bits.
5268 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
5269 class OpBits: public BitField<Token::Value, 2, 14> {};
5270
5271 Major MajorKey() { return ConvertToDouble; }
5272 int MinorKey() {
5273 // Encode the parameters in a unique 16 bit value.
5274 return result1_.code() +
5275 (result2_.code() << 4) +
5276 (source_.code() << 8) +
5277 (zeros_.code() << 12);
5278 }
5279
5280 void Generate(MacroAssembler* masm);
5281
5282 const char* GetName() { return "ConvertToDoubleStub"; }
5283
5284#ifdef DEBUG
5285 void Print() { PrintF("ConvertToDoubleStub\n"); }
5286#endif
5287};
5288
5289
5290void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
5291#ifndef BIG_ENDIAN_FLOATING_POINT
5292 Register exponent = result1_;
5293 Register mantissa = result2_;
5294#else
5295 Register exponent = result2_;
5296 Register mantissa = result1_;
5297#endif
5298 Label not_special;
5299 // Convert from Smi to integer.
5300 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
5301 // Move sign bit from source to destination. This works because the sign bit
5302 // in the exponent word of the double has the same position and polarity as
5303 // the 2's complement sign bit in a Smi.
5304 ASSERT(HeapNumber::kSignMask == 0x80000000u);
5305 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
5306 // Subtract from 0 if source was negative.
5307 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005308
5309 // We have -1, 0 or 1, which we treat specially. Register source_ contains
5310 // absolute value: it is either equal to 1 (special case of -1 and 1),
5311 // greater than 1 (not a special case) or less than 1 (special case of 0).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005312 __ cmp(source_, Operand(1));
5313 __ b(gt, &not_special);
5314
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005315 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
5316 static const uint32_t exponent_word_for_1 =
5317 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005318 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005319 // 1, 0 and -1 all have 0 for the second word.
5320 __ mov(mantissa, Operand(0));
5321 __ Ret();
5322
5323 __ bind(&not_special);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005324 // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005325 // Gets the wrong answer for 0, but we already checked for that case above.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005326 __ CountLeadingZeros(source_, mantissa, zeros_);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005327 // Compute exponent and or it into the exponent register.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005328 // We use mantissa as a scratch register here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005329 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias));
5330 __ orr(exponent,
5331 exponent,
5332 Operand(mantissa, LSL, HeapNumber::kExponentShift));
5333 // Shift up the source chopping the top bit off.
5334 __ add(zeros_, zeros_, Operand(1));
5335 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
5336 __ mov(source_, Operand(source_, LSL, zeros_));
5337 // Compute lower part of fraction (last 12 bits).
5338 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
5339 // And the top (top 20 bits).
5340 __ orr(exponent,
5341 exponent,
5342 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
5343 __ Ret();
5344}
5345
5346
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005347// See comment for class.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005348void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005349 Label max_negative_int;
5350 // the_int_ has the answer which is a signed int32 but not a Smi.
5351 // We test for the special value that has a different exponent. This test
5352 // has the neat side effect of setting the flags according to the sign.
5353 ASSERT(HeapNumber::kSignMask == 0x80000000u);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00005354 __ cmp(the_int_, Operand(0x80000000u));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005355 __ b(eq, &max_negative_int);
5356 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
5357 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
5358 uint32_t non_smi_exponent =
5359 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
5360 __ mov(scratch_, Operand(non_smi_exponent));
5361 // Set the sign bit in scratch_ if the value was negative.
5362 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
5363 // Subtract from 0 if the value was negative.
5364 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
5365 // We should be masking the implict first digit of the mantissa away here,
5366 // but it just ends up combining harmlessly with the last digit of the
5367 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
5368 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
5369 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
5370 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
5371 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
5372 __ str(scratch_, FieldMemOperand(the_heap_number_,
5373 HeapNumber::kExponentOffset));
5374 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
5375 __ str(scratch_, FieldMemOperand(the_heap_number_,
5376 HeapNumber::kMantissaOffset));
5377 __ Ret();
5378
5379 __ bind(&max_negative_int);
5380 // The max negative int32 is stored as a positive number in the mantissa of
5381 // a double because it uses a sign bit instead of using two's complement.
5382 // The actual mantissa bits stored are all 0 because the implicit most
5383 // significant 1 bit is not stored.
5384 non_smi_exponent += 1 << HeapNumber::kExponentShift;
5385 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
5386 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
5387 __ mov(ip, Operand(0));
5388 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
5389 __ Ret();
5390}
5391
5392
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005393// Handle the case where the lhs and rhs are the same object.
5394// Equality is almost reflexive (everything but NaN), so this is a test
5395// for "identity and not NaN".
5396static void EmitIdenticalObjectComparison(MacroAssembler* masm,
5397 Label* slow,
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005398 Condition cc,
5399 bool never_nan_nan) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005400 Label not_identical;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005401 Label heap_number, return_equal;
5402 Register exp_mask_reg = r5;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005403 __ cmp(r0, Operand(r1));
5404 __ b(ne, &not_identical);
5405
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005406 // The two objects are identical. If we know that one of them isn't NaN then
5407 // we now know they test equal.
5408 if (cc != eq || !never_nan_nan) {
5409 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005410
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005411 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
5412 // so we do the second best thing - test it ourselves.
5413 // They are both equal and they are not both Smis so both of them are not
5414 // Smis. If it's not a heap number, then return equal.
5415 if (cc == lt || cc == gt) {
5416 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005417 __ b(ge, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005418 } else {
5419 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5420 __ b(eq, &heap_number);
5421 // Comparing JS objects with <=, >= is complicated.
5422 if (cc != eq) {
5423 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
5424 __ b(ge, slow);
5425 // Normally here we fall through to return_equal, but undefined is
5426 // special: (undefined == undefined) == true, but
5427 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
5428 if (cc == le || cc == ge) {
5429 __ cmp(r4, Operand(ODDBALL_TYPE));
5430 __ b(ne, &return_equal);
5431 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
5432 __ cmp(r0, Operand(r2));
5433 __ b(ne, &return_equal);
5434 if (cc == le) {
5435 // undefined <= undefined should fail.
5436 __ mov(r0, Operand(GREATER));
5437 } else {
5438 // undefined >= undefined should fail.
5439 __ mov(r0, Operand(LESS));
5440 }
5441 __ mov(pc, Operand(lr)); // Return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005442 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005443 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005444 }
5445 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005446
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005447 __ bind(&return_equal);
5448 if (cc == lt) {
5449 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
5450 } else if (cc == gt) {
5451 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
5452 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005453 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005454 }
5455 __ mov(pc, Operand(lr)); // Return.
5456
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005457 if (cc != eq || !never_nan_nan) {
5458 // For less and greater we don't have to check for NaN since the result of
5459 // x < x is false regardless. For the others here is some code to check
5460 // for NaN.
5461 if (cc != lt && cc != gt) {
5462 __ bind(&heap_number);
5463 // It is a heap number, so return non-equal if it's NaN and equal if it's
5464 // not NaN.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005465
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005466 // The representation of NaN values has all exponent bits (52..62) set,
5467 // and not all mantissa bits (0..51) clear.
5468 // Read top bits of double representation (second word of value).
5469 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
5470 // Test that exponent bits are all set.
5471 __ and_(r3, r2, Operand(exp_mask_reg));
5472 __ cmp(r3, Operand(exp_mask_reg));
5473 __ b(ne, &return_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005474
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005475 // Shift out flag and all exponent bits, retaining only mantissa.
5476 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
5477 // Or with all low-bits of mantissa.
5478 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
5479 __ orr(r0, r3, Operand(r2), SetCC);
5480 // For equal we already have the right value in r0: Return zero (equal)
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005481 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
5482 // not (it's a NaN). For <= and >= we need to load r0 with the failing
5483 // value if it's a NaN.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005484 if (cc != eq) {
5485 // All-zero means Infinity means equal.
5486 __ mov(pc, Operand(lr), LeaveCC, eq); // Return equal
5487 if (cc == le) {
5488 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
5489 } else {
5490 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
5491 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005492 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005493 __ mov(pc, Operand(lr)); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005494 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005495 // No fall through here.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005496 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005497
5498 __ bind(&not_identical);
5499}
5500
5501
5502// See comment at call site.
5503static void EmitSmiNonsmiComparison(MacroAssembler* masm,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005504 Label* lhs_not_nan,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005505 Label* slow,
5506 bool strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005507 Label rhs_is_smi;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005508 __ tst(r0, Operand(kSmiTagMask));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005509 __ b(eq, &rhs_is_smi);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005510
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005511 // Lhs is a Smi. Check whether the rhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005512 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
5513 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005514 // If rhs is not a number and lhs is a Smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005515 // succeed. Return non-equal (r0 is already not zero)
5516 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
5517 } else {
5518 // Smi compared non-strictly with a non-Smi non-heap-number. Call
5519 // the runtime.
5520 __ b(ne, slow);
5521 }
5522
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005523 // Lhs (r1) is a smi, rhs (r0) is a number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005524 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005525 // Convert lhs to a double in d7 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005526 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005527 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
5528 __ vmov(s15, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005529 __ vcvt_f64_s32(d7, s15);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005530 // Load the double from rhs, tagged HeapNumber r0, to d6.
5531 __ sub(r7, r0, Operand(kHeapObjectTag));
5532 __ vldr(d6, r7, HeapNumber::kValueOffset);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005533 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005534 __ push(lr);
5535 // Convert lhs to a double in r2, r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005536 __ mov(r7, Operand(r1));
5537 ConvertToDoubleStub stub1(r3, r2, r7, r6);
5538 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005539 // Load rhs to a double in r0, r1.
5540 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
5541 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
5542 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005543 }
5544
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005545 // We now have both loaded as doubles but we can skip the lhs nan check
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005546 // since it's a smi.
5547 __ jmp(lhs_not_nan);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005548
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005549 __ bind(&rhs_is_smi);
5550 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005551 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
5552 if (strict) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005553 // If lhs is not a number and rhs is a smi then strict equality cannot
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005554 // succeed. Return non-equal.
5555 __ mov(r0, Operand(1), LeaveCC, ne); // Non-zero indicates not equal.
5556 __ mov(pc, Operand(lr), LeaveCC, ne); // Return.
5557 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005558 // Smi compared non-strictly with a non-smi non-heap-number. Call
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005559 // the runtime.
5560 __ b(ne, slow);
5561 }
5562
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005563 // Rhs (r0) is a smi, lhs (r1) is a heap number.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005564 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005565 // Convert rhs to a double in d6 .
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005566 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005567 // Load the double from lhs, tagged HeapNumber r1, to d7.
5568 __ sub(r7, r1, Operand(kHeapObjectTag));
5569 __ vldr(d7, r7, HeapNumber::kValueOffset);
5570 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
5571 __ vmov(s13, r7);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00005572 __ vcvt_f64_s32(d6, s13);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005573 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005574 __ push(lr);
5575 // Load lhs to a double in r2, r3.
5576 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
5577 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
5578 // Convert rhs to a double in r0, r1.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005579 __ mov(r7, Operand(r0));
5580 ConvertToDoubleStub stub2(r1, r0, r7, r6);
5581 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005582 __ pop(lr);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005583 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005584 // Fall through to both_loaded_as_doubles.
5585}
5586
5587
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005588void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005589 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005590 Register rhs_exponent = exp_first ? r0 : r1;
5591 Register lhs_exponent = exp_first ? r2 : r3;
5592 Register rhs_mantissa = exp_first ? r1 : r0;
5593 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005594 Label one_is_nan, neither_is_nan;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005595 Label lhs_not_nan_exp_mask_is_loaded;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005596
5597 Register exp_mask_reg = r5;
5598
5599 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005600 __ and_(r4, lhs_exponent, Operand(exp_mask_reg));
5601 __ cmp(r4, Operand(exp_mask_reg));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005602 __ b(ne, &lhs_not_nan_exp_mask_is_loaded);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005603 __ mov(r4,
5604 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
5605 SetCC);
5606 __ b(ne, &one_is_nan);
5607 __ cmp(lhs_mantissa, Operand(0));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005608 __ b(ne, &one_is_nan);
5609
5610 __ bind(lhs_not_nan);
5611 __ mov(exp_mask_reg, Operand(HeapNumber::kExponentMask));
5612 __ bind(&lhs_not_nan_exp_mask_is_loaded);
5613 __ and_(r4, rhs_exponent, Operand(exp_mask_reg));
5614 __ cmp(r4, Operand(exp_mask_reg));
5615 __ b(ne, &neither_is_nan);
5616 __ mov(r4,
5617 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
5618 SetCC);
5619 __ b(ne, &one_is_nan);
5620 __ cmp(rhs_mantissa, Operand(0));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005621 __ b(eq, &neither_is_nan);
5622
5623 __ bind(&one_is_nan);
5624 // NaN comparisons always fail.
5625 // Load whatever we need in r0 to make the comparison fail.
5626 if (cc == lt || cc == le) {
5627 __ mov(r0, Operand(GREATER));
5628 } else {
5629 __ mov(r0, Operand(LESS));
5630 }
5631 __ mov(pc, Operand(lr)); // Return.
5632
5633 __ bind(&neither_is_nan);
5634}
5635
5636
5637// See comment at call site.
5638static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
5639 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005640 Register rhs_exponent = exp_first ? r0 : r1;
5641 Register lhs_exponent = exp_first ? r2 : r3;
5642 Register rhs_mantissa = exp_first ? r1 : r0;
5643 Register lhs_mantissa = exp_first ? r3 : r2;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005644
5645 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
5646 if (cc == eq) {
5647 // Doubles are not equal unless they have the same bit pattern.
5648 // Exception: 0 and -0.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005649 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
5650 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005651 // Return non-zero if the numbers are unequal.
5652 __ mov(pc, Operand(lr), LeaveCC, ne);
5653
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005654 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005655 // If exponents are equal then return 0.
5656 __ mov(pc, Operand(lr), LeaveCC, eq);
5657
5658 // Exponents are unequal. The only way we can return that the numbers
5659 // are equal is if one is -0 and the other is 0. We already dealt
5660 // with the case where both are -0 or both are 0.
5661 // We start by seeing if the mantissas (that are equal) or the bottom
5662 // 31 bits of the rhs exponent are non-zero. If so we return not
5663 // equal.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005664 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005665 __ mov(r0, Operand(r4), LeaveCC, ne);
5666 __ mov(pc, Operand(lr), LeaveCC, ne); // Return conditionally.
5667 // Now they are equal if and only if the lhs exponent is zero in its
5668 // low 31 bits.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005669 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005670 __ mov(pc, Operand(lr));
5671 } else {
5672 // Call a native function to do a comparison between two non-NaNs.
5673 // Call C routine that may not cause GC or other trouble.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005674 __ push(lr);
5675 __ PrepareCallCFunction(4, r5); // Two doubles count as 4 arguments.
5676 __ CallCFunction(ExternalReference::compare_doubles(), 4);
5677 __ pop(pc); // Return.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005678 }
5679}
5680
5681
5682// See comment at call site.
5683static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm) {
5684 // If either operand is a JSObject or an oddball value, then they are
5685 // not equal since their pointers are different.
5686 // There is no test for undetectability in strict equality.
5687 ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
5688 Label first_non_object;
5689 // Get the type of the first operand into r2 and compare it with
5690 // FIRST_JS_OBJECT_TYPE.
5691 __ CompareObjectType(r0, r2, r2, FIRST_JS_OBJECT_TYPE);
5692 __ b(lt, &first_non_object);
5693
5694 // Return non-zero (r0 is not zero)
5695 Label return_not_equal;
5696 __ bind(&return_not_equal);
5697 __ mov(pc, Operand(lr)); // Return.
5698
5699 __ bind(&first_non_object);
5700 // Check for oddballs: true, false, null, undefined.
5701 __ cmp(r2, Operand(ODDBALL_TYPE));
5702 __ b(eq, &return_not_equal);
5703
5704 __ CompareObjectType(r1, r3, r3, FIRST_JS_OBJECT_TYPE);
5705 __ b(ge, &return_not_equal);
5706
5707 // Check for oddballs: true, false, null, undefined.
5708 __ cmp(r3, Operand(ODDBALL_TYPE));
5709 __ b(eq, &return_not_equal);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005710
5711 // Now that we have the types we might as well check for symbol-symbol.
5712 // Ensure that no non-strings have the symbol bit set.
5713 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
5714 ASSERT(kSymbolTag != 0);
5715 __ and_(r2, r2, Operand(r3));
5716 __ tst(r2, Operand(kIsSymbolMask));
5717 __ b(ne, &return_not_equal);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005718}
5719
5720
5721// See comment at call site.
5722static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
5723 Label* both_loaded_as_doubles,
5724 Label* not_heap_numbers,
5725 Label* slow) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005726 __ CompareObjectType(r0, r3, r2, HEAP_NUMBER_TYPE);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005727 __ b(ne, not_heap_numbers);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005728 __ ldr(r2, FieldMemOperand(r1, HeapObject::kMapOffset));
5729 __ cmp(r2, r3);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005730 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
5731
5732 // Both are heap numbers. Load them up then jump to the code we have
5733 // for that.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005734 if (CpuFeatures::IsSupported(VFP3)) {
5735 CpuFeatures::Scope scope(VFP3);
5736 __ sub(r7, r0, Operand(kHeapObjectTag));
5737 __ vldr(d6, r7, HeapNumber::kValueOffset);
5738 __ sub(r7, r1, Operand(kHeapObjectTag));
5739 __ vldr(d7, r7, HeapNumber::kValueOffset);
5740 } else {
5741 __ ldr(r2, FieldMemOperand(r1, HeapNumber::kValueOffset));
5742 __ ldr(r3, FieldMemOperand(r1, HeapNumber::kValueOffset + kPointerSize));
5743 __ ldr(r1, FieldMemOperand(r0, HeapNumber::kValueOffset + kPointerSize));
5744 __ ldr(r0, FieldMemOperand(r0, HeapNumber::kValueOffset));
5745 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005746 __ jmp(both_loaded_as_doubles);
5747}
5748
5749
5750// Fast negative check for symbol-to-symbol equality.
5751static void EmitCheckForSymbols(MacroAssembler* masm, Label* slow) {
5752 // r2 is object type of r0.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005753 // Ensure that no non-strings have the symbol bit set.
5754 ASSERT(kNotStringTag + kIsSymbolMask > LAST_TYPE);
5755 ASSERT(kSymbolTag != 0);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005756 __ tst(r2, Operand(kIsSymbolMask));
5757 __ b(eq, slow);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005758 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
5759 __ ldrb(r3, FieldMemOperand(r3, Map::kInstanceTypeOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005760 __ tst(r3, Operand(kIsSymbolMask));
5761 __ b(eq, slow);
5762
5763 // Both are symbols. We already checked they weren't the same pointer
5764 // so they are not equal.
5765 __ mov(r0, Operand(1)); // Non-zero indicates not equal.
5766 __ mov(pc, Operand(lr)); // Return.
5767}
5768
5769
fschneider@chromium.org086aac62010-03-17 13:18:24 +00005770void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
5771 Register object,
5772 Register result,
5773 Register scratch1,
5774 Register scratch2,
5775 bool object_is_smi,
5776 Label* not_found) {
5777 // Currently only lookup for smis. Check for smi if object is not known to be
5778 // a smi.
5779 if (!object_is_smi) {
5780 ASSERT(kSmiTag == 0);
5781 __ tst(object, Operand(kSmiTagMask));
5782 __ b(ne, not_found);
5783 }
5784
5785 // Use of registers. Register result is used as a temporary.
5786 Register number_string_cache = result;
5787 Register mask = scratch1;
5788 Register scratch = scratch2;
5789
5790 // Load the number string cache.
5791 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
5792
5793 // Make the hash mask from the length of the number string cache. It
5794 // contains two elements (number and string) for each cache entry.
5795 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
5796 // Divide length by two (length is not a smi).
5797 __ mov(mask, Operand(mask, ASR, 1));
5798 __ sub(mask, mask, Operand(1)); // Make mask.
5799
5800 // Calculate the entry in the number string cache. The hash value in the
5801 // number string cache for smis is just the smi value.
5802 __ and_(scratch, mask, Operand(object, ASR, 1));
5803
5804 // Calculate address of entry in string cache: each entry consists
5805 // of two pointer sized fields.
5806 __ add(scratch,
5807 number_string_cache,
5808 Operand(scratch, LSL, kPointerSizeLog2 + 1));
5809
5810 // Check if the entry is the smi we are looking for.
5811 Register object1 = scratch1;
5812 __ ldr(object1, FieldMemOperand(scratch, FixedArray::kHeaderSize));
5813 __ cmp(object, object1);
5814 __ b(ne, not_found);
5815
5816 // Get the result from the cache.
5817 __ ldr(result,
5818 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
5819
5820 __ IncrementCounter(&Counters::number_to_string_native,
5821 1,
5822 scratch1,
5823 scratch2);
5824}
5825
5826
5827void NumberToStringStub::Generate(MacroAssembler* masm) {
5828 Label runtime;
5829
5830 __ ldr(r1, MemOperand(sp, 0));
5831
5832 // Generate code to lookup number in the number string cache.
5833 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, false, &runtime);
5834 __ add(sp, sp, Operand(1 * kPointerSize));
5835 __ Ret();
5836
5837 __ bind(&runtime);
5838 // Handle number to string in the runtime system if not found in the cache.
5839 __ TailCallRuntime(Runtime::kNumberToString, 1, 1);
5840}
5841
5842
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005843// On entry r0 (rhs) and r1 (lhs) are the values to be compared.
5844// On exit r0 is 0, positive or negative to indicate the result of
5845// the comparison.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005846void CompareStub::Generate(MacroAssembler* masm) {
5847 Label slow; // Call builtin.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005848 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005849
5850 // NOTICE! This code is only reached after a smi-fast-case check, so
5851 // it is certain that at least one operand isn't a smi.
5852
5853 // Handle the case where the objects are identical. Either returns the answer
5854 // or goes to slow. Only falls through if the objects were not identical.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005855 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005856
5857 // If either is a Smi (we know that not both are), then they can only
5858 // be strictly equal if the other is a HeapNumber.
5859 ASSERT_EQ(0, kSmiTag);
5860 ASSERT_EQ(0, Smi::FromInt(0));
5861 __ and_(r2, r0, Operand(r1));
5862 __ tst(r2, Operand(kSmiTagMask));
5863 __ b(ne, &not_smis);
5864 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
5865 // 1) Return the answer.
5866 // 2) Go to slow.
5867 // 3) Fall through to both_loaded_as_doubles.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005868 // 4) Jump to lhs_not_nan.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005869 // In cases 3 and 4 we have found out we were dealing with a number-number
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005870 // comparison. If VFP3 is supported the double values of the numbers have
5871 // been loaded into d7 and d6. Otherwise, the double values have been loaded
5872 // into r0, r1, r2, and r3.
5873 EmitSmiNonsmiComparison(masm, &lhs_not_nan, &slow, strict_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005874
5875 __ bind(&both_loaded_as_doubles);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005876 // The arguments have been converted to doubles and stored in d6 and d7, if
5877 // VFP3 is supported, or in r0, r1, r2, and r3.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005878 if (CpuFeatures::IsSupported(VFP3)) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005879 __ bind(&lhs_not_nan);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005880 CpuFeatures::Scope scope(VFP3);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005881 Label no_nan;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005882 // ARMv7 VFP3 instructions to implement double precision comparison.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005883 __ vcmp(d7, d6);
5884 __ vmrs(pc); // Move vector status bits to normal status bits.
5885 Label nan;
5886 __ b(vs, &nan);
5887 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
5888 __ mov(r0, Operand(LESS), LeaveCC, lt);
5889 __ mov(r0, Operand(GREATER), LeaveCC, gt);
5890 __ mov(pc, Operand(lr));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005891
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005892 __ bind(&nan);
5893 // If one of the sides was a NaN then the v flag is set. Load r0 with
5894 // whatever it takes to make the comparison fail, since comparisons with NaN
5895 // always fail.
5896 if (cc_ == lt || cc_ == le) {
5897 __ mov(r0, Operand(GREATER));
5898 } else {
5899 __ mov(r0, Operand(LESS));
5900 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005901 __ mov(pc, Operand(lr));
5902 } else {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005903 // Checks for NaN in the doubles we have loaded. Can return the answer or
5904 // fall through if neither is a NaN. Also binds lhs_not_nan.
5905 EmitNanCheck(masm, &lhs_not_nan, cc_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005906 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
5907 // answer. Never falls through.
5908 EmitTwoNonNanDoubleComparison(masm, cc_);
5909 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005910
5911 __ bind(&not_smis);
5912 // At this point we know we are dealing with two different objects,
5913 // and neither of them is a Smi. The objects are in r0 and r1.
5914 if (strict_) {
5915 // This returns non-equal for some object types, or falls through if it
5916 // was not lucky.
5917 EmitStrictTwoHeapObjectCompare(masm);
5918 }
5919
5920 Label check_for_symbols;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005921 Label flat_string_check;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005922 // Check for heap-number-heap-number comparison. Can jump to slow case,
5923 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
5924 // that case. If the inputs are not doubles then jumps to check_for_symbols.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005925 // In this case r2 will contain the type of r0. Never falls through.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005926 EmitCheckForTwoHeapNumbers(masm,
5927 &both_loaded_as_doubles,
5928 &check_for_symbols,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005929 &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005930
5931 __ bind(&check_for_symbols);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005932 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
5933 // symbols.
5934 if (cc_ == eq && !strict_) {
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005935 // Either jumps to slow or returns the answer. Assumes that r2 is the type
5936 // of r0 on entry.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005937 EmitCheckForSymbols(masm, &flat_string_check);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005938 }
5939
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005940 // Check for both being sequential ASCII strings, and inline if that is the
5941 // case.
5942 __ bind(&flat_string_check);
5943
5944 __ JumpIfNonSmisNotBothSequentialAsciiStrings(r0, r1, r2, r3, &slow);
5945
5946 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
5947 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
5948 r1,
5949 r0,
5950 r2,
5951 r3,
5952 r4,
5953 r5);
5954 // Never falls through to here.
5955
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005956 __ bind(&slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00005957
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005958 __ push(r1);
5959 __ push(r0);
5960 // Figure out which native to call and setup the arguments.
5961 Builtins::JavaScript native;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005962 if (cc_ == eq) {
5963 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
5964 } else {
5965 native = Builtins::COMPARE;
5966 int ncr; // NaN compare result
5967 if (cc_ == lt || cc_ == le) {
5968 ncr = GREATER;
5969 } else {
5970 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
5971 ncr = LESS;
5972 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005973 __ mov(r0, Operand(Smi::FromInt(ncr)));
5974 __ push(r0);
5975 }
5976
5977 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
5978 // tagged as a small integer.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00005979 __ InvokeBuiltin(native, JUMP_JS);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00005980}
5981
5982
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +00005983// We fall into this code if the operands were Smis, but the result was
5984// not (eg. overflow). We branch into this code (to the not_smi label) if
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005985// the operands were not both Smi. The operands are in r0 and r1. In order
5986// to call the C-implemented binary fp operation routines we need to end up
5987// with the double precision floating point operands in r0 and r1 (for the
5988// value in r1) and r2 and r3 (for the value in r0).
ager@chromium.org357bf652010-04-12 11:30:10 +00005989void GenericBinaryOpStub::HandleBinaryOpSlowCases(
5990 MacroAssembler* masm,
5991 Label* not_smi,
5992 Register lhs,
5993 Register rhs,
5994 const Builtins::JavaScript& builtin) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00005995 Label slow, slow_pop_2_first, do_the_call;
5996 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
ager@chromium.org357bf652010-04-12 11:30:10 +00005997 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +00005998
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00005999 ASSERT((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)));
ager@chromium.org357bf652010-04-12 11:30:10 +00006000
6001 if (ShouldGenerateSmiCode()) {
6002 // Smi-smi case (overflow).
6003 // Since both are Smis there is no heap number to overwrite, so allocate.
6004 // The new heap number is in r5. r6 and r7 are scratch.
6005 __ AllocateHeapNumber(r5, r6, r7, &slow);
6006
6007 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
6008 // using registers d7 and d6 for the double values.
6009 if (use_fp_registers) {
6010 CpuFeatures::Scope scope(VFP3);
6011 __ mov(r7, Operand(rhs, ASR, kSmiTagSize));
6012 __ vmov(s15, r7);
6013 __ vcvt_f64_s32(d7, s15);
6014 __ mov(r7, Operand(lhs, ASR, kSmiTagSize));
6015 __ vmov(s13, r7);
6016 __ vcvt_f64_s32(d6, s13);
6017 } else {
6018 // Write Smi from rhs to r3 and r2 in double format. r6 is scratch.
6019 __ mov(r7, Operand(rhs));
6020 ConvertToDoubleStub stub1(r3, r2, r7, r6);
6021 __ push(lr);
6022 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
6023 // Write Smi from lhs to r1 and r0 in double format. r6 is scratch.
6024 __ mov(r7, Operand(lhs));
6025 ConvertToDoubleStub stub2(r1, r0, r7, r6);
6026 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
6027 __ pop(lr);
6028 }
6029 __ jmp(&do_the_call); // Tail call. No return.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006030 }
6031
ager@chromium.org357bf652010-04-12 11:30:10 +00006032 // We branch here if at least one of r0 and r1 is not a Smi.
6033 __ bind(not_smi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006034
ager@chromium.org357bf652010-04-12 11:30:10 +00006035 if (lhs.is(r0)) {
6036 __ Swap(r0, r1, ip);
6037 }
6038
6039 if (ShouldGenerateFPCode()) {
6040 if (runtime_operands_type_ == BinaryOpIC::DEFAULT) {
6041 switch (op_) {
6042 case Token::ADD:
6043 case Token::SUB:
6044 case Token::MUL:
6045 case Token::DIV:
6046 GenerateTypeTransition(masm);
6047 break;
6048
6049 default:
6050 break;
6051 }
6052 }
6053
6054 if (mode_ == NO_OVERWRITE) {
6055 // In the case where there is no chance of an overwritable float we may as
6056 // well do the allocation immediately while r0 and r1 are untouched.
6057 __ AllocateHeapNumber(r5, r6, r7, &slow);
6058 }
6059
6060 // Move r0 to a double in r2-r3.
6061 __ tst(r0, Operand(kSmiTagMask));
6062 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
6063 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
6064 __ b(ne, &slow);
6065 if (mode_ == OVERWRITE_RIGHT) {
6066 __ mov(r5, Operand(r0)); // Overwrite this heap number.
6067 }
6068 if (use_fp_registers) {
6069 CpuFeatures::Scope scope(VFP3);
6070 // Load the double from tagged HeapNumber r0 to d7.
6071 __ sub(r7, r0, Operand(kHeapObjectTag));
6072 __ vldr(d7, r7, HeapNumber::kValueOffset);
6073 } else {
6074 // Calling convention says that second double is in r2 and r3.
6075 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kValueOffset));
6076 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kValueOffset + 4));
6077 }
6078 __ jmp(&finished_loading_r0);
6079 __ bind(&r0_is_smi);
6080 if (mode_ == OVERWRITE_RIGHT) {
6081 // We can't overwrite a Smi so get address of new heap number into r5.
6082 __ AllocateHeapNumber(r5, r6, r7, &slow);
6083 }
6084
6085 if (use_fp_registers) {
6086 CpuFeatures::Scope scope(VFP3);
6087 // Convert smi in r0 to double in d7.
6088 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
6089 __ vmov(s15, r7);
6090 __ vcvt_f64_s32(d7, s15);
6091 } else {
6092 // Write Smi from r0 to r3 and r2 in double format.
6093 __ mov(r7, Operand(r0));
6094 ConvertToDoubleStub stub3(r3, r2, r7, r6);
6095 __ push(lr);
6096 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
6097 __ pop(lr);
6098 }
6099
6100 __ bind(&finished_loading_r0);
6101
6102 // Move r1 to a double in r0-r1.
6103 __ tst(r1, Operand(kSmiTagMask));
6104 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
6105 __ CompareObjectType(r1, r4, r4, HEAP_NUMBER_TYPE);
6106 __ b(ne, &slow);
6107 if (mode_ == OVERWRITE_LEFT) {
6108 __ mov(r5, Operand(r1)); // Overwrite this heap number.
6109 }
6110 if (use_fp_registers) {
6111 CpuFeatures::Scope scope(VFP3);
6112 // Load the double from tagged HeapNumber r1 to d6.
6113 __ sub(r7, r1, Operand(kHeapObjectTag));
6114 __ vldr(d6, r7, HeapNumber::kValueOffset);
6115 } else {
6116 // Calling convention says that first double is in r0 and r1.
6117 __ ldr(r0, FieldMemOperand(r1, HeapNumber::kValueOffset));
6118 __ ldr(r1, FieldMemOperand(r1, HeapNumber::kValueOffset + 4));
6119 }
6120 __ jmp(&finished_loading_r1);
6121 __ bind(&r1_is_smi);
6122 if (mode_ == OVERWRITE_LEFT) {
6123 // We can't overwrite a Smi so get address of new heap number into r5.
6124 __ AllocateHeapNumber(r5, r6, r7, &slow);
6125 }
6126
6127 if (use_fp_registers) {
6128 CpuFeatures::Scope scope(VFP3);
6129 // Convert smi in r1 to double in d6.
6130 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
6131 __ vmov(s13, r7);
6132 __ vcvt_f64_s32(d6, s13);
6133 } else {
6134 // Write Smi from r1 to r1 and r0 in double format.
6135 __ mov(r7, Operand(r1));
6136 ConvertToDoubleStub stub4(r1, r0, r7, r6);
6137 __ push(lr);
6138 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
6139 __ pop(lr);
6140 }
6141
6142 __ bind(&finished_loading_r1);
6143
6144 __ bind(&do_the_call);
6145 // If we are inlining the operation using VFP3 instructions for
6146 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
6147 if (use_fp_registers) {
6148 CpuFeatures::Scope scope(VFP3);
6149 // ARMv7 VFP3 instructions to implement
6150 // double precision, add, subtract, multiply, divide.
6151
6152 if (Token::MUL == op_) {
6153 __ vmul(d5, d6, d7);
6154 } else if (Token::DIV == op_) {
6155 __ vdiv(d5, d6, d7);
6156 } else if (Token::ADD == op_) {
6157 __ vadd(d5, d6, d7);
6158 } else if (Token::SUB == op_) {
6159 __ vsub(d5, d6, d7);
6160 } else {
6161 UNREACHABLE();
6162 }
6163 __ sub(r0, r5, Operand(kHeapObjectTag));
6164 __ vstr(d5, r0, HeapNumber::kValueOffset);
6165 __ add(r0, r0, Operand(kHeapObjectTag));
6166 __ mov(pc, lr);
6167 } else {
6168 // If we did not inline the operation, then the arguments are in:
6169 // r0: Left value (least significant part of mantissa).
6170 // r1: Left value (sign, exponent, top of mantissa).
6171 // r2: Right value (least significant part of mantissa).
6172 // r3: Right value (sign, exponent, top of mantissa).
6173 // r5: Address of heap number for result.
6174
6175 __ push(lr); // For later.
6176 __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments.
6177 // Call C routine that may not cause GC or other trouble. r5 is callee
6178 // save.
6179 __ CallCFunction(ExternalReference::double_fp_operation(op_), 4);
6180 // Store answer in the overwritable heap number.
6181 #if !defined(USE_ARM_EABI)
6182 // Double returned in fp coprocessor register 0 and 1, encoded as register
6183 // cr8. Offsets must be divisible by 4 for coprocessor so we need to
6184 // substract the tag from r5.
6185 __ sub(r4, r5, Operand(kHeapObjectTag));
6186 __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset));
6187 #else
6188 // Double returned in registers 0 and 1.
6189 __ str(r0, FieldMemOperand(r5, HeapNumber::kValueOffset));
6190 __ str(r1, FieldMemOperand(r5, HeapNumber::kValueOffset + 4));
6191 #endif
6192 __ mov(r0, Operand(r5));
6193 // And we are done.
6194 __ pop(pc);
6195 }
6196 }
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006197 // We jump to here if something goes wrong (one param is not a number of any
6198 // sort or new-space allocation fails).
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006199 __ bind(&slow);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006200
6201 // Push arguments to the stack
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006202 __ push(r1);
6203 __ push(r0);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006204
ager@chromium.org357bf652010-04-12 11:30:10 +00006205 if (Token::ADD == op_) {
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006206 // Test for string arguments before calling runtime.
6207 // r1 : first argument
6208 // r0 : second argument
6209 // sp[0] : second argument
ager@chromium.org5c838252010-02-19 08:53:10 +00006210 // sp[4] : first argument
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006211
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006212 Label not_strings, not_string1, string1, string1_smi2;
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006213 __ tst(r1, Operand(kSmiTagMask));
6214 __ b(eq, &not_string1);
6215 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
6216 __ b(ge, &not_string1);
6217
6218 // First argument is a a string, test second.
6219 __ tst(r0, Operand(kSmiTagMask));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006220 __ b(eq, &string1_smi2);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006221 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
6222 __ b(ge, &string1);
6223
6224 // First and second argument are strings.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00006225 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
6226 __ TailCallStub(&string_add_stub);
6227
6228 __ bind(&string1_smi2);
6229 // First argument is a string, second is a smi. Try to lookup the number
6230 // string for the smi in the number string cache.
6231 NumberToStringStub::GenerateLookupNumberStringCache(
6232 masm, r0, r2, r4, r5, true, &string1);
6233
6234 // Replace second argument on stack and tailcall string add stub to make
6235 // the result.
6236 __ str(r2, MemOperand(sp, 0));
6237 __ TailCallStub(&string_add_stub);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006238
6239 // Only first argument is a string.
6240 __ bind(&string1);
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006241 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
6242
6243 // First argument was not a string, test second.
6244 __ bind(&not_string1);
6245 __ tst(r0, Operand(kSmiTagMask));
6246 __ b(eq, &not_strings);
6247 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
6248 __ b(ge, &not_strings);
6249
6250 // Only second argument is a string.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00006251 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
6252
6253 __ bind(&not_strings);
6254 }
6255
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006256 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006257}
6258
6259
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006260// Tries to get a signed int32 out of a double precision floating point heap
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006261// number. Rounds towards 0. Fastest for doubles that are in the ranges
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006262// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
6263// almost to the range of signed int32 values that are not Smis. Jumps to the
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006264// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
6265// (excluding the endpoints).
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006266static void GetInt32(MacroAssembler* masm,
6267 Register source,
6268 Register dest,
6269 Register scratch,
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006270 Register scratch2,
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006271 Label* slow) {
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006272 Label right_exponent, done;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006273 // Get exponent word.
6274 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
6275 // Get exponent alone in scratch2.
6276 __ and_(scratch2, scratch, Operand(HeapNumber::kExponentMask));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006277 // Load dest with zero. We use this either for the final shift or
6278 // for the answer.
6279 __ mov(dest, Operand(0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006280 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006281 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
6282 // the exponent that we are fastest at and also the highest exponent we can
6283 // handle here.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006284 const uint32_t non_smi_exponent =
6285 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
6286 __ cmp(scratch2, Operand(non_smi_exponent));
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006287 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
6288 __ b(eq, &right_exponent);
6289 // If the exponent is higher than that then go to slow case. This catches
6290 // numbers that don't fit in a signed int32, infinities and NaNs.
6291 __ b(gt, slow);
6292
6293 // We know the exponent is smaller than 30 (biased). If it is less than
6294 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
6295 // it rounds to zero.
6296 const uint32_t zero_exponent =
6297 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
6298 __ sub(scratch2, scratch2, Operand(zero_exponent), SetCC);
6299 // Dest already has a Smi zero.
6300 __ b(lt, &done);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006301 if (!CpuFeatures::IsSupported(VFP3)) {
6302 // We have a shifted exponent between 0 and 30 in scratch2.
6303 __ mov(dest, Operand(scratch2, LSR, HeapNumber::kExponentShift));
6304 // We now have the exponent in dest. Subtract from 30 to get
6305 // how much to shift down.
6306 __ rsb(dest, dest, Operand(30));
6307 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006308 __ bind(&right_exponent);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006309 if (CpuFeatures::IsSupported(VFP3)) {
6310 CpuFeatures::Scope scope(VFP3);
6311 // ARMv7 VFP3 instructions implementing double precision to integer
6312 // conversion using round to zero.
6313 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006314 __ vmov(d7, scratch2, scratch);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006315 __ vcvt_s32_f64(s15, d7);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006316 __ vmov(dest, s15);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00006317 } else {
6318 // Get the top bits of the mantissa.
6319 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
6320 // Put back the implicit 1.
6321 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
6322 // Shift up the mantissa bits to take up the space the exponent used to
6323 // take. We just orred in the implicit bit so that took care of one and
6324 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
6325 // distance.
6326 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
6327 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
6328 // Put sign in zero flag.
6329 __ tst(scratch, Operand(HeapNumber::kSignMask));
6330 // Get the second half of the double. For some exponents we don't
6331 // actually need this because the bits get shifted out again, but
6332 // it's probably slower to test than just to do it.
6333 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
6334 // Shift down 22 bits to get the last 10 bits.
6335 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
6336 // Move down according to the exponent.
6337 __ mov(dest, Operand(scratch, LSR, dest));
6338 // Fix sign if sign bit was set.
6339 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
6340 }
ager@chromium.org5aa501c2009-06-23 07:57:28 +00006341 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006342}
6343
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006344// For bitwise ops where the inputs are not both Smis we here try to determine
6345// whether both inputs are either Smis or at least heap numbers that can be
6346// represented by a 32 bit signed value. We truncate towards zero as required
6347// by the ES spec. If this is the case we do the bitwise op and see if the
6348// result is a Smi. If so, great, otherwise we try to find a heap number to
6349// write the answer into (either by allocating or by overwriting).
ager@chromium.org357bf652010-04-12 11:30:10 +00006350// On entry the operands are in lhs and rhs. On exit the answer is in r0.
6351void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm,
6352 Register lhs,
6353 Register rhs) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006354 Label slow, result_not_a_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00006355 Label rhs_is_smi, lhs_is_smi;
6356 Label done_checking_rhs, done_checking_lhs;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006357
ager@chromium.org357bf652010-04-12 11:30:10 +00006358 __ tst(lhs, Operand(kSmiTagMask));
6359 __ b(eq, &lhs_is_smi); // It's a Smi so don't check it's a heap number.
6360 __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006361 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006362 GetInt32(masm, lhs, r3, r5, r4, &slow);
6363 __ jmp(&done_checking_lhs);
6364 __ bind(&lhs_is_smi);
6365 __ mov(r3, Operand(lhs, ASR, 1));
6366 __ bind(&done_checking_lhs);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006367
ager@chromium.org357bf652010-04-12 11:30:10 +00006368 __ tst(rhs, Operand(kSmiTagMask));
6369 __ b(eq, &rhs_is_smi); // It's a Smi so don't check it's a heap number.
6370 __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006371 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006372 GetInt32(masm, rhs, r2, r5, r4, &slow);
6373 __ jmp(&done_checking_rhs);
6374 __ bind(&rhs_is_smi);
6375 __ mov(r2, Operand(rhs, ASR, 1));
6376 __ bind(&done_checking_rhs);
6377
6378 ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0))));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006379
6380 // r0 and r1: Original operands (Smi or heap numbers).
6381 // r2 and r3: Signed int32 operands.
6382 switch (op_) {
6383 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
6384 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
6385 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
6386 case Token::SAR:
6387 // Use only the 5 least significant bits of the shift count.
6388 __ and_(r2, r2, Operand(0x1f));
6389 __ mov(r2, Operand(r3, ASR, r2));
6390 break;
6391 case Token::SHR:
6392 // Use only the 5 least significant bits of the shift count.
6393 __ and_(r2, r2, Operand(0x1f));
6394 __ mov(r2, Operand(r3, LSR, r2), SetCC);
6395 // SHR is special because it is required to produce a positive answer.
6396 // The code below for writing into heap numbers isn't capable of writing
6397 // the register as an unsigned int so we go to slow case if we hit this
6398 // case.
6399 __ b(mi, &slow);
6400 break;
6401 case Token::SHL:
6402 // Use only the 5 least significant bits of the shift count.
6403 __ and_(r2, r2, Operand(0x1f));
6404 __ mov(r2, Operand(r3, LSL, r2));
6405 break;
6406 default: UNREACHABLE();
6407 }
6408 // check that the *signed* result fits in a smi
6409 __ add(r3, r2, Operand(0x40000000), SetCC);
6410 __ b(mi, &result_not_a_smi);
6411 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
6412 __ Ret();
6413
6414 Label have_to_allocate, got_a_heap_number;
6415 __ bind(&result_not_a_smi);
6416 switch (mode_) {
6417 case OVERWRITE_RIGHT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00006418 __ tst(rhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006419 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00006420 __ mov(r5, Operand(rhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006421 break;
6422 }
6423 case OVERWRITE_LEFT: {
ager@chromium.org357bf652010-04-12 11:30:10 +00006424 __ tst(lhs, Operand(kSmiTagMask));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006425 __ b(eq, &have_to_allocate);
ager@chromium.org357bf652010-04-12 11:30:10 +00006426 __ mov(r5, Operand(lhs));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006427 break;
6428 }
6429 case NO_OVERWRITE: {
6430 // Get a new heap number in r5. r6 and r7 are scratch.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006431 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006432 }
6433 default: break;
6434 }
6435 __ bind(&got_a_heap_number);
6436 // r2: Answer as signed int32.
6437 // r5: Heap number to write answer into.
6438
6439 // Nothing can go wrong now, so move the heap number to r0, which is the
6440 // result.
6441 __ mov(r0, Operand(r5));
6442
6443 // Tail call that writes the int32 in r2 to the heap number in r0, using
6444 // r3 as scratch. r0 is preserved and returned.
6445 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
6446 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
6447
6448 if (mode_ != NO_OVERWRITE) {
6449 __ bind(&have_to_allocate);
6450 // Get a new heap number in r5. r6 and r7 are scratch.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006451 __ AllocateHeapNumber(r5, r6, r7, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006452 __ jmp(&got_a_heap_number);
6453 }
6454
6455 // If all else failed then we go to the runtime system.
6456 __ bind(&slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006457 __ push(lhs); // restore stack
6458 __ push(rhs);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006459 switch (op_) {
6460 case Token::BIT_OR:
6461 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
6462 break;
6463 case Token::BIT_AND:
6464 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
6465 break;
6466 case Token::BIT_XOR:
6467 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
6468 break;
6469 case Token::SAR:
6470 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
6471 break;
6472 case Token::SHR:
6473 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
6474 break;
6475 case Token::SHL:
6476 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
6477 break;
6478 default:
6479 UNREACHABLE();
6480 }
6481}
6482
6483
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006484// Can we multiply by x with max two shifts and an add.
6485// This answers yes to all integers from 2 to 10.
6486static bool IsEasyToMultiplyBy(int x) {
6487 if (x < 2) return false; // Avoid special cases.
6488 if (x > (Smi::kMaxValue + 1) >> 2) return false; // Almost always overflows.
6489 if (IsPowerOf2(x)) return true; // Simple shift.
6490 if (PopCountLessThanEqual2(x)) return true; // Shift and add and shift.
6491 if (IsPowerOf2(x + 1)) return true; // Patterns like 11111.
6492 return false;
6493}
6494
6495
6496// Can multiply by anything that IsEasyToMultiplyBy returns true for.
6497// Source and destination may be the same register. This routine does
6498// not set carry and overflow the way a mul instruction would.
6499static void MultiplyByKnownInt(MacroAssembler* masm,
6500 Register source,
6501 Register destination,
6502 int known_int) {
6503 if (IsPowerOf2(known_int)) {
6504 __ mov(destination, Operand(source, LSL, BitPosition(known_int)));
6505 } else if (PopCountLessThanEqual2(known_int)) {
6506 int first_bit = BitPosition(known_int);
6507 int second_bit = BitPosition(known_int ^ (1 << first_bit));
6508 __ add(destination, source, Operand(source, LSL, second_bit - first_bit));
6509 if (first_bit != 0) {
6510 __ mov(destination, Operand(destination, LSL, first_bit));
6511 }
6512 } else {
6513 ASSERT(IsPowerOf2(known_int + 1)); // Patterns like 1111.
6514 int the_bit = BitPosition(known_int + 1);
6515 __ rsb(destination, source, Operand(source, LSL, the_bit));
6516 }
6517}
6518
6519
6520// This function (as opposed to MultiplyByKnownInt) takes the known int in a
6521// a register for the cases where it doesn't know a good trick, and may deliver
6522// a result that needs shifting.
6523static void MultiplyByKnownInt2(
6524 MacroAssembler* masm,
6525 Register result,
6526 Register source,
6527 Register known_int_register, // Smi tagged.
6528 int known_int,
6529 int* required_shift) { // Including Smi tag shift
6530 switch (known_int) {
6531 case 3:
6532 __ add(result, source, Operand(source, LSL, 1));
6533 *required_shift = 1;
6534 break;
6535 case 5:
6536 __ add(result, source, Operand(source, LSL, 2));
6537 *required_shift = 1;
6538 break;
6539 case 6:
6540 __ add(result, source, Operand(source, LSL, 1));
6541 *required_shift = 2;
6542 break;
6543 case 7:
6544 __ rsb(result, source, Operand(source, LSL, 3));
6545 *required_shift = 1;
6546 break;
6547 case 9:
6548 __ add(result, source, Operand(source, LSL, 3));
6549 *required_shift = 1;
6550 break;
6551 case 10:
6552 __ add(result, source, Operand(source, LSL, 2));
6553 *required_shift = 2;
6554 break;
6555 default:
6556 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
6557 __ mul(result, source, known_int_register);
6558 *required_shift = 0;
6559 }
6560}
6561
6562
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00006563const char* GenericBinaryOpStub::GetName() {
6564 if (name_ != NULL) return name_;
6565 const int len = 100;
6566 name_ = Bootstrapper::AllocateAutoDeletedArray(len);
6567 if (name_ == NULL) return "OOM";
6568 const char* op_name = Token::Name(op_);
6569 const char* overwrite_name;
6570 switch (mode_) {
6571 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
6572 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
6573 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
6574 default: overwrite_name = "UnknownOverwrite"; break;
6575 }
6576
6577 OS::SNPrintF(Vector<char>(name_, len),
6578 "GenericBinaryOpStub_%s_%s%s",
6579 op_name,
6580 overwrite_name,
6581 specialized_on_rhs_ ? "_ConstantRhs" : 0);
6582 return name_;
6583}
6584
6585
ager@chromium.org5c838252010-02-19 08:53:10 +00006586
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006587void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006588 // lhs_ : x
6589 // rhs_ : y
6590 // r0 : result
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006591
ager@chromium.org357bf652010-04-12 11:30:10 +00006592 Register result = r0;
6593 Register lhs = lhs_;
6594 Register rhs = rhs_;
6595
6596 // This code can't cope with other register allocations yet.
6597 ASSERT(result.is(r0) &&
6598 ((lhs.is(r0) && rhs.is(r1)) ||
6599 (lhs.is(r1) && rhs.is(r0))));
6600
6601 Register smi_test_reg = VirtualFrame::scratch0();
6602 Register scratch = VirtualFrame::scratch1();
6603
6604 // All ops need to know whether we are dealing with two Smis. Set up
6605 // smi_test_reg to tell us that.
6606 if (ShouldGenerateSmiCode()) {
6607 __ orr(smi_test_reg, lhs, Operand(rhs));
6608 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006609
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006610 switch (op_) {
6611 case Token::ADD: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006612 Label not_smi;
6613 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00006614 if (ShouldGenerateSmiCode()) {
6615 ASSERT(kSmiTag == 0); // Adjust code below.
6616 __ tst(smi_test_reg, Operand(kSmiTagMask));
6617 __ b(ne, &not_smi);
6618 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
6619 // Return if no overflow.
6620 __ Ret(vc);
6621 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
6622 }
6623 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::ADD);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006624 break;
6625 }
6626
6627 case Token::SUB: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006628 Label not_smi;
6629 // Fast path.
ager@chromium.org357bf652010-04-12 11:30:10 +00006630 if (ShouldGenerateSmiCode()) {
6631 ASSERT(kSmiTag == 0); // Adjust code below.
6632 __ tst(smi_test_reg, Operand(kSmiTagMask));
6633 __ b(ne, &not_smi);
6634 if (lhs.is(r1)) {
6635 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
6636 // Return if no overflow.
6637 __ Ret(vc);
6638 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
6639 } else {
6640 __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically.
6641 // Return if no overflow.
6642 __ Ret(vc);
6643 __ add(r0, r0, Operand(r1)); // Revert optimistic subtract.
6644 }
6645 }
6646 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::SUB);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006647 break;
6648 }
6649
6650 case Token::MUL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006651 Label not_smi, slow;
ager@chromium.org357bf652010-04-12 11:30:10 +00006652 if (ShouldGenerateSmiCode()) {
6653 ASSERT(kSmiTag == 0); // adjust code below
6654 __ tst(smi_test_reg, Operand(kSmiTagMask));
6655 Register scratch2 = smi_test_reg;
6656 smi_test_reg = no_reg;
6657 __ b(ne, &not_smi);
6658 // Remove tag from one operand (but keep sign), so that result is Smi.
6659 __ mov(ip, Operand(rhs, ASR, kSmiTagSize));
6660 // Do multiplication
6661 // scratch = lower 32 bits of ip * lhs.
6662 __ smull(scratch, scratch2, lhs, ip);
6663 // Go slow on overflows (overflow bit is not set).
6664 __ mov(ip, Operand(scratch, ASR, 31));
6665 // No overflow if higher 33 bits are identical.
6666 __ cmp(ip, Operand(scratch2));
6667 __ b(ne, &slow);
6668 // Go slow on zero result to handle -0.
6669 __ tst(scratch, Operand(scratch));
6670 __ mov(result, Operand(scratch), LeaveCC, ne);
6671 __ Ret(ne);
6672 // We need -0 if we were multiplying a negative number with 0 to get 0.
6673 // We know one of them was zero.
6674 __ add(scratch2, rhs, Operand(lhs), SetCC);
6675 __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl);
6676 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
6677 // Slow case. We fall through here if we multiplied a negative number
6678 // with 0, because that would mean we should produce -0.
6679 __ bind(&slow);
6680 }
6681 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::MUL);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006682 break;
6683 }
6684
6685 case Token::DIV:
6686 case Token::MOD: {
6687 Label not_smi;
ager@chromium.org357bf652010-04-12 11:30:10 +00006688 if (ShouldGenerateSmiCode() && specialized_on_rhs_) {
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006689 Label smi_is_unsuitable;
ager@chromium.org357bf652010-04-12 11:30:10 +00006690 __ BranchOnNotSmi(lhs, &not_smi);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006691 if (IsPowerOf2(constant_rhs_)) {
6692 if (op_ == Token::MOD) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006693 __ and_(rhs,
6694 lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006695 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
6696 SetCC);
6697 // We now have the answer, but if the input was negative we also
6698 // have the sign bit. Our work is done if the result is
6699 // positive or zero:
ager@chromium.org357bf652010-04-12 11:30:10 +00006700 if (!rhs.is(r0)) {
6701 __ mov(r0, rhs, LeaveCC, pl);
6702 }
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006703 __ Ret(pl);
6704 // A mod of a negative left hand side must return a negative number.
6705 // Unfortunately if the answer is 0 then we must return -0. And we
ager@chromium.org357bf652010-04-12 11:30:10 +00006706 // already optimistically trashed rhs so we may need to restore it.
6707 __ eor(rhs, rhs, Operand(0x80000000u), SetCC);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006708 // Next two instructions are conditional on the answer being -0.
ager@chromium.org357bf652010-04-12 11:30:10 +00006709 __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006710 __ b(eq, &smi_is_unsuitable);
6711 // We need to subtract the dividend. Eg. -3 % 4 == -3.
ager@chromium.org357bf652010-04-12 11:30:10 +00006712 __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_)));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006713 } else {
6714 ASSERT(op_ == Token::DIV);
ager@chromium.org357bf652010-04-12 11:30:10 +00006715 __ tst(lhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006716 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
6717 __ b(ne, &smi_is_unsuitable); // Go slow on negative or remainder.
6718 int shift = 0;
6719 int d = constant_rhs_;
6720 while ((d & 1) == 0) {
6721 d >>= 1;
6722 shift++;
6723 }
ager@chromium.org357bf652010-04-12 11:30:10 +00006724 __ mov(r0, Operand(lhs, LSR, shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006725 __ bic(r0, r0, Operand(kSmiTagMask));
6726 }
6727 } else {
6728 // Not a power of 2.
ager@chromium.org357bf652010-04-12 11:30:10 +00006729 __ tst(lhs, Operand(0x80000000u));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006730 __ b(ne, &smi_is_unsuitable);
6731 // Find a fixed point reciprocal of the divisor so we can divide by
6732 // multiplying.
6733 double divisor = 1.0 / constant_rhs_;
6734 int shift = 32;
6735 double scale = 4294967296.0; // 1 << 32.
6736 uint32_t mul;
6737 // Maximise the precision of the fixed point reciprocal.
6738 while (true) {
6739 mul = static_cast<uint32_t>(scale * divisor);
6740 if (mul >= 0x7fffffff) break;
6741 scale *= 2.0;
6742 shift++;
6743 }
6744 mul++;
ager@chromium.org357bf652010-04-12 11:30:10 +00006745 Register scratch2 = smi_test_reg;
6746 smi_test_reg = no_reg;
6747 __ mov(scratch2, Operand(mul));
6748 __ umull(scratch, scratch2, scratch2, lhs);
6749 __ mov(scratch2, Operand(scratch2, LSR, shift - 31));
6750 // scratch2 is lhs / rhs. scratch2 is not Smi tagged.
6751 // rhs is still the known rhs. rhs is Smi tagged.
6752 // lhs is still the unkown lhs. lhs is Smi tagged.
6753 int required_scratch_shift = 0; // Including the Smi tag shift of 1.
6754 // scratch = scratch2 * rhs.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006755 MultiplyByKnownInt2(masm,
ager@chromium.org357bf652010-04-12 11:30:10 +00006756 scratch,
6757 scratch2,
6758 rhs,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006759 constant_rhs_,
ager@chromium.org357bf652010-04-12 11:30:10 +00006760 &required_scratch_shift);
6761 // scratch << required_scratch_shift is now the Smi tagged rhs *
6762 // (lhs / rhs) where / indicates integer division.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006763 if (op_ == Token::DIV) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006764 __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006765 __ b(ne, &smi_is_unsuitable); // There was a remainder.
ager@chromium.org357bf652010-04-12 11:30:10 +00006766 __ mov(result, Operand(scratch2, LSL, kSmiTagSize));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006767 } else {
6768 ASSERT(op_ == Token::MOD);
ager@chromium.org357bf652010-04-12 11:30:10 +00006769 __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006770 }
6771 }
6772 __ Ret();
6773 __ bind(&smi_is_unsuitable);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +00006774 }
ager@chromium.org357bf652010-04-12 11:30:10 +00006775 HandleBinaryOpSlowCases(
6776 masm,
6777 &not_smi,
6778 lhs,
6779 rhs,
6780 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006781 break;
6782 }
6783
6784 case Token::BIT_OR:
6785 case Token::BIT_AND:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006786 case Token::BIT_XOR:
6787 case Token::SAR:
6788 case Token::SHR:
6789 case Token::SHL: {
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006790 Label slow;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006791 ASSERT(kSmiTag == 0); // adjust code below
ager@chromium.org357bf652010-04-12 11:30:10 +00006792 __ tst(smi_test_reg, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006793 __ b(ne, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006794 Register scratch2 = smi_test_reg;
6795 smi_test_reg = no_reg;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006796 switch (op_) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006797 case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break;
6798 case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break;
6799 case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006800 case Token::SAR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006801 // Remove tags from right operand.
ager@chromium.org357bf652010-04-12 11:30:10 +00006802 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
6803 __ mov(result, Operand(lhs, ASR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006804 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00006805 __ bic(result, result, Operand(kSmiTagMask));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006806 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006807 case Token::SHR:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006808 // Remove tags from operands. We can't do this on a 31 bit number
6809 // because then the 0s get shifted into bit 30 instead of bit 31.
ager@chromium.org357bf652010-04-12 11:30:10 +00006810 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
6811 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
6812 __ mov(scratch, Operand(scratch, LSR, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006813 // Unsigned shift is not allowed to produce a negative number, so
6814 // check the sign bit and the sign bit after Smi tagging.
ager@chromium.org357bf652010-04-12 11:30:10 +00006815 __ tst(scratch, Operand(0xc0000000));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006816 __ b(ne, &slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006817 // Smi tag result.
ager@chromium.org357bf652010-04-12 11:30:10 +00006818 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006819 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006820 case Token::SHL:
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006821 // Remove tags from operands.
ager@chromium.org357bf652010-04-12 11:30:10 +00006822 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
6823 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
6824 __ mov(scratch, Operand(scratch, LSL, scratch2));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006825 // Check that the signed result fits in a Smi.
ager@chromium.org357bf652010-04-12 11:30:10 +00006826 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006827 __ b(mi, &slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006828 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006829 break;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006830 default: UNREACHABLE();
6831 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006832 __ Ret();
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006833 __ bind(&slow);
ager@chromium.org357bf652010-04-12 11:30:10 +00006834 HandleNonSmiBitwiseOp(masm, lhs, rhs);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006835 break;
6836 }
6837
6838 default: UNREACHABLE();
6839 }
ager@chromium.org65dad4b2009-04-23 08:48:43 +00006840 // This code should be unreachable.
6841 __ stop("Unreachable");
ager@chromium.org357bf652010-04-12 11:30:10 +00006842
6843 // Generate an unreachable reference to the DEFAULT stub so that it can be
6844 // found at the end of this stub when clearing ICs at GC.
6845 // TODO(kaznacheev): Check performance impact and get rid of this.
6846 if (runtime_operands_type_ != BinaryOpIC::DEFAULT) {
6847 GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT);
6848 __ CallStub(&uninit);
6849 }
6850}
6851
6852
6853void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
6854 Label get_result;
6855
6856 __ push(r1);
6857 __ push(r0);
6858
6859 // Internal frame is necessary to handle exceptions properly.
6860 __ EnterInternalFrame();
6861 // Call the stub proper to get the result in r0.
6862 __ Call(&get_result);
6863 __ LeaveInternalFrame();
6864
6865 __ push(r0);
6866
6867 __ mov(r0, Operand(Smi::FromInt(MinorKey())));
6868 __ push(r0);
6869 __ mov(r0, Operand(Smi::FromInt(op_)));
6870 __ push(r0);
6871 __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_)));
6872 __ push(r0);
6873
6874 __ TailCallExternalReference(
6875 ExternalReference(IC_Utility(IC::kBinaryOp_Patch)),
6876 6,
6877 1);
6878
6879 // The entry point for the result calculation is assumed to be immediately
6880 // after this sequence.
6881 __ bind(&get_result);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006882}
6883
6884
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006885Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
ager@chromium.org357bf652010-04-12 11:30:10 +00006886 GenericBinaryOpStub stub(key, type_info);
6887 return stub.GetCode();
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006888}
6889
6890
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006891void StackCheckStub::Generate(MacroAssembler* masm) {
ager@chromium.org3a37e9b2009-04-27 09:26:21 +00006892 // Do tail-call to runtime routine. Runtime routines expect at least one
6893 // argument, so give it a Smi.
6894 __ mov(r0, Operand(Smi::FromInt(0)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006895 __ push(r0);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00006896 __ TailCallRuntime(Runtime::kStackGuard, 1, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006897
6898 __ StubReturn(1);
6899}
6900
6901
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006902void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006903 Label slow, done;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00006904
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006905 if (op_ == Token::SUB) {
6906 // Check whether the value is a smi.
6907 Label try_float;
6908 __ tst(r0, Operand(kSmiTagMask));
6909 __ b(ne, &try_float);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006910
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006911 // Go slow case if the value of the expression is zero
6912 // to make sure that we switch between 0 and -0.
6913 __ cmp(r0, Operand(0));
6914 __ b(eq, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006915
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006916 // The value of the expression is a smi that is not zero. Try
6917 // optimistic subtraction '0 - value'.
6918 __ rsb(r1, r0, Operand(0), SetCC);
6919 __ b(vs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006920
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006921 __ mov(r0, Operand(r1)); // Set r0 to result.
6922 __ b(&done);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006923
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006924 __ bind(&try_float);
6925 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
6926 __ b(ne, &slow);
6927 // r0 is a heap number. Get a new heap number in r1.
6928 if (overwrite_) {
6929 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6930 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
6931 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6932 } else {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006933 __ AllocateHeapNumber(r1, r2, r3, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006934 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
6935 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
6936 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
6937 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
6938 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
6939 __ mov(r0, Operand(r1));
6940 }
6941 } else if (op_ == Token::BIT_NOT) {
6942 // Check if the operand is a heap number.
6943 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
6944 __ b(ne, &slow);
6945
6946 // Convert the heap number is r0 to an untagged integer in r1.
6947 GetInt32(masm, r0, r1, r2, r3, &slow);
6948
6949 // Do the bitwise operation (move negated) and check if the result
6950 // fits in a smi.
6951 Label try_float;
6952 __ mvn(r1, Operand(r1));
6953 __ add(r2, r1, Operand(0x40000000), SetCC);
6954 __ b(mi, &try_float);
6955 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
6956 __ b(&done);
6957
6958 __ bind(&try_float);
6959 if (!overwrite_) {
6960 // Allocate a fresh heap number, but don't overwrite r0 until
6961 // we're sure we can do it without going through the slow case
6962 // that needs the value in r0.
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00006963 __ AllocateHeapNumber(r2, r3, r4, &slow);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006964 __ mov(r0, Operand(r2));
6965 }
6966
6967 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
6968 // have to set up a frame.
6969 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
6970 __ push(lr);
6971 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
6972 __ pop(lr);
6973 } else {
6974 UNIMPLEMENTED();
6975 }
6976
6977 __ bind(&done);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006978 __ StubReturn(1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006979
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006980 // Handle the slow case by jumping to the JavaScript builtin.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006981 __ bind(&slow);
6982 __ push(r0);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00006983 switch (op_) {
6984 case Token::SUB:
6985 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
6986 break;
6987 case Token::BIT_NOT:
6988 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
6989 break;
6990 default:
6991 UNREACHABLE();
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006992 }
ager@chromium.orga1645e22009-09-09 19:27:10 +00006993}
6994
6995
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00006996void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00006997 // r0 holds the exception.
6998
6999 // Adjust this code if not the case.
7000 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
7001
7002 // Drop the sp to the top of the handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007003 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
7004 __ ldr(sp, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007005
7006 // Restore the next handler and frame pointer, discard handler state.
7007 ASSERT(StackHandlerConstants::kNextOffset == 0);
7008 __ pop(r2);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007009 __ str(r2, MemOperand(r3));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007010 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
7011 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
7012
7013 // Before returning we restore the context from the frame pointer if
7014 // not NULL. The frame pointer is NULL in the exception handler of a
7015 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007016 __ cmp(fp, Operand(0));
7017 // Set cp to NULL if fp is NULL.
7018 __ mov(cp, Operand(0), LeaveCC, eq);
7019 // Restore cp otherwise.
7020 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007021#ifdef DEBUG
7022 if (FLAG_debug_code) {
7023 __ mov(lr, Operand(pc));
7024 }
7025#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007026 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007027 __ pop(pc);
7028}
7029
7030
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007031void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
7032 UncatchableExceptionType type) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007033 // Adjust this code if not the case.
7034 ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
7035
7036 // Drop sp to the top stack handler.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007037 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007038 __ ldr(sp, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007039
7040 // Unwind the handlers until the ENTRY handler is found.
7041 Label loop, done;
7042 __ bind(&loop);
7043 // Load the type of the current stack handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007044 const int kStateOffset = StackHandlerConstants::kStateOffset;
7045 __ ldr(r2, MemOperand(sp, kStateOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007046 __ cmp(r2, Operand(StackHandler::ENTRY));
7047 __ b(eq, &done);
7048 // Fetch the next handler in the list.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007049 const int kNextOffset = StackHandlerConstants::kNextOffset;
7050 __ ldr(sp, MemOperand(sp, kNextOffset));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007051 __ jmp(&loop);
7052 __ bind(&done);
7053
7054 // Set the top handler address to next handler past the current ENTRY handler.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007055 ASSERT(StackHandlerConstants::kNextOffset == 0);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007056 __ pop(r2);
7057 __ str(r2, MemOperand(r3));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007058
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007059 if (type == OUT_OF_MEMORY) {
7060 // Set external caught exception to false.
7061 ExternalReference external_caught(Top::k_external_caught_exception_address);
7062 __ mov(r0, Operand(false));
7063 __ mov(r2, Operand(external_caught));
7064 __ str(r0, MemOperand(r2));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007065
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007066 // Set pending exception and r0 to out of memory exception.
7067 Failure* out_of_memory = Failure::OutOfMemoryException();
7068 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
7069 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
7070 __ str(r0, MemOperand(r2));
7071 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007072
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007073 // Stack layout at this point. See also StackHandlerConstants.
7074 // sp -> state (ENTRY)
7075 // fp
7076 // lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007077
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007078 // Discard handler state (r2 is not used) and restore frame pointer.
7079 ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
7080 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
7081 // Before returning we restore the context from the frame pointer if
7082 // not NULL. The frame pointer is NULL in the exception handler of a
7083 // JS entry frame.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007084 __ cmp(fp, Operand(0));
7085 // Set cp to NULL if fp is NULL.
7086 __ mov(cp, Operand(0), LeaveCC, eq);
7087 // Restore cp otherwise.
7088 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007089#ifdef DEBUG
7090 if (FLAG_debug_code) {
7091 __ mov(lr, Operand(pc));
7092 }
7093#endif
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007094 ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007095 __ pop(pc);
7096}
7097
7098
7099void CEntryStub::GenerateCore(MacroAssembler* masm,
7100 Label* throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007101 Label* throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007102 Label* throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007103 bool do_gc,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007104 bool always_allocate,
7105 int frame_alignment_skew) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007106 // r0: result parameter for PerformGC, if any
7107 // r4: number of arguments including receiver (C callee-saved)
7108 // r5: pointer to builtin function (C callee-saved)
7109 // r6: pointer to the first argument (C callee-saved)
7110
7111 if (do_gc) {
7112 // Passing r0.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007113 __ PrepareCallCFunction(1, r1);
7114 __ CallCFunction(ExternalReference::perform_gc_function(), 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007115 }
7116
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007117 ExternalReference scope_depth =
7118 ExternalReference::heap_always_allocate_scope_depth();
7119 if (always_allocate) {
7120 __ mov(r0, Operand(scope_depth));
7121 __ ldr(r1, MemOperand(r0));
7122 __ add(r1, r1, Operand(1));
7123 __ str(r1, MemOperand(r0));
7124 }
7125
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007126 // Call C built-in.
7127 // r0 = argc, r1 = argv
7128 __ mov(r0, Operand(r4));
7129 __ mov(r1, Operand(r6));
7130
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007131 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
7132 int frame_alignment_mask = frame_alignment - 1;
7133#if defined(V8_HOST_ARCH_ARM)
7134 if (FLAG_debug_code) {
7135 if (frame_alignment > kPointerSize) {
7136 Label alignment_as_expected;
7137 ASSERT(IsPowerOf2(frame_alignment));
7138 __ sub(r2, sp, Operand(frame_alignment_skew));
7139 __ tst(r2, Operand(frame_alignment_mask));
7140 __ b(eq, &alignment_as_expected);
7141 // Don't use Check here, as it will call Runtime_Abort re-entering here.
7142 __ stop("Unexpected alignment");
7143 __ bind(&alignment_as_expected);
7144 }
7145 }
7146#endif
7147
7148 // Just before the call (jump) below lr is pushed, so the actual alignment is
7149 // adding one to the current skew.
7150 int alignment_before_call =
7151 (frame_alignment_skew + kPointerSize) & frame_alignment_mask;
7152 if (alignment_before_call > 0) {
7153 // Push until the alignment before the call is met.
7154 __ mov(r2, Operand(0));
7155 for (int i = alignment_before_call;
7156 (i & frame_alignment_mask) != 0;
7157 i += kPointerSize) {
7158 __ push(r2);
7159 }
7160 }
7161
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007162 // TODO(1242173): To let the GC traverse the return address of the exit
7163 // frames, we need to know where the return address is. Right now,
7164 // we push it on the stack to be able to find it again, but we never
7165 // restore from it in case of changes, which makes it impossible to
7166 // support moving the C entry code stub. This should be fixed, but currently
7167 // this is OK because the CEntryStub gets generated so early in the V8 boot
7168 // sequence that it is not moving ever.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007169 masm->add(lr, pc, Operand(4)); // Compute return address: (pc + 8) + 4
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00007170 masm->push(lr);
7171 masm->Jump(r5);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007172
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007173 // Restore sp back to before aligning the stack.
7174 if (alignment_before_call > 0) {
7175 __ add(sp, sp, Operand(alignment_before_call));
7176 }
7177
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007178 if (always_allocate) {
7179 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
7180 // though (contain the result).
7181 __ mov(r2, Operand(scope_depth));
7182 __ ldr(r3, MemOperand(r2));
7183 __ sub(r3, r3, Operand(1));
7184 __ str(r3, MemOperand(r2));
7185 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007186
7187 // check for failure result
7188 Label failure_returned;
7189 ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
7190 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
7191 __ add(r2, r0, Operand(1));
7192 __ tst(r2, Operand(kFailureTagMask));
7193 __ b(eq, &failure_returned);
7194
7195 // Exit C frame and return.
7196 // r0:r1: result
7197 // sp: stack pointer
7198 // fp: frame pointer
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007199 __ LeaveExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007200
7201 // check if we should retry or throw exception
7202 Label retry;
7203 __ bind(&failure_returned);
7204 ASSERT(Failure::RETRY_AFTER_GC == 0);
7205 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
7206 __ b(eq, &retry);
7207
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007208 // Special handling of out of memory exceptions.
7209 Failure* out_of_memory = Failure::OutOfMemoryException();
7210 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
7211 __ b(eq, throw_out_of_memory_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007212
7213 // Retrieve the pending exception and clear the variable.
ager@chromium.org32912102009-01-16 10:38:43 +00007214 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007215 __ ldr(r3, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00007216 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007217 __ ldr(r0, MemOperand(ip));
7218 __ str(r3, MemOperand(ip));
7219
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007220 // Special handling of termination exceptions which are uncatchable
7221 // by javascript code.
7222 __ cmp(r0, Operand(Factory::termination_exception()));
7223 __ b(eq, throw_termination_exception);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007224
7225 // Handle normal exception.
7226 __ jmp(throw_normal_exception);
7227
7228 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
7229}
7230
7231
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007232void CEntryStub::Generate(MacroAssembler* masm) {
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007233 // Called from JavaScript; parameters are on stack as if calling JS function
7234 // r0: number of arguments including receiver
7235 // r1: pointer to builtin function
7236 // fp: frame pointer (restored after C call)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007237 // sp: stack pointer (restored as callee's sp after C call)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007238 // cp: current context (C callee-saved)
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007239
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007240 // Result returned in r0 or r0+r1 by default.
7241
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007242 // NOTE: Invocations of builtins may return failure objects
7243 // instead of a proper result. The builtin entry handles
7244 // this by performing a garbage collection and retrying the
7245 // builtin once.
7246
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007247 // Enter the exit frame that transitions from JavaScript to C++.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007248 __ EnterExitFrame(mode_);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007249
7250 // r4: number of arguments (C callee-saved)
7251 // r5: pointer to builtin function (C callee-saved)
7252 // r6: pointer to first argument (C callee-saved)
7253
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007254 Label throw_normal_exception;
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007255 Label throw_termination_exception;
7256 Label throw_out_of_memory_exception;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007257
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00007258 // Call into the runtime system.
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007259 GenerateCore(masm,
7260 &throw_normal_exception,
7261 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007262 &throw_out_of_memory_exception,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00007263 false,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007264 false,
7265 -kPointerSize);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007266
7267 // Do space-specific GC and retry runtime call.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007268 GenerateCore(masm,
7269 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007270 &throw_termination_exception,
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007271 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007272 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007273 false,
7274 0);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007275
7276 // Do full GC and retry runtime call one final time.
7277 Failure* failure = Failure::InternalError();
7278 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
7279 GenerateCore(masm,
7280 &throw_normal_exception,
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007281 &throw_termination_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007282 &throw_out_of_memory_exception,
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +00007283 true,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007284 true,
7285 kPointerSize);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007286
7287 __ bind(&throw_out_of_memory_exception);
sgjesse@chromium.orgc81c8942009-08-21 10:54:26 +00007288 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
7289
7290 __ bind(&throw_termination_exception);
7291 GenerateThrowUncatchable(masm, TERMINATION);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007292
7293 __ bind(&throw_normal_exception);
7294 GenerateThrowTOS(masm);
7295}
7296
7297
7298void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
7299 // r0: code entry
7300 // r1: function
7301 // r2: receiver
7302 // r3: argc
7303 // [sp+0]: argv
7304
7305 Label invoke, exit;
7306
7307 // Called from C, so do not pop argc and args on exit (preserve sp)
7308 // No need to save register-passed args
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007309 // Save callee-saved registers (incl. cp and fp), sp, and lr
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007310 __ stm(db_w, sp, kCalleeSaved | lr.bit());
7311
7312 // Get address of argv, see stm above.
7313 // r0: code entry
7314 // r1: function
7315 // r2: receiver
7316 // r3: argc
ager@chromium.org5c838252010-02-19 08:53:10 +00007317 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007318
7319 // Push a frame with special values setup to mark it as an entry frame.
7320 // r0: code entry
7321 // r1: function
7322 // r2: receiver
7323 // r3: argc
7324 // r4: argv
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007325 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00007326 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
7327 __ mov(r7, Operand(Smi::FromInt(marker)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007328 __ mov(r6, Operand(Smi::FromInt(marker)));
7329 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
7330 __ ldr(r5, MemOperand(r5));
7331 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | r8.bit());
7332
7333 // Setup frame pointer for the frame to be pushed.
7334 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
7335
7336 // Call a faked try-block that does the invoke.
7337 __ bl(&invoke);
7338
7339 // Caught exception: Store result (exception) in the pending
7340 // exception field in the JSEnv and return a failure sentinel.
7341 // Coming in here the fp will be invalid because the PushTryHandler below
7342 // sets it to 0 to signal the existence of the JSEntry frame.
ager@chromium.org32912102009-01-16 10:38:43 +00007343 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007344 __ str(r0, MemOperand(ip));
ager@chromium.org3bf7b912008-11-17 09:09:45 +00007345 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007346 __ b(&exit);
7347
7348 // Invoke: Link this frame into the handler chain.
7349 __ bind(&invoke);
7350 // Must preserve r0-r4, r5-r7 are available.
7351 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007352 // If an exception not caught by another handler occurs, this handler
7353 // returns control to the code after the bl(&invoke) above, which
7354 // restores all kCalleeSaved registers (including cp and fp) to their
7355 // saved values before returning a failure to C.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007356
7357 // Clear any pending exceptions.
7358 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
7359 __ ldr(r5, MemOperand(ip));
ager@chromium.org32912102009-01-16 10:38:43 +00007360 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007361 __ str(r5, MemOperand(ip));
7362
7363 // Invoke the function by calling through JS entry trampoline builtin.
7364 // Notice that we cannot store a reference to the trampoline code directly in
7365 // this stub, because runtime stubs are not traversed when doing GC.
7366
7367 // Expected registers by Builtins::JSEntryTrampoline
7368 // r0: code entry
7369 // r1: function
7370 // r2: receiver
7371 // r3: argc
7372 // r4: argv
7373 if (is_construct) {
7374 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
7375 __ mov(ip, Operand(construct_entry));
7376 } else {
7377 ExternalReference entry(Builtins::JSEntryTrampoline);
7378 __ mov(ip, Operand(entry));
7379 }
7380 __ ldr(ip, MemOperand(ip)); // deref address
7381
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007382 // Branch and link to JSEntryTrampoline. We don't use the double underscore
7383 // macro for the add instruction because we don't want the coverage tool
7384 // inserting instructions here after we read the pc.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007385 __ mov(lr, Operand(pc));
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007386 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007387
7388 // Unlink this frame from the handler chain. When reading the
7389 // address of the next handler, there is no need to use the address
7390 // displacement since the current stack pointer (sp) points directly
7391 // to the stack handler.
7392 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
7393 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
7394 __ str(r3, MemOperand(ip));
7395 // No need to restore registers
7396 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
7397
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007398
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007399 __ bind(&exit); // r0 holds result
7400 // Restore the top frame descriptors from the stack.
7401 __ pop(r3);
7402 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
7403 __ str(r3, MemOperand(ip));
7404
7405 // Reset the stack to the callee saved registers.
7406 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
7407
7408 // Restore callee-saved registers and return.
7409#ifdef DEBUG
ager@chromium.org65dad4b2009-04-23 08:48:43 +00007410 if (FLAG_debug_code) {
7411 __ mov(lr, Operand(pc));
7412 }
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007413#endif
7414 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
7415}
7416
7417
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007418// This stub performs an instanceof, calling the builtin function if
7419// necessary. Uses r1 for the object, r0 for the function that it may
7420// be an instance of (these are fetched from the stack).
7421void InstanceofStub::Generate(MacroAssembler* masm) {
7422 // Get the object - slow case for smis (we may need to throw an exception
7423 // depending on the rhs).
7424 Label slow, loop, is_instance, is_not_instance;
7425 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
7426 __ BranchOnSmi(r0, &slow);
7427
7428 // Check that the left hand is a JS object and put map in r3.
7429 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
7430 __ b(lt, &slow);
7431 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
7432 __ b(gt, &slow);
7433
7434 // Get the prototype of the function (r4 is result, r2 is scratch).
ager@chromium.org5c838252010-02-19 08:53:10 +00007435 __ ldr(r1, MemOperand(sp, 0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007436 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
7437
7438 // Check that the function prototype is a JS object.
7439 __ BranchOnSmi(r4, &slow);
7440 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
7441 __ b(lt, &slow);
7442 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
7443 __ b(gt, &slow);
7444
7445 // Register mapping: r3 is object map and r4 is function prototype.
7446 // Get prototype of object into r2.
7447 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
7448
7449 // Loop through the prototype chain looking for the function prototype.
7450 __ bind(&loop);
7451 __ cmp(r2, Operand(r4));
7452 __ b(eq, &is_instance);
ager@chromium.orgab99eea2009-08-25 07:05:41 +00007453 __ LoadRoot(ip, Heap::kNullValueRootIndex);
7454 __ cmp(r2, ip);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007455 __ b(eq, &is_not_instance);
7456 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
7457 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
7458 __ jmp(&loop);
7459
7460 __ bind(&is_instance);
7461 __ mov(r0, Operand(Smi::FromInt(0)));
7462 __ pop();
7463 __ pop();
7464 __ mov(pc, Operand(lr)); // Return.
7465
7466 __ bind(&is_not_instance);
7467 __ mov(r0, Operand(Smi::FromInt(1)));
7468 __ pop();
7469 __ pop();
7470 __ mov(pc, Operand(lr)); // Return.
7471
7472 // Slow-case. Tail call builtin.
7473 __ bind(&slow);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007474 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
7475}
7476
7477
ager@chromium.org7c537e22008-10-16 08:43:32 +00007478void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
7479 // The displacement is the offset of the last parameter (if any)
7480 // relative to the frame pointer.
7481 static const int kDisplacement =
7482 StandardFrameConstants::kCallerSPOffset - kPointerSize;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007483
ager@chromium.org7c537e22008-10-16 08:43:32 +00007484 // Check that the key is a smi.
7485 Label slow;
ager@chromium.orgeadaf222009-06-16 09:43:10 +00007486 __ BranchOnNotSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007487
ager@chromium.org7c537e22008-10-16 08:43:32 +00007488 // Check if the calling frame is an arguments adaptor frame.
7489 Label adaptor;
7490 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
7491 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00007492 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org7c537e22008-10-16 08:43:32 +00007493 __ b(eq, &adaptor);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007494
ager@chromium.org7c537e22008-10-16 08:43:32 +00007495 // Check index against formal parameters count limit passed in
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +00007496 // through register r0. Use unsigned comparison to get negative
ager@chromium.org7c537e22008-10-16 08:43:32 +00007497 // check for free.
7498 __ cmp(r1, r0);
7499 __ b(cs, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007500
ager@chromium.org7c537e22008-10-16 08:43:32 +00007501 // Read the argument from the stack and return it.
7502 __ sub(r3, r0, r1);
7503 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
7504 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00007505 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00007506
7507 // Arguments adaptor case: Check index against actual arguments
7508 // limit found in the arguments adaptor frame. Use unsigned
7509 // comparison to get negative check for free.
7510 __ bind(&adaptor);
7511 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
7512 __ cmp(r1, r0);
7513 __ b(cs, &slow);
7514
7515 // Read the argument from the adaptor frame and return it.
7516 __ sub(r3, r0, r1);
7517 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
7518 __ ldr(r0, MemOperand(r3, kDisplacement));
ager@chromium.org9085a012009-05-11 19:22:57 +00007519 __ Jump(lr);
ager@chromium.org7c537e22008-10-16 08:43:32 +00007520
7521 // Slow-case: Handle non-smi or out-of-bounds access to arguments
7522 // by calling the runtime system.
7523 __ bind(&slow);
7524 __ push(r1);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007525 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
ager@chromium.org7c537e22008-10-16 08:43:32 +00007526}
7527
7528
7529void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
ager@chromium.org5c838252010-02-19 08:53:10 +00007530 // sp[0] : number of parameters
7531 // sp[4] : receiver displacement
7532 // sp[8] : function
7533
ager@chromium.org7c537e22008-10-16 08:43:32 +00007534 // Check if the calling frame is an arguments adaptor frame.
ager@chromium.org5c838252010-02-19 08:53:10 +00007535 Label adaptor_frame, try_allocate, runtime;
ager@chromium.org7c537e22008-10-16 08:43:32 +00007536 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
7537 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
ager@chromium.org18ad94b2009-09-02 08:22:29 +00007538 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ager@chromium.org5c838252010-02-19 08:53:10 +00007539 __ b(eq, &adaptor_frame);
7540
7541 // Get the length from the frame.
7542 __ ldr(r1, MemOperand(sp, 0));
7543 __ b(&try_allocate);
ager@chromium.org7c537e22008-10-16 08:43:32 +00007544
7545 // Patch the arguments.length and the parameters pointer.
ager@chromium.org5c838252010-02-19 08:53:10 +00007546 __ bind(&adaptor_frame);
7547 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
7548 __ str(r1, MemOperand(sp, 0));
7549 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
ager@chromium.org7c537e22008-10-16 08:43:32 +00007550 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
7551 __ str(r3, MemOperand(sp, 1 * kPointerSize));
7552
ager@chromium.org5c838252010-02-19 08:53:10 +00007553 // Try the new space allocation. Start out with computing the size
7554 // of the arguments object and the elements array (in words, not
7555 // bytes because AllocateInNewSpace expects words).
7556 Label add_arguments_object;
7557 __ bind(&try_allocate);
7558 __ cmp(r1, Operand(0));
7559 __ b(eq, &add_arguments_object);
7560 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
7561 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
7562 __ bind(&add_arguments_object);
7563 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
7564
7565 // Do the allocation of both objects in one go.
7566 __ AllocateInNewSpace(r1, r0, r2, r3, &runtime, TAG_OBJECT);
7567
7568 // Get the arguments boilerplate from the current (global) context.
7569 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
7570 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
7571 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
7572 __ ldr(r4, MemOperand(r4, offset));
7573
7574 // Copy the JS object part.
7575 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
7576 __ ldr(r3, FieldMemOperand(r4, i));
7577 __ str(r3, FieldMemOperand(r0, i));
7578 }
7579
7580 // Setup the callee in-object property.
7581 ASSERT(Heap::arguments_callee_index == 0);
7582 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
7583 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
7584
7585 // Get the length (smi tagged) and set that as an in-object property too.
7586 ASSERT(Heap::arguments_length_index == 1);
7587 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
7588 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
7589
7590 // If there are no actual arguments, we're done.
7591 Label done;
7592 __ cmp(r1, Operand(0));
7593 __ b(eq, &done);
7594
7595 // Get the parameters pointer from the stack and untag the length.
7596 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
7597 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
7598
7599 // Setup the elements pointer in the allocated arguments object and
7600 // initialize the header in the elements fixed array.
7601 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
7602 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
7603 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
7604 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
7605 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
7606
7607 // Copy the fixed array slots.
7608 Label loop;
7609 // Setup r4 to point to the first array slot.
7610 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
7611 __ bind(&loop);
7612 // Pre-decrement r2 with kPointerSize on each iteration.
7613 // Pre-decrement in order to skip receiver.
7614 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
7615 // Post-increment r4 with kPointerSize on each iteration.
7616 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
7617 __ sub(r1, r1, Operand(1));
7618 __ cmp(r1, Operand(0));
7619 __ b(ne, &loop);
7620
7621 // Return and remove the on-stack parameters.
7622 __ bind(&done);
7623 __ add(sp, sp, Operand(3 * kPointerSize));
7624 __ Ret();
7625
ager@chromium.org7c537e22008-10-16 08:43:32 +00007626 // Do the runtime call to allocate the arguments object.
7627 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00007628 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007629}
7630
7631
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00007632void RegExpExecStub::Generate(MacroAssembler* masm) {
7633 // Just jump directly to runtime if native RegExp is not selected at compile
7634 // time or if regexp entry in generated code is turned off runtime switch or
7635 // at compilation.
7636#ifndef V8_NATIVE_REGEXP
7637 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
7638#else // V8_NATIVE_REGEXP
7639 if (!FLAG_regexp_entry_native) {
7640 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
7641 return;
7642 }
7643
7644 // Stack frame on entry.
7645 // sp[0]: last_match_info (expected JSArray)
7646 // sp[4]: previous index
7647 // sp[8]: subject string
7648 // sp[12]: JSRegExp object
7649
7650 static const int kLastMatchInfoOffset = 0 * kPointerSize;
7651 static const int kPreviousIndexOffset = 1 * kPointerSize;
7652 static const int kSubjectOffset = 2 * kPointerSize;
7653 static const int kJSRegExpOffset = 3 * kPointerSize;
7654
7655 Label runtime, invoke_regexp;
7656
7657 // Allocation of registers for this function. These are in callee save
7658 // registers and will be preserved by the call to the native RegExp code, as
7659 // this code is called using the normal C calling convention. When calling
7660 // directly from generated code the native RegExp code will not do a GC and
7661 // therefore the content of these registers are safe to use after the call.
7662 Register subject = r4;
7663 Register regexp_data = r5;
7664 Register last_match_info_elements = r6;
7665
7666 // Ensure that a RegExp stack is allocated.
7667 ExternalReference address_of_regexp_stack_memory_address =
7668 ExternalReference::address_of_regexp_stack_memory_address();
7669 ExternalReference address_of_regexp_stack_memory_size =
7670 ExternalReference::address_of_regexp_stack_memory_size();
7671 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
7672 __ ldr(r0, MemOperand(r0, 0));
7673 __ tst(r0, Operand(r0));
7674 __ b(eq, &runtime);
7675
7676 // Check that the first argument is a JSRegExp object.
7677 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
7678 ASSERT_EQ(0, kSmiTag);
7679 __ tst(r0, Operand(kSmiTagMask));
7680 __ b(eq, &runtime);
7681 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
7682 __ b(ne, &runtime);
7683
7684 // Check that the RegExp has been compiled (data contains a fixed array).
7685 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
7686 if (FLAG_debug_code) {
7687 __ tst(regexp_data, Operand(kSmiTagMask));
7688 __ Check(nz, "Unexpected type for RegExp data, FixedArray expected");
7689 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
7690 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
7691 }
7692
7693 // regexp_data: RegExp data (FixedArray)
7694 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
7695 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
7696 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
7697 __ b(ne, &runtime);
7698
7699 // regexp_data: RegExp data (FixedArray)
7700 // Check that the number of captures fit in the static offsets vector buffer.
7701 __ ldr(r2,
7702 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
7703 // Calculate number of capture registers (number_of_captures + 1) * 2. This
7704 // uses the asumption that smis are 2 * their untagged value.
7705 ASSERT_EQ(0, kSmiTag);
7706 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
7707 __ add(r2, r2, Operand(2)); // r2 was a smi.
7708 // Check that the static offsets vector buffer is large enough.
7709 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
7710 __ b(hi, &runtime);
7711
7712 // r2: Number of capture registers
7713 // regexp_data: RegExp data (FixedArray)
7714 // Check that the second argument is a string.
7715 __ ldr(subject, MemOperand(sp, kSubjectOffset));
7716 __ tst(subject, Operand(kSmiTagMask));
7717 __ b(eq, &runtime);
7718 Condition is_string = masm->IsObjectStringType(subject, r0);
7719 __ b(NegateCondition(is_string), &runtime);
7720 // Get the length of the string to r3.
7721 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
7722
7723 // r2: Number of capture registers
7724 // r3: Length of subject string
7725 // subject: Subject string
7726 // regexp_data: RegExp data (FixedArray)
7727 // Check that the third argument is a positive smi less than the subject
7728 // string length. A negative value will be greater (unsigned comparison).
7729 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
7730 __ cmp(r3, Operand(r0, ASR, kSmiTagSize + kSmiShiftSize));
7731 __ b(ls, &runtime);
7732
7733 // r2: Number of capture registers
7734 // subject: Subject string
7735 // regexp_data: RegExp data (FixedArray)
7736 // Check that the fourth object is a JSArray object.
7737 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
7738 __ tst(r0, Operand(kSmiTagMask));
7739 __ b(eq, &runtime);
7740 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
7741 __ b(ne, &runtime);
7742 // Check that the JSArray is in fast case.
7743 __ ldr(last_match_info_elements,
7744 FieldMemOperand(r0, JSArray::kElementsOffset));
7745 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
7746 __ cmp(r0, Operand(Factory::fixed_array_map()));
7747 __ b(ne, &runtime);
7748 // Check that the last match info has space for the capture registers and the
7749 // additional information.
7750 __ ldr(r0,
7751 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
7752 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
7753 __ cmp(r2, r0);
7754 __ b(gt, &runtime);
7755
7756 // subject: Subject string
7757 // regexp_data: RegExp data (FixedArray)
7758 // Check the representation and encoding of the subject string.
7759 Label seq_string;
7760 const int kStringRepresentationEncodingMask =
7761 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask;
7762 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
7763 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
7764 __ and_(r1, r0, Operand(kStringRepresentationEncodingMask));
7765 // First check for sequential string.
7766 ASSERT_EQ(0, kStringTag);
7767 ASSERT_EQ(0, kSeqStringTag);
7768 __ tst(r1, Operand(kIsNotStringMask | kStringRepresentationMask));
7769 __ b(eq, &seq_string);
7770
7771 // subject: Subject string
7772 // regexp_data: RegExp data (FixedArray)
7773 // Check for flat cons string.
7774 // A flat cons string is a cons string where the second part is the empty
7775 // string. In that case the subject string is just the first part of the cons
7776 // string. Also in this case the first part of the cons string is known to be
7777 // a sequential string or an external string.
7778 __ and_(r0, r0, Operand(kStringRepresentationMask));
7779 __ cmp(r0, Operand(kConsStringTag));
7780 __ b(ne, &runtime);
7781 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
7782 __ LoadRoot(r1, Heap::kEmptyStringRootIndex);
7783 __ cmp(r0, r1);
7784 __ b(ne, &runtime);
7785 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
7786 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
7787 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
7788 ASSERT_EQ(0, kSeqStringTag);
7789 __ tst(r0, Operand(kStringRepresentationMask));
7790 __ b(nz, &runtime);
7791 __ and_(r1, r0, Operand(kStringRepresentationEncodingMask));
7792
7793 __ bind(&seq_string);
7794 // r1: suject string type & kStringRepresentationEncodingMask
7795 // subject: Subject string
7796 // regexp_data: RegExp data (FixedArray)
7797 // Check that the irregexp code has been generated for an ascii string. If
7798 // it has, the field contains a code object otherwise it contains the hole.
7799#ifdef DEBUG
7800 const int kSeqAsciiString = kStringTag | kSeqStringTag | kAsciiStringTag;
7801 const int kSeqTwoByteString = kStringTag | kSeqStringTag | kTwoByteStringTag;
7802 CHECK_EQ(4, kSeqAsciiString);
7803 CHECK_EQ(0, kSeqTwoByteString);
7804#endif
7805 // Find the code object based on the assumptions above.
7806 __ mov(r3, Operand(r1, ASR, 2), SetCC);
7807 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
7808 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
7809
7810 // Check that the irregexp code has been generated for the actual string
7811 // encoding. If it has, the field contains a code object otherwise it contains
7812 // the hole.
7813 __ CompareObjectType(r7, r0, r0, CODE_TYPE);
7814 __ b(ne, &runtime);
7815
7816 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
7817 // r7: code
7818 // subject: Subject string
7819 // regexp_data: RegExp data (FixedArray)
7820 // Load used arguments before starting to push arguments for call to native
7821 // RegExp code to avoid handling changing stack height.
7822 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
7823 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
7824
7825 // r1: previous index
7826 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
7827 // r7: code
7828 // subject: Subject string
7829 // regexp_data: RegExp data (FixedArray)
7830 // All checks done. Now push arguments for native regexp code.
7831 __ IncrementCounter(&Counters::regexp_entry_native, 1, r0, r2);
7832
7833 static const int kRegExpExecuteArguments = 7;
7834 __ push(lr);
7835 __ PrepareCallCFunction(kRegExpExecuteArguments, r0);
7836
7837 // Argument 7 (sp[8]): Indicate that this is a direct call from JavaScript.
7838 __ mov(r0, Operand(1));
7839 __ str(r0, MemOperand(sp, 2 * kPointerSize));
7840
7841 // Argument 6 (sp[4]): Start (high end) of backtracking stack memory area.
7842 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
7843 __ ldr(r0, MemOperand(r0, 0));
7844 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
7845 __ ldr(r2, MemOperand(r2, 0));
7846 __ add(r0, r0, Operand(r2));
7847 __ str(r0, MemOperand(sp, 1 * kPointerSize));
7848
7849 // Argument 5 (sp[0]): static offsets vector buffer.
7850 __ mov(r0, Operand(ExternalReference::address_of_static_offsets_vector()));
7851 __ str(r0, MemOperand(sp, 0 * kPointerSize));
7852
7853 // For arguments 4 and 3 get string length, calculate start of string data and
7854 // calculate the shift of the index (0 for ASCII and 1 for two byte).
7855 __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset));
7856 ASSERT_EQ(SeqAsciiString::kHeaderSize, SeqTwoByteString::kHeaderSize);
7857 __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
7858 __ eor(r3, r3, Operand(1));
7859 // Argument 4 (r3): End of string data
7860 // Argument 3 (r2): Start of string data
7861 __ add(r2, r9, Operand(r1, LSL, r3));
7862 __ add(r3, r9, Operand(r0, LSL, r3));
7863
7864 // Argument 2 (r1): Previous index.
7865 // Already there
7866
7867 // Argument 1 (r0): Subject string.
7868 __ mov(r0, subject);
7869
7870 // Locate the code entry and call it.
7871 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
7872 __ CallCFunction(r7, kRegExpExecuteArguments);
7873 __ pop(lr);
7874
7875 // r0: result
7876 // subject: subject string (callee saved)
7877 // regexp_data: RegExp data (callee saved)
7878 // last_match_info_elements: Last match info elements (callee saved)
7879
7880 // Check the result.
7881 Label success;
7882 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
7883 __ b(eq, &success);
7884 Label failure;
7885 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
7886 __ b(eq, &failure);
7887 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
7888 // If not exception it can only be retry. Handle that in the runtime system.
7889 __ b(ne, &runtime);
7890 // Result must now be exception. If there is no pending exception already a
7891 // stack overflow (on the backtrack stack) was detected in RegExp code but
7892 // haven't created the exception yet. Handle that in the runtime system.
7893 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
7894 __ mov(r0, Operand(ExternalReference::the_hole_value_location()));
7895 __ ldr(r0, MemOperand(r0, 0));
7896 __ mov(r1, Operand(ExternalReference(Top::k_pending_exception_address)));
7897 __ ldr(r1, MemOperand(r1, 0));
7898 __ cmp(r0, r1);
7899 __ b(eq, &runtime);
7900 __ bind(&failure);
7901 // For failure and exception return null.
7902 __ mov(r0, Operand(Factory::null_value()));
7903 __ add(sp, sp, Operand(4 * kPointerSize));
7904 __ Ret();
7905
7906 // Process the result from the native regexp code.
7907 __ bind(&success);
7908 __ ldr(r1,
7909 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
7910 // Calculate number of capture registers (number_of_captures + 1) * 2.
7911 ASSERT_EQ(0, kSmiTag);
7912 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
7913 __ add(r1, r1, Operand(2)); // r1 was a smi.
7914
7915 // r1: number of capture registers
7916 // r4: subject string
7917 // Store the capture count.
7918 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
7919 __ str(r2, FieldMemOperand(last_match_info_elements,
7920 RegExpImpl::kLastCaptureCountOffset));
7921 // Store last subject and last input.
7922 __ mov(r3, last_match_info_elements); // Moved up to reduce latency.
7923 __ mov(r2, Operand(RegExpImpl::kLastSubjectOffset)); // Ditto.
7924 __ str(subject,
7925 FieldMemOperand(last_match_info_elements,
7926 RegExpImpl::kLastSubjectOffset));
7927 __ RecordWrite(r3, r2, r7);
7928 __ str(subject,
7929 FieldMemOperand(last_match_info_elements,
7930 RegExpImpl::kLastInputOffset));
7931 __ mov(r3, last_match_info_elements);
7932 __ mov(r2, Operand(RegExpImpl::kLastInputOffset));
7933 __ RecordWrite(r3, r2, r7);
7934
7935 // Get the static offsets vector filled by the native regexp code.
7936 ExternalReference address_of_static_offsets_vector =
7937 ExternalReference::address_of_static_offsets_vector();
7938 __ mov(r2, Operand(address_of_static_offsets_vector));
7939
7940 // r1: number of capture registers
7941 // r2: offsets vector
7942 Label next_capture, done;
7943 // Capture register counter starts from number of capture registers and
7944 // counts down until wraping after zero.
7945 __ add(r0,
7946 last_match_info_elements,
7947 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
7948 __ bind(&next_capture);
7949 __ sub(r1, r1, Operand(1), SetCC);
7950 __ b(mi, &done);
7951 // Read the value from the static offsets vector buffer.
7952 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
7953 // Store the smi value in the last match info.
7954 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
7955 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
7956 __ jmp(&next_capture);
7957 __ bind(&done);
7958
7959 // Return last match info.
7960 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
7961 __ add(sp, sp, Operand(4 * kPointerSize));
7962 __ Ret();
7963
7964 // Do the runtime call to execute the regexp.
7965 __ bind(&runtime);
7966 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
7967#endif // V8_NATIVE_REGEXP
7968}
7969
7970
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00007971void CallFunctionStub::Generate(MacroAssembler* masm) {
7972 Label slow;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00007973
7974 // If the receiver might be a value (string, number or boolean) check for this
7975 // and box it if it is.
7976 if (ReceiverMightBeValue()) {
7977 // Get the receiver from the stack.
7978 // function, receiver [, arguments]
7979 Label receiver_is_value, receiver_is_js_object;
7980 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
7981
7982 // Check if receiver is a smi (which is a number value).
7983 __ BranchOnSmi(r1, &receiver_is_value);
7984
7985 // Check if the receiver is a valid JS object.
7986 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
7987 __ b(ge, &receiver_is_js_object);
7988
7989 // Call the runtime to box the value.
7990 __ bind(&receiver_is_value);
7991 __ EnterInternalFrame();
7992 __ push(r1);
7993 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
7994 __ LeaveInternalFrame();
7995 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
7996
7997 __ bind(&receiver_is_js_object);
7998 }
7999
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008000 // Get the function to call from the stack.
8001 // function, receiver [, arguments]
8002 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
8003
8004 // Check that the function is really a JavaScript function.
8005 // r1: pushed function (to be verified)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008006 __ BranchOnSmi(r1, &slow);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008007 // Get the map of the function object.
ager@chromium.orgeadaf222009-06-16 09:43:10 +00008008 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008009 __ b(ne, &slow);
8010
8011 // Fast-case: Invoke the function now.
8012 // r1: pushed function
8013 ParameterCount actual(argc_);
8014 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
8015
8016 // Slow-case: Non-function called.
8017 __ bind(&slow);
ager@chromium.org5c838252010-02-19 08:53:10 +00008018 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
8019 // of the original receiver from the call site).
8020 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008021 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00008022 __ mov(r2, Operand(0));
8023 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
8024 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
8025 RelocInfo::CODE_TARGET);
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008026}
8027
8028
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008029// Unfortunately you have to run without snapshots to see most of these
8030// names in the profile since most compare stubs end up in the snapshot.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008031const char* CompareStub::GetName() {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008032 if (name_ != NULL) return name_;
8033 const int kMaxNameLength = 100;
8034 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
8035 if (name_ == NULL) return "OOM";
8036
8037 const char* cc_name;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008038 switch (cc_) {
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008039 case lt: cc_name = "LT"; break;
8040 case gt: cc_name = "GT"; break;
8041 case le: cc_name = "LE"; break;
8042 case ge: cc_name = "GE"; break;
8043 case eq: cc_name = "EQ"; break;
8044 case ne: cc_name = "NE"; break;
8045 default: cc_name = "UnknownCondition"; break;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008046 }
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008047
8048 const char* strict_name = "";
8049 if (strict_ && (cc_ == eq || cc_ == ne)) {
8050 strict_name = "_STRICT";
8051 }
8052
8053 const char* never_nan_nan_name = "";
8054 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
8055 never_nan_nan_name = "_NO_NAN";
8056 }
8057
8058 const char* include_number_compare_name = "";
8059 if (!include_number_compare_) {
8060 include_number_compare_name = "_NO_NUMBER";
8061 }
8062
8063 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
8064 "CompareStub_%s%s%s%s",
8065 cc_name,
8066 strict_name,
8067 never_nan_nan_name,
8068 include_number_compare_name);
8069 return name_;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00008070}
8071
8072
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008073int CompareStub::MinorKey() {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00008074 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
8075 // stubs the never NaN NaN condition is only taken into account if the
8076 // condition is equals.
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008077 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 13));
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00008078 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
8079 | StrictField::encode(strict_)
sgjesse@chromium.orgdf7a2842010-03-25 14:34:15 +00008080 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
8081 | IncludeNumberCompareField::encode(include_number_compare_);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00008082}
8083
8084
ager@chromium.org5c838252010-02-19 08:53:10 +00008085void StringStubBase::GenerateCopyCharacters(MacroAssembler* masm,
8086 Register dest,
8087 Register src,
8088 Register count,
8089 Register scratch,
8090 bool ascii) {
8091 Label loop;
8092 Label done;
8093 // This loop just copies one character at a time, as it is only used for very
8094 // short strings.
8095 if (!ascii) {
8096 __ add(count, count, Operand(count), SetCC);
8097 } else {
8098 __ cmp(count, Operand(0));
8099 }
8100 __ b(eq, &done);
8101
8102 __ bind(&loop);
8103 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
8104 // Perform sub between load and dependent store to get the load time to
8105 // complete.
8106 __ sub(count, count, Operand(1), SetCC);
8107 __ strb(scratch, MemOperand(dest, 1, PostIndex));
8108 // last iteration.
8109 __ b(gt, &loop);
8110
8111 __ bind(&done);
8112}
8113
8114
8115enum CopyCharactersFlags {
8116 COPY_ASCII = 1,
8117 DEST_ALWAYS_ALIGNED = 2
8118};
8119
8120
8121void StringStubBase::GenerateCopyCharactersLong(MacroAssembler* masm,
8122 Register dest,
8123 Register src,
8124 Register count,
8125 Register scratch1,
8126 Register scratch2,
8127 Register scratch3,
8128 Register scratch4,
8129 Register scratch5,
8130 int flags) {
8131 bool ascii = (flags & COPY_ASCII) != 0;
8132 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
8133
8134 if (dest_always_aligned && FLAG_debug_code) {
8135 // Check that destination is actually word aligned if the flag says
8136 // that it is.
8137 __ tst(dest, Operand(kPointerAlignmentMask));
8138 __ Check(eq, "Destination of copy not aligned.");
8139 }
8140
8141 const int kReadAlignment = 4;
8142 const int kReadAlignmentMask = kReadAlignment - 1;
8143 // Ensure that reading an entire aligned word containing the last character
8144 // of a string will not read outside the allocated area (because we pad up
8145 // to kObjectAlignment).
8146 ASSERT(kObjectAlignment >= kReadAlignment);
8147 // Assumes word reads and writes are little endian.
8148 // Nothing to do for zero characters.
8149 Label done;
8150 if (!ascii) {
8151 __ add(count, count, Operand(count), SetCC);
8152 } else {
8153 __ cmp(count, Operand(0));
8154 }
8155 __ b(eq, &done);
8156
8157 // Assume that you cannot read (or write) unaligned.
8158 Label byte_loop;
8159 // Must copy at least eight bytes, otherwise just do it one byte at a time.
8160 __ cmp(count, Operand(8));
8161 __ add(count, dest, Operand(count));
8162 Register limit = count; // Read until src equals this.
8163 __ b(lt, &byte_loop);
8164
8165 if (!dest_always_aligned) {
8166 // Align dest by byte copying. Copies between zero and three bytes.
8167 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
8168 Label dest_aligned;
8169 __ b(eq, &dest_aligned);
8170 __ cmp(scratch4, Operand(2));
8171 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
8172 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
8173 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
8174 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
8175 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
8176 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
8177 __ bind(&dest_aligned);
8178 }
8179
8180 Label simple_loop;
8181
8182 __ sub(scratch4, dest, Operand(src));
8183 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
8184 __ b(eq, &simple_loop);
8185 // Shift register is number of bits in a source word that
8186 // must be combined with bits in the next source word in order
8187 // to create a destination word.
8188
8189 // Complex loop for src/dst that are not aligned the same way.
8190 {
8191 Label loop;
8192 __ mov(scratch4, Operand(scratch4, LSL, 3));
8193 Register left_shift = scratch4;
8194 __ and_(src, src, Operand(~3)); // Round down to load previous word.
8195 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
8196 // Store the "shift" most significant bits of scratch in the least
8197 // signficant bits (i.e., shift down by (32-shift)).
8198 __ rsb(scratch2, left_shift, Operand(32));
8199 Register right_shift = scratch2;
8200 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
8201
8202 __ bind(&loop);
8203 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
8204 __ sub(scratch5, limit, Operand(dest));
8205 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
8206 __ str(scratch1, MemOperand(dest, 4, PostIndex));
8207 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
8208 // Loop if four or more bytes left to copy.
8209 // Compare to eight, because we did the subtract before increasing dst.
8210 __ sub(scratch5, scratch5, Operand(8), SetCC);
8211 __ b(ge, &loop);
8212 }
8213 // There is now between zero and three bytes left to copy (negative that
8214 // number is in scratch5), and between one and three bytes already read into
8215 // scratch1 (eight times that number in scratch4). We may have read past
8216 // the end of the string, but because objects are aligned, we have not read
8217 // past the end of the object.
8218 // Find the minimum of remaining characters to move and preloaded characters
8219 // and write those as bytes.
8220 __ add(scratch5, scratch5, Operand(4), SetCC);
8221 __ b(eq, &done);
8222 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
8223 // Move minimum of bytes read and bytes left to copy to scratch4.
8224 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
8225 // Between one and three (value in scratch5) characters already read into
8226 // scratch ready to write.
8227 __ cmp(scratch5, Operand(2));
8228 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
8229 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
8230 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
8231 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
8232 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
8233 // Copy any remaining bytes.
8234 __ b(&byte_loop);
8235
8236 // Simple loop.
8237 // Copy words from src to dst, until less than four bytes left.
8238 // Both src and dest are word aligned.
8239 __ bind(&simple_loop);
8240 {
8241 Label loop;
8242 __ bind(&loop);
8243 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
8244 __ sub(scratch3, limit, Operand(dest));
8245 __ str(scratch1, MemOperand(dest, 4, PostIndex));
8246 // Compare to 8, not 4, because we do the substraction before increasing
8247 // dest.
8248 __ cmp(scratch3, Operand(8));
8249 __ b(ge, &loop);
8250 }
8251
8252 // Copy bytes from src to dst until dst hits limit.
8253 __ bind(&byte_loop);
8254 __ cmp(dest, Operand(limit));
8255 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
8256 __ b(ge, &done);
8257 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
8258 __ b(&byte_loop);
8259
8260 __ bind(&done);
8261}
8262
8263
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008264void StringStubBase::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
8265 Register c1,
8266 Register c2,
8267 Register scratch1,
8268 Register scratch2,
8269 Register scratch3,
8270 Register scratch4,
8271 Register scratch5,
8272 Label* not_found) {
8273 // Register scratch3 is the general scratch register in this function.
8274 Register scratch = scratch3;
8275
8276 // Make sure that both characters are not digits as such strings has a
8277 // different hash algorithm. Don't try to look for these in the symbol table.
8278 Label not_array_index;
8279 __ sub(scratch, c1, Operand(static_cast<int>('0')));
8280 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
8281 __ b(hi, &not_array_index);
8282 __ sub(scratch, c2, Operand(static_cast<int>('0')));
8283 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
8284
8285 // If check failed combine both characters into single halfword.
8286 // This is required by the contract of the method: code at the
8287 // not_found branch expects this combination in c1 register
8288 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
8289 __ b(ls, not_found);
8290
8291 __ bind(&not_array_index);
8292 // Calculate the two character string hash.
8293 Register hash = scratch1;
8294 GenerateHashInit(masm, hash, c1);
8295 GenerateHashAddCharacter(masm, hash, c2);
8296 GenerateHashGetHash(masm, hash);
8297
8298 // Collect the two characters in a register.
8299 Register chars = c1;
8300 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
8301
8302 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
8303 // hash: hash of two character string.
8304
8305 // Load symbol table
8306 // Load address of first element of the symbol table.
8307 Register symbol_table = c2;
8308 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
8309
8310 // Load undefined value
8311 Register undefined = scratch4;
8312 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
8313
8314 // Calculate capacity mask from the symbol table capacity.
8315 Register mask = scratch2;
8316 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
8317 __ mov(mask, Operand(mask, ASR, 1));
8318 __ sub(mask, mask, Operand(1));
8319
8320 // Calculate untagged address of the first element of the symbol table.
8321 Register first_symbol_table_element = symbol_table;
8322 __ add(first_symbol_table_element, symbol_table,
8323 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
8324
8325 // Registers
8326 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
8327 // hash: hash of two character string
8328 // mask: capacity mask
8329 // first_symbol_table_element: address of the first element of
8330 // the symbol table
8331 // scratch: -
8332
8333 // Perform a number of probes in the symbol table.
8334 static const int kProbes = 4;
8335 Label found_in_symbol_table;
8336 Label next_probe[kProbes];
8337 for (int i = 0; i < kProbes; i++) {
8338 Register candidate = scratch5; // Scratch register contains candidate.
8339
8340 // Calculate entry in symbol table.
8341 if (i > 0) {
8342 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
8343 } else {
8344 __ mov(candidate, hash);
8345 }
8346
8347 __ and_(candidate, candidate, Operand(mask));
8348
8349 // Load the entry from the symble table.
8350 ASSERT_EQ(1, SymbolTable::kEntrySize);
8351 __ ldr(candidate,
8352 MemOperand(first_symbol_table_element,
8353 candidate,
8354 LSL,
8355 kPointerSizeLog2));
8356
8357 // If entry is undefined no string with this hash can be found.
8358 __ cmp(candidate, undefined);
8359 __ b(eq, not_found);
8360
8361 // If length is not 2 the string is not a candidate.
8362 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
8363 __ cmp(scratch, Operand(2));
8364 __ b(ne, &next_probe[i]);
8365
8366 // Check that the candidate is a non-external ascii string.
8367 __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset));
8368 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
8369 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch,
8370 &next_probe[i]);
8371
8372 // Check if the two characters match.
8373 // Assumes that word load is little endian.
8374 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
8375 __ cmp(chars, scratch);
8376 __ b(eq, &found_in_symbol_table);
8377 __ bind(&next_probe[i]);
8378 }
8379
8380 // No matching 2 character string found by probing.
8381 __ jmp(not_found);
8382
8383 // Scratch register contains result when we fall through to here.
8384 Register result = scratch;
8385 __ bind(&found_in_symbol_table);
ager@chromium.org357bf652010-04-12 11:30:10 +00008386 __ Move(r0, result);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008387}
8388
8389
8390void StringStubBase::GenerateHashInit(MacroAssembler* masm,
8391 Register hash,
8392 Register character) {
8393 // hash = character + (character << 10);
8394 __ add(hash, character, Operand(character, LSL, 10));
8395 // hash ^= hash >> 6;
8396 __ eor(hash, hash, Operand(hash, ASR, 6));
8397}
8398
8399
8400void StringStubBase::GenerateHashAddCharacter(MacroAssembler* masm,
8401 Register hash,
8402 Register character) {
8403 // hash += character;
8404 __ add(hash, hash, Operand(character));
8405 // hash += hash << 10;
8406 __ add(hash, hash, Operand(hash, LSL, 10));
8407 // hash ^= hash >> 6;
8408 __ eor(hash, hash, Operand(hash, ASR, 6));
8409}
8410
8411
8412void StringStubBase::GenerateHashGetHash(MacroAssembler* masm,
8413 Register hash) {
8414 // hash += hash << 3;
8415 __ add(hash, hash, Operand(hash, LSL, 3));
8416 // hash ^= hash >> 11;
8417 __ eor(hash, hash, Operand(hash, ASR, 11));
8418 // hash += hash << 15;
8419 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
8420
8421 // if (hash == 0) hash = 27;
8422 __ mov(hash, Operand(27), LeaveCC, nz);
8423}
8424
8425
ager@chromium.org5c838252010-02-19 08:53:10 +00008426void SubStringStub::Generate(MacroAssembler* masm) {
8427 Label runtime;
8428
8429 // Stack frame on entry.
8430 // lr: return address
8431 // sp[0]: to
8432 // sp[4]: from
8433 // sp[8]: string
8434
8435 // This stub is called from the native-call %_SubString(...), so
8436 // nothing can be assumed about the arguments. It is tested that:
8437 // "string" is a sequential string,
8438 // both "from" and "to" are smis, and
8439 // 0 <= from <= to <= string.length.
8440 // If any of these assumptions fail, we call the runtime system.
8441
8442 static const int kToOffset = 0 * kPointerSize;
8443 static const int kFromOffset = 1 * kPointerSize;
8444 static const int kStringOffset = 2 * kPointerSize;
8445
8446
8447 // Check bounds and smi-ness.
8448 __ ldr(r7, MemOperand(sp, kToOffset));
8449 __ ldr(r6, MemOperand(sp, kFromOffset));
8450 ASSERT_EQ(0, kSmiTag);
8451 ASSERT_EQ(1, kSmiTagSize + kSmiShiftSize);
8452 // I.e., arithmetic shift right by one un-smi-tags.
8453 __ mov(r2, Operand(r7, ASR, 1), SetCC);
8454 __ mov(r3, Operand(r6, ASR, 1), SetCC, cc);
8455 // If either r2 or r6 had the smi tag bit set, then carry is set now.
8456 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
8457 __ b(mi, &runtime); // From is negative.
8458
8459 __ sub(r2, r2, Operand(r3), SetCC);
8460 __ b(mi, &runtime); // Fail if from > to.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008461 // Special handling of sub-strings of length 1 and 2. One character strings
8462 // are handled in the runtime system (looked up in the single character
8463 // cache). Two character strings are looked for in the symbol cache.
ager@chromium.org5c838252010-02-19 08:53:10 +00008464 __ cmp(r2, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008465 __ b(lt, &runtime);
ager@chromium.org5c838252010-02-19 08:53:10 +00008466
8467 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008468 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00008469 // r6: from (smi)
8470 // r7: to (smi)
8471
8472 // Make sure first argument is a sequential (or flat) string.
8473 __ ldr(r5, MemOperand(sp, kStringOffset));
8474 ASSERT_EQ(0, kSmiTag);
8475 __ tst(r5, Operand(kSmiTagMask));
8476 __ b(eq, &runtime);
8477 Condition is_string = masm->IsObjectStringType(r5, r1);
8478 __ b(NegateCondition(is_string), &runtime);
8479
8480 // r1: instance type
8481 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008482 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00008483 // r5: string
8484 // r6: from (smi)
8485 // r7: to (smi)
8486 Label seq_string;
8487 __ and_(r4, r1, Operand(kStringRepresentationMask));
8488 ASSERT(kSeqStringTag < kConsStringTag);
8489 ASSERT(kExternalStringTag > kConsStringTag);
8490 __ cmp(r4, Operand(kConsStringTag));
8491 __ b(gt, &runtime); // External strings go to runtime.
8492 __ b(lt, &seq_string); // Sequential strings are handled directly.
8493
8494 // Cons string. Try to recurse (once) on the first substring.
8495 // (This adds a little more generality than necessary to handle flattened
8496 // cons strings, but not much).
8497 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
8498 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
8499 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
8500 __ tst(r1, Operand(kStringRepresentationMask));
8501 ASSERT_EQ(0, kSeqStringTag);
8502 __ b(ne, &runtime); // Cons and External strings go to runtime.
8503
8504 // Definitly a sequential string.
8505 __ bind(&seq_string);
8506
8507 // r1: instance type.
8508 // r2: length
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008509 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00008510 // r5: string
8511 // r6: from (smi)
8512 // r7: to (smi)
8513 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
8514 __ cmp(r4, Operand(r7, ASR, 1));
8515 __ b(lt, &runtime); // Fail if to > length.
8516
8517 // r1: instance type.
8518 // r2: result string length.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008519 // r3: from index (untaged smi)
ager@chromium.org5c838252010-02-19 08:53:10 +00008520 // r5: string.
8521 // r6: from offset (smi)
8522 // Check for flat ascii string.
8523 Label non_ascii_flat;
8524 __ tst(r1, Operand(kStringEncodingMask));
8525 ASSERT_EQ(0, kTwoByteStringTag);
8526 __ b(eq, &non_ascii_flat);
8527
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008528 Label result_longer_than_two;
8529 __ cmp(r2, Operand(2));
8530 __ b(gt, &result_longer_than_two);
8531
8532 // Sub string of length 2 requested.
8533 // Get the two characters forming the sub string.
8534 __ add(r5, r5, Operand(r3));
8535 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
8536 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
8537
8538 // Try to lookup two character string in symbol table.
8539 Label make_two_character_string;
8540 GenerateTwoCharacterSymbolTableProbe(masm, r3, r4, r1, r5, r6, r7, r9,
8541 &make_two_character_string);
8542 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
8543 __ add(sp, sp, Operand(3 * kPointerSize));
8544 __ Ret();
8545
8546 // r2: result string length.
8547 // r3: two characters combined into halfword in little endian byte order.
8548 __ bind(&make_two_character_string);
8549 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
8550 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
8551 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
8552 __ add(sp, sp, Operand(3 * kPointerSize));
8553 __ Ret();
8554
8555 __ bind(&result_longer_than_two);
8556
ager@chromium.org5c838252010-02-19 08:53:10 +00008557 // Allocate the result.
8558 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
8559
8560 // r0: result string.
8561 // r2: result string length.
8562 // r5: string.
8563 // r6: from offset (smi)
8564 // Locate first character of result.
8565 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
8566 // Locate 'from' character of string.
8567 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
8568 __ add(r5, r5, Operand(r6, ASR, 1));
8569
8570 // r0: result string.
8571 // r1: first character of result string.
8572 // r2: result string length.
8573 // r5: first character of sub string to copy.
8574 ASSERT_EQ(0, SeqAsciiString::kHeaderSize & kObjectAlignmentMask);
8575 GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
8576 COPY_ASCII | DEST_ALWAYS_ALIGNED);
8577 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
8578 __ add(sp, sp, Operand(3 * kPointerSize));
8579 __ Ret();
8580
8581 __ bind(&non_ascii_flat);
8582 // r2: result string length.
8583 // r5: string.
8584 // r6: from offset (smi)
8585 // Check for flat two byte string.
8586
8587 // Allocate the result.
8588 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
8589
8590 // r0: result string.
8591 // r2: result string length.
8592 // r5: string.
8593 // Locate first character of result.
8594 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
8595 // Locate 'from' character of string.
8596 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
8597 // As "from" is a smi it is 2 times the value which matches the size of a two
8598 // byte character.
8599 __ add(r5, r5, Operand(r6));
8600
8601 // r0: result string.
8602 // r1: first character of result.
8603 // r2: result length.
8604 // r5: first character of string to copy.
8605 ASSERT_EQ(0, SeqTwoByteString::kHeaderSize & kObjectAlignmentMask);
8606 GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
8607 DEST_ALWAYS_ALIGNED);
8608 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
8609 __ add(sp, sp, Operand(3 * kPointerSize));
8610 __ Ret();
8611
8612 // Just jump to runtime to create the sub string.
8613 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008614 __ TailCallRuntime(Runtime::kSubString, 3, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00008615}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008616
8617
8618void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
8619 Register left,
8620 Register right,
8621 Register scratch1,
8622 Register scratch2,
8623 Register scratch3,
8624 Register scratch4) {
8625 Label compare_lengths;
8626 // Find minimum length and length difference.
8627 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
8628 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
8629 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
8630 Register length_delta = scratch3;
8631 __ mov(scratch1, scratch2, LeaveCC, gt);
8632 Register min_length = scratch1;
8633 __ tst(min_length, Operand(min_length));
8634 __ b(eq, &compare_lengths);
8635
8636 // Setup registers so that we only need to increment one register
8637 // in the loop.
8638 __ add(scratch2, min_length,
8639 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
8640 __ add(left, left, Operand(scratch2));
8641 __ add(right, right, Operand(scratch2));
8642 // Registers left and right points to the min_length character of strings.
8643 __ rsb(min_length, min_length, Operand(-1));
8644 Register index = min_length;
8645 // Index starts at -min_length.
8646
8647 {
8648 // Compare loop.
8649 Label loop;
8650 __ bind(&loop);
8651 // Compare characters.
8652 __ add(index, index, Operand(1), SetCC);
8653 __ ldrb(scratch2, MemOperand(left, index), ne);
8654 __ ldrb(scratch4, MemOperand(right, index), ne);
8655 // Skip to compare lengths with eq condition true.
8656 __ b(eq, &compare_lengths);
8657 __ cmp(scratch2, scratch4);
8658 __ b(eq, &loop);
8659 // Fallthrough with eq condition false.
8660 }
8661 // Compare lengths - strings up to min-length are equal.
8662 __ bind(&compare_lengths);
8663 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
8664 // Use zero length_delta as result.
8665 __ mov(r0, Operand(length_delta), SetCC, eq);
8666 // Fall through to here if characters compare not-equal.
8667 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
8668 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
8669 __ Ret();
8670}
8671
8672
8673void StringCompareStub::Generate(MacroAssembler* masm) {
8674 Label runtime;
8675
8676 // Stack frame on entry.
ager@chromium.org5c838252010-02-19 08:53:10 +00008677 // sp[0]: right string
8678 // sp[4]: left string
8679 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left
8680 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008681
8682 Label not_same;
8683 __ cmp(r0, r1);
8684 __ b(ne, &not_same);
8685 ASSERT_EQ(0, EQUAL);
8686 ASSERT_EQ(0, kSmiTag);
8687 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
8688 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
8689 __ add(sp, sp, Operand(2 * kPointerSize));
8690 __ Ret();
8691
8692 __ bind(&not_same);
8693
8694 // Check that both objects are sequential ascii strings.
8695 __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime);
8696
8697 // Compare flat ascii strings natively. Remove arguments from stack first.
8698 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
8699 __ add(sp, sp, Operand(2 * kPointerSize));
8700 GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5);
8701
8702 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
8703 // tagged as a small integer.
8704 __ bind(&runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008705 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00008706}
8707
8708
ager@chromium.org5c838252010-02-19 08:53:10 +00008709void StringAddStub::Generate(MacroAssembler* masm) {
8710 Label string_add_runtime;
8711 // Stack on entry:
8712 // sp[0]: second argument.
8713 // sp[4]: first argument.
8714
8715 // Load the two arguments.
8716 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
8717 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
8718
8719 // Make sure that both arguments are strings if not known in advance.
8720 if (string_check_) {
8721 ASSERT_EQ(0, kSmiTag);
8722 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
8723 // Load instance types.
8724 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
8725 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
8726 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
8727 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
8728 ASSERT_EQ(0, kStringTag);
8729 // If either is not a string, go to runtime.
8730 __ tst(r4, Operand(kIsNotStringMask));
8731 __ tst(r5, Operand(kIsNotStringMask), eq);
8732 __ b(ne, &string_add_runtime);
8733 }
8734
8735 // Both arguments are strings.
8736 // r0: first string
8737 // r1: second string
8738 // r4: first string instance type (if string_check_)
8739 // r5: second string instance type (if string_check_)
8740 {
8741 Label strings_not_empty;
8742 // Check if either of the strings are empty. In that case return the other.
8743 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
8744 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
8745 __ cmp(r2, Operand(0)); // Test if first string is empty.
8746 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
8747 __ cmp(r3, Operand(0), ne); // Else test if second string is empty.
8748 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
8749
8750 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
8751 __ add(sp, sp, Operand(2 * kPointerSize));
8752 __ Ret();
8753
8754 __ bind(&strings_not_empty);
8755 }
8756
8757 // Both strings are non-empty.
8758 // r0: first string
8759 // r1: second string
8760 // r2: length of first string
8761 // r3: length of second string
8762 // r4: first string instance type (if string_check_)
8763 // r5: second string instance type (if string_check_)
8764 // Look at the length of the result of adding the two strings.
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008765 Label string_add_flat_result, longer_than_two;
ager@chromium.org5c838252010-02-19 08:53:10 +00008766 // Adding two lengths can't overflow.
8767 ASSERT(String::kMaxLength * 2 > String::kMaxLength);
8768 __ add(r6, r2, Operand(r3));
8769 // Use the runtime system when adding two one character strings, as it
8770 // contains optimizations for this specific case using the symbol table.
8771 __ cmp(r6, Operand(2));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008772 __ b(ne, &longer_than_two);
8773
8774 // Check that both strings are non-external ascii strings.
8775 if (!string_check_) {
8776 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
8777 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
8778 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
8779 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
8780 }
8781 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
8782 &string_add_runtime);
8783
8784 // Get the two characters forming the sub string.
8785 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
8786 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
8787
8788 // Try to lookup two character string in symbol table. If it is not found
8789 // just allocate a new one.
8790 Label make_two_character_string;
8791 GenerateTwoCharacterSymbolTableProbe(masm, r2, r3, r6, r7, r4, r5, r9,
8792 &make_two_character_string);
8793 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
8794 __ add(sp, sp, Operand(2 * kPointerSize));
8795 __ Ret();
8796
8797 __ bind(&make_two_character_string);
8798 // Resulting string has length 2 and first chars of two strings
8799 // are combined into single halfword in r2 register.
8800 // So we can fill resulting string without two loops by a single
8801 // halfword store instruction (which assumes that processor is
8802 // in a little endian mode)
8803 __ mov(r6, Operand(2));
8804 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
8805 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
8806 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
8807 __ add(sp, sp, Operand(2 * kPointerSize));
8808 __ Ret();
8809
8810 __ bind(&longer_than_two);
ager@chromium.org5c838252010-02-19 08:53:10 +00008811 // Check if resulting string will be flat.
8812 __ cmp(r6, Operand(String::kMinNonFlatLength));
8813 __ b(lt, &string_add_flat_result);
8814 // Handle exceptionally long strings in the runtime system.
8815 ASSERT((String::kMaxLength & 0x80000000) == 0);
8816 ASSERT(IsPowerOf2(String::kMaxLength + 1));
8817 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
8818 __ cmp(r6, Operand(String::kMaxLength + 1));
8819 __ b(hs, &string_add_runtime);
8820
8821 // If result is not supposed to be flat, allocate a cons string object.
8822 // If both strings are ascii the result is an ascii cons string.
8823 if (!string_check_) {
8824 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
8825 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
8826 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
8827 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
8828 }
8829 Label non_ascii, allocated;
8830 ASSERT_EQ(0, kTwoByteStringTag);
8831 __ tst(r4, Operand(kStringEncodingMask));
8832 __ tst(r5, Operand(kStringEncodingMask), ne);
8833 __ b(eq, &non_ascii);
8834
8835 // Allocate an ASCII cons string.
8836 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
8837 __ bind(&allocated);
8838 // Fill the fields of the cons string.
8839 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
8840 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
8841 __ mov(r0, Operand(r7));
8842 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
8843 __ add(sp, sp, Operand(2 * kPointerSize));
8844 __ Ret();
8845
8846 __ bind(&non_ascii);
8847 // Allocate a two byte cons string.
8848 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
8849 __ jmp(&allocated);
8850
8851 // Handle creating a flat result. First check that both strings are
8852 // sequential and that they have the same encoding.
8853 // r0: first string
8854 // r1: second string
8855 // r2: length of first string
8856 // r3: length of second string
8857 // r4: first string instance type (if string_check_)
8858 // r5: second string instance type (if string_check_)
8859 // r6: sum of lengths.
8860 __ bind(&string_add_flat_result);
8861 if (!string_check_) {
8862 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
8863 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
8864 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
8865 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
8866 }
8867 // Check that both strings are sequential.
8868 ASSERT_EQ(0, kSeqStringTag);
8869 __ tst(r4, Operand(kStringRepresentationMask));
8870 __ tst(r5, Operand(kStringRepresentationMask), eq);
8871 __ b(ne, &string_add_runtime);
8872 // Now check if both strings have the same encoding (ASCII/Two-byte).
8873 // r0: first string.
8874 // r1: second string.
8875 // r2: length of first string.
8876 // r3: length of second string.
8877 // r6: sum of lengths..
8878 Label non_ascii_string_add_flat_result;
8879 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
8880 __ eor(r7, r4, Operand(r5));
8881 __ tst(r7, Operand(kStringEncodingMask));
8882 __ b(ne, &string_add_runtime);
8883 // And see if it's ASCII or two-byte.
8884 __ tst(r4, Operand(kStringEncodingMask));
8885 __ b(eq, &non_ascii_string_add_flat_result);
8886
8887 // Both strings are sequential ASCII strings. We also know that they are
8888 // short (since the sum of the lengths is less than kMinNonFlatLength).
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008889 // r6: length of resulting flat string
ager@chromium.org5c838252010-02-19 08:53:10 +00008890 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
8891 // Locate first character of result.
8892 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
8893 // Locate first character of first argument.
8894 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
8895 // r0: first character of first string.
8896 // r1: second string.
8897 // r2: length of first string.
8898 // r3: length of second string.
8899 // r6: first character of result.
8900 // r7: result string.
8901 GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
8902
8903 // Load second argument and locate first character.
8904 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
8905 // r1: first character of second string.
8906 // r3: length of second string.
8907 // r6: next character of result.
8908 // r7: result string.
8909 GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
8910 __ mov(r0, Operand(r7));
8911 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
8912 __ add(sp, sp, Operand(2 * kPointerSize));
8913 __ Ret();
8914
8915 __ bind(&non_ascii_string_add_flat_result);
8916 // Both strings are sequential two byte strings.
8917 // r0: first string.
8918 // r1: second string.
8919 // r2: length of first string.
8920 // r3: length of second string.
8921 // r6: sum of length of strings.
8922 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
8923 // r0: first string.
8924 // r1: second string.
8925 // r2: length of first string.
8926 // r3: length of second string.
8927 // r7: result string.
8928
8929 // Locate first character of result.
8930 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
8931 // Locate first character of first argument.
8932 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
8933
8934 // r0: first character of first string.
8935 // r1: second string.
8936 // r2: length of first string.
8937 // r3: length of second string.
8938 // r6: first character of result.
8939 // r7: result string.
8940 GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
8941
8942 // Locate first character of second argument.
8943 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
8944
8945 // r1: first character of second string.
8946 // r3: length of second string.
8947 // r6: next character of result (after copy of first string).
8948 // r7: result string.
8949 GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
8950
8951 __ mov(r0, Operand(r7));
8952 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
8953 __ add(sp, sp, Operand(2 * kPointerSize));
8954 __ Ret();
8955
8956 // Just jump to runtime to add the two strings.
8957 __ bind(&string_add_runtime);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00008958 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00008959}
8960
8961
kasperl@chromium.org41044eb2008-10-06 08:24:46 +00008962#undef __
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00008963
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00008964} } // namespace v8::internal